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