Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 1 | //===-- PTXISelDAGToDAG.cpp - A dag to dag inst selector for PTX ----------===// |
| 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 PTX target. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "PTX.h" |
| 15 | #include "PTXTargetMachine.h" |
| 16 | #include "llvm/CodeGen/SelectionDAGISel.h" |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 17 | #include "llvm/DerivedTypes.h" |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | // PTXDAGToDAGISel - PTX specific code to select PTX machine |
| 24 | // instructions for SelectionDAG operations. |
| 25 | class PTXDAGToDAGISel : public SelectionDAGISel { |
| 26 | public: |
| 27 | PTXDAGToDAGISel(PTXTargetMachine &TM, CodeGenOpt::Level OptLevel); |
| 28 | |
| 29 | virtual const char *getPassName() const { |
| 30 | return "PTX DAG->DAG Pattern Instruction Selection"; |
| 31 | } |
| 32 | |
| 33 | SDNode *Select(SDNode *Node); |
| 34 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 35 | // Complex Pattern Selectors. |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 36 | bool SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 37 | bool SelectADDRri(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 38 | bool SelectADDRii(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 39 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 40 | // Include the pieces auto'gened from the target description |
| 41 | #include "PTXGenDAGISel.inc" |
| 42 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 43 | private: |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 44 | SDNode *SelectREAD_PARAM(SDNode *Node); |
| 45 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 46 | // We need this only because we can't match intruction BRAdp |
| 47 | // pattern (PTXbrcond bb:$d, ...) in PTXInstrInfo.td |
| 48 | SDNode *SelectBRCOND(SDNode *Node); |
| 49 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 50 | bool isImm(const SDValue &operand); |
| 51 | bool SelectImm(const SDValue &operand, SDValue &imm); |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 52 | |
| 53 | const PTXSubtarget& getSubtarget() const; |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 54 | }; // class PTXDAGToDAGISel |
| 55 | } // namespace |
| 56 | |
| 57 | // createPTXISelDag - This pass converts a legalized DAG into a |
| 58 | // PTX-specific DAG, ready for instruction scheduling |
| 59 | FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM, |
| 60 | CodeGenOpt::Level OptLevel) { |
| 61 | return new PTXDAGToDAGISel(TM, OptLevel); |
| 62 | } |
| 63 | |
| 64 | PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM, |
| 65 | CodeGenOpt::Level OptLevel) |
| 66 | : SelectionDAGISel(TM, OptLevel) {} |
| 67 | |
| 68 | SDNode *PTXDAGToDAGISel::Select(SDNode *Node) { |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 69 | switch (Node->getOpcode()) { |
| 70 | case PTXISD::READ_PARAM: |
| 71 | return SelectREAD_PARAM(Node); |
| 72 | case ISD::BRCOND: |
| 73 | return SelectBRCOND(Node); |
| 74 | default: |
| 75 | return SelectCode(Node); |
| 76 | } |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | SDNode *PTXDAGToDAGISel::SelectREAD_PARAM(SDNode *Node) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 80 | SDValue index = Node->getOperand(1); |
| 81 | DebugLoc dl = Node->getDebugLoc(); |
| 82 | unsigned opcode; |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 83 | |
| 84 | if (index.getOpcode() != ISD::TargetConstant) |
| 85 | llvm_unreachable("READ_PARAM: index is not ISD::TargetConstant"); |
| 86 | |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 87 | if (Node->getValueType(0) == MVT::i16) { |
| 88 | opcode = PTX::LDpiU16; |
| 89 | } |
| 90 | else if (Node->getValueType(0) == MVT::i32) { |
| 91 | opcode = PTX::LDpiU32; |
| 92 | } |
| 93 | else if (Node->getValueType(0) == MVT::i64) { |
| 94 | opcode = PTX::LDpiU64; |
| 95 | } |
| 96 | else if (Node->getValueType(0) == MVT::f32) { |
| 97 | opcode = PTX::LDpiF32; |
| 98 | } |
| 99 | else if (Node->getValueType(0) == MVT::f64) { |
| 100 | opcode = PTX::LDpiF64; |
| 101 | } |
| 102 | else { |
| 103 | llvm_unreachable("Unknown parameter type for ld.param"); |
| 104 | } |
| 105 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 106 | return PTXInstrInfo:: |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 107 | GetPTXMachineNode(CurDAG, opcode, dl, Node->getValueType(0), index); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 108 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 109 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 110 | SDNode *PTXDAGToDAGISel::SelectBRCOND(SDNode *Node) { |
| 111 | assert(Node->getNumOperands() >= 3); |
| 112 | |
| 113 | SDValue Chain = Node->getOperand(0); |
| 114 | SDValue Pred = Node->getOperand(1); |
| 115 | SDValue Target = Node->getOperand(2); // branch target |
| 116 | SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32); |
| 117 | DebugLoc dl = Node->getDebugLoc(); |
| 118 | |
| 119 | assert(Target.getOpcode() == ISD::BasicBlock); |
| 120 | assert(Pred.getValueType() == MVT::i1); |
| 121 | |
| 122 | // Emit BRAdp |
| 123 | SDValue Ops[] = { Target, Pred, PredOp, Chain }; |
| 124 | return CurDAG->getMachineNode(PTX::BRAdp, dl, MVT::Other, Ops, 4); |
| 125 | } |
| 126 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 127 | // Match memory operand of the form [reg+reg] |
| 128 | bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) { |
| 129 | if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 || |
| 130 | isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1))) |
| 131 | return false; |
| 132 | |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame^] | 133 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 134 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 135 | R1 = Addr; |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame^] | 136 | R2 = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 137 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 138 | return true; |
| 139 | } |
| 140 | |
| 141 | // Match memory operand of the form [reg], [imm+reg], and [reg+imm] |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 142 | bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base, |
| 143 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 144 | if (Addr.getOpcode() != ISD::ADD) { |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 145 | // let SelectADDRii handle the [imm] case |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 146 | if (isImm(Addr)) |
| 147 | return false; |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 148 | // it is [reg] |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame^] | 149 | |
| 150 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 151 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 152 | Base = Addr; |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame^] | 153 | Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 154 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 155 | return true; |
| 156 | } |
| 157 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 158 | if (Addr.getNumOperands() < 2) |
| 159 | return false; |
| 160 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 161 | // let SelectADDRii handle the [imm+imm] case |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 162 | if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1))) |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 163 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 164 | |
| 165 | // try [reg+imm] and [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 166 | for (int i = 0; i < 2; i ++) |
| 167 | if (SelectImm(Addr.getOperand(1-i), Offset)) { |
| 168 | Base = Addr.getOperand(i); |
| 169 | return true; |
| 170 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 171 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 172 | // neither [reg+imm] nor [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 173 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | // Match memory operand of the form [imm+imm] and [imm] |
| 177 | bool PTXDAGToDAGISel::SelectADDRii(SDValue &Addr, SDValue &Base, |
| 178 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 179 | // is [imm+imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 180 | if (Addr.getOpcode() == ISD::ADD) { |
| 181 | return SelectImm(Addr.getOperand(0), Base) && |
| 182 | SelectImm(Addr.getOperand(1), Offset); |
| 183 | } |
| 184 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 185 | // is [imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 186 | if (SelectImm(Addr, Base)) { |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame^] | 187 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 188 | |
| 189 | Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 190 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 191 | return true; |
| 192 | } |
| 193 | |
| 194 | return false; |
| 195 | } |
| 196 | |
| 197 | bool PTXDAGToDAGISel::isImm(const SDValue &operand) { |
| 198 | return ConstantSDNode::classof(operand.getNode()); |
| 199 | } |
| 200 | |
| 201 | bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) { |
| 202 | SDNode *node = operand.getNode(); |
| 203 | if (!ConstantSDNode::classof(node)) |
| 204 | return false; |
| 205 | |
| 206 | ConstantSDNode *CN = cast<ConstantSDNode>(node); |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame^] | 207 | imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(), |
| 208 | operand.getValueType()); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 209 | return true; |
| 210 | } |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 211 | |
| 212 | const PTXSubtarget& PTXDAGToDAGISel::getSubtarget() const |
| 213 | { |
| 214 | return TM.getSubtarget<PTXSubtarget>(); |
| 215 | } |
| 216 | |