pass the mangler down into the various SectionForGlobal methods.
[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       Dummy
56     };
57
58     // Keep track of different address spaces. 
59     enum AddressSpace {
60       RAM_SPACE = 0,   // RAM address space
61       ROM_SPACE = 1    // ROM address space number is 1
62     };
63     enum PIC16Libcall {
64       MUL_I8 = RTLIB::UNKNOWN_LIBCALL + 1,
65       SRA_I8,
66       SLL_I8,
67       SRL_I8,
68       PIC16UnknownCall
69     };
70   }
71
72
73   //===--------------------------------------------------------------------===//
74   // TargetLowering Implementation
75   //===--------------------------------------------------------------------===//
76   class PIC16TargetLowering : public TargetLowering {
77   public:
78     explicit PIC16TargetLowering(PIC16TargetMachine &TM);
79
80     /// getTargetNodeName - This method returns the name of a target specific
81     /// DAG node.
82     virtual const char *getTargetNodeName(unsigned Opcode) const;
83     /// getSetCCResultType - Return the ISD::SETCC ValueType
84     virtual MVT getSetCCResultType(MVT ValType) const;
85     SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG);
86     SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
87     SDValue LowerMUL(SDValue Op, SelectionDAG &DAG);
88     SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
89     SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
90     SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
91     SDValue LowerCALL(SDValue Op, SelectionDAG &DAG);
92     SDValue LowerRET(SDValue Op, SelectionDAG &DAG);
93     // Call returns
94     SDValue 
95     LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress, 
96                           SDValue InFlag, SelectionDAG &DAG);
97     SDValue 
98     LowerIndirectCallReturn(SDValue Op, SDValue Chain, SDValue InFlag,
99                             SDValue DataAddr_Lo, SDValue DataAddr_Hi,
100                             SelectionDAG &DAG);
101
102     // Call arguments
103     SDValue 
104     LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress, 
105                              SDValue InFlag, SelectionDAG &DAG);
106
107     SDValue 
108     LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag, 
109                                SDValue DataAddr_Lo, SDValue DataAddr_Hi, 
110                                SelectionDAG &DAG);
111
112     SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
113     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
114     SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
115                         SelectionDAG &DAG, DebugLoc dl);
116     virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
117                                                   MachineBasicBlock *MBB) const;
118
119
120     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
121     virtual void ReplaceNodeResults(SDNode *N,
122                                     SmallVectorImpl<SDValue> &Results,
123                                     SelectionDAG &DAG);
124     virtual void LowerOperationWrapper(SDNode *N,
125                                        SmallVectorImpl<SDValue> &Results,
126                                        SelectionDAG &DAG);
127
128     SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
129     SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
130     SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
131     SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
132     SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
133
134     SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
135     SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
136     SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
137
138     // This function returns the Tmp Offset for FrameIndex. If any TmpOffset 
139     // already exists for the FI then it returns the same else it creates the 
140     // new offset and returns.
141     unsigned GetTmpOffsetForFI(unsigned FI, unsigned slot_size); 
142     void ResetTmpOffsetMap() { FiTmpOffsetMap.clear(); SetTmpSize(0); }
143     void InitReservedFrameCount(const Function *F); 
144
145     // Return the size of Tmp variable 
146     unsigned GetTmpSize() { return TmpSize; }
147     void SetTmpSize(unsigned Size) { TmpSize = Size; }
148
149     /// getFunctionAlignment - Return the Log2 alignment of this function.
150     virtual unsigned getFunctionAlignment(const Function *) const {
151       // FIXME: The function never seems to be aligned.
152       return 1;
153     }
154   private:
155     // If the Node is a BUILD_PAIR representing a direct Address,
156     // then this function will return true.
157     bool isDirectAddress(const SDValue &Op);
158
159     // If the Node is a DirectAddress in ROM_SPACE then this 
160     // function will return true
161     bool isRomAddress(const SDValue &Op);
162
163     // Extract the Lo and Hi component of Op. 
164     void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo, 
165                           SDValue &Hi); 
166
167
168     // Load pointer can be a direct or indirect address. In PIC16 direct
169     // addresses need Banksel and Indirect addresses need to be loaded to
170     // FSR first. Handle address specific cases here.
171     void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain, 
172                          SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
173
174     // FrameIndex should be broken down into ExternalSymbol and FrameOffset. 
175     void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES, 
176                             int &Offset);
177
178
179     // CALL node should have all legal operands only. Legalize all non-legal
180     // operands of CALL node and then return the new call will all operands
181     // legal.
182     SDValue LegalizeCALL(SDValue Op, SelectionDAG &DAG);
183
184     // For indirect calls data address of the callee frame need to be
185     // extracted. This function fills the arguments DataAddr_Lo and 
186     // DataAddr_Hi with the address of the callee frame.
187     void GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
188                         SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
189                         SelectionDAG &DAG); 
190
191     // We can not have both operands of a binary operation in W.
192     // This function is used to put one operand on stack and generate a load.
193     SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl); 
194
195     // This function checks if we need to put an operand of an operation on
196     // stack and generate a load or not.
197     bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp); 
198
199     /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
200     /// make the right decision when generating code for different targets.
201     const PIC16Subtarget *Subtarget;
202
203
204     // Extending the LIB Call framework of LLVM
205     // to hold the names of PIC16Libcalls.
206     const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall]; 
207
208     // To set and retrieve the lib call names.
209     void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
210     const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
211
212     // Make PIC16 Libcall.
213     SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT, 
214                              const SDValue *Ops, unsigned NumOps, bool isSigned,
215                              SelectionDAG &DAG, DebugLoc dl);
216
217     // Check if operation has a direct load operand.
218     inline bool isDirectLoad(const SDValue Op);
219
220   private:
221     // The frameindexes generated for spill/reload are stack based.
222     // This maps maintain zero based indexes for these FIs.
223     std::map<unsigned, unsigned> FiTmpOffsetMap;
224     unsigned TmpSize;
225
226     // These are the frames for return value and argument passing 
227     // These FrameIndices will be expanded to foo.frame external symbol
228     // and all others will be expanded to foo.tmp external symbol.
229     unsigned ReservedFrameCount; 
230   };
231 } // namespace llvm
232
233 #endif // PIC16ISELLOWERING_H