blob: e88fc2f18d299b8874e69634c976ee7c1b327ab8 [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
Evan Cheng022d9e12010-02-02 23:55:14 +0000145 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.
Dan Gohman1e93df62010-04-17 14:41:14 +0000171 unsigned GetTmpOffsetForFI(unsigned FI, unsigned slot_size,
172 MachineFunction &MF);
173 void ResetTmpOffsetMap(SelectionDAG &DAG);
174 void InitReservedFrameCount(const Function *F,
175 SelectionDAG &DAG);
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000176
Bill Wendlingb4202b82009-07-01 18:50:55 +0000177 /// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000178 virtual unsigned getFunctionAlignment(const Function *) const {
179 // FIXME: The function never seems to be aligned.
180 return 1;
181 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000182 private:
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000183 // If the Node is a BUILD_PAIR representing a direct Address,
184 // then this function will return true.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000185 bool isDirectAddress(const SDValue &Op);
186
187 // If the Node is a DirectAddress in ROM_SPACE then this
188 // function will return true
189 bool isRomAddress(const SDValue &Op);
190
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000191 // Extract the Lo and Hi component of Op.
192 void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo,
193 SDValue &Hi);
194
195
196 // Load pointer can be a direct or indirect address. In PIC16 direct
197 // addresses need Banksel and Indirect addresses need to be loaded to
198 // FSR first. Handle address specific cases here.
199 void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000200 SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000201
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000202 // FrameIndex should be broken down into ExternalSymbol and FrameOffset.
203 void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES,
204 int &Offset);
205
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000206 // For indirect calls data address of the callee frame need to be
207 // extracted. This function fills the arguments DataAddr_Lo and
208 // DataAddr_Hi with the address of the callee frame.
209 void GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
210 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
211 SelectionDAG &DAG);
212
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000213 // We can not have both operands of a binary operation in W.
214 // This function is used to put one operand on stack and generate a load.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000215 SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000216
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000217 // This function checks if we need to put an operand of an operation on
218 // stack and generate a load or not.
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +0000219 // DAG parameter is required to access DAG information during
220 // analysis.
221 bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000222
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000223 /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
224 /// make the right decision when generating code for different targets.
225 const PIC16Subtarget *Subtarget;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000226
227
228 // Extending the LIB Call framework of LLVM
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000229 // to hold the names of PIC16Libcalls.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000230 const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000231
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000232 // To set and retrieve the lib call names.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000233 void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
234 const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000235
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000236 // Make PIC16 Libcall.
Owen Andersone50ed302009-08-10 22:56:29 +0000237 SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, EVT RetVT,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000238 const SDValue *Ops, unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000239 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000240
241 // Check if operation has a direct load operand.
242 inline bool isDirectLoad(const SDValue Op);
243
Sanjiv Gupta76d2f9a2009-12-23 11:19:09 +0000244 public:
245 // Keep a pointer to SelectionDAGISel to access its public
246 // interface (It is required during legalization)
247 SelectionDAGISel *ISel;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000248 };
249} // namespace llvm
250
251#endif // PIC16ISELLOWERING_H