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 | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 43 | bool isImm(const SDValue &operand); |
| 44 | bool SelectImm(const SDValue &operand, SDValue &imm); |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 45 | }; // class PTXDAGToDAGISel |
| 46 | } // namespace |
| 47 | |
| 48 | // createPTXISelDag - This pass converts a legalized DAG into a |
| 49 | // PTX-specific DAG, ready for instruction scheduling |
| 50 | FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM, |
| 51 | CodeGenOpt::Level OptLevel) { |
| 52 | return new PTXDAGToDAGISel(TM, OptLevel); |
| 53 | } |
| 54 | |
| 55 | PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM, |
| 56 | CodeGenOpt::Level OptLevel) |
| 57 | : SelectionDAGISel(TM, OptLevel) {} |
| 58 | |
| 59 | SDNode *PTXDAGToDAGISel::Select(SDNode *Node) { |
| 60 | // SelectCode() is auto'gened |
| 61 | return SelectCode(Node); |
| 62 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 63 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 64 | // Match memory operand of the form [reg+reg] |
| 65 | bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) { |
| 66 | if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 || |
| 67 | isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1))) |
| 68 | return false; |
| 69 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 70 | R1 = Addr; |
| 71 | R2 = CurDAG->getTargetConstant(0, MVT::i32); |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 72 | return true; |
| 73 | } |
| 74 | |
| 75 | // 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] | 76 | bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base, |
| 77 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 78 | if (Addr.getOpcode() != ISD::ADD) { |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 79 | // let SelectADDRii handle the [imm] case |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 80 | if (isImm(Addr)) |
| 81 | return false; |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 82 | // it is [reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 83 | Base = Addr; |
| 84 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 85 | return true; |
| 86 | } |
| 87 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 88 | if (Addr.getNumOperands() < 2) |
| 89 | return false; |
| 90 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 91 | // let SelectADDRii handle the [imm+imm] case |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 92 | if (isImm(Addr.getOperand(0)) && isImm(Addr.getOperand(1))) |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 93 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 94 | |
| 95 | // try [reg+imm] and [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 96 | for (int i = 0; i < 2; i ++) |
| 97 | if (SelectImm(Addr.getOperand(1-i), Offset)) { |
| 98 | Base = Addr.getOperand(i); |
| 99 | return true; |
| 100 | } |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 101 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 102 | // neither [reg+imm] nor [imm+reg] |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 103 | return false; |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // Match memory operand of the form [imm+imm] and [imm] |
| 107 | bool PTXDAGToDAGISel::SelectADDRii(SDValue &Addr, SDValue &Base, |
| 108 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 109 | // is [imm+imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 110 | if (Addr.getOpcode() == ISD::ADD) { |
| 111 | return SelectImm(Addr.getOperand(0), Base) && |
| 112 | SelectImm(Addr.getOperand(1), Offset); |
| 113 | } |
| 114 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 115 | // is [imm]? |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 116 | if (SelectImm(Addr, Base)) { |
| 117 | Offset = CurDAG->getTargetConstant(0, MVT::i32); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | return false; |
| 122 | } |
| 123 | |
| 124 | bool PTXDAGToDAGISel::isImm(const SDValue &operand) { |
| 125 | return ConstantSDNode::classof(operand.getNode()); |
| 126 | } |
| 127 | |
| 128 | bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) { |
| 129 | SDNode *node = operand.getNode(); |
| 130 | if (!ConstantSDNode::classof(node)) |
| 131 | return false; |
| 132 | |
| 133 | ConstantSDNode *CN = cast<ConstantSDNode>(node); |
| 134 | imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(), MVT::i32); |
| 135 | return true; |
| 136 | } |