blob: 74e4507741ff1422c3739e19ae331ee78de61fc2 [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
Sanjiv Gupta1b046942009-01-13 19:18:47 +000052 SUBCC, // Compare for equality or inequality.
53 SELECT_ICC, // Psuedo to be caught in schedular and expanded to brcond.
54 BRCOND, // Conditional branch.
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000055 PIC16StopPoint,
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 {
65 MUL_I8,
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
85 virtual MVT getSetCCResultType(MVT ValType) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000086 SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000087 SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000088 SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
89 SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000090 SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000091 SDValue LowerCALL(SDValue Op, SelectionDAG &DAG);
92 SDValue LowerRET(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta7836fc12009-04-08 05:38:48 +000093 // Call returns
94 SDValue
95 LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
96 SDValue InFlag, SelectionDAG &DAG);
97 SDValue
98 LowerIndirectCallReturn(SDValue Op, SDValue Chain, SDValue InFlag,
99 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
100 SelectionDAG &DAG);
101
102 // Call arguments
103 SDValue
104 LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
105 SDValue InFlag, SelectionDAG &DAG);
106
107 SDValue
108 LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag,
109 SDValue DataAddr_Lo, SDValue DataAddr_Hi,
110 SelectionDAG &DAG);
111
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000112 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
113 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +0000114 SDValue LowerStopPoint(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000115 SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000116 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000117 virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000118 MachineBasicBlock *MBB) const;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000119
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000120
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000121 virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000122 virtual void ReplaceNodeResults(SDNode *N,
123 SmallVectorImpl<SDValue> &Results,
Duncan Sands1607f052008-12-01 11:39:25 +0000124 SelectionDAG &DAG);
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000125 virtual void LowerOperationWrapper(SDNode *N,
126 SmallVectorImpl<SDValue> &Results,
127 SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000128
Duncan Sands1607f052008-12-01 11:39:25 +0000129 SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
130 SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
Duncan Sands1607f052008-12-01 11:39:25 +0000131 SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000132 SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000133 SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000134
135 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
136 SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000137 SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000138
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000139 // This function returns the Tmp Offset for FrameIndex. If any TmpOffset
140 // already exists for the FI then it returns the same else it creates the
141 // new offset and returns.
142 unsigned GetTmpOffsetForFI(unsigned FI);
143 void ResetTmpOffsetMap() { FiTmpOffsetMap.clear(); SetTmpSize(0); }
144
145 // Return the size of Tmp variable
146 unsigned GetTmpSize() { return TmpSize; }
147 void SetTmpSize(unsigned Size) { TmpSize = Size; }
148
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000149 private:
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000150 // If the Node is a BUILD_PAIR representing a direct Address,
151 // then this function will return true.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000152 bool isDirectAddress(const SDValue &Op);
153
154 // If the Node is a DirectAddress in ROM_SPACE then this
155 // function will return true
156 bool isRomAddress(const SDValue &Op);
157
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000158 // Extract the Lo and Hi component of Op.
159 void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo,
160 SDValue &Hi);
161
162
163 // Load pointer can be a direct or indirect address. In PIC16 direct
164 // addresses need Banksel and Indirect addresses need to be loaded to
165 // FSR first. Handle address specific cases here.
166 void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000167 SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000168
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000169 // FrameIndex should be broken down into ExternalSymbol and FrameOffset.
170 void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES,
171 int &Offset);
172
Sanjiv Gupta7836fc12009-04-08 05:38:48 +0000173 // CALL node should have all legal operands only. Legalize all non-legal
174 // operands of CALL node and then return the new call will all operands
175 // legal.
176 SDValue LegalizeCALL(SDValue Op, SelectionDAG &DAG);
177
178 // For indirect calls data address of the callee frame need to be
179 // extracted. This function fills the arguments DataAddr_Lo and
180 // DataAddr_Hi with the address of the callee frame.
181 void GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain,
182 SDValue &DataAddr_Lo, SDValue &DataAddr_Hi,
183 SelectionDAG &DAG);
184
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000185 // We can not have both operands of a binary operation in W.
186 // This function is used to put one operand on stack and generate a load.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000187 SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000188
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000189 // This function checks if we need to put an operand of an operation on
190 // stack and generate a load or not.
191 bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp);
192
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000193 /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
194 /// make the right decision when generating code for different targets.
195 const PIC16Subtarget *Subtarget;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000196
197
198 // Extending the LIB Call framework of LLVM
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000199 // to hold the names of PIC16Libcalls.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000200 const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000201
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000202 // To set and retrieve the lib call names.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000203 void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
204 const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000205
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000206 // Make PIC16 Libcall.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000207 SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000208 const SDValue *Ops, unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000209 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000210
211 // Check if operation has a direct load operand.
212 inline bool isDirectLoad(const SDValue Op);
213
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000214 private:
215 // The frameindexes generated for spill/reload are stack based.
216 // This maps maintain zero based indexes for these FIs.
217 std::map<unsigned, unsigned> FiTmpOffsetMap;
218 unsigned TmpSize;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000219 };
220} // namespace llvm
221
222#endif // PIC16ISELLOWERING_H