blob: 0a9f4b4ed71827499b0562ebcef0494f54ce4294 [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);
Evan Cheng386031a2006-03-24 07:29:27 +0000280 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
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 Cheng386031a2006-03-24 07:29:27 +0000303 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
304 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
305 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
306 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
307 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
Evan Cheng48090aa2006-03-21 23:01:21 +0000308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000310 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000311 }
312
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313 computeRegisterProperties();
314
Evan Cheng87ed7162006-02-14 08:25:08 +0000315 // FIXME: These should be based on subtarget info. Plus, the values should
316 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000317 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
318 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
319 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320 allowUnalignedMemoryAccesses = true; // x86 supports it!
321}
322
323std::vector<SDOperand>
324X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
325 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
326 return LowerFastCCArguments(F, DAG);
327 return LowerCCCArguments(F, DAG);
328}
329
330std::pair<SDOperand, SDOperand>
331X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
332 bool isVarArg, unsigned CallingConv,
333 bool isTailCall,
334 SDOperand Callee, ArgListTy &Args,
335 SelectionDAG &DAG) {
336 assert((!isVarArg || CallingConv == CallingConv::C) &&
337 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000338
339 // If the callee is a GlobalAddress node (quite common, every direct call is)
340 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
341 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
342 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000343 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
344 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000345
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000346 if (CallingConv == CallingConv::Fast && EnableFastCC)
347 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
348 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
349}
350
351//===----------------------------------------------------------------------===//
352// C Calling Convention implementation
353//===----------------------------------------------------------------------===//
354
355std::vector<SDOperand>
356X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
357 std::vector<SDOperand> ArgValues;
358
359 MachineFunction &MF = DAG.getMachineFunction();
360 MachineFrameInfo *MFI = MF.getFrameInfo();
361
362 // Add DAG nodes to load the arguments... On entry to a function on the X86,
363 // the stack frame looks like this:
364 //
365 // [ESP] -- return address
366 // [ESP + 4] -- first argument (leftmost lexically)
367 // [ESP + 8] -- second argument, if first argument is four bytes in size
368 // ...
369 //
370 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
371 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
372 MVT::ValueType ObjectVT = getValueType(I->getType());
373 unsigned ArgIncrement = 4;
374 unsigned ObjSize;
375 switch (ObjectVT) {
376 default: assert(0 && "Unhandled argument type!");
377 case MVT::i1:
378 case MVT::i8: ObjSize = 1; break;
379 case MVT::i16: ObjSize = 2; break;
380 case MVT::i32: ObjSize = 4; break;
381 case MVT::i64: ObjSize = ArgIncrement = 8; break;
382 case MVT::f32: ObjSize = 4; break;
383 case MVT::f64: ObjSize = ArgIncrement = 8; break;
384 }
385 // Create the frame index object for this incoming parameter...
386 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
387
388 // Create the SelectionDAG nodes corresponding to a load from this parameter
389 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
390
391 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
392 // dead loads.
393 SDOperand ArgValue;
394 if (!I->use_empty())
395 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
396 DAG.getSrcValue(NULL));
397 else {
398 if (MVT::isInteger(ObjectVT))
399 ArgValue = DAG.getConstant(0, ObjectVT);
400 else
401 ArgValue = DAG.getConstantFP(0, ObjectVT);
402 }
403 ArgValues.push_back(ArgValue);
404
405 ArgOffset += ArgIncrement; // Move on to the next argument...
406 }
407
408 // If the function takes variable number of arguments, make a frame index for
409 // the start of the first vararg value... for expansion of llvm.va_start.
410 if (F.isVarArg())
411 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
412 ReturnAddrIndex = 0; // No return address slot generated yet.
413 BytesToPopOnReturn = 0; // Callee pops nothing.
414 BytesCallerReserves = ArgOffset;
415
416 // Finally, inform the code generator which regs we return values in.
417 switch (getValueType(F.getReturnType())) {
418 default: assert(0 && "Unknown type!");
419 case MVT::isVoid: break;
420 case MVT::i1:
421 case MVT::i8:
422 case MVT::i16:
423 case MVT::i32:
424 MF.addLiveOut(X86::EAX);
425 break;
426 case MVT::i64:
427 MF.addLiveOut(X86::EAX);
428 MF.addLiveOut(X86::EDX);
429 break;
430 case MVT::f32:
431 case MVT::f64:
432 MF.addLiveOut(X86::ST0);
433 break;
434 }
435 return ArgValues;
436}
437
438std::pair<SDOperand, SDOperand>
439X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
440 bool isVarArg, bool isTailCall,
441 SDOperand Callee, ArgListTy &Args,
442 SelectionDAG &DAG) {
443 // Count how many bytes are to be pushed on the stack.
444 unsigned NumBytes = 0;
445
446 if (Args.empty()) {
447 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000448 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000449 } else {
450 for (unsigned i = 0, e = Args.size(); i != e; ++i)
451 switch (getValueType(Args[i].second)) {
452 default: assert(0 && "Unknown value type!");
453 case MVT::i1:
454 case MVT::i8:
455 case MVT::i16:
456 case MVT::i32:
457 case MVT::f32:
458 NumBytes += 4;
459 break;
460 case MVT::i64:
461 case MVT::f64:
462 NumBytes += 8;
463 break;
464 }
465
Chris Lattner94dd2922006-02-13 09:00:43 +0000466 Chain = DAG.getCALLSEQ_START(Chain,
467 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000468
469 // Arguments go on the stack in reverse order, as specified by the ABI.
470 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000471 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000472 std::vector<SDOperand> Stores;
473
474 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
475 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
476 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
477
478 switch (getValueType(Args[i].second)) {
479 default: assert(0 && "Unexpected ValueType for argument!");
480 case MVT::i1:
481 case MVT::i8:
482 case MVT::i16:
483 // Promote the integer to 32 bits. If the input type is signed use a
484 // sign extend, otherwise use a zero extend.
485 if (Args[i].second->isSigned())
486 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
487 else
488 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
489
490 // FALL THROUGH
491 case MVT::i32:
492 case MVT::f32:
493 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
494 Args[i].first, PtrOff,
495 DAG.getSrcValue(NULL)));
496 ArgOffset += 4;
497 break;
498 case MVT::i64:
499 case MVT::f64:
500 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
501 Args[i].first, PtrOff,
502 DAG.getSrcValue(NULL)));
503 ArgOffset += 8;
504 break;
505 }
506 }
507 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
508 }
509
510 std::vector<MVT::ValueType> RetVals;
511 MVT::ValueType RetTyVT = getValueType(RetTy);
512 RetVals.push_back(MVT::Other);
513
514 // The result values produced have to be legal. Promote the result.
515 switch (RetTyVT) {
516 case MVT::isVoid: break;
517 default:
518 RetVals.push_back(RetTyVT);
519 break;
520 case MVT::i1:
521 case MVT::i8:
522 case MVT::i16:
523 RetVals.push_back(MVT::i32);
524 break;
525 case MVT::f32:
526 if (X86ScalarSSE)
527 RetVals.push_back(MVT::f32);
528 else
529 RetVals.push_back(MVT::f64);
530 break;
531 case MVT::i64:
532 RetVals.push_back(MVT::i32);
533 RetVals.push_back(MVT::i32);
534 break;
535 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000536
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000537 std::vector<MVT::ValueType> NodeTys;
538 NodeTys.push_back(MVT::Other); // Returns a chain
539 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
540 std::vector<SDOperand> Ops;
541 Ops.push_back(Chain);
542 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000543
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000544 // FIXME: Do not generate X86ISD::TAILCALL for now.
545 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
546 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000547
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000548 NodeTys.clear();
549 NodeTys.push_back(MVT::Other); // Returns a chain
550 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
551 Ops.clear();
552 Ops.push_back(Chain);
553 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
554 Ops.push_back(DAG.getConstant(0, getPointerTy()));
555 Ops.push_back(InFlag);
556 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
557 InFlag = Chain.getValue(1);
558
559 SDOperand RetVal;
560 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000561 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000562 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000563 case MVT::i1:
564 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000565 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
566 Chain = RetVal.getValue(1);
567 if (RetTyVT == MVT::i1)
568 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
569 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000570 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000571 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
572 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000573 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000574 case MVT::i32:
575 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
576 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000577 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000578 case MVT::i64: {
579 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
580 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
581 Lo.getValue(2));
582 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
583 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000584 break;
585 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000586 case MVT::f32:
587 case MVT::f64: {
588 std::vector<MVT::ValueType> Tys;
589 Tys.push_back(MVT::f64);
590 Tys.push_back(MVT::Other);
591 Tys.push_back(MVT::Flag);
592 std::vector<SDOperand> Ops;
593 Ops.push_back(Chain);
594 Ops.push_back(InFlag);
595 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
596 Chain = RetVal.getValue(1);
597 InFlag = RetVal.getValue(2);
598 if (X86ScalarSSE) {
599 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
600 // shouldn't be necessary except that RFP cannot be live across
601 // multiple blocks. When stackifier is fixed, they can be uncoupled.
602 MachineFunction &MF = DAG.getMachineFunction();
603 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
604 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
605 Tys.clear();
606 Tys.push_back(MVT::Other);
607 Ops.clear();
608 Ops.push_back(Chain);
609 Ops.push_back(RetVal);
610 Ops.push_back(StackSlot);
611 Ops.push_back(DAG.getValueType(RetTyVT));
612 Ops.push_back(InFlag);
613 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
614 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
615 DAG.getSrcValue(NULL));
616 Chain = RetVal.getValue(1);
617 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000618
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000619 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
620 // FIXME: we would really like to remember that this FP_ROUND
621 // operation is okay to eliminate if we allow excess FP precision.
622 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
623 break;
624 }
625 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000626 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000627
628 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000629}
630
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000631//===----------------------------------------------------------------------===//
632// Fast Calling Convention implementation
633//===----------------------------------------------------------------------===//
634//
635// The X86 'fast' calling convention passes up to two integer arguments in
636// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
637// and requires that the callee pop its arguments off the stack (allowing proper
638// tail calls), and has the same return value conventions as C calling convs.
639//
640// This calling convention always arranges for the callee pop value to be 8n+4
641// bytes, which is needed for tail recursion elimination and stack alignment
642// reasons.
643//
644// Note that this can be enhanced in the future to pass fp vals in registers
645// (when we have a global fp allocator) and do other tricks.
646//
647
648/// AddLiveIn - This helper function adds the specified physical register to the
649/// MachineFunction as a live in value. It also creates a corresponding virtual
650/// register for it.
651static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
652 TargetRegisterClass *RC) {
653 assert(RC->contains(PReg) && "Not the correct regclass!");
654 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
655 MF.addLiveIn(PReg, VReg);
656 return VReg;
657}
658
Chris Lattner89fad2c2006-03-17 17:27:47 +0000659// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
660// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
661// EDX". Anything more is illegal.
662//
663// FIXME: The linscan register allocator currently has problem with
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000664// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-03-17 17:27:47 +0000665// a physreg with a virtreg, this increases the size of the physreg's live
666// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000667// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-03-17 17:27:47 +0000668// allocator to wedge itself.
669//
670// This code triggers this problem more often if we pass args in registers,
671// so disable it until this is fixed.
672//
673// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
674// about code being dead.
675//
676static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000677
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000678
679std::vector<SDOperand>
680X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
681 std::vector<SDOperand> ArgValues;
682
683 MachineFunction &MF = DAG.getMachineFunction();
684 MachineFrameInfo *MFI = MF.getFrameInfo();
685
686 // Add DAG nodes to load the arguments... On entry to a function the stack
687 // frame looks like this:
688 //
689 // [ESP] -- return address
690 // [ESP + 4] -- first nonreg argument (leftmost lexically)
691 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
692 // ...
693 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
694
695 // Keep track of the number of integer regs passed so far. This can be either
696 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
697 // used).
698 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000699
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000700 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
701 MVT::ValueType ObjectVT = getValueType(I->getType());
702 unsigned ArgIncrement = 4;
703 unsigned ObjSize = 0;
704 SDOperand ArgValue;
705
706 switch (ObjectVT) {
707 default: assert(0 && "Unhandled argument type!");
708 case MVT::i1:
709 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000710 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000711 if (!I->use_empty()) {
712 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
713 X86::R8RegisterClass);
714 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
715 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000716 if (ObjectVT == MVT::i1)
717 // FIXME: Should insert a assertzext here.
718 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000719 }
720 ++NumIntRegs;
721 break;
722 }
723
724 ObjSize = 1;
725 break;
726 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000727 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000728 if (!I->use_empty()) {
729 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
730 X86::R16RegisterClass);
731 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
732 DAG.setRoot(ArgValue.getValue(1));
733 }
734 ++NumIntRegs;
735 break;
736 }
737 ObjSize = 2;
738 break;
739 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000740 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000741 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000742 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000743 X86::R32RegisterClass);
744 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
745 DAG.setRoot(ArgValue.getValue(1));
746 }
747 ++NumIntRegs;
748 break;
749 }
750 ObjSize = 4;
751 break;
752 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000753 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000754 if (!I->use_empty()) {
755 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
756 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
757
758 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
759 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
760 DAG.setRoot(Hi.getValue(1));
761
762 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
763 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000764 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000765 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000766 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000767 if (!I->use_empty()) {
768 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
769 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
770 DAG.setRoot(Low.getValue(1));
771
772 // Load the high part from memory.
773 // Create the frame index object for this incoming parameter...
774 int FI = MFI->CreateFixedObject(4, ArgOffset);
775 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
776 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
777 DAG.getSrcValue(NULL));
778 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
779 }
780 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000781 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000782 break;
783 }
784 ObjSize = ArgIncrement = 8;
785 break;
786 case MVT::f32: ObjSize = 4; break;
787 case MVT::f64: ObjSize = ArgIncrement = 8; break;
788 }
789
790 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
791 // dead loads.
792 if (ObjSize && !I->use_empty()) {
793 // Create the frame index object for this incoming parameter...
794 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
795
796 // Create the SelectionDAG nodes corresponding to a load from this
797 // parameter.
798 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
799
800 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
801 DAG.getSrcValue(NULL));
802 } else if (ArgValue.Val == 0) {
803 if (MVT::isInteger(ObjectVT))
804 ArgValue = DAG.getConstant(0, ObjectVT);
805 else
806 ArgValue = DAG.getConstantFP(0, ObjectVT);
807 }
808 ArgValues.push_back(ArgValue);
809
810 if (ObjSize)
811 ArgOffset += ArgIncrement; // Move on to the next argument.
812 }
813
814 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
815 // arguments and the arguments after the retaddr has been pushed are aligned.
816 if ((ArgOffset & 7) == 0)
817 ArgOffset += 4;
818
819 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
820 ReturnAddrIndex = 0; // No return address slot generated yet.
821 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
822 BytesCallerReserves = 0;
823
824 // Finally, inform the code generator which regs we return values in.
825 switch (getValueType(F.getReturnType())) {
826 default: assert(0 && "Unknown type!");
827 case MVT::isVoid: break;
828 case MVT::i1:
829 case MVT::i8:
830 case MVT::i16:
831 case MVT::i32:
832 MF.addLiveOut(X86::EAX);
833 break;
834 case MVT::i64:
835 MF.addLiveOut(X86::EAX);
836 MF.addLiveOut(X86::EDX);
837 break;
838 case MVT::f32:
839 case MVT::f64:
840 MF.addLiveOut(X86::ST0);
841 break;
842 }
843 return ArgValues;
844}
845
846std::pair<SDOperand, SDOperand>
847X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
848 bool isTailCall, SDOperand Callee,
849 ArgListTy &Args, SelectionDAG &DAG) {
850 // Count how many bytes are to be pushed on the stack.
851 unsigned NumBytes = 0;
852
853 // Keep track of the number of integer regs passed so far. This can be either
854 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
855 // used).
856 unsigned NumIntRegs = 0;
857
858 for (unsigned i = 0, e = Args.size(); i != e; ++i)
859 switch (getValueType(Args[i].second)) {
860 default: assert(0 && "Unknown value type!");
861 case MVT::i1:
862 case MVT::i8:
863 case MVT::i16:
864 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000865 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000866 ++NumIntRegs;
867 break;
868 }
869 // fall through
870 case MVT::f32:
871 NumBytes += 4;
872 break;
873 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000874 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
875 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000876 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000877 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
878 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000879 NumBytes += 4;
880 break;
881 }
882
883 // fall through
884 case MVT::f64:
885 NumBytes += 8;
886 break;
887 }
888
889 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
890 // arguments and the arguments after the retaddr has been pushed are aligned.
891 if ((NumBytes & 7) == 0)
892 NumBytes += 4;
893
Chris Lattner94dd2922006-02-13 09:00:43 +0000894 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000895
896 // Arguments go on the stack in reverse order, as specified by the ABI.
897 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000898 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000899 NumIntRegs = 0;
900 std::vector<SDOperand> Stores;
901 std::vector<SDOperand> RegValuesToPass;
902 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
903 switch (getValueType(Args[i].second)) {
904 default: assert(0 && "Unexpected ValueType for argument!");
905 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000906 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
907 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000908 case MVT::i8:
909 case MVT::i16:
910 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000911 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000912 RegValuesToPass.push_back(Args[i].first);
913 ++NumIntRegs;
914 break;
915 }
916 // Fall through
917 case MVT::f32: {
918 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
919 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
920 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
921 Args[i].first, PtrOff,
922 DAG.getSrcValue(NULL)));
923 ArgOffset += 4;
924 break;
925 }
926 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000927 // Can pass (at least) part of it in regs?
928 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000929 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
930 Args[i].first, DAG.getConstant(1, MVT::i32));
931 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
932 Args[i].first, DAG.getConstant(0, MVT::i32));
933 RegValuesToPass.push_back(Lo);
934 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000935
936 // Pass both parts in regs?
937 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000938 RegValuesToPass.push_back(Hi);
939 ++NumIntRegs;
940 } else {
941 // Pass the high part in memory.
942 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
943 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
944 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
945 Hi, PtrOff, DAG.getSrcValue(NULL)));
946 ArgOffset += 4;
947 }
948 break;
949 }
950 // Fall through
951 case MVT::f64:
952 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
953 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
954 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
955 Args[i].first, PtrOff,
956 DAG.getSrcValue(NULL)));
957 ArgOffset += 8;
958 break;
959 }
960 }
961 if (!Stores.empty())
962 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
963
964 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
965 // arguments and the arguments after the retaddr has been pushed are aligned.
966 if ((ArgOffset & 7) == 0)
967 ArgOffset += 4;
968
969 std::vector<MVT::ValueType> RetVals;
970 MVT::ValueType RetTyVT = getValueType(RetTy);
971
972 RetVals.push_back(MVT::Other);
973
974 // The result values produced have to be legal. Promote the result.
975 switch (RetTyVT) {
976 case MVT::isVoid: break;
977 default:
978 RetVals.push_back(RetTyVT);
979 break;
980 case MVT::i1:
981 case MVT::i8:
982 case MVT::i16:
983 RetVals.push_back(MVT::i32);
984 break;
985 case MVT::f32:
986 if (X86ScalarSSE)
987 RetVals.push_back(MVT::f32);
988 else
989 RetVals.push_back(MVT::f64);
990 break;
991 case MVT::i64:
992 RetVals.push_back(MVT::i32);
993 RetVals.push_back(MVT::i32);
994 break;
995 }
996
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000997 // Build a sequence of copy-to-reg nodes chained together with token chain
998 // and flag operands which copy the outgoing args into registers.
999 SDOperand InFlag;
1000 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1001 unsigned CCReg;
1002 SDOperand RegToPass = RegValuesToPass[i];
1003 switch (RegToPass.getValueType()) {
1004 default: assert(0 && "Bad thing to pass in regs");
1005 case MVT::i8:
1006 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +00001007 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001008 case MVT::i16:
1009 CCReg = (i == 0) ? X86::AX : X86::DX;
1010 break;
1011 case MVT::i32:
1012 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1013 break;
1014 }
1015
1016 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1017 InFlag = Chain.getValue(1);
1018 }
1019
1020 std::vector<MVT::ValueType> NodeTys;
1021 NodeTys.push_back(MVT::Other); // Returns a chain
1022 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1023 std::vector<SDOperand> Ops;
1024 Ops.push_back(Chain);
1025 Ops.push_back(Callee);
1026 if (InFlag.Val)
1027 Ops.push_back(InFlag);
1028
1029 // FIXME: Do not generate X86ISD::TAILCALL for now.
1030 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1031 InFlag = Chain.getValue(1);
1032
1033 NodeTys.clear();
1034 NodeTys.push_back(MVT::Other); // Returns a chain
1035 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1036 Ops.clear();
1037 Ops.push_back(Chain);
1038 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1039 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1040 Ops.push_back(InFlag);
1041 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1042 InFlag = Chain.getValue(1);
1043
1044 SDOperand RetVal;
1045 if (RetTyVT != MVT::isVoid) {
1046 switch (RetTyVT) {
1047 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001048 case MVT::i1:
1049 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001050 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1051 Chain = RetVal.getValue(1);
1052 if (RetTyVT == MVT::i1)
1053 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1054 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001055 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001056 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1057 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001058 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001059 case MVT::i32:
1060 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1061 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001062 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001063 case MVT::i64: {
1064 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1065 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1066 Lo.getValue(2));
1067 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1068 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001069 break;
1070 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001071 case MVT::f32:
1072 case MVT::f64: {
1073 std::vector<MVT::ValueType> Tys;
1074 Tys.push_back(MVT::f64);
1075 Tys.push_back(MVT::Other);
1076 Tys.push_back(MVT::Flag);
1077 std::vector<SDOperand> Ops;
1078 Ops.push_back(Chain);
1079 Ops.push_back(InFlag);
1080 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1081 Chain = RetVal.getValue(1);
1082 InFlag = RetVal.getValue(2);
1083 if (X86ScalarSSE) {
1084 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1085 // shouldn't be necessary except that RFP cannot be live across
1086 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1087 MachineFunction &MF = DAG.getMachineFunction();
1088 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1089 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1090 Tys.clear();
1091 Tys.push_back(MVT::Other);
1092 Ops.clear();
1093 Ops.push_back(Chain);
1094 Ops.push_back(RetVal);
1095 Ops.push_back(StackSlot);
1096 Ops.push_back(DAG.getValueType(RetTyVT));
1097 Ops.push_back(InFlag);
1098 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1099 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1100 DAG.getSrcValue(NULL));
1101 Chain = RetVal.getValue(1);
1102 }
Evan Chengd9558e02006-01-06 00:43:03 +00001103
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001104 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1105 // FIXME: we would really like to remember that this FP_ROUND
1106 // operation is okay to eliminate if we allow excess FP precision.
1107 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1108 break;
1109 }
1110 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001111 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001112
1113 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001114}
1115
1116SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1117 if (ReturnAddrIndex == 0) {
1118 // Set up a frame object for the return address.
1119 MachineFunction &MF = DAG.getMachineFunction();
1120 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1121 }
1122
1123 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1124}
1125
1126
1127
1128std::pair<SDOperand, SDOperand> X86TargetLowering::
1129LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1130 SelectionDAG &DAG) {
1131 SDOperand Result;
1132 if (Depth) // Depths > 0 not supported yet!
1133 Result = DAG.getConstant(0, getPointerTy());
1134 else {
1135 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1136 if (!isFrameAddress)
1137 // Just load the return address
1138 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1139 DAG.getSrcValue(NULL));
1140 else
1141 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1142 DAG.getConstant(4, MVT::i32));
1143 }
1144 return std::make_pair(Result, Chain);
1145}
1146
Evan Cheng4a460802006-01-11 00:33:36 +00001147/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1148/// which corresponds to the condition code.
1149static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1150 switch (X86CC) {
1151 default: assert(0 && "Unknown X86 conditional code!");
1152 case X86ISD::COND_A: return X86::JA;
1153 case X86ISD::COND_AE: return X86::JAE;
1154 case X86ISD::COND_B: return X86::JB;
1155 case X86ISD::COND_BE: return X86::JBE;
1156 case X86ISD::COND_E: return X86::JE;
1157 case X86ISD::COND_G: return X86::JG;
1158 case X86ISD::COND_GE: return X86::JGE;
1159 case X86ISD::COND_L: return X86::JL;
1160 case X86ISD::COND_LE: return X86::JLE;
1161 case X86ISD::COND_NE: return X86::JNE;
1162 case X86ISD::COND_NO: return X86::JNO;
1163 case X86ISD::COND_NP: return X86::JNP;
1164 case X86ISD::COND_NS: return X86::JNS;
1165 case X86ISD::COND_O: return X86::JO;
1166 case X86ISD::COND_P: return X86::JP;
1167 case X86ISD::COND_S: return X86::JS;
1168 }
1169}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001170
Evan Cheng6dfa9992006-01-30 23:41:35 +00001171/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1172/// specific condition code. It returns a false if it cannot do a direct
1173/// translation. X86CC is the translated CondCode. Flip is set to true if the
1174/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001175static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1176 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001177 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001178 Flip = false;
1179 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001180 if (!isFP) {
1181 switch (SetCCOpcode) {
1182 default: break;
1183 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1184 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1185 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1186 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1187 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1188 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1189 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1190 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1191 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1192 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1193 }
1194 } else {
1195 // On a floating point condition, the flags are set as follows:
1196 // ZF PF CF op
1197 // 0 | 0 | 0 | X > Y
1198 // 0 | 0 | 1 | X < Y
1199 // 1 | 0 | 0 | X == Y
1200 // 1 | 1 | 1 | unordered
1201 switch (SetCCOpcode) {
1202 default: break;
1203 case ISD::SETUEQ:
1204 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001205 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001206 case ISD::SETOGT:
1207 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001208 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001209 case ISD::SETOGE:
1210 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001211 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001212 case ISD::SETULT:
1213 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001214 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001215 case ISD::SETULE:
1216 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1217 case ISD::SETONE:
1218 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1219 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1220 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1221 }
1222 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001223
1224 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001225}
1226
Evan Cheng4a460802006-01-11 00:33:36 +00001227/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1228/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001229/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001230static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001231 switch (X86CC) {
1232 default:
1233 return false;
1234 case X86ISD::COND_B:
1235 case X86ISD::COND_BE:
1236 case X86ISD::COND_E:
1237 case X86ISD::COND_P:
1238 case X86ISD::COND_A:
1239 case X86ISD::COND_AE:
1240 case X86ISD::COND_NE:
1241 case X86ISD::COND_NP:
1242 return true;
1243 }
1244}
1245
Evan Cheng4a460802006-01-11 00:33:36 +00001246MachineBasicBlock *
1247X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1248 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001249 switch (MI->getOpcode()) {
1250 default: assert(false && "Unexpected instr type to insert");
1251 case X86::CMOV_FR32:
1252 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001253 // To "insert" a SELECT_CC instruction, we actually have to insert the
1254 // diamond control-flow pattern. The incoming instruction knows the
1255 // destination vreg to set, the condition code register to branch on, the
1256 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001257 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1258 ilist<MachineBasicBlock>::iterator It = BB;
1259 ++It;
1260
1261 // thisMBB:
1262 // ...
1263 // TrueVal = ...
1264 // cmpTY ccX, r1, r2
1265 // bCC copy1MBB
1266 // fallthrough --> copy0MBB
1267 MachineBasicBlock *thisMBB = BB;
1268 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1269 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1270 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1271 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1272 MachineFunction *F = BB->getParent();
1273 F->getBasicBlockList().insert(It, copy0MBB);
1274 F->getBasicBlockList().insert(It, sinkMBB);
1275 // Update machine-CFG edges
1276 BB->addSuccessor(copy0MBB);
1277 BB->addSuccessor(sinkMBB);
1278
1279 // copy0MBB:
1280 // %FalseValue = ...
1281 // # fallthrough to sinkMBB
1282 BB = copy0MBB;
1283
1284 // Update machine-CFG edges
1285 BB->addSuccessor(sinkMBB);
1286
1287 // sinkMBB:
1288 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1289 // ...
1290 BB = sinkMBB;
1291 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1292 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1293 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001294
Evan Cheng0cc39452006-01-16 21:21:29 +00001295 delete MI; // The pseudo instruction is gone now.
1296 return BB;
1297 }
Evan Cheng4a460802006-01-11 00:33:36 +00001298
Evan Cheng0cc39452006-01-16 21:21:29 +00001299 case X86::FP_TO_INT16_IN_MEM:
1300 case X86::FP_TO_INT32_IN_MEM:
1301 case X86::FP_TO_INT64_IN_MEM: {
1302 // Change the floating point control register to use "round towards zero"
1303 // mode when truncating to an integer value.
1304 MachineFunction *F = BB->getParent();
1305 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1306 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1307
1308 // Load the old value of the high byte of the control word...
1309 unsigned OldCW =
1310 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1311 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1312
1313 // Set the high part to be round to zero...
1314 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1315
1316 // Reload the modified control word now...
1317 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1318
1319 // Restore the memory image of control word to original value
1320 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1321
1322 // Get the X86 opcode to use.
1323 unsigned Opc;
1324 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001325 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001326 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1327 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1328 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1329 }
1330
1331 X86AddressMode AM;
1332 MachineOperand &Op = MI->getOperand(0);
1333 if (Op.isRegister()) {
1334 AM.BaseType = X86AddressMode::RegBase;
1335 AM.Base.Reg = Op.getReg();
1336 } else {
1337 AM.BaseType = X86AddressMode::FrameIndexBase;
1338 AM.Base.FrameIndex = Op.getFrameIndex();
1339 }
1340 Op = MI->getOperand(1);
1341 if (Op.isImmediate())
1342 AM.Scale = Op.getImmedValue();
1343 Op = MI->getOperand(2);
1344 if (Op.isImmediate())
1345 AM.IndexReg = Op.getImmedValue();
1346 Op = MI->getOperand(3);
1347 if (Op.isGlobalAddress()) {
1348 AM.GV = Op.getGlobal();
1349 } else {
1350 AM.Disp = Op.getImmedValue();
1351 }
1352 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1353
1354 // Reload the original control word now.
1355 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1356
1357 delete MI; // The pseudo instruction is gone now.
1358 return BB;
1359 }
1360 }
Evan Cheng4a460802006-01-11 00:33:36 +00001361}
1362
1363
1364//===----------------------------------------------------------------------===//
1365// X86 Custom Lowering Hooks
1366//===----------------------------------------------------------------------===//
1367
Evan Cheng30b37b52006-03-13 23:18:16 +00001368/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1369/// load. For Darwin, external and weak symbols are indirect, loading the value
1370/// at address GV rather then the value of GV itself. This means that the
1371/// GlobalAddress must be in the base or index register of the address, not the
1372/// GV offset field.
1373static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1374 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1375 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1376}
1377
Evan Cheng0188ecb2006-03-22 18:59:22 +00001378/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1379/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1380bool X86::isPSHUFDMask(SDNode *N) {
1381 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1382
1383 if (N->getNumOperands() != 4)
1384 return false;
1385
1386 // Check if the value doesn't reference the second vector.
Evan Cheng14aed5e2006-03-24 01:18:28 +00001387 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng0188ecb2006-03-22 18:59:22 +00001388 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1389 "Invalid VECTOR_SHUFFLE mask!");
1390 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() >= 4) return false;
1391 }
1392
1393 return true;
1394}
1395
Evan Cheng14aed5e2006-03-24 01:18:28 +00001396/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1397/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1398bool X86::isSHUFPMask(SDNode *N) {
1399 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1400
Evan Chengbc4832b2006-03-24 23:15:12 +00001401 unsigned NumElems = N->getNumOperands();
1402 if (NumElems == 2) {
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001403 // The only case that ought be handled by SHUFPD is
1404 // Dest { 2, 1 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
1405 // Expect bit 0 == 1, bit1 == 2
1406 SDOperand Bit0 = N->getOperand(0);
1407 SDOperand Bit1 = N->getOperand(1);
1408 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1409 "Invalid VECTOR_SHUFFLE mask!");
1410 return (cast<ConstantSDNode>(Bit0)->getValue() == 1 &&
1411 cast<ConstantSDNode>(Bit1)->getValue() == 2);
1412 }
1413
Evan Chengbc4832b2006-03-24 23:15:12 +00001414 if (NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001415
1416 // Each half must refer to only one of the vector.
1417 SDOperand Elt = N->getOperand(0);
1418 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengbc4832b2006-03-24 23:15:12 +00001419 for (unsigned i = 1; i != NumElems / 2; ++i) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001420 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1421 "Invalid VECTOR_SHUFFLE mask!");
1422 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() !=
1423 cast<ConstantSDNode>(Elt)->getValue())
1424 return false;
1425 }
Evan Chengbc4832b2006-03-24 23:15:12 +00001426 Elt = N->getOperand(NumElems / 2);
Evan Cheng14aed5e2006-03-24 01:18:28 +00001427 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengbc4832b2006-03-24 23:15:12 +00001428 for (unsigned i = NumElems / 2; i != NumElems; ++i) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001429 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1430 "Invalid VECTOR_SHUFFLE mask!");
1431 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() !=
1432 cast<ConstantSDNode>(Elt)->getValue())
1433 return false;
1434 }
1435
1436 return true;
1437}
1438
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001439/// isMOVLHPSorUNPCKLPDMask - Return true if the specified VECTOR_SHUFFLE
1440/// operand specifies a shuffle of elements that is suitable for input to
1441/// MOVLHPS or UNPCKLPD.
1442bool X86::isMOVLHPSorUNPCKLPDMask(SDNode *N) {
1443 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1444
1445 if (N->getNumOperands() != 2)
1446 return false;
1447
1448 // Expect bit 0 == 0, bit1 == 2
1449 SDOperand Bit0 = N->getOperand(0);
1450 SDOperand Bit1 = N->getOperand(1);
1451 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1452 "Invalid VECTOR_SHUFFLE mask!");
1453 return (cast<ConstantSDNode>(Bit0)->getValue() == 0 &&
1454 cast<ConstantSDNode>(Bit1)->getValue() == 2);
1455}
1456
1457/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1458/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1459bool X86::isMOVHLPSMask(SDNode *N) {
1460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1461
1462 if (N->getNumOperands() != 2)
1463 return false;
1464
1465 // Expect bit 0 == 0, bit1 == 3
1466 SDOperand Bit0 = N->getOperand(0);
1467 SDOperand Bit1 = N->getOperand(1);
1468 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1469 "Invalid VECTOR_SHUFFLE mask!");
1470 return (cast<ConstantSDNode>(Bit0)->getValue() == 0 &&
1471 cast<ConstantSDNode>(Bit1)->getValue() == 3);
1472}
1473
1474/// isUNPCKHPDMask - Return true if the specified VECTOR_SHUFFLE operand
1475/// specifies a shuffle of elements that is suitable for input to UNPCKHPD.
1476bool X86::isUNPCKHPDMask(SDNode *N) {
1477 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1478
1479 if (N->getNumOperands() != 2)
1480 return false;
1481
1482 // Expect bit 0 == 1, bit1 == 3
1483 SDOperand Bit0 = N->getOperand(0);
1484 SDOperand Bit1 = N->getOperand(1);
1485 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1486 "Invalid VECTOR_SHUFFLE mask!");
1487 return (cast<ConstantSDNode>(Bit0)->getValue() == 1 &&
1488 cast<ConstantSDNode>(Bit1)->getValue() == 3);
1489}
1490
Evan Chengb9df0ca2006-03-22 02:53:00 +00001491/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1492/// a splat of a single element.
1493bool X86::isSplatMask(SDNode *N) {
1494 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1495
1496 // We can only splat 64-bit, and 32-bit quantities.
1497 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1498 return false;
1499
1500 // This is a splat operation if each element of the permute is the same, and
1501 // if the value doesn't reference the second vector.
1502 SDOperand Elt = N->getOperand(0);
1503 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1504 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1505 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1506 "Invalid VECTOR_SHUFFLE mask!");
1507 if (N->getOperand(i) != Elt) return false;
1508 }
1509
1510 // Make sure it is a splat of the first vector operand.
1511 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1512}
1513
Evan Cheng63d33002006-03-22 08:01:21 +00001514/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1515/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1516/// instructions.
1517unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001518 unsigned NumOperands = N->getNumOperands();
1519 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1520 unsigned Mask = 0;
1521 unsigned i = NumOperands - 1;
1522 do {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001523 unsigned Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1524 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001525 Mask |= Val;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001526 Mask <<= Shift;
Evan Cheng63d33002006-03-22 08:01:21 +00001527 --i;
1528 } while (i != 0);
1529
1530 return Mask;
1531}
1532
Evan Chengbc4832b2006-03-24 23:15:12 +00001533/// isZeroVector - Return true if this build_vector is an all-zero vector.
1534///
Evan Cheng386031a2006-03-24 07:29:27 +00001535bool X86::isZeroVector(SDNode *N) {
Evan Chengbc4832b2006-03-24 23:15:12 +00001536 if (MVT::isInteger(N->getOperand(0).getValueType())) {
1537 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1538 if (!isa<ConstantSDNode>(N->getOperand(i)) ||
1539 cast<ConstantSDNode>(N->getOperand(i))->getValue() != 0)
Evan Cheng386031a2006-03-24 07:29:27 +00001540 return false;
Evan Chengbc4832b2006-03-24 23:15:12 +00001541 } else {
1542 assert(MVT::isFloatingPoint(N->getOperand(0).getValueType()) &&
1543 "Vector of non-int, non-float values?");
1544 // See if this is all zeros.
1545 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i)
1546 if (!isa<ConstantFPSDNode>(N->getOperand(i)) ||
1547 !cast<ConstantFPSDNode>(N->getOperand(i))->isExactlyValue(0.0))
Evan Cheng386031a2006-03-24 07:29:27 +00001548 return false;
Evan Cheng386031a2006-03-24 07:29:27 +00001549 }
Evan Cheng386031a2006-03-24 07:29:27 +00001550 return true;
1551}
1552
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001553/// LowerOperation - Provide custom lowering hooks for some operations.
1554///
1555SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1556 switch (Op.getOpcode()) {
1557 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001558 case ISD::SHL_PARTS:
1559 case ISD::SRA_PARTS:
1560 case ISD::SRL_PARTS: {
1561 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1562 "Not an i64 shift!");
1563 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1564 SDOperand ShOpLo = Op.getOperand(0);
1565 SDOperand ShOpHi = Op.getOperand(1);
1566 SDOperand ShAmt = Op.getOperand(2);
1567 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001568 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001569 : DAG.getConstant(0, MVT::i32);
1570
1571 SDOperand Tmp2, Tmp3;
1572 if (Op.getOpcode() == ISD::SHL_PARTS) {
1573 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1574 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1575 } else {
1576 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001577 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001578 }
1579
1580 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1581 ShAmt, DAG.getConstant(32, MVT::i8));
1582
1583 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001584 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001585
1586 std::vector<MVT::ValueType> Tys;
1587 Tys.push_back(MVT::i32);
1588 Tys.push_back(MVT::Flag);
1589 std::vector<SDOperand> Ops;
1590 if (Op.getOpcode() == ISD::SHL_PARTS) {
1591 Ops.push_back(Tmp2);
1592 Ops.push_back(Tmp3);
1593 Ops.push_back(CC);
1594 Ops.push_back(InFlag);
1595 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1596 InFlag = Hi.getValue(1);
1597
1598 Ops.clear();
1599 Ops.push_back(Tmp3);
1600 Ops.push_back(Tmp1);
1601 Ops.push_back(CC);
1602 Ops.push_back(InFlag);
1603 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1604 } else {
1605 Ops.push_back(Tmp2);
1606 Ops.push_back(Tmp3);
1607 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001608 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001609 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1610 InFlag = Lo.getValue(1);
1611
1612 Ops.clear();
1613 Ops.push_back(Tmp3);
1614 Ops.push_back(Tmp1);
1615 Ops.push_back(CC);
1616 Ops.push_back(InFlag);
1617 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1618 }
1619
1620 Tys.clear();
1621 Tys.push_back(MVT::i32);
1622 Tys.push_back(MVT::i32);
1623 Ops.clear();
1624 Ops.push_back(Lo);
1625 Ops.push_back(Hi);
1626 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1627 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001628 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001629 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001630 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001631 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001632
1633 SDOperand Result;
1634 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1635 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001636 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001637 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001638 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001639 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1640 DAG.getEntryNode(), Op.getOperand(0),
1641 StackSlot, DAG.getSrcValue(NULL));
1642
1643 // Build the FILD
1644 std::vector<MVT::ValueType> Tys;
1645 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001646 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001647 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001648 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001649 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001650 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001651 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001652 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1653 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001654
1655 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001656 Chain = Result.getValue(1);
1657 SDOperand InFlag = Result.getValue(2);
1658
Evan Chenge3de85b2006-02-04 02:20:30 +00001659 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001660 // shouldn't be necessary except that RFP cannot be live across
1661 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1662 MachineFunction &MF = DAG.getMachineFunction();
1663 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1664 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1665 std::vector<MVT::ValueType> Tys;
1666 Tys.push_back(MVT::Other);
1667 std::vector<SDOperand> Ops;
1668 Ops.push_back(Chain);
1669 Ops.push_back(Result);
1670 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001671 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001672 Ops.push_back(InFlag);
1673 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1674 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1675 DAG.getSrcValue(NULL));
1676 }
1677
Evan Chenga3195e82006-01-12 22:54:21 +00001678 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001679 }
1680 case ISD::FP_TO_SINT: {
1681 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001682 "Unknown FP_TO_SINT to lower!");
1683 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1684 // stack slot.
1685 MachineFunction &MF = DAG.getMachineFunction();
1686 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1687 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1688 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1689
1690 unsigned Opc;
1691 switch (Op.getValueType()) {
1692 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1693 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1694 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1695 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1696 }
1697
Evan Cheng6dab0532006-01-30 08:02:57 +00001698 SDOperand Chain = DAG.getEntryNode();
1699 SDOperand Value = Op.getOperand(0);
1700 if (X86ScalarSSE) {
1701 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1702 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1703 DAG.getSrcValue(0));
1704 std::vector<MVT::ValueType> Tys;
1705 Tys.push_back(MVT::f64);
1706 Tys.push_back(MVT::Other);
1707 std::vector<SDOperand> Ops;
1708 Ops.push_back(Chain);
1709 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001710 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001711 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1712 Chain = Value.getValue(1);
1713 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1714 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1715 }
1716
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001717 // Build the FP_TO_INT*_IN_MEM
1718 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001719 Ops.push_back(Chain);
1720 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001721 Ops.push_back(StackSlot);
1722 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1723
1724 // Load the result.
1725 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1726 DAG.getSrcValue(NULL));
1727 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001728 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001729 std::vector<MVT::ValueType> Tys;
1730 Tys.push_back(MVT::Other);
1731 Tys.push_back(MVT::Flag);
1732 std::vector<SDOperand> Ops;
1733 Ops.push_back(Op.getOperand(0));
1734 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001735 Ops.clear();
1736 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1737 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1738 MVT::i32, Ops[0].getValue(2)));
1739 Ops.push_back(Ops[1].getValue(1));
1740 Tys[0] = Tys[1] = MVT::i32;
1741 Tys.push_back(MVT::Other);
1742 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001743 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001744 case ISD::FABS: {
1745 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001746 const Type *OpNTy = MVT::getTypeForValueType(VT);
1747 std::vector<Constant*> CV;
1748 if (VT == MVT::f64) {
1749 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1750 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1751 } else {
1752 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1753 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1754 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1755 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1756 }
1757 Constant *CS = ConstantStruct::get(CV);
1758 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1759 SDOperand Mask
1760 = DAG.getNode(X86ISD::LOAD_PACK,
1761 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001762 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1763 }
Evan Cheng223547a2006-01-31 22:28:30 +00001764 case ISD::FNEG: {
1765 MVT::ValueType VT = Op.getValueType();
1766 const Type *OpNTy = MVT::getTypeForValueType(VT);
1767 std::vector<Constant*> CV;
1768 if (VT == MVT::f64) {
1769 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1770 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1771 } else {
1772 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1773 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1774 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1775 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1776 }
1777 Constant *CS = ConstantStruct::get(CV);
1778 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1779 SDOperand Mask
1780 = DAG.getNode(X86ISD::LOAD_PACK,
1781 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1782 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1783 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001784 case ISD::SETCC: {
1785 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001786 SDOperand Cond;
1787 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001788 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1789 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001790 bool Flip;
1791 unsigned X86CC;
1792 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1793 if (Flip)
1794 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1795 Op.getOperand(1), Op.getOperand(0));
1796 else
1797 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1798 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001799 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1800 DAG.getConstant(X86CC, MVT::i8), Cond);
1801 } else {
1802 assert(isFP && "Illegal integer SetCC!");
1803
Evan Cheng6dfa9992006-01-30 23:41:35 +00001804 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1805 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001806 std::vector<MVT::ValueType> Tys;
1807 std::vector<SDOperand> Ops;
1808 switch (SetCCOpcode) {
1809 default: assert(false && "Illegal floating point SetCC!");
1810 case ISD::SETOEQ: { // !PF & ZF
1811 Tys.push_back(MVT::i8);
1812 Tys.push_back(MVT::Flag);
1813 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1814 Ops.push_back(Cond);
1815 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1816 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1817 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1818 Tmp1.getValue(1));
1819 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1820 }
Evan Chengd9558e02006-01-06 00:43:03 +00001821 case ISD::SETUNE: { // PF | !ZF
1822 Tys.push_back(MVT::i8);
1823 Tys.push_back(MVT::Flag);
1824 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1825 Ops.push_back(Cond);
1826 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1827 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1828 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1829 Tmp1.getValue(1));
1830 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1831 }
1832 }
1833 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001834 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001835 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001836 MVT::ValueType VT = Op.getValueType();
1837 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001838 bool isFPStack = isFP && !X86ScalarSSE;
1839 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001840 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001841 SDOperand Op0 = Op.getOperand(0);
1842 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001843 if (Op0.getOpcode() == ISD::SETCC)
1844 Op0 = LowerOperation(Op0, DAG);
1845
Evan Chengaaca22c2006-01-10 20:26:56 +00001846 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001847 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1848 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1849 // have another use it will be eliminated.
1850 // If the X86ISD::SETCC has more than one use, then it's probably better
1851 // to use a test instead of duplicating the X86ISD::CMP (for register
1852 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001853 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1854 if (!Op0.hasOneUse()) {
1855 std::vector<MVT::ValueType> Tys;
1856 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1857 Tys.push_back(Op0.Val->getValueType(i));
1858 std::vector<SDOperand> Ops;
1859 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1860 Ops.push_back(Op0.getOperand(i));
1861 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1862 }
1863
Evan Cheng1bcee362006-01-13 01:03:02 +00001864 CC = Op0.getOperand(0);
1865 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001866 // Make a copy as flag result cannot be used by more than one.
1867 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1868 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001869 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001870 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001871 } else
1872 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001873 } else
1874 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001875
Evan Cheng189d01e2006-01-13 01:06:49 +00001876 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001877 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001878 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001879 }
Evan Chenge3413162006-01-09 18:33:28 +00001880
1881 std::vector<MVT::ValueType> Tys;
1882 Tys.push_back(Op.getValueType());
1883 Tys.push_back(MVT::Flag);
1884 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001885 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1886 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001887 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001888 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001889 Ops.push_back(CC);
1890 Ops.push_back(Cond);
1891 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001892 }
Evan Cheng898101c2005-12-19 23:12:38 +00001893 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001894 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001895 SDOperand Cond = Op.getOperand(1);
1896 SDOperand Dest = Op.getOperand(2);
1897 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001898 if (Cond.getOpcode() == ISD::SETCC)
1899 Cond = LowerOperation(Cond, DAG);
1900
Evan Chengd5781fc2005-12-21 20:21:51 +00001901 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001902 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1903 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1904 // have another use it will be eliminated.
1905 // If the X86ISD::SETCC has more than one use, then it's probably better
1906 // to use a test instead of duplicating the X86ISD::CMP (for register
1907 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001908 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1909 if (!Cond.hasOneUse()) {
1910 std::vector<MVT::ValueType> Tys;
1911 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1912 Tys.push_back(Cond.Val->getValueType(i));
1913 std::vector<SDOperand> Ops;
1914 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1915 Ops.push_back(Cond.getOperand(i));
1916 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1917 }
1918
Evan Cheng1bcee362006-01-13 01:03:02 +00001919 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001920 Cond = Cond.getOperand(1);
1921 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001922 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001923 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001924 } else
1925 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001926 } else
1927 addTest = true;
1928
1929 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001930 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001931 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1932 }
1933 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1934 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1935 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001936 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001937 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001938 SDOperand Chain = Op.getOperand(0);
1939 unsigned Align =
1940 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1941 if (Align == 0) Align = 1;
1942
Evan Cheng18a84522006-02-16 00:21:07 +00001943 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1944 // If not DWORD aligned, call memset if size is less than the threshold.
1945 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001946 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001947 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001948 MVT::ValueType IntPtr = getPointerTy();
1949 const Type *IntPtrTy = getTargetData().getIntPtrType();
1950 std::vector<std::pair<SDOperand, const Type*> > Args;
1951 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1952 // Extend the ubyte argument to be an int value for the call.
1953 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1954 Args.push_back(std::make_pair(Val, IntPtrTy));
1955 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1956 std::pair<SDOperand,SDOperand> CallResult =
1957 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1958 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1959 return CallResult.second;
1960 }
1961
Evan Cheng67f92a72006-01-11 22:15:48 +00001962 MVT::ValueType AVT;
1963 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001964 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1965 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001966 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001967 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001968 unsigned ValReg;
1969 unsigned Val = ValC->getValue() & 255;
1970
1971 // If the value is a constant, then we can potentially use larger sets.
1972 switch (Align & 3) {
1973 case 2: // WORD aligned
1974 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001975 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1976 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001977 Val = (Val << 8) | Val;
1978 ValReg = X86::AX;
1979 break;
1980 case 0: // DWORD aligned
1981 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001982 if (I) {
1983 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1984 BytesLeft = I->getValue() % 4;
1985 } else {
1986 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1987 DAG.getConstant(2, MVT::i8));
1988 TwoRepStos = true;
1989 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001990 Val = (Val << 8) | Val;
1991 Val = (Val << 16) | Val;
1992 ValReg = X86::EAX;
1993 break;
1994 default: // Byte aligned
1995 AVT = MVT::i8;
1996 Count = Op.getOperand(3);
1997 ValReg = X86::AL;
1998 break;
1999 }
2000
2001 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2002 InFlag);
2003 InFlag = Chain.getValue(1);
2004 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00002005 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00002006 Count = Op.getOperand(3);
2007 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2008 InFlag = Chain.getValue(1);
2009 }
2010
2011 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2012 InFlag = Chain.getValue(1);
2013 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2014 InFlag = Chain.getValue(1);
2015
Evan Chengff909922006-03-07 23:29:39 +00002016 std::vector<MVT::ValueType> Tys;
2017 Tys.push_back(MVT::Other);
2018 Tys.push_back(MVT::Flag);
2019 std::vector<SDOperand> Ops;
2020 Ops.push_back(Chain);
2021 Ops.push_back(DAG.getValueType(AVT));
2022 Ops.push_back(InFlag);
2023 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2024
2025 if (TwoRepStos) {
2026 InFlag = Chain.getValue(1);
2027 Count = Op.getOperand(3);
2028 MVT::ValueType CVT = Count.getValueType();
2029 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2030 DAG.getConstant(3, CVT));
2031 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2032 InFlag = Chain.getValue(1);
2033 Tys.clear();
2034 Tys.push_back(MVT::Other);
2035 Tys.push_back(MVT::Flag);
2036 Ops.clear();
2037 Ops.push_back(Chain);
2038 Ops.push_back(DAG.getValueType(MVT::i8));
2039 Ops.push_back(InFlag);
2040 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2041 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002042 // Issue stores for the last 1 - 3 bytes.
2043 SDOperand Value;
2044 unsigned Val = ValC->getValue() & 255;
2045 unsigned Offset = I->getValue() - BytesLeft;
2046 SDOperand DstAddr = Op.getOperand(1);
2047 MVT::ValueType AddrVT = DstAddr.getValueType();
2048 if (BytesLeft >= 2) {
2049 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2050 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2051 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2052 DAG.getConstant(Offset, AddrVT)),
2053 DAG.getSrcValue(NULL));
2054 BytesLeft -= 2;
2055 Offset += 2;
2056 }
2057
2058 if (BytesLeft == 1) {
2059 Value = DAG.getConstant(Val, MVT::i8);
2060 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2061 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2062 DAG.getConstant(Offset, AddrVT)),
2063 DAG.getSrcValue(NULL));
2064 }
2065 }
2066
2067 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002068 }
2069 case ISD::MEMCPY: {
2070 SDOperand Chain = Op.getOperand(0);
2071 unsigned Align =
2072 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2073 if (Align == 0) Align = 1;
2074
Evan Cheng18a84522006-02-16 00:21:07 +00002075 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2076 // If not DWORD aligned, call memcpy if size is less than the threshold.
2077 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002078 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002079 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002080 MVT::ValueType IntPtr = getPointerTy();
2081 const Type *IntPtrTy = getTargetData().getIntPtrType();
2082 std::vector<std::pair<SDOperand, const Type*> > Args;
2083 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2084 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2085 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2086 std::pair<SDOperand,SDOperand> CallResult =
2087 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2088 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2089 return CallResult.second;
2090 }
2091
Evan Cheng67f92a72006-01-11 22:15:48 +00002092 MVT::ValueType AVT;
2093 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002094 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002095 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00002096 switch (Align & 3) {
2097 case 2: // WORD aligned
2098 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002099 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2100 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002101 break;
2102 case 0: // DWORD aligned
2103 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002104 if (I) {
2105 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2106 BytesLeft = I->getValue() % 4;
2107 } else {
2108 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2109 DAG.getConstant(2, MVT::i8));
2110 TwoRepMovs = true;
2111 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002112 break;
2113 default: // Byte aligned
2114 AVT = MVT::i8;
2115 Count = Op.getOperand(3);
2116 break;
2117 }
2118
Evan Cheng62bec2c2006-03-04 02:48:56 +00002119 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002120 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2121 InFlag = Chain.getValue(1);
2122 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2123 InFlag = Chain.getValue(1);
2124 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2125 InFlag = Chain.getValue(1);
2126
Evan Chengff909922006-03-07 23:29:39 +00002127 std::vector<MVT::ValueType> Tys;
2128 Tys.push_back(MVT::Other);
2129 Tys.push_back(MVT::Flag);
2130 std::vector<SDOperand> Ops;
2131 Ops.push_back(Chain);
2132 Ops.push_back(DAG.getValueType(AVT));
2133 Ops.push_back(InFlag);
2134 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2135
2136 if (TwoRepMovs) {
2137 InFlag = Chain.getValue(1);
2138 Count = Op.getOperand(3);
2139 MVT::ValueType CVT = Count.getValueType();
2140 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2141 DAG.getConstant(3, CVT));
2142 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2143 InFlag = Chain.getValue(1);
2144 Tys.clear();
2145 Tys.push_back(MVT::Other);
2146 Tys.push_back(MVT::Flag);
2147 Ops.clear();
2148 Ops.push_back(Chain);
2149 Ops.push_back(DAG.getValueType(MVT::i8));
2150 Ops.push_back(InFlag);
2151 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2152 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002153 // Issue loads and stores for the last 1 - 3 bytes.
2154 unsigned Offset = I->getValue() - BytesLeft;
2155 SDOperand DstAddr = Op.getOperand(1);
2156 MVT::ValueType DstVT = DstAddr.getValueType();
2157 SDOperand SrcAddr = Op.getOperand(2);
2158 MVT::ValueType SrcVT = SrcAddr.getValueType();
2159 SDOperand Value;
2160 if (BytesLeft >= 2) {
2161 Value = DAG.getLoad(MVT::i16, Chain,
2162 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2163 DAG.getConstant(Offset, SrcVT)),
2164 DAG.getSrcValue(NULL));
2165 Chain = Value.getValue(1);
2166 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2167 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2168 DAG.getConstant(Offset, DstVT)),
2169 DAG.getSrcValue(NULL));
2170 BytesLeft -= 2;
2171 Offset += 2;
2172 }
2173
2174 if (BytesLeft == 1) {
2175 Value = DAG.getLoad(MVT::i8, Chain,
2176 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2177 DAG.getConstant(Offset, SrcVT)),
2178 DAG.getSrcValue(NULL));
2179 Chain = Value.getValue(1);
2180 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2181 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2182 DAG.getConstant(Offset, DstVT)),
2183 DAG.getSrcValue(NULL));
2184 }
2185 }
2186
2187 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002188 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002189
2190 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2191 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2192 // one of the above mentioned nodes. It has to be wrapped because otherwise
2193 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2194 // be used to form addressing mode. These wrapped nodes will be selected
2195 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002196 case ISD::ConstantPool: {
2197 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002198 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2199 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2200 CP->getAlignment()));
Evan Chenga88973f2006-03-22 19:22:18 +00002201 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002202 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002203 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002204 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2205 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2206 }
2207
2208 return Result;
2209 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002210 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002211 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2212 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2213 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002214 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002215 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002216 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002217 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2218 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002219
2220 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002221 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002222 // the GlobalAddress must be in the base or index register of the address,
2223 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002224 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002225 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002226 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002227 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002228 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002229
Evan Cheng002fe9b2006-01-12 07:56:47 +00002230 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002231 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002232 case ISD::ExternalSymbol: {
2233 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2234 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2235 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002236 if (Subtarget->isTargetDarwin()) {
Evan Cheng020d2e82006-02-23 20:41:18 +00002237 // With PIC, the address is actually $g + Offset.
2238 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2239 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2240 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2241 }
2242
2243 return Result;
2244 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002245 case ISD::VASTART: {
2246 // vastart just stores the address of the VarArgsFrameIndex slot into the
2247 // memory location argument.
2248 // FIXME: Replace MVT::i32 with PointerTy
2249 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2250 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2251 Op.getOperand(1), Op.getOperand(2));
2252 }
Nate Begemanee625572006-01-27 21:09:22 +00002253 case ISD::RET: {
2254 SDOperand Copy;
2255
2256 switch(Op.getNumOperands()) {
2257 default:
2258 assert(0 && "Do not know how to return this many arguments!");
2259 abort();
2260 case 1:
2261 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2262 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2263 case 2: {
2264 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2265 if (MVT::isInteger(ArgVT))
2266 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2267 SDOperand());
2268 else if (!X86ScalarSSE) {
2269 std::vector<MVT::ValueType> Tys;
2270 Tys.push_back(MVT::Other);
2271 Tys.push_back(MVT::Flag);
2272 std::vector<SDOperand> Ops;
2273 Ops.push_back(Op.getOperand(0));
2274 Ops.push_back(Op.getOperand(1));
2275 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2276 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002277 SDOperand MemLoc;
2278 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002279 SDOperand Value = Op.getOperand(1);
2280
Evan Cheng760df292006-02-01 01:19:32 +00002281 if (Value.getOpcode() == ISD::LOAD &&
2282 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002283 Chain = Value.getOperand(0);
2284 MemLoc = Value.getOperand(1);
2285 } else {
2286 // Spill the value to memory and reload it into top of stack.
2287 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2288 MachineFunction &MF = DAG.getMachineFunction();
2289 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2290 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2291 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2292 Value, MemLoc, DAG.getSrcValue(0));
2293 }
Nate Begemanee625572006-01-27 21:09:22 +00002294 std::vector<MVT::ValueType> Tys;
2295 Tys.push_back(MVT::f64);
2296 Tys.push_back(MVT::Other);
2297 std::vector<SDOperand> Ops;
2298 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002299 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002300 Ops.push_back(DAG.getValueType(ArgVT));
2301 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2302 Tys.clear();
2303 Tys.push_back(MVT::Other);
2304 Tys.push_back(MVT::Flag);
2305 Ops.clear();
2306 Ops.push_back(Copy.getValue(1));
2307 Ops.push_back(Copy);
2308 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2309 }
2310 break;
2311 }
2312 case 3:
2313 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2314 SDOperand());
2315 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2316 break;
2317 }
2318 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2319 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2320 Copy.getValue(1));
2321 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002322 case ISD::SCALAR_TO_VECTOR: {
2323 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengbc4832b2006-03-24 23:15:12 +00002324 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Cheng48090aa2006-03-21 23:01:21 +00002325 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002326 case ISD::VECTOR_SHUFFLE: {
2327 SDOperand V1 = Op.getOperand(0);
2328 SDOperand V2 = Op.getOperand(1);
2329 SDOperand PermMask = Op.getOperand(2);
2330 MVT::ValueType VT = Op.getValueType();
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002331 unsigned NumElems = PermMask.getNumOperands();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002332
Evan Cheng0188ecb2006-03-22 18:59:22 +00002333 // Handle splat cases.
2334 if (X86::isSplatMask(PermMask.Val)) {
2335 if (V2.getOpcode() == ISD::UNDEF)
Evan Chengb9df0ca2006-03-22 02:53:00 +00002336 // Leave the VECTOR_SHUFFLE alone. It matches SHUFP*.
Chris Lattner6df11542006-03-22 04:18:34 +00002337 return SDOperand();
Evan Cheng0188ecb2006-03-22 18:59:22 +00002338 else
2339 // Make it match SHUFP* or UNPCKLPD. Second vector is undef since it's
2340 // not needed.
2341 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2342 DAG.getNode(ISD::UNDEF, V1.getValueType()),
2343 PermMask);
Evan Chengbc4832b2006-03-24 23:15:12 +00002344 } else if (NumElems == 2) {
2345 // All v2f64 cases are handled.
2346 return SDOperand();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002347 } else if (X86::isPSHUFDMask(PermMask.Val)) {
Evan Cheng0188ecb2006-03-22 18:59:22 +00002348 if (V2.getOpcode() == ISD::UNDEF)
Evan Chengb9df0ca2006-03-22 02:53:00 +00002349 // Leave the VECTOR_SHUFFLE alone. It matches PSHUFD.
Chris Lattner6df11542006-03-22 04:18:34 +00002350 return SDOperand();
Evan Cheng0188ecb2006-03-22 18:59:22 +00002351 else
2352 // Make it match PSHUFD. Second vector is undef since it's not needed.
2353 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2354 DAG.getNode(ISD::UNDEF, V1.getValueType()),
2355 PermMask);
Evan Cheng5217a5b2006-03-24 06:40:32 +00002356 } else if (X86::isSHUFPMask(PermMask.Val)) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00002357 SDOperand Elt = PermMask.getOperand(0);
2358 if (cast<ConstantSDNode>(Elt)->getValue() >= NumElems) {
2359 // Swap the operands and change mask.
2360 std::vector<SDOperand> MaskVec;
2361 for (unsigned i = NumElems / 2; i != NumElems; ++i)
2362 MaskVec.push_back(PermMask.getOperand(i));
2363 for (unsigned i = 0; i != NumElems / 2; ++i)
2364 MaskVec.push_back(PermMask.getOperand(i));
2365 PermMask =
2366 DAG.getNode(ISD::BUILD_VECTOR, PermMask.getValueType(), MaskVec);
2367 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, PermMask);
2368 }
2369 return SDOperand();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002370 }
2371
Evan Cheng386031a2006-03-24 07:29:27 +00002372 assert(0 && "Unexpected VECTOR_SHUFFLE to lower");
Chris Lattner6df11542006-03-22 04:18:34 +00002373 abort();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002374 }
Evan Cheng386031a2006-03-24 07:29:27 +00002375 case ISD::BUILD_VECTOR: {
Evan Chengbc4832b2006-03-24 23:15:12 +00002376 SDOperand Elt0 = Op.getOperand(0);
2377 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2378 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2379 (isa<ConstantFPSDNode>(Elt0) &&
2380 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2381 bool RestAreZero = true;
Evan Cheng386031a2006-03-24 07:29:27 +00002382 unsigned NumElems = Op.getNumOperands();
Evan Chengbc4832b2006-03-24 23:15:12 +00002383 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng386031a2006-03-24 07:29:27 +00002384 SDOperand V = Op.getOperand(i);
2385 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(V)) {
2386 if (!FPC->isExactlyValue(+0.0))
Evan Chengbc4832b2006-03-24 23:15:12 +00002387 RestAreZero = false;
Evan Cheng386031a2006-03-24 07:29:27 +00002388 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(V)) {
2389 if (!C->isNullValue())
Evan Chengbc4832b2006-03-24 23:15:12 +00002390 RestAreZero = false;
Evan Cheng386031a2006-03-24 07:29:27 +00002391 } else
Evan Chengbc4832b2006-03-24 23:15:12 +00002392 RestAreZero = false;
Evan Cheng386031a2006-03-24 07:29:27 +00002393 }
2394
Evan Chengbc4832b2006-03-24 23:15:12 +00002395 if (RestAreZero) {
2396 if (Elt0IsZero) return Op;
2397
2398 // Zero extend a scalar to a vector.
2399 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2400 }
2401
Evan Cheng386031a2006-03-24 07:29:27 +00002402 return SDOperand();
2403 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002404 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002405}
Evan Cheng72261582005-12-20 06:22:03 +00002406
2407const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2408 switch (Opcode) {
2409 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002410 case X86ISD::SHLD: return "X86ISD::SHLD";
2411 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002412 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002413 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002414 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002415 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002416 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2417 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2418 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002419 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002420 case X86ISD::FST: return "X86ISD::FST";
2421 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002422 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002423 case X86ISD::CALL: return "X86ISD::CALL";
2424 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2425 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2426 case X86ISD::CMP: return "X86ISD::CMP";
2427 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002428 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002429 case X86ISD::CMOV: return "X86ISD::CMOV";
2430 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002431 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002432 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2433 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002434 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002435 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002436 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00002437 case X86ISD::S2VEC: return "X86ISD::S2VEC";
2438 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Cheng72261582005-12-20 06:22:03 +00002439 }
2440}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002441
Nate Begeman368e18d2006-02-16 21:11:51 +00002442void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2443 uint64_t Mask,
2444 uint64_t &KnownZero,
2445 uint64_t &KnownOne,
2446 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002447
2448 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002449 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002450
2451 switch (Opc) {
2452 default:
2453 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2454 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002455 case X86ISD::SETCC:
2456 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2457 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002458 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002459}
Chris Lattner259e97c2006-01-31 19:43:35 +00002460
2461std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002462getRegClassForInlineAsmConstraint(const std::string &Constraint,
2463 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002464 if (Constraint.size() == 1) {
2465 // FIXME: not handling fp-stack yet!
2466 // FIXME: not handling MMX registers yet ('y' constraint).
2467 switch (Constraint[0]) { // GCC X86 Constraint Letters
2468 default: break; // Unknown constriant letter
2469 case 'r': // GENERAL_REGS
2470 case 'R': // LEGACY_REGS
2471 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2472 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2473 case 'l': // INDEX_REGS
2474 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2475 X86::ESI, X86::EDI, X86::EBP, 0);
2476 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2477 case 'Q': // Q_REGS
2478 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2479 case 'x': // SSE_REGS if SSE1 allowed
2480 if (Subtarget->hasSSE1())
2481 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2482 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2483 0);
2484 return std::vector<unsigned>();
2485 case 'Y': // SSE_REGS if SSE2 allowed
2486 if (Subtarget->hasSSE2())
2487 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2488 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2489 0);
2490 return std::vector<unsigned>();
2491 }
2492 }
2493
Chris Lattner1efa40f2006-02-22 00:56:39 +00002494 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002495}
Evan Cheng30b37b52006-03-13 23:18:16 +00002496
2497/// isLegalAddressImmediate - Return true if the integer value or
2498/// GlobalValue can be used as the offset of the target addressing mode.
2499bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2500 // X86 allows a sign-extended 32-bit immediate field.
2501 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2502}
2503
2504bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00002505 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00002506 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2507 if (RModel == Reloc::Static)
2508 return true;
2509 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002510 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002511 else
2512 return false;
2513 } else
2514 return true;
2515}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002516
2517/// isShuffleMaskLegal - Targets can use this to indicate that they only
2518/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2519/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2520/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00002521bool
2522X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2523 // Only do shuffles on 128-bit vector types for now.
2524 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002525 return (Mask.Val->getNumOperands() == 2 ||
2526 X86::isSplatMask(Mask.Val) ||
Evan Cheng14aed5e2006-03-24 01:18:28 +00002527 X86::isPSHUFDMask(Mask.Val) ||
2528 X86::isSHUFPMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00002529}