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