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