blob: 3dae35277fa58251c7079794e3f716d9025d1e5f [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 Gupta0e687712008-05-13 09:02:57 +000022
23namespace llvm {
24 namespace PIC16ISD {
25 enum NodeType {
26 // Start the numbering from where ISD NodeType finishes.
Dan Gohman0ba2bcf2008-09-23 18:42:32 +000027 FIRST_NUMBER = ISD::BUILTIN_OP_END,
Sanjiv Gupta0e687712008-05-13 09:02:57 +000028
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000029 Lo, // Low 8-bits of GlobalAddress.
30 Hi, // High 8-bits of GlobalAddress.
31 PIC16Load,
Sanjiv Guptab84d5a42009-04-02 17:42:00 +000032 PIC16LdArg, // This is replica of PIC16Load but used to load function
33 // arguments and is being used for facilitating for some
34 // store removal optimizations.
35
Sanjiv Gupta1b046942009-01-13 19:18:47 +000036 PIC16LdWF,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000037 PIC16Store,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000038 PIC16StWF,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000039 Banksel,
40 MTLO,
41 MTHI,
42 BCF,
43 LSLF, // PIC16 Logical shift left
44 LRLF, // PIC16 Logical shift right
45 RLF, // Rotate left through carry
46 RRF, // Rotate right through carry
Sanjiv Gupta1b046942009-01-13 19:18:47 +000047 CALL, // PIC16 Call instruction
48 SUBCC, // Compare for equality or inequality.
49 SELECT_ICC, // Psuedo to be caught in schedular and expanded to brcond.
50 BRCOND, // Conditional branch.
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000051 PIC16StopPoint,
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000052 Dummy
53 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000054
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000055 // Keep track of different address spaces.
56 enum AddressSpace {
57 RAM_SPACE = 0, // RAM address space
58 ROM_SPACE = 1 // ROM address space number is 1
Sanjiv Gupta0e687712008-05-13 09:02:57 +000059 };
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000060 enum PIC16Libcall {
61 MUL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000062 SRA_I8,
63 SLL_I8,
64 SRL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000065 PIC16UnknownCall
66 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000067 }
68
Sanjiv Gupta1b046942009-01-13 19:18:47 +000069
Sanjiv Gupta0e687712008-05-13 09:02:57 +000070 //===--------------------------------------------------------------------===//
71 // TargetLowering Implementation
72 //===--------------------------------------------------------------------===//
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000073 class PIC16TargetLowering : public TargetLowering {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000074 public:
Sanjiv Gupta0e687712008-05-13 09:02:57 +000075 explicit PIC16TargetLowering(PIC16TargetMachine &TM);
76
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000077 /// getTargetNodeName - This method returns the name of a target specific
78 /// DAG node.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000079 virtual const char *getTargetNodeName(unsigned Opcode) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000080 /// getSetCCResultType - Return the ISD::SETCC ValueType
81 virtual MVT getSetCCResultType(MVT ValType) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000082 SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000083 SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000084 SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
85 SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000086 SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000087 SDValue LowerCALL(SDValue Op, SelectionDAG &DAG);
88 SDValue LowerRET(SDValue Op, SelectionDAG &DAG);
89 SDValue LowerCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
90 SDValue InFlag, SelectionDAG &DAG);
91 SDValue LowerCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
92 SDValue InFlag, SelectionDAG &DAG);
93 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
94 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta5274a4a2009-04-02 18:03:10 +000095 SDValue LowerStopPoint(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000096 SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +000097 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000098 virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000099 MachineBasicBlock *MBB) const;
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000100
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000101
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000102 virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000103 virtual void ReplaceNodeResults(SDNode *N,
104 SmallVectorImpl<SDValue> &Results,
Duncan Sands1607f052008-12-01 11:39:25 +0000105 SelectionDAG &DAG);
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000106 virtual void LowerOperationWrapper(SDNode *N,
107 SmallVectorImpl<SDValue> &Results,
108 SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000109
Duncan Sands1607f052008-12-01 11:39:25 +0000110 SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
111 SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
Duncan Sands1607f052008-12-01 11:39:25 +0000112 SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000113 SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000114 SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000115
116 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
117 SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000118 SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000119
120 private:
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000121 // If the Node is a BUILD_PAIR representing a direct Address,
122 // then this function will return true.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000123 bool isDirectAddress(const SDValue &Op);
124
125 // If the Node is a DirectAddress in ROM_SPACE then this
126 // function will return true
127 bool isRomAddress(const SDValue &Op);
128
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000129 // Extract the Lo and Hi component of Op.
130 void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo,
131 SDValue &Hi);
132
133
134 // Load pointer can be a direct or indirect address. In PIC16 direct
135 // addresses need Banksel and Indirect addresses need to be loaded to
136 // FSR first. Handle address specific cases here.
137 void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000138 SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000139
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000140 // FrameIndex should be broken down into ExternalSymbol and FrameOffset.
141 void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES,
142 int &Offset);
143
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000144 // We can not have both operands of a binary operation in W.
145 // This function is used to put one operand on stack and generate a load.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000146 SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000147
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000148 // This function checks if we need to put an operand of an operation on
149 // stack and generate a load or not.
150 bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp);
151
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000152 /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
153 /// make the right decision when generating code for different targets.
154 const PIC16Subtarget *Subtarget;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000155
156
157 // Extending the LIB Call framework of LLVM
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000158 // to hold the names of PIC16Libcalls.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000159 const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000160
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000161 // To set and retrieve the lib call names.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000162 void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
163 const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000164
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000165 // Make PIC16 Libcall.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000166 SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000167 const SDValue *Ops, unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000168 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000169
170 // Check if operation has a direct load operand.
171 inline bool isDirectLoad(const SDValue Op);
172
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000173 };
174} // namespace llvm
175
176#endif // PIC16ISELLOWERING_H