blob: 801c22d62b535c09f55149aaa13940ebc33c98ff [file] [log] [blame]
Chris Lattnerc6d05672006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
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"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000023#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000024#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000025#include "llvm/ADT/VectorExtras.h"
26#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/SelectionDAG.h"
31#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000032#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000033#include "llvm/Target/TargetOptions.h"
Evan Chenge1113032006-10-04 18:33:38 +000034#include "llvm/Support/CommandLine.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000035#include "llvm/ADT/StringExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000036using namespace llvm;
37
38// FIXME: temporary.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
40 cl::desc("Enable fastcc on X86"));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000043 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
Evan Cheng25ab6902006-09-08 06:48:29 +000045 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Evan Cheng559806f2006-01-27 08:10:46 +000046
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000047 // Set up the TargetLowering object.
48
49 // X86 is weird, it always uses i8 for shift amounts and setcc results.
50 setShiftAmountType(MVT::i8);
51 setSetCCResultType(MVT::i8);
52 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000053 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000055 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000056
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000057 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000058 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000059 setUseUnderscoreSetJmp(false);
60 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000061 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000062 // MS runtime is weird: it exports _setjmp, but longjmp!
63 setUseUnderscoreSetJmp(true);
64 setUseUnderscoreLongJmp(false);
65 } else {
66 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(true);
68 }
69
Evan Cheng714554d2006-03-16 21:47:42 +000070 // Add legal addressing mode scale values.
71 addLegalAddressScale(8);
72 addLegalAddressScale(4);
73 addLegalAddressScale(2);
74 // Enter the ones which require both scale + index last. These are more
75 // expensive.
76 addLegalAddressScale(9);
77 addLegalAddressScale(5);
78 addLegalAddressScale(3);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +000079
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000081 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
Evan Chengc5484282006-10-04 00:56:09 +000087 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
88
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000089 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
90 // operation.
91 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
92 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
93 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000094
Evan Cheng25ab6902006-09-08 06:48:29 +000095 if (Subtarget->is64Bit()) {
96 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000097 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000098 } else {
99 if (X86ScalarSSE)
100 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
101 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
102 else
103 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
104 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105
106 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
107 // this operation.
108 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
109 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000110 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +0000111 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +0000112 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000113 else {
114 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
115 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
116 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000117
Evan Cheng25ab6902006-09-08 06:48:29 +0000118 if (!Subtarget->is64Bit()) {
119 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
120 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
121 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
122 }
Evan Cheng6dab0532006-01-30 08:02:57 +0000123
Evan Cheng02568ff2006-01-30 22:13:22 +0000124 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
125 // this operation.
126 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
127 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
128
129 if (X86ScalarSSE) {
130 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
131 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000132 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000133 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000134 }
135
136 // Handle FP_TO_UINT by promoting the destination to a larger signed
137 // conversion.
138 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
139 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
140 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
141
Evan Cheng25ab6902006-09-08 06:48:29 +0000142 if (Subtarget->is64Bit()) {
143 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000144 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000145 } else {
146 if (X86ScalarSSE && !Subtarget->hasSSE3())
147 // Expand FP_TO_UINT into a select.
148 // FIXME: We would like to use a Custom expander here eventually to do
149 // the optimal thing for SSE vs. the default expansion in the legalizer.
150 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
151 else
152 // With SSE3 we can use fisttpll to convert to a signed i64.
153 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
154 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155
Chris Lattner399610a2006-12-05 18:22:22 +0000156 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Chris Lattnerf3597a12006-12-05 18:45:06 +0000157 if (!X86ScalarSSE) {
158 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
159 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
160 }
Chris Lattner21f66852005-12-23 05:15:23 +0000161
Evan Chengc35497f2006-10-30 08:02:39 +0000162 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000163 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000164 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
165 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000167 if (Subtarget->is64Bit())
168 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000169 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000170 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
172 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000173 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000174
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000175 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
176 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
177 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
178 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
179 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
180 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
181 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
182 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
183 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000184 if (Subtarget->is64Bit()) {
185 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
186 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
187 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
188 }
189
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000190 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000191 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000192
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000193 // These should be promoted to a larger select which is supported.
194 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
195 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000196 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000197 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
198 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
199 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
200 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
201 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
202 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
203 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
204 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
205 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000206 if (Subtarget->is64Bit()) {
207 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
208 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
209 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000210 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000211 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000212 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000213 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000214 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000215 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000216 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000217 if (Subtarget->is64Bit()) {
218 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
219 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
220 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
221 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
222 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000223 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000224 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
225 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
226 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000227 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000228 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
229 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000230
Chris Lattnerf73bae12005-11-29 06:16:21 +0000231 // We don't have line number support yet.
232 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000233 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000234 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000235 if (!Subtarget->isTargetDarwin() &&
236 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000237 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000238 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000239
Nate Begemanacc398c2006-01-25 18:21:52 +0000240 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
241 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000242
Nate Begemanacc398c2006-01-25 18:21:52 +0000243 // Use the default implementation.
244 setOperationAction(ISD::VAARG , MVT::Other, Expand);
245 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
246 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000247 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000248 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit())
250 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000251 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000252
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000253 if (X86ScalarSSE) {
254 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000255 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
256 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000257
Evan Cheng223547a2006-01-31 22:28:30 +0000258 // Use ANDPD to simulate FABS.
259 setOperationAction(ISD::FABS , MVT::f64, Custom);
260 setOperationAction(ISD::FABS , MVT::f32, Custom);
261
262 // Use XORP to simulate FNEG.
263 setOperationAction(ISD::FNEG , MVT::f64, Custom);
264 setOperationAction(ISD::FNEG , MVT::f32, Custom);
265
Evan Cheng68c47cb2007-01-05 07:55:56 +0000266 // Use ANDPD and ORPD to simulate FCOPYSIGN.
267 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
268 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
269
Evan Chengd25e9e82006-02-02 00:28:23 +0000270 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000271 setOperationAction(ISD::FSIN , MVT::f64, Expand);
272 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000273 setOperationAction(ISD::FREM , MVT::f64, Expand);
274 setOperationAction(ISD::FSIN , MVT::f32, Expand);
275 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000276 setOperationAction(ISD::FREM , MVT::f32, Expand);
277
Chris Lattnera54aa942006-01-29 06:26:08 +0000278 // Expand FP immediates into loads from the stack, except for the special
279 // cases we handle.
280 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
281 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000282 addLegalFPImmediate(+0.0); // xorps / xorpd
283 } else {
284 // Set up the FP register classes.
285 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000286
Evan Cheng68c47cb2007-01-05 07:55:56 +0000287 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
288 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
289 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000290
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000291 if (!UnsafeFPMath) {
292 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
293 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
294 }
295
Chris Lattnera54aa942006-01-29 06:26:08 +0000296 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000297 addLegalFPImmediate(+0.0); // FLD0
298 addLegalFPImmediate(+1.0); // FLD1
299 addLegalFPImmediate(-0.0); // FLD0/FCHS
300 addLegalFPImmediate(-1.0); // FLD1/FCHS
301 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000302
Evan Chengd30bf012006-03-01 01:11:20 +0000303 // First set operation action for all vector types to expand. Then we
304 // will selectively turn on ones that can be effectively codegen'd.
305 for (unsigned VT = (unsigned)MVT::Vector + 1;
306 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
307 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
308 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000309 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
310 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000311 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000312 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
313 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
314 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
315 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
316 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
317 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000318 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000319 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000321 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000322 }
323
Evan Chenga88973f2006-03-22 19:22:18 +0000324 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000325 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
326 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
327 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
328
Evan Chengd30bf012006-03-01 01:11:20 +0000329 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000330 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
331 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
332 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000333 }
334
Evan Chenga88973f2006-03-22 19:22:18 +0000335 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000336 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
337
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000338 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
339 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
340 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
341 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000342 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
343 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
344 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000345 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000346 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000347 }
348
Evan Chenga88973f2006-03-22 19:22:18 +0000349 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000350 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
351 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
352 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
353 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
354 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
355
Evan Chengf7c378e2006-04-10 07:23:14 +0000356 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
357 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
358 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000359 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
360 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
361 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000362 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000363 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
364 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
365 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
366 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000367
Evan Chengf7c378e2006-04-10 07:23:14 +0000368 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
369 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000370 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000371 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
372 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
373 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000374
Evan Cheng2c3ae372006-04-12 21:21:57 +0000375 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
376 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
377 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
378 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
379 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
380 }
381 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
382 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
383 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
384 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
385 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
386 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
387
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000388 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000389 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
390 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
391 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
392 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
393 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
394 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
395 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000396 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
397 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000398 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
399 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000400 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000401
402 // Custom lower v2i64 and v2f64 selects.
403 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000404 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000405 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000406 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000407 }
408
Evan Cheng6be2c582006-04-05 23:38:46 +0000409 // We want to custom lower some of our intrinsics.
410 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
411
Evan Cheng206ee9d2006-07-07 08:33:52 +0000412 // We have target-specific dag combine patterns for the following nodes:
413 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000414 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000415
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000416 computeRegisterProperties();
417
Evan Cheng87ed7162006-02-14 08:25:08 +0000418 // FIXME: These should be based on subtarget info. Plus, the values should
419 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000420 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
421 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
422 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000423 allowUnalignedMemoryAccesses = true; // x86 supports it!
424}
425
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000426//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000427// C & StdCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000428//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000429// StdCall calling convention seems to be standard for many Windows' API
430// routines and around. It differs from C calling convention just a little:
431// callee should clean up the stack, not caller. Symbols should be also
432// decorated in some fancy way :) It doesn't support any vector arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000433
Evan Cheng85e38002006-04-27 05:35:28 +0000434/// AddLiveIn - This helper function adds the specified physical register to the
435/// MachineFunction as a live in value. It also creates a corresponding virtual
436/// register for it.
437static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000438 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000439 assert(RC->contains(PReg) && "Not the correct regclass!");
440 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
441 MF.addLiveIn(PReg, VReg);
442 return VReg;
443}
444
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000445/// HowToPassArgument - Returns how an formal argument of the specified type
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000446/// should be passed. If it is through stack, returns the size of the stack
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000447/// slot; if it is through integer or XMM register, returns the number of
448/// integer or XMM registers are needed.
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000449static void
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000450HowToPassCallArgument(MVT::ValueType ObjectVT,
451 bool ArgInReg,
452 unsigned NumIntRegs, unsigned NumXMMRegs,
453 unsigned MaxNumIntRegs,
454 unsigned &ObjSize, unsigned &ObjIntRegs,
455 unsigned &ObjXMMRegs,
456 bool AllowVectors = true) {
457 ObjSize = 0;
458 ObjIntRegs = 0;
Evan Cheng26755342006-06-01 05:53:27 +0000459 ObjXMMRegs = 0;
Evan Chengcc1fc222006-05-25 23:31:23 +0000460
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000461 if (MaxNumIntRegs>3) {
462 // We don't have too much registers on ia32! :)
463 MaxNumIntRegs = 3;
464 }
465
Evan Chengeda65fa2006-04-27 01:32:22 +0000466 switch (ObjectVT) {
467 default: assert(0 && "Unhandled argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000468 case MVT::i8:
469 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
470 ObjIntRegs = 1;
471 else
472 ObjSize = 1;
473 break;
474 case MVT::i16:
475 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
476 ObjIntRegs = 1;
477 else
478 ObjSize = 2;
479 break;
480 case MVT::i32:
481 if (ArgInReg && (NumIntRegs < MaxNumIntRegs))
482 ObjIntRegs = 1;
483 else
484 ObjSize = 4;
485 break;
486 case MVT::i64:
487 if (ArgInReg && (NumIntRegs+2 <= MaxNumIntRegs)) {
488 ObjIntRegs = 2;
489 } else if (ArgInReg && (NumIntRegs+1 <= MaxNumIntRegs)) {
490 ObjIntRegs = 1;
491 ObjSize = 4;
492 } else
493 ObjSize = 8;
494 case MVT::f32:
495 ObjSize = 4;
496 break;
497 case MVT::f64:
498 ObjSize = 8;
499 break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000500 case MVT::v16i8:
501 case MVT::v8i16:
502 case MVT::v4i32:
503 case MVT::v2i64:
504 case MVT::v4f32:
505 case MVT::v2f64:
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000506 if (AllowVectors) {
507 if (NumXMMRegs < 4)
508 ObjXMMRegs = 1;
509 else
510 ObjSize = 16;
511 break;
512 } else
513 assert(0 && "Unhandled argument type [vector]!");
Evan Chengeda65fa2006-04-27 01:32:22 +0000514 }
Evan Chengeda65fa2006-04-27 01:32:22 +0000515}
516
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000517SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
518 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000519 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000520 MachineFunction &MF = DAG.getMachineFunction();
521 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000522 SDOperand Root = Op.getOperand(0);
523 std::vector<SDOperand> ArgValues;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000524 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000525
Evan Chengeda65fa2006-04-27 01:32:22 +0000526 // Add DAG nodes to load the arguments... On entry to a function on the X86,
527 // the stack frame looks like this:
528 //
529 // [ESP] -- return address
530 // [ESP + 4] -- first argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +0000531 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +0000532 // ...
533 //
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000534 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
535 unsigned NumSRetBytes= 0; // How much bytes on stack used for struct return
536 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
537 unsigned NumIntRegs = 0; // Integer regs used for parameter passing
538
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000539 static const unsigned XMMArgRegs[] = {
540 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
541 };
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000542 static const unsigned GPRArgRegs[][3] = {
543 { X86::AL, X86::DL, X86::CL },
544 { X86::AX, X86::DX, X86::CX },
545 { X86::EAX, X86::EDX, X86::ECX }
546 };
547 static const TargetRegisterClass* GPRClasses[3] = {
548 X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass
549 };
550
551 // Handle regparm attribute
552 std::vector<bool> ArgInRegs(NumArgs, false);
553 std::vector<bool> SRetArgs(NumArgs, false);
554 if (!isVarArg) {
555 for (unsigned i = 0; i<NumArgs; ++i) {
556 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3+i))->getValue();
557 ArgInRegs[i] = (Flags >> 1) & 1;
558 SRetArgs[i] = (Flags >> 2) & 1;
559 }
560 }
561
Evan Cheng1bc78042006-04-26 01:20:17 +0000562 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +0000563 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
564 unsigned ArgIncrement = 4;
565 unsigned ObjSize = 0;
566 unsigned ObjXMMRegs = 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000567 unsigned ObjIntRegs = 0;
568 unsigned Reg = 0;
569 SDOperand ArgValue;
570
571 HowToPassCallArgument(ObjectVT,
572 ArgInRegs[i],
573 NumIntRegs, NumXMMRegs, 3,
574 ObjSize, ObjIntRegs, ObjXMMRegs,
575 !isStdCall);
576
Evan Cheng052fb512006-05-26 18:39:59 +0000577 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +0000578 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +0000579
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000580 if (ObjIntRegs || ObjXMMRegs) {
581 switch (ObjectVT) {
582 default: assert(0 && "Unhandled argument type!");
583 case MVT::i8:
584 case MVT::i16:
585 case MVT::i32: {
586 unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][NumIntRegs];
587 Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]);
588 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
589 break;
590 }
591 case MVT::v16i8:
592 case MVT::v8i16:
593 case MVT::v4i32:
594 case MVT::v2i64:
595 case MVT::v4f32:
596 case MVT::v2f64:
597 assert(!isStdCall && "Unhandled argument type!");
598 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
599 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
600 break;
601 }
602 NumIntRegs += ObjIntRegs;
Evan Cheng25caf632006-05-23 21:06:34 +0000603 NumXMMRegs += ObjXMMRegs;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000604 }
605 if (ObjSize) {
Evan Cheng3fddf242006-05-26 20:37:47 +0000606 // XMM arguments have to be aligned on 16-byte boundary.
607 if (ObjSize == 16)
608 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000609 // Create the SelectionDAG nodes corresponding to a load from this
610 // parameter.
Evan Cheng25caf632006-05-23 21:06:34 +0000611 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
612 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +0000613 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000614
615 ArgOffset += ArgIncrement; // Move on to the next argument.
616 if (SRetArgs[i])
617 NumSRetBytes += ArgIncrement;
Evan Cheng1bc78042006-04-26 01:20:17 +0000618 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000619
620 ArgValues.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +0000621 }
622
Evan Cheng25caf632006-05-23 21:06:34 +0000623 ArgValues.push_back(Root);
624
Evan Cheng1bc78042006-04-26 01:20:17 +0000625 // If the function takes variable number of arguments, make a frame index for
626 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000627 if (isVarArg)
Evan Cheng1bc78042006-04-26 01:20:17 +0000628 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000629
630 if (isStdCall && !isVarArg) {
631 BytesToPopOnReturn = ArgOffset; // Callee pops everything..
632 BytesCallerReserves = 0;
633 } else {
634 BytesToPopOnReturn = NumSRetBytes; // Callee pops hidden struct pointer.
635 BytesCallerReserves = ArgOffset;
636 }
637
Evan Cheng25ab6902006-09-08 06:48:29 +0000638 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
639 ReturnAddrIndex = 0; // No return address slot generated yet.
Evan Cheng25caf632006-05-23 21:06:34 +0000640
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000641
642 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +0000643
Evan Cheng25caf632006-05-23 21:06:34 +0000644 // Return the new list of results.
645 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
646 Op.Val->value_end());
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000647 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000648}
649
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000650SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
651 bool isStdCall) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000652 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000653 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000654 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
655 SDOperand Callee = Op.getOperand(4);
656 MVT::ValueType RetVT= Op.Val->getValueType(0);
657 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000658
Evan Cheng32fe1032006-05-25 00:59:30 +0000659 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000660 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000661 };
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000662 static const unsigned GPR32ArgRegs[] = {
663 X86::EAX, X86::EDX, X86::ECX
664 };
Evan Cheng347d5f72006-04-28 21:29:37 +0000665
Evan Cheng32fe1032006-05-25 00:59:30 +0000666 // Count how many bytes are to be pushed on the stack.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000667 unsigned NumBytes = 0;
668 // Keep track of the number of integer regs passed so far.
669 unsigned NumIntRegs = 0;
670 // Keep track of the number of XMM regs passed so far.
671 unsigned NumXMMRegs = 0;
672 // How much bytes on stack used for struct return
673 unsigned NumSRetBytes= 0;
674
675 // Handle regparm attribute
676 std::vector<bool> ArgInRegs(NumOps, false);
677 std::vector<bool> SRetArgs(NumOps, false);
678 for (unsigned i = 0; i<NumOps; ++i) {
679 unsigned Flags =
680 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue();
681 ArgInRegs[i] = (Flags >> 1) & 1;
682 SRetArgs[i] = (Flags >> 2) & 1;
683 }
684
685 // Calculate stack frame size
Evan Cheng32fe1032006-05-25 00:59:30 +0000686 for (unsigned i = 0; i != NumOps; ++i) {
687 SDOperand Arg = Op.getOperand(5+2*i);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000688 unsigned ArgIncrement = 4;
689 unsigned ObjSize = 0;
690 unsigned ObjIntRegs = 0;
691 unsigned ObjXMMRegs = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000692
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000693 HowToPassCallArgument(Arg.getValueType(),
694 ArgInRegs[i],
695 NumIntRegs, NumXMMRegs, 3,
696 ObjSize, ObjIntRegs, ObjXMMRegs,
697 !isStdCall);
698 if (ObjSize > 4)
699 ArgIncrement = ObjSize;
700
701 NumIntRegs += ObjIntRegs;
702 NumXMMRegs += ObjXMMRegs;
703 if (ObjSize) {
704 // XMM arguments have to be aligned on 16-byte boundary.
705 if (ObjSize == 16)
Evan Cheng3fddf242006-05-26 20:37:47 +0000706 NumBytes = ((NumBytes + 15) / 16) * 16;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000707 NumBytes += ArgIncrement;
Evan Cheng32fe1032006-05-25 00:59:30 +0000708 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000709 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000710
Evan Cheng32fe1032006-05-25 00:59:30 +0000711 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000712
Evan Cheng32fe1032006-05-25 00:59:30 +0000713 // Arguments go on the stack in reverse order, as specified by the ABI.
714 unsigned ArgOffset = 0;
715 NumXMMRegs = 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000716 NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000717 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
718 std::vector<SDOperand> MemOpChains;
Evan Cheng25ab6902006-09-08 06:48:29 +0000719 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +0000720 for (unsigned i = 0; i != NumOps; ++i) {
721 SDOperand Arg = Op.getOperand(5+2*i);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000722 unsigned ArgIncrement = 4;
723 unsigned ObjSize = 0;
724 unsigned ObjIntRegs = 0;
725 unsigned ObjXMMRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000726
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000727 HowToPassCallArgument(Arg.getValueType(),
728 ArgInRegs[i],
729 NumIntRegs, NumXMMRegs, 3,
730 ObjSize, ObjIntRegs, ObjXMMRegs,
731 !isStdCall);
732
733 if (ObjSize > 4)
734 ArgIncrement = ObjSize;
735
736 if (Arg.getValueType() == MVT::i8 || Arg.getValueType() == MVT::i16) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000737 // Promote the integer to 32 bits. If the input type is signed use a
738 // sign extend, otherwise use a zero extend.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000739 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue();
740
741 unsigned ExtOp = (Flags & 1) ? ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
Evan Cheng32fe1032006-05-25 00:59:30 +0000742 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
Evan Cheng6b5783d2006-05-25 18:56:34 +0000743 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000744
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000745 if (ObjIntRegs || ObjXMMRegs) {
746 switch (Arg.getValueType()) {
747 default: assert(0 && "Unhandled argument type!");
748 case MVT::i32:
749 RegsToPass.push_back(std::make_pair(GPR32ArgRegs[NumIntRegs], Arg));
750 break;
751 case MVT::v16i8:
752 case MVT::v8i16:
753 case MVT::v4i32:
754 case MVT::v2i64:
755 case MVT::v4f32:
756 case MVT::v2f64:
757 assert(!isStdCall && "Unhandled argument type!");
758 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
759 break;
Evan Cheng347d5f72006-04-28 21:29:37 +0000760 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000761
762 NumIntRegs += ObjIntRegs;
763 NumXMMRegs += ObjXMMRegs;
764 }
765 if (ObjSize) {
766 // XMM arguments have to be aligned on 16-byte boundary.
767 if (ObjSize == 16)
768 ArgOffset = ((ArgOffset + 15) / 16) * 16;
769
770 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
771 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
772 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
773
774 ArgOffset += ArgIncrement; // Move on to the next argument.
775 if (SRetArgs[i])
776 NumSRetBytes += ArgIncrement;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000777 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000778 }
779
Anton Korobeynikovb25fe822007-02-01 08:39:52 +0000780 // Sanity check: we haven't seen NumSRetBytes > 4
781 assert((NumSRetBytes<=4) &&
782 "Too much space for struct-return pointer requested");
783
Evan Cheng32fe1032006-05-25 00:59:30 +0000784 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000785 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
786 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000787
Evan Cheng347d5f72006-04-28 21:29:37 +0000788 // Build a sequence of copy-to-reg nodes chained together with token chain
789 // and flag operands which copy the outgoing args into registers.
790 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000791 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
792 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
793 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000794 InFlag = Chain.getValue(1);
795 }
796
Evan Chengf4684712007-02-21 21:18:14 +0000797 // ELF / PIC requires GOT in the EBX register before function calls via PLT
798 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +0000799 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
800 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000801 Chain = DAG.getCopyToReg(Chain, X86::EBX,
802 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
803 InFlag);
804 InFlag = Chain.getValue(1);
805 }
806
Evan Cheng32fe1032006-05-25 00:59:30 +0000807 // If the callee is a GlobalAddress node (quite common, every direct call is)
808 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +0000809 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +0000810 // We should use extra load for direct calls to dllimported functions in
811 // non-JIT mode.
812 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
813 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +0000814 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
815 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +0000816 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
817
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000818 std::vector<MVT::ValueType> NodeTys;
819 NodeTys.push_back(MVT::Other); // Returns a chain
820 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
821 std::vector<SDOperand> Ops;
822 Ops.push_back(Chain);
823 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +0000824
825 // Add argument registers to the end of the list so that they are known live
826 // into the call.
827 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000828 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +0000829 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +0000830
831 // Add an implicit use GOT pointer in EBX.
832 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
833 Subtarget->isPICStyleGOT())
834 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000835
Evan Cheng347d5f72006-04-28 21:29:37 +0000836 if (InFlag.Val)
837 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000838
Evan Cheng32fe1032006-05-25 00:59:30 +0000839 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000840 NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +0000841 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000842
Chris Lattner2d297092006-05-23 18:50:38 +0000843 // Create the CALLSEQ_END node.
844 unsigned NumBytesForCalleeToPush = 0;
845
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000846 if (isStdCall) {
847 if (isVarArg) {
848 NumBytesForCalleeToPush = NumSRetBytes;
849 } else {
850 NumBytesForCalleeToPush = NumBytes;
851 }
852 } else {
853 // If this is is a call to a struct-return function, the callee
854 // pops the hidden struct pointer, so we have to push it back.
855 // This is common for Darwin/X86, Linux & Mingw32 targets.
856 NumBytesForCalleeToPush = NumSRetBytes;
857 }
858
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000859 NodeTys.clear();
860 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +0000861 if (RetVT != MVT::Other)
862 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000863 Ops.clear();
864 Ops.push_back(Chain);
865 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +0000866 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000867 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000868 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +0000869 if (RetVT != MVT::Other)
870 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000871
Evan Cheng32fe1032006-05-25 00:59:30 +0000872 std::vector<SDOperand> ResultVals;
873 NodeTys.clear();
874 switch (RetVT) {
875 default: assert(0 && "Unknown value type to return!");
876 case MVT::Other: break;
877 case MVT::i8:
878 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
879 ResultVals.push_back(Chain.getValue(0));
880 NodeTys.push_back(MVT::i8);
881 break;
882 case MVT::i16:
883 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
884 ResultVals.push_back(Chain.getValue(0));
885 NodeTys.push_back(MVT::i16);
886 break;
887 case MVT::i32:
888 if (Op.Val->getValueType(1) == MVT::i32) {
889 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
890 ResultVals.push_back(Chain.getValue(0));
891 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
892 Chain.getValue(2)).getValue(1);
893 ResultVals.push_back(Chain.getValue(0));
894 NodeTys.push_back(MVT::i32);
895 } else {
896 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
897 ResultVals.push_back(Chain.getValue(0));
Evan Chengd90eb7f2006-01-05 00:27:02 +0000898 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000899 NodeTys.push_back(MVT::i32);
900 break;
901 case MVT::v16i8:
902 case MVT::v8i16:
903 case MVT::v4i32:
904 case MVT::v2i64:
905 case MVT::v4f32:
906 case MVT::v2f64:
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000907 assert(!isStdCall && "Unknown value type to return!");
Evan Cheng32fe1032006-05-25 00:59:30 +0000908 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
909 ResultVals.push_back(Chain.getValue(0));
910 NodeTys.push_back(RetVT);
911 break;
912 case MVT::f32:
913 case MVT::f64: {
914 std::vector<MVT::ValueType> Tys;
915 Tys.push_back(MVT::f64);
916 Tys.push_back(MVT::Other);
917 Tys.push_back(MVT::Flag);
918 std::vector<SDOperand> Ops;
919 Ops.push_back(Chain);
920 Ops.push_back(InFlag);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000921 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000922 &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +0000923 Chain = RetVal.getValue(1);
924 InFlag = RetVal.getValue(2);
925 if (X86ScalarSSE) {
926 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
927 // shouldn't be necessary except that RFP cannot be live across
928 // multiple blocks. When stackifier is fixed, they can be uncoupled.
929 MachineFunction &MF = DAG.getMachineFunction();
930 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
931 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
932 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000933 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +0000934 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000935 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +0000936 Ops.push_back(RetVal);
937 Ops.push_back(StackSlot);
938 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000939 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000940 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +0000941 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
Evan Cheng347d5f72006-04-28 21:29:37 +0000942 Chain = RetVal.getValue(1);
Evan Cheng347d5f72006-04-28 21:29:37 +0000943 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000944
945 if (RetVT == MVT::f32 && !X86ScalarSSE)
946 // FIXME: we would really like to remember that this FP_ROUND
947 // operation is okay to eliminate if we allow excess FP precision.
948 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
949 ResultVals.push_back(RetVal);
950 NodeTys.push_back(RetVT);
951 break;
952 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000953 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000954
Evan Cheng32fe1032006-05-25 00:59:30 +0000955 // If the function returns void, just return the chain.
956 if (ResultVals.empty())
957 return Chain;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000958
Evan Cheng32fe1032006-05-25 00:59:30 +0000959 // Otherwise, merge everything together with a MERGE_VALUES node.
960 NodeTys.push_back(MVT::Other);
961 ResultVals.push_back(Chain);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000962 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
963 &ResultVals[0], ResultVals.size());
Evan Cheng32fe1032006-05-25 00:59:30 +0000964 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000965}
966
Evan Cheng25ab6902006-09-08 06:48:29 +0000967
968//===----------------------------------------------------------------------===//
969// X86-64 C Calling Convention implementation
970//===----------------------------------------------------------------------===//
971
972/// HowToPassX86_64CCCArgument - Returns how an formal argument of the specified
973/// type should be passed. If it is through stack, returns the size of the stack
974/// slot; if it is through integer or XMM register, returns the number of
975/// integer or XMM registers are needed.
976static void
977HowToPassX86_64CCCArgument(MVT::ValueType ObjectVT,
978 unsigned NumIntRegs, unsigned NumXMMRegs,
979 unsigned &ObjSize, unsigned &ObjIntRegs,
980 unsigned &ObjXMMRegs) {
981 ObjSize = 0;
982 ObjIntRegs = 0;
983 ObjXMMRegs = 0;
984
985 switch (ObjectVT) {
986 default: assert(0 && "Unhandled argument type!");
987 case MVT::i8:
988 case MVT::i16:
989 case MVT::i32:
990 case MVT::i64:
991 if (NumIntRegs < 6)
992 ObjIntRegs = 1;
993 else {
994 switch (ObjectVT) {
995 default: break;
996 case MVT::i8: ObjSize = 1; break;
997 case MVT::i16: ObjSize = 2; break;
998 case MVT::i32: ObjSize = 4; break;
999 case MVT::i64: ObjSize = 8; break;
1000 }
1001 }
1002 break;
1003 case MVT::f32:
1004 case MVT::f64:
1005 case MVT::v16i8:
1006 case MVT::v8i16:
1007 case MVT::v4i32:
1008 case MVT::v2i64:
1009 case MVT::v4f32:
1010 case MVT::v2f64:
1011 if (NumXMMRegs < 8)
1012 ObjXMMRegs = 1;
1013 else {
1014 switch (ObjectVT) {
1015 default: break;
1016 case MVT::f32: ObjSize = 4; break;
1017 case MVT::f64: ObjSize = 8; break;
1018 case MVT::v16i8:
1019 case MVT::v8i16:
1020 case MVT::v4i32:
1021 case MVT::v2i64:
1022 case MVT::v4f32:
1023 case MVT::v2f64: ObjSize = 16; break;
1024 }
1025 break;
1026 }
1027 }
1028}
1029
1030SDOperand
1031X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
1032 unsigned NumArgs = Op.Val->getNumValues() - 1;
1033 MachineFunction &MF = DAG.getMachineFunction();
1034 MachineFrameInfo *MFI = MF.getFrameInfo();
1035 SDOperand Root = Op.getOperand(0);
1036 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1037 std::vector<SDOperand> ArgValues;
1038
1039 // Add DAG nodes to load the arguments... On entry to a function on the X86,
1040 // the stack frame looks like this:
1041 //
1042 // [RSP] -- return address
1043 // [RSP + 8] -- first nonreg argument (leftmost lexically)
1044 // [RSP +16] -- second nonreg argument, if 1st argument is <= 8 bytes in size
1045 // ...
1046 //
1047 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1048 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1049 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1050
1051 static const unsigned GPR8ArgRegs[] = {
1052 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1053 };
1054 static const unsigned GPR16ArgRegs[] = {
1055 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1056 };
1057 static const unsigned GPR32ArgRegs[] = {
1058 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1059 };
1060 static const unsigned GPR64ArgRegs[] = {
1061 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1062 };
1063 static const unsigned XMMArgRegs[] = {
1064 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1065 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1066 };
1067
1068 for (unsigned i = 0; i < NumArgs; ++i) {
1069 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1070 unsigned ArgIncrement = 8;
1071 unsigned ObjSize = 0;
1072 unsigned ObjIntRegs = 0;
1073 unsigned ObjXMMRegs = 0;
1074
1075 // FIXME: __int128 and long double support?
1076 HowToPassX86_64CCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
1077 ObjSize, ObjIntRegs, ObjXMMRegs);
1078 if (ObjSize > 8)
1079 ArgIncrement = ObjSize;
1080
1081 unsigned Reg = 0;
1082 SDOperand ArgValue;
1083 if (ObjIntRegs || ObjXMMRegs) {
1084 switch (ObjectVT) {
1085 default: assert(0 && "Unhandled argument type!");
1086 case MVT::i8:
1087 case MVT::i16:
1088 case MVT::i32:
1089 case MVT::i64: {
1090 TargetRegisterClass *RC = NULL;
1091 switch (ObjectVT) {
1092 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001093 case MVT::i8:
Evan Cheng25ab6902006-09-08 06:48:29 +00001094 RC = X86::GR8RegisterClass;
1095 Reg = GPR8ArgRegs[NumIntRegs];
1096 break;
1097 case MVT::i16:
1098 RC = X86::GR16RegisterClass;
1099 Reg = GPR16ArgRegs[NumIntRegs];
1100 break;
1101 case MVT::i32:
1102 RC = X86::GR32RegisterClass;
1103 Reg = GPR32ArgRegs[NumIntRegs];
1104 break;
1105 case MVT::i64:
1106 RC = X86::GR64RegisterClass;
1107 Reg = GPR64ArgRegs[NumIntRegs];
1108 break;
1109 }
1110 Reg = AddLiveIn(MF, Reg, RC);
1111 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1112 break;
1113 }
1114 case MVT::f32:
1115 case MVT::f64:
1116 case MVT::v16i8:
1117 case MVT::v8i16:
1118 case MVT::v4i32:
1119 case MVT::v2i64:
1120 case MVT::v4f32:
1121 case MVT::v2f64: {
1122 TargetRegisterClass *RC= (ObjectVT == MVT::f32) ?
1123 X86::FR32RegisterClass : ((ObjectVT == MVT::f64) ?
1124 X86::FR64RegisterClass : X86::VR128RegisterClass);
1125 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], RC);
1126 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1127 break;
1128 }
1129 }
1130 NumIntRegs += ObjIntRegs;
1131 NumXMMRegs += ObjXMMRegs;
1132 } else if (ObjSize) {
1133 // XMM arguments have to be aligned on 16-byte boundary.
1134 if (ObjSize == 16)
1135 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1136 // Create the SelectionDAG nodes corresponding to a load from this
1137 // parameter.
1138 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1139 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Cheng466685d2006-10-09 20:57:25 +00001140 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00001141 ArgOffset += ArgIncrement; // Move on to the next argument.
1142 }
1143
1144 ArgValues.push_back(ArgValue);
1145 }
1146
1147 // If the function takes variable number of arguments, make a frame index for
1148 // the start of the first vararg value... for expansion of llvm.va_start.
1149 if (isVarArg) {
1150 // For X86-64, if there are vararg parameters that are passed via
1151 // registers, then we must store them to their spots on the stack so they
1152 // may be loaded by deferencing the result of va_next.
1153 VarArgsGPOffset = NumIntRegs * 8;
1154 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1155 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
1156 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1157
1158 // Store the integer parameter registers.
1159 std::vector<SDOperand> MemOps;
1160 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1161 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1162 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1163 for (; NumIntRegs != 6; ++NumIntRegs) {
1164 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1165 X86::GR64RegisterClass);
1166 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001167 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00001168 MemOps.push_back(Store);
1169 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1170 DAG.getConstant(8, getPointerTy()));
1171 }
1172
1173 // Now store the XMM (fp + vector) parameter registers.
1174 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1175 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1176 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1177 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1178 X86::VR128RegisterClass);
1179 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001180 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00001181 MemOps.push_back(Store);
1182 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1183 DAG.getConstant(16, getPointerTy()));
1184 }
1185 if (!MemOps.empty())
1186 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1187 &MemOps[0], MemOps.size());
1188 }
1189
1190 ArgValues.push_back(Root);
1191
1192 ReturnAddrIndex = 0; // No return address slot generated yet.
1193 BytesToPopOnReturn = 0; // Callee pops nothing.
1194 BytesCallerReserves = ArgOffset;
1195
1196 // Return the new list of results.
1197 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1198 Op.Val->value_end());
1199 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
1200}
1201
1202SDOperand
1203X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG) {
1204 SDOperand Chain = Op.getOperand(0);
Evan Cheng25ab6902006-09-08 06:48:29 +00001205 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1206 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1207 SDOperand Callee = Op.getOperand(4);
1208 MVT::ValueType RetVT= Op.Val->getValueType(0);
1209 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1210
1211 // Count how many bytes are to be pushed on the stack.
1212 unsigned NumBytes = 0;
1213 unsigned NumIntRegs = 0; // Int regs used for parameter passing.
1214 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
1215
1216 static const unsigned GPR8ArgRegs[] = {
1217 X86::DIL, X86::SIL, X86::DL, X86::CL, X86::R8B, X86::R9B
1218 };
1219 static const unsigned GPR16ArgRegs[] = {
1220 X86::DI, X86::SI, X86::DX, X86::CX, X86::R8W, X86::R9W
1221 };
1222 static const unsigned GPR32ArgRegs[] = {
1223 X86::EDI, X86::ESI, X86::EDX, X86::ECX, X86::R8D, X86::R9D
1224 };
1225 static const unsigned GPR64ArgRegs[] = {
1226 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1227 };
1228 static const unsigned XMMArgRegs[] = {
1229 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1230 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1231 };
1232
1233 for (unsigned i = 0; i != NumOps; ++i) {
1234 SDOperand Arg = Op.getOperand(5+2*i);
1235 MVT::ValueType ArgVT = Arg.getValueType();
1236
1237 switch (ArgVT) {
1238 default: assert(0 && "Unknown value type!");
1239 case MVT::i8:
1240 case MVT::i16:
1241 case MVT::i32:
1242 case MVT::i64:
1243 if (NumIntRegs < 6)
1244 ++NumIntRegs;
1245 else
1246 NumBytes += 8;
1247 break;
1248 case MVT::f32:
1249 case MVT::f64:
1250 case MVT::v16i8:
1251 case MVT::v8i16:
1252 case MVT::v4i32:
1253 case MVT::v2i64:
1254 case MVT::v4f32:
1255 case MVT::v2f64:
1256 if (NumXMMRegs < 8)
1257 NumXMMRegs++;
1258 else if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1259 NumBytes += 8;
1260 else {
1261 // XMM arguments have to be aligned on 16-byte boundary.
1262 NumBytes = ((NumBytes + 15) / 16) * 16;
1263 NumBytes += 16;
1264 }
1265 break;
1266 }
1267 }
1268
1269 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1270
1271 // Arguments go on the stack in reverse order, as specified by the ABI.
1272 unsigned ArgOffset = 0;
1273 NumIntRegs = 0;
1274 NumXMMRegs = 0;
1275 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1276 std::vector<SDOperand> MemOpChains;
1277 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
1278 for (unsigned i = 0; i != NumOps; ++i) {
1279 SDOperand Arg = Op.getOperand(5+2*i);
1280 MVT::ValueType ArgVT = Arg.getValueType();
1281
1282 switch (ArgVT) {
1283 default: assert(0 && "Unexpected ValueType for argument!");
1284 case MVT::i8:
1285 case MVT::i16:
1286 case MVT::i32:
1287 case MVT::i64:
1288 if (NumIntRegs < 6) {
1289 unsigned Reg = 0;
1290 switch (ArgVT) {
1291 default: break;
1292 case MVT::i8: Reg = GPR8ArgRegs[NumIntRegs]; break;
1293 case MVT::i16: Reg = GPR16ArgRegs[NumIntRegs]; break;
1294 case MVT::i32: Reg = GPR32ArgRegs[NumIntRegs]; break;
1295 case MVT::i64: Reg = GPR64ArgRegs[NumIntRegs]; break;
1296 }
1297 RegsToPass.push_back(std::make_pair(Reg, Arg));
1298 ++NumIntRegs;
1299 } else {
1300 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1301 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001302 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Evan Cheng25ab6902006-09-08 06:48:29 +00001303 ArgOffset += 8;
1304 }
1305 break;
1306 case MVT::f32:
1307 case MVT::f64:
1308 case MVT::v16i8:
1309 case MVT::v8i16:
1310 case MVT::v4i32:
1311 case MVT::v2i64:
1312 case MVT::v4f32:
1313 case MVT::v2f64:
1314 if (NumXMMRegs < 8) {
1315 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1316 NumXMMRegs++;
1317 } else {
1318 if (ArgVT != MVT::f32 && ArgVT != MVT::f64) {
1319 // XMM arguments have to be aligned on 16-byte boundary.
1320 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1321 }
1322 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1323 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001324 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Evan Cheng25ab6902006-09-08 06:48:29 +00001325 if (ArgVT == MVT::f32 || ArgVT == MVT::f64)
1326 ArgOffset += 8;
1327 else
1328 ArgOffset += 16;
1329 }
1330 }
1331 }
1332
1333 if (!MemOpChains.empty())
1334 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1335 &MemOpChains[0], MemOpChains.size());
1336
1337 // Build a sequence of copy-to-reg nodes chained together with token chain
1338 // and flag operands which copy the outgoing args into registers.
1339 SDOperand InFlag;
1340 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1341 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1342 InFlag);
1343 InFlag = Chain.getValue(1);
1344 }
1345
1346 if (isVarArg) {
1347 // From AMD64 ABI document:
1348 // For calls that may call functions that use varargs or stdargs
1349 // (prototype-less calls or calls to functions containing ellipsis (...) in
1350 // the declaration) %al is used as hidden argument to specify the number
1351 // of SSE registers used. The contents of %al do not need to match exactly
1352 // the number of registers, but must be an ubound on the number of SSE
1353 // registers used and is in the range 0 - 8 inclusive.
1354 Chain = DAG.getCopyToReg(Chain, X86::AL,
1355 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1356 InFlag = Chain.getValue(1);
1357 }
1358
1359 // If the callee is a GlobalAddress node (quite common, every direct call is)
1360 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001361 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001362 // We should use extra load for direct calls to dllimported functions in
1363 // non-JIT mode.
1364 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1365 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001366 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1367 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng25ab6902006-09-08 06:48:29 +00001368 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1369
1370 std::vector<MVT::ValueType> NodeTys;
1371 NodeTys.push_back(MVT::Other); // Returns a chain
1372 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1373 std::vector<SDOperand> Ops;
1374 Ops.push_back(Chain);
1375 Ops.push_back(Callee);
1376
1377 // Add argument registers to the end of the list so that they are known live
1378 // into the call.
1379 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001380 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Cheng25ab6902006-09-08 06:48:29 +00001381 RegsToPass[i].second.getValueType()));
1382
1383 if (InFlag.Val)
1384 Ops.push_back(InFlag);
1385
1386 // FIXME: Do not generate X86ISD::TAILCALL for now.
1387 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1388 NodeTys, &Ops[0], Ops.size());
1389 InFlag = Chain.getValue(1);
1390
1391 NodeTys.clear();
1392 NodeTys.push_back(MVT::Other); // Returns a chain
1393 if (RetVT != MVT::Other)
1394 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1395 Ops.clear();
1396 Ops.push_back(Chain);
1397 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1398 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1399 Ops.push_back(InFlag);
1400 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1401 if (RetVT != MVT::Other)
1402 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001403
Evan Cheng25ab6902006-09-08 06:48:29 +00001404 std::vector<SDOperand> ResultVals;
1405 NodeTys.clear();
1406 switch (RetVT) {
1407 default: assert(0 && "Unknown value type to return!");
1408 case MVT::Other: break;
1409 case MVT::i8:
1410 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1411 ResultVals.push_back(Chain.getValue(0));
1412 NodeTys.push_back(MVT::i8);
1413 break;
1414 case MVT::i16:
1415 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1416 ResultVals.push_back(Chain.getValue(0));
1417 NodeTys.push_back(MVT::i16);
1418 break;
1419 case MVT::i32:
1420 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1421 ResultVals.push_back(Chain.getValue(0));
1422 NodeTys.push_back(MVT::i32);
1423 break;
1424 case MVT::i64:
1425 if (Op.Val->getValueType(1) == MVT::i64) {
1426 // FIXME: __int128 support?
1427 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1428 ResultVals.push_back(Chain.getValue(0));
1429 Chain = DAG.getCopyFromReg(Chain, X86::RDX, MVT::i64,
1430 Chain.getValue(2)).getValue(1);
1431 ResultVals.push_back(Chain.getValue(0));
1432 NodeTys.push_back(MVT::i64);
1433 } else {
1434 Chain = DAG.getCopyFromReg(Chain, X86::RAX, MVT::i64, InFlag).getValue(1);
1435 ResultVals.push_back(Chain.getValue(0));
1436 }
1437 NodeTys.push_back(MVT::i64);
1438 break;
1439 case MVT::f32:
1440 case MVT::f64:
1441 case MVT::v16i8:
1442 case MVT::v8i16:
1443 case MVT::v4i32:
1444 case MVT::v2i64:
1445 case MVT::v4f32:
1446 case MVT::v2f64:
1447 // FIXME: long double support?
1448 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1449 ResultVals.push_back(Chain.getValue(0));
1450 NodeTys.push_back(RetVT);
1451 break;
1452 }
1453
1454 // If the function returns void, just return the chain.
1455 if (ResultVals.empty())
1456 return Chain;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001457
Evan Cheng25ab6902006-09-08 06:48:29 +00001458 // Otherwise, merge everything together with a MERGE_VALUES node.
1459 NodeTys.push_back(MVT::Other);
1460 ResultVals.push_back(Chain);
1461 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1462 &ResultVals[0], ResultVals.size());
1463 return Res.getValue(Op.ResNo);
1464}
1465
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001466//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001467// Fast & FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001468//===----------------------------------------------------------------------===//
1469//
1470// The X86 'fast' calling convention passes up to two integer arguments in
1471// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
1472// and requires that the callee pop its arguments off the stack (allowing proper
1473// tail calls), and has the same return value conventions as C calling convs.
1474//
1475// This calling convention always arranges for the callee pop value to be 8n+4
1476// bytes, which is needed for tail recursion elimination and stack alignment
1477// reasons.
1478//
1479// Note that this can be enhanced in the future to pass fp vals in registers
1480// (when we have a global fp allocator) and do other tricks.
1481//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001482//===----------------------------------------------------------------------===//
1483// The X86 'fastcall' calling convention passes up to two integer arguments in
1484// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1485// and requires that the callee pop its arguments off the stack (allowing proper
1486// tail calls), and has the same return value conventions as C calling convs.
1487//
1488// This calling convention always arranges for the callee pop value to be 8n+4
1489// bytes, which is needed for tail recursion elimination and stack alignment
1490// reasons.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001491
Evan Chengeda65fa2006-04-27 01:32:22 +00001492
Evan Cheng25caf632006-05-23 21:06:34 +00001493SDOperand
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001494X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG,
1495 bool isFastCall) {
Evan Cheng25caf632006-05-23 21:06:34 +00001496 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001497 MachineFunction &MF = DAG.getMachineFunction();
1498 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001499 SDOperand Root = Op.getOperand(0);
1500 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001501
Evan Chengeda65fa2006-04-27 01:32:22 +00001502 // Add DAG nodes to load the arguments... On entry to a function the stack
1503 // frame looks like this:
1504 //
1505 // [ESP] -- return address
1506 // [ESP + 4] -- first nonreg argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +00001507 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +00001508 // ...
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001509 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
1510
1511 // Keep track of the number of integer regs passed so far. This can be either
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001512 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX
1513 // are both used).
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001514 unsigned NumIntRegs = 0;
Evan Cheng2fdd95e2006-04-27 08:31:10 +00001515 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng32fe1032006-05-25 00:59:30 +00001516
1517 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001518 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +00001519 };
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001520
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001521 static const unsigned GPRArgRegs[][2][2] = {
1522 {{ X86::AL, X86::DL }, { X86::CL, X86::DL }},
1523 {{ X86::AX, X86::DX }, { X86::CX, X86::DX }},
1524 {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }}
1525 };
1526
1527 static const TargetRegisterClass* GPRClasses[3] = {
1528 X86::GR8RegisterClass, X86::GR16RegisterClass, X86::GR32RegisterClass
1529 };
1530
1531 unsigned GPRInd = (isFastCall ? 1 : 0);
Evan Cheng1bc78042006-04-26 01:20:17 +00001532 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +00001533 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
1534 unsigned ArgIncrement = 4;
1535 unsigned ObjSize = 0;
Evan Cheng25caf632006-05-23 21:06:34 +00001536 unsigned ObjXMMRegs = 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001537 unsigned ObjIntRegs = 0;
1538 unsigned Reg = 0;
1539 SDOperand ArgValue;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001540
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001541 HowToPassCallArgument(ObjectVT,
1542 true, // Use as much registers as possible
1543 NumIntRegs, NumXMMRegs,
1544 (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS),
1545 ObjSize, ObjIntRegs, ObjXMMRegs,
1546 !isFastCall);
1547
Evan Cheng052fb512006-05-26 18:39:59 +00001548 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +00001549 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +00001550
Evan Cheng25caf632006-05-23 21:06:34 +00001551 if (ObjIntRegs || ObjXMMRegs) {
1552 switch (ObjectVT) {
1553 default: assert(0 && "Unhandled argument type!");
Evan Cheng25caf632006-05-23 21:06:34 +00001554 case MVT::i8:
Evan Cheng25caf632006-05-23 21:06:34 +00001555 case MVT::i16:
Nick Lewycky53108972007-01-28 15:39:16 +00001556 case MVT::i32: {
1557 unsigned RegToUse = GPRArgRegs[ObjectVT-MVT::i8][GPRInd][NumIntRegs];
1558 Reg = AddLiveIn(MF, RegToUse, GPRClasses[ObjectVT-MVT::i8]);
1559 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1560 break;
1561 }
Evan Cheng25caf632006-05-23 21:06:34 +00001562 case MVT::v16i8:
1563 case MVT::v8i16:
1564 case MVT::v4i32:
1565 case MVT::v2i64:
1566 case MVT::v4f32:
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001567 case MVT::v2f64: {
1568 assert(!isFastCall && "Unhandled argument type!");
Evan Cheng25caf632006-05-23 21:06:34 +00001569 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
1570 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
1571 break;
Evan Chengeda65fa2006-04-27 01:32:22 +00001572 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001573 }
Evan Cheng25caf632006-05-23 21:06:34 +00001574 NumIntRegs += ObjIntRegs;
1575 NumXMMRegs += ObjXMMRegs;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001576 }
Evan Cheng25caf632006-05-23 21:06:34 +00001577 if (ObjSize) {
Evan Cheng3fddf242006-05-26 20:37:47 +00001578 // XMM arguments have to be aligned on 16-byte boundary.
1579 if (ObjSize == 16)
1580 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng25caf632006-05-23 21:06:34 +00001581 // Create the SelectionDAG nodes corresponding to a load from this
1582 // parameter.
1583 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
1584 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001585 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN, NULL, 0);
1586
Evan Cheng25caf632006-05-23 21:06:34 +00001587 ArgOffset += ArgIncrement; // Move on to the next argument.
1588 }
1589
1590 ArgValues.push_back(ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001591 }
1592
Evan Cheng25caf632006-05-23 21:06:34 +00001593 ArgValues.push_back(Root);
1594
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001595 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1596 // arguments and the arguments after the retaddr has been pushed are aligned.
1597 if ((ArgOffset & 7) == 0)
1598 ArgOffset += 4;
1599
1600 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001601 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001602 ReturnAddrIndex = 0; // No return address slot generated yet.
1603 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
1604 BytesCallerReserves = 0;
1605
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001606 MF.getInfo<X86FunctionInfo>()->setBytesToPopOnReturn(BytesToPopOnReturn);
1607
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001608 // Finally, inform the code generator which regs we return values in.
Evan Cheng25caf632006-05-23 21:06:34 +00001609 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001610 default: assert(0 && "Unknown type!");
1611 case MVT::isVoid: break;
Chris Lattner13bf6c12006-10-03 17:18:42 +00001612 case MVT::i1:
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001613 case MVT::i8:
1614 case MVT::i16:
1615 case MVT::i32:
1616 MF.addLiveOut(X86::EAX);
1617 break;
1618 case MVT::i64:
1619 MF.addLiveOut(X86::EAX);
1620 MF.addLiveOut(X86::EDX);
1621 break;
1622 case MVT::f32:
1623 case MVT::f64:
1624 MF.addLiveOut(X86::ST0);
1625 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001626 case MVT::v16i8:
1627 case MVT::v8i16:
1628 case MVT::v4i32:
1629 case MVT::v2i64:
1630 case MVT::v4f32:
1631 case MVT::v2f64:
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001632 assert(!isFastCall && "Unknown result type");
Evan Cheng347d5f72006-04-28 21:29:37 +00001633 MF.addLiveOut(X86::XMM0);
1634 break;
1635 }
Evan Cheng347d5f72006-04-28 21:29:37 +00001636
Evan Cheng25caf632006-05-23 21:06:34 +00001637 // Return the new list of results.
1638 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
1639 Op.Val->value_end());
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001640 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, &ArgValues[0],ArgValues.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001641}
1642
Chris Lattnere87e1152006-09-26 03:57:53 +00001643SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
1644 bool isFastCall) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001645 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001646 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1647 SDOperand Callee = Op.getOperand(4);
1648 MVT::ValueType RetVT= Op.Val->getValueType(0);
1649 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1650
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001651 // Count how many bytes are to be pushed on the stack.
1652 unsigned NumBytes = 0;
1653
1654 // Keep track of the number of integer regs passed so far. This can be either
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001655 // 0 (neither EAX/ECX or EDX used), 1 (EAX/ECX is used) or 2 (EAX/ECX and EDX
1656 // are both used).
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001657 unsigned NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001658 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001659
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001660 static const unsigned GPRArgRegs[][2][2] = {
1661 {{ X86::AL, X86::DL }, { X86::CL, X86::DL }},
1662 {{ X86::AX, X86::DX }, { X86::CX, X86::DX }},
1663 {{ X86::EAX, X86::EDX }, { X86::ECX, X86::EDX }}
Evan Cheng32fe1032006-05-25 00:59:30 +00001664 };
1665 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001666 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +00001667 };
1668
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001669 unsigned GPRInd = (isFastCall ? 1 : 0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001670 for (unsigned i = 0; i != NumOps; ++i) {
1671 SDOperand Arg = Op.getOperand(5+2*i);
1672
1673 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001674 default: assert(0 && "Unknown value type!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001675 case MVT::i8:
1676 case MVT::i16:
Nick Lewycky70084fd2006-09-21 02:08:31 +00001677 case MVT::i32: {
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001678 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1679 if (NumIntRegs < MaxNumIntRegs) {
1680 ++NumIntRegs;
1681 break;
1682 }
Nick Lewycky70084fd2006-09-21 02:08:31 +00001683 } // Fall through
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001684 case MVT::f32:
1685 NumBytes += 4;
1686 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001687 case MVT::f64:
1688 NumBytes += 8;
1689 break;
Evan Cheng32fe1032006-05-25 00:59:30 +00001690 case MVT::v16i8:
1691 case MVT::v8i16:
1692 case MVT::v4i32:
1693 case MVT::v2i64:
1694 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001695 case MVT::v2f64:
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001696 assert(!isFastCall && "Unknown value type!");
1697 if (NumXMMRegs < 4)
1698 NumXMMRegs++;
1699 else {
1700 // XMM arguments have to be aligned on 16-byte boundary.
1701 NumBytes = ((NumBytes + 15) / 16) * 16;
1702 NumBytes += 16;
1703 }
1704 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001705 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001706 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001707
1708 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1709 // arguments and the arguments after the retaddr has been pushed are aligned.
1710 if ((NumBytes & 7) == 0)
1711 NumBytes += 4;
1712
Chris Lattner94dd2922006-02-13 09:00:43 +00001713 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001714
1715 // Arguments go on the stack in reverse order, as specified by the ABI.
1716 unsigned ArgOffset = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001717 NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001718 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1719 std::vector<SDOperand> MemOpChains;
Evan Cheng25ab6902006-09-08 06:48:29 +00001720 SDOperand StackPtr = DAG.getRegister(X86StackPtr, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001721 for (unsigned i = 0; i != NumOps; ++i) {
1722 SDOperand Arg = Op.getOperand(5+2*i);
1723
1724 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001725 default: assert(0 && "Unexpected ValueType for argument!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001726 case MVT::i8:
1727 case MVT::i16:
Nick Lewycky70084fd2006-09-21 02:08:31 +00001728 case MVT::i32: {
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001729 unsigned MaxNumIntRegs = (isFastCall ? 2 : FASTCC_NUM_INT_ARGS_INREGS);
1730 if (NumIntRegs < MaxNumIntRegs) {
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001731 unsigned RegToUse =
1732 GPRArgRegs[Arg.getValueType()-MVT::i8][GPRInd][NumIntRegs];
1733 RegsToPass.push_back(std::make_pair(RegToUse, Arg));
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001734 ++NumIntRegs;
1735 break;
1736 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001737 } // Fall through
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001738 case MVT::f32: {
1739 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001740 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001741 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001742 ArgOffset += 4;
1743 break;
1744 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001745 case MVT::f64: {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001746 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001747 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001748 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001749 ArgOffset += 8;
1750 break;
1751 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001752 case MVT::v16i8:
1753 case MVT::v8i16:
1754 case MVT::v4i32:
1755 case MVT::v2i64:
1756 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001757 case MVT::v2f64:
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001758 assert(!isFastCall && "Unexpected ValueType for argument!");
1759 if (NumXMMRegs < 4) {
1760 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1761 NumXMMRegs++;
1762 } else {
1763 // XMM arguments have to be aligned on 16-byte boundary.
1764 ArgOffset = ((ArgOffset + 15) / 16) * 16;
1765 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1766 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1767 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1768 ArgOffset += 16;
1769 }
1770 break;
Evan Cheng32fe1032006-05-25 00:59:30 +00001771 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001772 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001773
Evan Cheng32fe1032006-05-25 00:59:30 +00001774 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001775 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1776 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001777
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001778 // Build a sequence of copy-to-reg nodes chained together with token chain
1779 // and flag operands which copy the outgoing args into registers.
1780 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001781 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1782 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1783 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001784 InFlag = Chain.getValue(1);
1785 }
1786
Evan Cheng32fe1032006-05-25 00:59:30 +00001787 // If the callee is a GlobalAddress node (quite common, every direct call is)
1788 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001789 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001790 // We should use extra load for direct calls to dllimported functions in
1791 // non-JIT mode.
1792 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1793 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001794 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1795 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001796 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1797
Evan Chengf4684712007-02-21 21:18:14 +00001798 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1799 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001800 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1801 Subtarget->isPICStyleGOT()) {
1802 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1803 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1804 InFlag);
1805 InFlag = Chain.getValue(1);
1806 }
1807
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001808 std::vector<MVT::ValueType> NodeTys;
1809 NodeTys.push_back(MVT::Other); // Returns a chain
1810 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1811 std::vector<SDOperand> Ops;
1812 Ops.push_back(Chain);
1813 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001814
1815 // Add argument registers to the end of the list so that they are known live
1816 // into the call.
1817 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001818 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001819 RegsToPass[i].second.getValueType()));
1820
Evan Chengf4684712007-02-21 21:18:14 +00001821 // Add an implicit use GOT pointer in EBX.
1822 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1823 Subtarget->isPICStyleGOT())
1824 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1825
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001826 if (InFlag.Val)
1827 Ops.push_back(InFlag);
1828
1829 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001830 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001831 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001832 InFlag = Chain.getValue(1);
1833
1834 NodeTys.clear();
1835 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +00001836 if (RetVT != MVT::Other)
1837 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001838 Ops.clear();
1839 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001840 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1841 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001842 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001843 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +00001844 if (RetVT != MVT::Other)
1845 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001846
Evan Cheng32fe1032006-05-25 00:59:30 +00001847 std::vector<SDOperand> ResultVals;
1848 NodeTys.clear();
1849 switch (RetVT) {
1850 default: assert(0 && "Unknown value type to return!");
1851 case MVT::Other: break;
1852 case MVT::i8:
1853 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1854 ResultVals.push_back(Chain.getValue(0));
1855 NodeTys.push_back(MVT::i8);
1856 break;
1857 case MVT::i16:
1858 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1859 ResultVals.push_back(Chain.getValue(0));
1860 NodeTys.push_back(MVT::i16);
1861 break;
1862 case MVT::i32:
1863 if (Op.Val->getValueType(1) == MVT::i32) {
1864 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1865 ResultVals.push_back(Chain.getValue(0));
1866 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1867 Chain.getValue(2)).getValue(1);
1868 ResultVals.push_back(Chain.getValue(0));
1869 NodeTys.push_back(MVT::i32);
1870 } else {
1871 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1872 ResultVals.push_back(Chain.getValue(0));
Evan Chengd9558e02006-01-06 00:43:03 +00001873 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001874 NodeTys.push_back(MVT::i32);
1875 break;
1876 case MVT::v16i8:
1877 case MVT::v8i16:
1878 case MVT::v4i32:
1879 case MVT::v2i64:
1880 case MVT::v4f32:
1881 case MVT::v2f64:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00001882 if (isFastCall) {
1883 assert(0 && "Unknown value type to return!");
1884 } else {
1885 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1886 ResultVals.push_back(Chain.getValue(0));
1887 NodeTys.push_back(RetVT);
1888 }
1889 break;
Evan Cheng32fe1032006-05-25 00:59:30 +00001890 case MVT::f32:
1891 case MVT::f64: {
1892 std::vector<MVT::ValueType> Tys;
1893 Tys.push_back(MVT::f64);
1894 Tys.push_back(MVT::Other);
1895 Tys.push_back(MVT::Flag);
1896 std::vector<SDOperand> Ops;
1897 Ops.push_back(Chain);
1898 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001899 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys,
1900 &Ops[0], Ops.size());
Evan Cheng32fe1032006-05-25 00:59:30 +00001901 Chain = RetVal.getValue(1);
1902 InFlag = RetVal.getValue(2);
1903 if (X86ScalarSSE) {
1904 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1905 // shouldn't be necessary except that RFP cannot be live across
1906 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1907 MachineFunction &MF = DAG.getMachineFunction();
1908 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1909 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1910 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001911 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +00001912 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001913 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001914 Ops.push_back(RetVal);
1915 Ops.push_back(StackSlot);
1916 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001917 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001918 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00001919 RetVal = DAG.getLoad(RetVT, Chain, StackSlot, NULL, 0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001920 Chain = RetVal.getValue(1);
1921 }
Evan Chengd9558e02006-01-06 00:43:03 +00001922
Evan Cheng32fe1032006-05-25 00:59:30 +00001923 if (RetVT == MVT::f32 && !X86ScalarSSE)
1924 // FIXME: we would really like to remember that this FP_ROUND
1925 // operation is okay to eliminate if we allow excess FP precision.
1926 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1927 ResultVals.push_back(RetVal);
1928 NodeTys.push_back(RetVT);
1929 break;
1930 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001931 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001932
Evan Cheng32fe1032006-05-25 00:59:30 +00001933
1934 // If the function returns void, just return the chain.
1935 if (ResultVals.empty())
1936 return Chain;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001937
Evan Cheng32fe1032006-05-25 00:59:30 +00001938 // Otherwise, merge everything together with a MERGE_VALUES node.
1939 NodeTys.push_back(MVT::Other);
1940 ResultVals.push_back(Chain);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001941 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys,
1942 &ResultVals[0], ResultVals.size());
Evan Cheng32fe1032006-05-25 00:59:30 +00001943 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001944}
1945
1946SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1947 if (ReturnAddrIndex == 0) {
1948 // Set up a frame object for the return address.
1949 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng25ab6902006-09-08 06:48:29 +00001950 if (Subtarget->is64Bit())
1951 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1952 else
1953 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001954 }
1955
Evan Cheng25ab6902006-09-08 06:48:29 +00001956 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001957}
1958
1959
1960
Evan Cheng6dfa9992006-01-30 23:41:35 +00001961/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1962/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001963/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1964/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001965static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001966 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1967 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001968 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001969 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001970 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1971 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1972 // X > -1 -> X == 0, jump !sign.
1973 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001974 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001975 return true;
1976 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1977 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001978 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001979 return true;
1980 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001981 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001982
Evan Chengd9558e02006-01-06 00:43:03 +00001983 switch (SetCCOpcode) {
1984 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001985 case ISD::SETEQ: X86CC = X86::COND_E; break;
1986 case ISD::SETGT: X86CC = X86::COND_G; break;
1987 case ISD::SETGE: X86CC = X86::COND_GE; break;
1988 case ISD::SETLT: X86CC = X86::COND_L; break;
1989 case ISD::SETLE: X86CC = X86::COND_LE; break;
1990 case ISD::SETNE: X86CC = X86::COND_NE; break;
1991 case ISD::SETULT: X86CC = X86::COND_B; break;
1992 case ISD::SETUGT: X86CC = X86::COND_A; break;
1993 case ISD::SETULE: X86CC = X86::COND_BE; break;
1994 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001995 }
1996 } else {
1997 // On a floating point condition, the flags are set as follows:
1998 // ZF PF CF op
1999 // 0 | 0 | 0 | X > Y
2000 // 0 | 0 | 1 | X < Y
2001 // 1 | 0 | 0 | X == Y
2002 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00002003 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00002004 switch (SetCCOpcode) {
2005 default: break;
2006 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002007 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002008 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002009 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002010 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002011 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002012 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002013 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002014 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002015 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002016 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00002017 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00002018 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002019 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002020 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00002021 case ISD::SETNE: X86CC = X86::COND_NE; break;
2022 case ISD::SETUO: X86CC = X86::COND_P; break;
2023 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00002024 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002025 if (Flip)
2026 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00002027 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00002028
Chris Lattner7fbe9722006-10-20 17:42:20 +00002029 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002030}
2031
Evan Cheng4a460802006-01-11 00:33:36 +00002032/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2033/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002034/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002035static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002036 switch (X86CC) {
2037 default:
2038 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002039 case X86::COND_B:
2040 case X86::COND_BE:
2041 case X86::COND_E:
2042 case X86::COND_P:
2043 case X86::COND_A:
2044 case X86::COND_AE:
2045 case X86::COND_NE:
2046 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002047 return true;
2048 }
2049}
2050
Evan Cheng5ced1d82006-04-06 23:23:56 +00002051/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002052/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00002053static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
2054 if (Op.getOpcode() == ISD::UNDEF)
2055 return true;
2056
2057 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002058 return (Val >= Low && Val < Hi);
2059}
2060
2061/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2062/// true if Op is undef or if its value equal to the specified value.
2063static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2064 if (Op.getOpcode() == ISD::UNDEF)
2065 return true;
2066 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002067}
2068
Evan Cheng0188ecb2006-03-22 18:59:22 +00002069/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2070/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2071bool X86::isPSHUFDMask(SDNode *N) {
2072 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2073
2074 if (N->getNumOperands() != 4)
2075 return false;
2076
2077 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002078 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002079 SDOperand Arg = N->getOperand(i);
2080 if (Arg.getOpcode() == ISD::UNDEF) continue;
2081 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2082 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00002083 return false;
2084 }
2085
2086 return true;
2087}
2088
2089/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002090/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002091bool X86::isPSHUFHWMask(SDNode *N) {
2092 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2093
2094 if (N->getNumOperands() != 8)
2095 return false;
2096
2097 // Lower quadword copied in order.
2098 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002099 SDOperand Arg = N->getOperand(i);
2100 if (Arg.getOpcode() == ISD::UNDEF) continue;
2101 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2102 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002103 return false;
2104 }
2105
2106 // Upper quadword shuffled.
2107 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002108 SDOperand Arg = N->getOperand(i);
2109 if (Arg.getOpcode() == ISD::UNDEF) continue;
2110 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2111 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002112 if (Val < 4 || Val > 7)
2113 return false;
2114 }
2115
2116 return true;
2117}
2118
2119/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002120/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002121bool X86::isPSHUFLWMask(SDNode *N) {
2122 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2123
2124 if (N->getNumOperands() != 8)
2125 return false;
2126
2127 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002128 for (unsigned i = 4; i != 8; ++i)
2129 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002130 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002131
2132 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002133 for (unsigned i = 0; i != 4; ++i)
2134 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002135 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002136
2137 return true;
2138}
2139
Evan Cheng14aed5e2006-03-24 01:18:28 +00002140/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2141/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng39623da2006-04-20 08:58:49 +00002142static bool isSHUFPMask(std::vector<SDOperand> &N) {
2143 unsigned NumElems = N.size();
2144 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002145
Evan Cheng39623da2006-04-20 08:58:49 +00002146 unsigned Half = NumElems / 2;
2147 for (unsigned i = 0; i < Half; ++i)
2148 if (!isUndefOrInRange(N[i], 0, NumElems))
2149 return false;
2150 for (unsigned i = Half; i < NumElems; ++i)
2151 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
2152 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002153
2154 return true;
2155}
2156
Evan Cheng39623da2006-04-20 08:58:49 +00002157bool X86::isSHUFPMask(SDNode *N) {
2158 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2160 return ::isSHUFPMask(Ops);
2161}
2162
2163/// isCommutedSHUFP - Returns true if the shuffle mask is except
2164/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2165/// half elements to come from vector 1 (which would equal the dest.) and
2166/// the upper half to come from vector 2.
2167static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
2168 unsigned NumElems = Ops.size();
2169 if (NumElems != 2 && NumElems != 4) return false;
2170
2171 unsigned Half = NumElems / 2;
2172 for (unsigned i = 0; i < Half; ++i)
2173 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
2174 return false;
2175 for (unsigned i = Half; i < NumElems; ++i)
2176 if (!isUndefOrInRange(Ops[i], 0, NumElems))
2177 return false;
2178 return true;
2179}
2180
2181static bool isCommutedSHUFP(SDNode *N) {
2182 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2183 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2184 return isCommutedSHUFP(Ops);
2185}
2186
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002187/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2188/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2189bool X86::isMOVHLPSMask(SDNode *N) {
2190 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2191
Evan Cheng2064a2b2006-03-28 06:50:32 +00002192 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002193 return false;
2194
Evan Cheng2064a2b2006-03-28 06:50:32 +00002195 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002196 return isUndefOrEqual(N->getOperand(0), 6) &&
2197 isUndefOrEqual(N->getOperand(1), 7) &&
2198 isUndefOrEqual(N->getOperand(2), 2) &&
2199 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002200}
2201
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002202/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2203/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2204/// <2, 3, 2, 3>
2205bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2206 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2207
2208 if (N->getNumOperands() != 4)
2209 return false;
2210
2211 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2212 return isUndefOrEqual(N->getOperand(0), 2) &&
2213 isUndefOrEqual(N->getOperand(1), 3) &&
2214 isUndefOrEqual(N->getOperand(2), 2) &&
2215 isUndefOrEqual(N->getOperand(3), 3);
2216}
2217
Evan Cheng5ced1d82006-04-06 23:23:56 +00002218/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2219/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2220bool X86::isMOVLPMask(SDNode *N) {
2221 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2222
2223 unsigned NumElems = N->getNumOperands();
2224 if (NumElems != 2 && NumElems != 4)
2225 return false;
2226
Evan Chengc5cdff22006-04-07 21:53:05 +00002227 for (unsigned i = 0; i < NumElems/2; ++i)
2228 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2229 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002230
Evan Chengc5cdff22006-04-07 21:53:05 +00002231 for (unsigned i = NumElems/2; i < NumElems; ++i)
2232 if (!isUndefOrEqual(N->getOperand(i), i))
2233 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002234
2235 return true;
2236}
2237
2238/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002239/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2240/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002241bool X86::isMOVHPMask(SDNode *N) {
2242 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2243
2244 unsigned NumElems = N->getNumOperands();
2245 if (NumElems != 2 && NumElems != 4)
2246 return false;
2247
Evan Chengc5cdff22006-04-07 21:53:05 +00002248 for (unsigned i = 0; i < NumElems/2; ++i)
2249 if (!isUndefOrEqual(N->getOperand(i), i))
2250 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002251
2252 for (unsigned i = 0; i < NumElems/2; ++i) {
2253 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002254 if (!isUndefOrEqual(Arg, i + NumElems))
2255 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002256 }
2257
2258 return true;
2259}
2260
Evan Cheng0038e592006-03-28 00:39:58 +00002261/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2262/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng39623da2006-04-20 08:58:49 +00002263bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2264 unsigned NumElems = N.size();
Evan Cheng0038e592006-03-28 00:39:58 +00002265 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2266 return false;
2267
2268 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00002269 SDOperand BitI = N[i];
2270 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002271 if (!isUndefOrEqual(BitI, j))
2272 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002273 if (V2IsSplat) {
2274 if (isUndefOrEqual(BitI1, NumElems))
2275 return false;
2276 } else {
2277 if (!isUndefOrEqual(BitI1, j + NumElems))
2278 return false;
2279 }
Evan Cheng0038e592006-03-28 00:39:58 +00002280 }
2281
2282 return true;
2283}
2284
Evan Cheng39623da2006-04-20 08:58:49 +00002285bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2286 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2287 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2288 return ::isUNPCKLMask(Ops, V2IsSplat);
2289}
2290
Evan Cheng4fcb9222006-03-28 02:43:26 +00002291/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2292/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng39623da2006-04-20 08:58:49 +00002293bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
2294 unsigned NumElems = N.size();
Evan Cheng4fcb9222006-03-28 02:43:26 +00002295 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2296 return false;
2297
2298 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00002299 SDOperand BitI = N[i];
2300 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002301 if (!isUndefOrEqual(BitI, j + NumElems/2))
2302 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002303 if (V2IsSplat) {
2304 if (isUndefOrEqual(BitI1, NumElems))
2305 return false;
2306 } else {
2307 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
2308 return false;
2309 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002310 }
2311
2312 return true;
2313}
2314
Evan Cheng39623da2006-04-20 08:58:49 +00002315bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2316 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2317 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
2318 return ::isUNPCKHMask(Ops, V2IsSplat);
2319}
2320
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002321/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2322/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2323/// <0, 0, 1, 1>
2324bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2325 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2326
2327 unsigned NumElems = N->getNumOperands();
2328 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
2329 return false;
2330
2331 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2332 SDOperand BitI = N->getOperand(i);
2333 SDOperand BitI1 = N->getOperand(i+1);
2334
Evan Chengc5cdff22006-04-07 21:53:05 +00002335 if (!isUndefOrEqual(BitI, j))
2336 return false;
2337 if (!isUndefOrEqual(BitI1, j))
2338 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002339 }
2340
2341 return true;
2342}
2343
Evan Cheng017dcc62006-04-21 01:05:10 +00002344/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2345/// specifies a shuffle of elements that is suitable for input to MOVSS,
2346/// MOVSD, and MOVD, i.e. setting the lowest element.
2347static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002348 unsigned NumElems = N.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00002349 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002350 return false;
2351
Evan Cheng39623da2006-04-20 08:58:49 +00002352 if (!isUndefOrEqual(N[0], NumElems))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002353 return false;
2354
2355 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002356 SDOperand Arg = N[i];
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002357 if (!isUndefOrEqual(Arg, i))
2358 return false;
2359 }
2360
2361 return true;
2362}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002363
Evan Cheng017dcc62006-04-21 01:05:10 +00002364bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002365 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2366 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00002367 return ::isMOVLMask(Ops);
Evan Cheng39623da2006-04-20 08:58:49 +00002368}
2369
Evan Cheng017dcc62006-04-21 01:05:10 +00002370/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2371/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002372/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Cheng8cf723d2006-09-08 01:50:06 +00002373static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false,
2374 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002375 unsigned NumElems = Ops.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00002376 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002377 return false;
2378
2379 if (!isUndefOrEqual(Ops[0], 0))
2380 return false;
2381
2382 for (unsigned i = 1; i < NumElems; ++i) {
2383 SDOperand Arg = Ops[i];
Evan Cheng8cf723d2006-09-08 01:50:06 +00002384 if (!(isUndefOrEqual(Arg, i+NumElems) ||
2385 (V2IsUndef && isUndefOrInRange(Arg, NumElems, NumElems*2)) ||
2386 (V2IsSplat && isUndefOrEqual(Arg, NumElems))))
2387 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002388 }
2389
2390 return true;
2391}
2392
Evan Cheng8cf723d2006-09-08 01:50:06 +00002393static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2394 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002395 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2396 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng8cf723d2006-09-08 01:50:06 +00002397 return isCommutedMOVL(Ops, V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002398}
2399
Evan Chengd9539472006-04-14 21:59:03 +00002400/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2401/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2402bool X86::isMOVSHDUPMask(SDNode *N) {
2403 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2404
2405 if (N->getNumOperands() != 4)
2406 return false;
2407
2408 // Expect 1, 1, 3, 3
2409 for (unsigned i = 0; i < 2; ++i) {
2410 SDOperand Arg = N->getOperand(i);
2411 if (Arg.getOpcode() == ISD::UNDEF) continue;
2412 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2413 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2414 if (Val != 1) return false;
2415 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002416
2417 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002418 for (unsigned i = 2; i < 4; ++i) {
2419 SDOperand Arg = N->getOperand(i);
2420 if (Arg.getOpcode() == ISD::UNDEF) continue;
2421 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2422 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2423 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002424 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002425 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002426
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002427 // Don't use movshdup if it can be done with a shufps.
2428 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002429}
2430
2431/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2432/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2433bool X86::isMOVSLDUPMask(SDNode *N) {
2434 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2435
2436 if (N->getNumOperands() != 4)
2437 return false;
2438
2439 // Expect 0, 0, 2, 2
2440 for (unsigned i = 0; i < 2; ++i) {
2441 SDOperand Arg = N->getOperand(i);
2442 if (Arg.getOpcode() == ISD::UNDEF) continue;
2443 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2444 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2445 if (Val != 0) return false;
2446 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002447
2448 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002449 for (unsigned i = 2; i < 4; ++i) {
2450 SDOperand Arg = N->getOperand(i);
2451 if (Arg.getOpcode() == ISD::UNDEF) continue;
2452 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2453 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2454 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002455 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002456 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002457
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002458 // Don't use movshdup if it can be done with a shufps.
2459 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002460}
2461
Evan Chengb9df0ca2006-03-22 02:53:00 +00002462/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2463/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002464static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002465 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2466
Evan Chengb9df0ca2006-03-22 02:53:00 +00002467 // This is a splat operation if each element of the permute is the same, and
2468 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002469 unsigned NumElems = N->getNumOperands();
2470 SDOperand ElementBase;
2471 unsigned i = 0;
2472 for (; i != NumElems; ++i) {
2473 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002474 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002475 ElementBase = Elt;
2476 break;
2477 }
2478 }
2479
2480 if (!ElementBase.Val)
2481 return false;
2482
2483 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002484 SDOperand Arg = N->getOperand(i);
2485 if (Arg.getOpcode() == ISD::UNDEF) continue;
2486 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002487 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002488 }
2489
2490 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002491 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002492}
2493
Evan Chengc575ca22006-04-17 20:43:08 +00002494/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2495/// a splat of a single element and it's a 2 or 4 element mask.
2496bool X86::isSplatMask(SDNode *N) {
2497 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2498
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002499 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002500 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2501 return false;
2502 return ::isSplatMask(N);
2503}
2504
Evan Chengf686d9b2006-10-27 21:08:32 +00002505/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2506/// specifies a splat of zero element.
2507bool X86::isSplatLoMask(SDNode *N) {
2508 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2509
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002510 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002511 if (!isUndefOrEqual(N->getOperand(i), 0))
2512 return false;
2513 return true;
2514}
2515
Evan Cheng63d33002006-03-22 08:01:21 +00002516/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2517/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2518/// instructions.
2519unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002520 unsigned NumOperands = N->getNumOperands();
2521 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2522 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002523 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002524 unsigned Val = 0;
2525 SDOperand Arg = N->getOperand(NumOperands-i-1);
2526 if (Arg.getOpcode() != ISD::UNDEF)
2527 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002528 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002529 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002530 if (i != NumOperands - 1)
2531 Mask <<= Shift;
2532 }
Evan Cheng63d33002006-03-22 08:01:21 +00002533
2534 return Mask;
2535}
2536
Evan Cheng506d3df2006-03-29 23:07:14 +00002537/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2538/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2539/// instructions.
2540unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2541 unsigned Mask = 0;
2542 // 8 nodes, but we only care about the last 4.
2543 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002544 unsigned Val = 0;
2545 SDOperand Arg = N->getOperand(i);
2546 if (Arg.getOpcode() != ISD::UNDEF)
2547 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002548 Mask |= (Val - 4);
2549 if (i != 4)
2550 Mask <<= 2;
2551 }
2552
2553 return Mask;
2554}
2555
2556/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2557/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2558/// instructions.
2559unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2560 unsigned Mask = 0;
2561 // 8 nodes, but we only care about the first 4.
2562 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002563 unsigned Val = 0;
2564 SDOperand Arg = N->getOperand(i);
2565 if (Arg.getOpcode() != ISD::UNDEF)
2566 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002567 Mask |= Val;
2568 if (i != 0)
2569 Mask <<= 2;
2570 }
2571
2572 return Mask;
2573}
2574
Evan Chengc21a0532006-04-05 01:47:37 +00002575/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2576/// specifies a 8 element shuffle that can be broken into a pair of
2577/// PSHUFHW and PSHUFLW.
2578static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2579 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2580
2581 if (N->getNumOperands() != 8)
2582 return false;
2583
2584 // Lower quadword shuffled.
2585 for (unsigned i = 0; i != 4; ++i) {
2586 SDOperand Arg = N->getOperand(i);
2587 if (Arg.getOpcode() == ISD::UNDEF) continue;
2588 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2589 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2590 if (Val > 4)
2591 return false;
2592 }
2593
2594 // Upper quadword shuffled.
2595 for (unsigned i = 4; i != 8; ++i) {
2596 SDOperand Arg = N->getOperand(i);
2597 if (Arg.getOpcode() == ISD::UNDEF) continue;
2598 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2599 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2600 if (Val < 4 || Val > 7)
2601 return false;
2602 }
2603
2604 return true;
2605}
2606
Evan Cheng5ced1d82006-04-06 23:23:56 +00002607/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2608/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002609static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2610 SDOperand &V2, SDOperand &Mask,
2611 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002612 MVT::ValueType VT = Op.getValueType();
2613 MVT::ValueType MaskVT = Mask.getValueType();
2614 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2615 unsigned NumElems = Mask.getNumOperands();
2616 std::vector<SDOperand> MaskVec;
2617
2618 for (unsigned i = 0; i != NumElems; ++i) {
2619 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002620 if (Arg.getOpcode() == ISD::UNDEF) {
2621 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2622 continue;
2623 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002624 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2625 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2626 if (Val < NumElems)
2627 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2628 else
2629 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2630 }
2631
Evan Cheng9eca5e82006-10-25 21:49:50 +00002632 std::swap(V1, V2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002633 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng9eca5e82006-10-25 21:49:50 +00002634 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002635}
2636
Evan Cheng533a0aa2006-04-19 20:35:22 +00002637/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2638/// match movhlps. The lower half elements should come from upper half of
2639/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002640/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002641static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2642 unsigned NumElems = Mask->getNumOperands();
2643 if (NumElems != 4)
2644 return false;
2645 for (unsigned i = 0, e = 2; i != e; ++i)
2646 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2647 return false;
2648 for (unsigned i = 2; i != 4; ++i)
2649 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2650 return false;
2651 return true;
2652}
2653
Evan Cheng5ced1d82006-04-06 23:23:56 +00002654/// isScalarLoadToVector - Returns true if the node is a scalar load that
2655/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002656static inline bool isScalarLoadToVector(SDNode *N) {
2657 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2658 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002659 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002660 }
2661 return false;
2662}
2663
Evan Cheng533a0aa2006-04-19 20:35:22 +00002664/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2665/// match movlp{s|d}. The lower half elements should come from lower half of
2666/// V1 (and in order), and the upper half elements should come from the upper
2667/// half of V2 (and in order). And since V1 will become the source of the
2668/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002669static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002670 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002671 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002672 // Is V2 is a vector load, don't do this transformation. We will try to use
2673 // load folding shufps op.
2674 if (ISD::isNON_EXTLoad(V2))
2675 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002676
Evan Cheng533a0aa2006-04-19 20:35:22 +00002677 unsigned NumElems = Mask->getNumOperands();
2678 if (NumElems != 2 && NumElems != 4)
2679 return false;
2680 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2681 if (!isUndefOrEqual(Mask->getOperand(i), i))
2682 return false;
2683 for (unsigned i = NumElems/2; i != NumElems; ++i)
2684 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2685 return false;
2686 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002687}
2688
Evan Cheng39623da2006-04-20 08:58:49 +00002689/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2690/// all the same.
2691static bool isSplatVector(SDNode *N) {
2692 if (N->getOpcode() != ISD::BUILD_VECTOR)
2693 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002694
Evan Cheng39623da2006-04-20 08:58:49 +00002695 SDOperand SplatValue = N->getOperand(0);
2696 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2697 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002698 return false;
2699 return true;
2700}
2701
Evan Cheng8cf723d2006-09-08 01:50:06 +00002702/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2703/// to an undef.
2704static bool isUndefShuffle(SDNode *N) {
2705 if (N->getOpcode() != ISD::BUILD_VECTOR)
2706 return false;
2707
2708 SDOperand V1 = N->getOperand(0);
2709 SDOperand V2 = N->getOperand(1);
2710 SDOperand Mask = N->getOperand(2);
2711 unsigned NumElems = Mask.getNumOperands();
2712 for (unsigned i = 0; i != NumElems; ++i) {
2713 SDOperand Arg = Mask.getOperand(i);
2714 if (Arg.getOpcode() != ISD::UNDEF) {
2715 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2716 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2717 return false;
2718 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2719 return false;
2720 }
2721 }
2722 return true;
2723}
2724
Evan Cheng39623da2006-04-20 08:58:49 +00002725/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2726/// that point to V2 points to its first element.
2727static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2728 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2729
2730 bool Changed = false;
2731 std::vector<SDOperand> MaskVec;
2732 unsigned NumElems = Mask.getNumOperands();
2733 for (unsigned i = 0; i != NumElems; ++i) {
2734 SDOperand Arg = Mask.getOperand(i);
2735 if (Arg.getOpcode() != ISD::UNDEF) {
2736 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2737 if (Val > NumElems) {
2738 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2739 Changed = true;
2740 }
2741 }
2742 MaskVec.push_back(Arg);
2743 }
2744
2745 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002746 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2747 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002748 return Mask;
2749}
2750
Evan Cheng017dcc62006-04-21 01:05:10 +00002751/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2752/// operation of specified width.
2753static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002754 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2755 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2756
2757 std::vector<SDOperand> MaskVec;
2758 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2759 for (unsigned i = 1; i != NumElems; ++i)
2760 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002761 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002762}
2763
Evan Chengc575ca22006-04-17 20:43:08 +00002764/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2765/// of specified width.
2766static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2767 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2768 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2769 std::vector<SDOperand> MaskVec;
2770 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2771 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2772 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2773 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002774 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002775}
2776
Evan Cheng39623da2006-04-20 08:58:49 +00002777/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2778/// of specified width.
2779static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2780 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2781 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2782 unsigned Half = NumElems/2;
2783 std::vector<SDOperand> MaskVec;
2784 for (unsigned i = 0; i != Half; ++i) {
2785 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2786 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2787 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002788 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002789}
2790
Evan Cheng017dcc62006-04-21 01:05:10 +00002791/// getZeroVector - Returns a vector of specified type with all zero elements.
2792///
2793static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2794 assert(MVT::isVector(VT) && "Expected a vector type");
2795 unsigned NumElems = getVectorNumElements(VT);
2796 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2797 bool isFP = MVT::isFloatingPoint(EVT);
2798 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2799 std::vector<SDOperand> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002800 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
Evan Cheng017dcc62006-04-21 01:05:10 +00002801}
2802
Evan Chengc575ca22006-04-17 20:43:08 +00002803/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2804///
2805static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2806 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002807 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002808 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002809 unsigned NumElems = Mask.getNumOperands();
2810 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002811 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002812 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002813 NumElems >>= 1;
2814 }
2815 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2816
2817 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002818 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002819 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002820 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002821 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2822}
2823
Evan Cheng017dcc62006-04-21 01:05:10 +00002824/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2825/// constant +0.0.
2826static inline bool isZeroNode(SDOperand Elt) {
2827 return ((isa<ConstantSDNode>(Elt) &&
2828 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2829 (isa<ConstantFPSDNode>(Elt) &&
2830 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2831}
2832
Evan Chengba05f722006-04-21 23:03:30 +00002833/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2834/// vector and zero or undef vector.
2835static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002836 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002837 bool isZero, SelectionDAG &DAG) {
2838 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002839 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2840 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2841 SDOperand Zero = DAG.getConstant(0, EVT);
2842 std::vector<SDOperand> MaskVec(NumElems, Zero);
2843 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002844 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2845 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002846 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002847}
2848
Evan Chengc78d3b42006-04-24 18:01:45 +00002849/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2850///
2851static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2852 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002853 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002854 if (NumNonZero > 8)
2855 return SDOperand();
2856
2857 SDOperand V(0, 0);
2858 bool First = true;
2859 for (unsigned i = 0; i < 16; ++i) {
2860 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2861 if (ThisIsNonZero && First) {
2862 if (NumZero)
2863 V = getZeroVector(MVT::v8i16, DAG);
2864 else
2865 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2866 First = false;
2867 }
2868
2869 if ((i & 1) != 0) {
2870 SDOperand ThisElt(0, 0), LastElt(0, 0);
2871 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2872 if (LastIsNonZero) {
2873 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2874 }
2875 if (ThisIsNonZero) {
2876 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2877 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2878 ThisElt, DAG.getConstant(8, MVT::i8));
2879 if (LastIsNonZero)
2880 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2881 } else
2882 ThisElt = LastElt;
2883
2884 if (ThisElt.Val)
2885 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00002886 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002887 }
2888 }
2889
2890 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2891}
2892
2893/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2894///
2895static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2896 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002897 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002898 if (NumNonZero > 4)
2899 return SDOperand();
2900
2901 SDOperand V(0, 0);
2902 bool First = true;
2903 for (unsigned i = 0; i < 8; ++i) {
2904 bool isNonZero = (NonZeros & (1 << i)) != 0;
2905 if (isNonZero) {
2906 if (First) {
2907 if (NumZero)
2908 V = getZeroVector(MVT::v8i16, DAG);
2909 else
2910 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2911 First = false;
2912 }
2913 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00002914 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002915 }
2916 }
2917
2918 return V;
2919}
2920
Evan Cheng0db9fe62006-04-25 20:13:52 +00002921SDOperand
2922X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2923 // All zero's are handled with pxor.
2924 if (ISD::isBuildVectorAllZeros(Op.Val))
2925 return Op;
2926
2927 // All one's are handled with pcmpeqd.
2928 if (ISD::isBuildVectorAllOnes(Op.Val))
2929 return Op;
2930
2931 MVT::ValueType VT = Op.getValueType();
2932 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2933 unsigned EVTBits = MVT::getSizeInBits(EVT);
2934
2935 unsigned NumElems = Op.getNumOperands();
2936 unsigned NumZero = 0;
2937 unsigned NumNonZero = 0;
2938 unsigned NonZeros = 0;
2939 std::set<SDOperand> Values;
2940 for (unsigned i = 0; i < NumElems; ++i) {
2941 SDOperand Elt = Op.getOperand(i);
2942 if (Elt.getOpcode() != ISD::UNDEF) {
2943 Values.insert(Elt);
2944 if (isZeroNode(Elt))
2945 NumZero++;
2946 else {
2947 NonZeros |= (1 << i);
2948 NumNonZero++;
2949 }
2950 }
2951 }
2952
2953 if (NumNonZero == 0)
2954 // Must be a mix of zero and undef. Return a zero vector.
2955 return getZeroVector(VT, DAG);
2956
2957 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2958 if (Values.size() == 1)
2959 return SDOperand();
2960
2961 // Special case for single non-zero element.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002962 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002963 unsigned Idx = CountTrailingZeros_32(NonZeros);
2964 SDOperand Item = Op.getOperand(Idx);
2965 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2966 if (Idx == 0)
2967 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2968 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2969 NumZero > 0, DAG);
2970
2971 if (EVTBits == 32) {
2972 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2973 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2974 DAG);
2975 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2976 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2977 std::vector<SDOperand> MaskVec;
2978 for (unsigned i = 0; i < NumElems; i++)
2979 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002980 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2981 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002982 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2983 DAG.getNode(ISD::UNDEF, VT), Mask);
2984 }
2985 }
2986
Evan Chenge1113032006-10-04 18:33:38 +00002987 // Let legalizer expand 2-wide build_vector's.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002988 if (EVTBits == 64)
2989 return SDOperand();
2990
2991 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2992 if (EVTBits == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002993 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2994 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002995 if (V.Val) return V;
2996 }
2997
2998 if (EVTBits == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002999 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3000 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003001 if (V.Val) return V;
3002 }
3003
3004 // If element VT is == 32 bits, turn it into a number of shuffles.
3005 std::vector<SDOperand> V(NumElems);
3006 if (NumElems == 4 && NumZero > 0) {
3007 for (unsigned i = 0; i < 4; ++i) {
3008 bool isZero = !(NonZeros & (1 << i));
3009 if (isZero)
3010 V[i] = getZeroVector(VT, DAG);
3011 else
3012 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3013 }
3014
3015 for (unsigned i = 0; i < 2; ++i) {
3016 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3017 default: break;
3018 case 0:
3019 V[i] = V[i*2]; // Must be a zero vector.
3020 break;
3021 case 1:
3022 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3023 getMOVLMask(NumElems, DAG));
3024 break;
3025 case 2:
3026 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3027 getMOVLMask(NumElems, DAG));
3028 break;
3029 case 3:
3030 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3031 getUnpacklMask(NumElems, DAG));
3032 break;
3033 }
3034 }
3035
Evan Cheng069287d2006-05-16 07:21:53 +00003036 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003037 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003038 // FIXME: we can do the same for v4f32 case when we know both parts of
3039 // the lower half come from scalar_to_vector (loadf32). We should do
3040 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003041 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003042 return V[0];
3043 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3044 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
3045 std::vector<SDOperand> MaskVec;
3046 bool Reverse = (NonZeros & 0x3) == 2;
3047 for (unsigned i = 0; i < 2; ++i)
3048 if (Reverse)
3049 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3050 else
3051 MaskVec.push_back(DAG.getConstant(i, EVT));
3052 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3053 for (unsigned i = 0; i < 2; ++i)
3054 if (Reverse)
3055 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3056 else
3057 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003058 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3059 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003060 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3061 }
3062
3063 if (Values.size() > 2) {
3064 // Expand into a number of unpckl*.
3065 // e.g. for v4f32
3066 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3067 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3068 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3069 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3070 for (unsigned i = 0; i < NumElems; ++i)
3071 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3072 NumElems >>= 1;
3073 while (NumElems != 0) {
3074 for (unsigned i = 0; i < NumElems; ++i)
3075 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3076 UnpckMask);
3077 NumElems >>= 1;
3078 }
3079 return V[0];
3080 }
3081
3082 return SDOperand();
3083}
3084
3085SDOperand
3086X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3087 SDOperand V1 = Op.getOperand(0);
3088 SDOperand V2 = Op.getOperand(1);
3089 SDOperand PermMask = Op.getOperand(2);
3090 MVT::ValueType VT = Op.getValueType();
3091 unsigned NumElems = PermMask.getNumOperands();
3092 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3093 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003094 bool V1IsSplat = false;
3095 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003096
Evan Cheng8cf723d2006-09-08 01:50:06 +00003097 if (isUndefShuffle(Op.Val))
3098 return DAG.getNode(ISD::UNDEF, VT);
3099
Evan Cheng0db9fe62006-04-25 20:13:52 +00003100 if (isSplatMask(PermMask.Val)) {
3101 if (NumElems <= 4) return Op;
3102 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003103 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003104 }
3105
Evan Cheng9bbbb982006-10-25 20:48:19 +00003106 if (X86::isMOVLMask(PermMask.Val))
3107 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003108
Evan Cheng9bbbb982006-10-25 20:48:19 +00003109 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3110 X86::isMOVSLDUPMask(PermMask.Val) ||
3111 X86::isMOVHLPSMask(PermMask.Val) ||
3112 X86::isMOVHPMask(PermMask.Val) ||
3113 X86::isMOVLPMask(PermMask.Val))
3114 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003115
Evan Cheng9bbbb982006-10-25 20:48:19 +00003116 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3117 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003118 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003119
Evan Cheng9eca5e82006-10-25 21:49:50 +00003120 bool Commuted = false;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003121 V1IsSplat = isSplatVector(V1.Val);
3122 V2IsSplat = isSplatVector(V2.Val);
3123 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003124 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003125 std::swap(V1IsSplat, V2IsSplat);
3126 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003127 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003128 }
3129
3130 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3131 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003132 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003133 if (V2IsSplat) {
3134 // V2 is a splat, so the mask may be malformed. That is, it may point
3135 // to any V2 element. The instruction selectior won't like this. Get
3136 // a corrected mask and commute to form a proper MOVS{S|D}.
3137 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3138 if (NewMask.Val != PermMask.Val)
3139 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003140 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003141 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003142 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003143
Evan Chengd9b8e402006-10-16 06:36:00 +00003144 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3145 X86::isUNPCKLMask(PermMask.Val) ||
3146 X86::isUNPCKHMask(PermMask.Val))
3147 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003148
Evan Cheng9bbbb982006-10-25 20:48:19 +00003149 if (V2IsSplat) {
3150 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003151 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003152 // new vector_shuffle with the corrected mask.
3153 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3154 if (NewMask.Val != PermMask.Val) {
3155 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3156 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3157 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3158 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3159 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3160 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003161 }
3162 }
3163 }
3164
3165 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003166 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3167 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3168
3169 if (Commuted) {
3170 // Commute is back and try unpck* again.
3171 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3172 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
3173 X86::isUNPCKLMask(PermMask.Val) ||
3174 X86::isUNPCKHMask(PermMask.Val))
3175 return Op;
3176 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003177
3178 // If VT is integer, try PSHUF* first, then SHUFP*.
3179 if (MVT::isInteger(VT)) {
3180 if (X86::isPSHUFDMask(PermMask.Val) ||
3181 X86::isPSHUFHWMask(PermMask.Val) ||
3182 X86::isPSHUFLWMask(PermMask.Val)) {
3183 if (V2.getOpcode() != ISD::UNDEF)
3184 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3185 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3186 return Op;
3187 }
3188
3189 if (X86::isSHUFPMask(PermMask.Val))
3190 return Op;
3191
3192 // Handle v8i16 shuffle high / low shuffle node pair.
3193 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
3194 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3195 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3196 std::vector<SDOperand> MaskVec;
3197 for (unsigned i = 0; i != 4; ++i)
3198 MaskVec.push_back(PermMask.getOperand(i));
3199 for (unsigned i = 4; i != 8; ++i)
3200 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003201 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3202 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3204 MaskVec.clear();
3205 for (unsigned i = 0; i != 4; ++i)
3206 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3207 for (unsigned i = 4; i != 8; ++i)
3208 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00003209 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003210 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
3211 }
3212 } else {
3213 // Floating point cases in the other order.
3214 if (X86::isSHUFPMask(PermMask.Val))
3215 return Op;
3216 if (X86::isPSHUFDMask(PermMask.Val) ||
3217 X86::isPSHUFHWMask(PermMask.Val) ||
3218 X86::isPSHUFLWMask(PermMask.Val)) {
3219 if (V2.getOpcode() != ISD::UNDEF)
3220 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3221 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3222 return Op;
3223 }
3224 }
3225
3226 if (NumElems == 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003227 MVT::ValueType MaskVT = PermMask.getValueType();
3228 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng43f3bd32006-04-28 07:03:38 +00003229 std::vector<std::pair<int, int> > Locs;
3230 Locs.reserve(NumElems);
3231 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3232 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3233 unsigned NumHi = 0;
3234 unsigned NumLo = 0;
3235 // If no more than two elements come from either vector. This can be
3236 // implemented with two shuffles. First shuffle gather the elements.
3237 // The second shuffle, which takes the first shuffle as both of its
3238 // vector operands, put the elements into the right order.
3239 for (unsigned i = 0; i != NumElems; ++i) {
3240 SDOperand Elt = PermMask.getOperand(i);
3241 if (Elt.getOpcode() == ISD::UNDEF) {
3242 Locs[i] = std::make_pair(-1, -1);
3243 } else {
3244 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3245 if (Val < NumElems) {
3246 Locs[i] = std::make_pair(0, NumLo);
3247 Mask1[NumLo] = Elt;
3248 NumLo++;
3249 } else {
3250 Locs[i] = std::make_pair(1, NumHi);
3251 if (2+NumHi < NumElems)
3252 Mask1[2+NumHi] = Elt;
3253 NumHi++;
3254 }
3255 }
3256 }
3257 if (NumLo <= 2 && NumHi <= 2) {
3258 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003259 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3260 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003261 for (unsigned i = 0; i != NumElems; ++i) {
3262 if (Locs[i].first == -1)
3263 continue;
3264 else {
3265 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3266 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3267 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3268 }
3269 }
3270
3271 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003272 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3273 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003274 }
3275
3276 // Break it into (shuffle shuffle_hi, shuffle_lo).
3277 Locs.clear();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003278 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3279 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3280 std::vector<SDOperand> *MaskPtr = &LoMask;
3281 unsigned MaskIdx = 0;
3282 unsigned LoIdx = 0;
3283 unsigned HiIdx = NumElems/2;
3284 for (unsigned i = 0; i != NumElems; ++i) {
3285 if (i == NumElems/2) {
3286 MaskPtr = &HiMask;
3287 MaskIdx = 1;
3288 LoIdx = 0;
3289 HiIdx = NumElems/2;
3290 }
3291 SDOperand Elt = PermMask.getOperand(i);
3292 if (Elt.getOpcode() == ISD::UNDEF) {
3293 Locs[i] = std::make_pair(-1, -1);
3294 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3295 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3296 (*MaskPtr)[LoIdx] = Elt;
3297 LoIdx++;
3298 } else {
3299 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3300 (*MaskPtr)[HiIdx] = Elt;
3301 HiIdx++;
3302 }
3303 }
3304
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003305 SDOperand LoShuffle =
3306 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003307 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3308 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003309 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003310 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003311 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3312 &HiMask[0], HiMask.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003313 std::vector<SDOperand> MaskOps;
3314 for (unsigned i = 0; i != NumElems; ++i) {
3315 if (Locs[i].first == -1) {
3316 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3317 } else {
3318 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3319 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3320 }
3321 }
3322 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003323 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3324 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003325 }
3326
3327 return SDOperand();
3328}
3329
3330SDOperand
3331X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3332 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3333 return SDOperand();
3334
3335 MVT::ValueType VT = Op.getValueType();
3336 // TODO: handle v16i8.
3337 if (MVT::getSizeInBits(VT) == 16) {
3338 // Transform it so it match pextrw which produces a 32-bit result.
3339 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3340 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3341 Op.getOperand(0), Op.getOperand(1));
3342 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3343 DAG.getValueType(VT));
3344 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3345 } else if (MVT::getSizeInBits(VT) == 32) {
3346 SDOperand Vec = Op.getOperand(0);
3347 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3348 if (Idx == 0)
3349 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003350 // SHUFPS the element to the lowest double word, then movss.
3351 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003352 std::vector<SDOperand> IdxVec;
3353 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
3354 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3355 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
3356 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003357 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3358 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003359 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003360 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003361 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003362 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003363 } else if (MVT::getSizeInBits(VT) == 64) {
3364 SDOperand Vec = Op.getOperand(0);
3365 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3366 if (Idx == 0)
3367 return Op;
3368
3369 // UNPCKHPD the element to the lowest double word, then movsd.
3370 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3371 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3372 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3373 std::vector<SDOperand> IdxVec;
3374 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
3375 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003376 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3377 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003378 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3379 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3380 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003381 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003382 }
3383
3384 return SDOperand();
3385}
3386
3387SDOperand
3388X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00003389 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00003390 // as its second argument.
3391 MVT::ValueType VT = Op.getValueType();
3392 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
3393 SDOperand N0 = Op.getOperand(0);
3394 SDOperand N1 = Op.getOperand(1);
3395 SDOperand N2 = Op.getOperand(2);
3396 if (MVT::getSizeInBits(BaseVT) == 16) {
3397 if (N1.getValueType() != MVT::i32)
3398 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3399 if (N2.getValueType() != MVT::i32)
3400 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
3401 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3402 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3403 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3404 if (Idx == 0) {
3405 // Use a movss.
3406 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3407 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
3408 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
3409 std::vector<SDOperand> MaskVec;
3410 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3411 for (unsigned i = 1; i <= 3; ++i)
3412 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3413 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00003414 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3415 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003416 } else {
3417 // Use two pinsrw instructions to insert a 32 bit value.
3418 Idx <<= 1;
3419 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng466685d2006-10-09 20:57:25 +00003420 if (ISD::isNON_EXTLoad(N1.Val)) {
Evan Cheng069287d2006-05-16 07:21:53 +00003421 // Just load directly from f32mem to GR32.
Evan Cheng466685d2006-10-09 20:57:25 +00003422 LoadSDNode *LD = cast<LoadSDNode>(N1);
3423 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
3424 LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003425 } else {
3426 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3427 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3428 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003429 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003430 }
3431 }
3432 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3433 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003434 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003435 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3436 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003437 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003438 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3439 }
3440 }
3441
3442 return SDOperand();
3443}
3444
3445SDOperand
3446X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3447 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3448 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3449}
3450
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003451// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003452// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3453// one of the above mentioned nodes. It has to be wrapped because otherwise
3454// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3455// be used to form addressing mode. These wrapped nodes will be selected
3456// into MOV32ri.
3457SDOperand
3458X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3459 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003460 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3461 getPointerTy(),
3462 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003463 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003464 // With PIC, the address is actually $g + Offset.
3465 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3466 !Subtarget->isPICStyleRIPRel()) {
3467 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3468 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3469 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003470 }
3471
3472 return Result;
3473}
3474
3475SDOperand
3476X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3477 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003478 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003479 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003480 // With PIC, the address is actually $g + Offset.
3481 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3482 !Subtarget->isPICStyleRIPRel()) {
3483 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3484 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3485 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003486 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003487
3488 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3489 // load the value at address GV, not the value of GV itself. This means that
3490 // the GlobalAddress must be in the base or index register of the address, not
3491 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003492 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003493 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3494 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003495
3496 return Result;
3497}
3498
3499SDOperand
3500X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3501 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003502 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003503 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003504 // With PIC, the address is actually $g + Offset.
3505 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3506 !Subtarget->isPICStyleRIPRel()) {
3507 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3508 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3509 Result);
3510 }
3511
3512 return Result;
3513}
3514
3515SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3516 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3517 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3518 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3519 // With PIC, the address is actually $g + Offset.
3520 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3521 !Subtarget->isPICStyleRIPRel()) {
3522 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3523 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3524 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003525 }
3526
3527 return Result;
3528}
3529
3530SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00003531 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3532 "Not an i64 shift!");
3533 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3534 SDOperand ShOpLo = Op.getOperand(0);
3535 SDOperand ShOpHi = Op.getOperand(1);
3536 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng734503b2006-09-11 02:19:56 +00003537 SDOperand Tmp1 = isSRA ?
3538 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3539 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003540
3541 SDOperand Tmp2, Tmp3;
3542 if (Op.getOpcode() == ISD::SHL_PARTS) {
3543 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3544 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3545 } else {
3546 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00003547 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00003548 }
3549
Evan Cheng734503b2006-09-11 02:19:56 +00003550 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3551 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3552 DAG.getConstant(32, MVT::i8));
3553 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3554 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Chenge3413162006-01-09 18:33:28 +00003555
3556 SDOperand Hi, Lo;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003557 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00003558
Evan Cheng734503b2006-09-11 02:19:56 +00003559 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3560 SmallVector<SDOperand, 4> Ops;
Evan Chenge3413162006-01-09 18:33:28 +00003561 if (Op.getOpcode() == ISD::SHL_PARTS) {
3562 Ops.push_back(Tmp2);
3563 Ops.push_back(Tmp3);
3564 Ops.push_back(CC);
3565 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003566 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003567 InFlag = Hi.getValue(1);
3568
3569 Ops.clear();
3570 Ops.push_back(Tmp3);
3571 Ops.push_back(Tmp1);
3572 Ops.push_back(CC);
3573 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003574 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003575 } else {
3576 Ops.push_back(Tmp2);
3577 Ops.push_back(Tmp3);
3578 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00003579 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003580 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003581 InFlag = Lo.getValue(1);
3582
3583 Ops.clear();
3584 Ops.push_back(Tmp3);
3585 Ops.push_back(Tmp1);
3586 Ops.push_back(CC);
3587 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003588 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003589 }
3590
Evan Cheng734503b2006-09-11 02:19:56 +00003591 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003592 Ops.clear();
3593 Ops.push_back(Lo);
3594 Ops.push_back(Hi);
Evan Cheng734503b2006-09-11 02:19:56 +00003595 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003596}
Evan Chenga3195e82006-01-12 22:54:21 +00003597
Evan Cheng0db9fe62006-04-25 20:13:52 +00003598SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3599 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3600 Op.getOperand(0).getValueType() >= MVT::i16 &&
3601 "Unknown SINT_TO_FP to lower!");
3602
3603 SDOperand Result;
3604 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3605 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3606 MachineFunction &MF = DAG.getMachineFunction();
3607 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3608 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003609 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003610 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003611
3612 // Build the FILD
3613 std::vector<MVT::ValueType> Tys;
3614 Tys.push_back(MVT::f64);
3615 Tys.push_back(MVT::Other);
3616 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3617 std::vector<SDOperand> Ops;
3618 Ops.push_back(Chain);
3619 Ops.push_back(StackSlot);
3620 Ops.push_back(DAG.getValueType(SrcVT));
3621 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003622 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003623
3624 if (X86ScalarSSE) {
3625 Chain = Result.getValue(1);
3626 SDOperand InFlag = Result.getValue(2);
3627
3628 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3629 // shouldn't be necessary except that RFP cannot be live across
3630 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003631 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003632 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003633 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00003634 std::vector<MVT::ValueType> Tys;
Evan Cheng6dab0532006-01-30 08:02:57 +00003635 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003636 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003637 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003638 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003639 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003640 Ops.push_back(DAG.getValueType(Op.getValueType()));
3641 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003642 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00003643 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003644 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003645
Evan Cheng0db9fe62006-04-25 20:13:52 +00003646 return Result;
3647}
3648
3649SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3650 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3651 "Unknown FP_TO_SINT to lower!");
3652 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3653 // stack slot.
3654 MachineFunction &MF = DAG.getMachineFunction();
3655 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3656 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3657 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3658
3659 unsigned Opc;
3660 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003661 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3662 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3663 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3664 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003665 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003666
Evan Cheng0db9fe62006-04-25 20:13:52 +00003667 SDOperand Chain = DAG.getEntryNode();
3668 SDOperand Value = Op.getOperand(0);
3669 if (X86ScalarSSE) {
3670 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003671 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003672 std::vector<MVT::ValueType> Tys;
3673 Tys.push_back(MVT::f64);
3674 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003675 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00003676 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003677 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003678 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003679 Value = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003680 Chain = Value.getValue(1);
3681 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3682 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3683 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003684
Evan Cheng0db9fe62006-04-25 20:13:52 +00003685 // Build the FP_TO_INT*_IN_MEM
3686 std::vector<SDOperand> Ops;
3687 Ops.push_back(Chain);
3688 Ops.push_back(Value);
3689 Ops.push_back(StackSlot);
Evan Cheng311ace02006-08-11 07:35:45 +00003690 SDOperand FIST = DAG.getNode(Opc, MVT::Other, &Ops[0], Ops.size());
Evan Chengd9558e02006-01-06 00:43:03 +00003691
Evan Cheng0db9fe62006-04-25 20:13:52 +00003692 // Load the result.
Evan Cheng466685d2006-10-09 20:57:25 +00003693 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003694}
3695
3696SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3697 MVT::ValueType VT = Op.getValueType();
3698 const Type *OpNTy = MVT::getTypeForValueType(VT);
3699 std::vector<Constant*> CV;
3700 if (VT == MVT::f64) {
3701 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3702 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3703 } else {
3704 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3705 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3706 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3707 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3708 }
3709 Constant *CS = ConstantStruct::get(CV);
3710 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Evan Cheng64a752f2006-08-11 09:08:15 +00003711 std::vector<MVT::ValueType> Tys;
3712 Tys.push_back(VT);
3713 Tys.push_back(MVT::Other);
3714 SmallVector<SDOperand, 3> Ops;
3715 Ops.push_back(DAG.getEntryNode());
3716 Ops.push_back(CPIdx);
3717 Ops.push_back(DAG.getSrcValue(NULL));
3718 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003719 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3720}
3721
3722SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3723 MVT::ValueType VT = Op.getValueType();
3724 const Type *OpNTy = MVT::getTypeForValueType(VT);
3725 std::vector<Constant*> CV;
3726 if (VT == MVT::f64) {
3727 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3728 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3729 } else {
3730 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3731 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3732 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3733 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3734 }
3735 Constant *CS = ConstantStruct::get(CV);
3736 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Evan Cheng64a752f2006-08-11 09:08:15 +00003737 std::vector<MVT::ValueType> Tys;
3738 Tys.push_back(VT);
3739 Tys.push_back(MVT::Other);
3740 SmallVector<SDOperand, 3> Ops;
3741 Ops.push_back(DAG.getEntryNode());
3742 Ops.push_back(CPIdx);
3743 Ops.push_back(DAG.getSrcValue(NULL));
3744 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003745 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3746}
3747
Evan Cheng68c47cb2007-01-05 07:55:56 +00003748SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00003749 SDOperand Op0 = Op.getOperand(0);
3750 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003751 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00003752 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00003753 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003754
3755 // If second operand is smaller, extend it first.
3756 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3757 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3758 SrcVT = VT;
3759 }
3760
Evan Cheng68c47cb2007-01-05 07:55:56 +00003761 // First get the sign bit of second operand.
3762 std::vector<Constant*> CV;
3763 if (SrcVT == MVT::f64) {
3764 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3765 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3766 } else {
3767 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3768 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3769 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3770 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3771 }
3772 Constant *CS = ConstantStruct::get(CV);
3773 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3774 std::vector<MVT::ValueType> Tys;
Evan Cheng1722eee2007-01-05 08:32:24 +00003775 Tys.push_back(SrcVT);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003776 Tys.push_back(MVT::Other);
3777 SmallVector<SDOperand, 3> Ops;
3778 Ops.push_back(DAG.getEntryNode());
3779 Ops.push_back(CPIdx);
3780 Ops.push_back(DAG.getSrcValue(NULL));
Evan Cheng73d6cf12007-01-05 21:37:56 +00003781 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3782 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003783
3784 // Shift sign bit right or left if the two operands have different types.
3785 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3786 // Op0 is MVT::f32, Op1 is MVT::f64.
3787 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3788 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3789 DAG.getConstant(32, MVT::i32));
3790 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3791 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3792 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00003793 }
3794
Evan Cheng73d6cf12007-01-05 21:37:56 +00003795 // Clear first operand sign bit.
3796 CV.clear();
3797 if (VT == MVT::f64) {
3798 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3799 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3800 } else {
3801 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3802 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3803 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3804 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3805 }
3806 CS = ConstantStruct::get(CV);
3807 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3808 Tys.clear();
3809 Tys.push_back(VT);
3810 Tys.push_back(MVT::Other);
3811 Ops.clear();
3812 Ops.push_back(DAG.getEntryNode());
3813 Ops.push_back(CPIdx);
3814 Ops.push_back(DAG.getSrcValue(NULL));
3815 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3816 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3817
3818 // Or the value with the sign bit.
3819 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003820}
3821
Evan Cheng734503b2006-09-11 02:19:56 +00003822SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3823 SDOperand Chain) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003824 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3825 SDOperand Cond;
Evan Cheng734503b2006-09-11 02:19:56 +00003826 SDOperand Op0 = Op.getOperand(0);
3827 SDOperand Op1 = Op.getOperand(1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003828 SDOperand CC = Op.getOperand(2);
3829 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Chengcf12ec42006-10-12 19:12:56 +00003830 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3831 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003832 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003833 unsigned X86CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003834
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003835 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Chris Lattnerf9570512006-09-13 03:22:10 +00003836 Op0, Op1, DAG)) {
Evan Cheng734503b2006-09-11 02:19:56 +00003837 SDOperand Ops1[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003838 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003839 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003840 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003841 }
3842
3843 assert(isFP && "Illegal integer SetCC!");
3844
3845 SDOperand COps[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003846 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003847
3848 switch (SetCCOpcode) {
3849 default: assert(false && "Illegal floating point SetCC!");
3850 case ISD::SETOEQ: { // !PF & ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003851 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003852 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003853 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003854 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003855 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003856 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3857 }
3858 case ISD::SETUNE: { // PF | !ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003859 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003860 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003861 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003862 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003863 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003864 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3865 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003866 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003867}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003868
Evan Cheng0db9fe62006-04-25 20:13:52 +00003869SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003870 bool addTest = true;
3871 SDOperand Chain = DAG.getEntryNode();
3872 SDOperand Cond = Op.getOperand(0);
3873 SDOperand CC;
3874 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng9bba8942006-01-26 02:13:10 +00003875
Evan Cheng734503b2006-09-11 02:19:56 +00003876 if (Cond.getOpcode() == ISD::SETCC)
3877 Cond = LowerSETCC(Cond, DAG, Chain);
3878
3879 if (Cond.getOpcode() == X86ISD::SETCC) {
3880 CC = Cond.getOperand(0);
3881
Evan Cheng0db9fe62006-04-25 20:13:52 +00003882 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng734503b2006-09-11 02:19:56 +00003883 // (since flag operand cannot be shared). Use it as the condition setting
3884 // operand in place of the X86ISD::SETCC.
3885 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Cheng0db9fe62006-04-25 20:13:52 +00003886 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng734503b2006-09-11 02:19:56 +00003887 // pressure reason)?
3888 SDOperand Cmp = Cond.getOperand(1);
3889 unsigned Opc = Cmp.getOpcode();
3890 bool IllegalFPCMov = !X86ScalarSSE &&
3891 MVT::isFloatingPoint(Op.getValueType()) &&
3892 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3893 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3894 !IllegalFPCMov) {
3895 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3896 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3897 addTest = false;
3898 }
3899 }
Evan Chengaaca22c2006-01-10 20:26:56 +00003900
Evan Cheng0db9fe62006-04-25 20:13:52 +00003901 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003902 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003903 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3904 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng7df96d62005-12-17 01:21:05 +00003905 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003906
Evan Cheng734503b2006-09-11 02:19:56 +00003907 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3908 SmallVector<SDOperand, 4> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003909 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3910 // condition is true.
3911 Ops.push_back(Op.getOperand(2));
3912 Ops.push_back(Op.getOperand(1));
3913 Ops.push_back(CC);
Evan Cheng734503b2006-09-11 02:19:56 +00003914 Ops.push_back(Cond.getValue(1));
3915 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003916}
Evan Cheng9bba8942006-01-26 02:13:10 +00003917
Evan Cheng0db9fe62006-04-25 20:13:52 +00003918SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003919 bool addTest = true;
3920 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003921 SDOperand Cond = Op.getOperand(1);
3922 SDOperand Dest = Op.getOperand(2);
3923 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00003924 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3925
Evan Cheng0db9fe62006-04-25 20:13:52 +00003926 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng734503b2006-09-11 02:19:56 +00003927 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003928
3929 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00003930 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003931
Evan Cheng734503b2006-09-11 02:19:56 +00003932 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3933 // (since flag operand cannot be shared). Use it as the condition setting
3934 // operand in place of the X86ISD::SETCC.
3935 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3936 // to use a test instead of duplicating the X86ISD::CMP (for register
3937 // pressure reason)?
3938 SDOperand Cmp = Cond.getOperand(1);
3939 unsigned Opc = Cmp.getOpcode();
3940 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3941 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3942 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3943 addTest = false;
3944 }
3945 }
Evan Cheng1bcee362006-01-13 01:03:02 +00003946
Evan Cheng0db9fe62006-04-25 20:13:52 +00003947 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003948 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003949 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3950 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng898101c2005-12-19 23:12:38 +00003951 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003952 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng734503b2006-09-11 02:19:56 +00003953 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003954}
Evan Cheng67f92a72006-01-11 22:15:48 +00003955
Evan Cheng32fe1032006-05-25 00:59:30 +00003956SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3957 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003958
Evan Cheng25ab6902006-09-08 06:48:29 +00003959 if (Subtarget->is64Bit())
3960 return LowerX86_64CCCCallTo(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00003961 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003962 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00003963 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003964 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00003965 case CallingConv::Fast:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003966 if (EnableFastCC) {
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00003967 return LowerFastCCCallTo(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003968 }
3969 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00003970 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003971 return LowerCCCCallTo(Op, DAG);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003972 case CallingConv::X86_StdCall:
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00003973 return LowerCCCCallTo(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003974 case CallingConv::X86_FastCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003975 return LowerFastCCCallTo(Op, DAG, true);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003976 }
Evan Cheng32fe1032006-05-25 00:59:30 +00003977}
3978
Evan Cheng0db9fe62006-04-25 20:13:52 +00003979SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3980 SDOperand Copy;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003981
Evan Cheng0db9fe62006-04-25 20:13:52 +00003982 switch(Op.getNumOperands()) {
Nate Begemanee625572006-01-27 21:09:22 +00003983 default:
3984 assert(0 && "Do not know how to return this many arguments!");
3985 abort();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003986 case 1: // ret void.
Nate Begemanee625572006-01-27 21:09:22 +00003987 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003988 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Evan Cheng6848be12006-05-26 23:10:12 +00003989 case 3: {
Nate Begemanee625572006-01-27 21:09:22 +00003990 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003991
Evan Cheng25ab6902006-09-08 06:48:29 +00003992 if (MVT::isVector(ArgVT) ||
3993 (Subtarget->is64Bit() && MVT::isFloatingPoint(ArgVT))) {
Chris Lattnerb2be4032006-04-17 20:32:50 +00003994 // Integer or FP vector result -> XMM0.
3995 if (DAG.getMachineFunction().liveout_empty())
3996 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3997 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3998 SDOperand());
3999 } else if (MVT::isInteger(ArgVT)) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004000 // Integer result -> EAX / RAX.
4001 // The C calling convention guarantees the return value has been
4002 // promoted to at least MVT::i32. The X86-64 ABI doesn't require the
4003 // value to be promoted MVT::i64. So we don't have to extend it to
4004 // 64-bit. Return the value in EAX, but mark RAX as liveout.
4005 unsigned Reg = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
Chris Lattnerb2be4032006-04-17 20:32:50 +00004006 if (DAG.getMachineFunction().liveout_empty())
Evan Cheng25ab6902006-09-08 06:48:29 +00004007 DAG.getMachineFunction().addLiveOut(Reg);
Chris Lattnerb2be4032006-04-17 20:32:50 +00004008
Evan Cheng25ab6902006-09-08 06:48:29 +00004009 Reg = (ArgVT == MVT::i64) ? X86::RAX : X86::EAX;
4010 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg, Op.getOperand(1),
Nate Begemanee625572006-01-27 21:09:22 +00004011 SDOperand());
Chris Lattnerb2be4032006-04-17 20:32:50 +00004012 } else if (!X86ScalarSSE) {
4013 // FP return with fp-stack value.
4014 if (DAG.getMachineFunction().liveout_empty())
4015 DAG.getMachineFunction().addLiveOut(X86::ST0);
4016
Nate Begemanee625572006-01-27 21:09:22 +00004017 std::vector<MVT::ValueType> Tys;
4018 Tys.push_back(MVT::Other);
4019 Tys.push_back(MVT::Flag);
4020 std::vector<SDOperand> Ops;
4021 Ops.push_back(Op.getOperand(0));
4022 Ops.push_back(Op.getOperand(1));
Evan Cheng311ace02006-08-11 07:35:45 +00004023 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
Nate Begemanee625572006-01-27 21:09:22 +00004024 } else {
Chris Lattnerb2be4032006-04-17 20:32:50 +00004025 // FP return with ScalarSSE (return on fp-stack).
4026 if (DAG.getMachineFunction().liveout_empty())
4027 DAG.getMachineFunction().addLiveOut(X86::ST0);
4028
Evan Cheng0d084c92006-02-01 00:20:21 +00004029 SDOperand MemLoc;
4030 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00004031 SDOperand Value = Op.getOperand(1);
4032
Evan Cheng466685d2006-10-09 20:57:25 +00004033 if (ISD::isNON_EXTLoad(Value.Val) &&
Evan Cheng760df292006-02-01 01:19:32 +00004034 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00004035 Chain = Value.getOperand(0);
4036 MemLoc = Value.getOperand(1);
4037 } else {
4038 // Spill the value to memory and reload it into top of stack.
4039 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
4040 MachineFunction &MF = DAG.getMachineFunction();
4041 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4042 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004043 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00004044 }
Nate Begemanee625572006-01-27 21:09:22 +00004045 std::vector<MVT::ValueType> Tys;
4046 Tys.push_back(MVT::f64);
4047 Tys.push_back(MVT::Other);
4048 std::vector<SDOperand> Ops;
4049 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00004050 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00004051 Ops.push_back(DAG.getValueType(ArgVT));
Evan Cheng311ace02006-08-11 07:35:45 +00004052 Copy = DAG.getNode(X86ISD::FLD, Tys, &Ops[0], Ops.size());
Nate Begemanee625572006-01-27 21:09:22 +00004053 Tys.clear();
4054 Tys.push_back(MVT::Other);
4055 Tys.push_back(MVT::Flag);
4056 Ops.clear();
4057 Ops.push_back(Copy.getValue(1));
4058 Ops.push_back(Copy);
Evan Cheng311ace02006-08-11 07:35:45 +00004059 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, &Ops[0], Ops.size());
Nate Begemanee625572006-01-27 21:09:22 +00004060 }
4061 break;
4062 }
Evan Cheng25ab6902006-09-08 06:48:29 +00004063 case 5: {
4064 unsigned Reg1 = Subtarget->is64Bit() ? X86::RAX : X86::EAX;
4065 unsigned Reg2 = Subtarget->is64Bit() ? X86::RDX : X86::EDX;
Chris Lattnerb2be4032006-04-17 20:32:50 +00004066 if (DAG.getMachineFunction().liveout_empty()) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004067 DAG.getMachineFunction().addLiveOut(Reg1);
4068 DAG.getMachineFunction().addLiveOut(Reg2);
Chris Lattnerb2be4032006-04-17 20:32:50 +00004069 }
4070
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004071 Copy = DAG.getCopyToReg(Op.getOperand(0), Reg2, Op.getOperand(3),
Nate Begemanee625572006-01-27 21:09:22 +00004072 SDOperand());
Evan Cheng25ab6902006-09-08 06:48:29 +00004073 Copy = DAG.getCopyToReg(Copy, Reg1, Op.getOperand(1), Copy.getValue(1));
Nate Begemanee625572006-01-27 21:09:22 +00004074 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004075 }
Nate Begemanee625572006-01-27 21:09:22 +00004076 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004077 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
Evan Cheng25ab6902006-09-08 06:48:29 +00004078 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
Evan Cheng0db9fe62006-04-25 20:13:52 +00004079 Copy.getValue(1));
4080}
4081
Evan Cheng1bc78042006-04-26 01:20:17 +00004082SDOperand
4083X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00004084 MachineFunction &MF = DAG.getMachineFunction();
4085 const Function* Fn = MF.getFunction();
4086 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00004087 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00004088 Fn->getName() == "main")
Evan Chenge8bd0a32006-06-06 23:30:24 +00004089 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
4090
Evan Cheng25caf632006-05-23 21:06:34 +00004091 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00004092 if (Subtarget->is64Bit())
4093 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00004094 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004095 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00004096 default:
4097 assert(0 && "Unsupported calling convention");
4098 case CallingConv::Fast:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004099 if (EnableFastCC) {
4100 return LowerFastCCArguments(Op, DAG);
4101 }
4102 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00004103 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004104 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004105 case CallingConv::X86_StdCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004106 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004107 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00004108 case CallingConv::X86_FastCall:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004109 MF.getInfo<X86FunctionInfo>()->setDecorationStyle(FastCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004110 return LowerFastCCArguments(Op, DAG, true);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00004111 }
Evan Cheng1bc78042006-04-26 01:20:17 +00004112}
4113
Evan Cheng0db9fe62006-04-25 20:13:52 +00004114SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4115 SDOperand InFlag(0, 0);
4116 SDOperand Chain = Op.getOperand(0);
4117 unsigned Align =
4118 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4119 if (Align == 0) Align = 1;
4120
4121 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4122 // If not DWORD aligned, call memset if size is less than the threshold.
4123 // It knows how to align to the right boundary first.
4124 if ((Align & 3) != 0 ||
4125 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4126 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004127 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004128 TargetLowering::ArgListTy Args;
4129 TargetLowering::ArgListEntry Entry;
4130 Entry.Node = Op.getOperand(1);
4131 Entry.Ty = IntPtrTy;
4132 Entry.isSigned = false;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004133 Entry.isInReg = false;
4134 Entry.isSRet = false;
Reid Spencer47857812006-12-31 05:55:36 +00004135 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004136 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004137 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4138 Entry.Ty = IntPtrTy;
4139 Entry.isSigned = false;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004140 Entry.isInReg = false;
4141 Entry.isSRet = false;
Reid Spencer47857812006-12-31 05:55:36 +00004142 Args.push_back(Entry);
4143 Entry.Node = Op.getOperand(3);
4144 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004145 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004146 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004147 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4148 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004149 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004150
Evan Cheng0db9fe62006-04-25 20:13:52 +00004151 MVT::ValueType AVT;
4152 SDOperand Count;
4153 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4154 unsigned BytesLeft = 0;
4155 bool TwoRepStos = false;
4156 if (ValC) {
4157 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004158 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004159
Evan Cheng0db9fe62006-04-25 20:13:52 +00004160 // If the value is a constant, then we can potentially use larger sets.
4161 switch (Align & 3) {
4162 case 2: // WORD aligned
4163 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004164 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004165 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004166 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004167 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004168 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004169 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004170 Val = (Val << 8) | Val;
4171 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004172 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4173 AVT = MVT::i64;
4174 ValReg = X86::RAX;
4175 Val = (Val << 32) | Val;
4176 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004177 break;
4178 default: // Byte aligned
4179 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004180 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004181 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004182 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004183 }
4184
Evan Cheng25ab6902006-09-08 06:48:29 +00004185 if (AVT > MVT::i8) {
4186 if (I) {
4187 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4188 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4189 BytesLeft = I->getValue() % UBytes;
4190 } else {
4191 assert(AVT >= MVT::i32 &&
4192 "Do not use rep;stos if not at least DWORD aligned");
4193 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4194 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4195 TwoRepStos = true;
4196 }
4197 }
4198
Evan Cheng0db9fe62006-04-25 20:13:52 +00004199 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4200 InFlag);
4201 InFlag = Chain.getValue(1);
4202 } else {
4203 AVT = MVT::i8;
4204 Count = Op.getOperand(3);
4205 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4206 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004207 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004208
Evan Cheng25ab6902006-09-08 06:48:29 +00004209 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4210 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004211 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004212 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4213 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004214 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004215
Evan Cheng0db9fe62006-04-25 20:13:52 +00004216 std::vector<MVT::ValueType> Tys;
4217 Tys.push_back(MVT::Other);
4218 Tys.push_back(MVT::Flag);
4219 std::vector<SDOperand> Ops;
4220 Ops.push_back(Chain);
4221 Ops.push_back(DAG.getValueType(AVT));
4222 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004223 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004224
Evan Cheng0db9fe62006-04-25 20:13:52 +00004225 if (TwoRepStos) {
4226 InFlag = Chain.getValue(1);
4227 Count = Op.getOperand(3);
4228 MVT::ValueType CVT = Count.getValueType();
4229 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004230 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4231 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4232 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004233 InFlag = Chain.getValue(1);
4234 Tys.clear();
4235 Tys.push_back(MVT::Other);
4236 Tys.push_back(MVT::Flag);
4237 Ops.clear();
4238 Ops.push_back(Chain);
4239 Ops.push_back(DAG.getValueType(MVT::i8));
4240 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004241 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004242 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004243 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004244 SDOperand Value;
4245 unsigned Val = ValC->getValue() & 255;
4246 unsigned Offset = I->getValue() - BytesLeft;
4247 SDOperand DstAddr = Op.getOperand(1);
4248 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004249 if (BytesLeft >= 4) {
4250 Val = (Val << 8) | Val;
4251 Val = (Val << 16) | Val;
4252 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004253 Chain = DAG.getStore(Chain, Value,
4254 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4255 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004256 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004257 BytesLeft -= 4;
4258 Offset += 4;
4259 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004260 if (BytesLeft >= 2) {
4261 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004262 Chain = DAG.getStore(Chain, Value,
4263 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4264 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004265 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004266 BytesLeft -= 2;
4267 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004268 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004269 if (BytesLeft == 1) {
4270 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004271 Chain = DAG.getStore(Chain, Value,
4272 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4273 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004274 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004275 }
Evan Cheng386031a2006-03-24 07:29:27 +00004276 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004277
Evan Cheng0db9fe62006-04-25 20:13:52 +00004278 return Chain;
4279}
Evan Cheng11e15b32006-04-03 20:53:28 +00004280
Evan Cheng0db9fe62006-04-25 20:13:52 +00004281SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4282 SDOperand Chain = Op.getOperand(0);
4283 unsigned Align =
4284 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4285 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00004286
Evan Cheng0db9fe62006-04-25 20:13:52 +00004287 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
4288 // If not DWORD aligned, call memcpy if size is less than the threshold.
4289 // It knows how to align to the right boundary first.
4290 if ((Align & 3) != 0 ||
4291 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
4292 MVT::ValueType IntPtr = getPointerTy();
Reid Spencer47857812006-12-31 05:55:36 +00004293 TargetLowering::ArgListTy Args;
4294 TargetLowering::ArgListEntry Entry;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004295 Entry.Ty = getTargetData()->getIntPtrType();
4296 Entry.isSigned = false;
4297 Entry.isInReg = false;
4298 Entry.isSRet = false;
Reid Spencer47857812006-12-31 05:55:36 +00004299 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
4300 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
4301 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004302 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004303 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004304 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4305 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00004306 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004307
4308 MVT::ValueType AVT;
4309 SDOperand Count;
4310 unsigned BytesLeft = 0;
4311 bool TwoRepMovs = false;
4312 switch (Align & 3) {
4313 case 2: // WORD aligned
4314 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004315 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004316 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004317 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004318 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4319 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004320 break;
4321 default: // Byte aligned
4322 AVT = MVT::i8;
4323 Count = Op.getOperand(3);
4324 break;
4325 }
4326
Evan Cheng25ab6902006-09-08 06:48:29 +00004327 if (AVT > MVT::i8) {
4328 if (I) {
4329 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4330 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4331 BytesLeft = I->getValue() % UBytes;
4332 } else {
4333 assert(AVT >= MVT::i32 &&
4334 "Do not use rep;movs if not at least DWORD aligned");
4335 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4336 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4337 TwoRepMovs = true;
4338 }
4339 }
4340
Evan Cheng0db9fe62006-04-25 20:13:52 +00004341 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004342 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4343 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004344 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004345 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4346 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004347 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004348 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4349 Op.getOperand(2), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004350 InFlag = Chain.getValue(1);
4351
4352 std::vector<MVT::ValueType> Tys;
4353 Tys.push_back(MVT::Other);
4354 Tys.push_back(MVT::Flag);
4355 std::vector<SDOperand> Ops;
4356 Ops.push_back(Chain);
4357 Ops.push_back(DAG.getValueType(AVT));
4358 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004359 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004360
4361 if (TwoRepMovs) {
4362 InFlag = Chain.getValue(1);
4363 Count = Op.getOperand(3);
4364 MVT::ValueType CVT = Count.getValueType();
4365 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004366 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4367 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4368 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004369 InFlag = Chain.getValue(1);
4370 Tys.clear();
4371 Tys.push_back(MVT::Other);
4372 Tys.push_back(MVT::Flag);
4373 Ops.clear();
4374 Ops.push_back(Chain);
4375 Ops.push_back(DAG.getValueType(MVT::i8));
4376 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004377 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004378 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004379 // Issue loads and stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004380 unsigned Offset = I->getValue() - BytesLeft;
4381 SDOperand DstAddr = Op.getOperand(1);
4382 MVT::ValueType DstVT = DstAddr.getValueType();
4383 SDOperand SrcAddr = Op.getOperand(2);
4384 MVT::ValueType SrcVT = SrcAddr.getValueType();
4385 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004386 if (BytesLeft >= 4) {
4387 Value = DAG.getLoad(MVT::i32, Chain,
4388 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4389 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004390 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004391 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004392 Chain = DAG.getStore(Chain, Value,
4393 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4394 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004395 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004396 BytesLeft -= 4;
4397 Offset += 4;
4398 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 if (BytesLeft >= 2) {
4400 Value = DAG.getLoad(MVT::i16, Chain,
4401 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4402 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004403 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004404 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004405 Chain = DAG.getStore(Chain, Value,
4406 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4407 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004408 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004409 BytesLeft -= 2;
4410 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004411 }
4412
Evan Cheng0db9fe62006-04-25 20:13:52 +00004413 if (BytesLeft == 1) {
4414 Value = DAG.getLoad(MVT::i8, Chain,
4415 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4416 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004417 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004418 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004419 Chain = DAG.getStore(Chain, Value,
4420 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4421 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004422 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004423 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004424 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004425
4426 return Chain;
4427}
4428
4429SDOperand
4430X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
4431 std::vector<MVT::ValueType> Tys;
4432 Tys.push_back(MVT::Other);
4433 Tys.push_back(MVT::Flag);
4434 std::vector<SDOperand> Ops;
4435 Ops.push_back(Op.getOperand(0));
Evan Cheng311ace02006-08-11 07:35:45 +00004436 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004437 Ops.clear();
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004438 if (Subtarget->is64Bit()) {
4439 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4440 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4441 MVT::i64, Copy1.getValue(2));
4442 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4443 DAG.getConstant(32, MVT::i8));
4444 Ops.push_back(DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp));
4445 Ops.push_back(Copy2.getValue(1));
4446 Tys[0] = MVT::i64;
4447 Tys[1] = MVT::Other;
4448 } else {
4449 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4450 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4451 MVT::i32, Copy1.getValue(2));
4452 Ops.push_back(Copy1);
4453 Ops.push_back(Copy2);
4454 Ops.push_back(Copy2.getValue(1));
4455 Tys[0] = Tys[1] = MVT::i32;
4456 Tys.push_back(MVT::Other);
4457 }
Evan Cheng311ace02006-08-11 07:35:45 +00004458 return DAG.getNode(ISD::MERGE_VALUES, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004459}
4460
4461SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004462 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4463
Evan Cheng25ab6902006-09-08 06:48:29 +00004464 if (!Subtarget->is64Bit()) {
4465 // vastart just stores the address of the VarArgsFrameIndex slot into the
4466 // memory location argument.
4467 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004468 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4469 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004470 }
4471
4472 // __va_list_tag:
4473 // gp_offset (0 - 6 * 8)
4474 // fp_offset (48 - 48 + 8 * 16)
4475 // overflow_arg_area (point to parameters coming in memory).
4476 // reg_save_area
4477 std::vector<SDOperand> MemOps;
4478 SDOperand FIN = Op.getOperand(1);
4479 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004480 SDOperand Store = DAG.getStore(Op.getOperand(0),
4481 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004482 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004483 MemOps.push_back(Store);
4484
4485 // Store fp_offset
4486 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4487 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00004488 Store = DAG.getStore(Op.getOperand(0),
4489 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004490 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004491 MemOps.push_back(Store);
4492
4493 // Store ptr to overflow_arg_area
4494 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4495 DAG.getConstant(4, getPointerTy()));
4496 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004497 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4498 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004499 MemOps.push_back(Store);
4500
4501 // Store ptr to reg_save_area.
4502 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4503 DAG.getConstant(8, getPointerTy()));
4504 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004505 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4506 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004507 MemOps.push_back(Store);
4508 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004509}
4510
4511SDOperand
4512X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4513 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4514 switch (IntNo) {
4515 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004516 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004517 case Intrinsic::x86_sse_comieq_ss:
4518 case Intrinsic::x86_sse_comilt_ss:
4519 case Intrinsic::x86_sse_comile_ss:
4520 case Intrinsic::x86_sse_comigt_ss:
4521 case Intrinsic::x86_sse_comige_ss:
4522 case Intrinsic::x86_sse_comineq_ss:
4523 case Intrinsic::x86_sse_ucomieq_ss:
4524 case Intrinsic::x86_sse_ucomilt_ss:
4525 case Intrinsic::x86_sse_ucomile_ss:
4526 case Intrinsic::x86_sse_ucomigt_ss:
4527 case Intrinsic::x86_sse_ucomige_ss:
4528 case Intrinsic::x86_sse_ucomineq_ss:
4529 case Intrinsic::x86_sse2_comieq_sd:
4530 case Intrinsic::x86_sse2_comilt_sd:
4531 case Intrinsic::x86_sse2_comile_sd:
4532 case Intrinsic::x86_sse2_comigt_sd:
4533 case Intrinsic::x86_sse2_comige_sd:
4534 case Intrinsic::x86_sse2_comineq_sd:
4535 case Intrinsic::x86_sse2_ucomieq_sd:
4536 case Intrinsic::x86_sse2_ucomilt_sd:
4537 case Intrinsic::x86_sse2_ucomile_sd:
4538 case Intrinsic::x86_sse2_ucomigt_sd:
4539 case Intrinsic::x86_sse2_ucomige_sd:
4540 case Intrinsic::x86_sse2_ucomineq_sd: {
4541 unsigned Opc = 0;
4542 ISD::CondCode CC = ISD::SETCC_INVALID;
4543 switch (IntNo) {
4544 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004545 case Intrinsic::x86_sse_comieq_ss:
4546 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004547 Opc = X86ISD::COMI;
4548 CC = ISD::SETEQ;
4549 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004550 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004551 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552 Opc = X86ISD::COMI;
4553 CC = ISD::SETLT;
4554 break;
4555 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004556 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004557 Opc = X86ISD::COMI;
4558 CC = ISD::SETLE;
4559 break;
4560 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004561 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004562 Opc = X86ISD::COMI;
4563 CC = ISD::SETGT;
4564 break;
4565 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004566 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004567 Opc = X86ISD::COMI;
4568 CC = ISD::SETGE;
4569 break;
4570 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004571 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004572 Opc = X86ISD::COMI;
4573 CC = ISD::SETNE;
4574 break;
4575 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004576 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004577 Opc = X86ISD::UCOMI;
4578 CC = ISD::SETEQ;
4579 break;
4580 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004581 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582 Opc = X86ISD::UCOMI;
4583 CC = ISD::SETLT;
4584 break;
4585 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004586 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004587 Opc = X86ISD::UCOMI;
4588 CC = ISD::SETLE;
4589 break;
4590 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004591 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004592 Opc = X86ISD::UCOMI;
4593 CC = ISD::SETGT;
4594 break;
4595 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004596 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004597 Opc = X86ISD::UCOMI;
4598 CC = ISD::SETGE;
4599 break;
4600 case Intrinsic::x86_sse_ucomineq_ss:
4601 case Intrinsic::x86_sse2_ucomineq_sd:
4602 Opc = X86ISD::UCOMI;
4603 CC = ISD::SETNE;
4604 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004605 }
Evan Cheng734503b2006-09-11 02:19:56 +00004606
Evan Cheng0db9fe62006-04-25 20:13:52 +00004607 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004608 SDOperand LHS = Op.getOperand(1);
4609 SDOperand RHS = Op.getOperand(2);
4610 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004611
4612 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Chris Lattnerf9570512006-09-13 03:22:10 +00004613 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
Evan Cheng734503b2006-09-11 02:19:56 +00004614 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4615 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4616 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4617 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004618 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004619 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004620 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004621}
Evan Cheng72261582005-12-20 06:22:03 +00004622
Nate Begemanbcc5f362007-01-29 22:58:52 +00004623SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4624 // Depths > 0 not supported yet!
4625 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4626 return SDOperand();
4627
4628 // Just load the return address
4629 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4630 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4631}
4632
4633SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4634 // Depths > 0 not supported yet!
4635 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4636 return SDOperand();
4637
4638 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4639 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4640 DAG.getConstant(4, getPointerTy()));
4641}
4642
Evan Cheng0db9fe62006-04-25 20:13:52 +00004643/// LowerOperation - Provide custom lowering hooks for some operations.
4644///
4645SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4646 switch (Op.getOpcode()) {
4647 default: assert(0 && "Should not custom lower this!");
4648 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4649 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4650 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4651 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4652 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4653 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4654 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
4655 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4656 case ISD::SHL_PARTS:
4657 case ISD::SRA_PARTS:
4658 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4659 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4660 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4661 case ISD::FABS: return LowerFABS(Op, DAG);
4662 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004663 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004664 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004665 case ISD::SELECT: return LowerSELECT(Op, DAG);
4666 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4667 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00004668 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004669 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00004670 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004671 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4672 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4673 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4674 case ISD::VASTART: return LowerVASTART(Op, DAG);
4675 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00004676 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4677 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Jim Laskey62819f32007-02-21 22:54:50 +00004678 // Exception address and exception selector. Currently unimplemented.
4679 case ISD::EXCEPTIONADDR: break;
4680 case ISD::EHSELECTION: break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004681 }
Jim Laskey62819f32007-02-21 22:54:50 +00004682 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683}
4684
Evan Cheng72261582005-12-20 06:22:03 +00004685const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4686 switch (Opcode) {
4687 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00004688 case X86ISD::SHLD: return "X86ISD::SHLD";
4689 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00004690 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004691 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00004692 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004693 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00004694 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00004695 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00004696 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4697 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4698 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00004699 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00004700 case X86ISD::FST: return "X86ISD::FST";
4701 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00004702 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00004703 case X86ISD::CALL: return "X86ISD::CALL";
4704 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4705 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4706 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00004707 case X86ISD::COMI: return "X86ISD::COMI";
4708 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00004709 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00004710 case X86ISD::CMOV: return "X86ISD::CMOV";
4711 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00004712 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00004713 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4714 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00004715 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng206ee9d2006-07-07 08:33:52 +00004716 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
Evan Cheng7ccced62006-02-18 00:15:05 +00004717 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00004718 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00004719 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00004720 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00004721 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00004722 case X86ISD::FMAX: return "X86ISD::FMAX";
4723 case X86ISD::FMIN: return "X86ISD::FMIN";
Evan Cheng72261582005-12-20 06:22:03 +00004724 }
4725}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004726
Evan Cheng60c07e12006-07-05 22:17:51 +00004727/// isLegalAddressImmediate - Return true if the integer value or
4728/// GlobalValue can be used as the offset of the target addressing mode.
4729bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
4730 // X86 allows a sign-extended 32-bit immediate field.
4731 return (V > -(1LL << 32) && V < (1LL << 32)-1);
4732}
4733
4734bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengc8306bd2006-11-29 23:48:14 +00004735 // In 64-bit mode, GV is 64-bit so it won't fit in the 32-bit displacement
4736 // field unless we are in small code model.
4737 if (Subtarget->is64Bit() &&
4738 getTargetMachine().getCodeModel() != CodeModel::Small)
Evan Cheng60c07e12006-07-05 22:17:51 +00004739 return false;
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004740
4741 return (!Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false));
Evan Cheng60c07e12006-07-05 22:17:51 +00004742}
4743
4744/// isShuffleMaskLegal - Targets can use this to indicate that they only
4745/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4746/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4747/// are assumed to be legal.
4748bool
4749X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4750 // Only do shuffles on 128-bit vector types for now.
4751 if (MVT::getSizeInBits(VT) == 64) return false;
4752 return (Mask.Val->getNumOperands() <= 4 ||
4753 isSplatMask(Mask.Val) ||
4754 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4755 X86::isUNPCKLMask(Mask.Val) ||
4756 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
4757 X86::isUNPCKHMask(Mask.Val));
4758}
4759
4760bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4761 MVT::ValueType EVT,
4762 SelectionDAG &DAG) const {
4763 unsigned NumElts = BVOps.size();
4764 // Only do shuffles on 128-bit vector types for now.
4765 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4766 if (NumElts == 2) return true;
4767 if (NumElts == 4) {
4768 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
4769 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4770 }
4771 return false;
4772}
4773
4774//===----------------------------------------------------------------------===//
4775// X86 Scheduler Hooks
4776//===----------------------------------------------------------------------===//
4777
4778MachineBasicBlock *
4779X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4780 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004781 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00004782 switch (MI->getOpcode()) {
4783 default: assert(false && "Unexpected instr type to insert");
4784 case X86::CMOV_FR32:
4785 case X86::CMOV_FR64:
4786 case X86::CMOV_V4F32:
4787 case X86::CMOV_V2F64:
4788 case X86::CMOV_V2I64: {
4789 // To "insert" a SELECT_CC instruction, we actually have to insert the
4790 // diamond control-flow pattern. The incoming instruction knows the
4791 // destination vreg to set, the condition code register to branch on, the
4792 // true/false values to select between, and a branch opcode to use.
4793 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4794 ilist<MachineBasicBlock>::iterator It = BB;
4795 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004796
Evan Cheng60c07e12006-07-05 22:17:51 +00004797 // thisMBB:
4798 // ...
4799 // TrueVal = ...
4800 // cmpTY ccX, r1, r2
4801 // bCC copy1MBB
4802 // fallthrough --> copy0MBB
4803 MachineBasicBlock *thisMBB = BB;
4804 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4805 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004806 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00004807 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00004808 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00004809 MachineFunction *F = BB->getParent();
4810 F->getBasicBlockList().insert(It, copy0MBB);
4811 F->getBasicBlockList().insert(It, sinkMBB);
4812 // Update machine-CFG edges by first adding all successors of the current
4813 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004814 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00004815 e = BB->succ_end(); i != e; ++i)
4816 sinkMBB->addSuccessor(*i);
4817 // Next, remove all successors of the current block, and add the true
4818 // and fallthrough blocks as its successors.
4819 while(!BB->succ_empty())
4820 BB->removeSuccessor(BB->succ_begin());
4821 BB->addSuccessor(copy0MBB);
4822 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004823
Evan Cheng60c07e12006-07-05 22:17:51 +00004824 // copy0MBB:
4825 // %FalseValue = ...
4826 // # fallthrough to sinkMBB
4827 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004828
Evan Cheng60c07e12006-07-05 22:17:51 +00004829 // Update machine-CFG edges
4830 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004831
Evan Cheng60c07e12006-07-05 22:17:51 +00004832 // sinkMBB:
4833 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4834 // ...
4835 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00004836 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00004837 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4838 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4839
4840 delete MI; // The pseudo instruction is gone now.
4841 return BB;
4842 }
4843
4844 case X86::FP_TO_INT16_IN_MEM:
4845 case X86::FP_TO_INT32_IN_MEM:
4846 case X86::FP_TO_INT64_IN_MEM: {
4847 // Change the floating point control register to use "round towards zero"
4848 // mode when truncating to an integer value.
4849 MachineFunction *F = BB->getParent();
4850 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004851 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004852
4853 // Load the old value of the high byte of the control word...
4854 unsigned OldCW =
4855 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004856 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004857
4858 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004859 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4860 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00004861
4862 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004863 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004864
4865 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00004866 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4867 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00004868
4869 // Get the X86 opcode to use.
4870 unsigned Opc;
4871 switch (MI->getOpcode()) {
4872 default: assert(0 && "illegal opcode!");
4873 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4874 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4875 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4876 }
4877
4878 X86AddressMode AM;
4879 MachineOperand &Op = MI->getOperand(0);
4880 if (Op.isRegister()) {
4881 AM.BaseType = X86AddressMode::RegBase;
4882 AM.Base.Reg = Op.getReg();
4883 } else {
4884 AM.BaseType = X86AddressMode::FrameIndexBase;
4885 AM.Base.FrameIndex = Op.getFrameIndex();
4886 }
4887 Op = MI->getOperand(1);
4888 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004889 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004890 Op = MI->getOperand(2);
4891 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004892 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004893 Op = MI->getOperand(3);
4894 if (Op.isGlobalAddress()) {
4895 AM.GV = Op.getGlobal();
4896 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00004897 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004898 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00004899 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4900 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00004901
4902 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00004903 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004904
4905 delete MI; // The pseudo instruction is gone now.
4906 return BB;
4907 }
4908 }
4909}
4910
4911//===----------------------------------------------------------------------===//
4912// X86 Optimization Hooks
4913//===----------------------------------------------------------------------===//
4914
Nate Begeman368e18d2006-02-16 21:11:51 +00004915void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4916 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004917 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00004918 uint64_t &KnownOne,
4919 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004920 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00004921 assert((Opc >= ISD::BUILTIN_OP_END ||
4922 Opc == ISD::INTRINSIC_WO_CHAIN ||
4923 Opc == ISD::INTRINSIC_W_CHAIN ||
4924 Opc == ISD::INTRINSIC_VOID) &&
4925 "Should use MaskedValueIsZero if you don't know whether Op"
4926 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004927
Evan Cheng865f0602006-04-05 06:11:20 +00004928 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004929 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00004930 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004931 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00004932 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4933 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004934 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004935}
Chris Lattner259e97c2006-01-31 19:43:35 +00004936
Evan Cheng206ee9d2006-07-07 08:33:52 +00004937/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4938/// element of the result of the vector shuffle.
4939static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4940 MVT::ValueType VT = N->getValueType(0);
4941 SDOperand PermMask = N->getOperand(2);
4942 unsigned NumElems = PermMask.getNumOperands();
4943 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4944 i %= NumElems;
4945 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4946 return (i == 0)
4947 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4948 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4949 SDOperand Idx = PermMask.getOperand(i);
4950 if (Idx.getOpcode() == ISD::UNDEF)
4951 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4952 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4953 }
4954 return SDOperand();
4955}
4956
4957/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4958/// node is a GlobalAddress + an offset.
4959static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00004960 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004961 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004962 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4963 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4964 return true;
4965 }
Evan Cheng0085a282006-11-30 21:55:46 +00004966 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004967 SDOperand N1 = N->getOperand(0);
4968 SDOperand N2 = N->getOperand(1);
4969 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4970 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4971 if (V) {
4972 Offset += V->getSignExtended();
4973 return true;
4974 }
4975 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4976 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4977 if (V) {
4978 Offset += V->getSignExtended();
4979 return true;
4980 }
4981 }
4982 }
4983 return false;
4984}
4985
4986/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4987/// + Dist * Size.
4988static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4989 MachineFrameInfo *MFI) {
4990 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4991 return false;
4992
4993 SDOperand Loc = N->getOperand(1);
4994 SDOperand BaseLoc = Base->getOperand(1);
4995 if (Loc.getOpcode() == ISD::FrameIndex) {
4996 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4997 return false;
4998 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4999 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
5000 int FS = MFI->getObjectSize(FI);
5001 int BFS = MFI->getObjectSize(BFI);
5002 if (FS != BFS || FS != Size) return false;
5003 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5004 } else {
5005 GlobalValue *GV1 = NULL;
5006 GlobalValue *GV2 = NULL;
5007 int64_t Offset1 = 0;
5008 int64_t Offset2 = 0;
5009 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5010 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5011 if (isGA1 && isGA2 && GV1 == GV2)
5012 return Offset1 == (Offset2 + Dist*Size);
5013 }
5014
5015 return false;
5016}
5017
Evan Cheng1e60c092006-07-10 21:37:44 +00005018static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5019 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005020 GlobalValue *GV;
5021 int64_t Offset;
5022 if (isGAPlusOffset(Base, GV, Offset))
5023 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5024 else {
5025 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
5026 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00005027 if (BFI < 0)
5028 // Fixed objects do not specify alignment, however the offsets are known.
5029 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5030 (MFI->getObjectOffset(BFI) % 16) == 0);
5031 else
5032 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005033 }
5034 return false;
5035}
5036
5037
5038/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5039/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5040/// if the load addresses are consecutive, non-overlapping, and in the right
5041/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005042static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5043 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005044 MachineFunction &MF = DAG.getMachineFunction();
5045 MachineFrameInfo *MFI = MF.getFrameInfo();
5046 MVT::ValueType VT = N->getValueType(0);
5047 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
5048 SDOperand PermMask = N->getOperand(2);
5049 int NumElems = (int)PermMask.getNumOperands();
5050 SDNode *Base = NULL;
5051 for (int i = 0; i < NumElems; ++i) {
5052 SDOperand Idx = PermMask.getOperand(i);
5053 if (Idx.getOpcode() == ISD::UNDEF) {
5054 if (!Base) return SDOperand();
5055 } else {
5056 SDOperand Arg =
5057 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005058 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005059 return SDOperand();
5060 if (!Base)
5061 Base = Arg.Val;
5062 else if (!isConsecutiveLoad(Arg.Val, Base,
5063 i, MVT::getSizeInBits(EVT)/8,MFI))
5064 return SDOperand();
5065 }
5066 }
5067
Evan Cheng1e60c092006-07-10 21:37:44 +00005068 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Evan Cheng466685d2006-10-09 20:57:25 +00005069 if (isAlign16) {
5070 LoadSDNode *LD = cast<LoadSDNode>(Base);
5071 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5072 LD->getSrcValueOffset());
5073 } else {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005074 // Just use movups, it's shorter.
Evan Cheng64a752f2006-08-11 09:08:15 +00005075 std::vector<MVT::ValueType> Tys;
5076 Tys.push_back(MVT::v4f32);
5077 Tys.push_back(MVT::Other);
5078 SmallVector<SDOperand, 3> Ops;
5079 Ops.push_back(Base->getOperand(0));
5080 Ops.push_back(Base->getOperand(1));
5081 Ops.push_back(Base->getOperand(2));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005082 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Cheng64a752f2006-08-11 09:08:15 +00005083 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
Evan Cheng311ace02006-08-11 07:35:45 +00005084 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005085}
5086
Chris Lattner83e6c992006-10-04 06:57:07 +00005087/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5088static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5089 const X86Subtarget *Subtarget) {
5090 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005091
Chris Lattner83e6c992006-10-04 06:57:07 +00005092 // If we have SSE[12] support, try to form min/max nodes.
5093 if (Subtarget->hasSSE2() &&
5094 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5095 if (Cond.getOpcode() == ISD::SETCC) {
5096 // Get the LHS/RHS of the select.
5097 SDOperand LHS = N->getOperand(1);
5098 SDOperand RHS = N->getOperand(2);
5099 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005100
Evan Cheng8ca29322006-11-10 21:43:37 +00005101 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005102 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005103 switch (CC) {
5104 default: break;
5105 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5106 case ISD::SETULE:
5107 case ISD::SETLE:
5108 if (!UnsafeFPMath) break;
5109 // FALL THROUGH.
5110 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5111 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005112 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005113 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005114
Chris Lattner1907a7b2006-10-05 04:11:26 +00005115 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5116 case ISD::SETUGT:
5117 case ISD::SETGT:
5118 if (!UnsafeFPMath) break;
5119 // FALL THROUGH.
5120 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5121 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005122 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005123 break;
5124 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005125 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005126 switch (CC) {
5127 default: break;
5128 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5129 case ISD::SETUGT:
5130 case ISD::SETGT:
5131 if (!UnsafeFPMath) break;
5132 // FALL THROUGH.
5133 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5134 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005135 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005136 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005137
Chris Lattner1907a7b2006-10-05 04:11:26 +00005138 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5139 case ISD::SETULE:
5140 case ISD::SETLE:
5141 if (!UnsafeFPMath) break;
5142 // FALL THROUGH.
5143 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5144 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005145 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005146 break;
5147 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005148 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005149
Evan Cheng8ca29322006-11-10 21:43:37 +00005150 if (Opcode)
5151 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00005152 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005153
Chris Lattner83e6c992006-10-04 06:57:07 +00005154 }
5155
5156 return SDOperand();
5157}
5158
5159
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005160SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00005161 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005162 SelectionDAG &DAG = DCI.DAG;
5163 switch (N->getOpcode()) {
5164 default: break;
5165 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00005166 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00005167 case ISD::SELECT:
5168 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005169 }
5170
5171 return SDOperand();
5172}
5173
Evan Cheng60c07e12006-07-05 22:17:51 +00005174//===----------------------------------------------------------------------===//
5175// X86 Inline Assembly Support
5176//===----------------------------------------------------------------------===//
5177
Chris Lattnerf4dff842006-07-11 02:54:03 +00005178/// getConstraintType - Given a constraint letter, return the type of
5179/// constraint it is for this target.
5180X86TargetLowering::ConstraintType
5181X86TargetLowering::getConstraintType(char ConstraintLetter) const {
5182 switch (ConstraintLetter) {
Chris Lattner6d346572006-07-12 16:59:49 +00005183 case 'A':
5184 case 'r':
5185 case 'R':
5186 case 'l':
5187 case 'q':
5188 case 'Q':
5189 case 'x':
5190 case 'Y':
5191 return C_RegisterClass;
Chris Lattnerf4dff842006-07-11 02:54:03 +00005192 default: return TargetLowering::getConstraintType(ConstraintLetter);
5193 }
5194}
5195
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005196/// isOperandValidForConstraint - Return the specified operand (possibly
5197/// modified) if the specified SDOperand is valid for the specified target
5198/// constraint letter, otherwise return null.
5199SDOperand X86TargetLowering::
5200isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
5201 switch (Constraint) {
5202 default: break;
5203 case 'i':
5204 // Literal immediates are always ok.
5205 if (isa<ConstantSDNode>(Op)) return Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005206
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005207 // If we are in non-pic codegen mode, we allow the address of a global to
5208 // be used with 'i'.
5209 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
5210 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
5211 return SDOperand(0, 0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005212
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005213 if (GA->getOpcode() != ISD::TargetGlobalAddress)
5214 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5215 GA->getOffset());
5216 return Op;
5217 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005218
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005219 // Otherwise, not valid for this mode.
5220 return SDOperand(0, 0);
5221 }
5222 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
5223}
5224
5225
Chris Lattner259e97c2006-01-31 19:43:35 +00005226std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00005227getRegClassForInlineAsmConstraint(const std::string &Constraint,
5228 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00005229 if (Constraint.size() == 1) {
5230 // FIXME: not handling fp-stack yet!
5231 // FIXME: not handling MMX registers yet ('y' constraint).
5232 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00005233 default: break; // Unknown constraint letter
5234 case 'A': // EAX/EDX
5235 if (VT == MVT::i32 || VT == MVT::i64)
5236 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5237 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005238 case 'r': // GENERAL_REGS
5239 case 'R': // LEGACY_REGS
Chris Lattner98ae09c2006-12-04 22:38:21 +00005240 if (VT == MVT::i64 && Subtarget->is64Bit())
5241 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
5242 X86::RSI, X86::RDI, X86::RBP, X86::RSP,
5243 X86::R8, X86::R9, X86::R10, X86::R11,
5244 X86::R12, X86::R13, X86::R14, X86::R15, 0);
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005245 if (VT == MVT::i32)
5246 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5247 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
5248 else if (VT == MVT::i16)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005249 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005250 X86::SI, X86::DI, X86::BP, X86::SP, 0);
5251 else if (VT == MVT::i8)
Chris Lattneraf21f4f2006-12-05 17:29:40 +00005252 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005253 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005254 case 'l': // INDEX_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005255 if (VT == MVT::i32)
5256 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
5257 X86::ESI, X86::EDI, X86::EBP, 0);
5258 else if (VT == MVT::i16)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005259 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005260 X86::SI, X86::DI, X86::BP, 0);
5261 else if (VT == MVT::i8)
5262 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5263 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005264 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5265 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005266 if (VT == MVT::i32)
5267 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5268 else if (VT == MVT::i16)
5269 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5270 else if (VT == MVT::i8)
5271 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
5272 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005273 case 'x': // SSE_REGS if SSE1 allowed
5274 if (Subtarget->hasSSE1())
5275 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5276 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5277 0);
5278 return std::vector<unsigned>();
5279 case 'Y': // SSE_REGS if SSE2 allowed
5280 if (Subtarget->hasSSE2())
5281 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
5282 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
5283 0);
5284 return std::vector<unsigned>();
5285 }
5286 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005287
Chris Lattner1efa40f2006-02-22 00:56:39 +00005288 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00005289}
Chris Lattnerf76d1802006-07-31 23:26:50 +00005290
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005291std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00005292X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5293 MVT::ValueType VT) const {
5294 // Use the default implementation in TargetLowering to convert the register
5295 // constraint into a member of a register class.
5296 std::pair<unsigned, const TargetRegisterClass*> Res;
5297 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00005298
5299 // Not found as a standard register?
5300 if (Res.second == 0) {
5301 // GCC calls "st(0)" just plain "st".
5302 if (StringsEqualNoCase("{st}", Constraint)) {
5303 Res.first = X86::ST0;
5304 Res.second = X86::RSTRegisterClass;
5305 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005306
Chris Lattner1a60aa72006-10-31 19:42:44 +00005307 return Res;
5308 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005309
Chris Lattnerf76d1802006-07-31 23:26:50 +00005310 // Otherwise, check to see if this is a register class of the wrong value
5311 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5312 // turn into {ax},{dx}.
5313 if (Res.second->hasType(VT))
5314 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005315
Chris Lattnerf76d1802006-07-31 23:26:50 +00005316 // All of the single-register GCC register classes map their values onto
5317 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5318 // really want an 8-bit or 32-bit register, map to the appropriate register
5319 // class and return the appropriate register.
5320 if (Res.second != X86::GR16RegisterClass)
5321 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005322
Chris Lattnerf76d1802006-07-31 23:26:50 +00005323 if (VT == MVT::i8) {
5324 unsigned DestReg = 0;
5325 switch (Res.first) {
5326 default: break;
5327 case X86::AX: DestReg = X86::AL; break;
5328 case X86::DX: DestReg = X86::DL; break;
5329 case X86::CX: DestReg = X86::CL; break;
5330 case X86::BX: DestReg = X86::BL; break;
5331 }
5332 if (DestReg) {
5333 Res.first = DestReg;
5334 Res.second = Res.second = X86::GR8RegisterClass;
5335 }
5336 } else if (VT == MVT::i32) {
5337 unsigned DestReg = 0;
5338 switch (Res.first) {
5339 default: break;
5340 case X86::AX: DestReg = X86::EAX; break;
5341 case X86::DX: DestReg = X86::EDX; break;
5342 case X86::CX: DestReg = X86::ECX; break;
5343 case X86::BX: DestReg = X86::EBX; break;
5344 case X86::SI: DestReg = X86::ESI; break;
5345 case X86::DI: DestReg = X86::EDI; break;
5346 case X86::BP: DestReg = X86::EBP; break;
5347 case X86::SP: DestReg = X86::ESP; break;
5348 }
5349 if (DestReg) {
5350 Res.first = DestReg;
5351 Res.second = Res.second = X86::GR32RegisterClass;
5352 }
Evan Cheng25ab6902006-09-08 06:48:29 +00005353 } else if (VT == MVT::i64) {
5354 unsigned DestReg = 0;
5355 switch (Res.first) {
5356 default: break;
5357 case X86::AX: DestReg = X86::RAX; break;
5358 case X86::DX: DestReg = X86::RDX; break;
5359 case X86::CX: DestReg = X86::RCX; break;
5360 case X86::BX: DestReg = X86::RBX; break;
5361 case X86::SI: DestReg = X86::RSI; break;
5362 case X86::DI: DestReg = X86::RDI; break;
5363 case X86::BP: DestReg = X86::RBP; break;
5364 case X86::SP: DestReg = X86::RSP; break;
5365 }
5366 if (DestReg) {
5367 Res.first = DestReg;
5368 Res.second = Res.second = X86::GR64RegisterClass;
5369 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00005370 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005371
Chris Lattnerf76d1802006-07-31 23:26:50 +00005372 return Res;
5373}