blob: e7e8c4f19873517cd6fef60eb8326bf4f02986b0 [file] [log] [blame]
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/Function.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000022#include "llvm/ADT/VectorExtras.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000029#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/Target/TargetOptions.h"
31using namespace llvm;
32
33// FIXME: temporary.
34#include "llvm/Support/CommandLine.h"
35static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
36 cl::desc("Enable fastcc on X86"));
37
38X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000040 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
42
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000043 // Set up the TargetLowering object.
44
45 // X86 is weird, it always uses i8 for shift amounts and setcc results.
46 setShiftAmountType(MVT::i8);
47 setSetCCResultType(MVT::i8);
48 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000049 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000051 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng714554d2006-03-16 21:47:42 +000052
Evan Chenga88973f2006-03-22 19:22:18 +000053 if (!Subtarget->isTargetDarwin())
Evan Chengdf57fa02006-03-17 20:31:41 +000054 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
55 setUseUnderscoreSetJmpLongJmp(true);
56
Evan Cheng714554d2006-03-16 21:47:42 +000057 // Add legal addressing mode scale values.
58 addLegalAddressScale(8);
59 addLegalAddressScale(4);
60 addLegalAddressScale(2);
61 // Enter the ones which require both scale + index last. These are more
62 // expensive.
63 addLegalAddressScale(9);
64 addLegalAddressScale(5);
65 addLegalAddressScale(3);
Chris Lattnera54aa942006-01-29 06:26:08 +000066
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000067 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068 addRegisterClass(MVT::i8, X86::R8RegisterClass);
69 addRegisterClass(MVT::i16, X86::R16RegisterClass);
70 addRegisterClass(MVT::i32, X86::R32RegisterClass);
71
72 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
73 // operation.
74 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
75 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000077
78 if (X86ScalarSSE)
79 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
80 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
81 else
82 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000083
84 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
85 // this operation.
86 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
87 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000088 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000089 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000090 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000091 else {
92 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
93 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
94 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000095
Evan Cheng6dab0532006-01-30 08:02:57 +000096 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
97 // isn't legal.
98 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
99 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
100
Evan Cheng02568ff2006-01-30 22:13:22 +0000101 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
102 // this operation.
103 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
104 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
105
106 if (X86ScalarSSE) {
107 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
108 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 }
112
113 // Handle FP_TO_UINT by promoting the destination to a larger signed
114 // conversion.
115 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
116 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
118
Evan Cheng45af8fd2006-02-18 07:26:17 +0000119 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000120 // Expand FP_TO_UINT into a select.
121 // FIXME: We would like to use a Custom expander here eventually to do
122 // the optimal thing for SSE vs. the default expansion in the legalizer.
123 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
124 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000125 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
127
Evan Cheng02568ff2006-01-30 22:13:22 +0000128 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
129 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000130
Evan Cheng5298bcc2006-02-17 07:01:52 +0000131 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000132 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
133 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000134 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
135 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
138 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
139 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
140 setOperationAction(ISD::FREM , MVT::f64 , Expand);
141 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
142 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
143 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
145 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
146 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
148 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
149 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000150 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000151 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000152
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000153 // These should be promoted to a larger select which is supported.
154 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
155 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000156
157 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000158 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
159 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
160 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
162 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
165 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000167 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000168 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000169 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000170 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000171 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000172 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000173 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000174 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
175 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000177 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000178 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
179 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
Chris Lattnerf73bae12005-11-29 06:16:21 +0000181 // We don't have line number support yet.
182 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000183 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000184 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000185 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000186 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000187
Nate Begemanacc398c2006-01-25 18:21:52 +0000188 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
189 setOperationAction(ISD::VASTART , MVT::Other, Custom);
190
191 // Use the default implementation.
192 setOperationAction(ISD::VAARG , MVT::Other, Expand);
193 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
194 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000195 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
196 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
197 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000198
Chris Lattner9601a862006-03-05 05:08:37 +0000199 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
200 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
201
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000202 if (X86ScalarSSE) {
203 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000204 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
205 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000206
207 // SSE has no load+extend ops
208 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
209 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
210
Evan Cheng223547a2006-01-31 22:28:30 +0000211 // Use ANDPD to simulate FABS.
212 setOperationAction(ISD::FABS , MVT::f64, Custom);
213 setOperationAction(ISD::FABS , MVT::f32, Custom);
214
215 // Use XORP to simulate FNEG.
216 setOperationAction(ISD::FNEG , MVT::f64, Custom);
217 setOperationAction(ISD::FNEG , MVT::f32, Custom);
218
Evan Chengd25e9e82006-02-02 00:28:23 +0000219 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 setOperationAction(ISD::FSIN , MVT::f64, Expand);
221 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 setOperationAction(ISD::FREM , MVT::f64, Expand);
223 setOperationAction(ISD::FSIN , MVT::f32, Expand);
224 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f32, Expand);
226
Chris Lattnera54aa942006-01-29 06:26:08 +0000227 // Expand FP immediates into loads from the stack, except for the special
228 // cases we handle.
229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231 addLegalFPImmediate(+0.0); // xorps / xorpd
232 } else {
233 // Set up the FP register classes.
234 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000235
236 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
237
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 if (!UnsafeFPMath) {
239 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
240 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
241 }
242
Chris Lattnera54aa942006-01-29 06:26:08 +0000243 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244 addLegalFPImmediate(+0.0); // FLD0
245 addLegalFPImmediate(+1.0); // FLD1
246 addLegalFPImmediate(-0.0); // FLD0/FCHS
247 addLegalFPImmediate(-1.0); // FLD1/FCHS
248 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000249
Evan Chengd30bf012006-03-01 01:11:20 +0000250 // First set operation action for all vector types to expand. Then we
251 // will selectively turn on ones that can be effectively codegen'd.
252 for (unsigned VT = (unsigned)MVT::Vector + 1;
253 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Chris Lattner39afef32006-03-20 06:18:01 +0000258 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000259 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000260 }
261
Evan Chenga88973f2006-03-22 19:22:18 +0000262 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000263 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
266
Evan Chengd30bf012006-03-01 01:11:20 +0000267 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000268 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000271 }
272
Evan Chenga88973f2006-03-22 19:22:18 +0000273 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
275
Evan Cheng48090aa2006-03-21 23:01:21 +0000276 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
277 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
278 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
279 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
280 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000281 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000282 }
283
Evan Chenga88973f2006-03-22 19:22:18 +0000284 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000285 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
287 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
288 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
289 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
290
291
Evan Cheng48090aa2006-03-21 23:01:21 +0000292 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-03-23 01:57:24 +0000293 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
294 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
295 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng48090aa2006-03-21 23:01:21 +0000296 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
297 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
298 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-03-23 01:57:24 +0000299 setOperationAction(ISD::LOAD, MVT::v16i8, Legal);
300 setOperationAction(ISD::LOAD, MVT::v8i16, Legal);
301 setOperationAction(ISD::LOAD, MVT::v4i32, Legal);
302 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng48090aa2006-03-21 23:01:21 +0000303 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Expand);
304 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Expand);
305 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Expand);
306 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
Evan Cheng48090aa2006-03-21 23:01:21 +0000307 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000309 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000310 }
311
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000312 computeRegisterProperties();
313
Evan Cheng87ed7162006-02-14 08:25:08 +0000314 // FIXME: These should be based on subtarget info. Plus, the values should
315 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000316 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
317 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
318 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000319 allowUnalignedMemoryAccesses = true; // x86 supports it!
320}
321
322std::vector<SDOperand>
323X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
324 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
325 return LowerFastCCArguments(F, DAG);
326 return LowerCCCArguments(F, DAG);
327}
328
329std::pair<SDOperand, SDOperand>
330X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
331 bool isVarArg, unsigned CallingConv,
332 bool isTailCall,
333 SDOperand Callee, ArgListTy &Args,
334 SelectionDAG &DAG) {
335 assert((!isVarArg || CallingConv == CallingConv::C) &&
336 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000337
338 // If the callee is a GlobalAddress node (quite common, every direct call is)
339 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
340 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
341 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000342 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
343 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000344
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000345 if (CallingConv == CallingConv::Fast && EnableFastCC)
346 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
347 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
348}
349
350//===----------------------------------------------------------------------===//
351// C Calling Convention implementation
352//===----------------------------------------------------------------------===//
353
354std::vector<SDOperand>
355X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
356 std::vector<SDOperand> ArgValues;
357
358 MachineFunction &MF = DAG.getMachineFunction();
359 MachineFrameInfo *MFI = MF.getFrameInfo();
360
361 // Add DAG nodes to load the arguments... On entry to a function on the X86,
362 // the stack frame looks like this:
363 //
364 // [ESP] -- return address
365 // [ESP + 4] -- first argument (leftmost lexically)
366 // [ESP + 8] -- second argument, if first argument is four bytes in size
367 // ...
368 //
369 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
370 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
371 MVT::ValueType ObjectVT = getValueType(I->getType());
372 unsigned ArgIncrement = 4;
373 unsigned ObjSize;
374 switch (ObjectVT) {
375 default: assert(0 && "Unhandled argument type!");
376 case MVT::i1:
377 case MVT::i8: ObjSize = 1; break;
378 case MVT::i16: ObjSize = 2; break;
379 case MVT::i32: ObjSize = 4; break;
380 case MVT::i64: ObjSize = ArgIncrement = 8; break;
381 case MVT::f32: ObjSize = 4; break;
382 case MVT::f64: ObjSize = ArgIncrement = 8; break;
383 }
384 // Create the frame index object for this incoming parameter...
385 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
386
387 // Create the SelectionDAG nodes corresponding to a load from this parameter
388 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
389
390 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
391 // dead loads.
392 SDOperand ArgValue;
393 if (!I->use_empty())
394 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
395 DAG.getSrcValue(NULL));
396 else {
397 if (MVT::isInteger(ObjectVT))
398 ArgValue = DAG.getConstant(0, ObjectVT);
399 else
400 ArgValue = DAG.getConstantFP(0, ObjectVT);
401 }
402 ArgValues.push_back(ArgValue);
403
404 ArgOffset += ArgIncrement; // Move on to the next argument...
405 }
406
407 // If the function takes variable number of arguments, make a frame index for
408 // the start of the first vararg value... for expansion of llvm.va_start.
409 if (F.isVarArg())
410 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
411 ReturnAddrIndex = 0; // No return address slot generated yet.
412 BytesToPopOnReturn = 0; // Callee pops nothing.
413 BytesCallerReserves = ArgOffset;
414
415 // Finally, inform the code generator which regs we return values in.
416 switch (getValueType(F.getReturnType())) {
417 default: assert(0 && "Unknown type!");
418 case MVT::isVoid: break;
419 case MVT::i1:
420 case MVT::i8:
421 case MVT::i16:
422 case MVT::i32:
423 MF.addLiveOut(X86::EAX);
424 break;
425 case MVT::i64:
426 MF.addLiveOut(X86::EAX);
427 MF.addLiveOut(X86::EDX);
428 break;
429 case MVT::f32:
430 case MVT::f64:
431 MF.addLiveOut(X86::ST0);
432 break;
433 }
434 return ArgValues;
435}
436
437std::pair<SDOperand, SDOperand>
438X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
439 bool isVarArg, bool isTailCall,
440 SDOperand Callee, ArgListTy &Args,
441 SelectionDAG &DAG) {
442 // Count how many bytes are to be pushed on the stack.
443 unsigned NumBytes = 0;
444
445 if (Args.empty()) {
446 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000447 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000448 } else {
449 for (unsigned i = 0, e = Args.size(); i != e; ++i)
450 switch (getValueType(Args[i].second)) {
451 default: assert(0 && "Unknown value type!");
452 case MVT::i1:
453 case MVT::i8:
454 case MVT::i16:
455 case MVT::i32:
456 case MVT::f32:
457 NumBytes += 4;
458 break;
459 case MVT::i64:
460 case MVT::f64:
461 NumBytes += 8;
462 break;
463 }
464
Chris Lattner94dd2922006-02-13 09:00:43 +0000465 Chain = DAG.getCALLSEQ_START(Chain,
466 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000467
468 // Arguments go on the stack in reverse order, as specified by the ABI.
469 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000470 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000471 std::vector<SDOperand> Stores;
472
473 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
474 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
475 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
476
477 switch (getValueType(Args[i].second)) {
478 default: assert(0 && "Unexpected ValueType for argument!");
479 case MVT::i1:
480 case MVT::i8:
481 case MVT::i16:
482 // Promote the integer to 32 bits. If the input type is signed use a
483 // sign extend, otherwise use a zero extend.
484 if (Args[i].second->isSigned())
485 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
486 else
487 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
488
489 // FALL THROUGH
490 case MVT::i32:
491 case MVT::f32:
492 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
493 Args[i].first, PtrOff,
494 DAG.getSrcValue(NULL)));
495 ArgOffset += 4;
496 break;
497 case MVT::i64:
498 case MVT::f64:
499 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
500 Args[i].first, PtrOff,
501 DAG.getSrcValue(NULL)));
502 ArgOffset += 8;
503 break;
504 }
505 }
506 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
507 }
508
509 std::vector<MVT::ValueType> RetVals;
510 MVT::ValueType RetTyVT = getValueType(RetTy);
511 RetVals.push_back(MVT::Other);
512
513 // The result values produced have to be legal. Promote the result.
514 switch (RetTyVT) {
515 case MVT::isVoid: break;
516 default:
517 RetVals.push_back(RetTyVT);
518 break;
519 case MVT::i1:
520 case MVT::i8:
521 case MVT::i16:
522 RetVals.push_back(MVT::i32);
523 break;
524 case MVT::f32:
525 if (X86ScalarSSE)
526 RetVals.push_back(MVT::f32);
527 else
528 RetVals.push_back(MVT::f64);
529 break;
530 case MVT::i64:
531 RetVals.push_back(MVT::i32);
532 RetVals.push_back(MVT::i32);
533 break;
534 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000535
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000536 std::vector<MVT::ValueType> NodeTys;
537 NodeTys.push_back(MVT::Other); // Returns a chain
538 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
539 std::vector<SDOperand> Ops;
540 Ops.push_back(Chain);
541 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000542
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000543 // FIXME: Do not generate X86ISD::TAILCALL for now.
544 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
545 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000546
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000547 NodeTys.clear();
548 NodeTys.push_back(MVT::Other); // Returns a chain
549 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
550 Ops.clear();
551 Ops.push_back(Chain);
552 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
553 Ops.push_back(DAG.getConstant(0, getPointerTy()));
554 Ops.push_back(InFlag);
555 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
556 InFlag = Chain.getValue(1);
557
558 SDOperand RetVal;
559 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000560 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000561 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000562 case MVT::i1:
563 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000564 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
565 Chain = RetVal.getValue(1);
566 if (RetTyVT == MVT::i1)
567 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
568 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000569 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000570 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
571 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000572 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000573 case MVT::i32:
574 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
575 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000576 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000577 case MVT::i64: {
578 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
579 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
580 Lo.getValue(2));
581 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
582 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000583 break;
584 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000585 case MVT::f32:
586 case MVT::f64: {
587 std::vector<MVT::ValueType> Tys;
588 Tys.push_back(MVT::f64);
589 Tys.push_back(MVT::Other);
590 Tys.push_back(MVT::Flag);
591 std::vector<SDOperand> Ops;
592 Ops.push_back(Chain);
593 Ops.push_back(InFlag);
594 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
595 Chain = RetVal.getValue(1);
596 InFlag = RetVal.getValue(2);
597 if (X86ScalarSSE) {
598 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
599 // shouldn't be necessary except that RFP cannot be live across
600 // multiple blocks. When stackifier is fixed, they can be uncoupled.
601 MachineFunction &MF = DAG.getMachineFunction();
602 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
603 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
604 Tys.clear();
605 Tys.push_back(MVT::Other);
606 Ops.clear();
607 Ops.push_back(Chain);
608 Ops.push_back(RetVal);
609 Ops.push_back(StackSlot);
610 Ops.push_back(DAG.getValueType(RetTyVT));
611 Ops.push_back(InFlag);
612 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
613 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
614 DAG.getSrcValue(NULL));
615 Chain = RetVal.getValue(1);
616 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000617
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000618 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
619 // FIXME: we would really like to remember that this FP_ROUND
620 // operation is okay to eliminate if we allow excess FP precision.
621 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
622 break;
623 }
624 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000625 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000626
627 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000628}
629
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000630//===----------------------------------------------------------------------===//
631// Fast Calling Convention implementation
632//===----------------------------------------------------------------------===//
633//
634// The X86 'fast' calling convention passes up to two integer arguments in
635// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
636// and requires that the callee pop its arguments off the stack (allowing proper
637// tail calls), and has the same return value conventions as C calling convs.
638//
639// This calling convention always arranges for the callee pop value to be 8n+4
640// bytes, which is needed for tail recursion elimination and stack alignment
641// reasons.
642//
643// Note that this can be enhanced in the future to pass fp vals in registers
644// (when we have a global fp allocator) and do other tricks.
645//
646
647/// AddLiveIn - This helper function adds the specified physical register to the
648/// MachineFunction as a live in value. It also creates a corresponding virtual
649/// register for it.
650static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
651 TargetRegisterClass *RC) {
652 assert(RC->contains(PReg) && "Not the correct regclass!");
653 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
654 MF.addLiveIn(PReg, VReg);
655 return VReg;
656}
657
Chris Lattner89fad2c2006-03-17 17:27:47 +0000658// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
659// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
660// EDX". Anything more is illegal.
661//
662// FIXME: The linscan register allocator currently has problem with
663// coallescing. At the time of this writing, whenever it decides to coallesce
664// a physreg with a virtreg, this increases the size of the physreg's live
665// range, and the live range cannot ever be reduced. This causes problems if
666// too many physregs are coalleced with virtregs, which can cause the register
667// allocator to wedge itself.
668//
669// This code triggers this problem more often if we pass args in registers,
670// so disable it until this is fixed.
671//
672// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
673// about code being dead.
674//
675static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000676
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000677
678std::vector<SDOperand>
679X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
680 std::vector<SDOperand> ArgValues;
681
682 MachineFunction &MF = DAG.getMachineFunction();
683 MachineFrameInfo *MFI = MF.getFrameInfo();
684
685 // Add DAG nodes to load the arguments... On entry to a function the stack
686 // frame looks like this:
687 //
688 // [ESP] -- return address
689 // [ESP + 4] -- first nonreg argument (leftmost lexically)
690 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
691 // ...
692 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
693
694 // Keep track of the number of integer regs passed so far. This can be either
695 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
696 // used).
697 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000698
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000699 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
700 MVT::ValueType ObjectVT = getValueType(I->getType());
701 unsigned ArgIncrement = 4;
702 unsigned ObjSize = 0;
703 SDOperand ArgValue;
704
705 switch (ObjectVT) {
706 default: assert(0 && "Unhandled argument type!");
707 case MVT::i1:
708 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000709 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000710 if (!I->use_empty()) {
711 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
712 X86::R8RegisterClass);
713 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
714 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000715 if (ObjectVT == MVT::i1)
716 // FIXME: Should insert a assertzext here.
717 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000718 }
719 ++NumIntRegs;
720 break;
721 }
722
723 ObjSize = 1;
724 break;
725 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000726 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000727 if (!I->use_empty()) {
728 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
729 X86::R16RegisterClass);
730 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
731 DAG.setRoot(ArgValue.getValue(1));
732 }
733 ++NumIntRegs;
734 break;
735 }
736 ObjSize = 2;
737 break;
738 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000739 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000740 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000741 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000742 X86::R32RegisterClass);
743 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
744 DAG.setRoot(ArgValue.getValue(1));
745 }
746 ++NumIntRegs;
747 break;
748 }
749 ObjSize = 4;
750 break;
751 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000752 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000753 if (!I->use_empty()) {
754 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
755 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
756
757 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
758 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
759 DAG.setRoot(Hi.getValue(1));
760
761 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
762 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000763 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000764 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000765 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000766 if (!I->use_empty()) {
767 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
768 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
769 DAG.setRoot(Low.getValue(1));
770
771 // Load the high part from memory.
772 // Create the frame index object for this incoming parameter...
773 int FI = MFI->CreateFixedObject(4, ArgOffset);
774 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
775 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
776 DAG.getSrcValue(NULL));
777 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
778 }
779 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000780 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000781 break;
782 }
783 ObjSize = ArgIncrement = 8;
784 break;
785 case MVT::f32: ObjSize = 4; break;
786 case MVT::f64: ObjSize = ArgIncrement = 8; break;
787 }
788
789 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
790 // dead loads.
791 if (ObjSize && !I->use_empty()) {
792 // Create the frame index object for this incoming parameter...
793 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
794
795 // Create the SelectionDAG nodes corresponding to a load from this
796 // parameter.
797 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
798
799 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
800 DAG.getSrcValue(NULL));
801 } else if (ArgValue.Val == 0) {
802 if (MVT::isInteger(ObjectVT))
803 ArgValue = DAG.getConstant(0, ObjectVT);
804 else
805 ArgValue = DAG.getConstantFP(0, ObjectVT);
806 }
807 ArgValues.push_back(ArgValue);
808
809 if (ObjSize)
810 ArgOffset += ArgIncrement; // Move on to the next argument.
811 }
812
813 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
814 // arguments and the arguments after the retaddr has been pushed are aligned.
815 if ((ArgOffset & 7) == 0)
816 ArgOffset += 4;
817
818 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
819 ReturnAddrIndex = 0; // No return address slot generated yet.
820 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
821 BytesCallerReserves = 0;
822
823 // Finally, inform the code generator which regs we return values in.
824 switch (getValueType(F.getReturnType())) {
825 default: assert(0 && "Unknown type!");
826 case MVT::isVoid: break;
827 case MVT::i1:
828 case MVT::i8:
829 case MVT::i16:
830 case MVT::i32:
831 MF.addLiveOut(X86::EAX);
832 break;
833 case MVT::i64:
834 MF.addLiveOut(X86::EAX);
835 MF.addLiveOut(X86::EDX);
836 break;
837 case MVT::f32:
838 case MVT::f64:
839 MF.addLiveOut(X86::ST0);
840 break;
841 }
842 return ArgValues;
843}
844
845std::pair<SDOperand, SDOperand>
846X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
847 bool isTailCall, SDOperand Callee,
848 ArgListTy &Args, SelectionDAG &DAG) {
849 // Count how many bytes are to be pushed on the stack.
850 unsigned NumBytes = 0;
851
852 // Keep track of the number of integer regs passed so far. This can be either
853 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
854 // used).
855 unsigned NumIntRegs = 0;
856
857 for (unsigned i = 0, e = Args.size(); i != e; ++i)
858 switch (getValueType(Args[i].second)) {
859 default: assert(0 && "Unknown value type!");
860 case MVT::i1:
861 case MVT::i8:
862 case MVT::i16:
863 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000864 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000865 ++NumIntRegs;
866 break;
867 }
868 // fall through
869 case MVT::f32:
870 NumBytes += 4;
871 break;
872 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000873 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
874 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000875 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000876 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
877 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000878 NumBytes += 4;
879 break;
880 }
881
882 // fall through
883 case MVT::f64:
884 NumBytes += 8;
885 break;
886 }
887
888 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
889 // arguments and the arguments after the retaddr has been pushed are aligned.
890 if ((NumBytes & 7) == 0)
891 NumBytes += 4;
892
Chris Lattner94dd2922006-02-13 09:00:43 +0000893 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000894
895 // Arguments go on the stack in reverse order, as specified by the ABI.
896 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000897 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000898 NumIntRegs = 0;
899 std::vector<SDOperand> Stores;
900 std::vector<SDOperand> RegValuesToPass;
901 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
902 switch (getValueType(Args[i].second)) {
903 default: assert(0 && "Unexpected ValueType for argument!");
904 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000905 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
906 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000907 case MVT::i8:
908 case MVT::i16:
909 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000910 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000911 RegValuesToPass.push_back(Args[i].first);
912 ++NumIntRegs;
913 break;
914 }
915 // Fall through
916 case MVT::f32: {
917 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
918 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
919 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
920 Args[i].first, PtrOff,
921 DAG.getSrcValue(NULL)));
922 ArgOffset += 4;
923 break;
924 }
925 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000926 // Can pass (at least) part of it in regs?
927 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000928 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
929 Args[i].first, DAG.getConstant(1, MVT::i32));
930 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
931 Args[i].first, DAG.getConstant(0, MVT::i32));
932 RegValuesToPass.push_back(Lo);
933 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000934
935 // Pass both parts in regs?
936 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000937 RegValuesToPass.push_back(Hi);
938 ++NumIntRegs;
939 } else {
940 // Pass the high part in memory.
941 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
942 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
943 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
944 Hi, PtrOff, DAG.getSrcValue(NULL)));
945 ArgOffset += 4;
946 }
947 break;
948 }
949 // Fall through
950 case MVT::f64:
951 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
952 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
953 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
954 Args[i].first, PtrOff,
955 DAG.getSrcValue(NULL)));
956 ArgOffset += 8;
957 break;
958 }
959 }
960 if (!Stores.empty())
961 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
962
963 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
964 // arguments and the arguments after the retaddr has been pushed are aligned.
965 if ((ArgOffset & 7) == 0)
966 ArgOffset += 4;
967
968 std::vector<MVT::ValueType> RetVals;
969 MVT::ValueType RetTyVT = getValueType(RetTy);
970
971 RetVals.push_back(MVT::Other);
972
973 // The result values produced have to be legal. Promote the result.
974 switch (RetTyVT) {
975 case MVT::isVoid: break;
976 default:
977 RetVals.push_back(RetTyVT);
978 break;
979 case MVT::i1:
980 case MVT::i8:
981 case MVT::i16:
982 RetVals.push_back(MVT::i32);
983 break;
984 case MVT::f32:
985 if (X86ScalarSSE)
986 RetVals.push_back(MVT::f32);
987 else
988 RetVals.push_back(MVT::f64);
989 break;
990 case MVT::i64:
991 RetVals.push_back(MVT::i32);
992 RetVals.push_back(MVT::i32);
993 break;
994 }
995
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000996 // Build a sequence of copy-to-reg nodes chained together with token chain
997 // and flag operands which copy the outgoing args into registers.
998 SDOperand InFlag;
999 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1000 unsigned CCReg;
1001 SDOperand RegToPass = RegValuesToPass[i];
1002 switch (RegToPass.getValueType()) {
1003 default: assert(0 && "Bad thing to pass in regs");
1004 case MVT::i8:
1005 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +00001006 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001007 case MVT::i16:
1008 CCReg = (i == 0) ? X86::AX : X86::DX;
1009 break;
1010 case MVT::i32:
1011 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1012 break;
1013 }
1014
1015 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1016 InFlag = Chain.getValue(1);
1017 }
1018
1019 std::vector<MVT::ValueType> NodeTys;
1020 NodeTys.push_back(MVT::Other); // Returns a chain
1021 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1022 std::vector<SDOperand> Ops;
1023 Ops.push_back(Chain);
1024 Ops.push_back(Callee);
1025 if (InFlag.Val)
1026 Ops.push_back(InFlag);
1027
1028 // FIXME: Do not generate X86ISD::TAILCALL for now.
1029 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1030 InFlag = Chain.getValue(1);
1031
1032 NodeTys.clear();
1033 NodeTys.push_back(MVT::Other); // Returns a chain
1034 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1035 Ops.clear();
1036 Ops.push_back(Chain);
1037 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1038 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1039 Ops.push_back(InFlag);
1040 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1041 InFlag = Chain.getValue(1);
1042
1043 SDOperand RetVal;
1044 if (RetTyVT != MVT::isVoid) {
1045 switch (RetTyVT) {
1046 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001047 case MVT::i1:
1048 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001049 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1050 Chain = RetVal.getValue(1);
1051 if (RetTyVT == MVT::i1)
1052 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1053 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001054 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001055 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1056 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001057 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001058 case MVT::i32:
1059 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1060 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001061 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001062 case MVT::i64: {
1063 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1064 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1065 Lo.getValue(2));
1066 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1067 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001068 break;
1069 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001070 case MVT::f32:
1071 case MVT::f64: {
1072 std::vector<MVT::ValueType> Tys;
1073 Tys.push_back(MVT::f64);
1074 Tys.push_back(MVT::Other);
1075 Tys.push_back(MVT::Flag);
1076 std::vector<SDOperand> Ops;
1077 Ops.push_back(Chain);
1078 Ops.push_back(InFlag);
1079 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1080 Chain = RetVal.getValue(1);
1081 InFlag = RetVal.getValue(2);
1082 if (X86ScalarSSE) {
1083 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1084 // shouldn't be necessary except that RFP cannot be live across
1085 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1086 MachineFunction &MF = DAG.getMachineFunction();
1087 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1088 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1089 Tys.clear();
1090 Tys.push_back(MVT::Other);
1091 Ops.clear();
1092 Ops.push_back(Chain);
1093 Ops.push_back(RetVal);
1094 Ops.push_back(StackSlot);
1095 Ops.push_back(DAG.getValueType(RetTyVT));
1096 Ops.push_back(InFlag);
1097 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1098 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1099 DAG.getSrcValue(NULL));
1100 Chain = RetVal.getValue(1);
1101 }
Evan Chengd9558e02006-01-06 00:43:03 +00001102
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001103 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1104 // FIXME: we would really like to remember that this FP_ROUND
1105 // operation is okay to eliminate if we allow excess FP precision.
1106 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1107 break;
1108 }
1109 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001110 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001111
1112 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001113}
1114
1115SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1116 if (ReturnAddrIndex == 0) {
1117 // Set up a frame object for the return address.
1118 MachineFunction &MF = DAG.getMachineFunction();
1119 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1120 }
1121
1122 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1123}
1124
1125
1126
1127std::pair<SDOperand, SDOperand> X86TargetLowering::
1128LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1129 SelectionDAG &DAG) {
1130 SDOperand Result;
1131 if (Depth) // Depths > 0 not supported yet!
1132 Result = DAG.getConstant(0, getPointerTy());
1133 else {
1134 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1135 if (!isFrameAddress)
1136 // Just load the return address
1137 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1138 DAG.getSrcValue(NULL));
1139 else
1140 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1141 DAG.getConstant(4, MVT::i32));
1142 }
1143 return std::make_pair(Result, Chain);
1144}
1145
Evan Cheng4a460802006-01-11 00:33:36 +00001146/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1147/// which corresponds to the condition code.
1148static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1149 switch (X86CC) {
1150 default: assert(0 && "Unknown X86 conditional code!");
1151 case X86ISD::COND_A: return X86::JA;
1152 case X86ISD::COND_AE: return X86::JAE;
1153 case X86ISD::COND_B: return X86::JB;
1154 case X86ISD::COND_BE: return X86::JBE;
1155 case X86ISD::COND_E: return X86::JE;
1156 case X86ISD::COND_G: return X86::JG;
1157 case X86ISD::COND_GE: return X86::JGE;
1158 case X86ISD::COND_L: return X86::JL;
1159 case X86ISD::COND_LE: return X86::JLE;
1160 case X86ISD::COND_NE: return X86::JNE;
1161 case X86ISD::COND_NO: return X86::JNO;
1162 case X86ISD::COND_NP: return X86::JNP;
1163 case X86ISD::COND_NS: return X86::JNS;
1164 case X86ISD::COND_O: return X86::JO;
1165 case X86ISD::COND_P: return X86::JP;
1166 case X86ISD::COND_S: return X86::JS;
1167 }
1168}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001169
Evan Cheng6dfa9992006-01-30 23:41:35 +00001170/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1171/// specific condition code. It returns a false if it cannot do a direct
1172/// translation. X86CC is the translated CondCode. Flip is set to true if the
1173/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001174static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1175 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001176 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001177 Flip = false;
1178 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001179 if (!isFP) {
1180 switch (SetCCOpcode) {
1181 default: break;
1182 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1183 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1184 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1185 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1186 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1187 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1188 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1189 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1190 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1191 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1192 }
1193 } else {
1194 // On a floating point condition, the flags are set as follows:
1195 // ZF PF CF op
1196 // 0 | 0 | 0 | X > Y
1197 // 0 | 0 | 1 | X < Y
1198 // 1 | 0 | 0 | X == Y
1199 // 1 | 1 | 1 | unordered
1200 switch (SetCCOpcode) {
1201 default: break;
1202 case ISD::SETUEQ:
1203 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001204 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001205 case ISD::SETOGT:
1206 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001207 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001208 case ISD::SETOGE:
1209 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001210 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001211 case ISD::SETULT:
1212 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001213 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001214 case ISD::SETULE:
1215 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1216 case ISD::SETONE:
1217 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1218 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1219 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1220 }
1221 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001222
1223 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001224}
1225
Evan Cheng4a460802006-01-11 00:33:36 +00001226/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1227/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001228/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001229static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001230 switch (X86CC) {
1231 default:
1232 return false;
1233 case X86ISD::COND_B:
1234 case X86ISD::COND_BE:
1235 case X86ISD::COND_E:
1236 case X86ISD::COND_P:
1237 case X86ISD::COND_A:
1238 case X86ISD::COND_AE:
1239 case X86ISD::COND_NE:
1240 case X86ISD::COND_NP:
1241 return true;
1242 }
1243}
1244
Evan Cheng4a460802006-01-11 00:33:36 +00001245MachineBasicBlock *
1246X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1247 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001248 switch (MI->getOpcode()) {
1249 default: assert(false && "Unexpected instr type to insert");
1250 case X86::CMOV_FR32:
1251 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001252 // To "insert" a SELECT_CC instruction, we actually have to insert the
1253 // diamond control-flow pattern. The incoming instruction knows the
1254 // destination vreg to set, the condition code register to branch on, the
1255 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001256 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1257 ilist<MachineBasicBlock>::iterator It = BB;
1258 ++It;
1259
1260 // thisMBB:
1261 // ...
1262 // TrueVal = ...
1263 // cmpTY ccX, r1, r2
1264 // bCC copy1MBB
1265 // fallthrough --> copy0MBB
1266 MachineBasicBlock *thisMBB = BB;
1267 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1268 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1269 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1270 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1271 MachineFunction *F = BB->getParent();
1272 F->getBasicBlockList().insert(It, copy0MBB);
1273 F->getBasicBlockList().insert(It, sinkMBB);
1274 // Update machine-CFG edges
1275 BB->addSuccessor(copy0MBB);
1276 BB->addSuccessor(sinkMBB);
1277
1278 // copy0MBB:
1279 // %FalseValue = ...
1280 // # fallthrough to sinkMBB
1281 BB = copy0MBB;
1282
1283 // Update machine-CFG edges
1284 BB->addSuccessor(sinkMBB);
1285
1286 // sinkMBB:
1287 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1288 // ...
1289 BB = sinkMBB;
1290 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1291 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1292 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001293
Evan Cheng0cc39452006-01-16 21:21:29 +00001294 delete MI; // The pseudo instruction is gone now.
1295 return BB;
1296 }
Evan Cheng4a460802006-01-11 00:33:36 +00001297
Evan Cheng0cc39452006-01-16 21:21:29 +00001298 case X86::FP_TO_INT16_IN_MEM:
1299 case X86::FP_TO_INT32_IN_MEM:
1300 case X86::FP_TO_INT64_IN_MEM: {
1301 // Change the floating point control register to use "round towards zero"
1302 // mode when truncating to an integer value.
1303 MachineFunction *F = BB->getParent();
1304 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1305 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1306
1307 // Load the old value of the high byte of the control word...
1308 unsigned OldCW =
1309 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1310 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1311
1312 // Set the high part to be round to zero...
1313 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1314
1315 // Reload the modified control word now...
1316 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1317
1318 // Restore the memory image of control word to original value
1319 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1320
1321 // Get the X86 opcode to use.
1322 unsigned Opc;
1323 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001324 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001325 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1326 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1327 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1328 }
1329
1330 X86AddressMode AM;
1331 MachineOperand &Op = MI->getOperand(0);
1332 if (Op.isRegister()) {
1333 AM.BaseType = X86AddressMode::RegBase;
1334 AM.Base.Reg = Op.getReg();
1335 } else {
1336 AM.BaseType = X86AddressMode::FrameIndexBase;
1337 AM.Base.FrameIndex = Op.getFrameIndex();
1338 }
1339 Op = MI->getOperand(1);
1340 if (Op.isImmediate())
1341 AM.Scale = Op.getImmedValue();
1342 Op = MI->getOperand(2);
1343 if (Op.isImmediate())
1344 AM.IndexReg = Op.getImmedValue();
1345 Op = MI->getOperand(3);
1346 if (Op.isGlobalAddress()) {
1347 AM.GV = Op.getGlobal();
1348 } else {
1349 AM.Disp = Op.getImmedValue();
1350 }
1351 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1352
1353 // Reload the original control word now.
1354 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1355
1356 delete MI; // The pseudo instruction is gone now.
1357 return BB;
1358 }
1359 }
Evan Cheng4a460802006-01-11 00:33:36 +00001360}
1361
1362
1363//===----------------------------------------------------------------------===//
1364// X86 Custom Lowering Hooks
1365//===----------------------------------------------------------------------===//
1366
Evan Cheng30b37b52006-03-13 23:18:16 +00001367/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1368/// load. For Darwin, external and weak symbols are indirect, loading the value
1369/// at address GV rather then the value of GV itself. This means that the
1370/// GlobalAddress must be in the base or index register of the address, not the
1371/// GV offset field.
1372static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1373 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1374 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1375}
1376
Evan Cheng0188ecb2006-03-22 18:59:22 +00001377/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1378/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1379bool X86::isPSHUFDMask(SDNode *N) {
1380 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1381
1382 if (N->getNumOperands() != 4)
1383 return false;
1384
1385 // Check if the value doesn't reference the second vector.
1386 SDOperand Elt = N->getOperand(0);
1387 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1388 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1389 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1390 "Invalid VECTOR_SHUFFLE mask!");
1391 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() >= 4) return false;
1392 }
1393
1394 return true;
1395}
1396
Evan Chengb9df0ca2006-03-22 02:53:00 +00001397/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1398/// a splat of a single element.
1399bool X86::isSplatMask(SDNode *N) {
1400 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1401
1402 // We can only splat 64-bit, and 32-bit quantities.
1403 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1404 return false;
1405
1406 // This is a splat operation if each element of the permute is the same, and
1407 // if the value doesn't reference the second vector.
1408 SDOperand Elt = N->getOperand(0);
1409 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1410 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1411 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1412 "Invalid VECTOR_SHUFFLE mask!");
1413 if (N->getOperand(i) != Elt) return false;
1414 }
1415
1416 // Make sure it is a splat of the first vector operand.
1417 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1418}
1419
Evan Cheng63d33002006-03-22 08:01:21 +00001420/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1421/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1422/// instructions.
1423unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001424 unsigned NumOperands = N->getNumOperands();
1425 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1426 unsigned Mask = 0;
1427 unsigned i = NumOperands - 1;
1428 do {
1429 Mask |= cast<ConstantSDNode>(N->getOperand(i))->getValue();
1430 Mask <<= Shift;
1431 --i;
1432 } while (i != 0);
1433
1434 return Mask;
1435}
1436
Evan Cheng63d33002006-03-22 08:01:21 +00001437/// getShufflePSHUFDImmediate - Return the appropriate immediate to shuffle
1438/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFD instruction.
1439unsigned X86::getShufflePSHUFDImmediate(SDNode *N) {
1440 unsigned NumOperands = N->getNumOperands();
1441 unsigned Mask = 0;
1442
1443 assert(NumOperands == 4 && "Expect v4f32 / v4i32 vector operand");
1444
1445 unsigned i = NumOperands - 1;
1446 do {
1447 uint64_t Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1448 // Second vector operand must be undef. We can have it point to anything
1449 // we want.
1450 if (Val >= NumOperands) Val = 0;
1451 Mask |= Val;
1452 Mask <<= 2;
1453 --i;
1454 } while (i != 0);
1455
1456 return Mask;
1457}
1458
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001459/// LowerOperation - Provide custom lowering hooks for some operations.
1460///
1461SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1462 switch (Op.getOpcode()) {
1463 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001464 case ISD::SHL_PARTS:
1465 case ISD::SRA_PARTS:
1466 case ISD::SRL_PARTS: {
1467 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1468 "Not an i64 shift!");
1469 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1470 SDOperand ShOpLo = Op.getOperand(0);
1471 SDOperand ShOpHi = Op.getOperand(1);
1472 SDOperand ShAmt = Op.getOperand(2);
1473 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001474 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001475 : DAG.getConstant(0, MVT::i32);
1476
1477 SDOperand Tmp2, Tmp3;
1478 if (Op.getOpcode() == ISD::SHL_PARTS) {
1479 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1480 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1481 } else {
1482 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001483 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001484 }
1485
1486 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1487 ShAmt, DAG.getConstant(32, MVT::i8));
1488
1489 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001490 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001491
1492 std::vector<MVT::ValueType> Tys;
1493 Tys.push_back(MVT::i32);
1494 Tys.push_back(MVT::Flag);
1495 std::vector<SDOperand> Ops;
1496 if (Op.getOpcode() == ISD::SHL_PARTS) {
1497 Ops.push_back(Tmp2);
1498 Ops.push_back(Tmp3);
1499 Ops.push_back(CC);
1500 Ops.push_back(InFlag);
1501 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1502 InFlag = Hi.getValue(1);
1503
1504 Ops.clear();
1505 Ops.push_back(Tmp3);
1506 Ops.push_back(Tmp1);
1507 Ops.push_back(CC);
1508 Ops.push_back(InFlag);
1509 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1510 } else {
1511 Ops.push_back(Tmp2);
1512 Ops.push_back(Tmp3);
1513 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001514 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001515 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1516 InFlag = Lo.getValue(1);
1517
1518 Ops.clear();
1519 Ops.push_back(Tmp3);
1520 Ops.push_back(Tmp1);
1521 Ops.push_back(CC);
1522 Ops.push_back(InFlag);
1523 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1524 }
1525
1526 Tys.clear();
1527 Tys.push_back(MVT::i32);
1528 Tys.push_back(MVT::i32);
1529 Ops.clear();
1530 Ops.push_back(Lo);
1531 Ops.push_back(Hi);
1532 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1533 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001534 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001535 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001536 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001537 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001538
1539 SDOperand Result;
1540 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1541 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001542 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001543 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001544 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001545 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1546 DAG.getEntryNode(), Op.getOperand(0),
1547 StackSlot, DAG.getSrcValue(NULL));
1548
1549 // Build the FILD
1550 std::vector<MVT::ValueType> Tys;
1551 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001552 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001553 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001554 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001555 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001556 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001557 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001558 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1559 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001560
1561 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001562 Chain = Result.getValue(1);
1563 SDOperand InFlag = Result.getValue(2);
1564
Evan Chenge3de85b2006-02-04 02:20:30 +00001565 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001566 // shouldn't be necessary except that RFP cannot be live across
1567 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1568 MachineFunction &MF = DAG.getMachineFunction();
1569 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1570 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1571 std::vector<MVT::ValueType> Tys;
1572 Tys.push_back(MVT::Other);
1573 std::vector<SDOperand> Ops;
1574 Ops.push_back(Chain);
1575 Ops.push_back(Result);
1576 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001577 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001578 Ops.push_back(InFlag);
1579 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1580 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1581 DAG.getSrcValue(NULL));
1582 }
1583
Evan Chenga3195e82006-01-12 22:54:21 +00001584 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001585 }
1586 case ISD::FP_TO_SINT: {
1587 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001588 "Unknown FP_TO_SINT to lower!");
1589 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1590 // stack slot.
1591 MachineFunction &MF = DAG.getMachineFunction();
1592 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1593 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1594 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1595
1596 unsigned Opc;
1597 switch (Op.getValueType()) {
1598 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1599 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1600 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1601 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1602 }
1603
Evan Cheng6dab0532006-01-30 08:02:57 +00001604 SDOperand Chain = DAG.getEntryNode();
1605 SDOperand Value = Op.getOperand(0);
1606 if (X86ScalarSSE) {
1607 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1608 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1609 DAG.getSrcValue(0));
1610 std::vector<MVT::ValueType> Tys;
1611 Tys.push_back(MVT::f64);
1612 Tys.push_back(MVT::Other);
1613 std::vector<SDOperand> Ops;
1614 Ops.push_back(Chain);
1615 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001616 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001617 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1618 Chain = Value.getValue(1);
1619 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1620 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1621 }
1622
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001623 // Build the FP_TO_INT*_IN_MEM
1624 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001625 Ops.push_back(Chain);
1626 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001627 Ops.push_back(StackSlot);
1628 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1629
1630 // Load the result.
1631 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1632 DAG.getSrcValue(NULL));
1633 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001634 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001635 std::vector<MVT::ValueType> Tys;
1636 Tys.push_back(MVT::Other);
1637 Tys.push_back(MVT::Flag);
1638 std::vector<SDOperand> Ops;
1639 Ops.push_back(Op.getOperand(0));
1640 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001641 Ops.clear();
1642 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1643 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1644 MVT::i32, Ops[0].getValue(2)));
1645 Ops.push_back(Ops[1].getValue(1));
1646 Tys[0] = Tys[1] = MVT::i32;
1647 Tys.push_back(MVT::Other);
1648 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001649 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001650 case ISD::FABS: {
1651 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001652 const Type *OpNTy = MVT::getTypeForValueType(VT);
1653 std::vector<Constant*> CV;
1654 if (VT == MVT::f64) {
1655 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1656 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1657 } else {
1658 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1659 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1660 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1661 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1662 }
1663 Constant *CS = ConstantStruct::get(CV);
1664 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1665 SDOperand Mask
1666 = DAG.getNode(X86ISD::LOAD_PACK,
1667 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001668 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1669 }
Evan Cheng223547a2006-01-31 22:28:30 +00001670 case ISD::FNEG: {
1671 MVT::ValueType VT = Op.getValueType();
1672 const Type *OpNTy = MVT::getTypeForValueType(VT);
1673 std::vector<Constant*> CV;
1674 if (VT == MVT::f64) {
1675 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1676 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1677 } else {
1678 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1679 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1680 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1681 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1682 }
1683 Constant *CS = ConstantStruct::get(CV);
1684 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1685 SDOperand Mask
1686 = DAG.getNode(X86ISD::LOAD_PACK,
1687 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1688 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1689 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001690 case ISD::SETCC: {
1691 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001692 SDOperand Cond;
1693 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001694 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1695 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001696 bool Flip;
1697 unsigned X86CC;
1698 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1699 if (Flip)
1700 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1701 Op.getOperand(1), Op.getOperand(0));
1702 else
1703 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1704 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001705 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1706 DAG.getConstant(X86CC, MVT::i8), Cond);
1707 } else {
1708 assert(isFP && "Illegal integer SetCC!");
1709
Evan Cheng6dfa9992006-01-30 23:41:35 +00001710 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1711 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001712 std::vector<MVT::ValueType> Tys;
1713 std::vector<SDOperand> Ops;
1714 switch (SetCCOpcode) {
1715 default: assert(false && "Illegal floating point SetCC!");
1716 case ISD::SETOEQ: { // !PF & ZF
1717 Tys.push_back(MVT::i8);
1718 Tys.push_back(MVT::Flag);
1719 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1720 Ops.push_back(Cond);
1721 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1722 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1723 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1724 Tmp1.getValue(1));
1725 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1726 }
Evan Chengd9558e02006-01-06 00:43:03 +00001727 case ISD::SETUNE: { // PF | !ZF
1728 Tys.push_back(MVT::i8);
1729 Tys.push_back(MVT::Flag);
1730 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1731 Ops.push_back(Cond);
1732 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1733 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1734 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1735 Tmp1.getValue(1));
1736 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1737 }
1738 }
1739 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001740 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001741 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001742 MVT::ValueType VT = Op.getValueType();
1743 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001744 bool isFPStack = isFP && !X86ScalarSSE;
1745 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001746 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001747 SDOperand Op0 = Op.getOperand(0);
1748 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001749 if (Op0.getOpcode() == ISD::SETCC)
1750 Op0 = LowerOperation(Op0, DAG);
1751
Evan Chengaaca22c2006-01-10 20:26:56 +00001752 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001753 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1754 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1755 // have another use it will be eliminated.
1756 // If the X86ISD::SETCC has more than one use, then it's probably better
1757 // to use a test instead of duplicating the X86ISD::CMP (for register
1758 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001759 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1760 if (!Op0.hasOneUse()) {
1761 std::vector<MVT::ValueType> Tys;
1762 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1763 Tys.push_back(Op0.Val->getValueType(i));
1764 std::vector<SDOperand> Ops;
1765 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1766 Ops.push_back(Op0.getOperand(i));
1767 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1768 }
1769
Evan Cheng1bcee362006-01-13 01:03:02 +00001770 CC = Op0.getOperand(0);
1771 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001772 // Make a copy as flag result cannot be used by more than one.
1773 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1774 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001775 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001776 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001777 } else
1778 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001779 } else
1780 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001781
Evan Cheng189d01e2006-01-13 01:06:49 +00001782 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001783 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001784 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001785 }
Evan Chenge3413162006-01-09 18:33:28 +00001786
1787 std::vector<MVT::ValueType> Tys;
1788 Tys.push_back(Op.getValueType());
1789 Tys.push_back(MVT::Flag);
1790 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001791 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1792 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001793 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001794 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001795 Ops.push_back(CC);
1796 Ops.push_back(Cond);
1797 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001798 }
Evan Cheng898101c2005-12-19 23:12:38 +00001799 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001800 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001801 SDOperand Cond = Op.getOperand(1);
1802 SDOperand Dest = Op.getOperand(2);
1803 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001804 if (Cond.getOpcode() == ISD::SETCC)
1805 Cond = LowerOperation(Cond, DAG);
1806
Evan Chengd5781fc2005-12-21 20:21:51 +00001807 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001808 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1809 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1810 // have another use it will be eliminated.
1811 // If the X86ISD::SETCC has more than one use, then it's probably better
1812 // to use a test instead of duplicating the X86ISD::CMP (for register
1813 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001814 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1815 if (!Cond.hasOneUse()) {
1816 std::vector<MVT::ValueType> Tys;
1817 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1818 Tys.push_back(Cond.Val->getValueType(i));
1819 std::vector<SDOperand> Ops;
1820 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1821 Ops.push_back(Cond.getOperand(i));
1822 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1823 }
1824
Evan Cheng1bcee362006-01-13 01:03:02 +00001825 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001826 Cond = Cond.getOperand(1);
1827 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001828 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001829 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001830 } else
1831 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001832 } else
1833 addTest = true;
1834
1835 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001836 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001837 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1838 }
1839 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1840 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1841 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001842 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001843 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001844 SDOperand Chain = Op.getOperand(0);
1845 unsigned Align =
1846 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1847 if (Align == 0) Align = 1;
1848
Evan Cheng18a84522006-02-16 00:21:07 +00001849 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1850 // If not DWORD aligned, call memset if size is less than the threshold.
1851 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001852 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001853 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001854 MVT::ValueType IntPtr = getPointerTy();
1855 const Type *IntPtrTy = getTargetData().getIntPtrType();
1856 std::vector<std::pair<SDOperand, const Type*> > Args;
1857 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1858 // Extend the ubyte argument to be an int value for the call.
1859 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1860 Args.push_back(std::make_pair(Val, IntPtrTy));
1861 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1862 std::pair<SDOperand,SDOperand> CallResult =
1863 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1864 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1865 return CallResult.second;
1866 }
1867
Evan Cheng67f92a72006-01-11 22:15:48 +00001868 MVT::ValueType AVT;
1869 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001870 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1871 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001872 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001873 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001874 unsigned ValReg;
1875 unsigned Val = ValC->getValue() & 255;
1876
1877 // If the value is a constant, then we can potentially use larger sets.
1878 switch (Align & 3) {
1879 case 2: // WORD aligned
1880 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001881 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1882 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001883 Val = (Val << 8) | Val;
1884 ValReg = X86::AX;
1885 break;
1886 case 0: // DWORD aligned
1887 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001888 if (I) {
1889 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1890 BytesLeft = I->getValue() % 4;
1891 } else {
1892 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1893 DAG.getConstant(2, MVT::i8));
1894 TwoRepStos = true;
1895 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001896 Val = (Val << 8) | Val;
1897 Val = (Val << 16) | Val;
1898 ValReg = X86::EAX;
1899 break;
1900 default: // Byte aligned
1901 AVT = MVT::i8;
1902 Count = Op.getOperand(3);
1903 ValReg = X86::AL;
1904 break;
1905 }
1906
1907 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1908 InFlag);
1909 InFlag = Chain.getValue(1);
1910 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001911 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001912 Count = Op.getOperand(3);
1913 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1914 InFlag = Chain.getValue(1);
1915 }
1916
1917 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1918 InFlag = Chain.getValue(1);
1919 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1920 InFlag = Chain.getValue(1);
1921
Evan Chengff909922006-03-07 23:29:39 +00001922 std::vector<MVT::ValueType> Tys;
1923 Tys.push_back(MVT::Other);
1924 Tys.push_back(MVT::Flag);
1925 std::vector<SDOperand> Ops;
1926 Ops.push_back(Chain);
1927 Ops.push_back(DAG.getValueType(AVT));
1928 Ops.push_back(InFlag);
1929 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1930
1931 if (TwoRepStos) {
1932 InFlag = Chain.getValue(1);
1933 Count = Op.getOperand(3);
1934 MVT::ValueType CVT = Count.getValueType();
1935 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1936 DAG.getConstant(3, CVT));
1937 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1938 InFlag = Chain.getValue(1);
1939 Tys.clear();
1940 Tys.push_back(MVT::Other);
1941 Tys.push_back(MVT::Flag);
1942 Ops.clear();
1943 Ops.push_back(Chain);
1944 Ops.push_back(DAG.getValueType(MVT::i8));
1945 Ops.push_back(InFlag);
1946 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1947 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001948 // Issue stores for the last 1 - 3 bytes.
1949 SDOperand Value;
1950 unsigned Val = ValC->getValue() & 255;
1951 unsigned Offset = I->getValue() - BytesLeft;
1952 SDOperand DstAddr = Op.getOperand(1);
1953 MVT::ValueType AddrVT = DstAddr.getValueType();
1954 if (BytesLeft >= 2) {
1955 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1956 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1957 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1958 DAG.getConstant(Offset, AddrVT)),
1959 DAG.getSrcValue(NULL));
1960 BytesLeft -= 2;
1961 Offset += 2;
1962 }
1963
1964 if (BytesLeft == 1) {
1965 Value = DAG.getConstant(Val, MVT::i8);
1966 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1967 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1968 DAG.getConstant(Offset, AddrVT)),
1969 DAG.getSrcValue(NULL));
1970 }
1971 }
1972
1973 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001974 }
1975 case ISD::MEMCPY: {
1976 SDOperand Chain = Op.getOperand(0);
1977 unsigned Align =
1978 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1979 if (Align == 0) Align = 1;
1980
Evan Cheng18a84522006-02-16 00:21:07 +00001981 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1982 // If not DWORD aligned, call memcpy if size is less than the threshold.
1983 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001984 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001985 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001986 MVT::ValueType IntPtr = getPointerTy();
1987 const Type *IntPtrTy = getTargetData().getIntPtrType();
1988 std::vector<std::pair<SDOperand, const Type*> > Args;
1989 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1990 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1991 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1992 std::pair<SDOperand,SDOperand> CallResult =
1993 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1994 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1995 return CallResult.second;
1996 }
1997
Evan Cheng67f92a72006-01-11 22:15:48 +00001998 MVT::ValueType AVT;
1999 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002000 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002001 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00002002 switch (Align & 3) {
2003 case 2: // WORD aligned
2004 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002005 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2006 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002007 break;
2008 case 0: // DWORD aligned
2009 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002010 if (I) {
2011 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2012 BytesLeft = I->getValue() % 4;
2013 } else {
2014 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2015 DAG.getConstant(2, MVT::i8));
2016 TwoRepMovs = true;
2017 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002018 break;
2019 default: // Byte aligned
2020 AVT = MVT::i8;
2021 Count = Op.getOperand(3);
2022 break;
2023 }
2024
Evan Cheng62bec2c2006-03-04 02:48:56 +00002025 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002026 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2027 InFlag = Chain.getValue(1);
2028 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2029 InFlag = Chain.getValue(1);
2030 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2031 InFlag = Chain.getValue(1);
2032
Evan Chengff909922006-03-07 23:29:39 +00002033 std::vector<MVT::ValueType> Tys;
2034 Tys.push_back(MVT::Other);
2035 Tys.push_back(MVT::Flag);
2036 std::vector<SDOperand> Ops;
2037 Ops.push_back(Chain);
2038 Ops.push_back(DAG.getValueType(AVT));
2039 Ops.push_back(InFlag);
2040 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2041
2042 if (TwoRepMovs) {
2043 InFlag = Chain.getValue(1);
2044 Count = Op.getOperand(3);
2045 MVT::ValueType CVT = Count.getValueType();
2046 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2047 DAG.getConstant(3, CVT));
2048 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2049 InFlag = Chain.getValue(1);
2050 Tys.clear();
2051 Tys.push_back(MVT::Other);
2052 Tys.push_back(MVT::Flag);
2053 Ops.clear();
2054 Ops.push_back(Chain);
2055 Ops.push_back(DAG.getValueType(MVT::i8));
2056 Ops.push_back(InFlag);
2057 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2058 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002059 // Issue loads and stores for the last 1 - 3 bytes.
2060 unsigned Offset = I->getValue() - BytesLeft;
2061 SDOperand DstAddr = Op.getOperand(1);
2062 MVT::ValueType DstVT = DstAddr.getValueType();
2063 SDOperand SrcAddr = Op.getOperand(2);
2064 MVT::ValueType SrcVT = SrcAddr.getValueType();
2065 SDOperand Value;
2066 if (BytesLeft >= 2) {
2067 Value = DAG.getLoad(MVT::i16, Chain,
2068 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2069 DAG.getConstant(Offset, SrcVT)),
2070 DAG.getSrcValue(NULL));
2071 Chain = Value.getValue(1);
2072 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2073 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2074 DAG.getConstant(Offset, DstVT)),
2075 DAG.getSrcValue(NULL));
2076 BytesLeft -= 2;
2077 Offset += 2;
2078 }
2079
2080 if (BytesLeft == 1) {
2081 Value = DAG.getLoad(MVT::i8, Chain,
2082 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2083 DAG.getConstant(Offset, SrcVT)),
2084 DAG.getSrcValue(NULL));
2085 Chain = Value.getValue(1);
2086 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2087 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2088 DAG.getConstant(Offset, DstVT)),
2089 DAG.getSrcValue(NULL));
2090 }
2091 }
2092
2093 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002094 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002095
2096 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2097 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2098 // one of the above mentioned nodes. It has to be wrapped because otherwise
2099 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2100 // be used to form addressing mode. These wrapped nodes will be selected
2101 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002102 case ISD::ConstantPool: {
2103 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002104 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2105 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2106 CP->getAlignment()));
Evan Chenga88973f2006-03-22 19:22:18 +00002107 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002108 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002109 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002110 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2111 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2112 }
2113
2114 return Result;
2115 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002116 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002117 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2118 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2119 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002120 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002121 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002122 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002123 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2124 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002125
2126 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002127 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002128 // the GlobalAddress must be in the base or index register of the address,
2129 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002130 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002131 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002132 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002133 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002134 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002135
Evan Cheng002fe9b2006-01-12 07:56:47 +00002136 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002137 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002138 case ISD::ExternalSymbol: {
2139 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2140 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2141 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002142 if (Subtarget->isTargetDarwin()) {
Evan Cheng020d2e82006-02-23 20:41:18 +00002143 // With PIC, the address is actually $g + Offset.
2144 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2145 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2146 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2147 }
2148
2149 return Result;
2150 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002151 case ISD::VASTART: {
2152 // vastart just stores the address of the VarArgsFrameIndex slot into the
2153 // memory location argument.
2154 // FIXME: Replace MVT::i32 with PointerTy
2155 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2156 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2157 Op.getOperand(1), Op.getOperand(2));
2158 }
Nate Begemanee625572006-01-27 21:09:22 +00002159 case ISD::RET: {
2160 SDOperand Copy;
2161
2162 switch(Op.getNumOperands()) {
2163 default:
2164 assert(0 && "Do not know how to return this many arguments!");
2165 abort();
2166 case 1:
2167 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2168 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2169 case 2: {
2170 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2171 if (MVT::isInteger(ArgVT))
2172 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2173 SDOperand());
2174 else if (!X86ScalarSSE) {
2175 std::vector<MVT::ValueType> Tys;
2176 Tys.push_back(MVT::Other);
2177 Tys.push_back(MVT::Flag);
2178 std::vector<SDOperand> Ops;
2179 Ops.push_back(Op.getOperand(0));
2180 Ops.push_back(Op.getOperand(1));
2181 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2182 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002183 SDOperand MemLoc;
2184 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002185 SDOperand Value = Op.getOperand(1);
2186
Evan Cheng760df292006-02-01 01:19:32 +00002187 if (Value.getOpcode() == ISD::LOAD &&
2188 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002189 Chain = Value.getOperand(0);
2190 MemLoc = Value.getOperand(1);
2191 } else {
2192 // Spill the value to memory and reload it into top of stack.
2193 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2194 MachineFunction &MF = DAG.getMachineFunction();
2195 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2196 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2197 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2198 Value, MemLoc, DAG.getSrcValue(0));
2199 }
Nate Begemanee625572006-01-27 21:09:22 +00002200 std::vector<MVT::ValueType> Tys;
2201 Tys.push_back(MVT::f64);
2202 Tys.push_back(MVT::Other);
2203 std::vector<SDOperand> Ops;
2204 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002205 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002206 Ops.push_back(DAG.getValueType(ArgVT));
2207 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2208 Tys.clear();
2209 Tys.push_back(MVT::Other);
2210 Tys.push_back(MVT::Flag);
2211 Ops.clear();
2212 Ops.push_back(Copy.getValue(1));
2213 Ops.push_back(Copy);
2214 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2215 }
2216 break;
2217 }
2218 case 3:
2219 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2220 SDOperand());
2221 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2222 break;
2223 }
2224 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2225 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2226 Copy.getValue(1));
2227 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002228 case ISD::SCALAR_TO_VECTOR: {
2229 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2230 return DAG.getNode(X86ISD::SCALAR_TO_VECTOR, Op.getValueType(), AnyExt);
2231 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002232 case ISD::VECTOR_SHUFFLE: {
2233 SDOperand V1 = Op.getOperand(0);
2234 SDOperand V2 = Op.getOperand(1);
2235 SDOperand PermMask = Op.getOperand(2);
2236 MVT::ValueType VT = Op.getValueType();
2237
Evan Cheng0188ecb2006-03-22 18:59:22 +00002238 // Handle splat cases.
2239 if (X86::isSplatMask(PermMask.Val)) {
2240 if (V2.getOpcode() == ISD::UNDEF)
Evan Chengb9df0ca2006-03-22 02:53:00 +00002241 // Leave the VECTOR_SHUFFLE alone. It matches SHUFP*.
Chris Lattner6df11542006-03-22 04:18:34 +00002242 return SDOperand();
Evan Cheng0188ecb2006-03-22 18:59:22 +00002243 else
2244 // Make it match SHUFP* or UNPCKLPD. Second vector is undef since it's
2245 // not needed.
2246 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2247 DAG.getNode(ISD::UNDEF, V1.getValueType()),
2248 PermMask);
Evan Cheng1bffadd2006-03-22 19:16:21 +00002249 } else if (Subtarget->hasSSE2() && X86::isPSHUFDMask(PermMask.Val)) {
Evan Cheng0188ecb2006-03-22 18:59:22 +00002250 if (V2.getOpcode() == ISD::UNDEF)
Evan Chengb9df0ca2006-03-22 02:53:00 +00002251 // Leave the VECTOR_SHUFFLE alone. It matches PSHUFD.
Chris Lattner6df11542006-03-22 04:18:34 +00002252 return SDOperand();
Evan Cheng0188ecb2006-03-22 18:59:22 +00002253 else
2254 // Make it match PSHUFD. Second vector is undef since it's not needed.
2255 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2256 DAG.getNode(ISD::UNDEF, V1.getValueType()),
2257 PermMask);
Evan Chengb9df0ca2006-03-22 02:53:00 +00002258 }
2259
2260 // TODO.
Chris Lattner6df11542006-03-22 04:18:34 +00002261 assert(0 && "TODO");
2262 abort();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002263 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002264 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002265}
Evan Cheng72261582005-12-20 06:22:03 +00002266
2267const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2268 switch (Opcode) {
2269 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002270 case X86ISD::SHLD: return "X86ISD::SHLD";
2271 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002272 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002273 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002274 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002275 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002276 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2277 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2278 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002279 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002280 case X86ISD::FST: return "X86ISD::FST";
2281 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002282 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002283 case X86ISD::CALL: return "X86ISD::CALL";
2284 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2285 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2286 case X86ISD::CMP: return "X86ISD::CMP";
2287 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002288 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002289 case X86ISD::CMOV: return "X86ISD::CMOV";
2290 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002291 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002292 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2293 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002294 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002295 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002296 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng48090aa2006-03-21 23:01:21 +00002297 case X86ISD::SCALAR_TO_VECTOR: return "X86ISD::SCALAR_TO_VECTOR";
Evan Cheng72261582005-12-20 06:22:03 +00002298 }
2299}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002300
Nate Begeman368e18d2006-02-16 21:11:51 +00002301void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2302 uint64_t Mask,
2303 uint64_t &KnownZero,
2304 uint64_t &KnownOne,
2305 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002306
2307 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002308 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002309
2310 switch (Opc) {
2311 default:
2312 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2313 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002314 case X86ISD::SETCC:
2315 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2316 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002317 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002318}
Chris Lattner259e97c2006-01-31 19:43:35 +00002319
2320std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002321getRegClassForInlineAsmConstraint(const std::string &Constraint,
2322 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002323 if (Constraint.size() == 1) {
2324 // FIXME: not handling fp-stack yet!
2325 // FIXME: not handling MMX registers yet ('y' constraint).
2326 switch (Constraint[0]) { // GCC X86 Constraint Letters
2327 default: break; // Unknown constriant letter
2328 case 'r': // GENERAL_REGS
2329 case 'R': // LEGACY_REGS
2330 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2331 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2332 case 'l': // INDEX_REGS
2333 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2334 X86::ESI, X86::EDI, X86::EBP, 0);
2335 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2336 case 'Q': // Q_REGS
2337 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2338 case 'x': // SSE_REGS if SSE1 allowed
2339 if (Subtarget->hasSSE1())
2340 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2341 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2342 0);
2343 return std::vector<unsigned>();
2344 case 'Y': // SSE_REGS if SSE2 allowed
2345 if (Subtarget->hasSSE2())
2346 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2347 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2348 0);
2349 return std::vector<unsigned>();
2350 }
2351 }
2352
Chris Lattner1efa40f2006-02-22 00:56:39 +00002353 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002354}
Evan Cheng30b37b52006-03-13 23:18:16 +00002355
2356/// isLegalAddressImmediate - Return true if the integer value or
2357/// GlobalValue can be used as the offset of the target addressing mode.
2358bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2359 // X86 allows a sign-extended 32-bit immediate field.
2360 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2361}
2362
2363bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00002364 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00002365 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2366 if (RModel == Reloc::Static)
2367 return true;
2368 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002369 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002370 else
2371 return false;
2372 } else
2373 return true;
2374}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002375
2376/// isShuffleMaskLegal - Targets can use this to indicate that they only
2377/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2378/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2379/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00002380bool
2381X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2382 // Only do shuffles on 128-bit vector types for now.
2383 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng1bffadd2006-03-22 19:16:21 +00002384 return (X86::isSplatMask(Mask.Val) ||
2385 (Subtarget->hasSSE2() && X86::isPSHUFDMask(Mask.Val)));
Evan Cheng0188ecb2006-03-22 18:59:22 +00002386}