98172029b35210364ece376c51e32f428950eab9
[oota-llvm.git] / lib / Target / MSP430 / MSP430ISelLowering.cpp
1 //===-- 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"
31 #include "llvm/CodeGen/PseudoSourceValue.h"
32 #include "llvm/CodeGen/SelectionDAGISel.h"
33 #include "llvm/CodeGen/ValueTypes.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/ADT/VectorExtras.h"
36 using namespace llvm;
37
38 MSP430TargetLowering::MSP430TargetLowering(MSP430TargetMachine &tm) :
39   TargetLowering(tm), Subtarget(*tm.getSubtargetImpl()), TM(tm) {
40
41   // Set up the register classes.
42   addRegisterClass(MVT::i16, MSP430::GR16RegisterClass);
43
44   // Compute derived properties from the register classes
45   computeRegisterProperties();
46
47   // Provide all sorts of operation actions
48
49   // Division is expensive
50   setIntDivIsCheap(false);
51
52   // 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
58   setOperationAction(ISD::RET, MVT::Other, Custom);
59 }
60
61 SDValue MSP430TargetLowering::
62 LowerOperation(SDValue Op, SelectionDAG &DAG) {
63   switch (Op.getOpcode()) {
64   case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
65   case ISD::SRA: return LowerShifts(Op, DAG);
66   case ISD::RET: return LowerRET(Op, DAG);
67   default:
68     assert(0 && "unimplemented operand");
69     return SDValue();
70   }
71 }
72
73 //===----------------------------------------------------------------------===//
74 //                      Calling Convention Implementation
75 //===----------------------------------------------------------------------===//
76
77 #include "MSP430GenCallingConv.inc"
78
79 SDValue MSP430TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op,
80                                                     SelectionDAG &DAG) {
81   unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
82   switch (CC) {
83   default:
84     assert(0 && "Unsupported calling convention");
85   case CallingConv::C:
86   case CallingConv::Fast:
87     return LowerCCCArguments(Op, DAG);
88   }
89 }
90
91 /// LowerCCCArguments - transform physical registers into virtual registers and
92 /// generate load operations for arguments places on the stack.
93 // FIXME: struct return stuff
94 // FIXME: varargs
95 SDValue MSP430TargetLowering::LowerCCCArguments(SDValue Op,
96                                                 SelectionDAG &DAG) {
97   MachineFunction &MF = DAG.getMachineFunction();
98   MachineFrameInfo *MFI = MF.getFrameInfo();
99   MachineRegisterInfo &RegInfo = MF.getRegInfo();
100   SDValue Root = Op.getOperand(0);
101   bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
102   unsigned CC = MF.getFunction()->getCallingConv();
103   DebugLoc dl = Op.getDebugLoc();
104
105   // Assign locations to all of the incoming arguments.
106   SmallVector<CCValAssign, 16> ArgLocs;
107   CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
108   CCInfo.AnalyzeFormalArguments(Op.getNode(), CC_MSP430);
109
110   assert(!isVarArg && "Varargs not supported yet");
111
112   SmallVector<SDValue, 16> ArgValues;
113   for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
114     CCValAssign &VA = ArgLocs[i];
115     if (VA.isRegLoc()) {
116       // Arguments passed in registers
117       MVT RegVT = VA.getLocVT();
118       switch (RegVT.getSimpleVT()) {
119       default:
120         cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
121              << RegVT.getSimpleVT()
122              << "\n";
123         abort();
124       case MVT::i16:
125         unsigned VReg =
126           RegInfo.createVirtualRegister(MSP430::GR16RegisterClass);
127         RegInfo.addLiveIn(VA.getLocReg(), VReg);
128         SDValue ArgValue = DAG.getCopyFromReg(Root, dl, VReg, RegVT);
129
130         // If this is an 8-bit value, it is really passed promoted to 16
131         // bits. Insert an assert[sz]ext to capture this, then truncate to the
132         // right size.
133         if (VA.getLocInfo() == CCValAssign::SExt)
134           ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
135                                  DAG.getValueType(VA.getValVT()));
136         else if (VA.getLocInfo() == CCValAssign::ZExt)
137           ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
138                                  DAG.getValueType(VA.getValVT()));
139
140         if (VA.getLocInfo() != CCValAssign::Full)
141           ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
142
143         ArgValues.push_back(ArgValue);
144       }
145     } else {
146       // Sanity check
147       assert(VA.isMemLoc());
148       // Load the argument to a virtual register
149       unsigned ObjSize = VA.getLocVT().getSizeInBits()/8;
150       if (ObjSize > 2) {
151         cerr << "LowerFORMAL_ARGUMENTS Unhandled argument type: "
152              << VA.getLocVT().getSimpleVT()
153              << "\n";
154       }
155       // Create the frame index object for this incoming parameter...
156       int FI = MFI->CreateFixedObject(ObjSize, VA.getLocMemOffset());
157
158       // Create the SelectionDAG nodes corresponding to a load
159       //from this parameter
160       SDValue FIN = DAG.getFrameIndex(FI, MVT::i16);
161       ArgValues.push_back(DAG.getLoad(VA.getLocVT(), dl, Root, FIN,
162                                       PseudoSourceValue::getFixedStack(FI), 0));
163     }
164   }
165
166   ArgValues.push_back(Root);
167
168   // Return the new list of results.
169   return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
170                      &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
171 }
172
173 SDValue MSP430TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
174   // CCValAssign - represent the assignment of the return value to a location
175   SmallVector<CCValAssign, 16> RVLocs;
176   unsigned CC   = DAG.getMachineFunction().getFunction()->getCallingConv();
177   bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
178   DebugLoc dl = Op.getDebugLoc();
179
180   // CCState - Info about the registers and stack slot.
181   CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
182
183   // Analize return values of ISD::RET
184   CCInfo.AnalyzeReturn(Op.getNode(), RetCC_MSP430);
185
186   // If this is the first return lowered for this function, add the regs to the
187   // liveout set for the function.
188   if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
189     for (unsigned i = 0; i != RVLocs.size(); ++i)
190       if (RVLocs[i].isRegLoc())
191         DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
192   }
193
194   // The chain is always operand #0
195   SDValue Chain = Op.getOperand(0);
196   SDValue Flag;
197
198   // Copy the result values into the output registers.
199   for (unsigned i = 0; i != RVLocs.size(); ++i) {
200     CCValAssign &VA = RVLocs[i];
201     assert(VA.isRegLoc() && "Can only return in registers!");
202
203     // ISD::RET => ret chain, (regnum1,val1), ...
204     // So i*2+1 index only the regnums
205     Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(),
206                              Op.getOperand(i*2+1), Flag);
207
208     // Guarantee that all emitted copies are stuck together,
209     // avoiding something bad.
210     Flag = Chain.getValue(1);
211   }
212
213   if (Flag.getNode())
214     return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain, Flag);
215
216   // Return Void
217   return DAG.getNode(MSP430ISD::RET_FLAG, dl, MVT::Other, Chain);
218 }
219
220 SDValue MSP430TargetLowering::LowerShifts(SDValue Op,
221                                           SelectionDAG &DAG) {
222   assert(Op.getOpcode() == ISD::SRA && "Only SRA is currently supported.");
223   SDNode* N = Op.getNode();
224   MVT VT = Op.getValueType();
225   DebugLoc dl = N->getDebugLoc();
226
227   // We currently only lower SRA of constant argument.
228   if (!isa<ConstantSDNode>(N->getOperand(1)))
229     return SDValue();
230
231   uint64_t ShiftAmount = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
232
233   // Expand the stuff into sequence of shifts.
234   // FIXME: for some shift amounts this might be done better!
235   // E.g.: foo >> (8 + N) => sxt(swpb(foo)) >> N
236   SDValue Victim = N->getOperand(0);
237   while (ShiftAmount--)
238     Victim = DAG.getNode(MSP430ISD::RRA, dl, VT, Victim);
239
240   return Victim;
241 }
242
243 const char *MSP430TargetLowering::getTargetNodeName(unsigned Opcode) const {
244   switch (Opcode) {
245   default: return NULL;
246   case MSP430ISD::RET_FLAG:           return "MSP430ISD::RET_FLAG";
247   case MSP430ISD::RRA:                return "MSP430ISD::RRA";
248   }
249 }
250