blob: 5ddff9efed44e5e9d7a82d5e82253d614482faa5 [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"
21#include "llvm/Target/TargetLowering.h"
Sanjiv Guptacae1b622009-04-06 10:54:50 +000022#include <map>
Sanjiv Gupta0e687712008-05-13 09:02:57 +000023
24namespace llvm {
25 namespace PIC16ISD {
26 enum NodeType {
27 // Start the numbering from where ISD NodeType finishes.
Dan Gohman0ba2bcf2008-09-23 18:42:32 +000028 FIRST_NUMBER = ISD::BUILTIN_OP_END,
Sanjiv Gupta0e687712008-05-13 09:02:57 +000029
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000030 Lo, // Low 8-bits of GlobalAddress.
31 Hi, // High 8-bits of GlobalAddress.
32 PIC16Load,
Sanjiv Guptab84d5a42009-04-02 17:42:00 +000033 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
Sanjiv Gupta1b046942009-01-13 19:18:47 +000037 PIC16LdWF,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000038 PIC16Store,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000039 PIC16StWF,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000040 Banksel,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000041 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
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000045 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
Sanjiv Gupta1b046942009-01-13 19:18:47 +000050 CALL, // PIC16 Call instruction
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000051 CALLW, // PIC16 CALLW instruction
Bill Wendling51b16f42009-05-30 01:09:53 +000052 SUBCC, // Compare for equality or inequality.
Sanjiv Gupta1b046942009-01-13 19:18:47 +000053 SELECT_ICC, // Psuedo to be caught in schedular and expanded to brcond.
54 BRCOND, // Conditional branch.
Dan Gohman98ca4f22009-08-05 01:29:28 +000055 RET, // Return.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000056 Dummy
57 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000058
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000059 // Keep track of different address spaces.
60 enum AddressSpace {
61 RAM_SPACE = 0, // RAM address space
62 ROM_SPACE = 1 // ROM address space number is 1
Sanjiv Gupta0e687712008-05-13 09:02:57 +000063 };
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000064 enum PIC16Libcall {
Sanjiv Guptae0b4b0e2009-05-11 08:52:04 +000065 MUL_I8 = RTLIB::UNKNOWN_LIBCALL + 1,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000066 SRA_I8,
67 SLL_I8,
68 SRL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000069 PIC16UnknownCall
70 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000071 }
72
Sanjiv Gupta1b046942009-01-13 19:18:47 +000073
Sanjiv Gupta0e687712008-05-13 09:02:57 +000074 //===--------------------------------------------------------------------===//
75 // TargetLowering Implementation
76 //===--------------------------------------------------------------------===//
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000077 class PIC16TargetLowering : public TargetLowering {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000078 public:
Sanjiv Gupta0e687712008-05-13 09:02:57 +000079 explicit PIC16TargetLowering(PIC16TargetMachine &TM);
80
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000081 /// getTargetNodeName - This method returns the name of a target specific
82 /// DAG node.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000083 virtual const char *getTargetNodeName(unsigned Opcode) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000084 /// getSetCCResultType - Return the ISD::SETCC ValueType
Owen Anderson825b72b2009-08-11 20:47:22 +000085 virtual MVT::SimpleValueType getSetCCResultType(EVT ValType) const;
Sanjiv Gupta8f17a362009-12-28 02:40:33 +000086 virtual MVT::SimpleValueType getCmpLibcallReturnType() const;
Dan Gohmand858e902010-04-17 15:26:15 +000087 SDValue LowerShift(SDValue Op, SelectionDAG &DAG) const;
88 SDValue LowerMUL(SDValue Op, SelectionDAG &DAG) const;
89 SDValue LowerADD(SDValue Op, SelectionDAG &DAG) const;
90 SDValue LowerSUB(SDValue Op, SelectionDAG &DAG) const;
91 SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG) const;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000092 // Call returns
93 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000094 LowerDirectCallReturn(SDValue RetLabel, SDValue Chain, SDValue InFlag,
95 const SmallVectorImpl<ISD::InputArg> &Ins,
96 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +000097 SmallVectorImpl<SDValue> &InVals) const;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000098 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +000099 LowerIndirectCallReturn(SDValue Chain, SDValue InFlag,
100 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
101 const SmallVectorImpl<ISD::InputArg> &Ins,
102 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000103 SmallVectorImpl<SDValue> &InVals) const;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000104
105 // Call arguments
106 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000107 LowerDirectCallArguments(SDValue ArgLabel, SDValue Chain, SDValue InFlag,
108 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +0000109 DebugLoc dl, SelectionDAG &DAG) const;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000110
111 SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +0000112 LowerIndirectCallArguments(SDValue Chain, SDValue InFlag,
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000113 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000114 const SmallVectorImpl<ISD::OutputArg> &Outs,
Sanjiv Guptaa4186282009-08-11 01:51:55 +0000115 const SmallVectorImpl<ISD::InputArg> &Ins,
Dan Gohmand858e902010-04-17 15:26:15 +0000116 DebugLoc dl, SelectionDAG &DAG) const;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000117
Dan Gohmand858e902010-04-17 15:26:15 +0000118 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG) const;
119 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000120 SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
Dan Gohmand858e902010-04-17 15:26:15 +0000121 SelectionDAG &DAG, DebugLoc dl) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000122 virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
Evan Chengfb2e7522009-09-18 21:02:19 +0000123 MachineBasicBlock *MBB,
124 DenseMap<MachineBasicBlock*, MachineBasicBlock*> *EM) const;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000125
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000126
Dan Gohmand858e902010-04-17 15:26:15 +0000127 virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000128 virtual void ReplaceNodeResults(SDNode *N,
129 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +0000130 SelectionDAG &DAG) const;
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000131 virtual void LowerOperationWrapper(SDNode *N,
132 SmallVectorImpl<SDValue> &Results,
Dan Gohmand858e902010-04-17 15:26:15 +0000133 SelectionDAG &DAG) const;
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000134
Dan Gohman98ca4f22009-08-05 01:29:28 +0000135 virtual SDValue
136 LowerFormalArguments(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000137 CallingConv::ID CallConv,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000138 bool isVarArg,
139 const SmallVectorImpl<ISD::InputArg> &Ins,
140 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000141 SmallVectorImpl<SDValue> &InVals) const;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000142
143 virtual SDValue
Evan Cheng022d9e12010-02-02 23:55:14 +0000144 LowerCall(SDValue Chain, SDValue Callee,
Evan Cheng0c439eb2010-01-27 00:07:07 +0000145 CallingConv::ID CallConv, bool isVarArg, bool &isTailCall,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000146 const SmallVectorImpl<ISD::OutputArg> &Outs,
147 const SmallVectorImpl<ISD::InputArg> &Ins,
148 DebugLoc dl, SelectionDAG &DAG,
Dan Gohmand858e902010-04-17 15:26:15 +0000149 SmallVectorImpl<SDValue> &InVals) const;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000150
151 virtual SDValue
152 LowerReturn(SDValue Chain,
Sandeep Patel65c3c8f2009-09-02 08:44:58 +0000153 CallingConv::ID CallConv, bool isVarArg,
Dan Gohman98ca4f22009-08-05 01:29:28 +0000154 const SmallVectorImpl<ISD::OutputArg> &Outs,
Dan Gohmand858e902010-04-17 15:26:15 +0000155 DebugLoc dl, SelectionDAG &DAG) const;
Dan Gohman98ca4f22009-08-05 01:29:28 +0000156
Dan Gohmand858e902010-04-17 15:26:15 +0000157 SDValue ExpandStore(SDNode *N, SelectionDAG &DAG) const;
158 SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG) const;
159 SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG) const;
160 SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG) const;
161 SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000162
163 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
164 SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000165 SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000166
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000167 // This function returns the Tmp Offset for FrameIndex. If any TmpOffset
168 // already exists for the FI then it returns the same else it creates the
169 // new offset and returns.
Dan Gohman1e93df62010-04-17 14:41:14 +0000170 unsigned GetTmpOffsetForFI(unsigned FI, unsigned slot_size,
Dan Gohmand858e902010-04-17 15:26:15 +0000171 MachineFunction &MF) const;
172 void ResetTmpOffsetMap(SelectionDAG &DAG) const;
Dan Gohman1e93df62010-04-17 14:41:14 +0000173 void InitReservedFrameCount(const Function *F,
Dan Gohmand858e902010-04-17 15:26:15 +0000174 SelectionDAG &DAG) const;
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000175
Bill Wendlingb4202b82009-07-01 18:50:55 +0000176 /// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +0000177 virtual unsigned getFunctionAlignment(const Function *) const {
178 // FIXME: The function never seems to be aligned.
179 return 1;
180 }
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000181 private:
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000182 // If the Node is a BUILD_PAIR representing a direct Address,
183 // then this function will return true.
Dan Gohmand858e902010-04-17 15:26:15 +0000184 bool isDirectAddress(const SDValue &Op) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000185
186 // If the Node is a DirectAddress in ROM_SPACE then this
187 // function will return true
Dan Gohmand858e902010-04-17 15:26:15 +0000188 bool isRomAddress(const SDValue &Op) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000189
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000190 // Extract the Lo and Hi component of Op.
191 void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo,
Dan Gohmand858e902010-04-17 15:26:15 +0000192 SDValue &Hi) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000193
194
195 // Load pointer can be a direct or indirect address. In PIC16 direct
196 // addresses need Banksel and Indirect addresses need to be loaded to
197 // FSR first. Handle address specific cases here.
198 void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain,
Dan Gohmand858e902010-04-17 15:26:15 +0000199 SDValue &NewPtr, unsigned &Offset, DebugLoc dl) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000200
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000201 // FrameIndex should be broken down into ExternalSymbol and FrameOffset.
202 void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES,
Dan Gohmand858e902010-04-17 15:26:15 +0000203 int &Offset) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000204
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000205 // For indirect calls data address of the callee frame need to be
206 // extracted. This function fills the arguments DataAddr_Lo and
207 // DataAddr_Hi with the address of the callee frame.
208 void GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
209 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
Dan Gohmand858e902010-04-17 15:26:15 +0000210 SelectionDAG &DAG) const;
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000211
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000212 // We can not have both operands of a binary operation in W.
213 // This function is used to put one operand on stack and generate a load.
Dan Gohmand858e902010-04-17 15:26:15 +0000214 SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG,
215 DebugLoc dl) const;
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.
Dan Gohmand858e902010-04-17 15:26:15 +0000221 bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp,
222 SelectionDAG &DAG) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000223
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000224 /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
225 /// make the right decision when generating code for different targets.
226 const PIC16Subtarget *Subtarget;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000227
228
229 // Extending the LIB Call framework of LLVM
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000230 // to hold the names of PIC16Libcalls.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000231 const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000232
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000233 // To set and retrieve the lib call names.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000234 void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
Dan Gohmand858e902010-04-17 15:26:15 +0000235 const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000236
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000237 // Make PIC16 Libcall.
Owen Andersone50ed302009-08-10 22:56:29 +0000238 SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, EVT RetVT,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000239 const SDValue *Ops, unsigned NumOps, bool isSigned,
Dan Gohmand858e902010-04-17 15:26:15 +0000240 SelectionDAG &DAG, DebugLoc dl) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000241
242 // Check if operation has a direct load operand.
Dan Gohmand858e902010-04-17 15:26:15 +0000243 inline bool isDirectLoad(const SDValue Op) const;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000244 };
245} // namespace llvm
246
247#endif // PIC16ISELLOWERING_H