Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 1 | //===-- AlphaISelDAGToDAG.cpp - Alpha pattern matching inst selector ------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Andrew Lenharth and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines a pattern matching instruction selector for Alpha, |
| 11 | // converting from a legalized dag to a Alpha dag. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "Alpha.h" |
| 16 | #include "AlphaTargetMachine.h" |
| 17 | #include "AlphaISelLowering.h" |
| 18 | #include "llvm/CodeGen/MachineInstrBuilder.h" |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 19 | #include "llvm/CodeGen/MachineFrameInfo.h" |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/SSARegMap.h" |
| 22 | #include "llvm/CodeGen/SelectionDAG.h" |
| 23 | #include "llvm/CodeGen/SelectionDAGISel.h" |
| 24 | #include "llvm/Target/TargetOptions.h" |
| 25 | #include "llvm/ADT/Statistic.h" |
| 26 | #include "llvm/Constants.h" |
| 27 | #include "llvm/GlobalValue.h" |
| 28 | #include "llvm/Support/Debug.h" |
| 29 | #include "llvm/Support/MathExtras.h" |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 30 | #include <algorithm> |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 31 | using namespace llvm; |
| 32 | |
| 33 | namespace { |
| 34 | |
| 35 | //===--------------------------------------------------------------------===// |
| 36 | /// AlphaDAGToDAGISel - Alpha specific code to select Alpha machine |
| 37 | /// instructions for SelectionDAG operations. |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 38 | class AlphaDAGToDAGISel : public SelectionDAGISel { |
| 39 | AlphaTargetLowering AlphaLowering; |
| 40 | |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 41 | static const int IMM_LOW = -32768; |
| 42 | static const int IMM_HIGH = 32767; |
| 43 | static const int IMM_MULT = 65536; |
| 44 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 45 | public: |
| 46 | AlphaDAGToDAGISel(TargetMachine &TM) |
| 47 | : SelectionDAGISel(AlphaLowering), AlphaLowering(TM) {} |
| 48 | |
| 49 | /// getI64Imm - Return a target constant with the specified value, of type |
| 50 | /// i64. |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 51 | inline SDOperand getI64Imm(int64_t Imm) { |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 52 | return CurDAG->getTargetConstant(Imm, MVT::i64); |
| 53 | } |
| 54 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 55 | // Select - Convert the specified operand from a target-independent to a |
| 56 | // target-specific node if it hasn't already been changed. |
| 57 | SDOperand Select(SDOperand Op); |
| 58 | |
| 59 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 60 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 61 | virtual void InstructionSelectBasicBlock(SelectionDAG &DAG); |
| 62 | |
| 63 | virtual const char *getPassName() const { |
| 64 | return "Alpha DAG->DAG Pattern Instruction Selection"; |
| 65 | } |
| 66 | |
| 67 | // Include the pieces autogenerated from the target description. |
| 68 | #include "AlphaGenDAGISel.inc" |
| 69 | |
| 70 | private: |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 71 | SDOperand getGlobalBaseReg(); |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 72 | SDOperand getRASaveReg(); |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 73 | SDOperand SelectCALL(SDOperand Op); |
| 74 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 75 | }; |
| 76 | } |
| 77 | |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 78 | /// getGlobalBaseReg - Output the instructions required to put the |
| 79 | /// GOT address into a register. |
| 80 | /// |
| 81 | SDOperand AlphaDAGToDAGISel::getGlobalBaseReg() { |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 82 | return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 83 | AlphaLowering.getVRegGP(), |
| 84 | MVT::i64); |
| 85 | } |
| 86 | |
| 87 | /// getRASaveReg - Grab the return address |
| 88 | /// |
| 89 | SDOperand AlphaDAGToDAGISel::getRASaveReg() { |
| 90 | return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), |
| 91 | AlphaLowering.getVRegRA(), |
| 92 | MVT::i64); |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 93 | } |
| 94 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 95 | /// InstructionSelectBasicBlock - This callback is invoked by |
| 96 | /// SelectionDAGISel when it has created a SelectionDAG for us to codegen. |
| 97 | void AlphaDAGToDAGISel::InstructionSelectBasicBlock(SelectionDAG &DAG) { |
| 98 | DEBUG(BB->dump()); |
| 99 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 100 | // Select target instructions for the DAG. |
| 101 | DAG.setRoot(Select(DAG.getRoot())); |
| 102 | CodeGenMap.clear(); |
| 103 | DAG.RemoveDeadNodes(); |
| 104 | |
| 105 | // Emit machine code to BB. |
| 106 | ScheduleAndEmitDAG(DAG); |
| 107 | } |
| 108 | |
| 109 | // Select - Convert the specified operand from a target-independent to a |
| 110 | // target-specific node if it hasn't already been changed. |
| 111 | SDOperand AlphaDAGToDAGISel::Select(SDOperand Op) { |
| 112 | SDNode *N = Op.Val; |
| 113 | if (N->getOpcode() >= ISD::BUILTIN_OP_END && |
| 114 | N->getOpcode() < AlphaISD::FIRST_NUMBER) |
| 115 | return Op; // Already selected. |
| 116 | |
| 117 | // If this has already been converted, use it. |
| 118 | std::map<SDOperand, SDOperand>::iterator CGMI = CodeGenMap.find(Op); |
| 119 | if (CGMI != CodeGenMap.end()) return CGMI->second; |
| 120 | |
| 121 | switch (N->getOpcode()) { |
| 122 | default: break; |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 123 | case ISD::TAILCALL: |
| 124 | case ISD::CALL: return SelectCALL(Op); |
| 125 | |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 126 | case ISD::DYNAMIC_STACKALLOC: { |
| 127 | if (!isa<ConstantSDNode>(N->getOperand(2)) || |
| 128 | cast<ConstantSDNode>(N->getOperand(2))->getValue() != 0) { |
| 129 | std::cerr << "Cannot allocate stack object with greater alignment than" |
| 130 | << " the stack alignment yet!"; |
| 131 | abort(); |
| 132 | } |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 133 | |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 134 | SDOperand Chain = Select(N->getOperand(0)); |
| 135 | SDOperand Amt = Select(N->getOperand(1)); |
| 136 | SDOperand Reg = CurDAG->getRegister(Alpha::R30, MVT::i64); |
| 137 | SDOperand Val = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64); |
| 138 | Chain = Val.getValue(1); |
| 139 | |
| 140 | // Subtract the amount (guaranteed to be a multiple of the stack alignment) |
| 141 | // from the stack pointer, giving us the result pointer. |
| 142 | SDOperand Result = CurDAG->getTargetNode(Alpha::SUBQ, MVT::i64, Val, Amt); |
| 143 | |
| 144 | // Copy this result back into R30. |
| 145 | Chain = CurDAG->getNode(ISD::CopyToReg, MVT::Other, Chain, Reg, Result); |
| 146 | |
| 147 | // Copy this result back out of R30 to make sure we're not using the stack |
| 148 | // space without decrementing the stack pointer. |
| 149 | Result = CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64); |
| 150 | |
| 151 | // Finally, replace the DYNAMIC_STACKALLOC with the copyfromreg. |
| 152 | CodeGenMap[Op.getValue(0)] = Result; |
| 153 | CodeGenMap[Op.getValue(1)] = Result.getValue(1); |
| 154 | return SDOperand(Result.Val, Op.ResNo); |
| 155 | } |
Andrew Lenharth | 8b7f14e | 2005-10-23 03:43:48 +0000 | [diff] [blame] | 156 | case ISD::BRCOND: { |
Andrew Lenharth | f88471d | 2005-12-06 20:43:30 +0000 | [diff] [blame] | 157 | if (N->getOperand(1).getOpcode() == ISD::SETCC && |
| 158 | MVT::isFloatingPoint(N->getOperand(1).getOperand(0).getValueType())) { |
| 159 | SDOperand Chain = Select(N->getOperand(0)); |
| 160 | SDOperand CC1 = Select(N->getOperand(1).getOperand(0)); |
| 161 | SDOperand CC2 = Select(N->getOperand(1).getOperand(1)); |
| 162 | ISD::CondCode cCode= cast<CondCodeSDNode>(N->getOperand(1).getOperand(2))->get(); |
| 163 | |
| 164 | bool rev = false; |
| 165 | bool isNE = false; |
| 166 | unsigned Opc = Alpha::WTF; |
| 167 | switch(cCode) { |
| 168 | default: N->dump(); assert(0 && "Unknown FP comparison!"); |
| 169 | case ISD::SETEQ: Opc = Alpha::CMPTEQ; break; |
| 170 | case ISD::SETLT: Opc = Alpha::CMPTLT; break; |
| 171 | case ISD::SETLE: Opc = Alpha::CMPTLE; break; |
| 172 | case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break; |
| 173 | case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break; |
| 174 | case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break; |
| 175 | }; |
| 176 | SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64, |
| 177 | rev?CC2:CC1, |
| 178 | rev?CC1:CC2); |
| 179 | |
| 180 | MachineBasicBlock *Dest = |
| 181 | cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock(); |
| 182 | if(isNE) |
| 183 | return CurDAG->SelectNodeTo(N, Alpha::FBEQ, MVT::Other, cmp, |
| 184 | CurDAG->getBasicBlock(Dest), Chain); |
| 185 | else |
| 186 | return CurDAG->SelectNodeTo(N, Alpha::FBNE, MVT::Other, cmp, |
| 187 | CurDAG->getBasicBlock(Dest), Chain); |
| 188 | } |
Andrew Lenharth | 8b7f14e | 2005-10-23 03:43:48 +0000 | [diff] [blame] | 189 | SDOperand Chain = Select(N->getOperand(0)); |
| 190 | SDOperand CC = Select(N->getOperand(1)); |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 191 | MachineBasicBlock *Dest = |
| 192 | cast<BasicBlockSDNode>(N->getOperand(2))->getBasicBlock(); |
Chris Lattner | d5acfb4 | 2005-11-30 23:04:38 +0000 | [diff] [blame] | 193 | return CurDAG->SelectNodeTo(N, Alpha::BNE, MVT::Other, CC, |
| 194 | CurDAG->getBasicBlock(Dest), Chain); |
Andrew Lenharth | 8b7f14e | 2005-10-23 03:43:48 +0000 | [diff] [blame] | 195 | } |
Andrew Lenharth | 8b7f14e | 2005-10-23 03:43:48 +0000 | [diff] [blame] | 196 | |
Andrew Lenharth | f88471d | 2005-12-06 20:43:30 +0000 | [diff] [blame] | 197 | case ISD::BR: |
Chris Lattner | d5acfb4 | 2005-11-30 23:04:38 +0000 | [diff] [blame] | 198 | return CurDAG->SelectNodeTo(N, Alpha::BR_DAG, MVT::Other, N->getOperand(1), |
| 199 | Select(N->getOperand(0))); |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 200 | case ISD::FrameIndex: { |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 201 | int FI = cast<FrameIndexSDNode>(N)->getIndex(); |
Chris Lattner | d5acfb4 | 2005-11-30 23:04:38 +0000 | [diff] [blame] | 202 | return CurDAG->SelectNodeTo(N, Alpha::LDA, MVT::i64, |
| 203 | CurDAG->getTargetFrameIndex(FI, MVT::i32), |
| 204 | getI64Imm(0)); |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 205 | } |
Andrew Lenharth | 4e62951 | 2005-12-24 05:36:33 +0000 | [diff] [blame] | 206 | case AlphaISD::GlobalBaseReg: |
| 207 | return getGlobalBaseReg(); |
| 208 | |
Andrew Lenharth | 53d8970 | 2005-12-25 01:34:27 +0000 | [diff] [blame^] | 209 | case AlphaISD::DivCall: { |
| 210 | SDOperand Chain = CurDAG->getEntryNode(); |
| 211 | Chain = CurDAG->getCopyToReg(Chain, Alpha::R24, Select(Op.getOperand(1)), |
| 212 | SDOperand(0,0)); |
| 213 | Chain = CurDAG->getCopyToReg(Chain, Alpha::R25, Select(Op.getOperand(2)), |
| 214 | Chain.getValue(1)); |
| 215 | Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Select(Op.getOperand(0)), |
| 216 | Chain.getValue(1)); |
| 217 | Chain = CurDAG->getTargetNode(Alpha::JSRsDAG, MVT::Other, MVT::Flag, |
| 218 | Chain, Chain.getValue(1)); |
| 219 | Chain = CurDAG->getCopyFromReg(Chain, Alpha::R27, MVT::i64, |
| 220 | Chain.getValue(1)); |
| 221 | return CurDAG->SelectNodeTo(N, Alpha::BIS, MVT::i64, Chain, Chain); |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 222 | } |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 223 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 224 | case ISD::RET: { |
| 225 | SDOperand Chain = Select(N->getOperand(0)); // Token chain. |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 226 | SDOperand InFlag; |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 227 | |
| 228 | if (N->getNumOperands() == 2) { |
| 229 | SDOperand Val = Select(N->getOperand(1)); |
| 230 | if (N->getOperand(1).getValueType() == MVT::i64) { |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 231 | Chain = CurDAG->getCopyToReg(Chain, Alpha::R0, Val, InFlag); |
| 232 | InFlag = Chain.getValue(1); |
Andrew Lenharth | e41419f | 2005-12-11 03:54:31 +0000 | [diff] [blame] | 233 | } else if (N->getOperand(1).getValueType() == MVT::f64 || |
| 234 | N->getOperand(1).getValueType() == MVT::f32) { |
| 235 | Chain = CurDAG->getCopyToReg(Chain, Alpha::F0, Val, InFlag); |
| 236 | InFlag = Chain.getValue(1); |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 239 | Chain = CurDAG->getCopyToReg(Chain, Alpha::R26, getRASaveReg(), InFlag); |
| 240 | InFlag = Chain.getValue(1); |
| 241 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 242 | // Finally, select this to a ret instruction. |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 243 | return CurDAG->SelectNodeTo(N, Alpha::RETDAG, MVT::Other, Chain, InFlag); |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 244 | } |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 245 | case ISD::Constant: { |
| 246 | int64_t val = (int64_t)cast<ConstantSDNode>(N)->getValue(); |
| 247 | if (val > (int64_t)IMM_HIGH +(int64_t)IMM_HIGH* (int64_t)IMM_MULT || |
| 248 | val < (int64_t)IMM_LOW + (int64_t)IMM_LOW * (int64_t)IMM_MULT) { |
| 249 | MachineConstantPool *CP = BB->getParent()->getConstantPool(); |
| 250 | ConstantUInt *C = |
| 251 | ConstantUInt::get(Type::getPrimitiveType(Type::ULongTyID) , val); |
| 252 | SDOperand Tmp, CPI = CurDAG->getTargetConstantPool(C, MVT::i64); |
| 253 | Tmp = CurDAG->getTargetNode(Alpha::LDAHr, MVT::i64, CPI, getGlobalBaseReg()); |
Andrew Lenharth | b457a93 | 2005-12-05 17:51:02 +0000 | [diff] [blame] | 254 | return CurDAG->SelectNodeTo(N, Alpha::LDQr, MVT::i64, CPI, Tmp); |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 255 | } |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 256 | break; |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 257 | } |
| 258 | case ISD::ConstantFP: |
| 259 | if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N)) { |
| 260 | bool isDouble = N->getValueType(0) == MVT::f64; |
| 261 | MVT::ValueType T = isDouble ? MVT::f64 : MVT::f32; |
| 262 | if (CN->isExactlyValue(+0.0)) { |
Chris Lattner | d5acfb4 | 2005-11-30 23:04:38 +0000 | [diff] [blame] | 263 | return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYST : Alpha::CPYSS, |
| 264 | T, CurDAG->getRegister(Alpha::F31, T), |
| 265 | CurDAG->getRegister(Alpha::F31, T)); |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 266 | } else if ( CN->isExactlyValue(-0.0)) { |
Chris Lattner | d5acfb4 | 2005-11-30 23:04:38 +0000 | [diff] [blame] | 267 | return CurDAG->SelectNodeTo(N, isDouble ? Alpha::CPYSNT : Alpha::CPYSNS, |
| 268 | T, CurDAG->getRegister(Alpha::F31, T), |
| 269 | CurDAG->getRegister(Alpha::F31, T)); |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 270 | } else { |
| 271 | abort(); |
| 272 | } |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 273 | break; |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 274 | } |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 275 | |
| 276 | case ISD::SETCC: |
| 277 | if (MVT::isFloatingPoint(N->getOperand(0).Val->getValueType(0))) { |
| 278 | unsigned Opc = Alpha::WTF; |
| 279 | ISD::CondCode CC = cast<CondCodeSDNode>(N->getOperand(2))->get(); |
| 280 | bool rev = false; |
Andrew Lenharth | b2156f9 | 2005-11-30 17:11:20 +0000 | [diff] [blame] | 281 | bool isNE = false; |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 282 | switch(CC) { |
| 283 | default: N->dump(); assert(0 && "Unknown FP comparison!"); |
| 284 | case ISD::SETEQ: Opc = Alpha::CMPTEQ; break; |
| 285 | case ISD::SETLT: Opc = Alpha::CMPTLT; break; |
| 286 | case ISD::SETLE: Opc = Alpha::CMPTLE; break; |
| 287 | case ISD::SETGT: Opc = Alpha::CMPTLT; rev = true; break; |
| 288 | case ISD::SETGE: Opc = Alpha::CMPTLE; rev = true; break; |
Andrew Lenharth | b2156f9 | 2005-11-30 17:11:20 +0000 | [diff] [blame] | 289 | case ISD::SETNE: Opc = Alpha::CMPTEQ; isNE = true; break; |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 290 | }; |
| 291 | SDOperand tmp1 = Select(N->getOperand(0)), |
| 292 | tmp2 = Select(N->getOperand(1)); |
| 293 | SDOperand cmp = CurDAG->getTargetNode(Opc, MVT::f64, |
| 294 | rev?tmp2:tmp1, |
| 295 | rev?tmp1:tmp2); |
Andrew Lenharth | b2156f9 | 2005-11-30 17:11:20 +0000 | [diff] [blame] | 296 | if (isNE) |
| 297 | cmp = CurDAG->getTargetNode(Alpha::CMPTEQ, MVT::f64, cmp, |
| 298 | CurDAG->getRegister(Alpha::F31, MVT::f64)); |
| 299 | |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 300 | SDOperand LD; |
| 301 | if (AlphaLowering.hasITOF()) { |
| 302 | LD = CurDAG->getNode(AlphaISD::FTOIT_, MVT::i64, cmp); |
| 303 | } else { |
| 304 | int FrameIdx = |
| 305 | CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8); |
| 306 | SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64); |
| 307 | SDOperand ST = CurDAG->getTargetNode(Alpha::STT, MVT::Other, |
| 308 | cmp, FI, CurDAG->getRegister(Alpha::R31, MVT::i64)); |
| 309 | LD = CurDAG->getTargetNode(Alpha::LDQ, MVT::i64, FI, |
| 310 | CurDAG->getRegister(Alpha::R31, MVT::i64), |
| 311 | ST); |
| 312 | } |
| 313 | SDOperand FP = CurDAG->getTargetNode(Alpha::CMPULT, MVT::i64, |
| 314 | CurDAG->getRegister(Alpha::R31, MVT::i64), |
| 315 | LD); |
| 316 | return FP; |
| 317 | } |
| 318 | break; |
Andrew Lenharth | cd80496 | 2005-11-30 16:10:29 +0000 | [diff] [blame] | 319 | |
Andrew Lenharth | 361f45a | 2005-12-12 17:43:52 +0000 | [diff] [blame] | 320 | case ISD::SELECT: |
| 321 | if (MVT::isFloatingPoint(N->getValueType(0)) && |
| 322 | (N->getOperand(0).getOpcode() != ISD::SETCC || |
| 323 | !MVT::isFloatingPoint(N->getOperand(0).getOperand(1).getValueType()))) { |
| 324 | //This should be the condition not covered by the Patterns |
| 325 | //FIXME: Don't have SelectCode die, but rather return something testable |
| 326 | // so that things like this can be caught in fall though code |
| 327 | //move int to fp |
| 328 | bool isDouble = N->getValueType(0) == MVT::f64; |
| 329 | SDOperand LD, |
| 330 | cond = Select(N->getOperand(0)), |
| 331 | TV = Select(N->getOperand(1)), |
| 332 | FV = Select(N->getOperand(2)); |
| 333 | |
| 334 | if (AlphaLowering.hasITOF()) { |
| 335 | LD = CurDAG->getNode(AlphaISD::ITOFT_, MVT::f64, cond); |
| 336 | } else { |
| 337 | int FrameIdx = |
| 338 | CurDAG->getMachineFunction().getFrameInfo()->CreateStackObject(8, 8); |
| 339 | SDOperand FI = CurDAG->getFrameIndex(FrameIdx, MVT::i64); |
| 340 | SDOperand ST = CurDAG->getTargetNode(Alpha::STQ, MVT::Other, |
| 341 | cond, FI, CurDAG->getRegister(Alpha::R31, MVT::i64)); |
| 342 | LD = CurDAG->getTargetNode(Alpha::LDT, MVT::f64, FI, |
| 343 | CurDAG->getRegister(Alpha::R31, MVT::i64), |
| 344 | ST); |
| 345 | } |
Andrew Lenharth | 110f224 | 2005-12-12 20:30:09 +0000 | [diff] [blame] | 346 | SDOperand FP = CurDAG->getTargetNode(isDouble?Alpha::FCMOVNET:Alpha::FCMOVNES, |
Andrew Lenharth | 361f45a | 2005-12-12 17:43:52 +0000 | [diff] [blame] | 347 | MVT::f64, FV, TV, LD); |
| 348 | return FP; |
| 349 | } |
| 350 | break; |
| 351 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 352 | } |
Andrew Lenharth | cd80496 | 2005-11-30 16:10:29 +0000 | [diff] [blame] | 353 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 354 | return SelectCode(Op); |
| 355 | } |
| 356 | |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 357 | SDOperand AlphaDAGToDAGISel::SelectCALL(SDOperand Op) { |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 358 | //TODO: add flag stuff to prevent nondeturministic breakage! |
| 359 | |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 360 | SDNode *N = Op.Val; |
| 361 | SDOperand Chain = Select(N->getOperand(0)); |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 362 | SDOperand Addr = Select(N->getOperand(1)); |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 363 | SDOperand InFlag; // Null incoming flag value. |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 364 | |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 365 | std::vector<SDOperand> CallOperands; |
| 366 | std::vector<MVT::ValueType> TypeOperands; |
| 367 | |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 368 | //grab the arguments |
| 369 | for(int i = 2, e = N->getNumOperands(); i < e; ++i) { |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 370 | TypeOperands.push_back(N->getOperand(i).getValueType()); |
Andrew Lenharth | 8b7f14e | 2005-10-23 03:43:48 +0000 | [diff] [blame] | 371 | CallOperands.push_back(Select(N->getOperand(i))); |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 372 | } |
Andrew Lenharth | 8b7f14e | 2005-10-23 03:43:48 +0000 | [diff] [blame] | 373 | int count = N->getNumOperands() - 2; |
| 374 | |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 375 | static const unsigned args_int[] = {Alpha::R16, Alpha::R17, Alpha::R18, |
| 376 | Alpha::R19, Alpha::R20, Alpha::R21}; |
| 377 | static const unsigned args_float[] = {Alpha::F16, Alpha::F17, Alpha::F18, |
| 378 | Alpha::F19, Alpha::F20, Alpha::F21}; |
| 379 | |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 380 | for (int i = 6; i < count; ++i) { |
| 381 | unsigned Opc = Alpha::WTF; |
| 382 | if (MVT::isInteger(TypeOperands[i])) { |
| 383 | Opc = Alpha::STQ; |
| 384 | } else if (TypeOperands[i] == MVT::f32) { |
| 385 | Opc = Alpha::STS; |
| 386 | } else if (TypeOperands[i] == MVT::f64) { |
| 387 | Opc = Alpha::STT; |
| 388 | } else |
| 389 | assert(0 && "Unknown operand"); |
| 390 | Chain = CurDAG->getTargetNode(Opc, MVT::Other, CallOperands[i], |
| 391 | getI64Imm((i - 6) * 8), |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 392 | CurDAG->getCopyFromReg(Chain, Alpha::R30, MVT::i64), |
Andrew Lenharth | 7f0db91 | 2005-11-30 07:19:56 +0000 | [diff] [blame] | 393 | Chain); |
| 394 | } |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 395 | for (int i = 0; i < std::min(6, count); ++i) { |
| 396 | if (MVT::isInteger(TypeOperands[i])) { |
| 397 | Chain = CurDAG->getCopyToReg(Chain, args_int[i], CallOperands[i], InFlag); |
| 398 | InFlag = Chain.getValue(1); |
| 399 | } else if (TypeOperands[i] == MVT::f32 || TypeOperands[i] == MVT::f64) { |
| 400 | Chain = CurDAG->getCopyToReg(Chain, args_float[i], CallOperands[i], InFlag); |
| 401 | InFlag = Chain.getValue(1); |
| 402 | } else |
| 403 | assert(0 && "Unknown operand"); |
| 404 | } |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 405 | |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 406 | |
| 407 | Chain = CurDAG->getCopyToReg(Chain, Alpha::R27, Addr, InFlag); |
| 408 | InFlag = Chain.getValue(1); |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 409 | // Finally, once everything is in registers to pass to the call, emit the |
| 410 | // call itself. |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 411 | Chain = CurDAG->getTargetNode(Alpha::JSRDAG, MVT::Other, MVT::Flag, |
| 412 | Chain, InFlag ); |
| 413 | InFlag = Chain.getValue(1); |
| 414 | |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 415 | std::vector<SDOperand> CallResults; |
| 416 | |
| 417 | switch (N->getValueType(0)) { |
| 418 | default: assert(0 && "Unexpected ret value!"); |
| 419 | case MVT::Other: break; |
| 420 | case MVT::i64: |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 421 | Chain = CurDAG->getCopyFromReg(Chain, Alpha::R0, MVT::i64, InFlag).getValue(1); |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 422 | CallResults.push_back(Chain.getValue(0)); |
| 423 | break; |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 424 | case MVT::f32: |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 425 | Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f32, InFlag).getValue(1); |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 426 | CallResults.push_back(Chain.getValue(0)); |
| 427 | break; |
| 428 | case MVT::f64: |
Andrew Lenharth | 9352622 | 2005-12-01 01:53:10 +0000 | [diff] [blame] | 429 | Chain = CurDAG->getCopyFromReg(Chain, Alpha::F0, MVT::f64, InFlag).getValue(1); |
Andrew Lenharth | 50b3784 | 2005-11-22 04:20:06 +0000 | [diff] [blame] | 430 | CallResults.push_back(Chain.getValue(0)); |
| 431 | break; |
Andrew Lenharth | 756fbeb | 2005-10-22 22:06:58 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | CallResults.push_back(Chain); |
| 435 | for (unsigned i = 0, e = CallResults.size(); i != e; ++i) |
| 436 | CodeGenMap[Op.getValue(i)] = CallResults[i]; |
| 437 | return CallResults[Op.ResNo]; |
| 438 | } |
| 439 | |
| 440 | |
Andrew Lenharth | d97591a | 2005-10-20 00:29:02 +0000 | [diff] [blame] | 441 | /// createAlphaISelDag - This pass converts a legalized DAG into a |
| 442 | /// Alpha-specific DAG, ready for instruction scheduling. |
| 443 | /// |
| 444 | FunctionPass *llvm::createAlphaISelDag(TargetMachine &TM) { |
| 445 | return new AlphaDAGToDAGISel(TM); |
| 446 | } |