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