3dae35277fa58251c7079794e3f716d9025d1e5f
[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
23 namespace llvm {
24   namespace PIC16ISD {
25     enum NodeType {
26       // Start the numbering from where ISD NodeType finishes.
27       FIRST_NUMBER = ISD::BUILTIN_OP_END,
28
29       Lo,            // Low 8-bits of GlobalAddress.
30       Hi,            // High 8-bits of GlobalAddress.
31       PIC16Load,
32       PIC16LdArg,   // This is replica of PIC16Load but used to load function 
33                     // arguments and is being used for facilitating for some 
34                     // store removal optimizations. 
35
36       PIC16LdWF,
37       PIC16Store,
38       PIC16StWF,
39       Banksel,
40       MTLO,
41       MTHI,
42       BCF,
43       LSLF,          // PIC16 Logical shift left
44       LRLF,          // PIC16 Logical shift right
45       RLF,           // Rotate left through carry
46       RRF,           // Rotate right through carry
47       CALL,          // PIC16 Call instruction 
48       SUBCC,         // Compare for equality or inequality.
49       SELECT_ICC,    // Psuedo to be caught in schedular and expanded to brcond.
50       BRCOND,        // Conditional branch.
51       PIC16StopPoint,
52       Dummy
53     };
54
55     // Keep track of different address spaces. 
56     enum AddressSpace {
57       RAM_SPACE = 0,   // RAM address space
58       ROM_SPACE = 1    // ROM address space number is 1
59     };
60     enum PIC16Libcall {
61       MUL_I8,
62       SRA_I8,
63       SLL_I8,
64       SRL_I8,
65       PIC16UnknownCall
66     };
67   }
68
69
70   //===--------------------------------------------------------------------===//
71   // TargetLowering Implementation
72   //===--------------------------------------------------------------------===//
73   class PIC16TargetLowering : public TargetLowering {
74   public:
75     explicit PIC16TargetLowering(PIC16TargetMachine &TM);
76
77     /// getTargetNodeName - This method returns the name of a target specific
78     /// DAG node.
79     virtual const char *getTargetNodeName(unsigned Opcode) const;
80     /// getSetCCResultType - Return the ISD::SETCC ValueType
81     virtual MVT getSetCCResultType(MVT ValType) const;
82     SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG);
83     SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
84     SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
85     SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
86     SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
87     SDValue LowerCALL(SDValue Op, SelectionDAG &DAG);
88     SDValue LowerRET(SDValue Op, SelectionDAG &DAG);
89     SDValue LowerCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
90                             SDValue InFlag, SelectionDAG &DAG);
91     SDValue LowerCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
92                                SDValue InFlag, SelectionDAG &DAG);
93     SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
94     SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
95     SDValue LowerStopPoint(SDValue Op, SelectionDAG &DAG);
96     SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
97                         SelectionDAG &DAG, DebugLoc dl);
98     virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
99                                                   MachineBasicBlock *MBB) const;
100
101
102     virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
103     virtual void ReplaceNodeResults(SDNode *N,
104                                     SmallVectorImpl<SDValue> &Results,
105                                     SelectionDAG &DAG);
106     virtual void LowerOperationWrapper(SDNode *N,
107                                        SmallVectorImpl<SDValue> &Results,
108                                        SelectionDAG &DAG);
109
110     SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
111     SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
112     SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
113     SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
114     SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
115
116     SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
117     SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
118     SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; 
119
120   private:
121     // If the Node is a BUILD_PAIR representing a direct Address,
122     // then this function will return true.
123     bool isDirectAddress(const SDValue &Op);
124
125     // If the Node is a DirectAddress in ROM_SPACE then this 
126     // function will return true
127     bool isRomAddress(const SDValue &Op);
128
129     // Extract the Lo and Hi component of Op. 
130     void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo, 
131                           SDValue &Hi); 
132
133
134     // Load pointer can be a direct or indirect address. In PIC16 direct
135     // addresses need Banksel and Indirect addresses need to be loaded to
136     // FSR first. Handle address specific cases here.
137     void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain, 
138                          SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
139
140     // FrameIndex should be broken down into ExternalSymbol and FrameOffset. 
141     void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES, 
142                             int &Offset);
143
144     // We can not have both operands of a binary operation in W.
145     // This function is used to put one operand on stack and generate a load.
146     SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl); 
147
148     // This function checks if we need to put an operand of an operation on
149     // stack and generate a load or not.
150     bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp); 
151
152     /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
153     /// make the right decision when generating code for different targets.
154     const PIC16Subtarget *Subtarget;
155
156
157     // Extending the LIB Call framework of LLVM
158     // to hold the names of PIC16Libcalls.
159     const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall]; 
160
161     // To set and retrieve the lib call names.
162     void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
163     const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
164
165     // Make PIC16 Libcall.
166     SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT, 
167                              const SDValue *Ops, unsigned NumOps, bool isSigned,
168                              SelectionDAG &DAG, DebugLoc dl);
169
170     // Check if operation has a direct load operand.
171     inline bool isDirectLoad(const SDValue Op);
172
173   };
174 } // namespace llvm
175
176 #endif // PIC16ISELLOWERING_H