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 | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 46 | bool isImm(const SDValue &operand); |
| 47 | bool SelectImm(const SDValue &operand, SDValue &imm); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 48 | }; // class PTXDAGToDAGISel |
| 49 | } // namespace |
| 50 | |
| 51 | // createPTXISelDag - This pass converts a legalized DAG into a |
| 52 | // PTX-specific DAG, ready for instruction scheduling |
| 53 | FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM, |
| 54 | CodeGenOpt::Level OptLevel) { |
| 55 | return new PTXDAGToDAGISel(TM, OptLevel); |
| 56 | } |
| 57 | |
| 58 | PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM, |
| 59 | CodeGenOpt::Level OptLevel) |
| 60 | : SelectionDAGISel(TM, OptLevel) {} |
| 61 | |
| 62 | SDNode *PTXDAGToDAGISel::Select(SDNode *Node) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 63 | if (Node->getOpcode() == PTXISD::READ_PARAM) |
| 64 | return SelectREAD_PARAM(Node); |
| 65 | else |
| 66 | return SelectCode(Node); |
| 67 | } |
| 68 | |
| 69 | SDNode *PTXDAGToDAGISel::SelectREAD_PARAM(SDNode *Node) { |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame^] | 70 | SDValue index = Node->getOperand(1); |
| 71 | DebugLoc dl = Node->getDebugLoc(); |
| 72 | unsigned opcode; |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 73 | |
| 74 | if (index.getOpcode() != ISD::TargetConstant) |
| 75 | llvm_unreachable("READ_PARAM: index is not ISD::TargetConstant"); |
| 76 | |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame^] | 77 | if (Node->getValueType(0) == MVT::i16) { |
| 78 | opcode = PTX::LDpiU16; |
| 79 | } |
| 80 | else if (Node->getValueType(0) == MVT::i32) { |
| 81 | opcode = PTX::LDpiU32; |
| 82 | } |
| 83 | else if (Node->getValueType(0) == MVT::i64) { |
| 84 | opcode = PTX::LDpiU64; |
| 85 | } |
| 86 | else if (Node->getValueType(0) == MVT::f32) { |
| 87 | opcode = PTX::LDpiF32; |
| 88 | } |
| 89 | else if (Node->getValueType(0) == MVT::f64) { |
| 90 | opcode = PTX::LDpiF64; |
| 91 | } |
| 92 | else { |
| 93 | llvm_unreachable("Unknown parameter type for ld.param"); |
| 94 | } |
| 95 | |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 96 | return PTXInstrInfo:: |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame^] | 97 | GetPTXMachineNode(CurDAG, opcode, dl, Node->getValueType(0), index); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 98 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 99 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 100 | // Match memory operand of the form [reg+reg] |
| 101 | bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) { |
| 102 | if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 || |
| 103 | isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1))) |
| 104 | return false; |
| 105 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 106 | R1 = Addr; |
| 107 | R2 = CurDAG->getTargetConstant(0, MVT::i32); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 108 | return true; |
| 109 | } |
| 110 | |
| 111 | // 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] | 112 | bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base, |
| 113 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 114 | if (Addr.getOpcode() != ISD::ADD) { |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 115 | // let SelectADDRii handle the [imm] case |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 116 | if (isImm(Addr)) |
| 117 | return false; |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 118 | // it is [reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 119 | Base = Addr; |
| 120 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 121 | return true; |
| 122 | } |
| 123 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 124 | if (Addr.getNumOperands() < 2) |
| 125 | return false; |
| 126 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 127 | // let SelectADDRii handle the [imm+imm] case |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 128 | if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1))) |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 129 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 130 | |
| 131 | // try [reg+imm] and [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 132 | for (int i = 0; i < 2; i ++) |
| 133 | if (SelectImm(Addr.getOperand(1-i), Offset)) { |
| 134 | Base = Addr.getOperand(i); |
| 135 | return true; |
| 136 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 137 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 138 | // neither [reg+imm] nor [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 139 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | // Match memory operand of the form [imm+imm] and [imm] |
| 143 | bool PTXDAGToDAGISel::SelectADDRii(SDValue &Addr, SDValue &Base, |
| 144 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 145 | // is [imm+imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 146 | if (Addr.getOpcode() == ISD::ADD) { |
| 147 | return SelectImm(Addr.getOperand(0), Base) && |
| 148 | SelectImm(Addr.getOperand(1), Offset); |
| 149 | } |
| 150 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 151 | // is [imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 152 | if (SelectImm(Addr, Base)) { |
| 153 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | bool PTXDAGToDAGISel::isImm(const SDValue &operand) { |
| 161 | return ConstantSDNode::classof(operand.getNode()); |
| 162 | } |
| 163 | |
| 164 | bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) { |
| 165 | SDNode *node = operand.getNode(); |
| 166 | if (!ConstantSDNode::classof(node)) |
| 167 | return false; |
| 168 | |
| 169 | ConstantSDNode *CN = cast<ConstantSDNode>(node); |
| 170 | imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(), MVT::i32); |
| 171 | return true; |
| 172 | } |