blob: 39ee075661930ef08e2df47fa9936734ea037f85 [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 Korobeynikov1df221f2009-05-03 13:02:04 +000042 addRegisterClass(MVT::i16, MSP430::GR16RegisterClass);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000043
44 // Compute derived properties from the register classes
45 computeRegisterProperties();
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000046
Anton Korobeynikov1476d972009-05-03 13:03:14 +000047 // Provide all sorts of operation actions
48
49 // Division is expensive
50 setIntDivIsCheap(false);
51
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000052 // Even if we have only 1 bit shift here, we can perform
53 // shifts of the whole bitwidth 1 bit per step.
54 setShiftAmountType(MVT::i8);
55
56 setOperationAction(ISD::SRA, MVT::i16, Custom);
57
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000058 setOperationAction(ISD::RET, MVT::Other, Custom);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000059}
60
Anton Korobeynikovb8639f52009-05-03 13:03:50 +000061SDValue MSP430TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000062 switch (Op.getOpcode()) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000063 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +000064 case ISD::SRA: return LowerShifts(Op, DAG);
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +000065 case ISD::RET: return LowerRET(Op, DAG);
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000066 default:
67 assert(0 && "unimplemented operand");
68 return SDValue();
69 }
70}
71
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000072//===----------------------------------------------------------------------===//
73// Calling Convention Implementation
74//===----------------------------------------------------------------------===//
75
Anton Korobeynikovf2c3e172009-05-03 12:57:15 +000076#include "MSP430GenCallingConv.inc"
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000077
78SDValue MSP430TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
79 SelectionDAG &DAG) {
80 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
81 switch (CC) {
82 default:
83 assert(0 && "Unsupported calling convention");
84 case CallingConv::C:
85 case CallingConv::Fast:
86 return LowerCCCArguments(Op, DAG);
87 }
88}
89
90/// LowerCCCArguments - transform physical registers into virtual registers and
91/// generate load operations for arguments places on the stack.
92// FIXME: struct return stuff
93// FIXME: varargs
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +000094SDValue MSP430TargetLowering::LowerCCCArguments(SDValue Op,
95 SelectionDAG &DAG) {
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +000096 MachineFunction &MF = DAG.getMachineFunction();
97 MachineFrameInfo *MFI = MF.getFrameInfo();
98 MachineRegisterInfo &RegInfo = MF.getRegInfo();
99 SDValue Root = Op.getOperand(0);
100 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
101 unsigned CC = MF.getFunction()->getCallingConv();
102 DebugLoc dl = Op.getDebugLoc();
103
104 // Assign locations to all of the incoming arguments.
105 SmallVector<CCValAssign, 16> ArgLocs;
106 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
107 CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MSP430);
108
109 assert(!isVarArg && "Varargs not supported yet");
110
111 SmallVector<SDValue, 16> ArgValues;
112 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
113 CCValAssign &VA = ArgLocs[i];
114 if (VA.isRegLoc()) {
115 // Arguments passed in registers
116 MVT RegVT = VA.getLocVT();
117 switch (RegVT.getSimpleVT()) {
118 default:
119 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
120 << RegVT.getSimpleVT()
121 << "\n";
122 abort();
123 case MVT::i16:
124 unsigned VReg =
Anton Korobeynikov1df221f2009-05-03 13:02:04 +0000125 RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
Anton Korobeynikovc8fbb6a2009-05-03 12:59:33 +0000126 RegInfo.addLiveIn(VA.getLocReg(), VReg);
127 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
128
129 // If this is an 8-bit value, it is really passed promoted to 16
130 // bits. Insert an assert[sz]ext to capture this, then truncate to the
131 // right size.
132 if (VA.getLocInfo() == CCValAssign::SExt)
133 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
134 DAG.getValueType(VA.getValVT()));
135 else if (VA.getLocInfo() == CCValAssign::ZExt)
136 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
137 DAG.getValueType(VA.getValVT()));
138
139 if (VA.getLocInfo() != CCValAssign::Full)
140 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
141
142 ArgValues.push_back(ArgValue);
143 }
144 } else {
145 // Sanity check
146 assert(VA.isMemLoc());
147 // Load the argument to a virtual register
148 unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
149 if (ObjSize > 2) {
150 cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
151 << VA.getLocVT().getSimpleVT()
152 << "\n";
153 }
154 // Create the frame index object for this incoming parameter...
155 int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
156
157 // Create the SelectionDAG nodes corresponding to a load
158 //from this parameter
159 SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
160 ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
161 PseudoSourceValue::getFixedStack(FI), 0));
162 }
163 }
164
165 ArgValues.push_back(Root);
166
167 // Return the new list of results.
168 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
169 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
170}
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000171
172SDValue MSP430TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
173 // CCValAssign - represent the assignment of the return value to a location
174 SmallVector<CCValAssign, 16> RVLocs;
175 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
176 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
177 DebugLoc dl = Op.getDebugLoc();
178
179 // CCState - Info about the registers and stack slot.
180 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
181
182 // Analize return values of ISD::RET
183 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_MSP430);
184
185 // If this is the first return lowered for this function, add the regs to the
186 // liveout set for the function.
187 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
188 for (unsigned i = 0; i != RVLocs.size(); ++i)
189 if (RVLocs[i].isRegLoc())
190 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
191 }
192
193 // The chain is always operand #0
194 SDValue Chain = Op.getOperand(0);
195 SDValue Flag;
196
197 // Copy the result values into the output registers.
198 for (unsigned i = 0; i != RVLocs.size(); ++i) {
199 CCValAssign &VA = RVLocs[i];
200 assert(VA.isRegLoc() && "Can only return in registers!");
201
202 // ISD::RET => ret chain, (regnum1,val1), ...
203 // So i*2+1 index only the regnums
204 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
205 Op.getOperand(i*2+1), Flag);
206
Anton Korobeynikovdcb802c2009-05-03 13:00:11 +0000207 // Guarantee that all emitted copies are stuck together,
208 // avoiding something bad.
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000209 Flag = Chain.getValue(1);
210 }
211
212 if (Flag.getNode())
213 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
214
215 // Return Void
216 return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain);
217}
218
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000219SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
220 SelectionDAG &DAG) {
221 assert(Op.getOpcode() == ISD::SRA && "Only SRA is currently supported.");
222 SDNode* N = Op.getNode();
223 MVT VT = Op.getValueType();
224 DebugLoc dl = N->getDebugLoc();
225
226 // We currently only lower SRA of constant argument.
227 if (!isa<ConstantSDNode>(N->getOperand(1)))
228 return SDValue();
229
230 uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
231
232 // Expand the stuff into sequence of shifts.
233 // FIXME: for some shift amounts this might be done better!
234 // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
235 SDValue Victim = N->getOperand(0);
236 while (ShiftAmount--)
237 Victim = DAG.getNode(MSP430ISD::RRA, dl, VT, Victim);
238
239 return Victim;
240}
241
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000242const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
243 switch (Opcode) {
244 default: return NULL;
245 case MSP430ISD::RET_FLAG: return "MSP430ISD::RET_FLAG";
Anton Korobeynikovd2c94ae2009-05-03 13:03:33 +0000246 case MSP430ISD::RRA: return "MSP430ISD::RRA";
Anton Korobeynikovfd1b7c72009-05-03 12:59:50 +0000247 }
248}
249