Split the SDValue out of OutputArg so that SelectionDAG-independent
[oota-llvm.git] / lib / Target / PIC16 / PIC16ISelLowering.h
1 //===-- PIC16ISelLowering.h - PIC16 DAG Lowering Interface ------*- C++ -*-===//
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 defines the interfaces that PIC16 uses to lower LLVM code into a
11 // selection DAG.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #ifndef PIC16ISELLOWERING_H
16 #define PIC16ISELLOWERING_H
17
18 #include "PIC16.h"
19 #include "PIC16Subtarget.h"
20 #include "llvm/CodeGen/SelectionDAG.h"
21 #include "llvm/Target/TargetLowering.h"
22 #include <map>
23
24 namespace llvm {
25   namespace PIC16ISD {
26     enum NodeType {
27       // Start the numbering from where ISD NodeType finishes.
28       FIRST_NUMBER = ISD::BUILTIN_OP_END,
29
30       Lo,            // Low 8-bits of GlobalAddress.
31       Hi,            // High 8-bits of GlobalAddress.
32       PIC16Load,
33       PIC16LdArg,   // This is replica of PIC16Load but used to load function 
34                     // arguments and is being used for facilitating for some 
35                     // store removal optimizations. 
36
37       PIC16LdWF,
38       PIC16Store,
39       PIC16StWF,
40       Banksel,
41       MTLO,          // Move to low part of FSR
42       MTHI,          // Move to high part of FSR
43       MTPCLATH,      // Move to PCLATCH
44       PIC16Connect,  // General connector for PIC16 nodes
45       BCF,
46       LSLF,          // PIC16 Logical shift left
47       LRLF,          // PIC16 Logical shift right
48       RLF,           // Rotate left through carry
49       RRF,           // Rotate right through carry
50       CALL,          // PIC16 Call instruction 
51       CALLW,         // PIC16 CALLW instruction 
52       SUBCC,         // Compare for equality or inequality.
53       SELECT_ICC,    // Psuedo to be caught in schedular and expanded to brcond.
54       BRCOND,        // Conditional branch.
55       RET,           // Return.
56       Dummy
57     };
58
59     // Keep track of different address spaces. 
60     enum AddressSpace {
61       RAM_SPACE = 0,   // RAM address space
62       ROM_SPACE = 1    // ROM address space number is 1
63     };
64     enum PIC16Libcall {
65       MUL_I8 = RTLIB::UNKNOWN_LIBCALL + 1,
66       SRA_I8,
67       SLL_I8,
68       SRL_I8,
69       PIC16UnknownCall
70     };
71   }
72
73
74   //===--------------------------------------------------------------------===//
75   // TargetLowering Implementation
76   //===--------------------------------------------------------------------===//
77   class PIC16TargetLowering : public TargetLowering {
78   public:
79     explicit PIC16TargetLowering(PIC16TargetMachine &TM);
80
81     /// getTargetNodeName - This method returns the name of a target specific
82     /// DAG node.
83     virtual const char *getTargetNodeName(unsigned Opcode) const;
84     /// getSetCCResultType - Return the ISD::SETCC ValueType
85     virtual MVT::SimpleValueType getSetCCResultType(EVT ValType) const;
86     virtual MVT::SimpleValueType getCmpLibcallReturnType() const;
87     SDValue LowerShift(SDValue Op, SelectionDAG &DAG) const;
88     SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) const;
89     SDValue LowerADD(SDValue Op, SelectionDAG &DAG) const;
90     SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) const;
91     SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG) const;
92     // Call returns
93     SDValue 
94     LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag,
95                           const SmallVectorImpl<ISD::InputArg> &Ins,
96                           DebugLoc dl, SelectionDAG &DAG,
97                           SmallVectorImpl<SDValue> &InVals) const;
98     SDValue 
99     LowerIndirectCallReturn(SDValue Chain, SDValue InFlag,
100                              SDValue DataAddr_Lo, SDValue DataAddr_Hi,
101                             const SmallVectorImpl<ISD::InputArg> &Ins,
102                             DebugLoc dl, SelectionDAG &DAG,
103                             SmallVectorImpl<SDValue> &InVals) const;
104
105     // Call arguments
106     SDValue 
107     LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag,
108                              const SmallVectorImpl<ISD::OutputArg> &Outs,
109                              const SmallVectorImpl<SDValue> &OutVals,
110                              DebugLoc dl, SelectionDAG &DAG) const;
111
112     SDValue 
113     LowerIndirectCallArguments(SDValue Chain, SDValue InFlag,
114                                SDValue DataAddr_Lo, SDValue DataAddr_Hi, 
115                                const SmallVectorImpl<ISD::OutputArg> &Outs,
116                                const SmallVectorImpl<SDValue> &OutVals,
117                                const SmallVectorImpl<ISD::InputArg> &Ins,
118                                DebugLoc dl, SelectionDAG &DAG) const;
119
120     SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
121     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
122     SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
123                         SelectionDAG &DAG, DebugLoc dl) const;
124     virtual MachineBasicBlock *
125       EmitInstrWithCustomInserter(MachineInstr *MI,
126                                   MachineBasicBlock *MBB) const;
127
128     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
129     virtual void ReplaceNodeResults(SDNode *N,
130                                     SmallVectorImpl<SDValue> &Results,
131                                     SelectionDAG &DAG) const;
132     virtual void LowerOperationWrapper(SDNode *N,
133                                        SmallVectorImpl<SDValue> &Results,
134                                        SelectionDAG &DAG) const;
135
136     virtual SDValue
137     LowerFormalArguments(SDValue Chain,
138                          CallingConv::ID CallConv,
139                          bool isVarArg,
140                          const SmallVectorImpl<ISD::InputArg> &Ins,
141                          DebugLoc dl, SelectionDAG &DAG,
142                          SmallVectorImpl<SDValue> &InVals) const;
143
144     virtual SDValue
145       LowerCall(SDValue Chain, SDValue Callee,
146                 CallingConv::ID CallConv, bool isVarArg, bool &isTailCall,
147                 const SmallVectorImpl<ISD::OutputArg> &Outs,
148                 const SmallVectorImpl<SDValue> &OutVals,
149                 const SmallVectorImpl<ISD::InputArg> &Ins,
150                 DebugLoc dl, SelectionDAG &DAG,
151                 SmallVectorImpl<SDValue> &InVals) const;
152
153     virtual SDValue
154       LowerReturn(SDValue Chain,
155                   CallingConv::ID CallConv, bool isVarArg,
156                   const SmallVectorImpl<ISD::OutputArg> &Outs,
157                   const SmallVectorImpl<SDValue> &OutVals,
158                   DebugLoc dl, SelectionDAG &DAG) const;
159
160     SDValue ExpandStore(SDNode *N, SelectionDAG &DAG) const;
161     SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG) const;
162     SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) const;
163     SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG) const;
164     SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) const;
165
166     SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
167     SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
168     SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
169
170     // This function returns the Tmp Offset for FrameIndex. If any TmpOffset 
171     // already exists for the FI then it returns the same else it creates the 
172     // new offset and returns.
173     unsigned GetTmpOffsetForFI(unsigned FI, unsigned slot_size,
174                                MachineFunction &MF) const;
175     void ResetTmpOffsetMap(SelectionDAG &DAG) const;
176     void InitReservedFrameCount(const Function *F,
177                                 SelectionDAG &DAG) const;
178
179     /// getFunctionAlignment - Return the Log2 alignment of this function.
180     virtual unsigned getFunctionAlignment(const Function *) const {
181       // FIXME: The function never seems to be aligned.
182       return 1;
183     }
184   private:
185     // If the Node is a BUILD_PAIR representing a direct Address,
186     // then this function will return true.
187     bool isDirectAddress(const SDValue &Op) const;
188
189     // If the Node is a DirectAddress in ROM_SPACE then this 
190     // function will return true
191     bool isRomAddress(const SDValue &Op) const;
192
193     // Extract the Lo and Hi component of Op. 
194     void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo, 
195                           SDValue &Hi) const;
196
197
198     // Load pointer can be a direct or indirect address. In PIC16 direct
199     // addresses need Banksel and Indirect addresses need to be loaded to
200     // FSR first. Handle address specific cases here.
201     void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain, 
202                          SDValue &NewPtr, unsigned &Offset, DebugLoc dl) const;
203
204     // FrameIndex should be broken down into ExternalSymbol and FrameOffset. 
205     void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES, 
206                             int &Offset) const;
207
208     // For indirect calls data address of the callee frame need to be
209     // extracted. This function fills the arguments DataAddr_Lo and 
210     // DataAddr_Hi with the address of the callee frame.
211     void GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
212                         SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
213                         SelectionDAG &DAG) const;
214
215     // We can not have both operands of a binary operation in W.
216     // This function is used to put one operand on stack and generate a load.
217     SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG,
218                                 DebugLoc dl) const; 
219
220     // This function checks if we need to put an operand of an operation on
221     // stack and generate a load or not.
222     // DAG parameter is required to access DAG information during
223     // analysis.
224     bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp,
225                               SelectionDAG &DAG) const;
226
227     /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
228     /// make the right decision when generating code for different targets.
229     const PIC16Subtarget *Subtarget;
230
231
232     // Extending the LIB Call framework of LLVM
233     // to hold the names of PIC16Libcalls.
234     const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall]; 
235
236     // To set and retrieve the lib call names.
237     void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
238     const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) const;
239
240     // Make PIC16 Libcall.
241     SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, EVT RetVT, 
242                              const SDValue *Ops, unsigned NumOps, bool isSigned,
243                              SelectionDAG &DAG, DebugLoc dl) const;
244
245     // Check if operation has a direct load operand.
246     inline bool isDirectLoad(const SDValue Op) const;
247   };
248 } // namespace llvm
249
250 #endif // PIC16ISELLOWERING_H