blob: de1452015f60b8f260bfea069f93609c432bb394 [file] [log] [blame]
Sanjiv Gupta0e687712008-05-13 09:02:57 +00001//===-- 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
Sanjiv Gupta0e687712008-05-13 09:02:57 +000018#include "PIC16.h"
19#include "PIC16Subtarget.h"
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000020#include "llvm/CodeGen/SelectionDAG.h"
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +000021#include "llvm/CodeGen/SelectionDAGISel.h"
Sanjiv Gupta2010b3e2008-05-14 11:31:39 +000022#include "llvm/Target/TargetLowering.h"
Sanjiv Guptacae1b622009-04-06 10:54:50 +000023#include <map>
Sanjiv Gupta0e687712008-05-13 09:02:57 +000024
25namespace llvm {
26 namespace PIC16ISD {
27 enum NodeType {
28 // Start the numbering from where ISD NodeType finishes.
Dan Gohman0ba2bcf2008-09-23 18:42:32 +000029 FIRST_NUMBER = ISD::BUILTIN_OP_END,
Sanjiv Gupta0e687712008-05-13 09:02:57 +000030
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000031 Lo, // Low 8-bits of GlobalAddress.
32 Hi, // High 8-bits of GlobalAddress.
33 PIC16Load,
Sanjiv Guptab84d5a42009-04-02 17:42:00 +000034 PIC16LdArg, // This is replica of PIC16Load but used to load function
35 // arguments and is being used for facilitating for some
36 // store removal optimizations.
37
Sanjiv Gupta1b046942009-01-13 19:18:47 +000038 PIC16LdWF,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000039 PIC16Store,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000040 PIC16StWF,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000041 Banksel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000042 MTLO, // Move to low part of FSR
43 MTHI, // Move to high part of FSR
44 MTPCLATH, // Move to PCLATCH
45 PIC16Connect, // General connector for PIC16 nodes
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000046 BCF,
47 LSLF, // PIC16 Logical shift left
48 LRLF, // PIC16 Logical shift right
49 RLF, // Rotate left through carry
50 RRF, // Rotate right through carry
Sanjiv Gupta1b046942009-01-13 19:18:47 +000051 CALL, // PIC16 Call instruction
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000052 CALLW, // PIC16 CALLW instruction
Bill Wendling51b16f42009-05-30 01:09:53 +000053 SUBCC, // Compare for equality or inequality.
Sanjiv Gupta1b046942009-01-13 19:18:47 +000054 SELECT_ICC, // Psuedo to be caught in schedular and expanded to brcond.
55 BRCOND, // Conditional branch.
Dan Gohman98ca4f22009-08-05 01:29:28 +000056 RET, // Return.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000057 Dummy
58 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000059
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000060 // Keep track of different address spaces.
61 enum AddressSpace {
62 RAM_SPACE = 0, // RAM address space
63 ROM_SPACE = 1 // ROM address space number is 1
Sanjiv Gupta0e687712008-05-13 09:02:57 +000064 };
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000065 enum PIC16Libcall {
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000066 MUL_I8 = RTLIB::UNKNOWN_LIBCALL + 1,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000067 SRA_I8,
68 SLL_I8,
69 SRL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000070 PIC16UnknownCall
71 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000072 }
73
Sanjiv Gupta1b046942009-01-13 19:18:47 +000074
Sanjiv Gupta0e687712008-05-13 09:02:57 +000075 //===--------------------------------------------------------------------===//
76 // TargetLowering Implementation
77 //===--------------------------------------------------------------------===//
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000078 class PIC16TargetLowering : public TargetLowering {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000079 public:
Sanjiv Gupta0e687712008-05-13 09:02:57 +000080 explicit PIC16TargetLowering(PIC16TargetMachine &TM);
81
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000082 /// getTargetNodeName - This method returns the name of a target specific
83 /// DAG node.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000084 virtual const char *getTargetNodeName(unsigned Opcode) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000085 /// getSetCCResultType - Return the ISD::SETCC ValueType
Owen Anderson825b72b2009-08-11 20:47:22 +000086 virtual MVT::SimpleValueType getSetCCResultType(EVT ValType) const;
Sanjiv Gupta8f17a362009-12-28 02:40:33 +000087 virtual MVT::SimpleValueType getCmpLibcallReturnType() const;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000088 SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta8e14eab2009-07-27 02:44:46 +000089 SDValue LowerMUL(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000090 SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
91 SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000092 SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000093 // Call returns
94 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000095 LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag,
96 const SmallVectorImpl<ISD::InputArg> &Ins,
97 DebugLoc dl, SelectionDAG &DAG,
98 SmallVectorImpl<SDValue> &InVals);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000099 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000100 LowerIndirectCallReturn(SDValue Chain, SDValue InFlag,
101 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
102 const SmallVectorImpl<ISD::InputArg> &Ins,
103 DebugLoc dl, SelectionDAG &DAG,
104 SmallVectorImpl<SDValue> &InVals);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000105
106 // Call arguments
107 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000108 LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag,
109 const SmallVectorImpl<ISD::OutputArg> &Outs,
110 DebugLoc dl, SelectionDAG &DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000111
112 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000113 LowerIndirectCallArguments(SDValue Chain, SDValue InFlag,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000114 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000115 const SmallVectorImpl<ISD::OutputArg> &Outs,
Sanjiv Guptaa4186282009-08-11 01:51:55 +0000116 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000117 DebugLoc dl, SelectionDAG &DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000118
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000119 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
120 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
121 SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000122 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000123 virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000124 MachineBasicBlock *MBB,
125 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000126
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000127
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000128 virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000129 virtual void ReplaceNodeResults(SDNode *N,
130 SmallVectorImpl<SDValue> &Results,
Duncan Sands1607f052008-12-01 11:39:25 +0000131 SelectionDAG &DAG);
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000132 virtual void LowerOperationWrapper(SDNode *N,
133 SmallVectorImpl<SDValue> &Results,
134 SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000135
Dan Gohman98ca4f22009-08-05 01:29:28 +0000136 virtual SDValue
137 LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000138 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000139 bool isVarArg,
140 const SmallVectorImpl<ISD::InputArg> &Ins,
141 DebugLoc dl, SelectionDAG &DAG,
142 SmallVectorImpl<SDValue> &InVals);
143
144 virtual SDValue
145 LowerCall(SDValue Chain, SDValue Callee,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000146 CallingConv::ID CallConv, bool isVarArg, bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000147 const SmallVectorImpl<ISD::OutputArg> &Outs,
148 const SmallVectorImpl<ISD::InputArg> &Ins,
149 DebugLoc dl, SelectionDAG &DAG,
150 SmallVectorImpl<SDValue> &InVals);
151
152 virtual SDValue
153 LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000154 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000155 const SmallVectorImpl<ISD::OutputArg> &Outs,
156 DebugLoc dl, SelectionDAG &DAG);
157
Duncan Sands1607f052008-12-01 11:39:25 +0000158 SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
159 SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
Duncan Sands1607f052008-12-01 11:39:25 +0000160 SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000161 SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000162 SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000163
164 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
165 SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000166 SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000167
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000168 // This function returns the Tmp Offset for FrameIndex. If any TmpOffset
169 // already exists for the FI then it returns the same else it creates the
170 // new offset and returns.
Sanjiv Guptaa3613be2009-04-10 15:10:14 +0000171 unsigned GetTmpOffsetForFI(unsigned FI, unsigned slot_size);
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000172 void ResetTmpOffsetMap() { FiTmpOffsetMap.clear(); SetTmpSize(0); }
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000173 void InitReservedFrameCount(const Function *F);
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000174
175 // Return the size of Tmp variable
176 unsigned GetTmpSize() { return TmpSize; }
177 void SetTmpSize(unsigned Size) { TmpSize = Size; }
178
Bill Wendlingb4202b82009-07-01 18:50:55 +0000179 /// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000180 virtual unsigned getFunctionAlignment(const Function *) const {
181 // FIXME: The function never seems to be aligned.
182 return 1;
183 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000184 private:
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000185 // If the Node is a BUILD_PAIR representing a direct Address,
186 // then this function will return true.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000187 bool isDirectAddress(const SDValue &Op);
188
189 // If the Node is a DirectAddress in ROM_SPACE then this
190 // function will return true
191 bool isRomAddress(const SDValue &Op);
192
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000193 // Extract the Lo and Hi component of Op.
194 void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo,
195 SDValue &Hi);
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,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000202 SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000203
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000204 // FrameIndex should be broken down into ExternalSymbol and FrameOffset.
205 void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES,
206 int &Offset);
207
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000208 // 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);
214
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000215 // 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.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000217 SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000218
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000219 // This function checks if we need to put an operand of an operation on
220 // stack and generate a load or not.
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +0000221 // DAG parameter is required to access DAG information during
222 // analysis.
223 bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000224
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000225 /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
226 /// make the right decision when generating code for different targets.
227 const PIC16Subtarget *Subtarget;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000228
229
230 // Extending the LIB Call framework of LLVM
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000231 // to hold the names of PIC16Libcalls.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000232 const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000233
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000234 // To set and retrieve the lib call names.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000235 void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
236 const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000237
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000238 // Make PIC16 Libcall.
Owen Andersone50ed302009-08-10 22:56:29 +0000239 SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, EVT RetVT,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000240 const SDValue *Ops, unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000241 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000242
243 // Check if operation has a direct load operand.
244 inline bool isDirectLoad(const SDValue Op);
245
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +0000246 public:
247 // Keep a pointer to SelectionDAGISel to access its public
248 // interface (It is required during legalization)
249 SelectionDAGISel *ISel;
250
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000251 private:
252 // The frameindexes generated for spill/reload are stack based.
253 // This maps maintain zero based indexes for these FIs.
254 std::map<unsigned, unsigned> FiTmpOffsetMap;
255 unsigned TmpSize;
Sanjiv Guptae16178b2009-04-21 05:54:51 +0000256
257 // These are the frames for return value and argument passing
258 // These FrameIndices will be expanded to foo.frame external symbol
259 // and all others will be expanded to foo.tmp external symbol.
260 unsigned ReservedFrameCount;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000261 };
262} // namespace llvm
263
264#endif // PIC16ISELLOWERING_H