blob: 09be40cf4a7a9264267c7604b55d276703da9b29 [file] [log] [blame]
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +00001//===-- MSP430ISelLowering.cpp - MSP430 DAG Lowering Implementation ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the MSP430TargetLowering class.
11//
12//===----------------------------------------------------------------------===//
13
14#define DEBUG_TYPE "msp430-lower"
15
16#include "MSP430ISelLowering.h"
17#include "MSP430.h"
18#include "MSP430TargetMachine.h"
19#include "MSP430Subtarget.h"
20#include "llvm/DerivedTypes.h"
21#include "llvm/Function.h"
22#include "llvm/Intrinsics.h"
23#include "llvm/CallingConv.h"
24#include "llvm/GlobalVariable.h"
25#include "llvm/GlobalAlias.h"
26#include "llvm/CodeGen/CallingConvLower.h"
27#include "llvm/CodeGen/MachineFrameInfo.h"
28#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/CodeGen/MachineRegisterInfo.h"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000031#include "llvm/CodeGen/PseudoSourceValue.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000032#include "llvm/CodeGen/SelectionDAGISel.h"
33#include "llvm/CodeGen/ValueTypes.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000034#include "llvm/Target/TargetLoweringObjectFile.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000035#include "llvm/Support/Debug.h"
Torok Edwin804e0fe2009-07-08 19:04:27 +000036#include "llvm/Support/ErrorHandling.h"
Chris Lattner4437ae22009-08-23 07:05:07 +000037#include "llvm/Support/raw_ostream.h"
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000038#include "llvm/ADT/VectorExtras.h"
39using namespace llvm;
40
41MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
Chris Lattnerf0144122009-07-28 03:13:23 +000042 TargetLowering(tm, new TargetLoweringObjectFileELF()),
43 Subtarget(*tm.getSubtargetImpl()), TM(tm) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000044
45 // Set up the register classes.
Owen Anderson825b72b2009-08-11 20:47:22 +000046 addRegisterClass(MVT::i8, MSP430::GR8RegisterClass);
47 addRegisterClass(MVT::i16, MSP430::GR16RegisterClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000048
49 // Compute derived properties from the register classes
50 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000051
Anton Korobeynikov1476d972009-05-03 13:03:14 +000052 // Provide all sorts of operation actions
53
54 // Division is expensive
55 setIntDivIsCheap(false);
56
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000057 // Even if we have only 1 bit shift here, we can perform
58 // shifts of the whole bitwidth 1 bit per step.
Owen Anderson825b72b2009-08-11 20:47:22 +000059 setShiftAmountType(MVT::i8);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000060
Anton Korobeynikovc08163e2009-05-03 13:11:35 +000061 setStackPointerRegisterToSaveRestore(MSP430::SPW);
62 setBooleanContents(ZeroOrOneBooleanContent);
63 setSchedulingPreference(SchedulingForLatency);
64
Owen Anderson825b72b2009-08-11 20:47:22 +000065 setLoadExtAction(ISD::EXTLOAD, MVT::i1, Promote);
66 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
67 setLoadExtAction(ISD::ZEXTLOAD, MVT::i1, Promote);
68 setLoadExtAction(ISD::SEXTLOAD, MVT::i8, Expand);
69 setLoadExtAction(ISD::SEXTLOAD, MVT::i16, Expand);
Anton Korobeynikov36b6e532009-05-03 13:06:03 +000070
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000071 // We don't have any truncstores
Owen Anderson825b72b2009-08-11 20:47:22 +000072 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
Anton Korobeynikov54f30d32009-05-03 13:06:26 +000073
Owen Anderson825b72b2009-08-11 20:47:22 +000074 setOperationAction(ISD::SRA, MVT::i8, Custom);
75 setOperationAction(ISD::SHL, MVT::i8, Custom);
76 setOperationAction(ISD::SRL, MVT::i8, Custom);
77 setOperationAction(ISD::SRA, MVT::i16, Custom);
78 setOperationAction(ISD::SHL, MVT::i16, Custom);
79 setOperationAction(ISD::SRL, MVT::i16, Custom);
80 setOperationAction(ISD::ROTL, MVT::i8, Expand);
81 setOperationAction(ISD::ROTR, MVT::i8, Expand);
82 setOperationAction(ISD::ROTL, MVT::i16, Expand);
83 setOperationAction(ISD::ROTR, MVT::i16, Expand);
84 setOperationAction(ISD::GlobalAddress, MVT::i16, Custom);
85 setOperationAction(ISD::ExternalSymbol, MVT::i16, Custom);
86 setOperationAction(ISD::BR_JT, MVT::Other, Expand);
87 setOperationAction(ISD::BRIND, MVT::Other, Expand);
88 setOperationAction(ISD::BR_CC, MVT::i8, Custom);
89 setOperationAction(ISD::BR_CC, MVT::i16, Custom);
90 setOperationAction(ISD::BRCOND, MVT::Other, Expand);
91 setOperationAction(ISD::SETCC, MVT::i8, Expand);
92 setOperationAction(ISD::SETCC, MVT::i16, Expand);
93 setOperationAction(ISD::SELECT, MVT::i8, Expand);
94 setOperationAction(ISD::SELECT, MVT::i16, Expand);
95 setOperationAction(ISD::SELECT_CC, MVT::i8, Custom);
96 setOperationAction(ISD::SELECT_CC, MVT::i16, Custom);
97 setOperationAction(ISD::SIGN_EXTEND, MVT::i16, Custom);
Anton Korobeynikov379a0872009-08-25 17:00:23 +000098 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i8, Expand);
99 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i16, Expand);
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000100
Owen Anderson825b72b2009-08-11 20:47:22 +0000101 setOperationAction(ISD::CTTZ, MVT::i8, Expand);
102 setOperationAction(ISD::CTTZ, MVT::i16, Expand);
103 setOperationAction(ISD::CTLZ, MVT::i8, Expand);
104 setOperationAction(ISD::CTLZ, MVT::i16, Expand);
105 setOperationAction(ISD::CTPOP, MVT::i8, Expand);
106 setOperationAction(ISD::CTPOP, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000107
Owen Anderson825b72b2009-08-11 20:47:22 +0000108 setOperationAction(ISD::SHL_PARTS, MVT::i8, Expand);
109 setOperationAction(ISD::SHL_PARTS, MVT::i16, Expand);
110 setOperationAction(ISD::SRL_PARTS, MVT::i8, Expand);
111 setOperationAction(ISD::SRL_PARTS, MVT::i16, Expand);
112 setOperationAction(ISD::SRA_PARTS, MVT::i8, Expand);
113 setOperationAction(ISD::SRA_PARTS, MVT::i16, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000114
Owen Anderson825b72b2009-08-11 20:47:22 +0000115 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1, Expand);
Eli Friedmane4ce8802009-07-17 07:28:06 +0000116
Anton Korobeynikov8725bd22009-05-03 13:14:25 +0000117 // FIXME: Implement efficiently multiplication by a constant
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000118 setOperationAction(ISD::MUL, MVT::i8, Expand);
119 setOperationAction(ISD::MULHS, MVT::i8, Expand);
120 setOperationAction(ISD::MULHU, MVT::i8, Expand);
121 setOperationAction(ISD::SMUL_LOHI, MVT::i8, Expand);
122 setOperationAction(ISD::UMUL_LOHI, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000123 setOperationAction(ISD::MUL, MVT::i16, Expand);
124 setOperationAction(ISD::MULHS, MVT::i16, Expand);
125 setOperationAction(ISD::MULHU, MVT::i16, Expand);
126 setOperationAction(ISD::SMUL_LOHI, MVT::i16, Expand);
127 setOperationAction(ISD::UMUL_LOHI, MVT::i16, Expand);
Anton Korobeynikovf2f54022009-05-03 13:18:33 +0000128
Anton Korobeynikov8983da72009-11-07 17:14:39 +0000129 setOperationAction(ISD::UDIV, MVT::i8, Expand);
130 setOperationAction(ISD::UDIVREM, MVT::i8, Expand);
131 setOperationAction(ISD::UREM, MVT::i8, Expand);
132 setOperationAction(ISD::SDIV, MVT::i8, Expand);
133 setOperationAction(ISD::SDIVREM, MVT::i8, Expand);
134 setOperationAction(ISD::SREM, MVT::i8, Expand);
Owen Anderson825b72b2009-08-11 20:47:22 +0000135 setOperationAction(ISD::UDIV, MVT::i16, Expand);
136 setOperationAction(ISD::UDIVREM, MVT::i16, Expand);
137 setOperationAction(ISD::UREM, MVT::i16, Expand);
138 setOperationAction(ISD::SDIV, MVT::i16, Expand);
139 setOperationAction(ISD::SDIVREM, MVT::i16, Expand);
140 setOperationAction(ISD::SREM, MVT::i16, Expand);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000141}
142
Anton Korobeynikovb8639f52009-05-03 13:03:50 +0000143SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000144 switch (Op.getOpcode()) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000145 case ISD::SHL: // FALLTHROUGH
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000146 case ISD::SRL:
Anton Korobeynikov44288852009-05-03 13:07:31 +0000147 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000148 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000149 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000150 case ISD::BR_CC: return LowerBR_CC(Op, DAG);
151 case ISD::SELECT_CC: return LowerSELECT_CC(Op, DAG);
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000152 case ISD::SIGN_EXTEND: return LowerSIGN_EXTEND(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000153 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000154 llvm_unreachable("unimplemented operand");
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000155 return SDValue();
156 }
157}
158
Bill Wendlingb4202b82009-07-01 18:50:55 +0000159/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000160unsigned MSP430TargetLowering::getFunctionAlignment(const Function *F) const {
161 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4;
162}
163
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000164//===----------------------------------------------------------------------===//
Anton Korobeynikovcd761282009-08-26 13:44:29 +0000165// MSP430 Inline Assembly Support
166//===----------------------------------------------------------------------===//
167
168/// getConstraintType - Given a constraint letter, return the type of
169/// constraint it is for this target.
170TargetLowering::ConstraintType
171MSP430TargetLowering::getConstraintType(const std::string &Constraint) const {
172 if (Constraint.size() == 1) {
173 switch (Constraint[0]) {
174 case 'r':
175 return C_RegisterClass;
176 default:
177 break;
178 }
179 }
180 return TargetLowering::getConstraintType(Constraint);
181}
182
183std::pair<unsigned, const TargetRegisterClass*>
184MSP430TargetLowering::
185getRegForInlineAsmConstraint(const std::string &Constraint,
186 EVT VT) const {
187 if (Constraint.size() == 1) {
188 // GCC Constraint Letters
189 switch (Constraint[0]) {
190 default: break;
191 case 'r': // GENERAL_REGS
192 if (VT == MVT::i8)
193 return std::make_pair(0U, MSP430::GR8RegisterClass);
194
195 return std::make_pair(0U, MSP430::GR16RegisterClass);
196 }
197 }
198
199 return TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
200}
201
202//===----------------------------------------------------------------------===//
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000203// Calling Convention Implementation
204//===----------------------------------------------------------------------===//
205
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +0000206#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000207
Dan Gohman98ca4f22009-08-05 01:29:28 +0000208SDValue
209MSP430TargetLowering::LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000210 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000211 bool isVarArg,
212 const SmallVectorImpl<ISD::InputArg>
213 &Ins,
214 DebugLoc dl,
215 SelectionDAG &DAG,
216 SmallVectorImpl<SDValue> &InVals) {
217
218 switch (CallConv) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000219 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000220 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000221 case CallingConv::C:
222 case CallingConv::Fast:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000223 return LowerCCCArguments(Chain, CallConv, isVarArg, Ins, dl, DAG, InVals);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000224 }
225}
226
Dan Gohman98ca4f22009-08-05 01:29:28 +0000227SDValue
228MSP430TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000229 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000230 bool isTailCall,
231 const SmallVectorImpl<ISD::OutputArg> &Outs,
232 const SmallVectorImpl<ISD::InputArg> &Ins,
233 DebugLoc dl, SelectionDAG &DAG,
234 SmallVectorImpl<SDValue> &InVals) {
235
236 switch (CallConv) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000237 default:
Torok Edwinc23197a2009-07-14 16:55:14 +0000238 llvm_unreachable("Unsupported calling convention");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000239 case CallingConv::Fast:
240 case CallingConv::C:
Dan Gohman98ca4f22009-08-05 01:29:28 +0000241 return LowerCCCCallTo(Chain, Callee, CallConv, isVarArg, isTailCall,
242 Outs, Ins, dl, DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000243 }
244}
245
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000246/// LowerCCCArguments - transform physical registers into virtual registers and
247/// generate load operations for arguments places on the stack.
248// FIXME: struct return stuff
249// FIXME: varargs
Dan Gohman98ca4f22009-08-05 01:29:28 +0000250SDValue
251MSP430TargetLowering::LowerCCCArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000252 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000253 bool isVarArg,
254 const SmallVectorImpl<ISD::InputArg>
255 &Ins,
256 DebugLoc dl,
257 SelectionDAG &DAG,
258 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000259 MachineFunction &MF = DAG.getMachineFunction();
260 MachineFrameInfo *MFI = MF.getFrameInfo();
261 MachineRegisterInfo &RegInfo = MF.getRegInfo();
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000262
263 // Assign locations to all of the incoming arguments.
264 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000265 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
266 ArgLocs, *DAG.getContext());
267 CCInfo.AnalyzeFormalArguments(Ins, CC_MSP430);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000268
269 assert(!isVarArg && "Varargs not supported yet");
270
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000271 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
272 CCValAssign &VA = ArgLocs[i];
273 if (VA.isRegLoc()) {
274 // Arguments passed in registers
Owen Andersone50ed302009-08-10 22:56:29 +0000275 EVT RegVT = VA.getLocVT();
Owen Anderson825b72b2009-08-11 20:47:22 +0000276 switch (RegVT.getSimpleVT().SimpleTy) {
Torok Edwin804e0fe2009-07-08 19:04:27 +0000277 default:
278 {
Torok Edwindac237e2009-07-08 20:53:28 +0000279#ifndef NDEBUG
Chris Lattner4437ae22009-08-23 07:05:07 +0000280 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000281 << RegVT.getSimpleVT().SimpleTy << "\n";
Torok Edwindac237e2009-07-08 20:53:28 +0000282#endif
Torok Edwinc23197a2009-07-14 16:55:14 +0000283 llvm_unreachable(0);
Torok Edwin804e0fe2009-07-08 19:04:27 +0000284 }
Owen Anderson825b72b2009-08-11 20:47:22 +0000285 case MVT::i16:
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000286 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000287 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000288 RegInfo.addLiveIn(VA.getLocReg(), VReg);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000289 SDValue ArgValue = DAG.getCopyFromReg(Chain, dl, VReg, RegVT);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000290
291 // If this is an 8-bit value, it is really passed promoted to 16
292 // bits. Insert an assert[sz]ext to capture this, then truncate to the
293 // right size.
294 if (VA.getLocInfo() == CCValAssign::SExt)
295 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
296 DAG.getValueType(VA.getValVT()));
297 else if (VA.getLocInfo() == CCValAssign::ZExt)
298 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
299 DAG.getValueType(VA.getValVT()));
300
301 if (VA.getLocInfo() != CCValAssign::Full)
302 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
303
Dan Gohman98ca4f22009-08-05 01:29:28 +0000304 InVals.push_back(ArgValue);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000305 }
306 } else {
307 // Sanity check
308 assert(VA.isMemLoc());
309 // Load the argument to a virtual register
310 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
311 if (ObjSize > 2) {
Chris Lattner4437ae22009-08-23 07:05:07 +0000312 errs() << "LowerFormalArguments Unhandled argument type: "
Owen Anderson825b72b2009-08-11 20:47:22 +0000313 << VA.getLocVT().getSimpleVT().SimpleTy
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000314 << "\n";
315 }
316 // Create the frame index object for this incoming parameter...
317 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
318
319 // Create the SelectionDAG nodes corresponding to a load
320 //from this parameter
Owen Anderson825b72b2009-08-11 20:47:22 +0000321 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000322 InVals.push_back(DAG.getLoad(VA.getLocVT(), dl, Chain, FIN,
Evan Cheng65531552009-10-17 07:53:04 +0000323 PseudoSourceValue::getFixedStack(FI), 0));
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000324 }
325 }
326
Dan Gohman98ca4f22009-08-05 01:29:28 +0000327 return Chain;
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000328}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000329
Dan Gohman98ca4f22009-08-05 01:29:28 +0000330SDValue
331MSP430TargetLowering::LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000332 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000333 const SmallVectorImpl<ISD::OutputArg> &Outs,
334 DebugLoc dl, SelectionDAG &DAG) {
335
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000336 // CCValAssign - represent the assignment of the return value to a location
337 SmallVector<CCValAssign, 16> RVLocs;
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000338
339 // CCState - Info about the registers and stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000340 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
341 RVLocs, *DAG.getContext());
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000342
Dan Gohman98ca4f22009-08-05 01:29:28 +0000343 // Analize return values.
344 CCInfo.AnalyzeReturn(Outs, RetCC_MSP430);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000345
346 // If this is the first return lowered for this function, add the regs to the
347 // liveout set for the function.
348 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
349 for (unsigned i = 0; i != RVLocs.size(); ++i)
350 if (RVLocs[i].isRegLoc())
351 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
352 }
353
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000354 SDValue Flag;
355
356 // Copy the result values into the output registers.
357 for (unsigned i = 0; i != RVLocs.size(); ++i) {
358 CCValAssign &VA = RVLocs[i];
359 assert(VA.isRegLoc() && "Can only return in registers!");
360
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000361 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
Dan Gohman98ca4f22009-08-05 01:29:28 +0000362 Outs[i].Val, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000363
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000364 // Guarantee that all emitted copies are stuck together,
365 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000366 Flag = Chain.getValue(1);
367 }
368
369 if (Flag.getNode())
Owen Anderson825b72b2009-08-11 20:47:22 +0000370 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000371
372 // Return Void
Owen Anderson825b72b2009-08-11 20:47:22 +0000373 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000374}
375
Anton Korobeynikov44288852009-05-03 13:07:31 +0000376/// LowerCCCCallTo - functions arguments are copied from virtual regs to
377/// (physical regs)/(stack frame), CALLSEQ_START and CALLSEQ_END are emitted.
378/// TODO: sret.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000379SDValue
380MSP430TargetLowering::LowerCCCCallTo(SDValue Chain, SDValue Callee,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000381 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000382 bool isTailCall,
383 const SmallVectorImpl<ISD::OutputArg>
384 &Outs,
385 const SmallVectorImpl<ISD::InputArg> &Ins,
386 DebugLoc dl, SelectionDAG &DAG,
387 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000388 // Analyze operands of the call, assigning locations to each operand.
389 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000390 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
391 ArgLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000392
Dan Gohman98ca4f22009-08-05 01:29:28 +0000393 CCInfo.AnalyzeCallOperands(Outs, CC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000394
395 // Get a count of how many bytes are to be pushed on the stack.
396 unsigned NumBytes = CCInfo.getNextStackOffset();
397
398 Chain = DAG.getCALLSEQ_START(Chain ,DAG.getConstant(NumBytes,
399 getPointerTy(), true));
400
401 SmallVector<std::pair<unsigned, SDValue>, 4> RegsToPass;
402 SmallVector<SDValue, 12> MemOpChains;
403 SDValue StackPtr;
404
405 // Walk the register/memloc assignments, inserting copies/loads.
406 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
407 CCValAssign &VA = ArgLocs[i];
408
Dan Gohman98ca4f22009-08-05 01:29:28 +0000409 SDValue Arg = Outs[i].Val;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000410
411 // Promote the value if needed.
412 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000413 default: llvm_unreachable("Unknown loc info!");
Anton Korobeynikov44288852009-05-03 13:07:31 +0000414 case CCValAssign::Full: break;
415 case CCValAssign::SExt:
416 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
417 break;
418 case CCValAssign::ZExt:
419 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
420 break;
421 case CCValAssign::AExt:
422 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
423 break;
424 }
425
426 // Arguments that can be passed on register must be kept at RegsToPass
427 // vector
428 if (VA.isRegLoc()) {
429 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
430 } else {
431 assert(VA.isMemLoc());
432
433 if (StackPtr.getNode() == 0)
434 StackPtr = DAG.getCopyFromReg(Chain, dl, MSP430::SPW, getPointerTy());
435
436 SDValue PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(),
437 StackPtr,
438 DAG.getIntPtrConstant(VA.getLocMemOffset()));
439
440
441 MemOpChains.push_back(DAG.getStore(Chain, dl, Arg, PtrOff,
442 PseudoSourceValue::getStack(),
443 VA.getLocMemOffset()));
444 }
445 }
446
447 // Transform all store nodes into one single node because all store nodes are
448 // independent of each other.
449 if (!MemOpChains.empty())
Owen Anderson825b72b2009-08-11 20:47:22 +0000450 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Anton Korobeynikov44288852009-05-03 13:07:31 +0000451 &MemOpChains[0], MemOpChains.size());
452
453 // Build a sequence of copy-to-reg nodes chained together with token chain and
454 // flag operands which copy the outgoing args into registers. The InFlag in
455 // necessary since all emited instructions must be stuck together.
456 SDValue InFlag;
457 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
458 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
459 RegsToPass[i].second, InFlag);
460 InFlag = Chain.getValue(1);
461 }
462
463 // If the callee is a GlobalAddress node (quite common, every direct call is)
464 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
465 // Likewise ExternalSymbol -> TargetExternalSymbol.
466 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000467 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000468 else if (ExternalSymbolSDNode *E = dyn_cast<ExternalSymbolSDNode>(Callee))
Owen Anderson825b72b2009-08-11 20:47:22 +0000469 Callee = DAG.getTargetExternalSymbol(E->getSymbol(), MVT::i16);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000470
471 // Returns a chain & a flag for retval copy to use.
Owen Anderson825b72b2009-08-11 20:47:22 +0000472 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000473 SmallVector<SDValue, 8> Ops;
474 Ops.push_back(Chain);
475 Ops.push_back(Callee);
476
477 // Add argument registers to the end of the list so that they are
478 // known live into the call.
479 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
480 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
481 RegsToPass[i].second.getValueType()));
482
483 if (InFlag.getNode())
484 Ops.push_back(InFlag);
485
486 Chain = DAG.getNode(MSP430ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
487 InFlag = Chain.getValue(1);
488
489 // Create the CALLSEQ_END node.
490 Chain = DAG.getCALLSEQ_END(Chain,
491 DAG.getConstant(NumBytes, getPointerTy(), true),
492 DAG.getConstant(0, getPointerTy(), true),
493 InFlag);
494 InFlag = Chain.getValue(1);
495
496 // Handle result values, copying them out of physregs into vregs that we
497 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +0000498 return LowerCallResult(Chain, InFlag, CallConv, isVarArg, Ins, dl,
499 DAG, InVals);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000500}
501
Dan Gohman98ca4f22009-08-05 01:29:28 +0000502/// LowerCallResult - Lower the result values of a call into the
503/// appropriate copies out of appropriate physical registers.
504///
505SDValue
Anton Korobeynikov44288852009-05-03 13:07:31 +0000506MSP430TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000507 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000508 const SmallVectorImpl<ISD::InputArg> &Ins,
509 DebugLoc dl, SelectionDAG &DAG,
510 SmallVectorImpl<SDValue> &InVals) {
Anton Korobeynikov44288852009-05-03 13:07:31 +0000511
512 // Assign locations to each value returned by this call.
513 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000514 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +0000515 RVLocs, *DAG.getContext());
Anton Korobeynikov44288852009-05-03 13:07:31 +0000516
Dan Gohman98ca4f22009-08-05 01:29:28 +0000517 CCInfo.AnalyzeCallResult(Ins, RetCC_MSP430);
Anton Korobeynikov44288852009-05-03 13:07:31 +0000518
519 // Copy all of the result registers out of their specified physreg.
520 for (unsigned i = 0; i != RVLocs.size(); ++i) {
521 Chain = DAG.getCopyFromReg(Chain, dl, RVLocs[i].getLocReg(),
522 RVLocs[i].getValVT(), InFlag).getValue(1);
523 InFlag = Chain.getValue(2);
Dan Gohman98ca4f22009-08-05 01:29:28 +0000524 InVals.push_back(Chain.getValue(0));
Anton Korobeynikov44288852009-05-03 13:07:31 +0000525 }
526
Dan Gohman98ca4f22009-08-05 01:29:28 +0000527 return Chain;
Anton Korobeynikov44288852009-05-03 13:07:31 +0000528}
529
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000530SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
531 SelectionDAG &DAG) {
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000532 unsigned Opc = Op.getOpcode();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000533 SDNode* N = Op.getNode();
Owen Andersone50ed302009-08-10 22:56:29 +0000534 EVT VT = Op.getValueType();
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000535 DebugLoc dl = N->getDebugLoc();
536
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000537 // We currently only lower shifts of constant argument.
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000538 if (!isa<ConstantSDNode>(N->getOperand(1)))
539 return SDValue();
540
541 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
542
543 // Expand the stuff into sequence of shifts.
544 // FIXME: for some shift amounts this might be done better!
545 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
546 SDValue Victim = N->getOperand(0);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000547
548 if (Opc == ISD::SRL && ShiftAmount) {
549 // Emit a special goodness here:
550 // srl A, 1 => clrc; rrc A
Anton Korobeynikovbf8ef3f2009-05-03 13:16:37 +0000551 Victim = DAG.getNode(MSP430ISD::RRC, dl, VT, Victim);
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000552 ShiftAmount -= 1;
553 }
554
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000555 while (ShiftAmount--)
Anton Korobeynikovaceb6202009-05-17 10:15:22 +0000556 Victim = DAG.getNode((Opc == ISD::SHL ? MSP430ISD::RLA : MSP430ISD::RRA),
Anton Korobeynikovea54c982009-05-03 13:13:17 +0000557 dl, VT, Victim);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000558
559 return Victim;
560}
561
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000562SDValue MSP430TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
563 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
564 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
565
566 // Create the TargetGlobalAddress node, folding in the constant offset.
567 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
568 return DAG.getNode(MSP430ISD::Wrapper, Op.getDebugLoc(),
569 getPointerTy(), Result);
570}
571
Anton Korobeynikov5d59f682009-05-03 13:14:46 +0000572SDValue MSP430TargetLowering::LowerExternalSymbol(SDValue Op,
573 SelectionDAG &DAG) {
574 DebugLoc dl = Op.getDebugLoc();
575 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
576 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
577
578 return DAG.getNode(MSP430ISD::Wrapper, dl, getPointerTy(), Result);;
579}
580
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000581static SDValue EmitCMP(SDValue &LHS, SDValue &RHS, SDValue &TargetCC,
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000582 ISD::CondCode CC,
583 DebugLoc dl, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000584 // FIXME: Handle bittests someday
585 assert(!LHS.getValueType().isFloatingPoint() && "We don't handle FP yet");
586
587 // FIXME: Handle jump negative someday
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000588 MSP430CC::CondCodes TCC = MSP430CC::COND_INVALID;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000589 switch (CC) {
Torok Edwinc23197a2009-07-14 16:55:14 +0000590 default: llvm_unreachable("Invalid integer condition!");
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000591 case ISD::SETEQ:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000592 TCC = MSP430CC::COND_E; // aka COND_Z
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000593 break;
594 case ISD::SETNE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000595 TCC = MSP430CC::COND_NE; // aka COND_NZ
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000596 break;
597 case ISD::SETULE:
598 std::swap(LHS, RHS); // FALLTHROUGH
599 case ISD::SETUGE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000600 TCC = MSP430CC::COND_HS; // aka COND_C
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000601 break;
602 case ISD::SETUGT:
603 std::swap(LHS, RHS); // FALLTHROUGH
604 case ISD::SETULT:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000605 TCC = MSP430CC::COND_LO; // aka COND_NC
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000606 break;
607 case ISD::SETLE:
608 std::swap(LHS, RHS); // FALLTHROUGH
609 case ISD::SETGE:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000610 TCC = MSP430CC::COND_GE;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000611 break;
612 case ISD::SETGT:
613 std::swap(LHS, RHS); // FALLTHROUGH
614 case ISD::SETLT:
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000615 TCC = MSP430CC::COND_L;
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000616 break;
617 }
618
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000619 TargetCC = DAG.getConstant(TCC, MVT::i8);
Owen Anderson825b72b2009-08-11 20:47:22 +0000620 return DAG.getNode(MSP430ISD::CMP, dl, MVT::Flag, LHS, RHS);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000621}
622
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000623
624SDValue MSP430TargetLowering::LowerBR_CC(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000625 SDValue Chain = Op.getOperand(0);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000626 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(1))->get();
627 SDValue LHS = Op.getOperand(2);
628 SDValue RHS = Op.getOperand(3);
629 SDValue Dest = Op.getOperand(4);
630 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000631
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000632 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000633 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000634
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000635 return DAG.getNode(MSP430ISD::BR_CC, dl, Op.getValueType(),
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000636 Chain, Dest, TargetCC, Flag);
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000637}
638
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000639SDValue MSP430TargetLowering::LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) {
640 SDValue LHS = Op.getOperand(0);
641 SDValue RHS = Op.getOperand(1);
642 SDValue TrueV = Op.getOperand(2);
643 SDValue FalseV = Op.getOperand(3);
644 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(4))->get();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000645 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000646
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000647 SDValue TargetCC;
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000648 SDValue Flag = EmitCMP(LHS, RHS, TargetCC, CC, dl, DAG);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000649
Owen Anderson825b72b2009-08-11 20:47:22 +0000650 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000651 SmallVector<SDValue, 4> Ops;
652 Ops.push_back(TrueV);
653 Ops.push_back(FalseV);
Anton Korobeynikov3926fb62009-10-21 19:16:49 +0000654 Ops.push_back(TargetCC);
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000655 Ops.push_back(Flag);
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000656
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000657 return DAG.getNode(MSP430ISD::SELECT_CC, dl, VTs, &Ops[0], Ops.size());
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000658}
659
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000660SDValue MSP430TargetLowering::LowerSIGN_EXTEND(SDValue Op,
661 SelectionDAG &DAG) {
662 SDValue Val = Op.getOperand(0);
Owen Andersone50ed302009-08-10 22:56:29 +0000663 EVT VT = Op.getValueType();
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000664 DebugLoc dl = Op.getDebugLoc();
665
Owen Anderson825b72b2009-08-11 20:47:22 +0000666 assert(VT == MVT::i16 && "Only support i16 for now!");
Anton Korobeynikovb78e2142009-05-03 13:17:49 +0000667
668 return DAG.getNode(ISD::SIGN_EXTEND_INREG, dl, VT,
669 DAG.getNode(ISD::ANY_EXTEND, dl, VT, Val),
670 DAG.getValueType(Val.getValueType()));
671}
672
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000673const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
674 switch (Opcode) {
675 default: return NULL;
676 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000677 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikove699d0f2009-05-03 13:16:17 +0000678 case MSP430ISD::RLA: return "MSP430ISD::RLA";
679 case MSP430ISD::RRC: return "MSP430ISD::RRC";
Anton Korobeynikovb5612642009-05-03 13:07:54 +0000680 case MSP430ISD::CALL: return "MSP430ISD::CALL";
Anton Korobeynikov3513ca82009-05-03 13:08:33 +0000681 case MSP430ISD::Wrapper: return "MSP430ISD::Wrapper";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000682 case MSP430ISD::BR_CC: return "MSP430ISD::BR_CC";
Anton Korobeynikoved1a51a2009-05-03 13:12:06 +0000683 case MSP430ISD::CMP: return "MSP430ISD::CMP";
Anton Korobeynikov1bb8cd72009-05-03 13:19:09 +0000684 case MSP430ISD::SELECT_CC: return "MSP430ISD::SELECT_CC";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000685 }
686}
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000687
688//===----------------------------------------------------------------------===//
689// Other Lowering Code
690//===----------------------------------------------------------------------===//
691
692MachineBasicBlock*
693MSP430TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000694 MachineBasicBlock *BB,
695 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const {
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000696 const TargetInstrInfo &TII = *getTargetMachine().getInstrInfo();
697 DebugLoc dl = MI->getDebugLoc();
Anton Korobeynikovda4d2f62009-05-08 18:51:21 +0000698 assert((MI->getOpcode() == MSP430::Select16 ||
699 MI->getOpcode() == MSP430::Select8) &&
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000700 "Unexpected instr type to insert");
701
702 // To "insert" a SELECT instruction, we actually have to insert the diamond
703 // control-flow pattern. The incoming instruction knows the destination vreg
704 // to set, the condition code register to branch on, the true/false values to
705 // select between, and a branch opcode to use.
706 const BasicBlock *LLVM_BB = BB->getBasicBlock();
707 MachineFunction::iterator I = BB;
708 ++I;
709
710 // thisMBB:
711 // ...
712 // TrueVal = ...
713 // cmpTY ccX, r1, r2
714 // jCC copy1MBB
715 // fallthrough --> copy0MBB
716 MachineBasicBlock *thisMBB = BB;
717 MachineFunction *F = BB->getParent();
718 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
719 MachineBasicBlock *copy1MBB = F->CreateMachineBasicBlock(LLVM_BB);
720 BuildMI(BB, dl, TII.get(MSP430::JCC))
721 .addMBB(copy1MBB)
722 .addImm(MI->getOperand(3).getImm());
723 F->insert(I, copy0MBB);
724 F->insert(I, copy1MBB);
Evan Chengce319102009-09-19 09:51:03 +0000725 // Inform sdisel of the edge changes.
726 for (MachineBasicBlock::succ_iterator SI = BB->succ_begin(),
727 SE = BB->succ_end(); SI != SE; ++SI)
728 EM->insert(std::make_pair(*SI, copy1MBB));
Anton Korobeynikov8b528e52009-05-03 13:12:23 +0000729 // Update machine-CFG edges by transferring all successors of the current
730 // block to the new block which will contain the Phi node for the select.
731 copy1MBB->transferSuccessors(BB);
732 // Next, add the true and fallthrough blocks as its successors.
733 BB->addSuccessor(copy0MBB);
734 BB->addSuccessor(copy1MBB);
735
736 // copy0MBB:
737 // %FalseValue = ...
738 // # fallthrough to copy1MBB
739 BB = copy0MBB;
740
741 // Update machine-CFG edges
742 BB->addSuccessor(copy1MBB);
743
744 // copy1MBB:
745 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
746 // ...
747 BB = copy1MBB;
748 BuildMI(BB, dl, TII.get(MSP430::PHI),
749 MI->getOperand(0).getReg())
750 .addReg(MI->getOperand(2).getReg()).addMBB(copy0MBB)
751 .addReg(MI->getOperand(1).getReg()).addMBB(thisMBB);
752
753 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
754 return BB;
755}