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" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 18 | |
| 19 | using namespace llvm; |
| 20 | |
| 21 | namespace { |
| 22 | // PTXDAGToDAGISel - PTX specific code to select PTX machine |
| 23 | // instructions for SelectionDAG operations. |
| 24 | class PTXDAGToDAGISel : public SelectionDAGISel { |
| 25 | public: |
| 26 | PTXDAGToDAGISel(PTXTargetMachine &TM, CodeGenOpt::Level OptLevel); |
| 27 | |
| 28 | virtual const char *getPassName() const { |
| 29 | return "PTX DAG->DAG Pattern Instruction Selection"; |
| 30 | } |
| 31 | |
| 32 | SDNode *Select(SDNode *Node); |
| 33 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 34 | // Complex Pattern Selectors. |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 35 | bool SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 36 | bool SelectADDRri(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 37 | bool SelectADDRii(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 38 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 39 | // Include the pieces auto'gened from the target description |
| 40 | #include "PTXGenDAGISel.inc" |
| 41 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 42 | private: |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 43 | SDNode *SelectREAD_PARAM(SDNode *Node); |
| 44 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 45 | bool isImm(const SDValue &operand); |
| 46 | bool SelectImm(const SDValue &operand, SDValue &imm); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 47 | }; // class PTXDAGToDAGISel |
| 48 | } // namespace |
| 49 | |
| 50 | // createPTXISelDag - This pass converts a legalized DAG into a |
| 51 | // PTX-specific DAG, ready for instruction scheduling |
| 52 | FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM, |
| 53 | CodeGenOpt::Level OptLevel) { |
| 54 | return new PTXDAGToDAGISel(TM, OptLevel); |
| 55 | } |
| 56 | |
| 57 | PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM, |
| 58 | CodeGenOpt::Level OptLevel) |
| 59 | : SelectionDAGISel(TM, OptLevel) {} |
| 60 | |
| 61 | SDNode *PTXDAGToDAGISel::Select(SDNode *Node) { |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 62 | if (Node->getOpcode() == PTXISD::READ_PARAM) |
| 63 | return SelectREAD_PARAM(Node); |
| 64 | else |
| 65 | return SelectCode(Node); |
| 66 | } |
| 67 | |
| 68 | SDNode *PTXDAGToDAGISel::SelectREAD_PARAM(SDNode *Node) { |
| 69 | SDValue index = Node->getOperand(1); |
| 70 | DebugLoc dl = Node->getDebugLoc(); |
| 71 | |
| 72 | if (index.getOpcode() != ISD::TargetConstant) |
| 73 | llvm_unreachable("READ_PARAM: index is not ISD::TargetConstant"); |
| 74 | |
| 75 | return PTXInstrInfo:: |
| 76 | GetPTXMachineNode(CurDAG, PTX::LDpi, dl, MVT::i32, index); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 77 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 78 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 79 | // Match memory operand of the form [reg+reg] |
| 80 | bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) { |
| 81 | if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 || |
| 82 | isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1))) |
| 83 | return false; |
| 84 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 85 | R1 = Addr; |
| 86 | R2 = CurDAG->getTargetConstant(0, MVT::i32); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 87 | return true; |
| 88 | } |
| 89 | |
| 90 | // 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] | 91 | bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base, |
| 92 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 93 | if (Addr.getOpcode() != ISD::ADD) { |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 94 | // let SelectADDRii handle the [imm] case |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 95 | if (isImm(Addr)) |
| 96 | return false; |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 97 | // it is [reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 98 | Base = Addr; |
| 99 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 100 | return true; |
| 101 | } |
| 102 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 103 | if (Addr.getNumOperands() < 2) |
| 104 | return false; |
| 105 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 106 | // let SelectADDRii handle the [imm+imm] case |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 107 | if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1))) |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 108 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 109 | |
| 110 | // try [reg+imm] and [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 111 | for (int i = 0; i < 2; i ++) |
| 112 | if (SelectImm(Addr.getOperand(1-i), Offset)) { |
| 113 | Base = Addr.getOperand(i); |
| 114 | return true; |
| 115 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 116 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 117 | // neither [reg+imm] nor [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 118 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | // Match memory operand of the form [imm+imm] and [imm] |
| 122 | bool PTXDAGToDAGISel::SelectADDRii(SDValue &Addr, SDValue &Base, |
| 123 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 124 | // is [imm+imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 125 | if (Addr.getOpcode() == ISD::ADD) { |
| 126 | return SelectImm(Addr.getOperand(0), Base) && |
| 127 | SelectImm(Addr.getOperand(1), Offset); |
| 128 | } |
| 129 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 130 | // is [imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 131 | if (SelectImm(Addr, Base)) { |
| 132 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | bool PTXDAGToDAGISel::isImm(const SDValue &operand) { |
| 140 | return ConstantSDNode::classof(operand.getNode()); |
| 141 | } |
| 142 | |
| 143 | bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) { |
| 144 | SDNode *node = operand.getNode(); |
| 145 | if (!ConstantSDNode::classof(node)) |
| 146 | return false; |
| 147 | |
| 148 | ConstantSDNode *CN = cast<ConstantSDNode>(node); |
| 149 | imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(), MVT::i32); |
| 150 | return true; |
| 151 | } |