blob: dd8ed73cb36e7092f0c29609aef8ab1eeeb5a595 [file] [log] [blame]
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/Function.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000022#include "llvm/ADT/VectorExtras.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000029#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/Target/TargetOptions.h"
31using namespace llvm;
32
33// FIXME: temporary.
34#include "llvm/Support/CommandLine.h"
35static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
36 cl::desc("Enable fastcc on X86"));
37
38X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000040 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
42
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000043 // Set up the TargetLowering object.
44
45 // X86 is weird, it always uses i8 for shift amounts and setcc results.
46 setShiftAmountType(MVT::i8);
47 setSetCCResultType(MVT::i8);
48 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000049 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000051 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng714554d2006-03-16 21:47:42 +000052
Evan Chenga88973f2006-03-22 19:22:18 +000053 if (!Subtarget->isTargetDarwin())
Evan Chengdf57fa02006-03-17 20:31:41 +000054 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
55 setUseUnderscoreSetJmpLongJmp(true);
56
Evan Cheng714554d2006-03-16 21:47:42 +000057 // Add legal addressing mode scale values.
58 addLegalAddressScale(8);
59 addLegalAddressScale(4);
60 addLegalAddressScale(2);
61 // Enter the ones which require both scale + index last. These are more
62 // expensive.
63 addLegalAddressScale(9);
64 addLegalAddressScale(5);
65 addLegalAddressScale(3);
Chris Lattnera54aa942006-01-29 06:26:08 +000066
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000067 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068 addRegisterClass(MVT::i8, X86::R8RegisterClass);
69 addRegisterClass(MVT::i16, X86::R16RegisterClass);
70 addRegisterClass(MVT::i32, X86::R32RegisterClass);
71
72 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
73 // operation.
74 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
75 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000077
78 if (X86ScalarSSE)
79 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
80 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
81 else
82 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000083
84 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
85 // this operation.
86 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
87 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000088 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000089 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000090 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000091 else {
92 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
93 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
94 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000095
Evan Cheng6dab0532006-01-30 08:02:57 +000096 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
97 // isn't legal.
98 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
99 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
100
Evan Cheng02568ff2006-01-30 22:13:22 +0000101 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
102 // this operation.
103 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
104 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
105
106 if (X86ScalarSSE) {
107 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
108 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 }
112
113 // Handle FP_TO_UINT by promoting the destination to a larger signed
114 // conversion.
115 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
116 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
118
Evan Cheng45af8fd2006-02-18 07:26:17 +0000119 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000120 // Expand FP_TO_UINT into a select.
121 // FIXME: We would like to use a Custom expander here eventually to do
122 // the optimal thing for SSE vs. the default expansion in the legalizer.
123 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
124 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000125 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
127
Evan Cheng02568ff2006-01-30 22:13:22 +0000128 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
129 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000130
Evan Cheng5298bcc2006-02-17 07:01:52 +0000131 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000132 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
133 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000134 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
135 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
138 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
139 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
140 setOperationAction(ISD::FREM , MVT::f64 , Expand);
141 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
142 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
143 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
145 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
146 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
148 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
149 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000150 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000151 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000152
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000153 // These should be promoted to a larger select which is supported.
154 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
155 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000156
157 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000158 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
159 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
160 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
162 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
165 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000167 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000168 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000169 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000170 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000171 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000172 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000173 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000174 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
175 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000177 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000178 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
179 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
Chris Lattnerf73bae12005-11-29 06:16:21 +0000181 // We don't have line number support yet.
182 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000183 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000184 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000185 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000186 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000187
Nate Begemanacc398c2006-01-25 18:21:52 +0000188 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
189 setOperationAction(ISD::VASTART , MVT::Other, Custom);
190
191 // Use the default implementation.
192 setOperationAction(ISD::VAARG , MVT::Other, Expand);
193 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
194 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000195 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
196 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
197 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000198
Chris Lattner9601a862006-03-05 05:08:37 +0000199 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
200 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
201
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000202 if (X86ScalarSSE) {
203 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000204 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
205 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000206
207 // SSE has no load+extend ops
208 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
209 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
210
Evan Cheng223547a2006-01-31 22:28:30 +0000211 // Use ANDPD to simulate FABS.
212 setOperationAction(ISD::FABS , MVT::f64, Custom);
213 setOperationAction(ISD::FABS , MVT::f32, Custom);
214
215 // Use XORP to simulate FNEG.
216 setOperationAction(ISD::FNEG , MVT::f64, Custom);
217 setOperationAction(ISD::FNEG , MVT::f32, Custom);
218
Evan Chengd25e9e82006-02-02 00:28:23 +0000219 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 setOperationAction(ISD::FSIN , MVT::f64, Expand);
221 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 setOperationAction(ISD::FREM , MVT::f64, Expand);
223 setOperationAction(ISD::FSIN , MVT::f32, Expand);
224 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f32, Expand);
226
Chris Lattnera54aa942006-01-29 06:26:08 +0000227 // Expand FP immediates into loads from the stack, except for the special
228 // cases we handle.
229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231 addLegalFPImmediate(+0.0); // xorps / xorpd
232 } else {
233 // Set up the FP register classes.
234 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000235
236 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
237
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 if (!UnsafeFPMath) {
239 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
240 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
241 }
242
Chris Lattnera54aa942006-01-29 06:26:08 +0000243 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244 addLegalFPImmediate(+0.0); // FLD0
245 addLegalFPImmediate(+1.0); // FLD1
246 addLegalFPImmediate(-0.0); // FLD0/FCHS
247 addLegalFPImmediate(-1.0); // FLD1/FCHS
248 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000249
Evan Chengd30bf012006-03-01 01:11:20 +0000250 // First set operation action for all vector types to expand. Then we
251 // will selectively turn on ones that can be effectively codegen'd.
252 for (unsigned VT = (unsigned)MVT::Vector + 1;
253 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Chris Lattner39afef32006-03-20 06:18:01 +0000258 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000259 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000260 }
261
Evan Chenga88973f2006-03-22 19:22:18 +0000262 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000263 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
266
Evan Chengd30bf012006-03-01 01:11:20 +0000267 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000268 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000271 }
272
Evan Chenga88973f2006-03-22 19:22:18 +0000273 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
275
Evan Cheng48090aa2006-03-21 23:01:21 +0000276 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
277 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
278 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
279 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
Evan Cheng386031a2006-03-24 07:29:27 +0000280 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000281 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000282 }
283
Evan Chenga88973f2006-03-22 19:22:18 +0000284 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000285 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
287 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
288 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
289 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
290
291
Evan Cheng48090aa2006-03-21 23:01:21 +0000292 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-03-23 01:57:24 +0000293 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
294 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
295 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng48090aa2006-03-21 23:01:21 +0000296 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
Evan Cheng7b1d34b2006-03-25 01:33:37 +0000297 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
298 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
299 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng48090aa2006-03-21 23:01:21 +0000300 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
301 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-03-23 01:57:24 +0000302 setOperationAction(ISD::LOAD, MVT::v16i8, Legal);
303 setOperationAction(ISD::LOAD, MVT::v8i16, Legal);
304 setOperationAction(ISD::LOAD, MVT::v4i32, Legal);
305 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng386031a2006-03-24 07:29:27 +0000306 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
307 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
308 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
309 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
310 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
Evan Cheng48090aa2006-03-21 23:01:21 +0000311 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
312 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000313 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000314 }
315
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000316 computeRegisterProperties();
317
Evan Cheng87ed7162006-02-14 08:25:08 +0000318 // FIXME: These should be based on subtarget info. Plus, the values should
319 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000320 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
321 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
322 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000323 allowUnalignedMemoryAccesses = true; // x86 supports it!
324}
325
326std::vector<SDOperand>
327X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
328 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
329 return LowerFastCCArguments(F, DAG);
330 return LowerCCCArguments(F, DAG);
331}
332
333std::pair<SDOperand, SDOperand>
334X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
335 bool isVarArg, unsigned CallingConv,
336 bool isTailCall,
337 SDOperand Callee, ArgListTy &Args,
338 SelectionDAG &DAG) {
339 assert((!isVarArg || CallingConv == CallingConv::C) &&
340 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000341
342 // If the callee is a GlobalAddress node (quite common, every direct call is)
343 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
344 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
345 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000346 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
347 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000348
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000349 if (CallingConv == CallingConv::Fast && EnableFastCC)
350 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
351 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
352}
353
354//===----------------------------------------------------------------------===//
355// C Calling Convention implementation
356//===----------------------------------------------------------------------===//
357
358std::vector<SDOperand>
359X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
360 std::vector<SDOperand> ArgValues;
361
362 MachineFunction &MF = DAG.getMachineFunction();
363 MachineFrameInfo *MFI = MF.getFrameInfo();
364
365 // Add DAG nodes to load the arguments... On entry to a function on the X86,
366 // the stack frame looks like this:
367 //
368 // [ESP] -- return address
369 // [ESP + 4] -- first argument (leftmost lexically)
370 // [ESP + 8] -- second argument, if first argument is four bytes in size
371 // ...
372 //
373 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
374 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
375 MVT::ValueType ObjectVT = getValueType(I->getType());
376 unsigned ArgIncrement = 4;
377 unsigned ObjSize;
378 switch (ObjectVT) {
379 default: assert(0 && "Unhandled argument type!");
380 case MVT::i1:
381 case MVT::i8: ObjSize = 1; break;
382 case MVT::i16: ObjSize = 2; break;
383 case MVT::i32: ObjSize = 4; break;
384 case MVT::i64: ObjSize = ArgIncrement = 8; break;
385 case MVT::f32: ObjSize = 4; break;
386 case MVT::f64: ObjSize = ArgIncrement = 8; break;
387 }
388 // Create the frame index object for this incoming parameter...
389 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
390
391 // Create the SelectionDAG nodes corresponding to a load from this parameter
392 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
393
394 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
395 // dead loads.
396 SDOperand ArgValue;
397 if (!I->use_empty())
398 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
399 DAG.getSrcValue(NULL));
400 else {
401 if (MVT::isInteger(ObjectVT))
402 ArgValue = DAG.getConstant(0, ObjectVT);
403 else
404 ArgValue = DAG.getConstantFP(0, ObjectVT);
405 }
406 ArgValues.push_back(ArgValue);
407
408 ArgOffset += ArgIncrement; // Move on to the next argument...
409 }
410
411 // If the function takes variable number of arguments, make a frame index for
412 // the start of the first vararg value... for expansion of llvm.va_start.
413 if (F.isVarArg())
414 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
415 ReturnAddrIndex = 0; // No return address slot generated yet.
416 BytesToPopOnReturn = 0; // Callee pops nothing.
417 BytesCallerReserves = ArgOffset;
418
419 // Finally, inform the code generator which regs we return values in.
420 switch (getValueType(F.getReturnType())) {
421 default: assert(0 && "Unknown type!");
422 case MVT::isVoid: break;
423 case MVT::i1:
424 case MVT::i8:
425 case MVT::i16:
426 case MVT::i32:
427 MF.addLiveOut(X86::EAX);
428 break;
429 case MVT::i64:
430 MF.addLiveOut(X86::EAX);
431 MF.addLiveOut(X86::EDX);
432 break;
433 case MVT::f32:
434 case MVT::f64:
435 MF.addLiveOut(X86::ST0);
436 break;
437 }
438 return ArgValues;
439}
440
441std::pair<SDOperand, SDOperand>
442X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
443 bool isVarArg, bool isTailCall,
444 SDOperand Callee, ArgListTy &Args,
445 SelectionDAG &DAG) {
446 // Count how many bytes are to be pushed on the stack.
447 unsigned NumBytes = 0;
448
449 if (Args.empty()) {
450 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000451 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452 } else {
453 for (unsigned i = 0, e = Args.size(); i != e; ++i)
454 switch (getValueType(Args[i].second)) {
455 default: assert(0 && "Unknown value type!");
456 case MVT::i1:
457 case MVT::i8:
458 case MVT::i16:
459 case MVT::i32:
460 case MVT::f32:
461 NumBytes += 4;
462 break;
463 case MVT::i64:
464 case MVT::f64:
465 NumBytes += 8;
466 break;
467 }
468
Chris Lattner94dd2922006-02-13 09:00:43 +0000469 Chain = DAG.getCALLSEQ_START(Chain,
470 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000471
472 // Arguments go on the stack in reverse order, as specified by the ABI.
473 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000474 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000475 std::vector<SDOperand> Stores;
476
477 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
478 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
479 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
480
481 switch (getValueType(Args[i].second)) {
482 default: assert(0 && "Unexpected ValueType for argument!");
483 case MVT::i1:
484 case MVT::i8:
485 case MVT::i16:
486 // Promote the integer to 32 bits. If the input type is signed use a
487 // sign extend, otherwise use a zero extend.
488 if (Args[i].second->isSigned())
489 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
490 else
491 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
492
493 // FALL THROUGH
494 case MVT::i32:
495 case MVT::f32:
496 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
497 Args[i].first, PtrOff,
498 DAG.getSrcValue(NULL)));
499 ArgOffset += 4;
500 break;
501 case MVT::i64:
502 case MVT::f64:
503 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
504 Args[i].first, PtrOff,
505 DAG.getSrcValue(NULL)));
506 ArgOffset += 8;
507 break;
508 }
509 }
510 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
511 }
512
513 std::vector<MVT::ValueType> RetVals;
514 MVT::ValueType RetTyVT = getValueType(RetTy);
515 RetVals.push_back(MVT::Other);
516
517 // The result values produced have to be legal. Promote the result.
518 switch (RetTyVT) {
519 case MVT::isVoid: break;
520 default:
521 RetVals.push_back(RetTyVT);
522 break;
523 case MVT::i1:
524 case MVT::i8:
525 case MVT::i16:
526 RetVals.push_back(MVT::i32);
527 break;
528 case MVT::f32:
529 if (X86ScalarSSE)
530 RetVals.push_back(MVT::f32);
531 else
532 RetVals.push_back(MVT::f64);
533 break;
534 case MVT::i64:
535 RetVals.push_back(MVT::i32);
536 RetVals.push_back(MVT::i32);
537 break;
538 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000539
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000540 std::vector<MVT::ValueType> NodeTys;
541 NodeTys.push_back(MVT::Other); // Returns a chain
542 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
543 std::vector<SDOperand> Ops;
544 Ops.push_back(Chain);
545 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000546
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000547 // FIXME: Do not generate X86ISD::TAILCALL for now.
548 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
549 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000550
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000551 NodeTys.clear();
552 NodeTys.push_back(MVT::Other); // Returns a chain
553 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
554 Ops.clear();
555 Ops.push_back(Chain);
556 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
557 Ops.push_back(DAG.getConstant(0, getPointerTy()));
558 Ops.push_back(InFlag);
559 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
560 InFlag = Chain.getValue(1);
561
562 SDOperand RetVal;
563 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000564 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000565 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000566 case MVT::i1:
567 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000568 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
569 Chain = RetVal.getValue(1);
570 if (RetTyVT == MVT::i1)
571 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
572 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000573 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000574 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, 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::i32:
578 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
579 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000580 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000581 case MVT::i64: {
582 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
583 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
584 Lo.getValue(2));
585 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
586 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000587 break;
588 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000589 case MVT::f32:
590 case MVT::f64: {
591 std::vector<MVT::ValueType> Tys;
592 Tys.push_back(MVT::f64);
593 Tys.push_back(MVT::Other);
594 Tys.push_back(MVT::Flag);
595 std::vector<SDOperand> Ops;
596 Ops.push_back(Chain);
597 Ops.push_back(InFlag);
598 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
599 Chain = RetVal.getValue(1);
600 InFlag = RetVal.getValue(2);
601 if (X86ScalarSSE) {
602 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
603 // shouldn't be necessary except that RFP cannot be live across
604 // multiple blocks. When stackifier is fixed, they can be uncoupled.
605 MachineFunction &MF = DAG.getMachineFunction();
606 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
607 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
608 Tys.clear();
609 Tys.push_back(MVT::Other);
610 Ops.clear();
611 Ops.push_back(Chain);
612 Ops.push_back(RetVal);
613 Ops.push_back(StackSlot);
614 Ops.push_back(DAG.getValueType(RetTyVT));
615 Ops.push_back(InFlag);
616 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
617 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
618 DAG.getSrcValue(NULL));
619 Chain = RetVal.getValue(1);
620 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000621
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000622 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
623 // FIXME: we would really like to remember that this FP_ROUND
624 // operation is okay to eliminate if we allow excess FP precision.
625 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
626 break;
627 }
628 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000629 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000630
631 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000632}
633
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000634//===----------------------------------------------------------------------===//
635// Fast Calling Convention implementation
636//===----------------------------------------------------------------------===//
637//
638// The X86 'fast' calling convention passes up to two integer arguments in
639// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
640// and requires that the callee pop its arguments off the stack (allowing proper
641// tail calls), and has the same return value conventions as C calling convs.
642//
643// This calling convention always arranges for the callee pop value to be 8n+4
644// bytes, which is needed for tail recursion elimination and stack alignment
645// reasons.
646//
647// Note that this can be enhanced in the future to pass fp vals in registers
648// (when we have a global fp allocator) and do other tricks.
649//
650
651/// AddLiveIn - This helper function adds the specified physical register to the
652/// MachineFunction as a live in value. It also creates a corresponding virtual
653/// register for it.
654static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
655 TargetRegisterClass *RC) {
656 assert(RC->contains(PReg) && "Not the correct regclass!");
657 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
658 MF.addLiveIn(PReg, VReg);
659 return VReg;
660}
661
Chris Lattner89fad2c2006-03-17 17:27:47 +0000662// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
663// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
664// EDX". Anything more is illegal.
665//
666// FIXME: The linscan register allocator currently has problem with
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000667// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-03-17 17:27:47 +0000668// a physreg with a virtreg, this increases the size of the physreg's live
669// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000670// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-03-17 17:27:47 +0000671// allocator to wedge itself.
672//
673// This code triggers this problem more often if we pass args in registers,
674// so disable it until this is fixed.
675//
676// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
677// about code being dead.
678//
679static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000680
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000681
682std::vector<SDOperand>
683X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
684 std::vector<SDOperand> ArgValues;
685
686 MachineFunction &MF = DAG.getMachineFunction();
687 MachineFrameInfo *MFI = MF.getFrameInfo();
688
689 // Add DAG nodes to load the arguments... On entry to a function the stack
690 // frame looks like this:
691 //
692 // [ESP] -- return address
693 // [ESP + 4] -- first nonreg argument (leftmost lexically)
694 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
695 // ...
696 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
697
698 // Keep track of the number of integer regs passed so far. This can be either
699 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
700 // used).
701 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000702
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000703 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
704 MVT::ValueType ObjectVT = getValueType(I->getType());
705 unsigned ArgIncrement = 4;
706 unsigned ObjSize = 0;
707 SDOperand ArgValue;
708
709 switch (ObjectVT) {
710 default: assert(0 && "Unhandled argument type!");
711 case MVT::i1:
712 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000713 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000714 if (!I->use_empty()) {
715 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
716 X86::R8RegisterClass);
717 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
718 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000719 if (ObjectVT == MVT::i1)
720 // FIXME: Should insert a assertzext here.
721 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000722 }
723 ++NumIntRegs;
724 break;
725 }
726
727 ObjSize = 1;
728 break;
729 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000730 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000731 if (!I->use_empty()) {
732 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
733 X86::R16RegisterClass);
734 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
735 DAG.setRoot(ArgValue.getValue(1));
736 }
737 ++NumIntRegs;
738 break;
739 }
740 ObjSize = 2;
741 break;
742 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000743 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000744 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000745 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000746 X86::R32RegisterClass);
747 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
748 DAG.setRoot(ArgValue.getValue(1));
749 }
750 ++NumIntRegs;
751 break;
752 }
753 ObjSize = 4;
754 break;
755 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000756 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000757 if (!I->use_empty()) {
758 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
759 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
760
761 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
762 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
763 DAG.setRoot(Hi.getValue(1));
764
765 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
766 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000767 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000768 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000769 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000770 if (!I->use_empty()) {
771 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
772 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
773 DAG.setRoot(Low.getValue(1));
774
775 // Load the high part from memory.
776 // Create the frame index object for this incoming parameter...
777 int FI = MFI->CreateFixedObject(4, ArgOffset);
778 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
779 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
780 DAG.getSrcValue(NULL));
781 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
782 }
783 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000784 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000785 break;
786 }
787 ObjSize = ArgIncrement = 8;
788 break;
789 case MVT::f32: ObjSize = 4; break;
790 case MVT::f64: ObjSize = ArgIncrement = 8; break;
791 }
792
793 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
794 // dead loads.
795 if (ObjSize && !I->use_empty()) {
796 // Create the frame index object for this incoming parameter...
797 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
798
799 // Create the SelectionDAG nodes corresponding to a load from this
800 // parameter.
801 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
802
803 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
804 DAG.getSrcValue(NULL));
805 } else if (ArgValue.Val == 0) {
806 if (MVT::isInteger(ObjectVT))
807 ArgValue = DAG.getConstant(0, ObjectVT);
808 else
809 ArgValue = DAG.getConstantFP(0, ObjectVT);
810 }
811 ArgValues.push_back(ArgValue);
812
813 if (ObjSize)
814 ArgOffset += ArgIncrement; // Move on to the next argument.
815 }
816
817 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
818 // arguments and the arguments after the retaddr has been pushed are aligned.
819 if ((ArgOffset & 7) == 0)
820 ArgOffset += 4;
821
822 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
823 ReturnAddrIndex = 0; // No return address slot generated yet.
824 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
825 BytesCallerReserves = 0;
826
827 // Finally, inform the code generator which regs we return values in.
828 switch (getValueType(F.getReturnType())) {
829 default: assert(0 && "Unknown type!");
830 case MVT::isVoid: break;
831 case MVT::i1:
832 case MVT::i8:
833 case MVT::i16:
834 case MVT::i32:
835 MF.addLiveOut(X86::EAX);
836 break;
837 case MVT::i64:
838 MF.addLiveOut(X86::EAX);
839 MF.addLiveOut(X86::EDX);
840 break;
841 case MVT::f32:
842 case MVT::f64:
843 MF.addLiveOut(X86::ST0);
844 break;
845 }
846 return ArgValues;
847}
848
849std::pair<SDOperand, SDOperand>
850X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
851 bool isTailCall, SDOperand Callee,
852 ArgListTy &Args, SelectionDAG &DAG) {
853 // Count how many bytes are to be pushed on the stack.
854 unsigned NumBytes = 0;
855
856 // Keep track of the number of integer regs passed so far. This can be either
857 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
858 // used).
859 unsigned NumIntRegs = 0;
860
861 for (unsigned i = 0, e = Args.size(); i != e; ++i)
862 switch (getValueType(Args[i].second)) {
863 default: assert(0 && "Unknown value type!");
864 case MVT::i1:
865 case MVT::i8:
866 case MVT::i16:
867 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000868 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000869 ++NumIntRegs;
870 break;
871 }
872 // fall through
873 case MVT::f32:
874 NumBytes += 4;
875 break;
876 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000877 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
878 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000879 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000880 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
881 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000882 NumBytes += 4;
883 break;
884 }
885
886 // fall through
887 case MVT::f64:
888 NumBytes += 8;
889 break;
890 }
891
892 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
893 // arguments and the arguments after the retaddr has been pushed are aligned.
894 if ((NumBytes & 7) == 0)
895 NumBytes += 4;
896
Chris Lattner94dd2922006-02-13 09:00:43 +0000897 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000898
899 // Arguments go on the stack in reverse order, as specified by the ABI.
900 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000901 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000902 NumIntRegs = 0;
903 std::vector<SDOperand> Stores;
904 std::vector<SDOperand> RegValuesToPass;
905 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
906 switch (getValueType(Args[i].second)) {
907 default: assert(0 && "Unexpected ValueType for argument!");
908 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000909 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
910 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000911 case MVT::i8:
912 case MVT::i16:
913 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000914 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000915 RegValuesToPass.push_back(Args[i].first);
916 ++NumIntRegs;
917 break;
918 }
919 // Fall through
920 case MVT::f32: {
921 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
922 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
923 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
924 Args[i].first, PtrOff,
925 DAG.getSrcValue(NULL)));
926 ArgOffset += 4;
927 break;
928 }
929 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000930 // Can pass (at least) part of it in regs?
931 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000932 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
933 Args[i].first, DAG.getConstant(1, MVT::i32));
934 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
935 Args[i].first, DAG.getConstant(0, MVT::i32));
936 RegValuesToPass.push_back(Lo);
937 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000938
939 // Pass both parts in regs?
940 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000941 RegValuesToPass.push_back(Hi);
942 ++NumIntRegs;
943 } else {
944 // Pass the high part in memory.
945 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
946 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
947 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
948 Hi, PtrOff, DAG.getSrcValue(NULL)));
949 ArgOffset += 4;
950 }
951 break;
952 }
953 // Fall through
954 case MVT::f64:
955 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
956 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
957 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
958 Args[i].first, PtrOff,
959 DAG.getSrcValue(NULL)));
960 ArgOffset += 8;
961 break;
962 }
963 }
964 if (!Stores.empty())
965 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
966
967 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
968 // arguments and the arguments after the retaddr has been pushed are aligned.
969 if ((ArgOffset & 7) == 0)
970 ArgOffset += 4;
971
972 std::vector<MVT::ValueType> RetVals;
973 MVT::ValueType RetTyVT = getValueType(RetTy);
974
975 RetVals.push_back(MVT::Other);
976
977 // The result values produced have to be legal. Promote the result.
978 switch (RetTyVT) {
979 case MVT::isVoid: break;
980 default:
981 RetVals.push_back(RetTyVT);
982 break;
983 case MVT::i1:
984 case MVT::i8:
985 case MVT::i16:
986 RetVals.push_back(MVT::i32);
987 break;
988 case MVT::f32:
989 if (X86ScalarSSE)
990 RetVals.push_back(MVT::f32);
991 else
992 RetVals.push_back(MVT::f64);
993 break;
994 case MVT::i64:
995 RetVals.push_back(MVT::i32);
996 RetVals.push_back(MVT::i32);
997 break;
998 }
999
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001000 // Build a sequence of copy-to-reg nodes chained together with token chain
1001 // and flag operands which copy the outgoing args into registers.
1002 SDOperand InFlag;
1003 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1004 unsigned CCReg;
1005 SDOperand RegToPass = RegValuesToPass[i];
1006 switch (RegToPass.getValueType()) {
1007 default: assert(0 && "Bad thing to pass in regs");
1008 case MVT::i8:
1009 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +00001010 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001011 case MVT::i16:
1012 CCReg = (i == 0) ? X86::AX : X86::DX;
1013 break;
1014 case MVT::i32:
1015 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1016 break;
1017 }
1018
1019 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1020 InFlag = Chain.getValue(1);
1021 }
1022
1023 std::vector<MVT::ValueType> NodeTys;
1024 NodeTys.push_back(MVT::Other); // Returns a chain
1025 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1026 std::vector<SDOperand> Ops;
1027 Ops.push_back(Chain);
1028 Ops.push_back(Callee);
1029 if (InFlag.Val)
1030 Ops.push_back(InFlag);
1031
1032 // FIXME: Do not generate X86ISD::TAILCALL for now.
1033 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1034 InFlag = Chain.getValue(1);
1035
1036 NodeTys.clear();
1037 NodeTys.push_back(MVT::Other); // Returns a chain
1038 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1039 Ops.clear();
1040 Ops.push_back(Chain);
1041 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1042 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1043 Ops.push_back(InFlag);
1044 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1045 InFlag = Chain.getValue(1);
1046
1047 SDOperand RetVal;
1048 if (RetTyVT != MVT::isVoid) {
1049 switch (RetTyVT) {
1050 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001051 case MVT::i1:
1052 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001053 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1054 Chain = RetVal.getValue(1);
1055 if (RetTyVT == MVT::i1)
1056 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1057 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001058 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001059 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, 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::i32:
1063 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1064 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001065 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001066 case MVT::i64: {
1067 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1068 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1069 Lo.getValue(2));
1070 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1071 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001072 break;
1073 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001074 case MVT::f32:
1075 case MVT::f64: {
1076 std::vector<MVT::ValueType> Tys;
1077 Tys.push_back(MVT::f64);
1078 Tys.push_back(MVT::Other);
1079 Tys.push_back(MVT::Flag);
1080 std::vector<SDOperand> Ops;
1081 Ops.push_back(Chain);
1082 Ops.push_back(InFlag);
1083 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1084 Chain = RetVal.getValue(1);
1085 InFlag = RetVal.getValue(2);
1086 if (X86ScalarSSE) {
1087 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1088 // shouldn't be necessary except that RFP cannot be live across
1089 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1090 MachineFunction &MF = DAG.getMachineFunction();
1091 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1092 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1093 Tys.clear();
1094 Tys.push_back(MVT::Other);
1095 Ops.clear();
1096 Ops.push_back(Chain);
1097 Ops.push_back(RetVal);
1098 Ops.push_back(StackSlot);
1099 Ops.push_back(DAG.getValueType(RetTyVT));
1100 Ops.push_back(InFlag);
1101 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1102 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1103 DAG.getSrcValue(NULL));
1104 Chain = RetVal.getValue(1);
1105 }
Evan Chengd9558e02006-01-06 00:43:03 +00001106
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001107 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1108 // FIXME: we would really like to remember that this FP_ROUND
1109 // operation is okay to eliminate if we allow excess FP precision.
1110 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1111 break;
1112 }
1113 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001114 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001115
1116 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001117}
1118
1119SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1120 if (ReturnAddrIndex == 0) {
1121 // Set up a frame object for the return address.
1122 MachineFunction &MF = DAG.getMachineFunction();
1123 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1124 }
1125
1126 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1127}
1128
1129
1130
1131std::pair<SDOperand, SDOperand> X86TargetLowering::
1132LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1133 SelectionDAG &DAG) {
1134 SDOperand Result;
1135 if (Depth) // Depths > 0 not supported yet!
1136 Result = DAG.getConstant(0, getPointerTy());
1137 else {
1138 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1139 if (!isFrameAddress)
1140 // Just load the return address
1141 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1142 DAG.getSrcValue(NULL));
1143 else
1144 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1145 DAG.getConstant(4, MVT::i32));
1146 }
1147 return std::make_pair(Result, Chain);
1148}
1149
Evan Cheng4a460802006-01-11 00:33:36 +00001150/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1151/// which corresponds to the condition code.
1152static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1153 switch (X86CC) {
1154 default: assert(0 && "Unknown X86 conditional code!");
1155 case X86ISD::COND_A: return X86::JA;
1156 case X86ISD::COND_AE: return X86::JAE;
1157 case X86ISD::COND_B: return X86::JB;
1158 case X86ISD::COND_BE: return X86::JBE;
1159 case X86ISD::COND_E: return X86::JE;
1160 case X86ISD::COND_G: return X86::JG;
1161 case X86ISD::COND_GE: return X86::JGE;
1162 case X86ISD::COND_L: return X86::JL;
1163 case X86ISD::COND_LE: return X86::JLE;
1164 case X86ISD::COND_NE: return X86::JNE;
1165 case X86ISD::COND_NO: return X86::JNO;
1166 case X86ISD::COND_NP: return X86::JNP;
1167 case X86ISD::COND_NS: return X86::JNS;
1168 case X86ISD::COND_O: return X86::JO;
1169 case X86ISD::COND_P: return X86::JP;
1170 case X86ISD::COND_S: return X86::JS;
1171 }
1172}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001173
Evan Cheng6dfa9992006-01-30 23:41:35 +00001174/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1175/// specific condition code. It returns a false if it cannot do a direct
1176/// translation. X86CC is the translated CondCode. Flip is set to true if the
1177/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001178static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1179 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001180 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001181 Flip = false;
1182 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001183 if (!isFP) {
1184 switch (SetCCOpcode) {
1185 default: break;
1186 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1187 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1188 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1189 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1190 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1191 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1192 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1193 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1194 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1195 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1196 }
1197 } else {
1198 // On a floating point condition, the flags are set as follows:
1199 // ZF PF CF op
1200 // 0 | 0 | 0 | X > Y
1201 // 0 | 0 | 1 | X < Y
1202 // 1 | 0 | 0 | X == Y
1203 // 1 | 1 | 1 | unordered
1204 switch (SetCCOpcode) {
1205 default: break;
1206 case ISD::SETUEQ:
1207 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001208 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001209 case ISD::SETOGT:
1210 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001211 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001212 case ISD::SETOGE:
1213 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001214 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001215 case ISD::SETULT:
1216 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001217 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001218 case ISD::SETULE:
1219 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1220 case ISD::SETONE:
1221 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1222 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1223 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1224 }
1225 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001226
1227 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001228}
1229
Evan Cheng4a460802006-01-11 00:33:36 +00001230/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1231/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001232/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001233static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001234 switch (X86CC) {
1235 default:
1236 return false;
1237 case X86ISD::COND_B:
1238 case X86ISD::COND_BE:
1239 case X86ISD::COND_E:
1240 case X86ISD::COND_P:
1241 case X86ISD::COND_A:
1242 case X86ISD::COND_AE:
1243 case X86ISD::COND_NE:
1244 case X86ISD::COND_NP:
1245 return true;
1246 }
1247}
1248
Evan Cheng4a460802006-01-11 00:33:36 +00001249MachineBasicBlock *
1250X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1251 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001252 switch (MI->getOpcode()) {
1253 default: assert(false && "Unexpected instr type to insert");
1254 case X86::CMOV_FR32:
1255 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001256 // To "insert" a SELECT_CC instruction, we actually have to insert the
1257 // diamond control-flow pattern. The incoming instruction knows the
1258 // destination vreg to set, the condition code register to branch on, the
1259 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001260 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1261 ilist<MachineBasicBlock>::iterator It = BB;
1262 ++It;
1263
1264 // thisMBB:
1265 // ...
1266 // TrueVal = ...
1267 // cmpTY ccX, r1, r2
1268 // bCC copy1MBB
1269 // fallthrough --> copy0MBB
1270 MachineBasicBlock *thisMBB = BB;
1271 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1272 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1273 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1274 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1275 MachineFunction *F = BB->getParent();
1276 F->getBasicBlockList().insert(It, copy0MBB);
1277 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001278 // Update machine-CFG edges by first adding all successors of the current
1279 // block to the new block which will contain the Phi node for the select.
1280 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1281 e = BB->succ_end(); i != e; ++i)
1282 sinkMBB->addSuccessor(*i);
1283 // Next, remove all successors of the current block, and add the true
1284 // and fallthrough blocks as its successors.
1285 while(!BB->succ_empty())
1286 BB->removeSuccessor(BB->succ_begin());
Evan Cheng0cc39452006-01-16 21:21:29 +00001287 BB->addSuccessor(copy0MBB);
1288 BB->addSuccessor(sinkMBB);
1289
1290 // copy0MBB:
1291 // %FalseValue = ...
1292 // # fallthrough to sinkMBB
1293 BB = copy0MBB;
1294
1295 // Update machine-CFG edges
1296 BB->addSuccessor(sinkMBB);
1297
1298 // sinkMBB:
1299 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1300 // ...
1301 BB = sinkMBB;
1302 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1303 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1304 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001305
Evan Cheng0cc39452006-01-16 21:21:29 +00001306 delete MI; // The pseudo instruction is gone now.
1307 return BB;
1308 }
Evan Cheng4a460802006-01-11 00:33:36 +00001309
Evan Cheng0cc39452006-01-16 21:21:29 +00001310 case X86::FP_TO_INT16_IN_MEM:
1311 case X86::FP_TO_INT32_IN_MEM:
1312 case X86::FP_TO_INT64_IN_MEM: {
1313 // Change the floating point control register to use "round towards zero"
1314 // mode when truncating to an integer value.
1315 MachineFunction *F = BB->getParent();
1316 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1317 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1318
1319 // Load the old value of the high byte of the control word...
1320 unsigned OldCW =
1321 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1322 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1323
1324 // Set the high part to be round to zero...
1325 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1326
1327 // Reload the modified control word now...
1328 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1329
1330 // Restore the memory image of control word to original value
1331 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1332
1333 // Get the X86 opcode to use.
1334 unsigned Opc;
1335 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001336 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001337 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1338 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1339 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1340 }
1341
1342 X86AddressMode AM;
1343 MachineOperand &Op = MI->getOperand(0);
1344 if (Op.isRegister()) {
1345 AM.BaseType = X86AddressMode::RegBase;
1346 AM.Base.Reg = Op.getReg();
1347 } else {
1348 AM.BaseType = X86AddressMode::FrameIndexBase;
1349 AM.Base.FrameIndex = Op.getFrameIndex();
1350 }
1351 Op = MI->getOperand(1);
1352 if (Op.isImmediate())
1353 AM.Scale = Op.getImmedValue();
1354 Op = MI->getOperand(2);
1355 if (Op.isImmediate())
1356 AM.IndexReg = Op.getImmedValue();
1357 Op = MI->getOperand(3);
1358 if (Op.isGlobalAddress()) {
1359 AM.GV = Op.getGlobal();
1360 } else {
1361 AM.Disp = Op.getImmedValue();
1362 }
1363 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1364
1365 // Reload the original control word now.
1366 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1367
1368 delete MI; // The pseudo instruction is gone now.
1369 return BB;
1370 }
1371 }
Evan Cheng4a460802006-01-11 00:33:36 +00001372}
1373
1374
1375//===----------------------------------------------------------------------===//
1376// X86 Custom Lowering Hooks
1377//===----------------------------------------------------------------------===//
1378
Evan Cheng30b37b52006-03-13 23:18:16 +00001379/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1380/// load. For Darwin, external and weak symbols are indirect, loading the value
1381/// at address GV rather then the value of GV itself. This means that the
1382/// GlobalAddress must be in the base or index register of the address, not the
1383/// GV offset field.
1384static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1385 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1386 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1387}
1388
Evan Cheng0188ecb2006-03-22 18:59:22 +00001389/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1390/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1391bool X86::isPSHUFDMask(SDNode *N) {
1392 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1393
1394 if (N->getNumOperands() != 4)
1395 return false;
1396
1397 // Check if the value doesn't reference the second vector.
Evan Cheng14aed5e2006-03-24 01:18:28 +00001398 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng0188ecb2006-03-22 18:59:22 +00001399 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1400 "Invalid VECTOR_SHUFFLE mask!");
1401 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() >= 4) return false;
1402 }
1403
1404 return true;
1405}
1406
Evan Cheng14aed5e2006-03-24 01:18:28 +00001407/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1408/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1409bool X86::isSHUFPMask(SDNode *N) {
1410 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1411
Evan Chengbc4832b2006-03-24 23:15:12 +00001412 unsigned NumElems = N->getNumOperands();
1413 if (NumElems == 2) {
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001414 // The only case that ought be handled by SHUFPD is
1415 // Dest { 2, 1 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
1416 // Expect bit 0 == 1, bit1 == 2
1417 SDOperand Bit0 = N->getOperand(0);
1418 SDOperand Bit1 = N->getOperand(1);
1419 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1420 "Invalid VECTOR_SHUFFLE mask!");
1421 return (cast<ConstantSDNode>(Bit0)->getValue() == 1 &&
1422 cast<ConstantSDNode>(Bit1)->getValue() == 2);
1423 }
1424
Evan Chengbc4832b2006-03-24 23:15:12 +00001425 if (NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001426
1427 // Each half must refer to only one of the vector.
1428 SDOperand Elt = N->getOperand(0);
1429 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengbc4832b2006-03-24 23:15:12 +00001430 for (unsigned i = 1; i != NumElems / 2; ++i) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001431 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1432 "Invalid VECTOR_SHUFFLE mask!");
1433 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() !=
1434 cast<ConstantSDNode>(Elt)->getValue())
1435 return false;
1436 }
Evan Chengbc4832b2006-03-24 23:15:12 +00001437 Elt = N->getOperand(NumElems / 2);
Evan Cheng14aed5e2006-03-24 01:18:28 +00001438 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengbc4832b2006-03-24 23:15:12 +00001439 for (unsigned i = NumElems / 2; i != NumElems; ++i) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001440 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1441 "Invalid VECTOR_SHUFFLE mask!");
1442 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() !=
1443 cast<ConstantSDNode>(Elt)->getValue())
1444 return false;
1445 }
1446
1447 return true;
1448}
1449
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001450/// isMOVLHPSorUNPCKLPDMask - Return true if the specified VECTOR_SHUFFLE
1451/// operand specifies a shuffle of elements that is suitable for input to
1452/// MOVLHPS or UNPCKLPD.
1453bool X86::isMOVLHPSorUNPCKLPDMask(SDNode *N) {
1454 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1455
1456 if (N->getNumOperands() != 2)
1457 return false;
1458
1459 // Expect bit 0 == 0, bit1 == 2
1460 SDOperand Bit0 = N->getOperand(0);
1461 SDOperand Bit1 = N->getOperand(1);
1462 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1463 "Invalid VECTOR_SHUFFLE mask!");
1464 return (cast<ConstantSDNode>(Bit0)->getValue() == 0 &&
1465 cast<ConstantSDNode>(Bit1)->getValue() == 2);
1466}
1467
1468/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1469/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1470bool X86::isMOVHLPSMask(SDNode *N) {
1471 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1472
1473 if (N->getNumOperands() != 2)
1474 return false;
1475
1476 // Expect bit 0 == 0, bit1 == 3
1477 SDOperand Bit0 = N->getOperand(0);
1478 SDOperand Bit1 = N->getOperand(1);
1479 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1480 "Invalid VECTOR_SHUFFLE mask!");
1481 return (cast<ConstantSDNode>(Bit0)->getValue() == 0 &&
1482 cast<ConstantSDNode>(Bit1)->getValue() == 3);
1483}
1484
1485/// isUNPCKHPDMask - Return true if the specified VECTOR_SHUFFLE operand
1486/// specifies a shuffle of elements that is suitable for input to UNPCKHPD.
1487bool X86::isUNPCKHPDMask(SDNode *N) {
1488 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1489
1490 if (N->getNumOperands() != 2)
1491 return false;
1492
1493 // Expect bit 0 == 1, bit1 == 3
1494 SDOperand Bit0 = N->getOperand(0);
1495 SDOperand Bit1 = N->getOperand(1);
1496 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1497 "Invalid VECTOR_SHUFFLE mask!");
1498 return (cast<ConstantSDNode>(Bit0)->getValue() == 1 &&
1499 cast<ConstantSDNode>(Bit1)->getValue() == 3);
1500}
1501
Evan Chengb9df0ca2006-03-22 02:53:00 +00001502/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1503/// a splat of a single element.
1504bool X86::isSplatMask(SDNode *N) {
1505 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1506
1507 // We can only splat 64-bit, and 32-bit quantities.
1508 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1509 return false;
1510
1511 // This is a splat operation if each element of the permute is the same, and
1512 // if the value doesn't reference the second vector.
1513 SDOperand Elt = N->getOperand(0);
1514 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1515 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1516 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1517 "Invalid VECTOR_SHUFFLE mask!");
1518 if (N->getOperand(i) != Elt) return false;
1519 }
1520
1521 // Make sure it is a splat of the first vector operand.
1522 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1523}
1524
Evan Cheng63d33002006-03-22 08:01:21 +00001525/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1526/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1527/// instructions.
1528unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001529 unsigned NumOperands = N->getNumOperands();
1530 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1531 unsigned Mask = 0;
1532 unsigned i = NumOperands - 1;
1533 do {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001534 unsigned Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1535 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001536 Mask |= Val;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001537 Mask <<= Shift;
Evan Cheng63d33002006-03-22 08:01:21 +00001538 --i;
1539 } while (i != 0);
1540
1541 return Mask;
1542}
1543
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001544/// LowerOperation - Provide custom lowering hooks for some operations.
1545///
1546SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1547 switch (Op.getOpcode()) {
1548 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001549 case ISD::SHL_PARTS:
1550 case ISD::SRA_PARTS:
1551 case ISD::SRL_PARTS: {
1552 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1553 "Not an i64 shift!");
1554 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1555 SDOperand ShOpLo = Op.getOperand(0);
1556 SDOperand ShOpHi = Op.getOperand(1);
1557 SDOperand ShAmt = Op.getOperand(2);
1558 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001559 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001560 : DAG.getConstant(0, MVT::i32);
1561
1562 SDOperand Tmp2, Tmp3;
1563 if (Op.getOpcode() == ISD::SHL_PARTS) {
1564 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1565 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1566 } else {
1567 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001568 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001569 }
1570
1571 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1572 ShAmt, DAG.getConstant(32, MVT::i8));
1573
1574 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001575 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001576
1577 std::vector<MVT::ValueType> Tys;
1578 Tys.push_back(MVT::i32);
1579 Tys.push_back(MVT::Flag);
1580 std::vector<SDOperand> Ops;
1581 if (Op.getOpcode() == ISD::SHL_PARTS) {
1582 Ops.push_back(Tmp2);
1583 Ops.push_back(Tmp3);
1584 Ops.push_back(CC);
1585 Ops.push_back(InFlag);
1586 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1587 InFlag = Hi.getValue(1);
1588
1589 Ops.clear();
1590 Ops.push_back(Tmp3);
1591 Ops.push_back(Tmp1);
1592 Ops.push_back(CC);
1593 Ops.push_back(InFlag);
1594 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1595 } else {
1596 Ops.push_back(Tmp2);
1597 Ops.push_back(Tmp3);
1598 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001599 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001600 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1601 InFlag = Lo.getValue(1);
1602
1603 Ops.clear();
1604 Ops.push_back(Tmp3);
1605 Ops.push_back(Tmp1);
1606 Ops.push_back(CC);
1607 Ops.push_back(InFlag);
1608 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1609 }
1610
1611 Tys.clear();
1612 Tys.push_back(MVT::i32);
1613 Tys.push_back(MVT::i32);
1614 Ops.clear();
1615 Ops.push_back(Lo);
1616 Ops.push_back(Hi);
1617 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1618 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001619 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001620 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001621 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001622 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001623
1624 SDOperand Result;
1625 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1626 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001627 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001628 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001629 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001630 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1631 DAG.getEntryNode(), Op.getOperand(0),
1632 StackSlot, DAG.getSrcValue(NULL));
1633
1634 // Build the FILD
1635 std::vector<MVT::ValueType> Tys;
1636 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001637 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001638 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001639 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001640 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001641 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001642 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001643 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1644 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001645
1646 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001647 Chain = Result.getValue(1);
1648 SDOperand InFlag = Result.getValue(2);
1649
Evan Chenge3de85b2006-02-04 02:20:30 +00001650 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001651 // shouldn't be necessary except that RFP cannot be live across
1652 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1653 MachineFunction &MF = DAG.getMachineFunction();
1654 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1655 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1656 std::vector<MVT::ValueType> Tys;
1657 Tys.push_back(MVT::Other);
1658 std::vector<SDOperand> Ops;
1659 Ops.push_back(Chain);
1660 Ops.push_back(Result);
1661 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001662 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001663 Ops.push_back(InFlag);
1664 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1665 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1666 DAG.getSrcValue(NULL));
1667 }
1668
Evan Chenga3195e82006-01-12 22:54:21 +00001669 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001670 }
1671 case ISD::FP_TO_SINT: {
1672 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001673 "Unknown FP_TO_SINT to lower!");
1674 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1675 // stack slot.
1676 MachineFunction &MF = DAG.getMachineFunction();
1677 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1678 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1679 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1680
1681 unsigned Opc;
1682 switch (Op.getValueType()) {
1683 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1684 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1685 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1686 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1687 }
1688
Evan Cheng6dab0532006-01-30 08:02:57 +00001689 SDOperand Chain = DAG.getEntryNode();
1690 SDOperand Value = Op.getOperand(0);
1691 if (X86ScalarSSE) {
1692 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1693 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1694 DAG.getSrcValue(0));
1695 std::vector<MVT::ValueType> Tys;
1696 Tys.push_back(MVT::f64);
1697 Tys.push_back(MVT::Other);
1698 std::vector<SDOperand> Ops;
1699 Ops.push_back(Chain);
1700 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001701 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001702 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1703 Chain = Value.getValue(1);
1704 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1705 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1706 }
1707
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001708 // Build the FP_TO_INT*_IN_MEM
1709 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001710 Ops.push_back(Chain);
1711 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001712 Ops.push_back(StackSlot);
1713 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1714
1715 // Load the result.
1716 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1717 DAG.getSrcValue(NULL));
1718 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001719 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001720 std::vector<MVT::ValueType> Tys;
1721 Tys.push_back(MVT::Other);
1722 Tys.push_back(MVT::Flag);
1723 std::vector<SDOperand> Ops;
1724 Ops.push_back(Op.getOperand(0));
1725 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001726 Ops.clear();
1727 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1728 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1729 MVT::i32, Ops[0].getValue(2)));
1730 Ops.push_back(Ops[1].getValue(1));
1731 Tys[0] = Tys[1] = MVT::i32;
1732 Tys.push_back(MVT::Other);
1733 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001734 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001735 case ISD::FABS: {
1736 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001737 const Type *OpNTy = MVT::getTypeForValueType(VT);
1738 std::vector<Constant*> CV;
1739 if (VT == MVT::f64) {
1740 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1741 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1742 } else {
1743 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1744 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1745 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1746 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1747 }
1748 Constant *CS = ConstantStruct::get(CV);
1749 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1750 SDOperand Mask
1751 = DAG.getNode(X86ISD::LOAD_PACK,
1752 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001753 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1754 }
Evan Cheng223547a2006-01-31 22:28:30 +00001755 case ISD::FNEG: {
1756 MVT::ValueType VT = Op.getValueType();
1757 const Type *OpNTy = MVT::getTypeForValueType(VT);
1758 std::vector<Constant*> CV;
1759 if (VT == MVT::f64) {
1760 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1761 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1762 } else {
1763 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1764 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1765 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1766 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1767 }
1768 Constant *CS = ConstantStruct::get(CV);
1769 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1770 SDOperand Mask
1771 = DAG.getNode(X86ISD::LOAD_PACK,
1772 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1773 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1774 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001775 case ISD::SETCC: {
1776 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001777 SDOperand Cond;
1778 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001779 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1780 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001781 bool Flip;
1782 unsigned X86CC;
1783 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1784 if (Flip)
1785 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1786 Op.getOperand(1), Op.getOperand(0));
1787 else
1788 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1789 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001790 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1791 DAG.getConstant(X86CC, MVT::i8), Cond);
1792 } else {
1793 assert(isFP && "Illegal integer SetCC!");
1794
Evan Cheng6dfa9992006-01-30 23:41:35 +00001795 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1796 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001797 std::vector<MVT::ValueType> Tys;
1798 std::vector<SDOperand> Ops;
1799 switch (SetCCOpcode) {
1800 default: assert(false && "Illegal floating point SetCC!");
1801 case ISD::SETOEQ: { // !PF & ZF
1802 Tys.push_back(MVT::i8);
1803 Tys.push_back(MVT::Flag);
1804 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1805 Ops.push_back(Cond);
1806 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1807 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1808 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1809 Tmp1.getValue(1));
1810 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1811 }
Evan Chengd9558e02006-01-06 00:43:03 +00001812 case ISD::SETUNE: { // PF | !ZF
1813 Tys.push_back(MVT::i8);
1814 Tys.push_back(MVT::Flag);
1815 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1816 Ops.push_back(Cond);
1817 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1818 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1819 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1820 Tmp1.getValue(1));
1821 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1822 }
1823 }
1824 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001825 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001826 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001827 MVT::ValueType VT = Op.getValueType();
1828 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001829 bool isFPStack = isFP && !X86ScalarSSE;
1830 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001831 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001832 SDOperand Op0 = Op.getOperand(0);
1833 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001834 if (Op0.getOpcode() == ISD::SETCC)
1835 Op0 = LowerOperation(Op0, DAG);
1836
Evan Chengaaca22c2006-01-10 20:26:56 +00001837 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001838 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1839 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1840 // have another use it will be eliminated.
1841 // If the X86ISD::SETCC has more than one use, then it's probably better
1842 // to use a test instead of duplicating the X86ISD::CMP (for register
1843 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001844 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1845 if (!Op0.hasOneUse()) {
1846 std::vector<MVT::ValueType> Tys;
1847 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1848 Tys.push_back(Op0.Val->getValueType(i));
1849 std::vector<SDOperand> Ops;
1850 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1851 Ops.push_back(Op0.getOperand(i));
1852 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1853 }
1854
Evan Cheng1bcee362006-01-13 01:03:02 +00001855 CC = Op0.getOperand(0);
1856 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001857 // Make a copy as flag result cannot be used by more than one.
1858 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1859 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001860 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001861 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001862 } else
1863 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001864 } else
1865 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001866
Evan Cheng189d01e2006-01-13 01:06:49 +00001867 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001868 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001869 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001870 }
Evan Chenge3413162006-01-09 18:33:28 +00001871
1872 std::vector<MVT::ValueType> Tys;
1873 Tys.push_back(Op.getValueType());
1874 Tys.push_back(MVT::Flag);
1875 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001876 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1877 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001878 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001879 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001880 Ops.push_back(CC);
1881 Ops.push_back(Cond);
1882 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001883 }
Evan Cheng898101c2005-12-19 23:12:38 +00001884 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001885 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001886 SDOperand Cond = Op.getOperand(1);
1887 SDOperand Dest = Op.getOperand(2);
1888 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001889 if (Cond.getOpcode() == ISD::SETCC)
1890 Cond = LowerOperation(Cond, DAG);
1891
Evan Chengd5781fc2005-12-21 20:21:51 +00001892 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001893 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1894 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1895 // have another use it will be eliminated.
1896 // If the X86ISD::SETCC has more than one use, then it's probably better
1897 // to use a test instead of duplicating the X86ISD::CMP (for register
1898 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001899 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1900 if (!Cond.hasOneUse()) {
1901 std::vector<MVT::ValueType> Tys;
1902 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1903 Tys.push_back(Cond.Val->getValueType(i));
1904 std::vector<SDOperand> Ops;
1905 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1906 Ops.push_back(Cond.getOperand(i));
1907 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1908 }
1909
Evan Cheng1bcee362006-01-13 01:03:02 +00001910 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001911 Cond = Cond.getOperand(1);
1912 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001913 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001914 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001915 } else
1916 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001917 } else
1918 addTest = true;
1919
1920 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001921 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001922 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1923 }
1924 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1925 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1926 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001927 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001928 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001929 SDOperand Chain = Op.getOperand(0);
1930 unsigned Align =
1931 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1932 if (Align == 0) Align = 1;
1933
Evan Cheng18a84522006-02-16 00:21:07 +00001934 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1935 // If not DWORD aligned, call memset if size is less than the threshold.
1936 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001937 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001938 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001939 MVT::ValueType IntPtr = getPointerTy();
1940 const Type *IntPtrTy = getTargetData().getIntPtrType();
1941 std::vector<std::pair<SDOperand, const Type*> > Args;
1942 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1943 // Extend the ubyte argument to be an int value for the call.
1944 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1945 Args.push_back(std::make_pair(Val, IntPtrTy));
1946 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1947 std::pair<SDOperand,SDOperand> CallResult =
1948 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1949 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1950 return CallResult.second;
1951 }
1952
Evan Cheng67f92a72006-01-11 22:15:48 +00001953 MVT::ValueType AVT;
1954 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001955 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1956 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001957 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001958 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001959 unsigned ValReg;
1960 unsigned Val = ValC->getValue() & 255;
1961
1962 // If the value is a constant, then we can potentially use larger sets.
1963 switch (Align & 3) {
1964 case 2: // WORD aligned
1965 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001966 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1967 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001968 Val = (Val << 8) | Val;
1969 ValReg = X86::AX;
1970 break;
1971 case 0: // DWORD aligned
1972 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001973 if (I) {
1974 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1975 BytesLeft = I->getValue() % 4;
1976 } else {
1977 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1978 DAG.getConstant(2, MVT::i8));
1979 TwoRepStos = true;
1980 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001981 Val = (Val << 8) | Val;
1982 Val = (Val << 16) | Val;
1983 ValReg = X86::EAX;
1984 break;
1985 default: // Byte aligned
1986 AVT = MVT::i8;
1987 Count = Op.getOperand(3);
1988 ValReg = X86::AL;
1989 break;
1990 }
1991
1992 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1993 InFlag);
1994 InFlag = Chain.getValue(1);
1995 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001996 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001997 Count = Op.getOperand(3);
1998 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1999 InFlag = Chain.getValue(1);
2000 }
2001
2002 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2003 InFlag = Chain.getValue(1);
2004 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2005 InFlag = Chain.getValue(1);
2006
Evan Chengff909922006-03-07 23:29:39 +00002007 std::vector<MVT::ValueType> Tys;
2008 Tys.push_back(MVT::Other);
2009 Tys.push_back(MVT::Flag);
2010 std::vector<SDOperand> Ops;
2011 Ops.push_back(Chain);
2012 Ops.push_back(DAG.getValueType(AVT));
2013 Ops.push_back(InFlag);
2014 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2015
2016 if (TwoRepStos) {
2017 InFlag = Chain.getValue(1);
2018 Count = Op.getOperand(3);
2019 MVT::ValueType CVT = Count.getValueType();
2020 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2021 DAG.getConstant(3, CVT));
2022 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2023 InFlag = Chain.getValue(1);
2024 Tys.clear();
2025 Tys.push_back(MVT::Other);
2026 Tys.push_back(MVT::Flag);
2027 Ops.clear();
2028 Ops.push_back(Chain);
2029 Ops.push_back(DAG.getValueType(MVT::i8));
2030 Ops.push_back(InFlag);
2031 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2032 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002033 // Issue stores for the last 1 - 3 bytes.
2034 SDOperand Value;
2035 unsigned Val = ValC->getValue() & 255;
2036 unsigned Offset = I->getValue() - BytesLeft;
2037 SDOperand DstAddr = Op.getOperand(1);
2038 MVT::ValueType AddrVT = DstAddr.getValueType();
2039 if (BytesLeft >= 2) {
2040 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2041 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2042 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2043 DAG.getConstant(Offset, AddrVT)),
2044 DAG.getSrcValue(NULL));
2045 BytesLeft -= 2;
2046 Offset += 2;
2047 }
2048
2049 if (BytesLeft == 1) {
2050 Value = DAG.getConstant(Val, MVT::i8);
2051 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2052 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2053 DAG.getConstant(Offset, AddrVT)),
2054 DAG.getSrcValue(NULL));
2055 }
2056 }
2057
2058 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002059 }
2060 case ISD::MEMCPY: {
2061 SDOperand Chain = Op.getOperand(0);
2062 unsigned Align =
2063 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2064 if (Align == 0) Align = 1;
2065
Evan Cheng18a84522006-02-16 00:21:07 +00002066 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2067 // If not DWORD aligned, call memcpy if size is less than the threshold.
2068 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002069 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002070 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002071 MVT::ValueType IntPtr = getPointerTy();
2072 const Type *IntPtrTy = getTargetData().getIntPtrType();
2073 std::vector<std::pair<SDOperand, const Type*> > Args;
2074 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2075 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2076 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2077 std::pair<SDOperand,SDOperand> CallResult =
2078 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2079 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2080 return CallResult.second;
2081 }
2082
Evan Cheng67f92a72006-01-11 22:15:48 +00002083 MVT::ValueType AVT;
2084 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002085 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002086 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00002087 switch (Align & 3) {
2088 case 2: // WORD aligned
2089 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002090 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2091 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002092 break;
2093 case 0: // DWORD aligned
2094 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002095 if (I) {
2096 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2097 BytesLeft = I->getValue() % 4;
2098 } else {
2099 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2100 DAG.getConstant(2, MVT::i8));
2101 TwoRepMovs = true;
2102 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002103 break;
2104 default: // Byte aligned
2105 AVT = MVT::i8;
2106 Count = Op.getOperand(3);
2107 break;
2108 }
2109
Evan Cheng62bec2c2006-03-04 02:48:56 +00002110 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002111 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2112 InFlag = Chain.getValue(1);
2113 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2114 InFlag = Chain.getValue(1);
2115 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2116 InFlag = Chain.getValue(1);
2117
Evan Chengff909922006-03-07 23:29:39 +00002118 std::vector<MVT::ValueType> Tys;
2119 Tys.push_back(MVT::Other);
2120 Tys.push_back(MVT::Flag);
2121 std::vector<SDOperand> Ops;
2122 Ops.push_back(Chain);
2123 Ops.push_back(DAG.getValueType(AVT));
2124 Ops.push_back(InFlag);
2125 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2126
2127 if (TwoRepMovs) {
2128 InFlag = Chain.getValue(1);
2129 Count = Op.getOperand(3);
2130 MVT::ValueType CVT = Count.getValueType();
2131 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2132 DAG.getConstant(3, CVT));
2133 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2134 InFlag = Chain.getValue(1);
2135 Tys.clear();
2136 Tys.push_back(MVT::Other);
2137 Tys.push_back(MVT::Flag);
2138 Ops.clear();
2139 Ops.push_back(Chain);
2140 Ops.push_back(DAG.getValueType(MVT::i8));
2141 Ops.push_back(InFlag);
2142 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2143 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002144 // Issue loads and stores for the last 1 - 3 bytes.
2145 unsigned Offset = I->getValue() - BytesLeft;
2146 SDOperand DstAddr = Op.getOperand(1);
2147 MVT::ValueType DstVT = DstAddr.getValueType();
2148 SDOperand SrcAddr = Op.getOperand(2);
2149 MVT::ValueType SrcVT = SrcAddr.getValueType();
2150 SDOperand Value;
2151 if (BytesLeft >= 2) {
2152 Value = DAG.getLoad(MVT::i16, Chain,
2153 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2154 DAG.getConstant(Offset, SrcVT)),
2155 DAG.getSrcValue(NULL));
2156 Chain = Value.getValue(1);
2157 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2158 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2159 DAG.getConstant(Offset, DstVT)),
2160 DAG.getSrcValue(NULL));
2161 BytesLeft -= 2;
2162 Offset += 2;
2163 }
2164
2165 if (BytesLeft == 1) {
2166 Value = DAG.getLoad(MVT::i8, Chain,
2167 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2168 DAG.getConstant(Offset, SrcVT)),
2169 DAG.getSrcValue(NULL));
2170 Chain = Value.getValue(1);
2171 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2172 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2173 DAG.getConstant(Offset, DstVT)),
2174 DAG.getSrcValue(NULL));
2175 }
2176 }
2177
2178 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002179 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002180
2181 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2182 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2183 // one of the above mentioned nodes. It has to be wrapped because otherwise
2184 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2185 // be used to form addressing mode. These wrapped nodes will be selected
2186 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002187 case ISD::ConstantPool: {
2188 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002189 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2190 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2191 CP->getAlignment()));
Evan Chenga88973f2006-03-22 19:22:18 +00002192 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002193 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002194 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002195 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2196 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2197 }
2198
2199 return Result;
2200 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002201 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002202 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2203 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2204 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002205 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002206 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002207 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002208 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2209 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002210
2211 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002212 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002213 // the GlobalAddress must be in the base or index register of the address,
2214 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002215 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002216 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002217 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002218 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002219 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002220
Evan Cheng002fe9b2006-01-12 07:56:47 +00002221 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002222 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002223 case ISD::ExternalSymbol: {
2224 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2225 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2226 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002227 if (Subtarget->isTargetDarwin()) {
Evan Cheng020d2e82006-02-23 20:41:18 +00002228 // With PIC, the address is actually $g + Offset.
2229 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2230 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2231 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2232 }
2233
2234 return Result;
2235 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002236 case ISD::VASTART: {
2237 // vastart just stores the address of the VarArgsFrameIndex slot into the
2238 // memory location argument.
2239 // FIXME: Replace MVT::i32 with PointerTy
2240 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2241 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2242 Op.getOperand(1), Op.getOperand(2));
2243 }
Nate Begemanee625572006-01-27 21:09:22 +00002244 case ISD::RET: {
2245 SDOperand Copy;
2246
2247 switch(Op.getNumOperands()) {
2248 default:
2249 assert(0 && "Do not know how to return this many arguments!");
2250 abort();
2251 case 1:
2252 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2253 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2254 case 2: {
2255 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2256 if (MVT::isInteger(ArgVT))
2257 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2258 SDOperand());
2259 else if (!X86ScalarSSE) {
2260 std::vector<MVT::ValueType> Tys;
2261 Tys.push_back(MVT::Other);
2262 Tys.push_back(MVT::Flag);
2263 std::vector<SDOperand> Ops;
2264 Ops.push_back(Op.getOperand(0));
2265 Ops.push_back(Op.getOperand(1));
2266 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2267 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002268 SDOperand MemLoc;
2269 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002270 SDOperand Value = Op.getOperand(1);
2271
Evan Cheng760df292006-02-01 01:19:32 +00002272 if (Value.getOpcode() == ISD::LOAD &&
2273 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002274 Chain = Value.getOperand(0);
2275 MemLoc = Value.getOperand(1);
2276 } else {
2277 // Spill the value to memory and reload it into top of stack.
2278 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2279 MachineFunction &MF = DAG.getMachineFunction();
2280 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2281 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2282 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2283 Value, MemLoc, DAG.getSrcValue(0));
2284 }
Nate Begemanee625572006-01-27 21:09:22 +00002285 std::vector<MVT::ValueType> Tys;
2286 Tys.push_back(MVT::f64);
2287 Tys.push_back(MVT::Other);
2288 std::vector<SDOperand> Ops;
2289 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002290 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002291 Ops.push_back(DAG.getValueType(ArgVT));
2292 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2293 Tys.clear();
2294 Tys.push_back(MVT::Other);
2295 Tys.push_back(MVT::Flag);
2296 Ops.clear();
2297 Ops.push_back(Copy.getValue(1));
2298 Ops.push_back(Copy);
2299 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2300 }
2301 break;
2302 }
2303 case 3:
2304 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2305 SDOperand());
2306 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2307 break;
2308 }
2309 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2310 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2311 Copy.getValue(1));
2312 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002313 case ISD::SCALAR_TO_VECTOR: {
2314 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengbc4832b2006-03-24 23:15:12 +00002315 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Cheng48090aa2006-03-21 23:01:21 +00002316 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002317 case ISD::VECTOR_SHUFFLE: {
2318 SDOperand V1 = Op.getOperand(0);
2319 SDOperand V2 = Op.getOperand(1);
2320 SDOperand PermMask = Op.getOperand(2);
2321 MVT::ValueType VT = Op.getValueType();
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002322 unsigned NumElems = PermMask.getNumOperands();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002323
Evan Cheng0188ecb2006-03-22 18:59:22 +00002324 // Handle splat cases.
2325 if (X86::isSplatMask(PermMask.Val)) {
2326 if (V2.getOpcode() == ISD::UNDEF)
Evan Chengb9df0ca2006-03-22 02:53:00 +00002327 // Leave the VECTOR_SHUFFLE alone. It matches SHUFP*.
Chris Lattner6df11542006-03-22 04:18:34 +00002328 return SDOperand();
Evan Cheng0188ecb2006-03-22 18:59:22 +00002329 else
2330 // Make it match SHUFP* or UNPCKLPD. Second vector is undef since it's
2331 // not needed.
2332 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2333 DAG.getNode(ISD::UNDEF, V1.getValueType()),
2334 PermMask);
Evan Chengbc4832b2006-03-24 23:15:12 +00002335 } else if (NumElems == 2) {
2336 // All v2f64 cases are handled.
2337 return SDOperand();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002338 } else if (X86::isPSHUFDMask(PermMask.Val)) {
Evan Cheng0188ecb2006-03-22 18:59:22 +00002339 if (V2.getOpcode() == ISD::UNDEF)
Evan Chengb9df0ca2006-03-22 02:53:00 +00002340 // Leave the VECTOR_SHUFFLE alone. It matches PSHUFD.
Chris Lattner6df11542006-03-22 04:18:34 +00002341 return SDOperand();
Evan Cheng0188ecb2006-03-22 18:59:22 +00002342 else
2343 // Make it match PSHUFD. Second vector is undef since it's not needed.
2344 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2345 DAG.getNode(ISD::UNDEF, V1.getValueType()),
2346 PermMask);
Evan Cheng5217a5b2006-03-24 06:40:32 +00002347 } else if (X86::isSHUFPMask(PermMask.Val)) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00002348 SDOperand Elt = PermMask.getOperand(0);
2349 if (cast<ConstantSDNode>(Elt)->getValue() >= NumElems) {
2350 // Swap the operands and change mask.
2351 std::vector<SDOperand> MaskVec;
2352 for (unsigned i = NumElems / 2; i != NumElems; ++i)
2353 MaskVec.push_back(PermMask.getOperand(i));
2354 for (unsigned i = 0; i != NumElems / 2; ++i)
2355 MaskVec.push_back(PermMask.getOperand(i));
2356 PermMask =
2357 DAG.getNode(ISD::BUILD_VECTOR, PermMask.getValueType(), MaskVec);
2358 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, PermMask);
2359 }
2360 return SDOperand();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002361 }
2362
Evan Cheng386031a2006-03-24 07:29:27 +00002363 assert(0 && "Unexpected VECTOR_SHUFFLE to lower");
Chris Lattner6df11542006-03-22 04:18:34 +00002364 abort();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002365 }
Evan Cheng386031a2006-03-24 07:29:27 +00002366 case ISD::BUILD_VECTOR: {
Evan Chengc60bd972006-03-25 09:37:23 +00002367 std::set<SDOperand> Values;
Evan Chengbc4832b2006-03-24 23:15:12 +00002368 SDOperand Elt0 = Op.getOperand(0);
Evan Chengc60bd972006-03-25 09:37:23 +00002369 Values.insert(Elt0);
Evan Chengbc4832b2006-03-24 23:15:12 +00002370 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2371 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2372 (isa<ConstantFPSDNode>(Elt0) &&
2373 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2374 bool RestAreZero = true;
Evan Cheng386031a2006-03-24 07:29:27 +00002375 unsigned NumElems = Op.getNumOperands();
Evan Chengbc4832b2006-03-24 23:15:12 +00002376 for (unsigned i = 1; i < NumElems; ++i) {
Evan Chengc60bd972006-03-25 09:37:23 +00002377 SDOperand Elt = Op.getOperand(i);
2378 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002379 if (!FPC->isExactlyValue(+0.0))
Evan Chengbc4832b2006-03-24 23:15:12 +00002380 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002381 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002382 if (!C->isNullValue())
Evan Chengbc4832b2006-03-24 23:15:12 +00002383 RestAreZero = false;
Evan Cheng386031a2006-03-24 07:29:27 +00002384 } else
Evan Chengbc4832b2006-03-24 23:15:12 +00002385 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002386 Values.insert(Elt);
Evan Cheng386031a2006-03-24 07:29:27 +00002387 }
2388
Evan Chengbc4832b2006-03-24 23:15:12 +00002389 if (RestAreZero) {
2390 if (Elt0IsZero) return Op;
2391
2392 // Zero extend a scalar to a vector.
2393 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2394 }
2395
Evan Chengc60bd972006-03-25 09:37:23 +00002396 if (Values.size() > 2) {
2397 // Expand into a number of unpckl*.
2398 // e.g. for v4f32
2399 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2400 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2401 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2402 MVT::ValueType VT = Op.getValueType();
2403 std::vector<SDOperand> V(NumElems);
2404 for (unsigned i = 0; i < NumElems; ++i)
2405 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2406 NumElems >>= 1;
2407 while (NumElems != 0) {
2408 for (unsigned i = 0; i < NumElems; ++i)
2409 V[i] = DAG.getNode(X86ISD::UNPCKL, VT, V[i], V[i + NumElems]);
2410 NumElems >>= 1;
2411 }
2412 return V[0];
2413 }
2414
Evan Cheng386031a2006-03-24 07:29:27 +00002415 return SDOperand();
2416 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002417 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002418}
Evan Cheng72261582005-12-20 06:22:03 +00002419
2420const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2421 switch (Opcode) {
2422 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002423 case X86ISD::SHLD: return "X86ISD::SHLD";
2424 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002425 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002426 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002427 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002428 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002429 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2430 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2431 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002432 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002433 case X86ISD::FST: return "X86ISD::FST";
2434 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002435 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002436 case X86ISD::CALL: return "X86ISD::CALL";
2437 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2438 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2439 case X86ISD::CMP: return "X86ISD::CMP";
2440 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002441 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002442 case X86ISD::CMOV: return "X86ISD::CMOV";
2443 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002444 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002445 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2446 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002447 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002448 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002449 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00002450 case X86ISD::S2VEC: return "X86ISD::S2VEC";
2451 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Chengc60bd972006-03-25 09:37:23 +00002452 case X86ISD::UNPCKL: return "X86ISD::UNPCKL";
Evan Cheng72261582005-12-20 06:22:03 +00002453 }
2454}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002455
Nate Begeman368e18d2006-02-16 21:11:51 +00002456void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2457 uint64_t Mask,
2458 uint64_t &KnownZero,
2459 uint64_t &KnownOne,
2460 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002461
2462 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002463 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002464
2465 switch (Opc) {
2466 default:
2467 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2468 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002469 case X86ISD::SETCC:
2470 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2471 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002472 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002473}
Chris Lattner259e97c2006-01-31 19:43:35 +00002474
2475std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002476getRegClassForInlineAsmConstraint(const std::string &Constraint,
2477 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002478 if (Constraint.size() == 1) {
2479 // FIXME: not handling fp-stack yet!
2480 // FIXME: not handling MMX registers yet ('y' constraint).
2481 switch (Constraint[0]) { // GCC X86 Constraint Letters
2482 default: break; // Unknown constriant letter
2483 case 'r': // GENERAL_REGS
2484 case 'R': // LEGACY_REGS
2485 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2486 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2487 case 'l': // INDEX_REGS
2488 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2489 X86::ESI, X86::EDI, X86::EBP, 0);
2490 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2491 case 'Q': // Q_REGS
2492 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2493 case 'x': // SSE_REGS if SSE1 allowed
2494 if (Subtarget->hasSSE1())
2495 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2496 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2497 0);
2498 return std::vector<unsigned>();
2499 case 'Y': // SSE_REGS if SSE2 allowed
2500 if (Subtarget->hasSSE2())
2501 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2502 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2503 0);
2504 return std::vector<unsigned>();
2505 }
2506 }
2507
Chris Lattner1efa40f2006-02-22 00:56:39 +00002508 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002509}
Evan Cheng30b37b52006-03-13 23:18:16 +00002510
2511/// isLegalAddressImmediate - Return true if the integer value or
2512/// GlobalValue can be used as the offset of the target addressing mode.
2513bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2514 // X86 allows a sign-extended 32-bit immediate field.
2515 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2516}
2517
2518bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00002519 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00002520 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2521 if (RModel == Reloc::Static)
2522 return true;
2523 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002524 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002525 else
2526 return false;
2527 } else
2528 return true;
2529}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002530
2531/// isShuffleMaskLegal - Targets can use this to indicate that they only
2532/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2533/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2534/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00002535bool
2536X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2537 // Only do shuffles on 128-bit vector types for now.
2538 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002539 return (Mask.Val->getNumOperands() == 2 ||
2540 X86::isSplatMask(Mask.Val) ||
Evan Cheng14aed5e2006-03-24 01:18:28 +00002541 X86::isPSHUFDMask(Mask.Val) ||
2542 X86::isSHUFPMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00002543}