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 { |
| 32 | SparcTargetLowering Lowering; |
| 33 | |
| 34 | /// Subtarget - Keep a pointer to the Sparc Subtarget around so that we can |
| 35 | /// make the right decision when generating code for different targets. |
| 36 | const SparcSubtarget &Subtarget; |
| 37 | public: |
Dan Gohman | e887fdf | 2008-07-07 18:00:37 +0000 | [diff] [blame] | 38 | explicit SparcDAGToDAGISel(TargetMachine &TM) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 39 | : SelectionDAGISel(Lowering), Lowering(TM), |
| 40 | Subtarget(TM.getSubtarget<SparcSubtarget>()) { |
| 41 | } |
| 42 | |
| 43 | SDNode *Select(SDOperand Op); |
| 44 | |
| 45 | // Complex Pattern Selectors. |
| 46 | bool SelectADDRrr(SDOperand Op, SDOperand N, SDOperand &R1, SDOperand &R2); |
| 47 | bool SelectADDRri(SDOperand Op, SDOperand N, SDOperand &Base, |
| 48 | SDOperand &Offset); |
| 49 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 50 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 51 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 52 | virtual void InstructionSelect(SelectionDAG &DAG); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 53 | |
| 54 | virtual const char *getPassName() const { |
| 55 | return "SPARC DAG->DAG Pattern Instruction Selection"; |
| 56 | } |
| 57 | |
| 58 | // Include the pieces autogenerated from the target description. |
| 59 | #include "SparcGenDAGISel.inc" |
| 60 | }; |
| 61 | } // end anonymous namespace |
| 62 | |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 63 | /// InstructionSelect - This callback is invoked by |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 64 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
Evan Cheng | 34fd4f3 | 2008-06-30 20:45:06 +0000 | [diff] [blame] | 65 | void SparcDAGToDAGISel::InstructionSelect(SelectionDAG &DAG) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 66 | DEBUG(BB->dump()); |
| 67 | |
| 68 | // Select target instructions for the DAG. |
| 69 | DAG.setRoot(SelectRoot(DAG.getRoot())); |
| 70 | DAG.RemoveDeadNodes(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | bool SparcDAGToDAGISel::SelectADDRri(SDOperand Op, SDOperand Addr, |
| 74 | SDOperand &Base, SDOperand &Offset) { |
| 75 | if (FrameIndexSDNode *FIN = dyn_cast<FrameIndexSDNode>(Addr)) { |
| 76 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 77 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 78 | return true; |
| 79 | } |
| 80 | if (Addr.getOpcode() == ISD::TargetExternalSymbol || |
| 81 | Addr.getOpcode() == ISD::TargetGlobalAddress) |
| 82 | return false; // direct calls. |
| 83 | |
| 84 | if (Addr.getOpcode() == ISD::ADD) { |
| 85 | if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) { |
| 86 | if (Predicate_simm13(CN)) { |
| 87 | if (FrameIndexSDNode *FIN = |
| 88 | dyn_cast<FrameIndexSDNode>(Addr.getOperand(0))) { |
| 89 | // Constant offset from frame ref. |
| 90 | Base = CurDAG->getTargetFrameIndex(FIN->getIndex(), MVT::i32); |
| 91 | } else { |
| 92 | Base = Addr.getOperand(0); |
| 93 | } |
| 94 | Offset = CurDAG->getTargetConstant(CN->getValue(), MVT::i32); |
| 95 | return true; |
| 96 | } |
| 97 | } |
| 98 | if (Addr.getOperand(0).getOpcode() == SPISD::Lo) { |
| 99 | Base = Addr.getOperand(1); |
| 100 | Offset = Addr.getOperand(0).getOperand(0); |
| 101 | return true; |
| 102 | } |
| 103 | if (Addr.getOperand(1).getOpcode() == SPISD::Lo) { |
| 104 | Base = Addr.getOperand(0); |
| 105 | Offset = Addr.getOperand(1).getOperand(0); |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | Base = Addr; |
| 110 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 111 | return true; |
| 112 | } |
| 113 | |
| 114 | bool SparcDAGToDAGISel::SelectADDRrr(SDOperand Op, SDOperand Addr, |
| 115 | SDOperand &R1, SDOperand &R2) { |
| 116 | if (Addr.getOpcode() == ISD::FrameIndex) return false; |
| 117 | if (Addr.getOpcode() == ISD::TargetExternalSymbol || |
| 118 | Addr.getOpcode() == ISD::TargetGlobalAddress) |
| 119 | return false; // direct calls. |
| 120 | |
| 121 | if (Addr.getOpcode() == ISD::ADD) { |
| 122 | if (isa<ConstantSDNode>(Addr.getOperand(1)) && |
| 123 | Predicate_simm13(Addr.getOperand(1).Val)) |
| 124 | return false; // Let the reg+imm pattern catch this! |
| 125 | if (Addr.getOperand(0).getOpcode() == SPISD::Lo || |
| 126 | Addr.getOperand(1).getOpcode() == SPISD::Lo) |
| 127 | return false; // Let the reg+imm pattern catch this! |
| 128 | R1 = Addr.getOperand(0); |
| 129 | R2 = Addr.getOperand(1); |
| 130 | return true; |
| 131 | } |
| 132 | |
| 133 | R1 = Addr; |
| 134 | R2 = CurDAG->getRegister(SP::G0, MVT::i32); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | SDNode *SparcDAGToDAGISel::Select(SDOperand Op) { |
| 139 | SDNode *N = Op.Val; |
| 140 | if (N->getOpcode() >= ISD::BUILTIN_OP_END && |
| 141 | N->getOpcode() < SPISD::FIRST_NUMBER) |
| 142 | return NULL; // Already selected. |
| 143 | |
| 144 | switch (N->getOpcode()) { |
| 145 | default: break; |
| 146 | case ISD::SDIV: |
| 147 | case ISD::UDIV: { |
| 148 | // FIXME: should use a custom expander to expose the SRA to the dag. |
| 149 | SDOperand DivLHS = N->getOperand(0); |
| 150 | SDOperand DivRHS = N->getOperand(1); |
| 151 | AddToISelQueue(DivLHS); |
| 152 | AddToISelQueue(DivRHS); |
| 153 | |
| 154 | // Set the Y register to the high-part. |
| 155 | SDOperand TopPart; |
| 156 | if (N->getOpcode() == ISD::SDIV) { |
| 157 | TopPart = SDOperand(CurDAG->getTargetNode(SP::SRAri, MVT::i32, DivLHS, |
| 158 | CurDAG->getTargetConstant(31, MVT::i32)), 0); |
| 159 | } else { |
| 160 | TopPart = CurDAG->getRegister(SP::G0, MVT::i32); |
| 161 | } |
| 162 | TopPart = SDOperand(CurDAG->getTargetNode(SP::WRYrr, MVT::Flag, TopPart, |
| 163 | CurDAG->getRegister(SP::G0, MVT::i32)), 0); |
| 164 | |
| 165 | // FIXME: Handle div by immediate. |
| 166 | unsigned Opcode = N->getOpcode() == ISD::SDIV ? SP::SDIVrr : SP::UDIVrr; |
| 167 | return CurDAG->SelectNodeTo(N, Opcode, MVT::i32, DivLHS, DivRHS, |
| 168 | TopPart); |
| 169 | } |
| 170 | case ISD::MULHU: |
| 171 | case ISD::MULHS: { |
| 172 | // FIXME: Handle mul by immediate. |
| 173 | SDOperand MulLHS = N->getOperand(0); |
| 174 | SDOperand MulRHS = N->getOperand(1); |
| 175 | AddToISelQueue(MulLHS); |
| 176 | AddToISelQueue(MulRHS); |
| 177 | unsigned Opcode = N->getOpcode() == ISD::MULHU ? SP::UMULrr : SP::SMULrr; |
| 178 | SDNode *Mul = CurDAG->getTargetNode(Opcode, MVT::i32, MVT::Flag, |
| 179 | MulLHS, MulRHS); |
| 180 | // The high part is in the Y register. |
| 181 | return CurDAG->SelectNodeTo(N, SP::RDY, MVT::i32, SDOperand(Mul, 1)); |
| 182 | return NULL; |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | return SelectCode(Op); |
| 187 | } |
| 188 | |
| 189 | |
| 190 | /// createSparcISelDag - This pass converts a legalized DAG into a |
| 191 | /// SPARC-specific DAG, ready for instruction scheduling. |
| 192 | /// |
| 193 | FunctionPass *llvm::createSparcISelDag(TargetMachine &TM) { |
| 194 | return new SparcDAGToDAGISel(TM); |
| 195 | } |