blob: b2a89db3ea83821948cfb510d48e6e586e542812 [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 Guptab1b5ffd2008-11-19 11:00:54 +000051 Dummy
52 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000053
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000054 // Keep track of different address spaces.
55 enum AddressSpace {
56 RAM_SPACE = 0, // RAM address space
57 ROM_SPACE = 1 // ROM address space number is 1
Sanjiv Gupta0e687712008-05-13 09:02:57 +000058 };
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000059 enum PIC16Libcall {
60 MUL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000061 SRA_I8,
62 SLL_I8,
63 SRL_I8,
Sanjiv Gupta1b046942009-01-13 19:18:47 +000064 PIC16UnknownCall
65 };
Sanjiv Gupta0e687712008-05-13 09:02:57 +000066 }
67
Sanjiv Gupta1b046942009-01-13 19:18:47 +000068
Sanjiv Gupta0e687712008-05-13 09:02:57 +000069 //===--------------------------------------------------------------------===//
70 // TargetLowering Implementation
71 //===--------------------------------------------------------------------===//
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000072 class PIC16TargetLowering : public TargetLowering {
Sanjiv Gupta0e687712008-05-13 09:02:57 +000073 public:
Sanjiv Gupta0e687712008-05-13 09:02:57 +000074 explicit PIC16TargetLowering(PIC16TargetMachine &TM);
75
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000076 /// getTargetNodeName - This method returns the name of a target specific
77 /// DAG node.
Sanjiv Gupta0e687712008-05-13 09:02:57 +000078 virtual const char *getTargetNodeName(unsigned Opcode) const;
Sanjiv Gupta1b046942009-01-13 19:18:47 +000079 /// getSetCCResultType - Return the ISD::SETCC ValueType
80 virtual MVT getSetCCResultType(MVT ValType) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +000081 SDValue LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +000082 SDValue LowerShift(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000083 SDValue LowerADD(SDValue Op, SelectionDAG &DAG);
84 SDValue LowerSUB(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta8f78fa82008-11-26 10:53:50 +000085 SDValue LowerBinOp(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000086 SDValue LowerCALL(SDValue Op, SelectionDAG &DAG);
87 SDValue LowerRET(SDValue Op, SelectionDAG &DAG);
88 SDValue LowerCallReturn(SDValue Op, SDValue Chain, SDValue FrameAddress,
89 SDValue InFlag, SelectionDAG &DAG);
90 SDValue LowerCallArguments(SDValue Op, SDValue Chain, SDValue FrameAddress,
91 SDValue InFlag, SelectionDAG &DAG);
92 SDValue LowerBR_CC(SDValue Op, SelectionDAG &DAG);
93 SDValue LowerSELECT_CC(SDValue Op, SelectionDAG &DAG);
94 SDValue getPIC16Cmp(SDValue LHS, SDValue RHS, unsigned OrigCC, SDValue &CC,
Dale Johannesen2fabcb22009-02-05 01:01:16 +000095 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +000096 virtual MachineBasicBlock *EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +000097 MachineBasicBlock *MBB) const;
Sanjiv Gupta0e687712008-05-13 09:02:57 +000098
Sanjiv Gupta1b046942009-01-13 19:18:47 +000099
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000100 virtual SDValue LowerOperation(SDValue Op, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000101 virtual void ReplaceNodeResults(SDNode *N,
102 SmallVectorImpl<SDValue> &Results,
Duncan Sands1607f052008-12-01 11:39:25 +0000103 SelectionDAG &DAG);
Duncan Sands9fbc7e22009-01-21 09:00:29 +0000104 virtual void LowerOperationWrapper(SDNode *N,
105 SmallVectorImpl<SDValue> &Results,
106 SelectionDAG &DAG);
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000107
Duncan Sands1607f052008-12-01 11:39:25 +0000108 SDValue ExpandStore(SDNode *N, SelectionDAG &DAG);
109 SDValue ExpandLoad(SDNode *N, SelectionDAG &DAG);
Duncan Sands1607f052008-12-01 11:39:25 +0000110 SDValue ExpandGlobalAddress(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000111 SDValue ExpandExternalSymbol(SDNode *N, SelectionDAG &DAG);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000112 SDValue ExpandFrameIndex(SDNode *N, SelectionDAG &DAG);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000113
114 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
115 SDValue PerformPIC16LoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000116 SDValue PerformStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000117
118 private:
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000119 // If the Node is a BUILD_PAIR representing a direct Address,
120 // then this function will return true.
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000121 bool isDirectAddress(const SDValue &Op);
122
123 // If the Node is a DirectAddress in ROM_SPACE then this
124 // function will return true
125 bool isRomAddress(const SDValue &Op);
126
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000127 // Extract the Lo and Hi component of Op.
128 void GetExpandedParts(SDValue Op, SelectionDAG &DAG, SDValue &Lo,
129 SDValue &Hi);
130
131
132 // Load pointer can be a direct or indirect address. In PIC16 direct
133 // addresses need Banksel and Indirect addresses need to be loaded to
134 // FSR first. Handle address specific cases here.
135 void LegalizeAddress(SDValue Ptr, SelectionDAG &DAG, SDValue &Chain,
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000136 SDValue &NewPtr, unsigned &Offset, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000137
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000138 // FrameIndex should be broken down into ExternalSymbol and FrameOffset.
139 void LegalizeFrameIndex(SDValue Op, SelectionDAG &DAG, SDValue &ES,
140 int &Offset);
141
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000142 // We can not have both operands of a binary operation in W.
143 // This function is used to put one operand on stack and generate a load.
Dale Johannesen2fabcb22009-02-05 01:01:16 +0000144 SDValue ConvertToMemOperand(SDValue Op, SelectionDAG &DAG, DebugLoc dl);
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000145
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000146 // This function checks if we need to put an operand of an operation on
147 // stack and generate a load or not.
148 bool NeedToConvertToMemOp(SDValue Op, unsigned &MemOp);
149
Sanjiv Guptab1b5ffd2008-11-19 11:00:54 +0000150 /// Subtarget - Keep a pointer to the PIC16Subtarget around so that we can
151 /// make the right decision when generating code for different targets.
152 const PIC16Subtarget *Subtarget;
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000153
154
155 // Extending the LIB Call framework of LLVM
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000156 // to hold the names of PIC16Libcalls.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000157 const char *PIC16LibcallNames[PIC16ISD::PIC16UnknownCall];
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000158
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000159 // To set and retrieve the lib call names.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000160 void setPIC16LibcallName(PIC16ISD::PIC16Libcall Call, const char *Name);
161 const char *getPIC16LibcallName(PIC16ISD::PIC16Libcall Call);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000162
Sanjiv Guptab84d5a42009-04-02 17:42:00 +0000163 // Make PIC16 Libcall.
Sanjiv Gupta08b9b052009-01-21 05:44:05 +0000164 SDValue MakePIC16Libcall(PIC16ISD::PIC16Libcall Call, MVT RetVT,
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000165 const SDValue *Ops, unsigned NumOps, bool isSigned,
Dale Johannesen7d2ad622009-01-30 23:10:59 +0000166 SelectionDAG &DAG, DebugLoc dl);
Sanjiv Gupta1b046942009-01-13 19:18:47 +0000167
168 // Check if operation has a direct load operand.
169 inline bool isDirectLoad(const SDValue Op);
170
Sanjiv Gupta0e687712008-05-13 09:02:57 +0000171 };
172} // namespace llvm
173
174#endif // PIC16ISELLOWERING_H