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" |
Justin Holewinski | 67a9184 | 2011-06-23 18:10:03 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Che-Liang Chiou | fd8978b | 2011-03-02 03:20:28 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
| 23 | namespace { |
| 24 | // PTXDAGToDAGISel - PTX specific code to select PTX machine |
| 25 | // instructions for SelectionDAG operations. |
| 26 | class PTXDAGToDAGISel : public SelectionDAGISel { |
| 27 | public: |
| 28 | PTXDAGToDAGISel(PTXTargetMachine &TM, CodeGenOpt::Level OptLevel); |
| 29 | |
| 30 | virtual const char *getPassName() const { |
| 31 | return "PTX DAG->DAG Pattern Instruction Selection"; |
| 32 | } |
| 33 | |
| 34 | SDNode *Select(SDNode *Node); |
| 35 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 36 | // Complex Pattern Selectors. |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 37 | bool SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 38 | bool SelectADDRri(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 39 | bool SelectADDRii(SDValue &Addr, SDValue &Base, SDValue &Offset); |
| 40 | |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 41 | // Include the pieces auto'gened from the target description |
| 42 | #include "PTXGenDAGISel.inc" |
| 43 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 44 | private: |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 45 | // We need this only because we can't match intruction BRAdp |
| 46 | // pattern (PTXbrcond bb:$d, ...) in PTXInstrInfo.td |
| 47 | SDNode *SelectBRCOND(SDNode *Node); |
| 48 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 49 | bool isImm(const SDValue &operand); |
| 50 | bool SelectImm(const SDValue &operand, SDValue &imm); |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 51 | |
| 52 | const PTXSubtarget& getSubtarget() const; |
Eric Christopher | 50880d0 | 2010-09-18 18:52:28 +0000 | [diff] [blame] | 53 | }; // class PTXDAGToDAGISel |
| 54 | } // namespace |
| 55 | |
| 56 | // createPTXISelDag - This pass converts a legalized DAG into a |
| 57 | // PTX-specific DAG, ready for instruction scheduling |
| 58 | FunctionPass *llvm::createPTXISelDag(PTXTargetMachine &TM, |
| 59 | CodeGenOpt::Level OptLevel) { |
| 60 | return new PTXDAGToDAGISel(TM, OptLevel); |
| 61 | } |
| 62 | |
| 63 | PTXDAGToDAGISel::PTXDAGToDAGISel(PTXTargetMachine &TM, |
| 64 | CodeGenOpt::Level OptLevel) |
| 65 | : SelectionDAGISel(TM, OptLevel) {} |
| 66 | |
| 67 | SDNode *PTXDAGToDAGISel::Select(SDNode *Node) { |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 68 | switch (Node->getOpcode()) { |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 69 | case ISD::BRCOND: |
| 70 | return SelectBRCOND(Node); |
| 71 | default: |
| 72 | return SelectCode(Node); |
| 73 | } |
Che-Liang Chiou | 8e5d01c | 2011-02-10 12:01:24 +0000 | [diff] [blame] | 74 | } |
| 75 | |
Che-Liang Chiou | 88d3367 | 2011-03-18 11:08:52 +0000 | [diff] [blame] | 76 | SDNode *PTXDAGToDAGISel::SelectBRCOND(SDNode *Node) { |
| 77 | assert(Node->getNumOperands() >= 3); |
| 78 | |
| 79 | SDValue Chain = Node->getOperand(0); |
| 80 | SDValue Pred = Node->getOperand(1); |
| 81 | SDValue Target = Node->getOperand(2); // branch target |
| 82 | SDValue PredOp = CurDAG->getTargetConstant(PTX::PRED_NORMAL, MVT::i32); |
| 83 | DebugLoc dl = Node->getDebugLoc(); |
| 84 | |
| 85 | assert(Target.getOpcode() == ISD::BasicBlock); |
| 86 | assert(Pred.getValueType() == MVT::i1); |
| 87 | |
| 88 | // Emit BRAdp |
| 89 | SDValue Ops[] = { Target, Pred, PredOp, Chain }; |
| 90 | return CurDAG->getMachineNode(PTX::BRAdp, dl, MVT::Other, Ops, 4); |
| 91 | } |
| 92 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 93 | // Match memory operand of the form [reg+reg] |
| 94 | bool PTXDAGToDAGISel::SelectADDRrr(SDValue &Addr, SDValue &R1, SDValue &R2) { |
| 95 | if (Addr.getOpcode() != ISD::ADD || Addr.getNumOperands() < 2 || |
| 96 | isImm(Addr.getOperand(0)) || isImm(Addr.getOperand(1))) |
| 97 | return false; |
| 98 | |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 99 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 100 | |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 101 | R1 = Addr; |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 102 | R2 = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 103 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 104 | return true; |
| 105 | } |
| 106 | |
| 107 | // 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] | 108 | bool PTXDAGToDAGISel::SelectADDRri(SDValue &Addr, SDValue &Base, |
| 109 | SDValue &Offset) { |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 110 | if (Addr.getOpcode() != ISD::ADD) { |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 111 | // let SelectADDRii handle the [imm] case |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 112 | if (isImm(Addr)) |
| 113 | return false; |
Che-Liang Chiou | c88e91b | 2011-01-01 11:58:58 +0000 | [diff] [blame] | 114 | // it is [reg] |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 115 | |
| 116 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 117 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 118 | Base = Addr; |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 119 | Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 120 | |
Che-Liang Chiou | fc7072c | 2010-12-22 10:38:51 +0000 | [diff] [blame] | 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)) { |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 153 | assert(Addr.getValueType().isSimple() && "Type must be simple"); |
| 154 | |
| 155 | Offset = CurDAG->getTargetConstant(0, Addr.getValueType().getSimpleVT()); |
| 156 | |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 157 | return true; |
| 158 | } |
| 159 | |
| 160 | return false; |
| 161 | } |
| 162 | |
| 163 | bool PTXDAGToDAGISel::isImm(const SDValue &operand) { |
| 164 | return ConstantSDNode::classof(operand.getNode()); |
| 165 | } |
| 166 | |
| 167 | bool PTXDAGToDAGISel::SelectImm(const SDValue &operand, SDValue &imm) { |
| 168 | SDNode *node = operand.getNode(); |
| 169 | if (!ConstantSDNode::classof(node)) |
| 170 | return false; |
| 171 | |
| 172 | ConstantSDNode *CN = cast<ConstantSDNode>(node); |
Justin Holewinski | d662576 | 2011-03-23 16:58:51 +0000 | [diff] [blame] | 173 | imm = CurDAG->getTargetConstant(*CN->getConstantIntValue(), |
| 174 | operand.getValueType()); |
Che-Liang Chiou | 3f8e617 | 2010-11-30 07:34:44 +0000 | [diff] [blame] | 175 | return true; |
| 176 | } |
Che-Liang Chiou | f48817c | 2011-03-02 07:36:48 +0000 | [diff] [blame] | 177 | |
| 178 | const PTXSubtarget& PTXDAGToDAGISel::getSubtarget() const |
| 179 | { |
| 180 | return TM.getSubtarget<PTXSubtarget>(); |
| 181 | } |
| 182 | |