Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 1 | //===-- 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 Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 18 | #include "PIC16.h" |
| 19 | #include "PIC16Subtarget.h" |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/SelectionDAG.h" |
| 21 | #include "llvm/Target/TargetLowering.h" |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 22 | #include <map> |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | namespace PIC16ISD { |
| 26 | enum NodeType { |
| 27 | // Start the numbering from where ISD NodeType finishes. |
Dan Gohman | 0ba2bcf | 2008-09-23 18:42:32 +0000 | [diff] [blame] | 28 | FIRST_NUMBER = ISD::BUILTIN_OP_END, |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 29 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 30 | Lo, // Low 8-bits of GlobalAddress. |
| 31 | Hi, // High 8-bits of GlobalAddress. |
| 32 | PIC16Load, |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 33 | 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 Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 37 | PIC16LdWF, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 38 | PIC16Store, |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 39 | PIC16StWF, |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 40 | Banksel, |
Sanjiv Gupta | 7836fc1 | 2009-04-08 05:38:48 +0000 | [diff] [blame] | 41 | 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 Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 45 | 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 Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 50 | CALL, // PIC16 Call instruction |
Sanjiv Gupta | 7836fc1 | 2009-04-08 05:38:48 +0000 | [diff] [blame] | 51 | CALLW, // PIC16 CALLW instruction |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 52 | SUBCC, // Compare for equality or inequality. |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 53 | SELECT_ICC, // Psuedo to be caught in schedular and expanded to brcond. |
| 54 | BRCOND, // Conditional branch. |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 55 | Dummy |
| 56 | }; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 57 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 58 | // Keep track of different address spaces. |
| 59 | enum AddressSpace { |
| 60 | RAM_SPACE = 0, // RAM address space |
| 61 | ROM_SPACE = 1 // ROM address space number is 1 |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 62 | }; |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 63 | enum PIC16Libcall { |
Sanjiv Gupta | e0b4b0e | 2009-05-11 08:52:04 +0000 | [diff] [blame] | 64 | MUL_I8 = RTLIB::UNKNOWN_LIBCALL + 1, |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 65 | SRA_I8, |
| 66 | SLL_I8, |
| 67 | SRL_I8, |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 68 | PIC16UnknownCall |
| 69 | }; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 72 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 73 | //===--------------------------------------------------------------------===// |
| 74 | // TargetLowering Implementation |
| 75 | //===--------------------------------------------------------------------===// |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 76 | class PIC16TargetLowering : public TargetLowering { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 77 | public: |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 78 | explicit PIC16TargetLowering(PIC16TargetMachine &TM); |
| 79 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 80 | /// getTargetNodeName - This method returns the name of a target specific |
| 81 | /// DAG node. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 82 | virtual const char *getTargetNodeName(unsigned Opcode) const; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 83 | /// getSetCCResultType - Return the ISD::SETCC ValueType |
| 84 | virtual MVT getSetCCResultType(MVT ValType) const; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 85 | SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG); |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 86 | SDValue LowerShift(SDValue Op, SelectionDAG &DAG); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 87 | SDValue LowerADD(SDValue Op, SelectionDAG &DAG); |
| 88 | SDValue LowerSUB(SDValue Op, SelectionDAG &DAG); |
Sanjiv Gupta | 8f78fa8 | 2008-11-26 10:53:50 +0000 | [diff] [blame] | 89 | SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 90 | SDValue LowerCALL(SDValue Op, SelectionDAG &DAG); |
| 91 | SDValue LowerRET(SDValue Op, SelectionDAG &DAG); |
Sanjiv Gupta | 7836fc1 | 2009-04-08 05:38:48 +0000 | [diff] [blame] | 92 | // Call returns |
| 93 | SDValue |
| 94 | LowerDirectCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress, |
| 95 | SDValue InFlag, SelectionDAG &DAG); |
| 96 | SDValue |
| 97 | LowerIndirectCallReturn(SDValue Op, SDValue Chain, SDValue InFlag, |
| 98 | SDValue DataAddr_Lo, SDValue DataAddr_Hi, |
| 99 | SelectionDAG &DAG); |
| 100 | |
| 101 | // Call arguments |
| 102 | SDValue |
| 103 | LowerDirectCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress, |
| 104 | SDValue InFlag, SelectionDAG &DAG); |
| 105 | |
| 106 | SDValue |
| 107 | LowerIndirectCallArguments(SDValue Op, SDValue Chain, SDValue InFlag, |
| 108 | SDValue DataAddr_Lo, SDValue DataAddr_Hi, |
| 109 | SelectionDAG &DAG); |
| 110 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 111 | SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG); |
| 112 | SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG); |
| 113 | SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC, |
Dale Johannesen | 2fabcb2 | 2009-02-05 01:01:16 +0000 | [diff] [blame] | 114 | SelectionDAG &DAG, DebugLoc dl); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 115 | virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI, |
Dan Gohman | 1fdbc1d | 2009-02-07 16:15:20 +0000 | [diff] [blame] | 116 | MachineBasicBlock *MBB) const; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 117 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 118 | |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 119 | virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 120 | virtual void ReplaceNodeResults(SDNode *N, |
| 121 | SmallVectorImpl<SDValue> &Results, |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 122 | SelectionDAG &DAG); |
Duncan Sands | 9fbc7e2 | 2009-01-21 09:00:29 +0000 | [diff] [blame] | 123 | virtual void LowerOperationWrapper(SDNode *N, |
| 124 | SmallVectorImpl<SDValue> &Results, |
| 125 | SelectionDAG &DAG); |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 126 | |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 127 | SDValue ExpandStore(SDNode *N, SelectionDAG &DAG); |
| 128 | SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG); |
Duncan Sands | 1607f05 | 2008-12-01 11:39:25 +0000 | [diff] [blame] | 129 | SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 130 | SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 131 | SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 132 | |
| 133 | SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const; |
| 134 | SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const; |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 135 | SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const; |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 136 | |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 137 | // This function returns the Tmp Offset for FrameIndex. If any TmpOffset |
| 138 | // already exists for the FI then it returns the same else it creates the |
| 139 | // new offset and returns. |
Sanjiv Gupta | a3613be | 2009-04-10 15:10:14 +0000 | [diff] [blame] | 140 | unsigned GetTmpOffsetForFI(unsigned FI, unsigned slot_size); |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 141 | void ResetTmpOffsetMap() { FiTmpOffsetMap.clear(); SetTmpSize(0); } |
Sanjiv Gupta | e16178b | 2009-04-21 05:54:51 +0000 | [diff] [blame] | 142 | void InitReservedFrameCount(const Function *F); |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 143 | |
| 144 | // Return the size of Tmp variable |
| 145 | unsigned GetTmpSize() { return TmpSize; } |
| 146 | void SetTmpSize(unsigned Size) { TmpSize = Size; } |
| 147 | |
Bill Wendling | b4202b8 | 2009-07-01 18:50:55 +0000 | [diff] [blame^] | 148 | /// getFunctionAlignment - Return the Log2 alignment of this function. |
Bill Wendling | 20c568f | 2009-06-30 22:38:32 +0000 | [diff] [blame] | 149 | virtual unsigned getFunctionAlignment(const Function *) const { |
| 150 | // FIXME: The function never seems to be aligned. |
| 151 | return 1; |
| 152 | } |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 153 | private: |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 154 | // If the Node is a BUILD_PAIR representing a direct Address, |
| 155 | // then this function will return true. |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 156 | bool isDirectAddress(const SDValue &Op); |
| 157 | |
| 158 | // If the Node is a DirectAddress in ROM_SPACE then this |
| 159 | // function will return true |
| 160 | bool isRomAddress(const SDValue &Op); |
| 161 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 162 | // Extract the Lo and Hi component of Op. |
| 163 | void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo, |
| 164 | SDValue &Hi); |
| 165 | |
| 166 | |
| 167 | // Load pointer can be a direct or indirect address. In PIC16 direct |
| 168 | // addresses need Banksel and Indirect addresses need to be loaded to |
| 169 | // FSR first. Handle address specific cases here. |
| 170 | void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain, |
Dale Johannesen | b300d2a | 2009-02-07 00:55:49 +0000 | [diff] [blame] | 171 | SDValue &NewPtr, unsigned &Offset, DebugLoc dl); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 172 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 173 | // FrameIndex should be broken down into ExternalSymbol and FrameOffset. |
| 174 | void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES, |
| 175 | int &Offset); |
| 176 | |
Sanjiv Gupta | 85be408 | 2009-04-14 02:49:52 +0000 | [diff] [blame] | 177 | |
Sanjiv Gupta | 7836fc1 | 2009-04-08 05:38:48 +0000 | [diff] [blame] | 178 | // CALL node should have all legal operands only. Legalize all non-legal |
| 179 | // operands of CALL node and then return the new call will all operands |
| 180 | // legal. |
| 181 | SDValue LegalizeCALL(SDValue Op, SelectionDAG &DAG); |
| 182 | |
| 183 | // For indirect calls data address of the callee frame need to be |
| 184 | // extracted. This function fills the arguments DataAddr_Lo and |
| 185 | // DataAddr_Hi with the address of the callee frame. |
| 186 | void GetDataAddress(DebugLoc dl, SDValue Callee, SDValue &Chain, |
| 187 | SDValue &DataAddr_Lo, SDValue &DataAddr_Hi, |
| 188 | SelectionDAG &DAG); |
| 189 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 190 | // We can not have both operands of a binary operation in W. |
| 191 | // This function is used to put one operand on stack and generate a load. |
Dale Johannesen | 2fabcb2 | 2009-02-05 01:01:16 +0000 | [diff] [blame] | 192 | SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl); |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 193 | |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 194 | // This function checks if we need to put an operand of an operation on |
| 195 | // stack and generate a load or not. |
| 196 | bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp); |
| 197 | |
Sanjiv Gupta | b1b5ffd | 2008-11-19 11:00:54 +0000 | [diff] [blame] | 198 | /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can |
| 199 | /// make the right decision when generating code for different targets. |
| 200 | const PIC16Subtarget *Subtarget; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 201 | |
| 202 | |
| 203 | // Extending the LIB Call framework of LLVM |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 204 | // to hold the names of PIC16Libcalls. |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 205 | const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall]; |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 206 | |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 207 | // To set and retrieve the lib call names. |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 208 | void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name); |
| 209 | const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 210 | |
Sanjiv Gupta | b84d5a4 | 2009-04-02 17:42:00 +0000 | [diff] [blame] | 211 | // Make PIC16 Libcall. |
Sanjiv Gupta | 08b9b05 | 2009-01-21 05:44:05 +0000 | [diff] [blame] | 212 | SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT, |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 213 | const SDValue *Ops, unsigned NumOps, bool isSigned, |
Dale Johannesen | 7d2ad62 | 2009-01-30 23:10:59 +0000 | [diff] [blame] | 214 | SelectionDAG &DAG, DebugLoc dl); |
Sanjiv Gupta | 1b04694 | 2009-01-13 19:18:47 +0000 | [diff] [blame] | 215 | |
| 216 | // Check if operation has a direct load operand. |
| 217 | inline bool isDirectLoad(const SDValue Op); |
| 218 | |
Sanjiv Gupta | cae1b62 | 2009-04-06 10:54:50 +0000 | [diff] [blame] | 219 | private: |
| 220 | // The frameindexes generated for spill/reload are stack based. |
| 221 | // This maps maintain zero based indexes for these FIs. |
| 222 | std::map<unsigned, unsigned> FiTmpOffsetMap; |
| 223 | unsigned TmpSize; |
Sanjiv Gupta | e16178b | 2009-04-21 05:54:51 +0000 | [diff] [blame] | 224 | |
| 225 | // These are the frames for return value and argument passing |
| 226 | // These FrameIndices will be expanded to foo.frame external symbol |
| 227 | // and all others will be expanded to foo.tmp external symbol. |
| 228 | unsigned ReservedFrameCount; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 229 | }; |
| 230 | } // namespace llvm |
| 231 | |
| 232 | #endif // PIC16ISELLOWERING_H |