498610a9753199f69576ba6e4c8987e7d21d16e3
[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,
42       MTHI,
43       BCF,
44       LSLF,          // PIC16 Logical shift left
45       LRLF,          // PIC16 Logical shift right
46       RLF,           // Rotate left through carry
47       RRF,           // Rotate right through carry
48       CALL,          // PIC16 Call instruction 
49       SUBCC,         // Compare for equality or inequality.
50       SELECT_ICC,    // Psuedo to be caught in schedular and expanded to brcond.
51       BRCOND,        // Conditional branch.
52       PIC16StopPoint,
53       Dummy
54     };
55
56     // Keep track of different address spaces. 
57     enum AddressSpace {
58       RAM_SPACE = 0,   // RAM address space
59       ROM_SPACE = 1    // ROM address space number is 1
60     };
61     enum PIC16Libcall {
62       MUL_I8,
63       SRA_I8,
64       SLL_I8,
65       SRL_I8,
66       PIC16UnknownCall
67     };
68   }
69
70
71   //===--------------------------------------------------------------------===//
72   // TargetLowering Implementation
73   //===--------------------------------------------------------------------===//
74   class PIC16TargetLowering : public TargetLowering {
75   public:
76     explicit PIC16TargetLowering(PIC16TargetMachine &TM);
77
78     /// getTargetNodeName - This method returns the name of a target specific
79     /// DAG node.
80     virtual const char *getTargetNodeName(unsigned Opcode) const;
81     /// getSetCCResultType - Return the ISD::SETCC ValueType
82     virtual MVT getSetCCResultType(MVT ValType) const;
83     SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG);
84     SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
85     SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
86     SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
87     SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
88     SDValue LowerCALL(SDValue Op, SelectionDAG &DAG);
89     SDValue LowerRET(SDValue Op, SelectionDAG &DAG);
90     SDValue LowerCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
91                             SDValue InFlag, SelectionDAG &DAG);
92     SDValue LowerCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
93                                SDValue InFlag, SelectionDAG &DAG);
94     SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
95     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
96     SDValue LowerStopPoint(SDValue Op, SelectionDAG &DAG);
97     SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
98                         SelectionDAG &DAG, DebugLoc dl);
99     virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
100                                                   MachineBasicBlock *MBB) const;
101
102
103     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
104     virtual void ReplaceNodeResults(SDNode *N,
105                                     SmallVectorImpl<SDValue> &Results,
106                                     SelectionDAG &DAG);
107     virtual void LowerOperationWrapper(SDNode *N,
108                                        SmallVectorImpl<SDValue> &Results,
109                                        SelectionDAG &DAG);
110
111     SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
112     SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
113     SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
114     SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
115     SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
116
117     SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
118     SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
119     SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
120
121     // This function returns the Tmp Offset for FrameIndex. If any TmpOffset 
122     // already exists for the FI then it returns the same else it creates the 
123     // new offset and returns.
124     unsigned GetTmpOffsetForFI(unsigned FI); 
125     void ResetTmpOffsetMap() { FiTmpOffsetMap.clear(); SetTmpSize(0); }
126
127     // Return the size of Tmp variable 
128     unsigned GetTmpSize() { return TmpSize; }
129     void SetTmpSize(unsigned Size) { TmpSize = Size; }
130
131   private:
132     // If the Node is a BUILD_PAIR representing a direct Address,
133     // then this function will return true.
134     bool isDirectAddress(const SDValue &Op);
135
136     // If the Node is a DirectAddress in ROM_SPACE then this 
137     // function will return true
138     bool isRomAddress(const SDValue &Op);
139
140     // Extract the Lo and Hi component of Op. 
141     void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo, 
142                           SDValue &Hi); 
143
144
145     // Load pointer can be a direct or indirect address. In PIC16 direct
146     // addresses need Banksel and Indirect addresses need to be loaded to
147     // FSR first. Handle address specific cases here.
148     void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain, 
149                          SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
150
151     // FrameIndex should be broken down into ExternalSymbol and FrameOffset. 
152     void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES, 
153                             int &Offset);
154
155     // We can not have both operands of a binary operation in W.
156     // This function is used to put one operand on stack and generate a load.
157     SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl); 
158
159     // This function checks if we need to put an operand of an operation on
160     // stack and generate a load or not.
161     bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp); 
162
163     /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
164     /// make the right decision when generating code for different targets.
165     const PIC16Subtarget *Subtarget;
166
167
168     // Extending the LIB Call framework of LLVM
169     // to hold the names of PIC16Libcalls.
170     const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall]; 
171
172     // To set and retrieve the lib call names.
173     void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
174     const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
175
176     // Make PIC16 Libcall.
177     SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT, 
178                              const SDValue *Ops, unsigned NumOps, bool isSigned,
179                              SelectionDAG &DAG, DebugLoc dl);
180
181     // Check if operation has a direct load operand.
182     inline bool isDirectLoad(const SDValue Op);
183
184   private:
185     // The frameindexes generated for spill/reload are stack based.
186     // This maps maintain zero based indexes for these FIs.
187     std::map<unsigned, unsigned> FiTmpOffsetMap;
188     unsigned TmpSize;
189   };
190 } // namespace llvm
191
192 #endif // PIC16ISELLOWERING_H