Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1 | //===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the interfaces that X86 uses to lower LLVM code into a |
| 11 | // selection DAG. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "X86.h" |
| 16 | #include "X86ISelLowering.h" |
| 17 | #include "X86TargetMachine.h" |
| 18 | #include "llvm/CallingConv.h" |
| 19 | #include "llvm/Function.h" |
| 20 | #include "llvm/CodeGen/MachineFunction.h" |
| 21 | #include "llvm/CodeGen/MachineFrameInfo.h" |
| 22 | #include "llvm/CodeGen/SelectionDAG.h" |
| 23 | #include "llvm/CodeGen/SSARegMap.h" |
| 24 | #include "llvm/Target/TargetOptions.h" |
| 25 | using namespace llvm; |
| 26 | |
| 27 | // FIXME: temporary. |
| 28 | #include "llvm/Support/CommandLine.h" |
| 29 | static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden, |
| 30 | cl::desc("Enable fastcc on X86")); |
| 31 | |
| 32 | X86TargetLowering::X86TargetLowering(TargetMachine &TM) |
| 33 | : TargetLowering(TM) { |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 34 | // Set up the TargetLowering object. |
| 35 | |
| 36 | // X86 is weird, it always uses i8 for shift amounts and setcc results. |
| 37 | setShiftAmountType(MVT::i8); |
| 38 | setSetCCResultType(MVT::i8); |
| 39 | setSetCCResultContents(ZeroOrOneSetCCResult); |
| 40 | setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0 |
| 41 | |
| 42 | // Set up the register classes. |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 43 | addRegisterClass(MVT::i8, X86::R8RegisterClass); |
| 44 | addRegisterClass(MVT::i16, X86::R16RegisterClass); |
| 45 | addRegisterClass(MVT::i32, X86::R32RegisterClass); |
| 46 | |
| 47 | // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this |
| 48 | // operation. |
| 49 | setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote); |
| 50 | setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote); |
| 51 | setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote); |
| 52 | setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote); |
| 53 | |
| 54 | // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have |
| 55 | // this operation. |
| 56 | setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote); |
| 57 | setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote); |
| 58 | |
| 59 | if (!X86ScalarSSE) { |
| 60 | // We can handle SINT_TO_FP and FP_TO_SINT from/TO i64 even though i64 |
| 61 | // isn't legal. |
| 62 | setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom); |
| 63 | setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom); |
| 64 | setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom); |
| 65 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom); |
| 66 | } |
| 67 | |
| 68 | // Handle FP_TO_UINT by promoting the destination to a larger signed |
| 69 | // conversion. |
| 70 | setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote); |
| 71 | setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote); |
| 72 | setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote); |
| 73 | |
| 74 | if (!X86ScalarSSE) |
| 75 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote); |
| 76 | |
| 77 | // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have |
| 78 | // this operation. |
| 79 | setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote); |
| 80 | setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote); |
| 81 | setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote); |
| 82 | |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 83 | if (X86DAGIsel) { |
| 84 | setOperationAction(ISD::BRCOND , MVT::Other, Custom); |
| 85 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 86 | setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand); |
| 87 | setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand); |
| 88 | setOperationAction(ISD::MEMMOVE , MVT::Other, Expand); |
| 89 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand); |
Chris Lattner | e80242a | 2005-12-07 17:59:14 +0000 | [diff] [blame] | 90 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 91 | setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand); |
| 92 | setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand); |
| 93 | setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand); |
| 94 | setOperationAction(ISD::FREM , MVT::f64 , Expand); |
| 95 | setOperationAction(ISD::CTPOP , MVT::i8 , Expand); |
| 96 | setOperationAction(ISD::CTTZ , MVT::i8 , Expand); |
| 97 | setOperationAction(ISD::CTLZ , MVT::i8 , Expand); |
| 98 | setOperationAction(ISD::CTPOP , MVT::i16 , Expand); |
| 99 | setOperationAction(ISD::CTTZ , MVT::i16 , Expand); |
| 100 | setOperationAction(ISD::CTLZ , MVT::i16 , Expand); |
| 101 | setOperationAction(ISD::CTPOP , MVT::i32 , Expand); |
| 102 | setOperationAction(ISD::CTTZ , MVT::i32 , Expand); |
| 103 | setOperationAction(ISD::CTLZ , MVT::i32 , Expand); |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 104 | setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom); |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 105 | |
| 106 | setOperationAction(ISD::READIO , MVT::i1 , Expand); |
| 107 | setOperationAction(ISD::READIO , MVT::i8 , Expand); |
| 108 | setOperationAction(ISD::READIO , MVT::i16 , Expand); |
| 109 | setOperationAction(ISD::READIO , MVT::i32 , Expand); |
| 110 | setOperationAction(ISD::WRITEIO , MVT::i1 , Expand); |
| 111 | setOperationAction(ISD::WRITEIO , MVT::i8 , Expand); |
| 112 | setOperationAction(ISD::WRITEIO , MVT::i16 , Expand); |
| 113 | setOperationAction(ISD::WRITEIO , MVT::i32 , Expand); |
| 114 | |
| 115 | // These should be promoted to a larger select which is supported. |
| 116 | setOperationAction(ISD::SELECT , MVT::i1 , Promote); |
| 117 | setOperationAction(ISD::SELECT , MVT::i8 , Promote); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 118 | // X86 wants to expand cmov itself. |
| 119 | if (X86DAGIsel) { |
| 120 | setOperationAction(ISD::SELECT , MVT::i16 , Custom); |
| 121 | setOperationAction(ISD::SELECT , MVT::i32 , Custom); |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 122 | setOperationAction(ISD::SETCC , MVT::i8 , Custom); |
| 123 | setOperationAction(ISD::SETCC , MVT::i16 , Custom); |
| 124 | setOperationAction(ISD::SETCC , MVT::i32 , Custom); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 125 | } |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 126 | |
Chris Lattner | f73bae1 | 2005-11-29 06:16:21 +0000 | [diff] [blame] | 127 | // We don't have line number support yet. |
| 128 | setOperationAction(ISD::LOCATION, MVT::Other, Expand); |
| 129 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 130 | if (X86ScalarSSE) { |
| 131 | // Set up the FP register classes. |
| 132 | addRegisterClass(MVT::f32, X86::V4F4RegisterClass); |
| 133 | addRegisterClass(MVT::f64, X86::V2F8RegisterClass); |
| 134 | |
| 135 | // SSE has no load+extend ops |
| 136 | setOperationAction(ISD::EXTLOAD, MVT::f32, Expand); |
| 137 | setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand); |
| 138 | |
| 139 | // SSE has no i16 to fp conversion, only i32 |
| 140 | setOperationAction(ISD::SINT_TO_FP, MVT::i16, Promote); |
| 141 | setOperationAction(ISD::FP_TO_SINT, MVT::i16, Promote); |
| 142 | |
| 143 | // Expand FP_TO_UINT into a select. |
| 144 | // FIXME: We would like to use a Custom expander here eventually to do |
| 145 | // the optimal thing for SSE vs. the default expansion in the legalizer. |
| 146 | setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand); |
| 147 | |
| 148 | // We don't support sin/cos/sqrt/fmod |
| 149 | setOperationAction(ISD::FSIN , MVT::f64, Expand); |
| 150 | setOperationAction(ISD::FCOS , MVT::f64, Expand); |
| 151 | setOperationAction(ISD::FABS , MVT::f64, Expand); |
| 152 | setOperationAction(ISD::FNEG , MVT::f64, Expand); |
| 153 | setOperationAction(ISD::FREM , MVT::f64, Expand); |
| 154 | setOperationAction(ISD::FSIN , MVT::f32, Expand); |
| 155 | setOperationAction(ISD::FCOS , MVT::f32, Expand); |
| 156 | setOperationAction(ISD::FABS , MVT::f32, Expand); |
| 157 | setOperationAction(ISD::FNEG , MVT::f32, Expand); |
| 158 | setOperationAction(ISD::FREM , MVT::f32, Expand); |
| 159 | |
| 160 | addLegalFPImmediate(+0.0); // xorps / xorpd |
| 161 | } else { |
| 162 | // Set up the FP register classes. |
| 163 | addRegisterClass(MVT::f64, X86::RFPRegisterClass); |
| 164 | |
| 165 | if (!UnsafeFPMath) { |
| 166 | setOperationAction(ISD::FSIN , MVT::f64 , Expand); |
| 167 | setOperationAction(ISD::FCOS , MVT::f64 , Expand); |
| 168 | } |
| 169 | |
| 170 | addLegalFPImmediate(+0.0); // FLD0 |
| 171 | addLegalFPImmediate(+1.0); // FLD1 |
| 172 | addLegalFPImmediate(-0.0); // FLD0/FCHS |
| 173 | addLegalFPImmediate(-1.0); // FLD1/FCHS |
| 174 | } |
| 175 | computeRegisterProperties(); |
| 176 | |
| 177 | maxStoresPerMemSet = 8; // For %llvm.memset -> sequence of stores |
| 178 | maxStoresPerMemCpy = 8; // For %llvm.memcpy -> sequence of stores |
| 179 | maxStoresPerMemMove = 8; // For %llvm.memmove -> sequence of stores |
| 180 | allowUnalignedMemoryAccesses = true; // x86 supports it! |
| 181 | } |
| 182 | |
| 183 | std::vector<SDOperand> |
| 184 | X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) { |
| 185 | if (F.getCallingConv() == CallingConv::Fast && EnableFastCC) |
| 186 | return LowerFastCCArguments(F, DAG); |
| 187 | return LowerCCCArguments(F, DAG); |
| 188 | } |
| 189 | |
| 190 | std::pair<SDOperand, SDOperand> |
| 191 | X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy, |
| 192 | bool isVarArg, unsigned CallingConv, |
| 193 | bool isTailCall, |
| 194 | SDOperand Callee, ArgListTy &Args, |
| 195 | SelectionDAG &DAG) { |
| 196 | assert((!isVarArg || CallingConv == CallingConv::C) && |
| 197 | "Only C takes varargs!"); |
| 198 | if (CallingConv == CallingConv::Fast && EnableFastCC) |
| 199 | return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG); |
| 200 | return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG); |
| 201 | } |
| 202 | |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 203 | SDOperand X86TargetLowering::LowerReturnTo(SDOperand Chain, SDOperand Op, |
| 204 | SelectionDAG &DAG) { |
| 205 | if (!X86DAGIsel) |
| 206 | return DAG.getNode(ISD::RET, MVT::Other, Chain, Op); |
| 207 | |
| 208 | SDOperand Copy; |
| 209 | MVT::ValueType OpVT = Op.getValueType(); |
| 210 | switch (OpVT) { |
| 211 | default: assert(0 && "Unknown type to return!"); |
| 212 | case MVT::i32: |
| 213 | Copy = DAG.getCopyToReg(Chain, X86::EAX, Op, SDOperand()); |
| 214 | break; |
| 215 | case MVT::i64: { |
| 216 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op, |
| 217 | DAG.getConstant(1, MVT::i32)); |
| 218 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op, |
| 219 | DAG.getConstant(0, MVT::i32)); |
| 220 | Copy = DAG.getCopyToReg(Chain, X86::EAX, Hi, SDOperand()); |
| 221 | Copy = DAG.getCopyToReg(Copy, X86::EDX, Lo, Copy.getValue(1)); |
| 222 | break; |
| 223 | } |
| 224 | case MVT::f32: |
| 225 | assert(X86ScalarSSE && "MVT::f32 only legal with scalar sse fp"); |
| 226 | // Fallthrough intended |
| 227 | case MVT::f64: |
| 228 | if (!X86ScalarSSE) { |
| 229 | std::vector<MVT::ValueType> Tys; |
| 230 | Tys.push_back(MVT::Other); |
| 231 | Tys.push_back(MVT::Flag); |
| 232 | std::vector<SDOperand> Ops; |
| 233 | Ops.push_back(Chain); |
| 234 | Ops.push_back(Op); |
| 235 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops); |
| 236 | } else { |
| 237 | // Spill the value to memory and reload it into top of stack. |
| 238 | unsigned Size = MVT::getSizeInBits(OpVT)/8; |
| 239 | MachineFunction &MF = DAG.getMachineFunction(); |
| 240 | int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size); |
| 241 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 242 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Op, |
| 243 | StackSlot, DAG.getSrcValue(NULL)); |
| 244 | std::vector<MVT::ValueType> Tys; |
| 245 | Tys.push_back(MVT::f64); |
| 246 | Tys.push_back(MVT::Other); |
| 247 | std::vector<SDOperand> Ops; |
| 248 | Ops.push_back(Chain); |
| 249 | Ops.push_back(StackSlot); |
| 250 | Ops.push_back(DAG.getValueType(OpVT)); |
| 251 | Copy = DAG.getNode(X86ISD::FLD, Tys, Ops); |
| 252 | Tys.clear(); |
| 253 | Tys.push_back(MVT::Other); |
| 254 | Tys.push_back(MVT::Flag); |
| 255 | Ops.clear(); |
| 256 | Ops.push_back(Copy.getValue(1)); |
| 257 | Ops.push_back(Copy); |
| 258 | Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops); |
| 259 | } |
| 260 | break; |
| 261 | } |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 262 | |
| 263 | return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, |
| 264 | Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16), |
| 265 | Copy.getValue(1)); |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 268 | //===----------------------------------------------------------------------===// |
| 269 | // C Calling Convention implementation |
| 270 | //===----------------------------------------------------------------------===// |
| 271 | |
| 272 | std::vector<SDOperand> |
| 273 | X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) { |
| 274 | std::vector<SDOperand> ArgValues; |
| 275 | |
| 276 | MachineFunction &MF = DAG.getMachineFunction(); |
| 277 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 278 | |
| 279 | // Add DAG nodes to load the arguments... On entry to a function on the X86, |
| 280 | // the stack frame looks like this: |
| 281 | // |
| 282 | // [ESP] -- return address |
| 283 | // [ESP + 4] -- first argument (leftmost lexically) |
| 284 | // [ESP + 8] -- second argument, if first argument is four bytes in size |
| 285 | // ... |
| 286 | // |
| 287 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 288 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 289 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 290 | unsigned ArgIncrement = 4; |
| 291 | unsigned ObjSize; |
| 292 | switch (ObjectVT) { |
| 293 | default: assert(0 && "Unhandled argument type!"); |
| 294 | case MVT::i1: |
| 295 | case MVT::i8: ObjSize = 1; break; |
| 296 | case MVT::i16: ObjSize = 2; break; |
| 297 | case MVT::i32: ObjSize = 4; break; |
| 298 | case MVT::i64: ObjSize = ArgIncrement = 8; break; |
| 299 | case MVT::f32: ObjSize = 4; break; |
| 300 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 301 | } |
| 302 | // Create the frame index object for this incoming parameter... |
| 303 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 304 | |
| 305 | // Create the SelectionDAG nodes corresponding to a load from this parameter |
| 306 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 307 | |
| 308 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 309 | // dead loads. |
| 310 | SDOperand ArgValue; |
| 311 | if (!I->use_empty()) |
| 312 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 313 | DAG.getSrcValue(NULL)); |
| 314 | else { |
| 315 | if (MVT::isInteger(ObjectVT)) |
| 316 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 317 | else |
| 318 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 319 | } |
| 320 | ArgValues.push_back(ArgValue); |
| 321 | |
| 322 | ArgOffset += ArgIncrement; // Move on to the next argument... |
| 323 | } |
| 324 | |
| 325 | // If the function takes variable number of arguments, make a frame index for |
| 326 | // the start of the first vararg value... for expansion of llvm.va_start. |
| 327 | if (F.isVarArg()) |
| 328 | VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset); |
| 329 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 330 | BytesToPopOnReturn = 0; // Callee pops nothing. |
| 331 | BytesCallerReserves = ArgOffset; |
| 332 | |
| 333 | // Finally, inform the code generator which regs we return values in. |
| 334 | switch (getValueType(F.getReturnType())) { |
| 335 | default: assert(0 && "Unknown type!"); |
| 336 | case MVT::isVoid: break; |
| 337 | case MVT::i1: |
| 338 | case MVT::i8: |
| 339 | case MVT::i16: |
| 340 | case MVT::i32: |
| 341 | MF.addLiveOut(X86::EAX); |
| 342 | break; |
| 343 | case MVT::i64: |
| 344 | MF.addLiveOut(X86::EAX); |
| 345 | MF.addLiveOut(X86::EDX); |
| 346 | break; |
| 347 | case MVT::f32: |
| 348 | case MVT::f64: |
| 349 | MF.addLiveOut(X86::ST0); |
| 350 | break; |
| 351 | } |
| 352 | return ArgValues; |
| 353 | } |
| 354 | |
| 355 | std::pair<SDOperand, SDOperand> |
| 356 | X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy, |
| 357 | bool isVarArg, bool isTailCall, |
| 358 | SDOperand Callee, ArgListTy &Args, |
| 359 | SelectionDAG &DAG) { |
| 360 | // Count how many bytes are to be pushed on the stack. |
| 361 | unsigned NumBytes = 0; |
| 362 | |
| 363 | if (Args.empty()) { |
| 364 | // Save zero bytes. |
| 365 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
| 366 | DAG.getConstant(0, getPointerTy())); |
| 367 | } else { |
| 368 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 369 | switch (getValueType(Args[i].second)) { |
| 370 | default: assert(0 && "Unknown value type!"); |
| 371 | case MVT::i1: |
| 372 | case MVT::i8: |
| 373 | case MVT::i16: |
| 374 | case MVT::i32: |
| 375 | case MVT::f32: |
| 376 | NumBytes += 4; |
| 377 | break; |
| 378 | case MVT::i64: |
| 379 | case MVT::f64: |
| 380 | NumBytes += 8; |
| 381 | break; |
| 382 | } |
| 383 | |
| 384 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
| 385 | DAG.getConstant(NumBytes, getPointerTy())); |
| 386 | |
| 387 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 388 | unsigned ArgOffset = 0; |
| 389 | SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(), |
| 390 | X86::ESP, MVT::i32); |
| 391 | std::vector<SDOperand> Stores; |
| 392 | |
| 393 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 394 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 395 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 396 | |
| 397 | switch (getValueType(Args[i].second)) { |
| 398 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 399 | case MVT::i1: |
| 400 | case MVT::i8: |
| 401 | case MVT::i16: |
| 402 | // Promote the integer to 32 bits. If the input type is signed use a |
| 403 | // sign extend, otherwise use a zero extend. |
| 404 | if (Args[i].second->isSigned()) |
| 405 | Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first); |
| 406 | else |
| 407 | Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first); |
| 408 | |
| 409 | // FALL THROUGH |
| 410 | case MVT::i32: |
| 411 | case MVT::f32: |
| 412 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 413 | Args[i].first, PtrOff, |
| 414 | DAG.getSrcValue(NULL))); |
| 415 | ArgOffset += 4; |
| 416 | break; |
| 417 | case MVT::i64: |
| 418 | case MVT::f64: |
| 419 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 420 | Args[i].first, PtrOff, |
| 421 | DAG.getSrcValue(NULL))); |
| 422 | ArgOffset += 8; |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 427 | } |
| 428 | |
| 429 | std::vector<MVT::ValueType> RetVals; |
| 430 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 431 | RetVals.push_back(MVT::Other); |
| 432 | |
| 433 | // The result values produced have to be legal. Promote the result. |
| 434 | switch (RetTyVT) { |
| 435 | case MVT::isVoid: break; |
| 436 | default: |
| 437 | RetVals.push_back(RetTyVT); |
| 438 | break; |
| 439 | case MVT::i1: |
| 440 | case MVT::i8: |
| 441 | case MVT::i16: |
| 442 | RetVals.push_back(MVT::i32); |
| 443 | break; |
| 444 | case MVT::f32: |
| 445 | if (X86ScalarSSE) |
| 446 | RetVals.push_back(MVT::f32); |
| 447 | else |
| 448 | RetVals.push_back(MVT::f64); |
| 449 | break; |
| 450 | case MVT::i64: |
| 451 | RetVals.push_back(MVT::i32); |
| 452 | RetVals.push_back(MVT::i32); |
| 453 | break; |
| 454 | } |
| 455 | std::vector<SDOperand> Ops; |
| 456 | Ops.push_back(Chain); |
| 457 | Ops.push_back(Callee); |
| 458 | Ops.push_back(DAG.getConstant(NumBytes, getPointerTy())); |
| 459 | Ops.push_back(DAG.getConstant(0, getPointerTy())); |
| 460 | SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 461 | RetVals, Ops); |
| 462 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall); |
| 463 | |
| 464 | SDOperand ResultVal; |
| 465 | switch (RetTyVT) { |
| 466 | case MVT::isVoid: break; |
| 467 | default: |
| 468 | ResultVal = TheCall.getValue(1); |
| 469 | break; |
| 470 | case MVT::i1: |
| 471 | case MVT::i8: |
| 472 | case MVT::i16: |
| 473 | ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1)); |
| 474 | break; |
| 475 | case MVT::f32: |
| 476 | // FIXME: we would really like to remember that this FP_ROUND operation is |
| 477 | // okay to eliminate if we allow excess FP precision. |
| 478 | ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1)); |
| 479 | break; |
| 480 | case MVT::i64: |
| 481 | ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1), |
| 482 | TheCall.getValue(2)); |
| 483 | break; |
| 484 | } |
| 485 | |
| 486 | return std::make_pair(ResultVal, Chain); |
| 487 | } |
| 488 | |
| 489 | SDOperand |
| 490 | X86TargetLowering::LowerVAStart(SDOperand Chain, SDOperand VAListP, |
| 491 | Value *VAListV, SelectionDAG &DAG) { |
| 492 | // vastart just stores the address of the VarArgsFrameIndex slot. |
| 493 | SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32); |
| 494 | return DAG.getNode(ISD::STORE, MVT::Other, Chain, FR, VAListP, |
| 495 | DAG.getSrcValue(VAListV)); |
| 496 | } |
| 497 | |
| 498 | |
| 499 | std::pair<SDOperand,SDOperand> |
| 500 | X86TargetLowering::LowerVAArg(SDOperand Chain, SDOperand VAListP, |
| 501 | Value *VAListV, const Type *ArgTy, |
| 502 | SelectionDAG &DAG) { |
| 503 | MVT::ValueType ArgVT = getValueType(ArgTy); |
| 504 | SDOperand Val = DAG.getLoad(MVT::i32, Chain, |
| 505 | VAListP, DAG.getSrcValue(VAListV)); |
| 506 | SDOperand Result = DAG.getLoad(ArgVT, Chain, Val, |
| 507 | DAG.getSrcValue(NULL)); |
| 508 | unsigned Amt; |
| 509 | if (ArgVT == MVT::i32) |
| 510 | Amt = 4; |
| 511 | else { |
| 512 | assert((ArgVT == MVT::i64 || ArgVT == MVT::f64) && |
| 513 | "Other types should have been promoted for varargs!"); |
| 514 | Amt = 8; |
| 515 | } |
| 516 | Val = DAG.getNode(ISD::ADD, Val.getValueType(), Val, |
| 517 | DAG.getConstant(Amt, Val.getValueType())); |
| 518 | Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 519 | Val, VAListP, DAG.getSrcValue(VAListV)); |
| 520 | return std::make_pair(Result, Chain); |
| 521 | } |
| 522 | |
| 523 | //===----------------------------------------------------------------------===// |
| 524 | // Fast Calling Convention implementation |
| 525 | //===----------------------------------------------------------------------===// |
| 526 | // |
| 527 | // The X86 'fast' calling convention passes up to two integer arguments in |
| 528 | // registers (an appropriate portion of EAX/EDX), passes arguments in C order, |
| 529 | // and requires that the callee pop its arguments off the stack (allowing proper |
| 530 | // tail calls), and has the same return value conventions as C calling convs. |
| 531 | // |
| 532 | // This calling convention always arranges for the callee pop value to be 8n+4 |
| 533 | // bytes, which is needed for tail recursion elimination and stack alignment |
| 534 | // reasons. |
| 535 | // |
| 536 | // Note that this can be enhanced in the future to pass fp vals in registers |
| 537 | // (when we have a global fp allocator) and do other tricks. |
| 538 | // |
| 539 | |
| 540 | /// AddLiveIn - This helper function adds the specified physical register to the |
| 541 | /// MachineFunction as a live in value. It also creates a corresponding virtual |
| 542 | /// register for it. |
| 543 | static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg, |
| 544 | TargetRegisterClass *RC) { |
| 545 | assert(RC->contains(PReg) && "Not the correct regclass!"); |
| 546 | unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC); |
| 547 | MF.addLiveIn(PReg, VReg); |
| 548 | return VReg; |
| 549 | } |
| 550 | |
| 551 | |
| 552 | std::vector<SDOperand> |
| 553 | X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) { |
| 554 | std::vector<SDOperand> ArgValues; |
| 555 | |
| 556 | MachineFunction &MF = DAG.getMachineFunction(); |
| 557 | MachineFrameInfo *MFI = MF.getFrameInfo(); |
| 558 | |
| 559 | // Add DAG nodes to load the arguments... On entry to a function the stack |
| 560 | // frame looks like this: |
| 561 | // |
| 562 | // [ESP] -- return address |
| 563 | // [ESP + 4] -- first nonreg argument (leftmost lexically) |
| 564 | // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size |
| 565 | // ... |
| 566 | unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot |
| 567 | |
| 568 | // Keep track of the number of integer regs passed so far. This can be either |
| 569 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 570 | // used). |
| 571 | unsigned NumIntRegs = 0; |
| 572 | |
| 573 | for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) { |
| 574 | MVT::ValueType ObjectVT = getValueType(I->getType()); |
| 575 | unsigned ArgIncrement = 4; |
| 576 | unsigned ObjSize = 0; |
| 577 | SDOperand ArgValue; |
| 578 | |
| 579 | switch (ObjectVT) { |
| 580 | default: assert(0 && "Unhandled argument type!"); |
| 581 | case MVT::i1: |
| 582 | case MVT::i8: |
| 583 | if (NumIntRegs < 2) { |
| 584 | if (!I->use_empty()) { |
| 585 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL, |
| 586 | X86::R8RegisterClass); |
| 587 | ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8); |
| 588 | DAG.setRoot(ArgValue.getValue(1)); |
| 589 | } |
| 590 | ++NumIntRegs; |
| 591 | break; |
| 592 | } |
| 593 | |
| 594 | ObjSize = 1; |
| 595 | break; |
| 596 | case MVT::i16: |
| 597 | if (NumIntRegs < 2) { |
| 598 | if (!I->use_empty()) { |
| 599 | unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX, |
| 600 | X86::R16RegisterClass); |
| 601 | ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16); |
| 602 | DAG.setRoot(ArgValue.getValue(1)); |
| 603 | } |
| 604 | ++NumIntRegs; |
| 605 | break; |
| 606 | } |
| 607 | ObjSize = 2; |
| 608 | break; |
| 609 | case MVT::i32: |
| 610 | if (NumIntRegs < 2) { |
| 611 | if (!I->use_empty()) { |
| 612 | unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX, |
| 613 | X86::R32RegisterClass); |
| 614 | ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32); |
| 615 | DAG.setRoot(ArgValue.getValue(1)); |
| 616 | } |
| 617 | ++NumIntRegs; |
| 618 | break; |
| 619 | } |
| 620 | ObjSize = 4; |
| 621 | break; |
| 622 | case MVT::i64: |
| 623 | if (NumIntRegs == 0) { |
| 624 | if (!I->use_empty()) { |
| 625 | unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass); |
| 626 | unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
| 627 | |
| 628 | SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32); |
| 629 | SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32); |
| 630 | DAG.setRoot(Hi.getValue(1)); |
| 631 | |
| 632 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 633 | } |
| 634 | NumIntRegs = 2; |
| 635 | break; |
| 636 | } else if (NumIntRegs == 1) { |
| 637 | if (!I->use_empty()) { |
| 638 | unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass); |
| 639 | SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32); |
| 640 | DAG.setRoot(Low.getValue(1)); |
| 641 | |
| 642 | // Load the high part from memory. |
| 643 | // Create the frame index object for this incoming parameter... |
| 644 | int FI = MFI->CreateFixedObject(4, ArgOffset); |
| 645 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 646 | SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN, |
| 647 | DAG.getSrcValue(NULL)); |
| 648 | ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi); |
| 649 | } |
| 650 | ArgOffset += 4; |
| 651 | NumIntRegs = 2; |
| 652 | break; |
| 653 | } |
| 654 | ObjSize = ArgIncrement = 8; |
| 655 | break; |
| 656 | case MVT::f32: ObjSize = 4; break; |
| 657 | case MVT::f64: ObjSize = ArgIncrement = 8; break; |
| 658 | } |
| 659 | |
| 660 | // Don't codegen dead arguments. FIXME: remove this check when we can nuke |
| 661 | // dead loads. |
| 662 | if (ObjSize && !I->use_empty()) { |
| 663 | // Create the frame index object for this incoming parameter... |
| 664 | int FI = MFI->CreateFixedObject(ObjSize, ArgOffset); |
| 665 | |
| 666 | // Create the SelectionDAG nodes corresponding to a load from this |
| 667 | // parameter. |
| 668 | SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32); |
| 669 | |
| 670 | ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN, |
| 671 | DAG.getSrcValue(NULL)); |
| 672 | } else if (ArgValue.Val == 0) { |
| 673 | if (MVT::isInteger(ObjectVT)) |
| 674 | ArgValue = DAG.getConstant(0, ObjectVT); |
| 675 | else |
| 676 | ArgValue = DAG.getConstantFP(0, ObjectVT); |
| 677 | } |
| 678 | ArgValues.push_back(ArgValue); |
| 679 | |
| 680 | if (ObjSize) |
| 681 | ArgOffset += ArgIncrement; // Move on to the next argument. |
| 682 | } |
| 683 | |
| 684 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 685 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 686 | if ((ArgOffset & 7) == 0) |
| 687 | ArgOffset += 4; |
| 688 | |
| 689 | VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs. |
| 690 | ReturnAddrIndex = 0; // No return address slot generated yet. |
| 691 | BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments. |
| 692 | BytesCallerReserves = 0; |
| 693 | |
| 694 | // Finally, inform the code generator which regs we return values in. |
| 695 | switch (getValueType(F.getReturnType())) { |
| 696 | default: assert(0 && "Unknown type!"); |
| 697 | case MVT::isVoid: break; |
| 698 | case MVT::i1: |
| 699 | case MVT::i8: |
| 700 | case MVT::i16: |
| 701 | case MVT::i32: |
| 702 | MF.addLiveOut(X86::EAX); |
| 703 | break; |
| 704 | case MVT::i64: |
| 705 | MF.addLiveOut(X86::EAX); |
| 706 | MF.addLiveOut(X86::EDX); |
| 707 | break; |
| 708 | case MVT::f32: |
| 709 | case MVT::f64: |
| 710 | MF.addLiveOut(X86::ST0); |
| 711 | break; |
| 712 | } |
| 713 | return ArgValues; |
| 714 | } |
| 715 | |
| 716 | std::pair<SDOperand, SDOperand> |
| 717 | X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy, |
| 718 | bool isTailCall, SDOperand Callee, |
| 719 | ArgListTy &Args, SelectionDAG &DAG) { |
| 720 | // Count how many bytes are to be pushed on the stack. |
| 721 | unsigned NumBytes = 0; |
| 722 | |
| 723 | // Keep track of the number of integer regs passed so far. This can be either |
| 724 | // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both |
| 725 | // used). |
| 726 | unsigned NumIntRegs = 0; |
| 727 | |
| 728 | for (unsigned i = 0, e = Args.size(); i != e; ++i) |
| 729 | switch (getValueType(Args[i].second)) { |
| 730 | default: assert(0 && "Unknown value type!"); |
| 731 | case MVT::i1: |
| 732 | case MVT::i8: |
| 733 | case MVT::i16: |
| 734 | case MVT::i32: |
| 735 | if (NumIntRegs < 2) { |
| 736 | ++NumIntRegs; |
| 737 | break; |
| 738 | } |
| 739 | // fall through |
| 740 | case MVT::f32: |
| 741 | NumBytes += 4; |
| 742 | break; |
| 743 | case MVT::i64: |
| 744 | if (NumIntRegs == 0) { |
| 745 | NumIntRegs = 2; |
| 746 | break; |
| 747 | } else if (NumIntRegs == 1) { |
| 748 | NumIntRegs = 2; |
| 749 | NumBytes += 4; |
| 750 | break; |
| 751 | } |
| 752 | |
| 753 | // fall through |
| 754 | case MVT::f64: |
| 755 | NumBytes += 8; |
| 756 | break; |
| 757 | } |
| 758 | |
| 759 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 760 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 761 | if ((NumBytes & 7) == 0) |
| 762 | NumBytes += 4; |
| 763 | |
| 764 | Chain = DAG.getNode(ISD::CALLSEQ_START, MVT::Other, Chain, |
| 765 | DAG.getConstant(NumBytes, getPointerTy())); |
| 766 | |
| 767 | // Arguments go on the stack in reverse order, as specified by the ABI. |
| 768 | unsigned ArgOffset = 0; |
| 769 | SDOperand StackPtr = DAG.getCopyFromReg(DAG.getEntryNode(), |
| 770 | X86::ESP, MVT::i32); |
| 771 | NumIntRegs = 0; |
| 772 | std::vector<SDOperand> Stores; |
| 773 | std::vector<SDOperand> RegValuesToPass; |
| 774 | for (unsigned i = 0, e = Args.size(); i != e; ++i) { |
| 775 | switch (getValueType(Args[i].second)) { |
| 776 | default: assert(0 && "Unexpected ValueType for argument!"); |
| 777 | case MVT::i1: |
| 778 | case MVT::i8: |
| 779 | case MVT::i16: |
| 780 | case MVT::i32: |
| 781 | if (NumIntRegs < 2) { |
| 782 | RegValuesToPass.push_back(Args[i].first); |
| 783 | ++NumIntRegs; |
| 784 | break; |
| 785 | } |
| 786 | // Fall through |
| 787 | case MVT::f32: { |
| 788 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 789 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 790 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 791 | Args[i].first, PtrOff, |
| 792 | DAG.getSrcValue(NULL))); |
| 793 | ArgOffset += 4; |
| 794 | break; |
| 795 | } |
| 796 | case MVT::i64: |
| 797 | if (NumIntRegs < 2) { // Can pass part of it in regs? |
| 798 | SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 799 | Args[i].first, DAG.getConstant(1, MVT::i32)); |
| 800 | SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, |
| 801 | Args[i].first, DAG.getConstant(0, MVT::i32)); |
| 802 | RegValuesToPass.push_back(Lo); |
| 803 | ++NumIntRegs; |
| 804 | if (NumIntRegs < 2) { // Pass both parts in regs? |
| 805 | RegValuesToPass.push_back(Hi); |
| 806 | ++NumIntRegs; |
| 807 | } else { |
| 808 | // Pass the high part in memory. |
| 809 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 810 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 811 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 812 | Hi, PtrOff, DAG.getSrcValue(NULL))); |
| 813 | ArgOffset += 4; |
| 814 | } |
| 815 | break; |
| 816 | } |
| 817 | // Fall through |
| 818 | case MVT::f64: |
| 819 | SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy()); |
| 820 | PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff); |
| 821 | Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain, |
| 822 | Args[i].first, PtrOff, |
| 823 | DAG.getSrcValue(NULL))); |
| 824 | ArgOffset += 8; |
| 825 | break; |
| 826 | } |
| 827 | } |
| 828 | if (!Stores.empty()) |
| 829 | Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores); |
| 830 | |
| 831 | // Make sure the instruction takes 8n+4 bytes to make sure the start of the |
| 832 | // arguments and the arguments after the retaddr has been pushed are aligned. |
| 833 | if ((ArgOffset & 7) == 0) |
| 834 | ArgOffset += 4; |
| 835 | |
| 836 | std::vector<MVT::ValueType> RetVals; |
| 837 | MVT::ValueType RetTyVT = getValueType(RetTy); |
| 838 | |
| 839 | RetVals.push_back(MVT::Other); |
| 840 | |
| 841 | // The result values produced have to be legal. Promote the result. |
| 842 | switch (RetTyVT) { |
| 843 | case MVT::isVoid: break; |
| 844 | default: |
| 845 | RetVals.push_back(RetTyVT); |
| 846 | break; |
| 847 | case MVT::i1: |
| 848 | case MVT::i8: |
| 849 | case MVT::i16: |
| 850 | RetVals.push_back(MVT::i32); |
| 851 | break; |
| 852 | case MVT::f32: |
| 853 | if (X86ScalarSSE) |
| 854 | RetVals.push_back(MVT::f32); |
| 855 | else |
| 856 | RetVals.push_back(MVT::f64); |
| 857 | break; |
| 858 | case MVT::i64: |
| 859 | RetVals.push_back(MVT::i32); |
| 860 | RetVals.push_back(MVT::i32); |
| 861 | break; |
| 862 | } |
| 863 | |
| 864 | std::vector<SDOperand> Ops; |
| 865 | Ops.push_back(Chain); |
| 866 | Ops.push_back(Callee); |
| 867 | Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy())); |
| 868 | // Callee pops all arg values on the stack. |
| 869 | Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy())); |
| 870 | |
| 871 | // Pass register arguments as needed. |
| 872 | Ops.insert(Ops.end(), RegValuesToPass.begin(), RegValuesToPass.end()); |
| 873 | |
| 874 | SDOperand TheCall = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL, |
| 875 | RetVals, Ops); |
| 876 | Chain = DAG.getNode(ISD::CALLSEQ_END, MVT::Other, TheCall); |
| 877 | |
| 878 | SDOperand ResultVal; |
| 879 | switch (RetTyVT) { |
| 880 | case MVT::isVoid: break; |
| 881 | default: |
| 882 | ResultVal = TheCall.getValue(1); |
| 883 | break; |
| 884 | case MVT::i1: |
| 885 | case MVT::i8: |
| 886 | case MVT::i16: |
| 887 | ResultVal = DAG.getNode(ISD::TRUNCATE, RetTyVT, TheCall.getValue(1)); |
| 888 | break; |
| 889 | case MVT::f32: |
| 890 | // FIXME: we would really like to remember that this FP_ROUND operation is |
| 891 | // okay to eliminate if we allow excess FP precision. |
| 892 | ResultVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, TheCall.getValue(1)); |
| 893 | break; |
| 894 | case MVT::i64: |
| 895 | ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, TheCall.getValue(1), |
| 896 | TheCall.getValue(2)); |
| 897 | break; |
| 898 | } |
| 899 | |
| 900 | return std::make_pair(ResultVal, Chain); |
| 901 | } |
| 902 | |
| 903 | SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) { |
| 904 | if (ReturnAddrIndex == 0) { |
| 905 | // Set up a frame object for the return address. |
| 906 | MachineFunction &MF = DAG.getMachineFunction(); |
| 907 | ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4); |
| 908 | } |
| 909 | |
| 910 | return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32); |
| 911 | } |
| 912 | |
| 913 | |
| 914 | |
| 915 | std::pair<SDOperand, SDOperand> X86TargetLowering:: |
| 916 | LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth, |
| 917 | SelectionDAG &DAG) { |
| 918 | SDOperand Result; |
| 919 | if (Depth) // Depths > 0 not supported yet! |
| 920 | Result = DAG.getConstant(0, getPointerTy()); |
| 921 | else { |
| 922 | SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG); |
| 923 | if (!isFrameAddress) |
| 924 | // Just load the return address |
| 925 | Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI, |
| 926 | DAG.getSrcValue(NULL)); |
| 927 | else |
| 928 | Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI, |
| 929 | DAG.getConstant(4, MVT::i32)); |
| 930 | } |
| 931 | return std::make_pair(Result, Chain); |
| 932 | } |
| 933 | |
| 934 | //===----------------------------------------------------------------------===// |
| 935 | // X86 Custom Lowering Hooks |
| 936 | //===----------------------------------------------------------------------===// |
| 937 | |
| 938 | /// LowerOperation - Provide custom lowering hooks for some operations. |
| 939 | /// |
| 940 | SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) { |
| 941 | switch (Op.getOpcode()) { |
| 942 | default: assert(0 && "Should not custom lower this!"); |
| 943 | case ISD::SINT_TO_FP: { |
| 944 | assert(Op.getValueType() == MVT::f64 && |
| 945 | Op.getOperand(0).getValueType() == MVT::i64 && |
| 946 | "Unknown SINT_TO_FP to lower!"); |
| 947 | // We lower sint64->FP into a store to a temporary stack slot, followed by a |
| 948 | // FILD64m node. |
| 949 | MachineFunction &MF = DAG.getMachineFunction(); |
| 950 | int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8); |
| 951 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 952 | SDOperand Store = DAG.getNode(ISD::STORE, MVT::Other, DAG.getEntryNode(), |
| 953 | Op.getOperand(0), StackSlot, DAG.getSrcValue(NULL)); |
| 954 | std::vector<MVT::ValueType> RTs; |
| 955 | RTs.push_back(MVT::f64); |
| 956 | RTs.push_back(MVT::Other); |
| 957 | std::vector<SDOperand> Ops; |
| 958 | Ops.push_back(Store); |
| 959 | Ops.push_back(StackSlot); |
| 960 | return DAG.getNode(X86ISD::FILD64m, RTs, Ops); |
| 961 | } |
| 962 | case ISD::FP_TO_SINT: { |
| 963 | assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 && |
| 964 | Op.getOperand(0).getValueType() == MVT::f64 && |
| 965 | "Unknown FP_TO_SINT to lower!"); |
| 966 | // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary |
| 967 | // stack slot. |
| 968 | MachineFunction &MF = DAG.getMachineFunction(); |
| 969 | unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8; |
| 970 | int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize); |
| 971 | SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy()); |
| 972 | |
| 973 | unsigned Opc; |
| 974 | switch (Op.getValueType()) { |
| 975 | default: assert(0 && "Invalid FP_TO_SINT to lower!"); |
| 976 | case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break; |
| 977 | case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break; |
| 978 | case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break; |
| 979 | } |
| 980 | |
| 981 | // Build the FP_TO_INT*_IN_MEM |
| 982 | std::vector<SDOperand> Ops; |
| 983 | Ops.push_back(DAG.getEntryNode()); |
| 984 | Ops.push_back(Op.getOperand(0)); |
| 985 | Ops.push_back(StackSlot); |
| 986 | SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops); |
| 987 | |
| 988 | // Load the result. |
| 989 | return DAG.getLoad(Op.getValueType(), FIST, StackSlot, |
| 990 | DAG.getSrcValue(NULL)); |
| 991 | } |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 992 | case ISD::READCYCLECOUNTER: { |
Chris Lattner | 81363c3 | 2005-11-20 22:01:40 +0000 | [diff] [blame] | 993 | std::vector<MVT::ValueType> Tys; |
| 994 | Tys.push_back(MVT::Other); |
| 995 | Tys.push_back(MVT::Flag); |
| 996 | std::vector<SDOperand> Ops; |
| 997 | Ops.push_back(Op.getOperand(0)); |
| 998 | SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops); |
Chris Lattner | 81f803d | 2005-11-20 22:57:19 +0000 | [diff] [blame] | 999 | Ops.clear(); |
| 1000 | Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1))); |
| 1001 | Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX, |
| 1002 | MVT::i32, Ops[0].getValue(2))); |
| 1003 | Ops.push_back(Ops[1].getValue(1)); |
| 1004 | Tys[0] = Tys[1] = MVT::i32; |
| 1005 | Tys.push_back(MVT::Other); |
| 1006 | return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops); |
Andrew Lenharth | b873ff3 | 2005-11-20 21:41:10 +0000 | [diff] [blame] | 1007 | } |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 1008 | case ISD::SETCC: { |
| 1009 | assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer"); |
| 1010 | SDOperand CC = Op.getOperand(2); |
| 1011 | SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 1012 | Op.getOperand(0), Op.getOperand(1)); |
| 1013 | return DAG.getNode(X86ISD::SETCC, MVT::i8, CC, Cond); |
| 1014 | } |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 1015 | case ISD::SELECT: { |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 1016 | SDOperand Cond = Op.getOperand(0); |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 1017 | SDOperand CC; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 1018 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 1019 | CC = Cond.getOperand(0); |
| 1020 | Cond = Cond.getOperand(1); |
| 1021 | } else if (Cond.getOpcode() == ISD::SETCC) { |
Evan Cheng | 7df96d6 | 2005-12-17 01:21:05 +0000 | [diff] [blame] | 1022 | CC = Cond.getOperand(2); |
| 1023 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 1024 | Cond.getOperand(0), Cond.getOperand(1)); |
| 1025 | } else { |
| 1026 | CC = DAG.getCondCode(ISD::SETEQ); |
| 1027 | Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond); |
| 1028 | } |
| 1029 | return DAG.getNode(X86ISD::CMOV, Op.getValueType(), |
| 1030 | Op.getOperand(1), Op.getOperand(2), CC, Cond); |
| 1031 | } |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 1032 | case ISD::BRCOND: { |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 1033 | SDOperand Cond = Op.getOperand(1); |
| 1034 | SDOperand Dest = Op.getOperand(2); |
| 1035 | SDOperand CC; |
| 1036 | // TODO: handle Cond == OR / AND / XOR |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 1037 | if (Cond.getOpcode() == X86ISD::SETCC) { |
| 1038 | CC = Cond.getOperand(0); |
| 1039 | Cond = Cond.getOperand(1); |
| 1040 | } else if (Cond.getOpcode() == ISD::SETCC) { |
Evan Cheng | 898101c | 2005-12-19 23:12:38 +0000 | [diff] [blame] | 1041 | CC = Cond.getOperand(2); |
| 1042 | Cond = DAG.getNode(X86ISD::CMP, MVT::Flag, |
| 1043 | Cond.getOperand(0), Cond.getOperand(1)); |
| 1044 | } else { |
| 1045 | CC = DAG.getCondCode(ISD::SETNE); |
| 1046 | Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond); |
| 1047 | } |
| 1048 | return DAG.getNode(X86ISD::BRCOND, Op.getValueType(), |
| 1049 | Op.getOperand(0), Op.getOperand(2), CC, Cond); |
| 1050 | } |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 1051 | case ISD::GlobalAddress: |
| 1052 | GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal(); |
| 1053 | // For Darwin, external and weak symbols are indirect, so we want to load |
| 1054 | // the value at address GV, not the value of GV itself. This means that |
| 1055 | // the GlobalAddress must be in the base or index register of the address, |
| 1056 | // not the GV offset field. |
| 1057 | if (getTargetMachine(). |
| 1058 | getSubtarget<X86Subtarget>().getIndirectExternAndWeakGlobals() && |
| 1059 | (GV->hasWeakLinkage() || GV->isExternal())) |
| 1060 | return DAG.getLoad(MVT::i32, DAG.getEntryNode(), Op, |
| 1061 | DAG.getSrcValue(NULL)); |
| 1062 | else |
| 1063 | return Op; |
| 1064 | break; |
Chris Lattner | dbdbf0c | 2005-11-15 00:40:23 +0000 | [diff] [blame] | 1065 | } |
| 1066 | } |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 1067 | |
| 1068 | const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const { |
| 1069 | switch (Opcode) { |
| 1070 | default: return NULL; |
| 1071 | case X86ISD::FILD64m: return "X86ISD::FILD64m"; |
| 1072 | case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM"; |
| 1073 | case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM"; |
| 1074 | case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 1075 | case X86ISD::FLD: return "X86ISD::FLD"; |
| 1076 | case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 1077 | case X86ISD::CALL: return "X86ISD::CALL"; |
| 1078 | case X86ISD::TAILCALL: return "X86ISD::TAILCALL"; |
| 1079 | case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG"; |
| 1080 | case X86ISD::CMP: return "X86ISD::CMP"; |
| 1081 | case X86ISD::TEST: return "X86ISD::TEST"; |
Evan Cheng | d5781fc | 2005-12-21 20:21:51 +0000 | [diff] [blame] | 1082 | case X86ISD::SETCC: return "X86ISD::SETCC"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 1083 | case X86ISD::CMOV: return "X86ISD::CMOV"; |
| 1084 | case X86ISD::BRCOND: return "X86ISD::BRCOND"; |
Evan Cheng | b077b84 | 2005-12-21 02:39:21 +0000 | [diff] [blame] | 1085 | case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG"; |
Evan Cheng | 7226158 | 2005-12-20 06:22:03 +0000 | [diff] [blame] | 1086 | } |
| 1087 | } |