blob: 8fba7547d08294e041288105ddacb536bdbd11b7 [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 Cheng6be2c582006-04-05 23:38:46 +000022#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000023#include "llvm/ADT/VectorExtras.h"
24#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000028#include "llvm/CodeGen/SelectionDAG.h"
29#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000030#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000031#include "llvm/Target/TargetOptions.h"
32using namespace llvm;
33
34// FIXME: temporary.
35#include "llvm/Support/CommandLine.h"
36static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
37 cl::desc("Enable fastcc on X86"));
38
39X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000041 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
43
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000044 // Set up the TargetLowering object.
45
46 // X86 is weird, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
49 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000050 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000051 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000052 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng714554d2006-03-16 21:47:42 +000053
Evan Chenga88973f2006-03-22 19:22:18 +000054 if (!Subtarget->isTargetDarwin())
Evan Chengdf57fa02006-03-17 20:31:41 +000055 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
56 setUseUnderscoreSetJmpLongJmp(true);
57
Evan Cheng714554d2006-03-16 21:47:42 +000058 // Add legal addressing mode scale values.
59 addLegalAddressScale(8);
60 addLegalAddressScale(4);
61 addLegalAddressScale(2);
62 // Enter the ones which require both scale + index last. These are more
63 // expensive.
64 addLegalAddressScale(9);
65 addLegalAddressScale(5);
66 addLegalAddressScale(3);
Chris Lattnera54aa942006-01-29 06:26:08 +000067
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000069 addRegisterClass(MVT::i8, X86::R8RegisterClass);
70 addRegisterClass(MVT::i16, X86::R16RegisterClass);
71 addRegisterClass(MVT::i32, X86::R32RegisterClass);
72
73 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
74 // operation.
75 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
77 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000078
79 if (X86ScalarSSE)
80 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
81 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
82 else
83 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000084
85 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
86 // this operation.
87 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
88 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000089 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000090 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000091 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000092 else {
93 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
94 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
95 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000096
Evan Cheng6dab0532006-01-30 08:02:57 +000097 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
98 // isn't legal.
99 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
100 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
101
Evan Cheng02568ff2006-01-30 22:13:22 +0000102 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
103 // this operation.
104 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
105 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
106
107 if (X86ScalarSSE) {
108 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
109 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000111 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112 }
113
114 // Handle FP_TO_UINT by promoting the destination to a larger signed
115 // conversion.
116 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
118 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
119
Evan Cheng45af8fd2006-02-18 07:26:17 +0000120 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000121 // Expand FP_TO_UINT into a select.
122 // FIXME: We would like to use a Custom expander here eventually to do
123 // the optimal thing for SSE vs. the default expansion in the legalizer.
124 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
125 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000126 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000127 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
128
Evan Cheng02568ff2006-01-30 22:13:22 +0000129 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
130 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000131
Evan Cheng5298bcc2006-02-17 07:01:52 +0000132 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000133 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
134 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000135 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
139 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
140 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
141 setOperationAction(ISD::FREM , MVT::f64 , Expand);
142 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
143 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
146 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
148 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
149 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
150 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000151 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000152 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000153
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154 // These should be promoted to a larger select which is supported.
155 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
156 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000157
158 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000159 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
160 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
162 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
167 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000168 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000169 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000170 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000171 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000172 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000173 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000174 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000175 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000178 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000179 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
180 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181
Chris Lattnerf73bae12005-11-29 06:16:21 +0000182 // We don't have line number support yet.
183 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000184 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000185 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000186 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000187 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000188
Nate Begemanacc398c2006-01-25 18:21:52 +0000189 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
190 setOperationAction(ISD::VASTART , MVT::Other, Custom);
191
192 // Use the default implementation.
193 setOperationAction(ISD::VAARG , MVT::Other, Expand);
194 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
195 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000196 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
197 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
198 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000199
Chris Lattner9601a862006-03-05 05:08:37 +0000200 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
201 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
202
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000203 if (X86ScalarSSE) {
204 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000205 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
206 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207
208 // SSE has no load+extend ops
209 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
210 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
211
Evan Cheng223547a2006-01-31 22:28:30 +0000212 // Use ANDPD to simulate FABS.
213 setOperationAction(ISD::FABS , MVT::f64, Custom);
214 setOperationAction(ISD::FABS , MVT::f32, Custom);
215
216 // Use XORP to simulate FNEG.
217 setOperationAction(ISD::FNEG , MVT::f64, Custom);
218 setOperationAction(ISD::FNEG , MVT::f32, Custom);
219
Evan Chengd25e9e82006-02-02 00:28:23 +0000220 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221 setOperationAction(ISD::FSIN , MVT::f64, Expand);
222 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 setOperationAction(ISD::FREM , MVT::f64, Expand);
224 setOperationAction(ISD::FSIN , MVT::f32, Expand);
225 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 setOperationAction(ISD::FREM , MVT::f32, Expand);
227
Chris Lattnera54aa942006-01-29 06:26:08 +0000228 // Expand FP immediates into loads from the stack, except for the special
229 // cases we handle.
230 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
231 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232 addLegalFPImmediate(+0.0); // xorps / xorpd
233 } else {
234 // Set up the FP register classes.
235 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000236
237 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
238
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000239 if (!UnsafeFPMath) {
240 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
241 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
242 }
243
Chris Lattnera54aa942006-01-29 06:26:08 +0000244 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000245 addLegalFPImmediate(+0.0); // FLD0
246 addLegalFPImmediate(+1.0); // FLD1
247 addLegalFPImmediate(-0.0); // FLD0/FCHS
248 addLegalFPImmediate(-1.0); // FLD1/FCHS
249 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000250
Evan Chengd30bf012006-03-01 01:11:20 +0000251 // First set operation action for all vector types to expand. Then we
252 // will selectively turn on ones that can be effectively codegen'd.
253 for (unsigned VT = (unsigned)MVT::Vector + 1;
254 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
255 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
258 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000259 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000260 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000261 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000262 }
263
Evan Chenga88973f2006-03-22 19:22:18 +0000264 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000265 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
266 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
267 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
268
Evan Chengd30bf012006-03-01 01:11:20 +0000269 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000270 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
271 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
272 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000273 }
274
Evan Chenga88973f2006-03-22 19:22:18 +0000275 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000276 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
277
Evan Cheng2c3ae372006-04-12 21:21:57 +0000278 setOperationAction(ISD::AND, MVT::v4f32, Legal);
279 setOperationAction(ISD::OR, MVT::v4f32, Legal);
280 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000281 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
282 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
283 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
284 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
285 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
286 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000287 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000288 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000289 }
290
Evan Chenga88973f2006-03-22 19:22:18 +0000291 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000292 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
293 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
295 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
296 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
297
Evan Chengf7c378e2006-04-10 07:23:14 +0000298 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
299 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
300 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
301 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
302 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
303 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
304 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
305 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000306 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000307 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000308
Evan Chengf7c378e2006-04-10 07:23:14 +0000309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
310 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000311 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000312 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
313 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
314 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000315
Evan Cheng2c3ae372006-04-12 21:21:57 +0000316 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
317 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
318 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
319 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
321 }
322 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
323 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
324 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
325 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
326 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
327 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
328
329 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
330 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
331 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
332 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
333 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
334 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
335 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
336 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000337 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
338 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000339 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
340 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000341 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000342
343 // Custom lower v2i64 and v2f64 selects.
344 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000345 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000346 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000347 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000348 }
349
Evan Cheng6be2c582006-04-05 23:38:46 +0000350 // We want to custom lower some of our intrinsics.
351 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
352
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000353 computeRegisterProperties();
354
Evan Cheng87ed7162006-02-14 08:25:08 +0000355 // FIXME: These should be based on subtarget info. Plus, the values should
356 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000357 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
358 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
359 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000360 allowUnalignedMemoryAccesses = true; // x86 supports it!
361}
362
363std::vector<SDOperand>
364X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
365 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
366 return LowerFastCCArguments(F, DAG);
367 return LowerCCCArguments(F, DAG);
368}
369
370std::pair<SDOperand, SDOperand>
371X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
372 bool isVarArg, unsigned CallingConv,
373 bool isTailCall,
374 SDOperand Callee, ArgListTy &Args,
375 SelectionDAG &DAG) {
376 assert((!isVarArg || CallingConv == CallingConv::C) &&
377 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000378
379 // If the callee is a GlobalAddress node (quite common, every direct call is)
380 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
381 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
382 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000383 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
384 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000385
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000386 if (CallingConv == CallingConv::Fast && EnableFastCC)
387 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
388 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
389}
390
391//===----------------------------------------------------------------------===//
392// C Calling Convention implementation
393//===----------------------------------------------------------------------===//
394
395std::vector<SDOperand>
396X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
397 std::vector<SDOperand> ArgValues;
398
399 MachineFunction &MF = DAG.getMachineFunction();
400 MachineFrameInfo *MFI = MF.getFrameInfo();
401
402 // Add DAG nodes to load the arguments... On entry to a function on the X86,
403 // the stack frame looks like this:
404 //
405 // [ESP] -- return address
406 // [ESP + 4] -- first argument (leftmost lexically)
407 // [ESP + 8] -- second argument, if first argument is four bytes in size
408 // ...
409 //
410 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
411 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
412 MVT::ValueType ObjectVT = getValueType(I->getType());
413 unsigned ArgIncrement = 4;
414 unsigned ObjSize;
415 switch (ObjectVT) {
416 default: assert(0 && "Unhandled argument type!");
417 case MVT::i1:
418 case MVT::i8: ObjSize = 1; break;
419 case MVT::i16: ObjSize = 2; break;
420 case MVT::i32: ObjSize = 4; break;
421 case MVT::i64: ObjSize = ArgIncrement = 8; break;
422 case MVT::f32: ObjSize = 4; break;
423 case MVT::f64: ObjSize = ArgIncrement = 8; break;
424 }
425 // Create the frame index object for this incoming parameter...
426 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
427
428 // Create the SelectionDAG nodes corresponding to a load from this parameter
429 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
430
431 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
432 // dead loads.
433 SDOperand ArgValue;
434 if (!I->use_empty())
435 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
436 DAG.getSrcValue(NULL));
437 else {
438 if (MVT::isInteger(ObjectVT))
439 ArgValue = DAG.getConstant(0, ObjectVT);
440 else
441 ArgValue = DAG.getConstantFP(0, ObjectVT);
442 }
443 ArgValues.push_back(ArgValue);
444
445 ArgOffset += ArgIncrement; // Move on to the next argument...
446 }
447
448 // If the function takes variable number of arguments, make a frame index for
449 // the start of the first vararg value... for expansion of llvm.va_start.
450 if (F.isVarArg())
451 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
452 ReturnAddrIndex = 0; // No return address slot generated yet.
453 BytesToPopOnReturn = 0; // Callee pops nothing.
454 BytesCallerReserves = ArgOffset;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000455 return ArgValues;
456}
457
458std::pair<SDOperand, SDOperand>
459X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
460 bool isVarArg, bool isTailCall,
461 SDOperand Callee, ArgListTy &Args,
462 SelectionDAG &DAG) {
463 // Count how many bytes are to be pushed on the stack.
464 unsigned NumBytes = 0;
465
466 if (Args.empty()) {
467 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000468 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000469 } else {
470 for (unsigned i = 0, e = Args.size(); i != e; ++i)
471 switch (getValueType(Args[i].second)) {
472 default: assert(0 && "Unknown value type!");
473 case MVT::i1:
474 case MVT::i8:
475 case MVT::i16:
476 case MVT::i32:
477 case MVT::f32:
478 NumBytes += 4;
479 break;
480 case MVT::i64:
481 case MVT::f64:
482 NumBytes += 8;
483 break;
484 }
485
Chris Lattner94dd2922006-02-13 09:00:43 +0000486 Chain = DAG.getCALLSEQ_START(Chain,
487 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000488
489 // Arguments go on the stack in reverse order, as specified by the ABI.
490 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000491 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000492 std::vector<SDOperand> Stores;
493
494 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
495 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
496 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
497
498 switch (getValueType(Args[i].second)) {
499 default: assert(0 && "Unexpected ValueType for argument!");
500 case MVT::i1:
501 case MVT::i8:
502 case MVT::i16:
503 // Promote the integer to 32 bits. If the input type is signed use a
504 // sign extend, otherwise use a zero extend.
505 if (Args[i].second->isSigned())
506 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
507 else
508 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
509
510 // FALL THROUGH
511 case MVT::i32:
512 case MVT::f32:
513 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
514 Args[i].first, PtrOff,
515 DAG.getSrcValue(NULL)));
516 ArgOffset += 4;
517 break;
518 case MVT::i64:
519 case MVT::f64:
520 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
521 Args[i].first, PtrOff,
522 DAG.getSrcValue(NULL)));
523 ArgOffset += 8;
524 break;
525 }
526 }
527 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
528 }
529
530 std::vector<MVT::ValueType> RetVals;
531 MVT::ValueType RetTyVT = getValueType(RetTy);
532 RetVals.push_back(MVT::Other);
533
534 // The result values produced have to be legal. Promote the result.
535 switch (RetTyVT) {
536 case MVT::isVoid: break;
537 default:
538 RetVals.push_back(RetTyVT);
539 break;
540 case MVT::i1:
541 case MVT::i8:
542 case MVT::i16:
543 RetVals.push_back(MVT::i32);
544 break;
545 case MVT::f32:
546 if (X86ScalarSSE)
547 RetVals.push_back(MVT::f32);
548 else
549 RetVals.push_back(MVT::f64);
550 break;
551 case MVT::i64:
552 RetVals.push_back(MVT::i32);
553 RetVals.push_back(MVT::i32);
554 break;
555 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000556
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000557 std::vector<MVT::ValueType> NodeTys;
558 NodeTys.push_back(MVT::Other); // Returns a chain
559 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
560 std::vector<SDOperand> Ops;
561 Ops.push_back(Chain);
562 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000563
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000564 // FIXME: Do not generate X86ISD::TAILCALL for now.
565 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
566 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000567
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000568 NodeTys.clear();
569 NodeTys.push_back(MVT::Other); // Returns a chain
570 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
571 Ops.clear();
572 Ops.push_back(Chain);
573 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
574 Ops.push_back(DAG.getConstant(0, getPointerTy()));
575 Ops.push_back(InFlag);
576 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
577 InFlag = Chain.getValue(1);
578
579 SDOperand RetVal;
580 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000581 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000582 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000583 case MVT::i1:
584 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000585 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
586 Chain = RetVal.getValue(1);
587 if (RetTyVT == MVT::i1)
588 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
589 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000590 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000591 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
592 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000593 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000594 case MVT::i32:
595 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
596 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000597 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000598 case MVT::i64: {
599 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
600 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
601 Lo.getValue(2));
602 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
603 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000604 break;
605 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000606 case MVT::f32:
607 case MVT::f64: {
608 std::vector<MVT::ValueType> Tys;
609 Tys.push_back(MVT::f64);
610 Tys.push_back(MVT::Other);
611 Tys.push_back(MVT::Flag);
612 std::vector<SDOperand> Ops;
613 Ops.push_back(Chain);
614 Ops.push_back(InFlag);
615 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
616 Chain = RetVal.getValue(1);
617 InFlag = RetVal.getValue(2);
618 if (X86ScalarSSE) {
619 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
620 // shouldn't be necessary except that RFP cannot be live across
621 // multiple blocks. When stackifier is fixed, they can be uncoupled.
622 MachineFunction &MF = DAG.getMachineFunction();
623 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
624 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
625 Tys.clear();
626 Tys.push_back(MVT::Other);
627 Ops.clear();
628 Ops.push_back(Chain);
629 Ops.push_back(RetVal);
630 Ops.push_back(StackSlot);
631 Ops.push_back(DAG.getValueType(RetTyVT));
632 Ops.push_back(InFlag);
633 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
634 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
635 DAG.getSrcValue(NULL));
636 Chain = RetVal.getValue(1);
637 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000638
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000639 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
640 // FIXME: we would really like to remember that this FP_ROUND
641 // operation is okay to eliminate if we allow excess FP precision.
642 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
643 break;
644 }
645 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000646 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000647
648 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000649}
650
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000651//===----------------------------------------------------------------------===//
652// Fast Calling Convention implementation
653//===----------------------------------------------------------------------===//
654//
655// The X86 'fast' calling convention passes up to two integer arguments in
656// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
657// and requires that the callee pop its arguments off the stack (allowing proper
658// tail calls), and has the same return value conventions as C calling convs.
659//
660// This calling convention always arranges for the callee pop value to be 8n+4
661// bytes, which is needed for tail recursion elimination and stack alignment
662// reasons.
663//
664// Note that this can be enhanced in the future to pass fp vals in registers
665// (when we have a global fp allocator) and do other tricks.
666//
667
668/// AddLiveIn - This helper function adds the specified physical register to the
669/// MachineFunction as a live in value. It also creates a corresponding virtual
670/// register for it.
671static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
672 TargetRegisterClass *RC) {
673 assert(RC->contains(PReg) && "Not the correct regclass!");
674 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
675 MF.addLiveIn(PReg, VReg);
676 return VReg;
677}
678
Chris Lattner89fad2c2006-03-17 17:27:47 +0000679// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
680// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
681// EDX". Anything more is illegal.
682//
683// FIXME: The linscan register allocator currently has problem with
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000684// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-03-17 17:27:47 +0000685// a physreg with a virtreg, this increases the size of the physreg's live
686// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000687// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-03-17 17:27:47 +0000688// allocator to wedge itself.
689//
690// This code triggers this problem more often if we pass args in registers,
691// so disable it until this is fixed.
692//
693// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
694// about code being dead.
695//
696static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000697
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000698
699std::vector<SDOperand>
700X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
701 std::vector<SDOperand> ArgValues;
702
703 MachineFunction &MF = DAG.getMachineFunction();
704 MachineFrameInfo *MFI = MF.getFrameInfo();
705
706 // Add DAG nodes to load the arguments... On entry to a function the stack
707 // frame looks like this:
708 //
709 // [ESP] -- return address
710 // [ESP + 4] -- first nonreg argument (leftmost lexically)
711 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
712 // ...
713 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
714
715 // Keep track of the number of integer regs passed so far. This can be either
716 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
717 // used).
718 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000719
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000720 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
721 MVT::ValueType ObjectVT = getValueType(I->getType());
722 unsigned ArgIncrement = 4;
723 unsigned ObjSize = 0;
724 SDOperand ArgValue;
725
726 switch (ObjectVT) {
727 default: assert(0 && "Unhandled argument type!");
728 case MVT::i1:
729 case MVT::i8:
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::DL : X86::AL,
733 X86::R8RegisterClass);
734 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
735 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000736 if (ObjectVT == MVT::i1)
737 // FIXME: Should insert a assertzext here.
738 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000739 }
740 ++NumIntRegs;
741 break;
742 }
743
744 ObjSize = 1;
745 break;
746 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000747 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000748 if (!I->use_empty()) {
749 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
750 X86::R16RegisterClass);
751 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
752 DAG.setRoot(ArgValue.getValue(1));
753 }
754 ++NumIntRegs;
755 break;
756 }
757 ObjSize = 2;
758 break;
759 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000760 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000761 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000762 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000763 X86::R32RegisterClass);
764 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
765 DAG.setRoot(ArgValue.getValue(1));
766 }
767 ++NumIntRegs;
768 break;
769 }
770 ObjSize = 4;
771 break;
772 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000773 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000774 if (!I->use_empty()) {
775 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
776 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
777
778 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
779 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
780 DAG.setRoot(Hi.getValue(1));
781
782 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
783 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000784 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000785 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000786 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000787 if (!I->use_empty()) {
788 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
789 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
790 DAG.setRoot(Low.getValue(1));
791
792 // Load the high part from memory.
793 // Create the frame index object for this incoming parameter...
794 int FI = MFI->CreateFixedObject(4, ArgOffset);
795 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
796 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
797 DAG.getSrcValue(NULL));
798 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
799 }
800 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000801 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000802 break;
803 }
804 ObjSize = ArgIncrement = 8;
805 break;
806 case MVT::f32: ObjSize = 4; break;
807 case MVT::f64: ObjSize = ArgIncrement = 8; break;
808 }
809
810 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
811 // dead loads.
812 if (ObjSize && !I->use_empty()) {
813 // Create the frame index object for this incoming parameter...
814 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
815
816 // Create the SelectionDAG nodes corresponding to a load from this
817 // parameter.
818 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
819
820 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
821 DAG.getSrcValue(NULL));
822 } else if (ArgValue.Val == 0) {
823 if (MVT::isInteger(ObjectVT))
824 ArgValue = DAG.getConstant(0, ObjectVT);
825 else
826 ArgValue = DAG.getConstantFP(0, ObjectVT);
827 }
828 ArgValues.push_back(ArgValue);
829
830 if (ObjSize)
831 ArgOffset += ArgIncrement; // Move on to the next argument.
832 }
833
834 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
835 // arguments and the arguments after the retaddr has been pushed are aligned.
836 if ((ArgOffset & 7) == 0)
837 ArgOffset += 4;
838
839 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
840 ReturnAddrIndex = 0; // No return address slot generated yet.
841 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
842 BytesCallerReserves = 0;
843
844 // Finally, inform the code generator which regs we return values in.
845 switch (getValueType(F.getReturnType())) {
846 default: assert(0 && "Unknown type!");
847 case MVT::isVoid: break;
848 case MVT::i1:
849 case MVT::i8:
850 case MVT::i16:
851 case MVT::i32:
852 MF.addLiveOut(X86::EAX);
853 break;
854 case MVT::i64:
855 MF.addLiveOut(X86::EAX);
856 MF.addLiveOut(X86::EDX);
857 break;
858 case MVT::f32:
859 case MVT::f64:
860 MF.addLiveOut(X86::ST0);
861 break;
862 }
863 return ArgValues;
864}
865
866std::pair<SDOperand, SDOperand>
867X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
868 bool isTailCall, SDOperand Callee,
869 ArgListTy &Args, SelectionDAG &DAG) {
870 // Count how many bytes are to be pushed on the stack.
871 unsigned NumBytes = 0;
872
873 // Keep track of the number of integer regs passed so far. This can be either
874 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
875 // used).
876 unsigned NumIntRegs = 0;
877
878 for (unsigned i = 0, e = Args.size(); i != e; ++i)
879 switch (getValueType(Args[i].second)) {
880 default: assert(0 && "Unknown value type!");
881 case MVT::i1:
882 case MVT::i8:
883 case MVT::i16:
884 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000885 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000886 ++NumIntRegs;
887 break;
888 }
889 // fall through
890 case MVT::f32:
891 NumBytes += 4;
892 break;
893 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000894 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
895 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000896 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000897 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
898 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000899 NumBytes += 4;
900 break;
901 }
902
903 // fall through
904 case MVT::f64:
905 NumBytes += 8;
906 break;
907 }
908
909 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
910 // arguments and the arguments after the retaddr has been pushed are aligned.
911 if ((NumBytes & 7) == 0)
912 NumBytes += 4;
913
Chris Lattner94dd2922006-02-13 09:00:43 +0000914 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000915
916 // Arguments go on the stack in reverse order, as specified by the ABI.
917 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000918 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000919 NumIntRegs = 0;
920 std::vector<SDOperand> Stores;
921 std::vector<SDOperand> RegValuesToPass;
922 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
923 switch (getValueType(Args[i].second)) {
924 default: assert(0 && "Unexpected ValueType for argument!");
925 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000926 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
927 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000928 case MVT::i8:
929 case MVT::i16:
930 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000931 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000932 RegValuesToPass.push_back(Args[i].first);
933 ++NumIntRegs;
934 break;
935 }
936 // Fall through
937 case MVT::f32: {
938 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
939 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
940 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
941 Args[i].first, PtrOff,
942 DAG.getSrcValue(NULL)));
943 ArgOffset += 4;
944 break;
945 }
946 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000947 // Can pass (at least) part of it in regs?
948 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000949 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
950 Args[i].first, DAG.getConstant(1, MVT::i32));
951 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
952 Args[i].first, DAG.getConstant(0, MVT::i32));
953 RegValuesToPass.push_back(Lo);
954 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000955
956 // Pass both parts in regs?
957 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000958 RegValuesToPass.push_back(Hi);
959 ++NumIntRegs;
960 } else {
961 // Pass the high part in memory.
962 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
963 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
964 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
965 Hi, PtrOff, DAG.getSrcValue(NULL)));
966 ArgOffset += 4;
967 }
968 break;
969 }
970 // Fall through
971 case MVT::f64:
972 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
973 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
974 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
975 Args[i].first, PtrOff,
976 DAG.getSrcValue(NULL)));
977 ArgOffset += 8;
978 break;
979 }
980 }
981 if (!Stores.empty())
982 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
983
984 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
985 // arguments and the arguments after the retaddr has been pushed are aligned.
986 if ((ArgOffset & 7) == 0)
987 ArgOffset += 4;
988
989 std::vector<MVT::ValueType> RetVals;
990 MVT::ValueType RetTyVT = getValueType(RetTy);
991
992 RetVals.push_back(MVT::Other);
993
994 // The result values produced have to be legal. Promote the result.
995 switch (RetTyVT) {
996 case MVT::isVoid: break;
997 default:
998 RetVals.push_back(RetTyVT);
999 break;
1000 case MVT::i1:
1001 case MVT::i8:
1002 case MVT::i16:
1003 RetVals.push_back(MVT::i32);
1004 break;
1005 case MVT::f32:
1006 if (X86ScalarSSE)
1007 RetVals.push_back(MVT::f32);
1008 else
1009 RetVals.push_back(MVT::f64);
1010 break;
1011 case MVT::i64:
1012 RetVals.push_back(MVT::i32);
1013 RetVals.push_back(MVT::i32);
1014 break;
1015 }
1016
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001017 // Build a sequence of copy-to-reg nodes chained together with token chain
1018 // and flag operands which copy the outgoing args into registers.
1019 SDOperand InFlag;
1020 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1021 unsigned CCReg;
1022 SDOperand RegToPass = RegValuesToPass[i];
1023 switch (RegToPass.getValueType()) {
1024 default: assert(0 && "Bad thing to pass in regs");
1025 case MVT::i8:
1026 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +00001027 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001028 case MVT::i16:
1029 CCReg = (i == 0) ? X86::AX : X86::DX;
1030 break;
1031 case MVT::i32:
1032 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1033 break;
1034 }
1035
1036 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1037 InFlag = Chain.getValue(1);
1038 }
1039
1040 std::vector<MVT::ValueType> NodeTys;
1041 NodeTys.push_back(MVT::Other); // Returns a chain
1042 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1043 std::vector<SDOperand> Ops;
1044 Ops.push_back(Chain);
1045 Ops.push_back(Callee);
1046 if (InFlag.Val)
1047 Ops.push_back(InFlag);
1048
1049 // FIXME: Do not generate X86ISD::TAILCALL for now.
1050 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1051 InFlag = Chain.getValue(1);
1052
1053 NodeTys.clear();
1054 NodeTys.push_back(MVT::Other); // Returns a chain
1055 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1056 Ops.clear();
1057 Ops.push_back(Chain);
1058 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1059 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1060 Ops.push_back(InFlag);
1061 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1062 InFlag = Chain.getValue(1);
1063
1064 SDOperand RetVal;
1065 if (RetTyVT != MVT::isVoid) {
1066 switch (RetTyVT) {
1067 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001068 case MVT::i1:
1069 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001070 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1071 Chain = RetVal.getValue(1);
1072 if (RetTyVT == MVT::i1)
1073 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1074 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001075 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001076 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1077 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001078 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001079 case MVT::i32:
1080 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1081 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001082 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001083 case MVT::i64: {
1084 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1085 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1086 Lo.getValue(2));
1087 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1088 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001089 break;
1090 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001091 case MVT::f32:
1092 case MVT::f64: {
1093 std::vector<MVT::ValueType> Tys;
1094 Tys.push_back(MVT::f64);
1095 Tys.push_back(MVT::Other);
1096 Tys.push_back(MVT::Flag);
1097 std::vector<SDOperand> Ops;
1098 Ops.push_back(Chain);
1099 Ops.push_back(InFlag);
1100 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1101 Chain = RetVal.getValue(1);
1102 InFlag = RetVal.getValue(2);
1103 if (X86ScalarSSE) {
1104 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1105 // shouldn't be necessary except that RFP cannot be live across
1106 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1107 MachineFunction &MF = DAG.getMachineFunction();
1108 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1109 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1110 Tys.clear();
1111 Tys.push_back(MVT::Other);
1112 Ops.clear();
1113 Ops.push_back(Chain);
1114 Ops.push_back(RetVal);
1115 Ops.push_back(StackSlot);
1116 Ops.push_back(DAG.getValueType(RetTyVT));
1117 Ops.push_back(InFlag);
1118 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1119 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1120 DAG.getSrcValue(NULL));
1121 Chain = RetVal.getValue(1);
1122 }
Evan Chengd9558e02006-01-06 00:43:03 +00001123
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001124 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1125 // FIXME: we would really like to remember that this FP_ROUND
1126 // operation is okay to eliminate if we allow excess FP precision.
1127 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1128 break;
1129 }
1130 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001131 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001132
1133 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001134}
1135
1136SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1137 if (ReturnAddrIndex == 0) {
1138 // Set up a frame object for the return address.
1139 MachineFunction &MF = DAG.getMachineFunction();
1140 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1141 }
1142
1143 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1144}
1145
1146
1147
1148std::pair<SDOperand, SDOperand> X86TargetLowering::
1149LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1150 SelectionDAG &DAG) {
1151 SDOperand Result;
1152 if (Depth) // Depths > 0 not supported yet!
1153 Result = DAG.getConstant(0, getPointerTy());
1154 else {
1155 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1156 if (!isFrameAddress)
1157 // Just load the return address
1158 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1159 DAG.getSrcValue(NULL));
1160 else
1161 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1162 DAG.getConstant(4, MVT::i32));
1163 }
1164 return std::make_pair(Result, Chain);
1165}
1166
Evan Cheng4a460802006-01-11 00:33:36 +00001167/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1168/// which corresponds to the condition code.
1169static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1170 switch (X86CC) {
1171 default: assert(0 && "Unknown X86 conditional code!");
1172 case X86ISD::COND_A: return X86::JA;
1173 case X86ISD::COND_AE: return X86::JAE;
1174 case X86ISD::COND_B: return X86::JB;
1175 case X86ISD::COND_BE: return X86::JBE;
1176 case X86ISD::COND_E: return X86::JE;
1177 case X86ISD::COND_G: return X86::JG;
1178 case X86ISD::COND_GE: return X86::JGE;
1179 case X86ISD::COND_L: return X86::JL;
1180 case X86ISD::COND_LE: return X86::JLE;
1181 case X86ISD::COND_NE: return X86::JNE;
1182 case X86ISD::COND_NO: return X86::JNO;
1183 case X86ISD::COND_NP: return X86::JNP;
1184 case X86ISD::COND_NS: return X86::JNS;
1185 case X86ISD::COND_O: return X86::JO;
1186 case X86ISD::COND_P: return X86::JP;
1187 case X86ISD::COND_S: return X86::JS;
1188 }
1189}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001190
Evan Cheng6dfa9992006-01-30 23:41:35 +00001191/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1192/// specific condition code. It returns a false if it cannot do a direct
1193/// translation. X86CC is the translated CondCode. Flip is set to true if the
1194/// the order of comparison operands should be flipped.
Evan Cheng6be2c582006-04-05 23:38:46 +00001195static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1196 unsigned &X86CC, bool &Flip) {
Evan Cheng6dfa9992006-01-30 23:41:35 +00001197 Flip = false;
1198 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001199 if (!isFP) {
1200 switch (SetCCOpcode) {
1201 default: break;
1202 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1203 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1204 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1205 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1206 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1207 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1208 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1209 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1210 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1211 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1212 }
1213 } else {
1214 // On a floating point condition, the flags are set as follows:
1215 // ZF PF CF op
1216 // 0 | 0 | 0 | X > Y
1217 // 0 | 0 | 1 | X < Y
1218 // 1 | 0 | 0 | X == Y
1219 // 1 | 1 | 1 | unordered
1220 switch (SetCCOpcode) {
1221 default: break;
1222 case ISD::SETUEQ:
1223 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001224 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001225 case ISD::SETOGT:
1226 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001227 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001228 case ISD::SETOGE:
1229 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001230 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001231 case ISD::SETULT:
1232 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001233 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001234 case ISD::SETULE:
1235 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1236 case ISD::SETONE:
1237 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1238 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1239 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1240 }
1241 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001242
1243 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001244}
1245
Evan Cheng6be2c582006-04-05 23:38:46 +00001246static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1247 bool &Flip) {
1248 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1249}
1250
Evan Cheng4a460802006-01-11 00:33:36 +00001251/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1252/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001253/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001254static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001255 switch (X86CC) {
1256 default:
1257 return false;
1258 case X86ISD::COND_B:
1259 case X86ISD::COND_BE:
1260 case X86ISD::COND_E:
1261 case X86ISD::COND_P:
1262 case X86ISD::COND_A:
1263 case X86ISD::COND_AE:
1264 case X86ISD::COND_NE:
1265 case X86ISD::COND_NP:
1266 return true;
1267 }
1268}
1269
Evan Cheng4a460802006-01-11 00:33:36 +00001270MachineBasicBlock *
1271X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1272 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001273 switch (MI->getOpcode()) {
1274 default: assert(false && "Unexpected instr type to insert");
1275 case X86::CMOV_FR32:
Evan Chengf7c378e2006-04-10 07:23:14 +00001276 case X86::CMOV_FR64:
1277 case X86::CMOV_V4F32:
1278 case X86::CMOV_V2F64:
1279 case X86::CMOV_V2I64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001280 // To "insert" a SELECT_CC instruction, we actually have to insert the
1281 // diamond control-flow pattern. The incoming instruction knows the
1282 // destination vreg to set, the condition code register to branch on, the
1283 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001284 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1285 ilist<MachineBasicBlock>::iterator It = BB;
1286 ++It;
1287
1288 // thisMBB:
1289 // ...
1290 // TrueVal = ...
1291 // cmpTY ccX, r1, r2
1292 // bCC copy1MBB
1293 // fallthrough --> copy0MBB
1294 MachineBasicBlock *thisMBB = BB;
1295 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1296 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1297 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1298 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1299 MachineFunction *F = BB->getParent();
1300 F->getBasicBlockList().insert(It, copy0MBB);
1301 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001302 // Update machine-CFG edges by first adding all successors of the current
1303 // block to the new block which will contain the Phi node for the select.
1304 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1305 e = BB->succ_end(); i != e; ++i)
1306 sinkMBB->addSuccessor(*i);
1307 // Next, remove all successors of the current block, and add the true
1308 // and fallthrough blocks as its successors.
1309 while(!BB->succ_empty())
1310 BB->removeSuccessor(BB->succ_begin());
Evan Cheng0cc39452006-01-16 21:21:29 +00001311 BB->addSuccessor(copy0MBB);
1312 BB->addSuccessor(sinkMBB);
1313
1314 // copy0MBB:
1315 // %FalseValue = ...
1316 // # fallthrough to sinkMBB
1317 BB = copy0MBB;
1318
1319 // Update machine-CFG edges
1320 BB->addSuccessor(sinkMBB);
1321
1322 // sinkMBB:
1323 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1324 // ...
1325 BB = sinkMBB;
1326 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1327 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1328 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001329
Evan Cheng0cc39452006-01-16 21:21:29 +00001330 delete MI; // The pseudo instruction is gone now.
1331 return BB;
1332 }
Evan Cheng4a460802006-01-11 00:33:36 +00001333
Evan Cheng0cc39452006-01-16 21:21:29 +00001334 case X86::FP_TO_INT16_IN_MEM:
1335 case X86::FP_TO_INT32_IN_MEM:
1336 case X86::FP_TO_INT64_IN_MEM: {
1337 // Change the floating point control register to use "round towards zero"
1338 // mode when truncating to an integer value.
1339 MachineFunction *F = BB->getParent();
1340 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1341 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1342
1343 // Load the old value of the high byte of the control word...
1344 unsigned OldCW =
1345 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1346 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1347
1348 // Set the high part to be round to zero...
1349 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1350
1351 // Reload the modified control word now...
1352 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1353
1354 // Restore the memory image of control word to original value
1355 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1356
1357 // Get the X86 opcode to use.
1358 unsigned Opc;
1359 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001360 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001361 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1362 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1363 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1364 }
1365
1366 X86AddressMode AM;
1367 MachineOperand &Op = MI->getOperand(0);
1368 if (Op.isRegister()) {
1369 AM.BaseType = X86AddressMode::RegBase;
1370 AM.Base.Reg = Op.getReg();
1371 } else {
1372 AM.BaseType = X86AddressMode::FrameIndexBase;
1373 AM.Base.FrameIndex = Op.getFrameIndex();
1374 }
1375 Op = MI->getOperand(1);
1376 if (Op.isImmediate())
1377 AM.Scale = Op.getImmedValue();
1378 Op = MI->getOperand(2);
1379 if (Op.isImmediate())
1380 AM.IndexReg = Op.getImmedValue();
1381 Op = MI->getOperand(3);
1382 if (Op.isGlobalAddress()) {
1383 AM.GV = Op.getGlobal();
1384 } else {
1385 AM.Disp = Op.getImmedValue();
1386 }
1387 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1388
1389 // Reload the original control word now.
1390 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1391
1392 delete MI; // The pseudo instruction is gone now.
1393 return BB;
1394 }
1395 }
Evan Cheng4a460802006-01-11 00:33:36 +00001396}
1397
1398
1399//===----------------------------------------------------------------------===//
1400// X86 Custom Lowering Hooks
1401//===----------------------------------------------------------------------===//
1402
Evan Cheng30b37b52006-03-13 23:18:16 +00001403/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1404/// load. For Darwin, external and weak symbols are indirect, loading the value
1405/// at address GV rather then the value of GV itself. This means that the
1406/// GlobalAddress must be in the base or index register of the address, not the
1407/// GV offset field.
1408static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1409 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1410 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1411}
1412
Evan Cheng5ced1d82006-04-06 23:23:56 +00001413/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001414/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001415static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1416 if (Op.getOpcode() == ISD::UNDEF)
1417 return true;
1418
1419 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001420 return (Val >= Low && Val < Hi);
1421}
1422
1423/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1424/// true if Op is undef or if its value equal to the specified value.
1425static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1426 if (Op.getOpcode() == ISD::UNDEF)
1427 return true;
1428 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001429}
1430
Evan Cheng0188ecb2006-03-22 18:59:22 +00001431/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1432/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1433bool X86::isPSHUFDMask(SDNode *N) {
1434 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1435
1436 if (N->getNumOperands() != 4)
1437 return false;
1438
1439 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001440 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001441 SDOperand Arg = N->getOperand(i);
1442 if (Arg.getOpcode() == ISD::UNDEF) continue;
1443 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1444 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00001445 return false;
1446 }
1447
1448 return true;
1449}
1450
1451/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001452/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001453bool X86::isPSHUFHWMask(SDNode *N) {
1454 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1455
1456 if (N->getNumOperands() != 8)
1457 return false;
1458
1459 // Lower quadword copied in order.
1460 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001461 SDOperand Arg = N->getOperand(i);
1462 if (Arg.getOpcode() == ISD::UNDEF) continue;
1463 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1464 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001465 return false;
1466 }
1467
1468 // Upper quadword shuffled.
1469 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001470 SDOperand Arg = N->getOperand(i);
1471 if (Arg.getOpcode() == ISD::UNDEF) continue;
1472 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1473 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001474 if (Val < 4 || Val > 7)
1475 return false;
1476 }
1477
1478 return true;
1479}
1480
1481/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001482/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001483bool X86::isPSHUFLWMask(SDNode *N) {
1484 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1485
1486 if (N->getNumOperands() != 8)
1487 return false;
1488
1489 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001490 for (unsigned i = 4; i != 8; ++i)
1491 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001492 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001493
1494 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001495 for (unsigned i = 0; i != 4; ++i)
1496 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001497 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001498
1499 return true;
1500}
1501
Evan Cheng14aed5e2006-03-24 01:18:28 +00001502/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1503/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1504bool X86::isSHUFPMask(SDNode *N) {
1505 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1506
Evan Chengbc4832b2006-03-24 23:15:12 +00001507 unsigned NumElems = N->getNumOperands();
1508 if (NumElems == 2) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00001509 // The only cases that ought be handled by SHUFPD is
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001510 // Dest { 2, 1 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00001511 // Dest { 3, 0 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001512 // Expect bit 0 == 1, bit1 == 2
1513 SDOperand Bit0 = N->getOperand(0);
1514 SDOperand Bit1 = N->getOperand(1);
Evan Chengc5cdff22006-04-07 21:53:05 +00001515 if (isUndefOrEqual(Bit0, 0) && isUndefOrEqual(Bit1, 3))
Evan Cheng5ced1d82006-04-06 23:23:56 +00001516 return true;
Evan Chengc5cdff22006-04-07 21:53:05 +00001517 if (isUndefOrEqual(Bit0, 1) && isUndefOrEqual(Bit1, 2))
Evan Cheng5ced1d82006-04-06 23:23:56 +00001518 return true;
1519 return false;
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001520 }
1521
Evan Chengbc4832b2006-03-24 23:15:12 +00001522 if (NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001523
1524 // Each half must refer to only one of the vector.
Evan Cheng7d9061e2006-03-30 19:54:57 +00001525 for (unsigned i = 0; i < 2; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001526 SDOperand Arg = N->getOperand(i);
1527 if (Arg.getOpcode() == ISD::UNDEF) continue;
1528 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1529 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7d9061e2006-03-30 19:54:57 +00001530 if (Val >= 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001531 }
Evan Cheng7d9061e2006-03-30 19:54:57 +00001532 for (unsigned i = 2; i < 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001533 SDOperand Arg = N->getOperand(i);
1534 if (Arg.getOpcode() == ISD::UNDEF) continue;
1535 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1536 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7d9061e2006-03-30 19:54:57 +00001537 if (Val < 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001538 }
1539
1540 return true;
1541}
1542
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001543/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1544/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1545bool X86::isMOVHLPSMask(SDNode *N) {
1546 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1547
Evan Cheng2064a2b2006-03-28 06:50:32 +00001548 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001549 return false;
1550
Evan Cheng2064a2b2006-03-28 06:50:32 +00001551 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001552 return isUndefOrEqual(N->getOperand(0), 6) &&
1553 isUndefOrEqual(N->getOperand(1), 7) &&
1554 isUndefOrEqual(N->getOperand(2), 2) &&
1555 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001556}
1557
1558/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
1559/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1560bool X86::isMOVLHPSMask(SDNode *N) {
1561 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1562
1563 if (N->getNumOperands() != 4)
1564 return false;
1565
1566 // Expect bit0 == 0, bit1 == 1, bit2 == 4, bit3 == 5
Evan Chengc5cdff22006-04-07 21:53:05 +00001567 return isUndefOrEqual(N->getOperand(0), 0) &&
1568 isUndefOrEqual(N->getOperand(1), 1) &&
1569 isUndefOrEqual(N->getOperand(2), 4) &&
1570 isUndefOrEqual(N->getOperand(3), 5);
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001571}
1572
Evan Cheng5ced1d82006-04-06 23:23:56 +00001573/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1574/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1575bool X86::isMOVLPMask(SDNode *N) {
1576 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1577
1578 unsigned NumElems = N->getNumOperands();
1579 if (NumElems != 2 && NumElems != 4)
1580 return false;
1581
Evan Chengc5cdff22006-04-07 21:53:05 +00001582 for (unsigned i = 0; i < NumElems/2; ++i)
1583 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1584 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001585
Evan Chengc5cdff22006-04-07 21:53:05 +00001586 for (unsigned i = NumElems/2; i < NumElems; ++i)
1587 if (!isUndefOrEqual(N->getOperand(i), i))
1588 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001589
1590 return true;
1591}
1592
1593/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
1594/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}.
1595bool X86::isMOVHPMask(SDNode *N) {
1596 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1597
1598 unsigned NumElems = N->getNumOperands();
1599 if (NumElems != 2 && NumElems != 4)
1600 return false;
1601
Evan Chengc5cdff22006-04-07 21:53:05 +00001602 for (unsigned i = 0; i < NumElems/2; ++i)
1603 if (!isUndefOrEqual(N->getOperand(i), i))
1604 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001605
1606 for (unsigned i = 0; i < NumElems/2; ++i) {
1607 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001608 if (!isUndefOrEqual(Arg, i + NumElems))
1609 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001610 }
1611
1612 return true;
1613}
1614
Evan Cheng0038e592006-03-28 00:39:58 +00001615/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1616/// specifies a shuffle of elements that is suitable for input to UNPCKL.
1617bool X86::isUNPCKLMask(SDNode *N) {
1618 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1619
1620 unsigned NumElems = N->getNumOperands();
1621 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1622 return false;
1623
1624 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1625 SDOperand BitI = N->getOperand(i);
1626 SDOperand BitI1 = N->getOperand(i+1);
Evan Chengc5cdff22006-04-07 21:53:05 +00001627 if (!isUndefOrEqual(BitI, j))
1628 return false;
1629 if (!isUndefOrEqual(BitI1, j + NumElems))
1630 return false;
Evan Cheng0038e592006-03-28 00:39:58 +00001631 }
1632
1633 return true;
1634}
1635
Evan Cheng4fcb9222006-03-28 02:43:26 +00001636/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1637/// specifies a shuffle of elements that is suitable for input to UNPCKH.
1638bool X86::isUNPCKHMask(SDNode *N) {
1639 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1640
1641 unsigned NumElems = N->getNumOperands();
1642 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1643 return false;
1644
1645 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1646 SDOperand BitI = N->getOperand(i);
1647 SDOperand BitI1 = N->getOperand(i+1);
Evan Chengc5cdff22006-04-07 21:53:05 +00001648 if (!isUndefOrEqual(BitI, j + NumElems/2))
1649 return false;
1650 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1651 return false;
Evan Cheng4fcb9222006-03-28 02:43:26 +00001652 }
1653
1654 return true;
1655}
1656
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001657/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1658/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1659/// <0, 0, 1, 1>
1660bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1661 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1662
1663 unsigned NumElems = N->getNumOperands();
1664 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1665 return false;
1666
1667 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1668 SDOperand BitI = N->getOperand(i);
1669 SDOperand BitI1 = N->getOperand(i+1);
1670
Evan Chengc5cdff22006-04-07 21:53:05 +00001671 if (!isUndefOrEqual(BitI, j))
1672 return false;
1673 if (!isUndefOrEqual(BitI1, j))
1674 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001675 }
1676
1677 return true;
1678}
1679
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001680/// isMOVSMask - Return true if the specified VECTOR_SHUFFLE operand
1681/// specifies a shuffle of elements that is suitable for input to MOVS{S|D}.
1682bool X86::isMOVSMask(SDNode *N) {
1683 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1684
1685 unsigned NumElems = N->getNumOperands();
1686 if (NumElems != 2 && NumElems != 4)
1687 return false;
1688
1689 if (!isUndefOrEqual(N->getOperand(0), NumElems))
1690 return false;
1691
1692 for (unsigned i = 1; i < NumElems; ++i) {
1693 SDOperand Arg = N->getOperand(i);
1694 if (!isUndefOrEqual(Arg, i))
1695 return false;
1696 }
1697
1698 return true;
1699}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001700
Evan Chengd9539472006-04-14 21:59:03 +00001701/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1702/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1703bool X86::isMOVSHDUPMask(SDNode *N) {
1704 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1705
1706 if (N->getNumOperands() != 4)
1707 return false;
1708
1709 // Expect 1, 1, 3, 3
1710 for (unsigned i = 0; i < 2; ++i) {
1711 SDOperand Arg = N->getOperand(i);
1712 if (Arg.getOpcode() == ISD::UNDEF) continue;
1713 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1714 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1715 if (Val != 1) return false;
1716 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001717
1718 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001719 for (unsigned i = 2; i < 4; ++i) {
1720 SDOperand Arg = N->getOperand(i);
1721 if (Arg.getOpcode() == ISD::UNDEF) continue;
1722 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1723 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1724 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001725 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001726 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001727
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001728 // Don't use movshdup if it can be done with a shufps.
1729 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001730}
1731
1732/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1733/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1734bool X86::isMOVSLDUPMask(SDNode *N) {
1735 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1736
1737 if (N->getNumOperands() != 4)
1738 return false;
1739
1740 // Expect 0, 0, 2, 2
1741 for (unsigned i = 0; i < 2; ++i) {
1742 SDOperand Arg = N->getOperand(i);
1743 if (Arg.getOpcode() == ISD::UNDEF) continue;
1744 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1745 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1746 if (Val != 0) return false;
1747 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001748
1749 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001750 for (unsigned i = 2; i < 4; ++i) {
1751 SDOperand Arg = N->getOperand(i);
1752 if (Arg.getOpcode() == ISD::UNDEF) continue;
1753 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1754 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1755 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001756 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001757 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001758
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001759 // Don't use movshdup if it can be done with a shufps.
1760 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001761}
1762
Evan Chengb9df0ca2006-03-22 02:53:00 +00001763/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1764/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00001765static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001766 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1767
Evan Chengb9df0ca2006-03-22 02:53:00 +00001768 // This is a splat operation if each element of the permute is the same, and
1769 // if the value doesn't reference the second vector.
1770 SDOperand Elt = N->getOperand(0);
1771 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1772 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001773 SDOperand Arg = N->getOperand(i);
1774 if (Arg.getOpcode() == ISD::UNDEF) continue;
1775 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1776 if (Arg != Elt) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001777 }
1778
1779 // Make sure it is a splat of the first vector operand.
1780 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1781}
1782
Evan Chengc575ca22006-04-17 20:43:08 +00001783/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1784/// a splat of a single element and it's a 2 or 4 element mask.
1785bool X86::isSplatMask(SDNode *N) {
1786 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1787
1788 // We can only splat 64-bit, and 32-bit quantities.
1789 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1790 return false;
1791 return ::isSplatMask(N);
1792}
1793
Evan Cheng63d33002006-03-22 08:01:21 +00001794/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1795/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1796/// instructions.
1797unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001798 unsigned NumOperands = N->getNumOperands();
1799 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1800 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001801 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001802 unsigned Val = 0;
1803 SDOperand Arg = N->getOperand(NumOperands-i-1);
1804 if (Arg.getOpcode() != ISD::UNDEF)
1805 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00001806 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001807 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00001808 if (i != NumOperands - 1)
1809 Mask <<= Shift;
1810 }
Evan Cheng63d33002006-03-22 08:01:21 +00001811
1812 return Mask;
1813}
1814
Evan Cheng506d3df2006-03-29 23:07:14 +00001815/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1816/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1817/// instructions.
1818unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1819 unsigned Mask = 0;
1820 // 8 nodes, but we only care about the last 4.
1821 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001822 unsigned Val = 0;
1823 SDOperand Arg = N->getOperand(i);
1824 if (Arg.getOpcode() != ISD::UNDEF)
1825 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001826 Mask |= (Val - 4);
1827 if (i != 4)
1828 Mask <<= 2;
1829 }
1830
1831 return Mask;
1832}
1833
1834/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1835/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1836/// instructions.
1837unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1838 unsigned Mask = 0;
1839 // 8 nodes, but we only care about the first 4.
1840 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001841 unsigned Val = 0;
1842 SDOperand Arg = N->getOperand(i);
1843 if (Arg.getOpcode() != ISD::UNDEF)
1844 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001845 Mask |= Val;
1846 if (i != 0)
1847 Mask <<= 2;
1848 }
1849
1850 return Mask;
1851}
1852
Evan Chengc21a0532006-04-05 01:47:37 +00001853/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1854/// specifies a 8 element shuffle that can be broken into a pair of
1855/// PSHUFHW and PSHUFLW.
1856static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1857 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1858
1859 if (N->getNumOperands() != 8)
1860 return false;
1861
1862 // Lower quadword shuffled.
1863 for (unsigned i = 0; i != 4; ++i) {
1864 SDOperand Arg = N->getOperand(i);
1865 if (Arg.getOpcode() == ISD::UNDEF) continue;
1866 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1867 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1868 if (Val > 4)
1869 return false;
1870 }
1871
1872 // Upper quadword shuffled.
1873 for (unsigned i = 4; i != 8; ++i) {
1874 SDOperand Arg = N->getOperand(i);
1875 if (Arg.getOpcode() == ISD::UNDEF) continue;
1876 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1877 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1878 if (Val < 4 || Val > 7)
1879 return false;
1880 }
1881
1882 return true;
1883}
1884
Evan Cheng5ced1d82006-04-06 23:23:56 +00001885/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
1886/// values in ther permute mask.
1887static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1888 SDOperand V1 = Op.getOperand(0);
1889 SDOperand V2 = Op.getOperand(1);
1890 SDOperand Mask = Op.getOperand(2);
1891 MVT::ValueType VT = Op.getValueType();
1892 MVT::ValueType MaskVT = Mask.getValueType();
1893 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
1894 unsigned NumElems = Mask.getNumOperands();
1895 std::vector<SDOperand> MaskVec;
1896
1897 for (unsigned i = 0; i != NumElems; ++i) {
1898 SDOperand Arg = Mask.getOperand(i);
1899 if (Arg.getOpcode() == ISD::UNDEF) continue;
1900 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1901 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1902 if (Val < NumElems)
1903 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
1904 else
1905 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
1906 }
1907
1908 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
1909 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1910}
1911
1912/// isScalarLoadToVector - Returns true if the node is a scalar load that
1913/// is promoted to a vector.
1914static inline bool isScalarLoadToVector(SDOperand Op) {
1915 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR) {
1916 Op = Op.getOperand(0);
1917 return (Op.getOpcode() == ISD::LOAD);
1918 }
1919 return false;
1920}
1921
1922/// ShouldXformedToMOVLP - Return true if the node should be transformed to
1923/// match movlp{d|s}. The lower half elements should come from V1 (and in
1924/// order), and the upper half elements should come from the upper half of
1925/// V2 (not necessarily in order). And since V1 will become the source of
1926/// the MOVLP, it must be a scalar load.
1927static bool ShouldXformedToMOVLP(SDOperand V1, SDOperand V2, SDOperand Mask) {
1928 if (isScalarLoadToVector(V1)) {
1929 unsigned NumElems = Mask.getNumOperands();
1930 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Evan Chengc5cdff22006-04-07 21:53:05 +00001931 if (!isUndefOrEqual(Mask.getOperand(i), i))
Evan Cheng5ced1d82006-04-06 23:23:56 +00001932 return false;
1933 for (unsigned i = NumElems/2; i != NumElems; ++i)
1934 if (!isUndefOrInRange(Mask.getOperand(i),
Evan Chengc5cdff22006-04-07 21:53:05 +00001935 NumElems+NumElems/2, NumElems*2))
Evan Cheng5ced1d82006-04-06 23:23:56 +00001936 return false;
1937 return true;
1938 }
1939
1940 return false;
1941}
1942
1943/// isLowerFromV2UpperFromV1 - Returns true if the shuffle mask is except
1944/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1945/// half elements to come from vector 1 (which would equal the dest.) and
1946/// the upper half to come from vector 2.
1947static bool isLowerFromV2UpperFromV1(SDOperand Op) {
1948 assert(Op.getOpcode() == ISD::BUILD_VECTOR);
1949
1950 unsigned NumElems = Op.getNumOperands();
1951 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Evan Chengc5cdff22006-04-07 21:53:05 +00001952 if (!isUndefOrInRange(Op.getOperand(i), NumElems, NumElems*2))
Evan Cheng5ced1d82006-04-06 23:23:56 +00001953 return false;
1954 for (unsigned i = NumElems/2; i != NumElems; ++i)
Evan Chengc5cdff22006-04-07 21:53:05 +00001955 if (!isUndefOrInRange(Op.getOperand(i), 0, NumElems))
Evan Cheng5ced1d82006-04-06 23:23:56 +00001956 return false;
1957 return true;
1958}
1959
Evan Chengc575ca22006-04-17 20:43:08 +00001960/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
1961/// of specified width.
1962static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
1963 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
1964 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
1965 std::vector<SDOperand> MaskVec;
1966 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
1967 MaskVec.push_back(DAG.getConstant(i, BaseVT));
1968 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
1969 }
1970 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
1971}
1972
1973/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
1974///
1975static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
1976 SDOperand V1 = Op.getOperand(0);
1977 SDOperand PermMask = Op.getOperand(2);
1978 MVT::ValueType VT = Op.getValueType();
1979 unsigned NumElems = PermMask.getNumOperands();
1980 PermMask = getUnpacklMask(NumElems, DAG);
1981 while (NumElems != 4) {
1982 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, PermMask);
1983 NumElems >>= 1;
1984 }
1985 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
1986
1987 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
1988 SDOperand Zero = DAG.getConstant(0, MVT::getVectorBaseType(MaskVT));
1989 std::vector<SDOperand> ZeroVec(4, Zero);
1990 SDOperand SplatMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, ZeroVec);
1991 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
1992 DAG.getNode(ISD::UNDEF, MVT::v4i32),
1993 SplatMask);
1994 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
1995}
1996
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001997/// LowerOperation - Provide custom lowering hooks for some operations.
1998///
1999SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
2000 switch (Op.getOpcode()) {
2001 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00002002 case ISD::SHL_PARTS:
2003 case ISD::SRA_PARTS:
2004 case ISD::SRL_PARTS: {
2005 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2006 "Not an i64 shift!");
2007 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2008 SDOperand ShOpLo = Op.getOperand(0);
2009 SDOperand ShOpHi = Op.getOperand(1);
2010 SDOperand ShAmt = Op.getOperand(2);
2011 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00002012 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00002013 : DAG.getConstant(0, MVT::i32);
2014
2015 SDOperand Tmp2, Tmp3;
2016 if (Op.getOpcode() == ISD::SHL_PARTS) {
2017 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2018 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2019 } else {
2020 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00002021 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00002022 }
2023
2024 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2025 ShAmt, DAG.getConstant(32, MVT::i8));
2026
2027 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00002028 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00002029
2030 std::vector<MVT::ValueType> Tys;
2031 Tys.push_back(MVT::i32);
2032 Tys.push_back(MVT::Flag);
2033 std::vector<SDOperand> Ops;
2034 if (Op.getOpcode() == ISD::SHL_PARTS) {
2035 Ops.push_back(Tmp2);
2036 Ops.push_back(Tmp3);
2037 Ops.push_back(CC);
2038 Ops.push_back(InFlag);
2039 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2040 InFlag = Hi.getValue(1);
2041
2042 Ops.clear();
2043 Ops.push_back(Tmp3);
2044 Ops.push_back(Tmp1);
2045 Ops.push_back(CC);
2046 Ops.push_back(InFlag);
2047 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2048 } else {
2049 Ops.push_back(Tmp2);
2050 Ops.push_back(Tmp3);
2051 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00002052 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00002053 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2054 InFlag = Lo.getValue(1);
2055
2056 Ops.clear();
2057 Ops.push_back(Tmp3);
2058 Ops.push_back(Tmp1);
2059 Ops.push_back(CC);
2060 Ops.push_back(InFlag);
2061 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2062 }
2063
2064 Tys.clear();
2065 Tys.push_back(MVT::i32);
2066 Tys.push_back(MVT::i32);
2067 Ops.clear();
2068 Ops.push_back(Lo);
2069 Ops.push_back(Hi);
2070 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
2071 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002072 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00002073 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00002074 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002075 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00002076
2077 SDOperand Result;
2078 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2079 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002080 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00002081 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002082 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00002083 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2084 DAG.getEntryNode(), Op.getOperand(0),
2085 StackSlot, DAG.getSrcValue(NULL));
2086
2087 // Build the FILD
2088 std::vector<MVT::ValueType> Tys;
2089 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00002090 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00002091 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002092 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00002093 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002094 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00002095 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00002096 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
2097 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00002098
2099 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00002100 Chain = Result.getValue(1);
2101 SDOperand InFlag = Result.getValue(2);
2102
Evan Chenge3de85b2006-02-04 02:20:30 +00002103 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00002104 // shouldn't be necessary except that RFP cannot be live across
2105 // multiple blocks. When stackifier is fixed, they can be uncoupled.
2106 MachineFunction &MF = DAG.getMachineFunction();
2107 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
2108 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2109 std::vector<MVT::ValueType> Tys;
2110 Tys.push_back(MVT::Other);
2111 std::vector<SDOperand> Ops;
2112 Ops.push_back(Chain);
2113 Ops.push_back(Result);
2114 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00002115 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00002116 Ops.push_back(InFlag);
2117 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
2118 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
2119 DAG.getSrcValue(NULL));
2120 }
2121
Evan Chenga3195e82006-01-12 22:54:21 +00002122 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002123 }
2124 case ISD::FP_TO_SINT: {
2125 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002126 "Unknown FP_TO_SINT to lower!");
2127 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
2128 // stack slot.
2129 MachineFunction &MF = DAG.getMachineFunction();
2130 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
2131 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2132 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2133
2134 unsigned Opc;
2135 switch (Op.getValueType()) {
2136 default: assert(0 && "Invalid FP_TO_SINT to lower!");
2137 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
2138 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
2139 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
2140 }
2141
Evan Cheng6dab0532006-01-30 08:02:57 +00002142 SDOperand Chain = DAG.getEntryNode();
2143 SDOperand Value = Op.getOperand(0);
2144 if (X86ScalarSSE) {
2145 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
2146 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
2147 DAG.getSrcValue(0));
2148 std::vector<MVT::ValueType> Tys;
2149 Tys.push_back(MVT::f64);
2150 Tys.push_back(MVT::Other);
2151 std::vector<SDOperand> Ops;
2152 Ops.push_back(Chain);
2153 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00002154 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00002155 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2156 Chain = Value.getValue(1);
2157 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2158 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2159 }
2160
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002161 // Build the FP_TO_INT*_IN_MEM
2162 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00002163 Ops.push_back(Chain);
2164 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002165 Ops.push_back(StackSlot);
2166 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
2167
2168 // Load the result.
2169 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
2170 DAG.getSrcValue(NULL));
2171 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00002172 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00002173 std::vector<MVT::ValueType> Tys;
2174 Tys.push_back(MVT::Other);
2175 Tys.push_back(MVT::Flag);
2176 std::vector<SDOperand> Ops;
2177 Ops.push_back(Op.getOperand(0));
2178 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00002179 Ops.clear();
2180 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
2181 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
2182 MVT::i32, Ops[0].getValue(2)));
2183 Ops.push_back(Ops[1].getValue(1));
2184 Tys[0] = Tys[1] = MVT::i32;
2185 Tys.push_back(MVT::Other);
2186 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00002187 }
Evan Chengef6ffb12006-01-31 03:14:29 +00002188 case ISD::FABS: {
2189 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00002190 const Type *OpNTy = MVT::getTypeForValueType(VT);
2191 std::vector<Constant*> CV;
2192 if (VT == MVT::f64) {
2193 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2194 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2195 } else {
2196 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2197 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2198 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2199 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2200 }
2201 Constant *CS = ConstantStruct::get(CV);
2202 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2203 SDOperand Mask
2204 = DAG.getNode(X86ISD::LOAD_PACK,
2205 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00002206 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
2207 }
Evan Cheng223547a2006-01-31 22:28:30 +00002208 case ISD::FNEG: {
2209 MVT::ValueType VT = Op.getValueType();
2210 const Type *OpNTy = MVT::getTypeForValueType(VT);
2211 std::vector<Constant*> CV;
2212 if (VT == MVT::f64) {
2213 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2214 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2215 } else {
2216 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
2217 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2218 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2219 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2220 }
2221 Constant *CS = ConstantStruct::get(CV);
2222 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2223 SDOperand Mask
2224 = DAG.getNode(X86ISD::LOAD_PACK,
2225 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2226 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
2227 }
Evan Chengd5781fc2005-12-21 20:21:51 +00002228 case ISD::SETCC: {
2229 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00002230 SDOperand Cond;
2231 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00002232 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2233 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00002234 bool Flip;
2235 unsigned X86CC;
2236 if (translateX86CC(CC, isFP, X86CC, Flip)) {
2237 if (Flip)
2238 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2239 Op.getOperand(1), Op.getOperand(0));
2240 else
2241 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2242 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00002243 return DAG.getNode(X86ISD::SETCC, MVT::i8,
2244 DAG.getConstant(X86CC, MVT::i8), Cond);
2245 } else {
2246 assert(isFP && "Illegal integer SetCC!");
2247
Evan Cheng6dfa9992006-01-30 23:41:35 +00002248 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2249 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00002250 std::vector<MVT::ValueType> Tys;
2251 std::vector<SDOperand> Ops;
2252 switch (SetCCOpcode) {
2253 default: assert(false && "Illegal floating point SetCC!");
2254 case ISD::SETOEQ: { // !PF & ZF
2255 Tys.push_back(MVT::i8);
2256 Tys.push_back(MVT::Flag);
2257 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
2258 Ops.push_back(Cond);
2259 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2260 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2261 DAG.getConstant(X86ISD::COND_E, MVT::i8),
2262 Tmp1.getValue(1));
2263 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
2264 }
Evan Chengd9558e02006-01-06 00:43:03 +00002265 case ISD::SETUNE: { // PF | !ZF
2266 Tys.push_back(MVT::i8);
2267 Tys.push_back(MVT::Flag);
2268 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
2269 Ops.push_back(Cond);
2270 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2271 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2272 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
2273 Tmp1.getValue(1));
2274 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
2275 }
2276 }
2277 }
Evan Chengd5781fc2005-12-21 20:21:51 +00002278 }
Evan Cheng7df96d62005-12-17 01:21:05 +00002279 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00002280 MVT::ValueType VT = Op.getValueType();
Evan Chengf7c378e2006-04-10 07:23:14 +00002281 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00002282 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00002283 SDOperand Op0 = Op.getOperand(0);
2284 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00002285 if (Op0.getOpcode() == ISD::SETCC)
2286 Op0 = LowerOperation(Op0, DAG);
2287
Evan Chengaaca22c2006-01-10 20:26:56 +00002288 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00002289 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2290 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2291 // have another use it will be eliminated.
2292 // If the X86ISD::SETCC has more than one use, then it's probably better
2293 // to use a test instead of duplicating the X86ISD::CMP (for register
2294 // pressure reason).
Evan Cheng6be2c582006-04-05 23:38:46 +00002295 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
2296 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
2297 CmpOpc == X86ISD::UCOMI) {
Evan Cheng9bba8942006-01-26 02:13:10 +00002298 if (!Op0.hasOneUse()) {
2299 std::vector<MVT::ValueType> Tys;
2300 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2301 Tys.push_back(Op0.Val->getValueType(i));
2302 std::vector<SDOperand> Ops;
2303 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2304 Ops.push_back(Op0.getOperand(i));
2305 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2306 }
2307
Evan Cheng1bcee362006-01-13 01:03:02 +00002308 CC = Op0.getOperand(0);
2309 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00002310 // Make a copy as flag result cannot be used by more than one.
Evan Cheng6be2c582006-04-05 23:38:46 +00002311 Cond = DAG.getNode(CmpOpc, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00002312 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00002313 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00002314 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00002315 } else
2316 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00002317 } else
2318 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00002319
Evan Cheng189d01e2006-01-13 01:06:49 +00002320 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00002321 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00002322 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00002323 }
Evan Chenge3413162006-01-09 18:33:28 +00002324
2325 std::vector<MVT::ValueType> Tys;
2326 Tys.push_back(Op.getValueType());
2327 Tys.push_back(MVT::Flag);
2328 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00002329 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2330 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00002331 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00002332 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00002333 Ops.push_back(CC);
2334 Ops.push_back(Cond);
2335 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00002336 }
Evan Cheng898101c2005-12-19 23:12:38 +00002337 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00002338 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00002339 SDOperand Cond = Op.getOperand(1);
2340 SDOperand Dest = Op.getOperand(2);
2341 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00002342 if (Cond.getOpcode() == ISD::SETCC)
2343 Cond = LowerOperation(Cond, DAG);
2344
Evan Chengd5781fc2005-12-21 20:21:51 +00002345 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00002346 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2347 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2348 // have another use it will be eliminated.
2349 // If the X86ISD::SETCC has more than one use, then it's probably better
2350 // to use a test instead of duplicating the X86ISD::CMP (for register
2351 // pressure reason).
Evan Cheng6be2c582006-04-05 23:38:46 +00002352 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
2353 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
2354 CmpOpc == X86ISD::UCOMI) {
Evan Cheng9bba8942006-01-26 02:13:10 +00002355 if (!Cond.hasOneUse()) {
2356 std::vector<MVT::ValueType> Tys;
2357 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2358 Tys.push_back(Cond.Val->getValueType(i));
2359 std::vector<SDOperand> Ops;
2360 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2361 Ops.push_back(Cond.getOperand(i));
2362 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2363 }
2364
Evan Cheng1bcee362006-01-13 01:03:02 +00002365 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00002366 Cond = Cond.getOperand(1);
2367 // Make a copy as flag result cannot be used by more than one.
Evan Cheng6be2c582006-04-05 23:38:46 +00002368 Cond = DAG.getNode(CmpOpc, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00002369 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00002370 } else
2371 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00002372 } else
2373 addTest = true;
2374
2375 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00002376 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00002377 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2378 }
2379 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2380 Op.getOperand(0), Op.getOperand(2), CC, Cond);
2381 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002382 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002383 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002384 SDOperand Chain = Op.getOperand(0);
2385 unsigned Align =
2386 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2387 if (Align == 0) Align = 1;
2388
Evan Cheng18a84522006-02-16 00:21:07 +00002389 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2390 // If not DWORD aligned, call memset if size is less than the threshold.
2391 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002392 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002393 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002394 MVT::ValueType IntPtr = getPointerTy();
2395 const Type *IntPtrTy = getTargetData().getIntPtrType();
2396 std::vector<std::pair<SDOperand, const Type*> > Args;
2397 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2398 // Extend the ubyte argument to be an int value for the call.
2399 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2400 Args.push_back(std::make_pair(Val, IntPtrTy));
2401 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2402 std::pair<SDOperand,SDOperand> CallResult =
2403 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2404 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2405 return CallResult.second;
2406 }
2407
Evan Cheng67f92a72006-01-11 22:15:48 +00002408 MVT::ValueType AVT;
2409 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002410 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2411 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002412 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002413 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00002414 unsigned ValReg;
2415 unsigned Val = ValC->getValue() & 255;
2416
2417 // If the value is a constant, then we can potentially use larger sets.
2418 switch (Align & 3) {
2419 case 2: // WORD aligned
2420 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002421 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2422 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002423 Val = (Val << 8) | Val;
2424 ValReg = X86::AX;
2425 break;
2426 case 0: // DWORD aligned
2427 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002428 if (I) {
2429 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2430 BytesLeft = I->getValue() % 4;
2431 } else {
2432 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2433 DAG.getConstant(2, MVT::i8));
2434 TwoRepStos = true;
2435 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002436 Val = (Val << 8) | Val;
2437 Val = (Val << 16) | Val;
2438 ValReg = X86::EAX;
2439 break;
2440 default: // Byte aligned
2441 AVT = MVT::i8;
2442 Count = Op.getOperand(3);
2443 ValReg = X86::AL;
2444 break;
2445 }
2446
2447 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2448 InFlag);
2449 InFlag = Chain.getValue(1);
2450 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00002451 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00002452 Count = Op.getOperand(3);
2453 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2454 InFlag = Chain.getValue(1);
2455 }
2456
2457 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2458 InFlag = Chain.getValue(1);
2459 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2460 InFlag = Chain.getValue(1);
2461
Evan Chengff909922006-03-07 23:29:39 +00002462 std::vector<MVT::ValueType> Tys;
2463 Tys.push_back(MVT::Other);
2464 Tys.push_back(MVT::Flag);
2465 std::vector<SDOperand> Ops;
2466 Ops.push_back(Chain);
2467 Ops.push_back(DAG.getValueType(AVT));
2468 Ops.push_back(InFlag);
2469 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2470
2471 if (TwoRepStos) {
2472 InFlag = Chain.getValue(1);
2473 Count = Op.getOperand(3);
2474 MVT::ValueType CVT = Count.getValueType();
2475 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2476 DAG.getConstant(3, CVT));
2477 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2478 InFlag = Chain.getValue(1);
2479 Tys.clear();
2480 Tys.push_back(MVT::Other);
2481 Tys.push_back(MVT::Flag);
2482 Ops.clear();
2483 Ops.push_back(Chain);
2484 Ops.push_back(DAG.getValueType(MVT::i8));
2485 Ops.push_back(InFlag);
2486 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2487 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002488 // Issue stores for the last 1 - 3 bytes.
2489 SDOperand Value;
2490 unsigned Val = ValC->getValue() & 255;
2491 unsigned Offset = I->getValue() - BytesLeft;
2492 SDOperand DstAddr = Op.getOperand(1);
2493 MVT::ValueType AddrVT = DstAddr.getValueType();
2494 if (BytesLeft >= 2) {
2495 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2496 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2497 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2498 DAG.getConstant(Offset, AddrVT)),
2499 DAG.getSrcValue(NULL));
2500 BytesLeft -= 2;
2501 Offset += 2;
2502 }
2503
2504 if (BytesLeft == 1) {
2505 Value = DAG.getConstant(Val, MVT::i8);
2506 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2507 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2508 DAG.getConstant(Offset, AddrVT)),
2509 DAG.getSrcValue(NULL));
2510 }
2511 }
2512
2513 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002514 }
2515 case ISD::MEMCPY: {
2516 SDOperand Chain = Op.getOperand(0);
2517 unsigned Align =
2518 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2519 if (Align == 0) Align = 1;
2520
Evan Cheng18a84522006-02-16 00:21:07 +00002521 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2522 // If not DWORD aligned, call memcpy if size is less than the threshold.
2523 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002524 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002525 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002526 MVT::ValueType IntPtr = getPointerTy();
2527 const Type *IntPtrTy = getTargetData().getIntPtrType();
2528 std::vector<std::pair<SDOperand, const Type*> > Args;
2529 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2530 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2531 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2532 std::pair<SDOperand,SDOperand> CallResult =
2533 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2534 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2535 return CallResult.second;
2536 }
2537
Evan Cheng67f92a72006-01-11 22:15:48 +00002538 MVT::ValueType AVT;
2539 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002540 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002541 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00002542 switch (Align & 3) {
2543 case 2: // WORD aligned
2544 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002545 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2546 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002547 break;
2548 case 0: // DWORD aligned
2549 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002550 if (I) {
2551 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2552 BytesLeft = I->getValue() % 4;
2553 } else {
2554 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2555 DAG.getConstant(2, MVT::i8));
2556 TwoRepMovs = true;
2557 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002558 break;
2559 default: // Byte aligned
2560 AVT = MVT::i8;
2561 Count = Op.getOperand(3);
2562 break;
2563 }
2564
Evan Cheng62bec2c2006-03-04 02:48:56 +00002565 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002566 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2567 InFlag = Chain.getValue(1);
2568 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2569 InFlag = Chain.getValue(1);
2570 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2571 InFlag = Chain.getValue(1);
2572
Evan Chengff909922006-03-07 23:29:39 +00002573 std::vector<MVT::ValueType> Tys;
2574 Tys.push_back(MVT::Other);
2575 Tys.push_back(MVT::Flag);
2576 std::vector<SDOperand> Ops;
2577 Ops.push_back(Chain);
2578 Ops.push_back(DAG.getValueType(AVT));
2579 Ops.push_back(InFlag);
2580 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2581
2582 if (TwoRepMovs) {
2583 InFlag = Chain.getValue(1);
2584 Count = Op.getOperand(3);
2585 MVT::ValueType CVT = Count.getValueType();
2586 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2587 DAG.getConstant(3, CVT));
2588 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2589 InFlag = Chain.getValue(1);
2590 Tys.clear();
2591 Tys.push_back(MVT::Other);
2592 Tys.push_back(MVT::Flag);
2593 Ops.clear();
2594 Ops.push_back(Chain);
2595 Ops.push_back(DAG.getValueType(MVT::i8));
2596 Ops.push_back(InFlag);
2597 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2598 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002599 // Issue loads and stores for the last 1 - 3 bytes.
2600 unsigned Offset = I->getValue() - BytesLeft;
2601 SDOperand DstAddr = Op.getOperand(1);
2602 MVT::ValueType DstVT = DstAddr.getValueType();
2603 SDOperand SrcAddr = Op.getOperand(2);
2604 MVT::ValueType SrcVT = SrcAddr.getValueType();
2605 SDOperand Value;
2606 if (BytesLeft >= 2) {
2607 Value = DAG.getLoad(MVT::i16, Chain,
2608 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2609 DAG.getConstant(Offset, SrcVT)),
2610 DAG.getSrcValue(NULL));
2611 Chain = Value.getValue(1);
2612 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2613 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2614 DAG.getConstant(Offset, DstVT)),
2615 DAG.getSrcValue(NULL));
2616 BytesLeft -= 2;
2617 Offset += 2;
2618 }
2619
2620 if (BytesLeft == 1) {
2621 Value = DAG.getLoad(MVT::i8, Chain,
2622 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2623 DAG.getConstant(Offset, SrcVT)),
2624 DAG.getSrcValue(NULL));
2625 Chain = Value.getValue(1);
2626 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2627 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2628 DAG.getConstant(Offset, DstVT)),
2629 DAG.getSrcValue(NULL));
2630 }
2631 }
2632
2633 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002634 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002635
2636 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2637 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2638 // one of the above mentioned nodes. It has to be wrapped because otherwise
2639 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2640 // be used to form addressing mode. These wrapped nodes will be selected
2641 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002642 case ISD::ConstantPool: {
2643 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002644 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2645 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2646 CP->getAlignment()));
Evan Chenga88973f2006-03-22 19:22:18 +00002647 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002648 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002649 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002650 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2651 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2652 }
2653
2654 return Result;
2655 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002656 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002657 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2658 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2659 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002660 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002661 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002662 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002663 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2664 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002665
2666 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002667 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002668 // the GlobalAddress must be in the base or index register of the address,
2669 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002670 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002671 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002672 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002673 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002674 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002675
Evan Cheng002fe9b2006-01-12 07:56:47 +00002676 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002677 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002678 case ISD::ExternalSymbol: {
2679 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2680 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2681 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002682 if (Subtarget->isTargetDarwin()) {
Evan Cheng020d2e82006-02-23 20:41:18 +00002683 // With PIC, the address is actually $g + Offset.
2684 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2685 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2686 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2687 }
2688
2689 return Result;
2690 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002691 case ISD::VASTART: {
2692 // vastart just stores the address of the VarArgsFrameIndex slot into the
2693 // memory location argument.
2694 // FIXME: Replace MVT::i32 with PointerTy
2695 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2696 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2697 Op.getOperand(1), Op.getOperand(2));
2698 }
Nate Begemanee625572006-01-27 21:09:22 +00002699 case ISD::RET: {
2700 SDOperand Copy;
2701
2702 switch(Op.getNumOperands()) {
2703 default:
2704 assert(0 && "Do not know how to return this many arguments!");
2705 abort();
Chris Lattnerb2be4032006-04-17 20:32:50 +00002706 case 1: // ret void.
Nate Begemanee625572006-01-27 21:09:22 +00002707 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2708 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2709 case 2: {
2710 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerb2be4032006-04-17 20:32:50 +00002711
2712 if (MVT::isVector(ArgVT)) {
2713 // Integer or FP vector result -> XMM0.
2714 if (DAG.getMachineFunction().liveout_empty())
2715 DAG.getMachineFunction().addLiveOut(X86::XMM0);
2716 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
2717 SDOperand());
2718 } else if (MVT::isInteger(ArgVT)) {
2719 // Integer result -> EAX
2720 if (DAG.getMachineFunction().liveout_empty())
2721 DAG.getMachineFunction().addLiveOut(X86::EAX);
2722
Nate Begemanee625572006-01-27 21:09:22 +00002723 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2724 SDOperand());
Chris Lattnerb2be4032006-04-17 20:32:50 +00002725 } else if (!X86ScalarSSE) {
2726 // FP return with fp-stack value.
2727 if (DAG.getMachineFunction().liveout_empty())
2728 DAG.getMachineFunction().addLiveOut(X86::ST0);
2729
Nate Begemanee625572006-01-27 21:09:22 +00002730 std::vector<MVT::ValueType> Tys;
2731 Tys.push_back(MVT::Other);
2732 Tys.push_back(MVT::Flag);
2733 std::vector<SDOperand> Ops;
2734 Ops.push_back(Op.getOperand(0));
2735 Ops.push_back(Op.getOperand(1));
2736 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2737 } else {
Chris Lattnerb2be4032006-04-17 20:32:50 +00002738 // FP return with ScalarSSE (return on fp-stack).
2739 if (DAG.getMachineFunction().liveout_empty())
2740 DAG.getMachineFunction().addLiveOut(X86::ST0);
2741
Evan Cheng0d084c92006-02-01 00:20:21 +00002742 SDOperand MemLoc;
2743 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002744 SDOperand Value = Op.getOperand(1);
2745
Evan Cheng760df292006-02-01 01:19:32 +00002746 if (Value.getOpcode() == ISD::LOAD &&
2747 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002748 Chain = Value.getOperand(0);
2749 MemLoc = Value.getOperand(1);
2750 } else {
2751 // Spill the value to memory and reload it into top of stack.
2752 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2753 MachineFunction &MF = DAG.getMachineFunction();
2754 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2755 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2756 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2757 Value, MemLoc, DAG.getSrcValue(0));
2758 }
Nate Begemanee625572006-01-27 21:09:22 +00002759 std::vector<MVT::ValueType> Tys;
2760 Tys.push_back(MVT::f64);
2761 Tys.push_back(MVT::Other);
2762 std::vector<SDOperand> Ops;
2763 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002764 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002765 Ops.push_back(DAG.getValueType(ArgVT));
2766 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2767 Tys.clear();
2768 Tys.push_back(MVT::Other);
2769 Tys.push_back(MVT::Flag);
2770 Ops.clear();
2771 Ops.push_back(Copy.getValue(1));
2772 Ops.push_back(Copy);
2773 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2774 }
2775 break;
2776 }
2777 case 3:
Chris Lattnerb2be4032006-04-17 20:32:50 +00002778 if (DAG.getMachineFunction().liveout_empty()) {
2779 DAG.getMachineFunction().addLiveOut(X86::EAX);
2780 DAG.getMachineFunction().addLiveOut(X86::EDX);
2781 }
2782
Nate Begemanee625572006-01-27 21:09:22 +00002783 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2784 SDOperand());
2785 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2786 break;
2787 }
2788 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2789 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2790 Copy.getValue(1));
2791 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002792 case ISD::SCALAR_TO_VECTOR: {
2793 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengbc4832b2006-03-24 23:15:12 +00002794 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Cheng48090aa2006-03-21 23:01:21 +00002795 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002796 case ISD::VECTOR_SHUFFLE: {
2797 SDOperand V1 = Op.getOperand(0);
2798 SDOperand V2 = Op.getOperand(1);
2799 SDOperand PermMask = Op.getOperand(2);
2800 MVT::ValueType VT = Op.getValueType();
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002801 unsigned NumElems = PermMask.getNumOperands();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002802
Evan Chengc575ca22006-04-17 20:43:08 +00002803 if (isSplatMask(PermMask.Val)) {
2804 if (NumElems <= 4) return Op;
2805 // Promote it to a v4i32 splat.
2806 return PromoteSplat(Op, DAG);
2807 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002808
2809 // Normalize the node to match x86 shuffle ops if needed
2810 if (V2.getOpcode() != ISD::UNDEF) {
2811 bool DoSwap = false;
2812
2813 if (ShouldXformedToMOVLP(V1, V2, PermMask))
2814 DoSwap = true;
2815 else if (isLowerFromV2UpperFromV1(PermMask))
2816 DoSwap = true;
2817
2818 if (DoSwap) {
2819 Op = CommuteVectorShuffle(Op, DAG);
2820 V1 = Op.getOperand(0);
2821 V2 = Op.getOperand(1);
2822 PermMask = Op.getOperand(2);
2823 }
Evan Cheng475aecf2006-03-29 03:04:49 +00002824 }
Evan Cheng4f563382006-03-29 01:30:51 +00002825
Evan Cheng5ced1d82006-04-06 23:23:56 +00002826 if (NumElems == 2)
2827 return Op;
2828
Evan Chengd9539472006-04-14 21:59:03 +00002829 if (X86::isMOVSMask(PermMask.Val) ||
2830 X86::isMOVSHDUPMask(PermMask.Val) ||
2831 X86::isMOVSLDUPMask(PermMask.Val))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002832 return Op;
2833
Evan Cheng691c9232006-03-29 19:02:40 +00002834 if (X86::isUNPCKLMask(PermMask.Val) ||
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002835 X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Evan Cheng691c9232006-03-29 19:02:40 +00002836 X86::isUNPCKHMask(PermMask.Val))
2837 // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
Evan Cheng278158b2006-04-05 06:09:26 +00002838 return Op;
Evan Cheng691c9232006-03-29 19:02:40 +00002839
Evan Cheng7d9061e2006-03-30 19:54:57 +00002840 // If VT is integer, try PSHUF* first, then SHUFP*.
2841 if (MVT::isInteger(VT)) {
2842 if (X86::isPSHUFDMask(PermMask.Val) ||
2843 X86::isPSHUFHWMask(PermMask.Val) ||
2844 X86::isPSHUFLWMask(PermMask.Val)) {
2845 if (V2.getOpcode() != ISD::UNDEF)
2846 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2847 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng278158b2006-04-05 06:09:26 +00002848 return Op;
Evan Cheng7d9061e2006-03-30 19:54:57 +00002849 }
2850
2851 if (X86::isSHUFPMask(PermMask.Val))
Evan Cheng5ced1d82006-04-06 23:23:56 +00002852 return Op;
Evan Chengc21a0532006-04-05 01:47:37 +00002853
2854 // Handle v8i16 shuffle high / low shuffle node pair.
2855 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2856 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2857 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2858 std::vector<SDOperand> MaskVec;
2859 for (unsigned i = 0; i != 4; ++i)
2860 MaskVec.push_back(PermMask.getOperand(i));
2861 for (unsigned i = 4; i != 8; ++i)
2862 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2863 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2864 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2865 MaskVec.clear();
2866 for (unsigned i = 0; i != 4; ++i)
2867 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2868 for (unsigned i = 4; i != 8; ++i)
2869 MaskVec.push_back(PermMask.getOperand(i));
2870 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2871 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2872 }
Evan Cheng7d9061e2006-03-30 19:54:57 +00002873 } else {
2874 // Floating point cases in the other order.
2875 if (X86::isSHUFPMask(PermMask.Val))
Evan Cheng5ced1d82006-04-06 23:23:56 +00002876 return Op;
Evan Cheng7d9061e2006-03-30 19:54:57 +00002877 if (X86::isPSHUFDMask(PermMask.Val) ||
2878 X86::isPSHUFHWMask(PermMask.Val) ||
2879 X86::isPSHUFLWMask(PermMask.Val)) {
2880 if (V2.getOpcode() != ISD::UNDEF)
2881 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2882 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng278158b2006-04-05 06:09:26 +00002883 return Op;
Evan Cheng7d9061e2006-03-30 19:54:57 +00002884 }
Evan Cheng4f563382006-03-29 01:30:51 +00002885 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002886
Evan Cheng278158b2006-04-05 06:09:26 +00002887 return SDOperand();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002888 }
Evan Cheng386031a2006-03-24 07:29:27 +00002889 case ISD::BUILD_VECTOR: {
Evan Chenga0b3afb2006-03-27 07:00:16 +00002890 // All one's are handled with pcmpeqd.
2891 if (ISD::isBuildVectorAllOnes(Op.Val))
2892 return Op;
2893
Evan Chengc60bd972006-03-25 09:37:23 +00002894 std::set<SDOperand> Values;
Evan Chengbc4832b2006-03-24 23:15:12 +00002895 SDOperand Elt0 = Op.getOperand(0);
Evan Chengc60bd972006-03-25 09:37:23 +00002896 Values.insert(Elt0);
Evan Chengbc4832b2006-03-24 23:15:12 +00002897 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2898 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2899 (isa<ConstantFPSDNode>(Elt0) &&
2900 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2901 bool RestAreZero = true;
Evan Cheng386031a2006-03-24 07:29:27 +00002902 unsigned NumElems = Op.getNumOperands();
Evan Chengbc4832b2006-03-24 23:15:12 +00002903 for (unsigned i = 1; i < NumElems; ++i) {
Evan Chengc60bd972006-03-25 09:37:23 +00002904 SDOperand Elt = Op.getOperand(i);
2905 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002906 if (!FPC->isExactlyValue(+0.0))
Evan Chengbc4832b2006-03-24 23:15:12 +00002907 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002908 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002909 if (!C->isNullValue())
Evan Chengbc4832b2006-03-24 23:15:12 +00002910 RestAreZero = false;
Evan Cheng386031a2006-03-24 07:29:27 +00002911 } else
Evan Chengbc4832b2006-03-24 23:15:12 +00002912 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002913 Values.insert(Elt);
Evan Cheng386031a2006-03-24 07:29:27 +00002914 }
2915
Evan Chengbc4832b2006-03-24 23:15:12 +00002916 if (RestAreZero) {
2917 if (Elt0IsZero) return Op;
2918
2919 // Zero extend a scalar to a vector.
2920 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2921 }
2922
Evan Chengc60bd972006-03-25 09:37:23 +00002923 if (Values.size() > 2) {
2924 // Expand into a number of unpckl*.
2925 // e.g. for v4f32
2926 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2927 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2928 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2929 MVT::ValueType VT = Op.getValueType();
Evan Chengc575ca22006-04-17 20:43:08 +00002930 SDOperand PermMask = getUnpacklMask(NumElems, DAG);
Evan Chengc60bd972006-03-25 09:37:23 +00002931 std::vector<SDOperand> V(NumElems);
2932 for (unsigned i = 0; i < NumElems; ++i)
2933 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2934 NumElems >>= 1;
2935 while (NumElems != 0) {
2936 for (unsigned i = 0; i < NumElems; ++i)
Evan Cheng0038e592006-03-28 00:39:58 +00002937 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2938 PermMask);
Evan Chengc60bd972006-03-25 09:37:23 +00002939 NumElems >>= 1;
2940 }
2941 return V[0];
2942 }
2943
Evan Cheng386031a2006-03-24 07:29:27 +00002944 return SDOperand();
2945 }
Evan Chengb067a1e2006-03-31 19:22:53 +00002946 case ISD::EXTRACT_VECTOR_ELT: {
Evan Cheng11e15b32006-04-03 20:53:28 +00002947 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2948 return SDOperand();
2949
Evan Chengb067a1e2006-03-31 19:22:53 +00002950 MVT::ValueType VT = Op.getValueType();
Evan Cheng2c3ae372006-04-12 21:21:57 +00002951 // TODO: handle v16i8.
Evan Chengb067a1e2006-03-31 19:22:53 +00002952 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng11e15b32006-04-03 20:53:28 +00002953 // Transform it so it match pextrw which produces a 32-bit result.
Evan Chengb067a1e2006-03-31 19:22:53 +00002954 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2955 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2956 Op.getOperand(0), Op.getOperand(1));
2957 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2958 DAG.getValueType(VT));
2959 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng11e15b32006-04-03 20:53:28 +00002960 } else if (MVT::getSizeInBits(VT) == 32) {
2961 SDOperand Vec = Op.getOperand(0);
2962 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2963 if (Idx == 0)
2964 return Op;
2965
2966 // TODO: if Idex == 2, we can use unpckhps
2967 // SHUFPS the element to the lowest double word, then movss.
2968 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2969 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2970 MVT::getVectorBaseType(MaskVT));
2971 std::vector<SDOperand> IdxVec;
2972 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2973 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2974 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2975 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2976 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2977 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2978 Vec, Vec, Mask);
2979 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2980 DAG.getConstant(0, MVT::i32));
2981 } else if (MVT::getSizeInBits(VT) == 64) {
2982 SDOperand Vec = Op.getOperand(0);
2983 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2984 if (Idx == 0)
2985 return Op;
2986
2987 // UNPCKHPD the element to the lowest double word, then movsd.
Evan Cheng20e3ed12006-04-03 22:30:54 +00002988 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2989 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Evan Cheng11e15b32006-04-03 20:53:28 +00002990 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2991 std::vector<SDOperand> IdxVec;
2992 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2993 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2994 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2995 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2996 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2997 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2998 DAG.getConstant(0, MVT::i32));
Evan Chengb067a1e2006-03-31 19:22:53 +00002999 }
3000
3001 return SDOperand();
3002 }
3003 case ISD::INSERT_VECTOR_ELT: {
3004 // Transform it so it match pinsrw which expects a 16-bit value in a R32
3005 // as its second argument.
3006 MVT::ValueType VT = Op.getValueType();
3007 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
Evan Cheng5edb8d22006-04-17 22:04:06 +00003008 SDOperand N0 = Op.getOperand(0);
3009 SDOperand N1 = Op.getOperand(1);
3010 SDOperand N2 = Op.getOperand(2);
Evan Chengb067a1e2006-03-31 19:22:53 +00003011 if (MVT::getSizeInBits(BaseVT) == 16) {
Evan Chengb067a1e2006-03-31 19:22:53 +00003012 if (N1.getValueType() != MVT::i32)
3013 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3014 if (N2.getValueType() != MVT::i32)
3015 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
Evan Cheng5edb8d22006-04-17 22:04:06 +00003016 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3017 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3018 // Use two pinsrw instructions to insert a 32 bit value.
3019 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3020 Idx <<= 1;
3021 if (MVT::isFloatingPoint(N1.getValueType())) {
3022 if (N1.getOpcode() == ISD::LOAD) {
3023 // Just load directly from f32mem to R32.
3024 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
3025 N1.getOperand(2));
3026 } else {
3027 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3028 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3029 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3030 DAG.getConstant(0, MVT::i32));
3031 }
3032 }
3033 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3034 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3035 DAG.getConstant(Idx, MVT::i32));
3036 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3037 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
3038 DAG.getConstant(Idx+1, MVT::i32));
3039 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
Evan Chengb067a1e2006-03-31 19:22:53 +00003040 }
3041
3042 return SDOperand();
3043 }
Evan Cheng6be2c582006-04-05 23:38:46 +00003044 case ISD::INTRINSIC_WO_CHAIN: {
3045 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3046 switch (IntNo) {
3047 default: return SDOperand(); // Don't custom lower most intrinsics.
3048 // Comparison intrinsics.
3049 case Intrinsic::x86_sse_comieq_ss:
3050 case Intrinsic::x86_sse_comilt_ss:
3051 case Intrinsic::x86_sse_comile_ss:
3052 case Intrinsic::x86_sse_comigt_ss:
3053 case Intrinsic::x86_sse_comige_ss:
3054 case Intrinsic::x86_sse_comineq_ss:
3055 case Intrinsic::x86_sse_ucomieq_ss:
3056 case Intrinsic::x86_sse_ucomilt_ss:
3057 case Intrinsic::x86_sse_ucomile_ss:
3058 case Intrinsic::x86_sse_ucomigt_ss:
3059 case Intrinsic::x86_sse_ucomige_ss:
3060 case Intrinsic::x86_sse_ucomineq_ss:
3061 case Intrinsic::x86_sse2_comieq_sd:
3062 case Intrinsic::x86_sse2_comilt_sd:
3063 case Intrinsic::x86_sse2_comile_sd:
3064 case Intrinsic::x86_sse2_comigt_sd:
3065 case Intrinsic::x86_sse2_comige_sd:
3066 case Intrinsic::x86_sse2_comineq_sd:
3067 case Intrinsic::x86_sse2_ucomieq_sd:
3068 case Intrinsic::x86_sse2_ucomilt_sd:
3069 case Intrinsic::x86_sse2_ucomile_sd:
3070 case Intrinsic::x86_sse2_ucomigt_sd:
3071 case Intrinsic::x86_sse2_ucomige_sd:
3072 case Intrinsic::x86_sse2_ucomineq_sd: {
Evan Cheng5ced1d82006-04-06 23:23:56 +00003073 unsigned Opc = 0;
3074 ISD::CondCode CC = ISD::SETCC_INVALID;
Evan Cheng6be2c582006-04-05 23:38:46 +00003075 switch (IntNo) {
3076 default: break;
3077 case Intrinsic::x86_sse_comieq_ss:
3078 case Intrinsic::x86_sse2_comieq_sd:
3079 Opc = X86ISD::COMI;
3080 CC = ISD::SETEQ;
3081 break;
3082 case Intrinsic::x86_sse_comilt_ss:
3083 case Intrinsic::x86_sse2_comilt_sd:
3084 Opc = X86ISD::COMI;
3085 CC = ISD::SETLT;
3086 break;
3087 case Intrinsic::x86_sse_comile_ss:
3088 case Intrinsic::x86_sse2_comile_sd:
3089 Opc = X86ISD::COMI;
3090 CC = ISD::SETLE;
3091 break;
3092 case Intrinsic::x86_sse_comigt_ss:
3093 case Intrinsic::x86_sse2_comigt_sd:
3094 Opc = X86ISD::COMI;
3095 CC = ISD::SETGT;
3096 break;
3097 case Intrinsic::x86_sse_comige_ss:
3098 case Intrinsic::x86_sse2_comige_sd:
3099 Opc = X86ISD::COMI;
3100 CC = ISD::SETGE;
3101 break;
3102 case Intrinsic::x86_sse_comineq_ss:
3103 case Intrinsic::x86_sse2_comineq_sd:
3104 Opc = X86ISD::COMI;
3105 CC = ISD::SETNE;
3106 break;
3107 case Intrinsic::x86_sse_ucomieq_ss:
3108 case Intrinsic::x86_sse2_ucomieq_sd:
3109 Opc = X86ISD::UCOMI;
3110 CC = ISD::SETEQ;
3111 break;
3112 case Intrinsic::x86_sse_ucomilt_ss:
3113 case Intrinsic::x86_sse2_ucomilt_sd:
3114 Opc = X86ISD::UCOMI;
3115 CC = ISD::SETLT;
3116 break;
3117 case Intrinsic::x86_sse_ucomile_ss:
3118 case Intrinsic::x86_sse2_ucomile_sd:
3119 Opc = X86ISD::UCOMI;
3120 CC = ISD::SETLE;
3121 break;
3122 case Intrinsic::x86_sse_ucomigt_ss:
3123 case Intrinsic::x86_sse2_ucomigt_sd:
3124 Opc = X86ISD::UCOMI;
3125 CC = ISD::SETGT;
3126 break;
3127 case Intrinsic::x86_sse_ucomige_ss:
3128 case Intrinsic::x86_sse2_ucomige_sd:
3129 Opc = X86ISD::UCOMI;
3130 CC = ISD::SETGE;
3131 break;
3132 case Intrinsic::x86_sse_ucomineq_ss:
3133 case Intrinsic::x86_sse2_ucomineq_sd:
3134 Opc = X86ISD::UCOMI;
3135 CC = ISD::SETNE;
3136 break;
3137 }
3138 bool Flip;
3139 unsigned X86CC;
3140 translateX86CC(CC, true, X86CC, Flip);
3141 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3142 Op.getOperand(Flip?1:2));
3143 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3144 DAG.getConstant(X86CC, MVT::i8), Cond);
3145 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
3146 }
3147 }
3148 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00003149 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003150}
Evan Cheng72261582005-12-20 06:22:03 +00003151
3152const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3153 switch (Opcode) {
3154 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00003155 case X86ISD::SHLD: return "X86ISD::SHLD";
3156 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00003157 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00003158 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00003159 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00003160 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00003161 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3162 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3163 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00003164 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00003165 case X86ISD::FST: return "X86ISD::FST";
3166 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00003167 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00003168 case X86ISD::CALL: return "X86ISD::CALL";
3169 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3170 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3171 case X86ISD::CMP: return "X86ISD::CMP";
3172 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng6be2c582006-04-05 23:38:46 +00003173 case X86ISD::COMI: return "X86ISD::COMI";
3174 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00003175 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00003176 case X86ISD::CMOV: return "X86ISD::CMOV";
3177 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00003178 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00003179 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3180 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00003181 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00003182 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00003183 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00003184 case X86ISD::S2VEC: return "X86ISD::S2VEC";
3185 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00003186 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00003187 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng72261582005-12-20 06:22:03 +00003188 }
3189}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003190
Nate Begeman368e18d2006-02-16 21:11:51 +00003191void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3192 uint64_t Mask,
3193 uint64_t &KnownZero,
3194 uint64_t &KnownOne,
3195 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003196 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00003197 assert((Opc >= ISD::BUILTIN_OP_END ||
3198 Opc == ISD::INTRINSIC_WO_CHAIN ||
3199 Opc == ISD::INTRINSIC_W_CHAIN ||
3200 Opc == ISD::INTRINSIC_VOID) &&
3201 "Should use MaskedValueIsZero if you don't know whether Op"
3202 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003203
Evan Cheng865f0602006-04-05 06:11:20 +00003204 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003205 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00003206 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +00003207 case X86ISD::SETCC:
3208 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3209 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003210 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003211}
Chris Lattner259e97c2006-01-31 19:43:35 +00003212
3213std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00003214getRegClassForInlineAsmConstraint(const std::string &Constraint,
3215 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00003216 if (Constraint.size() == 1) {
3217 // FIXME: not handling fp-stack yet!
3218 // FIXME: not handling MMX registers yet ('y' constraint).
3219 switch (Constraint[0]) { // GCC X86 Constraint Letters
3220 default: break; // Unknown constriant letter
3221 case 'r': // GENERAL_REGS
3222 case 'R': // LEGACY_REGS
3223 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3224 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3225 case 'l': // INDEX_REGS
3226 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3227 X86::ESI, X86::EDI, X86::EBP, 0);
3228 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3229 case 'Q': // Q_REGS
3230 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
3231 case 'x': // SSE_REGS if SSE1 allowed
3232 if (Subtarget->hasSSE1())
3233 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3234 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3235 0);
3236 return std::vector<unsigned>();
3237 case 'Y': // SSE_REGS if SSE2 allowed
3238 if (Subtarget->hasSSE2())
3239 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3240 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3241 0);
3242 return std::vector<unsigned>();
3243 }
3244 }
3245
Chris Lattner1efa40f2006-02-22 00:56:39 +00003246 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00003247}
Evan Cheng30b37b52006-03-13 23:18:16 +00003248
3249/// isLegalAddressImmediate - Return true if the integer value or
3250/// GlobalValue can be used as the offset of the target addressing mode.
3251bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3252 // X86 allows a sign-extended 32-bit immediate field.
3253 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3254}
3255
3256bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00003257 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00003258 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3259 if (RModel == Reloc::Static)
3260 return true;
3261 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00003262 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00003263 else
3264 return false;
3265 } else
3266 return true;
3267}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003268
3269/// isShuffleMaskLegal - Targets can use this to indicate that they only
3270/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3271/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3272/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00003273bool
3274X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3275 // Only do shuffles on 128-bit vector types for now.
3276 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2c0dbd02006-03-24 02:58:06 +00003277 return (Mask.Val->getNumOperands() == 2 ||
Evan Chengc575ca22006-04-17 20:43:08 +00003278 isSplatMask(Mask.Val) ||
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003279 X86::isMOVSMask(Mask.Val) ||
Evan Chengd9539472006-04-14 21:59:03 +00003280 X86::isMOVSHDUPMask(Mask.Val) ||
3281 X86::isMOVSLDUPMask(Mask.Val) ||
Evan Cheng14aed5e2006-03-24 01:18:28 +00003282 X86::isPSHUFDMask(Mask.Val) ||
Evan Chengc21a0532006-04-05 01:47:37 +00003283 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Chengd6d1cbd2006-04-11 00:19:04 +00003284 X86::isSHUFPMask(Mask.Val) ||
Evan Chenged4ca7f2006-03-28 08:27:15 +00003285 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003286 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey2d2a6132006-03-28 10:17:11 +00003287 X86::isUNPCKHMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00003288}