blob: 498610a9753199f69576ba6e4c8987e7d21d16e3 [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,
41 MTLO,
42 MTHI,
43 BCF,
44 LSLF, // PIC16 Logical shift left
45 LRLF, // PIC16 Logical shift right
46 RLF, // Rotate left through carry
47 RRF, // Rotate right through carry
Sanjiv Gupta1b046942009-01-13 19:18:47 +000048 CALL, // PIC16 Call instruction
49 SUBCC, // Compare for equality or inequality.
50 SELECT_ICC, // Psuedo to be caught in schedular and expanded to brcond.
51 BRCOND, // Conditional branch.
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000052 PIC16StopPoint,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000053 Dummy
54 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000055
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000056 // Keep track of different address spaces.
57 enum AddressSpace {
58 RAM_SPACE = 0, // RAM address space
59 ROM_SPACE = 1 // ROM address space number is 1
Sanjiv Gupta0e687712008-05-13 09:02:57 +000060 };
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000061 enum PIC16Libcall {
62 MUL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000063 SRA_I8,
64 SLL_I8,
65 SRL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000066 PIC16UnknownCall
67 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000068 }
69
Sanjiv Gupta1b046942009-01-13 19:18:47 +000070
Sanjiv Gupta0e687712008-05-13 09:02:57 +000071 //===--------------------------------------------------------------------===//
72 // TargetLowering Implementation
73 //===--------------------------------------------------------------------===//
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000074 class PIC16TargetLowering : public TargetLowering {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000075 public:
Sanjiv Gupta0e687712008-05-13 09:02:57 +000076 explicit PIC16TargetLowering(PIC16TargetMachine &TM);
77
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000078 /// getTargetNodeName - This method returns the name of a target specific
79 /// DAG node.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000080 virtual const char *getTargetNodeName(unsigned Opcode) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000081 /// getSetCCResultType - Return the ISD::SETCC ValueType
82 virtual MVT getSetCCResultType(MVT ValType) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000083 SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000084 SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000085 SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
86 SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000087 SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000088 SDValue LowerCALL(SDValue Op, SelectionDAG &DAG);
89 SDValue LowerRET(SDValue Op, SelectionDAG &DAG);
90 SDValue LowerCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
91 SDValue InFlag, SelectionDAG &DAG);
92 SDValue LowerCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
93 SDValue InFlag, SelectionDAG &DAG);
94 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
95 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000096 SDValue LowerStopPoint(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000097 SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +000098 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000099 virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +0000100 MachineBasicBlock *MBB) const;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000101
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000102
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000103 virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000104 virtual void ReplaceNodeResults(SDNode *N,
105 SmallVectorImpl<SDValue> &Results,
Duncan Sands1607f052008-12-01 11:39:25 +0000106 SelectionDAG &DAG);
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000107 virtual void LowerOperationWrapper(SDNode *N,
108 SmallVectorImpl<SDValue> &Results,
109 SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000110
Duncan Sands1607f052008-12-01 11:39:25 +0000111 SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
112 SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
Duncan Sands1607f052008-12-01 11:39:25 +0000113 SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000114 SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000115 SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000116
117 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
118 SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000119 SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000120
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000121 // This function returns the Tmp Offset for FrameIndex. If any TmpOffset
122 // already exists for the FI then it returns the same else it creates the
123 // new offset and returns.
124 unsigned GetTmpOffsetForFI(unsigned FI);
125 void ResetTmpOffsetMap() { FiTmpOffsetMap.clear(); SetTmpSize(0); }
126
127 // Return the size of Tmp variable
128 unsigned GetTmpSize() { return TmpSize; }
129 void SetTmpSize(unsigned Size) { TmpSize = Size; }
130
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000131 private:
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000132 // If the Node is a BUILD_PAIR representing a direct Address,
133 // then this function will return true.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000134 bool isDirectAddress(const SDValue &Op);
135
136 // If the Node is a DirectAddress in ROM_SPACE then this
137 // function will return true
138 bool isRomAddress(const SDValue &Op);
139
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000140 // Extract the Lo and Hi component of Op.
141 void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo,
142 SDValue &Hi);
143
144
145 // Load pointer can be a direct or indirect address. In PIC16 direct
146 // addresses need Banksel and Indirect addresses need to be loaded to
147 // FSR first. Handle address specific cases here.
148 void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000149 SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000150
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000151 // FrameIndex should be broken down into ExternalSymbol and FrameOffset.
152 void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES,
153 int &Offset);
154
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000155 // We can not have both operands of a binary operation in W.
156 // This function is used to put one operand on stack and generate a load.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000157 SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000158
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000159 // This function checks if we need to put an operand of an operation on
160 // stack and generate a load or not.
161 bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp);
162
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000163 /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
164 /// make the right decision when generating code for different targets.
165 const PIC16Subtarget *Subtarget;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000166
167
168 // Extending the LIB Call framework of LLVM
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000169 // to hold the names of PIC16Libcalls.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000170 const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000171
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000172 // To set and retrieve the lib call names.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000173 void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
174 const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000175
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000176 // Make PIC16 Libcall.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000177 SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000178 const SDValue *Ops, unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000179 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000180
181 // Check if operation has a direct load operand.
182 inline bool isDirectLoad(const SDValue Op);
183
Sanjiv Guptacae1b622009-04-06 10:54:50 +0000184 private:
185 // The frameindexes generated for spill/reload are stack based.
186 // This maps maintain zero based indexes for these FIs.
187 std::map<unsigned, unsigned> FiTmpOffsetMap;
188 unsigned TmpSize;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000189 };
190} // namespace llvm
191
192#endif // PIC16ISELLOWERING_H