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