Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- SparcISelDAGToDAG.cpp - A dag to dag inst selector for Sparc ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines an instruction selector for the SPARC target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | 36d2344 | 2008-03-17 03:21:36 +0000 | [diff] [blame] | 14 | #include "SparcISelLowering.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 15 | #include "SparcTargetMachine.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 16 | #include "llvm/Intrinsics.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 17 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Chris Lattner | 93c741a | 2008-02-03 05:43:57 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Compiler.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | //===----------------------------------------------------------------------===// |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | // Instruction Selector Implementation |
| 24 | //===----------------------------------------------------------------------===// |
| 25 | |
| 26 | //===--------------------------------------------------------------------===// |
| 27 | /// SparcDAGToDAGISel - SPARC specific code to select SPARC machine |
| 28 | /// instructions for SelectionDAG operations. |
| 29 | /// |
| 30 | namespace { |
| 31 | class SparcDAGToDAGISel : public SelectionDAGISel { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 32 | /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can |
| 33 | /// make the right decision when generating code for different targets. |
| 34 | const SparcSubtarget &Subtarget; |
| 35 | public: |
Dan Gohman | f2b2957 | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 36 | explicit SparcDAGToDAGISel(SparcTargetMachine &TM) |
| 37 | : SelectionDAGISel(*TM.getTargetLowering()), |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 38 | Subtarget(TM.getSubtarget<SparcSubtarget>()) { |
| 39 | } |
| 40 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 41 | SDNode *Select(SDValue Op); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 42 | |
| 43 | // Complex Pattern Selectors. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 44 | bool SelectADDRrr(SDValue Op, SDValue N, SDValue &R1, SDValue &R2); |
| 45 | bool SelectADDRri(SDValue Op, SDValue N, SDValue &Base, |
| 46 | SDValue &Offset); |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 47 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 48 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 50 | virtual void InstructionSelect(); |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 51 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 52 | virtual const char *getPassName() const { |
| 53 | return "SPARC DAG->DAG Pattern Instruction Selection"; |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 54 | } |
| 55 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 56 | // Include the pieces autogenerated from the target description. |
| 57 | #include "SparcGenDAGISel.inc" |
| 58 | }; |
| 59 | } // end anonymous namespace |
| 60 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 61 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 62 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 63 | void SparcDAGToDAGISel::InstructionSelect() { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | DEBUG(BB->dump()); |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 65 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | // Select target instructions for the DAG. |
Dan Gohman | bd3f882 | 2008-08-21 16:36:34 +0000 | [diff] [blame] | 67 | SelectRoot(); |
Dan Gohman | 14a6644 | 2008-08-23 02:25:05 +0000 | [diff] [blame] | 68 | CurDAG->RemoveDeadNodes(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 71 | bool SparcDAGToDAGISel::SelectADDRri(SDValue Op, SDValue Addr, |
| 72 | SDValue &Base, SDValue &Offset) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 73 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
| 74 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 75 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 76 | return true; |
| 77 | } |
Bill Wendling | fef0605 | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 78 | if (Addr.getOpcode() == ISD::TargetExternalSymbol || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 79 | Addr.getOpcode() == ISD::TargetGlobalAddress) |
| 80 | return false; // direct calls. |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 81 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 82 | if (Addr.getOpcode() == ISD::ADD) { |
| 83 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) { |
| 84 | if (Predicate_simm13(CN)) { |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 85 | if (FrameIndexSDNode *FIN = |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 86 | dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) { |
| 87 | // Constant offset from frame ref. |
| 88 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 89 | } else { |
| 90 | Base = Addr.getOperand(0); |
| 91 | } |
Dan Gohman | faeb4a3 | 2008-09-12 16:56:44 +0000 | [diff] [blame] | 92 | Offset = CurDAG->getTargetConstant(CN->getZExtValue(), MVT::i32); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | } |
| 96 | if (Addr.getOperand(0).getOpcode() == SPISD::Lo) { |
| 97 | Base = Addr.getOperand(1); |
| 98 | Offset = Addr.getOperand(0).getOperand(0); |
| 99 | return true; |
| 100 | } |
| 101 | if (Addr.getOperand(1).getOpcode() == SPISD::Lo) { |
| 102 | Base = Addr.getOperand(0); |
| 103 | Offset = Addr.getOperand(1).getOperand(0); |
| 104 | return true; |
| 105 | } |
| 106 | } |
| 107 | Base = Addr; |
| 108 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 109 | return true; |
| 110 | } |
| 111 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 112 | bool SparcDAGToDAGISel::SelectADDRrr(SDValue Op, SDValue Addr, |
| 113 | SDValue &R1, SDValue &R2) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 114 | if (Addr.getOpcode() == ISD::FrameIndex) return false; |
Bill Wendling | fef0605 | 2008-09-16 21:48:12 +0000 | [diff] [blame] | 115 | if (Addr.getOpcode() == ISD::TargetExternalSymbol || |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 116 | Addr.getOpcode() == ISD::TargetGlobalAddress) |
| 117 | return false; // direct calls. |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 118 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 119 | if (Addr.getOpcode() == ISD::ADD) { |
| 120 | if (isa<ConstantSDNode>(Addr.getOperand(1)) && |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 121 | Predicate_simm13(Addr.getOperand(1).getNode())) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 122 | return false; // Let the reg+imm pattern catch this! |
| 123 | if (Addr.getOperand(0).getOpcode() == SPISD::Lo || |
| 124 | Addr.getOperand(1).getOpcode() == SPISD::Lo) |
| 125 | return false; // Let the reg+imm pattern catch this! |
| 126 | R1 = Addr.getOperand(0); |
| 127 | R2 = Addr.getOperand(1); |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | R1 = Addr; |
| 132 | R2 = CurDAG->getRegister(SP::G0, MVT::i32); |
| 133 | return true; |
| 134 | } |
| 135 | |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 136 | SDNode *SparcDAGToDAGISel::Select(SDValue Op) { |
Gabor Greif | 1c80d11 | 2008-08-28 21:40:38 +0000 | [diff] [blame] | 137 | SDNode *N = Op.getNode(); |
Dan Gohman | bd68c79 | 2008-07-17 19:10:17 +0000 | [diff] [blame] | 138 | if (N->isMachineOpcode()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 139 | return NULL; // Already selected. |
| 140 | |
| 141 | switch (N->getOpcode()) { |
| 142 | default: break; |
| 143 | case ISD::SDIV: |
| 144 | case ISD::UDIV: { |
| 145 | // FIXME: should use a custom expander to expose the SRA to the dag. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 146 | SDValue DivLHS = N->getOperand(0); |
| 147 | SDValue DivRHS = N->getOperand(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 148 | AddToISelQueue(DivLHS); |
| 149 | AddToISelQueue(DivRHS); |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 150 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 151 | // Set the Y register to the high-part. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 152 | SDValue TopPart; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 153 | if (N->getOpcode() == ISD::SDIV) { |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 154 | TopPart = SDValue(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 155 | CurDAG->getTargetConstant(31, MVT::i32)), 0); |
| 156 | } else { |
| 157 | TopPart = CurDAG->getRegister(SP::G0, MVT::i32); |
| 158 | } |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 159 | TopPart = SDValue(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart, |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 160 | CurDAG->getRegister(SP::G0, MVT::i32)), 0); |
| 161 | |
| 162 | // FIXME: Handle div by immediate. |
| 163 | unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr; |
| 164 | return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, |
| 165 | TopPart); |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 166 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 167 | case ISD::MULHU: |
| 168 | case ISD::MULHS: { |
| 169 | // FIXME: Handle mul by immediate. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 170 | SDValue MulLHS = N->getOperand(0); |
| 171 | SDValue MulRHS = N->getOperand(1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 172 | AddToISelQueue(MulLHS); |
| 173 | AddToISelQueue(MulRHS); |
| 174 | unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr; |
| 175 | SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag, |
| 176 | MulLHS, MulRHS); |
| 177 | // The high part is in the Y register. |
Dan Gohman | 8181bd1 | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 178 | return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDValue(Mul, 1)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 179 | return NULL; |
| 180 | } |
| 181 | } |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 182 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 183 | return SelectCode(Op); |
| 184 | } |
| 185 | |
| 186 | |
Anton Korobeynikov | 05b8955 | 2008-10-10 10:14:15 +0000 | [diff] [blame^] | 187 | /// createSparcISelDag - This pass converts a legalized DAG into a |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | /// SPARC-specific DAG, ready for instruction scheduling. |
| 189 | /// |
Dan Gohman | f2b2957 | 2008-10-03 16:55:19 +0000 | [diff] [blame] | 190 | FunctionPass *llvm::createSparcISelDag(SparcTargetMachine &TM) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 191 | return new SparcDAGToDAGISel(TM); |
| 192 | } |