Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 1 | //===-- PIC16ISelDAGToDAG.cpp - A dag to dag inst selector for PIC16 ------===// |
| 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 an instruction selector for the PIC16 target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "pic16-isel" |
| 15 | |
| 16 | #include "PIC16.h" |
| 17 | #include "PIC16ISelLowering.h" |
| 18 | #include "PIC16RegisterInfo.h" |
| 19 | #include "PIC16Subtarget.h" |
| 20 | #include "PIC16TargetMachine.h" |
| 21 | #include "llvm/GlobalValue.h" |
| 22 | #include "llvm/Instructions.h" |
| 23 | #include "llvm/Intrinsics.h" |
| 24 | #include "llvm/Type.h" |
| 25 | #include "llvm/CodeGen/MachineConstantPool.h" |
| 26 | #include "llvm/CodeGen/MachineFunction.h" |
| 27 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 28 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
| 29 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 30 | #include "llvm/Support/CFG.h" |
| 31 | #include "llvm/Support/Compiler.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Target/TargetMachine.h" |
| 34 | #include <queue> |
| 35 | #include <set> |
| 36 | |
| 37 | using namespace llvm; |
| 38 | |
| 39 | //===----------------------------------------------------------------------===// |
| 40 | // Instruction Selector Implementation |
| 41 | //===----------------------------------------------------------------------===// |
| 42 | |
| 43 | //===----------------------------------------------------------------------===// |
| 44 | // PIC16DAGToDAGISel - PIC16 specific code to select PIC16 machine |
| 45 | // instructions for SelectionDAG operations. |
| 46 | //===----------------------------------------------------------------------===// |
| 47 | namespace { |
| 48 | |
| 49 | class VISIBILITY_HIDDEN PIC16DAGToDAGISel : public SelectionDAGISel { |
| 50 | |
| 51 | /// TM - Keep a reference to PIC16TargetMachine. |
| 52 | PIC16TargetMachine &TM; |
| 53 | |
| 54 | /// PIC16Lowering - This object fully describes how to lower LLVM code to an |
| 55 | /// PIC16-specific SelectionDAG. |
| 56 | PIC16TargetLowering PIC16Lowering; |
| 57 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 58 | public: |
Dan Gohman | 1002c02 | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 59 | explicit PIC16DAGToDAGISel(PIC16TargetMachine &tm) : |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 60 | SelectionDAGISel(PIC16Lowering), |
| 61 | TM(tm), PIC16Lowering(*TM.getTargetLowering()) {} |
| 62 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 63 | virtual void InstructionSelect(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 64 | |
| 65 | // Pass Name |
| 66 | virtual const char *getPassName() const { |
| 67 | return "PIC16 DAG->DAG Pattern Instruction Selection"; |
| 68 | } |
| 69 | |
| 70 | private: |
| 71 | // Include the pieces autogenerated from the target description. |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 72 | #include "PIC16GenDAGISel.inc" |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 73 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 74 | SDNode *Select(SDValue N); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 75 | |
| 76 | // Select addressing mode. currently assume base + offset addr mode. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 77 | bool SelectAM(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset); |
| 78 | bool SelectDirectAM(SDValue Op, SDValue N, SDValue &Base, |
| 79 | SDValue &Offset); |
| 80 | bool StoreInDirectAM(SDValue Op, SDValue N, SDValue &fsr); |
| 81 | bool LoadFSR(SDValue Op, SDValue N, SDValue &Base, SDValue &Offset); |
| 82 | bool LoadNothing(SDValue Op, SDValue N, SDValue &Base, |
| 83 | SDValue &Offset); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 84 | |
| 85 | // getI8Imm - Return a target constant with the specified |
| 86 | // value, of type i8. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 87 | inline SDValue getI8Imm(unsigned Imm) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 88 | return CurDAG->getTargetConstant(Imm, MVT::i8); |
| 89 | } |
| 90 | |
| 91 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 92 | #ifndef NDEBUG |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 93 | unsigned Indent; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 94 | #endif |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | } |
| 98 | |
Evan Cheng | db8d56b | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 99 | /// InstructionSelect - This callback is invoked by |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 100 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 101 | void PIC16DAGToDAGISel::InstructionSelect() |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 102 | { |
| 103 | DEBUG(BB->dump()); |
| 104 | // Codegen the basic block. |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 105 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 106 | DOUT << "===== Instruction selection begins:\n"; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 107 | #ifndef NDEBUG |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 108 | Indent = 0; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 109 | #endif |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 110 | |
| 111 | // Select target instructions for the DAG. |
Dan Gohman | ad3460c | 2008-08-21 16:36:34 +0000 | [diff] [blame] | 112 | SelectRoot(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 113 | |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 114 | DOUT << "===== Instruction selection ends:\n"; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 115 | |
Dan Gohman | f350b27 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 116 | CurDAG->RemoveDeadNodes(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | |
| 120 | bool PIC16DAGToDAGISel:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 121 | SelectDirectAM (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset) |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 122 | { |
| 123 | GlobalAddressSDNode *GA; |
| 124 | ConstantSDNode *GC; |
| 125 | |
| 126 | // if Address is FI, get the TargetFrameIndex. |
| 127 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(N)) { |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 128 | DOUT << "--------- its frame Index\n"; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 129 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 130 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 131 | return true; |
| 132 | } |
| 133 | |
| 134 | if (N.getOpcode() == ISD::GlobalAddress) { |
| 135 | GA = dyn_cast<GlobalAddressSDNode>(N); |
| 136 | Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8); |
| 137 | Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16, |
| 138 | GA->getOffset()); |
| 139 | return true; |
| 140 | } |
| 141 | |
| 142 | if (N.getOpcode() == ISD::ADD) { |
| 143 | GC = dyn_cast<ConstantSDNode>(N.getOperand(1)); |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame^] | 144 | Offset = CurDAG->getTargetConstant((unsigned char)GC->getZExtValue(), |
| 145 | MVT::i8); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 146 | if ((GA = dyn_cast<GlobalAddressSDNode>(N.getOperand(0)))) { |
| 147 | Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16, |
Dan Gohman | f5aeb1a | 2008-09-12 16:56:44 +0000 | [diff] [blame^] | 148 | GC->getZExtValue()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 149 | return true; |
| 150 | } |
| 151 | else if (FrameIndexSDNode *FIN |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 152 | = dyn_cast<FrameIndexSDNode>(N.getOperand(0))) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 153 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 154 | return true; |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | return false; |
| 159 | } |
| 160 | |
| 161 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 162 | // FIXME: must also account for preinc/predec/postinc/postdec. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 163 | bool PIC16DAGToDAGISel:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 164 | StoreInDirectAM (SDValue Op, SDValue N, SDValue &fsr) |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 165 | { |
| 166 | RegisterSDNode *Reg; |
| 167 | if (N.getOpcode() == ISD::LOAD) { |
| 168 | LoadSDNode *LD = dyn_cast<LoadSDNode>(N); |
| 169 | if (LD) { |
| 170 | fsr = LD->getBasePtr(); |
| 171 | } |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 172 | else if (isa<RegisterSDNode>(N.getNode())) { |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 173 | //FIXME an attempt to retrieve the register number |
| 174 | //but does not work |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 175 | DOUT << "this is a register\n"; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 176 | Reg = dyn_cast<RegisterSDNode>(N.getNode()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 177 | fsr = CurDAG->getRegister(Reg->getReg(),MVT::i16); |
| 178 | } |
| 179 | else { |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 180 | DOUT << "this is not a register\n"; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 181 | // FIXME must use whatever load is using |
| 182 | fsr = CurDAG->getRegister(1,MVT::i16); |
| 183 | } |
| 184 | return true; |
| 185 | } |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | bool PIC16DAGToDAGISel:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 190 | LoadFSR (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset) |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 191 | { |
| 192 | GlobalAddressSDNode *GA; |
| 193 | |
| 194 | if (N.getOpcode() == ISD::GlobalAddress) { |
| 195 | GA = dyn_cast<GlobalAddressSDNode>(N); |
| 196 | Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8); |
| 197 | Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16, |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 198 | GA->getOffset()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 199 | return true; |
| 200 | } |
| 201 | else if (N.getOpcode() == PIC16ISD::Package) { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 202 | CurDAG->setGraphColor(Op.getNode(), "blue"); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 203 | CurDAG->viewGraph(); |
| 204 | } |
| 205 | |
| 206 | return false; |
| 207 | } |
| 208 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 209 | // LoadNothing - Don't thake this seriously, it will change. |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 210 | bool PIC16DAGToDAGISel:: |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 211 | LoadNothing (SDValue Op, SDValue N, SDValue &Base, SDValue &Offset) |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 212 | { |
| 213 | GlobalAddressSDNode *GA; |
| 214 | if (N.getOpcode() == ISD::GlobalAddress) { |
| 215 | GA = dyn_cast<GlobalAddressSDNode>(N); |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 216 | DOUT << "==========" << GA->getOffset() << "\n"; |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 217 | Offset = CurDAG->getTargetConstant((unsigned char)GA->getOffset(), MVT::i8); |
| 218 | Base = CurDAG->getTargetGlobalAddress(GA->getGlobal(), MVT::i16, |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 219 | GA->getOffset()); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 220 | return true; |
| 221 | } |
| 222 | |
| 223 | return false; |
| 224 | } |
| 225 | |
| 226 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 227 | /// Select - Select instructions not customized! Used for |
| 228 | /// expanded, promoted and normal instructions. |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 229 | SDNode* PIC16DAGToDAGISel::Select(SDValue N) |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 230 | { |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 231 | SDNode *Node = N.getNode(); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 232 | unsigned Opcode = Node->getOpcode(); |
| 233 | |
| 234 | // Dump information about the Node being selected |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 235 | #ifndef NDEBUG |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 236 | DOUT << std::string(Indent, ' ') << "Selecting: "; |
| 237 | DEBUG(Node->dump(CurDAG)); |
| 238 | DOUT << "\n"; |
| 239 | Indent += 2; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 240 | #endif |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 241 | |
| 242 | // If we have a custom node, we already have selected! |
Dan Gohman | e8be6c6 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 243 | if (Node->isMachineOpcode()) { |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 244 | #ifndef NDEBUG |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 245 | DOUT << std::string(Indent-2, ' ') << "== "; |
| 246 | DEBUG(Node->dump(CurDAG)); |
| 247 | DOUT << "\n"; |
| 248 | Indent -= 2; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 249 | #endif |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 250 | return NULL; |
| 251 | } |
| 252 | |
| 253 | /// |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 254 | // FIXME: Instruction Selection not handled by custom or by the |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 255 | // auto-generated tablegen selection should be handled here. |
| 256 | /// |
| 257 | switch(Opcode) { |
| 258 | default: break; |
| 259 | } |
| 260 | |
| 261 | // Select the default instruction. |
| 262 | SDNode *ResNode = SelectCode(N); |
| 263 | |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 264 | #ifndef NDEBUG |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 265 | DOUT << std::string(Indent-2, ' ') << "=> "; |
Gabor Greif | ba36cb5 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 266 | if (ResNode == NULL || ResNode == N.getNode()) |
| 267 | DEBUG(N.getNode()->dump(CurDAG)); |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 268 | else |
| 269 | DEBUG(ResNode->dump(CurDAG)); |
| 270 | DOUT << "\n"; |
| 271 | Indent -= 2; |
Sanjiv Gupta | 2010b3e | 2008-05-14 11:31:39 +0000 | [diff] [blame] | 272 | #endif |
Sanjiv Gupta | 0e68771 | 2008-05-13 09:02:57 +0000 | [diff] [blame] | 273 | |
| 274 | return ResNode; |
| 275 | } |
| 276 | |
| 277 | /// createPIC16ISelDag - This pass converts a legalized DAG into a |
| 278 | /// PIC16-specific DAG, ready for instruction scheduling. |
| 279 | FunctionPass *llvm::createPIC16ISelDag(PIC16TargetMachine &TM) { |
| 280 | return new PIC16DAGToDAGISel(TM); |
| 281 | } |
| 282 | |