blob: 8214e2ef22697733b88203b2d8bdce2d51c8c84f [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);
Nate Begeman37efe672006-04-22 18:53:45 +0000172 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000173 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000174 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000175 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000176 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000179 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000180 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
181 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000182
Chris Lattnerf73bae12005-11-29 06:16:21 +0000183 // We don't have line number support yet.
184 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000185 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000186 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000187 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000188 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000189
Nate Begemanacc398c2006-01-25 18:21:52 +0000190 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
191 setOperationAction(ISD::VASTART , MVT::Other, Custom);
192
193 // Use the default implementation.
194 setOperationAction(ISD::VAARG , MVT::Other, Expand);
195 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
196 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000197 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
198 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
199 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000200
Chris Lattner9601a862006-03-05 05:08:37 +0000201 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
202 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
203
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000204 if (X86ScalarSSE) {
205 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000206 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
207 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000208
209 // SSE has no load+extend ops
210 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
211 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
212
Evan Cheng223547a2006-01-31 22:28:30 +0000213 // Use ANDPD to simulate FABS.
214 setOperationAction(ISD::FABS , MVT::f64, Custom);
215 setOperationAction(ISD::FABS , MVT::f32, Custom);
216
217 // Use XORP to simulate FNEG.
218 setOperationAction(ISD::FNEG , MVT::f64, Custom);
219 setOperationAction(ISD::FNEG , MVT::f32, Custom);
220
Evan Chengd25e9e82006-02-02 00:28:23 +0000221 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 setOperationAction(ISD::FSIN , MVT::f64, Expand);
223 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000224 setOperationAction(ISD::FREM , MVT::f64, Expand);
225 setOperationAction(ISD::FSIN , MVT::f32, Expand);
226 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000227 setOperationAction(ISD::FREM , MVT::f32, Expand);
228
Chris Lattnera54aa942006-01-29 06:26:08 +0000229 // Expand FP immediates into loads from the stack, except for the special
230 // cases we handle.
231 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
232 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000233 addLegalFPImmediate(+0.0); // xorps / xorpd
234 } else {
235 // Set up the FP register classes.
236 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000237
238 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
239
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 if (!UnsafeFPMath) {
241 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
242 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
243 }
244
Chris Lattnera54aa942006-01-29 06:26:08 +0000245 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 addLegalFPImmediate(+0.0); // FLD0
247 addLegalFPImmediate(+1.0); // FLD1
248 addLegalFPImmediate(-0.0); // FLD0/FCHS
249 addLegalFPImmediate(-1.0); // FLD1/FCHS
250 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000251
Evan Chengd30bf012006-03-01 01:11:20 +0000252 // First set operation action for all vector types to expand. Then we
253 // will selectively turn on ones that can be effectively codegen'd.
254 for (unsigned VT = (unsigned)MVT::Vector + 1;
255 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
256 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
258 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
259 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000260 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000261 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000262 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000263 }
264
Evan Chenga88973f2006-03-22 19:22:18 +0000265 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000266 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
267 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
268 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
269
Evan Chengd30bf012006-03-01 01:11:20 +0000270 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000271 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
272 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
273 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000274 }
275
Evan Chenga88973f2006-03-22 19:22:18 +0000276 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000277 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
278
Evan Cheng2c3ae372006-04-12 21:21:57 +0000279 setOperationAction(ISD::AND, MVT::v4f32, Legal);
280 setOperationAction(ISD::OR, MVT::v4f32, Legal);
281 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000282 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
283 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
284 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
285 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
286 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
287 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000288 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000289 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000290 }
291
Evan Chenga88973f2006-03-22 19:22:18 +0000292 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000293 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
295 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
296 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
297 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
298
Evan Chengf7c378e2006-04-10 07:23:14 +0000299 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
300 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
301 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
302 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
303 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
304 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
305 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
306 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000307 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000308 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000309
Evan Chengf7c378e2006-04-10 07:23:14 +0000310 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
311 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000312 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000313 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
314 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
315 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000316
Evan Cheng2c3ae372006-04-12 21:21:57 +0000317 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
318 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
319 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
320 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
322 }
323 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
324 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
325 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
326 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
327 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
328 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
329
330 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
331 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
332 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
333 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
334 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
335 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
336 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
337 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000338 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
339 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000340 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
341 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000342 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000343
344 // Custom lower v2i64 and v2f64 selects.
345 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000346 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000347 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000348 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000349 }
350
Evan Cheng6be2c582006-04-05 23:38:46 +0000351 // We want to custom lower some of our intrinsics.
352 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
353
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000354 computeRegisterProperties();
355
Evan Cheng87ed7162006-02-14 08:25:08 +0000356 // FIXME: These should be based on subtarget info. Plus, the values should
357 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000358 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
359 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
360 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000361 allowUnalignedMemoryAccesses = true; // x86 supports it!
362}
363
364std::vector<SDOperand>
365X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +0000366 std::vector<SDOperand> Args = TargetLowering::LowerArguments(F, DAG);
367
368 FormalArgs.clear();
Evan Chengeda65fa2006-04-27 01:32:22 +0000369 FormalArgLocs.clear();
370
Evan Cheng1bc78042006-04-26 01:20:17 +0000371 // This sets BytesToPopOnReturn, BytesCallerReserves, etc. which have to be set
372 // before the rest of the function can be lowered.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000373 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
Evan Chengeda65fa2006-04-27 01:32:22 +0000374 PreprocessFastCCArguments(Args, F, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +0000375 else
Evan Chengeda65fa2006-04-27 01:32:22 +0000376 PreprocessCCCArguments(Args, F, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +0000377 return Args;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000378}
379
380std::pair<SDOperand, SDOperand>
381X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
382 bool isVarArg, unsigned CallingConv,
383 bool isTailCall,
384 SDOperand Callee, ArgListTy &Args,
385 SelectionDAG &DAG) {
386 assert((!isVarArg || CallingConv == CallingConv::C) &&
387 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000388
389 // If the callee is a GlobalAddress node (quite common, every direct call is)
390 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
391 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
392 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000393 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
394 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000395
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000396 if (CallingConv == CallingConv::Fast && EnableFastCC)
397 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
398 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
399}
400
401//===----------------------------------------------------------------------===//
402// C Calling Convention implementation
403//===----------------------------------------------------------------------===//
404
Evan Chengeda65fa2006-04-27 01:32:22 +0000405static unsigned getFormalArgSize(MVT::ValueType ObjectVT) {
406 unsigned ObjSize = 0;
407 switch (ObjectVT) {
408 default: assert(0 && "Unhandled argument type!");
409 case MVT::i1:
410 case MVT::i8: ObjSize = 1; break;
411 case MVT::i16: ObjSize = 2; break;
412 case MVT::i32: ObjSize = 4; break;
413 case MVT::i64: ObjSize = 8; break;
414 case MVT::f32: ObjSize = 4; break;
415 case MVT::f64: ObjSize = 8; break;
416 }
417 return ObjSize;
418}
419
420static std::vector<SDOperand> getFormalArgObjects(SDOperand Op) {
421 unsigned Opc = Op.getOpcode();
422 std::vector<SDOperand> Objs;
423 if (Opc == ISD::TRUNCATE) {
424 Op = Op.getOperand(0);
425 assert(Op.getOpcode() == ISD::AssertSext ||
426 Op.getOpcode() == ISD::AssertZext);
427 Objs.push_back(Op.getOperand(0));
428 } else if (Opc == ISD::FP_ROUND) {
429 Objs.push_back(Op.getOperand(0));
430 } else if (Opc == ISD::BUILD_PAIR) {
431 Objs.push_back(Op.getOperand(0));
432 Objs.push_back(Op.getOperand(1));
433 } else {
434 Objs.push_back(Op);
435 }
436 return Objs;
437}
438
439void X86TargetLowering::PreprocessCCCArguments(std::vector<SDOperand>Args,
440 Function &F, SelectionDAG &DAG) {
441 unsigned NumArgs = Args.size();
Evan Cheng1bc78042006-04-26 01:20:17 +0000442 MachineFunction &MF = DAG.getMachineFunction();
443 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000444
Evan Chengeda65fa2006-04-27 01:32:22 +0000445 // Add DAG nodes to load the arguments... On entry to a function on the X86,
446 // the stack frame looks like this:
447 //
448 // [ESP] -- return address
449 // [ESP + 4] -- first argument (leftmost lexically)
450 // [ESP + 8] -- second argument, if first argument is four bytes in size
451 // ...
452 //
Evan Cheng1bc78042006-04-26 01:20:17 +0000453 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
454 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Chengeda65fa2006-04-27 01:32:22 +0000455 SDOperand Op = Args[i];
456 std::vector<SDOperand> Objs = getFormalArgObjects(Op);
457 for (std::vector<SDOperand>::iterator I = Objs.begin(), E = Objs.end();
458 I != E; ++I) {
459 SDOperand Obj = *I;
460 MVT::ValueType ObjectVT = Obj.getValueType();
461 unsigned ArgIncrement = 4;
462 unsigned ObjSize = getFormalArgSize(ObjectVT);
463 if (ObjSize == 8)
464 ArgIncrement = 8;
465
466 // Create the frame index object for this incoming parameter...
467 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
468 std::pair<FALocInfo, FALocInfo> Loc =
469 std::make_pair(FALocInfo(FALocInfo::StackFrameLoc, FI), FALocInfo());
470 FormalArgLocs.push_back(Loc);
471 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Cheng1bc78042006-04-26 01:20:17 +0000472 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000473 }
474
475 // If the function takes variable number of arguments, make a frame index for
476 // the start of the first vararg value... for expansion of llvm.va_start.
477 if (F.isVarArg())
478 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
479 ReturnAddrIndex = 0; // No return address slot generated yet.
480 BytesToPopOnReturn = 0; // Callee pops nothing.
481 BytesCallerReserves = ArgOffset;
482}
483
484void X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
485 unsigned NumArgs = Op.Val->getNumValues();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000486 MachineFunction &MF = DAG.getMachineFunction();
487 MachineFrameInfo *MFI = MF.getFrameInfo();
488
Evan Cheng1bc78042006-04-26 01:20:17 +0000489 for (unsigned i = 0; i < NumArgs; ++i) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000490 // Create the SelectionDAG nodes corresponding to a load from this parameter
Evan Chengeda65fa2006-04-27 01:32:22 +0000491 unsigned FI = FormalArgLocs[i].first.Loc;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000492 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
Evan Chengeda65fa2006-04-27 01:32:22 +0000493 SDOperand ArgValue = DAG.getLoad(Op.Val->getValueType(i),DAG.getEntryNode(),
494 FIN, DAG.getSrcValue(NULL));
Evan Cheng1bc78042006-04-26 01:20:17 +0000495 FormalArgs.push_back(ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000496 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000497}
498
499std::pair<SDOperand, SDOperand>
500X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
501 bool isVarArg, bool isTailCall,
502 SDOperand Callee, ArgListTy &Args,
503 SelectionDAG &DAG) {
504 // Count how many bytes are to be pushed on the stack.
505 unsigned NumBytes = 0;
506
507 if (Args.empty()) {
508 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000509 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000510 } else {
511 for (unsigned i = 0, e = Args.size(); i != e; ++i)
512 switch (getValueType(Args[i].second)) {
513 default: assert(0 && "Unknown value type!");
514 case MVT::i1:
515 case MVT::i8:
516 case MVT::i16:
517 case MVT::i32:
518 case MVT::f32:
519 NumBytes += 4;
520 break;
521 case MVT::i64:
522 case MVT::f64:
523 NumBytes += 8;
524 break;
525 }
526
Chris Lattner94dd2922006-02-13 09:00:43 +0000527 Chain = DAG.getCALLSEQ_START(Chain,
528 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000529
530 // Arguments go on the stack in reverse order, as specified by the ABI.
531 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000532 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000533 std::vector<SDOperand> Stores;
534
535 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
536 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
537 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
538
539 switch (getValueType(Args[i].second)) {
540 default: assert(0 && "Unexpected ValueType for argument!");
541 case MVT::i1:
542 case MVT::i8:
543 case MVT::i16:
544 // Promote the integer to 32 bits. If the input type is signed use a
545 // sign extend, otherwise use a zero extend.
546 if (Args[i].second->isSigned())
547 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
548 else
549 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
550
551 // FALL THROUGH
552 case MVT::i32:
553 case MVT::f32:
554 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
555 Args[i].first, PtrOff,
556 DAG.getSrcValue(NULL)));
557 ArgOffset += 4;
558 break;
559 case MVT::i64:
560 case MVT::f64:
561 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
562 Args[i].first, PtrOff,
563 DAG.getSrcValue(NULL)));
564 ArgOffset += 8;
565 break;
566 }
567 }
568 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
569 }
570
571 std::vector<MVT::ValueType> RetVals;
572 MVT::ValueType RetTyVT = getValueType(RetTy);
573 RetVals.push_back(MVT::Other);
574
575 // The result values produced have to be legal. Promote the result.
576 switch (RetTyVT) {
577 case MVT::isVoid: break;
578 default:
579 RetVals.push_back(RetTyVT);
580 break;
581 case MVT::i1:
582 case MVT::i8:
583 case MVT::i16:
584 RetVals.push_back(MVT::i32);
585 break;
586 case MVT::f32:
587 if (X86ScalarSSE)
588 RetVals.push_back(MVT::f32);
589 else
590 RetVals.push_back(MVT::f64);
591 break;
592 case MVT::i64:
593 RetVals.push_back(MVT::i32);
594 RetVals.push_back(MVT::i32);
595 break;
596 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000597
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000598 std::vector<MVT::ValueType> NodeTys;
599 NodeTys.push_back(MVT::Other); // Returns a chain
600 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
601 std::vector<SDOperand> Ops;
602 Ops.push_back(Chain);
603 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000604
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000605 // FIXME: Do not generate X86ISD::TAILCALL for now.
606 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
607 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000608
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000609 NodeTys.clear();
610 NodeTys.push_back(MVT::Other); // Returns a chain
611 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
612 Ops.clear();
613 Ops.push_back(Chain);
614 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
615 Ops.push_back(DAG.getConstant(0, getPointerTy()));
616 Ops.push_back(InFlag);
617 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
618 InFlag = Chain.getValue(1);
619
620 SDOperand RetVal;
621 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000622 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000623 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000624 case MVT::i1:
625 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000626 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
627 Chain = RetVal.getValue(1);
628 if (RetTyVT == MVT::i1)
629 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
630 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000631 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000632 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
633 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000634 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000635 case MVT::i32:
636 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
637 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000638 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000639 case MVT::i64: {
640 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
641 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
642 Lo.getValue(2));
643 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
644 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000645 break;
646 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000647 case MVT::f32:
648 case MVT::f64: {
649 std::vector<MVT::ValueType> Tys;
650 Tys.push_back(MVT::f64);
651 Tys.push_back(MVT::Other);
652 Tys.push_back(MVT::Flag);
653 std::vector<SDOperand> Ops;
654 Ops.push_back(Chain);
655 Ops.push_back(InFlag);
656 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
657 Chain = RetVal.getValue(1);
658 InFlag = RetVal.getValue(2);
659 if (X86ScalarSSE) {
660 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
661 // shouldn't be necessary except that RFP cannot be live across
662 // multiple blocks. When stackifier is fixed, they can be uncoupled.
663 MachineFunction &MF = DAG.getMachineFunction();
664 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
665 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
666 Tys.clear();
667 Tys.push_back(MVT::Other);
668 Ops.clear();
669 Ops.push_back(Chain);
670 Ops.push_back(RetVal);
671 Ops.push_back(StackSlot);
672 Ops.push_back(DAG.getValueType(RetTyVT));
673 Ops.push_back(InFlag);
674 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
675 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
676 DAG.getSrcValue(NULL));
677 Chain = RetVal.getValue(1);
678 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000679
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000680 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
681 // FIXME: we would really like to remember that this FP_ROUND
682 // operation is okay to eliminate if we allow excess FP precision.
683 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
684 break;
685 }
686 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000687 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000688
689 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000690}
691
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000692//===----------------------------------------------------------------------===//
693// Fast Calling Convention implementation
694//===----------------------------------------------------------------------===//
695//
696// The X86 'fast' calling convention passes up to two integer arguments in
697// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
698// and requires that the callee pop its arguments off the stack (allowing proper
699// tail calls), and has the same return value conventions as C calling convs.
700//
701// This calling convention always arranges for the callee pop value to be 8n+4
702// bytes, which is needed for tail recursion elimination and stack alignment
703// reasons.
704//
705// Note that this can be enhanced in the future to pass fp vals in registers
706// (when we have a global fp allocator) and do other tricks.
707//
708
709/// AddLiveIn - This helper function adds the specified physical register to the
710/// MachineFunction as a live in value. It also creates a corresponding virtual
711/// register for it.
712static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
713 TargetRegisterClass *RC) {
714 assert(RC->contains(PReg) && "Not the correct regclass!");
715 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
716 MF.addLiveIn(PReg, VReg);
717 return VReg;
718}
719
Chris Lattner89fad2c2006-03-17 17:27:47 +0000720// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
721// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
722// EDX". Anything more is illegal.
723//
724// FIXME: The linscan register allocator currently has problem with
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000725// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-03-17 17:27:47 +0000726// a physreg with a virtreg, this increases the size of the physreg's live
727// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000728// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-03-17 17:27:47 +0000729// allocator to wedge itself.
730//
731// This code triggers this problem more often if we pass args in registers,
732// so disable it until this is fixed.
733//
734// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
735// about code being dead.
736//
737static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000738
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000739
Evan Chengeda65fa2006-04-27 01:32:22 +0000740static void
741DetermineFastCCFormalArgSizeNumRegs(MVT::ValueType ObjectVT,
742 unsigned &ObjSize, unsigned &NumIntRegs) {
743 ObjSize = 0;
744 NumIntRegs = 0;
745
746 switch (ObjectVT) {
747 default: assert(0 && "Unhandled argument type!");
748 case MVT::i1:
749 case MVT::i8:
750 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
751 NumIntRegs = 1;
752 else
753 ObjSize = 1;
754 break;
755 case MVT::i16:
756 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
757 NumIntRegs = 1;
758 else
759 ObjSize = 2;
760 break;
761 case MVT::i32:
762 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
763 NumIntRegs = 1;
764 else
765 ObjSize = 4;
766 break;
767 case MVT::i64:
768 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
769 NumIntRegs = 2;
770 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
771 NumIntRegs = 1;
772 ObjSize = 4;
773 } else
774 ObjSize = 8;
775 case MVT::f32:
776 ObjSize = 4;
777 break;
778 case MVT::f64:
779 ObjSize = 8;
780 break;
781 }
782}
783
Evan Cheng1bc78042006-04-26 01:20:17 +0000784void
Evan Chengeda65fa2006-04-27 01:32:22 +0000785X86TargetLowering::PreprocessFastCCArguments(std::vector<SDOperand>Args,
786 Function &F, SelectionDAG &DAG) {
787 unsigned NumArgs = Args.size();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000788 MachineFunction &MF = DAG.getMachineFunction();
789 MachineFrameInfo *MFI = MF.getFrameInfo();
790
Evan Chengeda65fa2006-04-27 01:32:22 +0000791 // Add DAG nodes to load the arguments... On entry to a function the stack
792 // frame looks like this:
793 //
794 // [ESP] -- return address
795 // [ESP + 4] -- first nonreg argument (leftmost lexically)
796 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
797 // ...
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000798 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
799
800 // Keep track of the number of integer regs passed so far. This can be either
801 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
802 // used).
803 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000804
Evan Cheng1bc78042006-04-26 01:20:17 +0000805 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Chengeda65fa2006-04-27 01:32:22 +0000806 SDOperand Op = Args[i];
807 std::vector<SDOperand> Objs = getFormalArgObjects(Op);
808 for (std::vector<SDOperand>::iterator I = Objs.begin(), E = Objs.end();
809 I != E; ++I) {
810 SDOperand Obj = *I;
811 MVT::ValueType ObjectVT = Obj.getValueType();
812 unsigned ArgIncrement = 4;
813 unsigned ObjSize = 0;
814 unsigned NumRegs = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000815
Evan Chengeda65fa2006-04-27 01:32:22 +0000816 DetermineFastCCFormalArgSizeNumRegs(ObjectVT, ObjSize, NumRegs);
817 if (ObjSize == 8)
818 ArgIncrement = 8;
819
820 unsigned Reg;
821 std::pair<FALocInfo,FALocInfo> Loc = std::make_pair(FALocInfo(),
822 FALocInfo());
823 if (NumRegs) {
824 switch (ObjectVT) {
825 default: assert(0 && "Unhandled argument type!");
826 case MVT::i1:
827 case MVT::i8:
828 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
829 X86::R8RegisterClass);
830 Loc.first.Kind = FALocInfo::LiveInRegLoc;
831 Loc.first.Loc = Reg;
832 Loc.first.Typ = MVT::i8;
833 break;
834 case MVT::i16:
835 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
836 X86::R16RegisterClass);
837 Loc.first.Kind = FALocInfo::LiveInRegLoc;
838 Loc.first.Loc = Reg;
839 Loc.first.Typ = MVT::i16;
840 break;
841 case MVT::i32:
842 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
843 X86::R32RegisterClass);
844 Loc.first.Kind = FALocInfo::LiveInRegLoc;
845 Loc.first.Loc = Reg;
846 Loc.first.Typ = MVT::i32;
847 break;
848 case MVT::i64:
849 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
850 X86::R32RegisterClass);
851 Loc.first.Kind = FALocInfo::LiveInRegLoc;
852 Loc.first.Loc = Reg;
853 Loc.first.Typ = MVT::i32;
854 if (NumRegs == 2) {
855 Reg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
856 Loc.second.Kind = FALocInfo::LiveInRegLoc;
857 Loc.second.Loc = Reg;
858 Loc.second.Typ = MVT::i32;
859 }
860 break;
861 }
862 }
863 if (ObjSize) {
864 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
865 if (ObjectVT == MVT::i64 && NumRegs) {
866 Loc.second.Kind = FALocInfo::StackFrameLoc;
867 Loc.second.Loc = FI;
868 } else {
869 Loc.first.Kind = FALocInfo::StackFrameLoc;
870 Loc.first.Loc = FI;
871 }
872 ArgOffset += ArgIncrement; // Move on to the next argument.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000873 }
874
Evan Chengeda65fa2006-04-27 01:32:22 +0000875 FormalArgLocs.push_back(Loc);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000876 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000877 }
878
879 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
880 // arguments and the arguments after the retaddr has been pushed are aligned.
881 if ((ArgOffset & 7) == 0)
882 ArgOffset += 4;
883
884 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
885 ReturnAddrIndex = 0; // No return address slot generated yet.
886 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
887 BytesCallerReserves = 0;
888
889 // Finally, inform the code generator which regs we return values in.
890 switch (getValueType(F.getReturnType())) {
891 default: assert(0 && "Unknown type!");
892 case MVT::isVoid: break;
893 case MVT::i1:
894 case MVT::i8:
895 case MVT::i16:
896 case MVT::i32:
897 MF.addLiveOut(X86::EAX);
898 break;
899 case MVT::i64:
900 MF.addLiveOut(X86::EAX);
901 MF.addLiveOut(X86::EDX);
902 break;
903 case MVT::f32:
904 case MVT::f64:
905 MF.addLiveOut(X86::ST0);
906 break;
907 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000908}
909void
910X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
911 unsigned NumArgs = Op.Val->getNumValues();
912 MachineFunction &MF = DAG.getMachineFunction();
913 MachineFrameInfo *MFI = MF.getFrameInfo();
914
Evan Cheng1bc78042006-04-26 01:20:17 +0000915 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Chengeda65fa2006-04-27 01:32:22 +0000916 MVT::ValueType VT = Op.Val->getValueType(i);
917 std::pair<FALocInfo, FALocInfo> Loc = FormalArgLocs[i];
Evan Cheng1bc78042006-04-26 01:20:17 +0000918 SDOperand ArgValue;
Evan Chengeda65fa2006-04-27 01:32:22 +0000919 if (Loc.first.Kind == FALocInfo::StackFrameLoc) {
920 // Create the SelectionDAG nodes corresponding to a load from this parameter
921 SDOperand FIN = DAG.getFrameIndex(Loc.first.Loc, MVT::i32);
922 ArgValue = DAG.getLoad(Op.Val->getValueType(i),DAG.getEntryNode(), FIN,
923 DAG.getSrcValue(NULL));
924 } else {
925 // Must be a CopyFromReg
926 ArgValue= DAG.getCopyFromReg(DAG.getRoot(), Loc.first.Loc, Loc.first.Typ);
Evan Cheng1bc78042006-04-26 01:20:17 +0000927 }
928
Evan Chengeda65fa2006-04-27 01:32:22 +0000929 if (Loc.second.Kind != FALocInfo::None) {
930 SDOperand ArgValue2;
931 if (Loc.second.Kind == FALocInfo::StackFrameLoc) {
932 // Create the SelectionDAG nodes corresponding to a load from this parameter
933 SDOperand FIN = DAG.getFrameIndex(Loc.second.Loc, MVT::i32);
934 ArgValue2 = DAG.getLoad(Op.Val->getValueType(i),DAG.getEntryNode(), FIN,
935 DAG.getSrcValue(NULL));
936 } else {
937 // Must be a CopyFromReg
938 ArgValue2 = DAG.getCopyFromReg(DAG.getRoot(),
939 Loc.second.Loc, Loc.second.Typ);
940 }
941 ArgValue = DAG.getNode(ISD::BUILD_PAIR, VT, ArgValue, ArgValue2);
Evan Cheng1bc78042006-04-26 01:20:17 +0000942 }
943 FormalArgs.push_back(ArgValue);
944 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000945}
946
947std::pair<SDOperand, SDOperand>
948X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
949 bool isTailCall, SDOperand Callee,
950 ArgListTy &Args, SelectionDAG &DAG) {
951 // Count how many bytes are to be pushed on the stack.
952 unsigned NumBytes = 0;
953
954 // Keep track of the number of integer regs passed so far. This can be either
955 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
956 // used).
957 unsigned NumIntRegs = 0;
958
959 for (unsigned i = 0, e = Args.size(); i != e; ++i)
960 switch (getValueType(Args[i].second)) {
961 default: assert(0 && "Unknown value type!");
962 case MVT::i1:
963 case MVT::i8:
964 case MVT::i16:
965 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000966 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000967 ++NumIntRegs;
968 break;
969 }
970 // fall through
971 case MVT::f32:
972 NumBytes += 4;
973 break;
974 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000975 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
976 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000977 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000978 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
979 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000980 NumBytes += 4;
981 break;
982 }
983
984 // fall through
985 case MVT::f64:
986 NumBytes += 8;
987 break;
988 }
989
990 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
991 // arguments and the arguments after the retaddr has been pushed are aligned.
992 if ((NumBytes & 7) == 0)
993 NumBytes += 4;
994
Chris Lattner94dd2922006-02-13 09:00:43 +0000995 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000996
997 // Arguments go on the stack in reverse order, as specified by the ABI.
998 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000999 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001000 NumIntRegs = 0;
1001 std::vector<SDOperand> Stores;
1002 std::vector<SDOperand> RegValuesToPass;
1003 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1004 switch (getValueType(Args[i].second)) {
1005 default: assert(0 && "Unexpected ValueType for argument!");
1006 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +00001007 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
1008 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001009 case MVT::i8:
1010 case MVT::i16:
1011 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +00001012 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001013 RegValuesToPass.push_back(Args[i].first);
1014 ++NumIntRegs;
1015 break;
1016 }
1017 // Fall through
1018 case MVT::f32: {
1019 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1020 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1021 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1022 Args[i].first, PtrOff,
1023 DAG.getSrcValue(NULL)));
1024 ArgOffset += 4;
1025 break;
1026 }
1027 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +00001028 // Can pass (at least) part of it in regs?
1029 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001030 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1031 Args[i].first, DAG.getConstant(1, MVT::i32));
1032 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1033 Args[i].first, DAG.getConstant(0, MVT::i32));
1034 RegValuesToPass.push_back(Lo);
1035 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +00001036
1037 // Pass both parts in regs?
1038 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001039 RegValuesToPass.push_back(Hi);
1040 ++NumIntRegs;
1041 } else {
1042 // Pass the high part in memory.
1043 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1044 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1045 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1046 Hi, PtrOff, DAG.getSrcValue(NULL)));
1047 ArgOffset += 4;
1048 }
1049 break;
1050 }
1051 // Fall through
1052 case MVT::f64:
1053 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1054 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1055 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1056 Args[i].first, PtrOff,
1057 DAG.getSrcValue(NULL)));
1058 ArgOffset += 8;
1059 break;
1060 }
1061 }
1062 if (!Stores.empty())
1063 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
1064
1065 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1066 // arguments and the arguments after the retaddr has been pushed are aligned.
1067 if ((ArgOffset & 7) == 0)
1068 ArgOffset += 4;
1069
1070 std::vector<MVT::ValueType> RetVals;
1071 MVT::ValueType RetTyVT = getValueType(RetTy);
1072
1073 RetVals.push_back(MVT::Other);
1074
1075 // The result values produced have to be legal. Promote the result.
1076 switch (RetTyVT) {
1077 case MVT::isVoid: break;
1078 default:
1079 RetVals.push_back(RetTyVT);
1080 break;
1081 case MVT::i1:
1082 case MVT::i8:
1083 case MVT::i16:
1084 RetVals.push_back(MVT::i32);
1085 break;
1086 case MVT::f32:
1087 if (X86ScalarSSE)
1088 RetVals.push_back(MVT::f32);
1089 else
1090 RetVals.push_back(MVT::f64);
1091 break;
1092 case MVT::i64:
1093 RetVals.push_back(MVT::i32);
1094 RetVals.push_back(MVT::i32);
1095 break;
1096 }
1097
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001098 // Build a sequence of copy-to-reg nodes chained together with token chain
1099 // and flag operands which copy the outgoing args into registers.
1100 SDOperand InFlag;
1101 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1102 unsigned CCReg;
1103 SDOperand RegToPass = RegValuesToPass[i];
1104 switch (RegToPass.getValueType()) {
1105 default: assert(0 && "Bad thing to pass in regs");
1106 case MVT::i8:
1107 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +00001108 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001109 case MVT::i16:
1110 CCReg = (i == 0) ? X86::AX : X86::DX;
1111 break;
1112 case MVT::i32:
1113 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1114 break;
1115 }
1116
1117 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1118 InFlag = Chain.getValue(1);
1119 }
1120
1121 std::vector<MVT::ValueType> NodeTys;
1122 NodeTys.push_back(MVT::Other); // Returns a chain
1123 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1124 std::vector<SDOperand> Ops;
1125 Ops.push_back(Chain);
1126 Ops.push_back(Callee);
1127 if (InFlag.Val)
1128 Ops.push_back(InFlag);
1129
1130 // FIXME: Do not generate X86ISD::TAILCALL for now.
1131 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1132 InFlag = Chain.getValue(1);
1133
1134 NodeTys.clear();
1135 NodeTys.push_back(MVT::Other); // Returns a chain
1136 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1137 Ops.clear();
1138 Ops.push_back(Chain);
1139 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1140 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1141 Ops.push_back(InFlag);
1142 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1143 InFlag = Chain.getValue(1);
1144
1145 SDOperand RetVal;
1146 if (RetTyVT != MVT::isVoid) {
1147 switch (RetTyVT) {
1148 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001149 case MVT::i1:
1150 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001151 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1152 Chain = RetVal.getValue(1);
1153 if (RetTyVT == MVT::i1)
1154 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1155 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001156 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001157 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1158 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001159 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001160 case MVT::i32:
1161 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1162 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001163 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001164 case MVT::i64: {
1165 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1166 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1167 Lo.getValue(2));
1168 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1169 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001170 break;
1171 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001172 case MVT::f32:
1173 case MVT::f64: {
1174 std::vector<MVT::ValueType> Tys;
1175 Tys.push_back(MVT::f64);
1176 Tys.push_back(MVT::Other);
1177 Tys.push_back(MVT::Flag);
1178 std::vector<SDOperand> Ops;
1179 Ops.push_back(Chain);
1180 Ops.push_back(InFlag);
1181 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1182 Chain = RetVal.getValue(1);
1183 InFlag = RetVal.getValue(2);
1184 if (X86ScalarSSE) {
1185 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1186 // shouldn't be necessary except that RFP cannot be live across
1187 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1188 MachineFunction &MF = DAG.getMachineFunction();
1189 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1190 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1191 Tys.clear();
1192 Tys.push_back(MVT::Other);
1193 Ops.clear();
1194 Ops.push_back(Chain);
1195 Ops.push_back(RetVal);
1196 Ops.push_back(StackSlot);
1197 Ops.push_back(DAG.getValueType(RetTyVT));
1198 Ops.push_back(InFlag);
1199 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1200 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1201 DAG.getSrcValue(NULL));
1202 Chain = RetVal.getValue(1);
1203 }
Evan Chengd9558e02006-01-06 00:43:03 +00001204
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001205 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1206 // FIXME: we would really like to remember that this FP_ROUND
1207 // operation is okay to eliminate if we allow excess FP precision.
1208 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1209 break;
1210 }
1211 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001212 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001213
1214 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001215}
1216
1217SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1218 if (ReturnAddrIndex == 0) {
1219 // Set up a frame object for the return address.
1220 MachineFunction &MF = DAG.getMachineFunction();
1221 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1222 }
1223
1224 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1225}
1226
1227
1228
1229std::pair<SDOperand, SDOperand> X86TargetLowering::
1230LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1231 SelectionDAG &DAG) {
1232 SDOperand Result;
1233 if (Depth) // Depths > 0 not supported yet!
1234 Result = DAG.getConstant(0, getPointerTy());
1235 else {
1236 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1237 if (!isFrameAddress)
1238 // Just load the return address
1239 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1240 DAG.getSrcValue(NULL));
1241 else
1242 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1243 DAG.getConstant(4, MVT::i32));
1244 }
1245 return std::make_pair(Result, Chain);
1246}
1247
Evan Cheng4a460802006-01-11 00:33:36 +00001248/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1249/// which corresponds to the condition code.
1250static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1251 switch (X86CC) {
1252 default: assert(0 && "Unknown X86 conditional code!");
1253 case X86ISD::COND_A: return X86::JA;
1254 case X86ISD::COND_AE: return X86::JAE;
1255 case X86ISD::COND_B: return X86::JB;
1256 case X86ISD::COND_BE: return X86::JBE;
1257 case X86ISD::COND_E: return X86::JE;
1258 case X86ISD::COND_G: return X86::JG;
1259 case X86ISD::COND_GE: return X86::JGE;
1260 case X86ISD::COND_L: return X86::JL;
1261 case X86ISD::COND_LE: return X86::JLE;
1262 case X86ISD::COND_NE: return X86::JNE;
1263 case X86ISD::COND_NO: return X86::JNO;
1264 case X86ISD::COND_NP: return X86::JNP;
1265 case X86ISD::COND_NS: return X86::JNS;
1266 case X86ISD::COND_O: return X86::JO;
1267 case X86ISD::COND_P: return X86::JP;
1268 case X86ISD::COND_S: return X86::JS;
1269 }
1270}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001271
Evan Cheng6dfa9992006-01-30 23:41:35 +00001272/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1273/// specific condition code. It returns a false if it cannot do a direct
1274/// translation. X86CC is the translated CondCode. Flip is set to true if the
1275/// the order of comparison operands should be flipped.
Evan Cheng6be2c582006-04-05 23:38:46 +00001276static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1277 unsigned &X86CC, bool &Flip) {
Evan Cheng6dfa9992006-01-30 23:41:35 +00001278 Flip = false;
1279 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001280 if (!isFP) {
1281 switch (SetCCOpcode) {
1282 default: break;
1283 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1284 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1285 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1286 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1287 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1288 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1289 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1290 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1291 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1292 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1293 }
1294 } else {
1295 // On a floating point condition, the flags are set as follows:
1296 // ZF PF CF op
1297 // 0 | 0 | 0 | X > Y
1298 // 0 | 0 | 1 | X < Y
1299 // 1 | 0 | 0 | X == Y
1300 // 1 | 1 | 1 | unordered
1301 switch (SetCCOpcode) {
1302 default: break;
1303 case ISD::SETUEQ:
1304 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001305 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001306 case ISD::SETOGT:
1307 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001308 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001309 case ISD::SETOGE:
1310 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001311 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001312 case ISD::SETULT:
1313 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001314 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001315 case ISD::SETULE:
1316 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1317 case ISD::SETONE:
1318 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1319 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1320 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1321 }
1322 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001323
1324 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001325}
1326
Evan Cheng6be2c582006-04-05 23:38:46 +00001327static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1328 bool &Flip) {
1329 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1330}
1331
Evan Cheng4a460802006-01-11 00:33:36 +00001332/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1333/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001334/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001335static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001336 switch (X86CC) {
1337 default:
1338 return false;
1339 case X86ISD::COND_B:
1340 case X86ISD::COND_BE:
1341 case X86ISD::COND_E:
1342 case X86ISD::COND_P:
1343 case X86ISD::COND_A:
1344 case X86ISD::COND_AE:
1345 case X86ISD::COND_NE:
1346 case X86ISD::COND_NP:
1347 return true;
1348 }
1349}
1350
Evan Cheng4a460802006-01-11 00:33:36 +00001351MachineBasicBlock *
1352X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1353 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001354 switch (MI->getOpcode()) {
1355 default: assert(false && "Unexpected instr type to insert");
1356 case X86::CMOV_FR32:
Evan Chengf7c378e2006-04-10 07:23:14 +00001357 case X86::CMOV_FR64:
1358 case X86::CMOV_V4F32:
1359 case X86::CMOV_V2F64:
1360 case X86::CMOV_V2I64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001361 // To "insert" a SELECT_CC instruction, we actually have to insert the
1362 // diamond control-flow pattern. The incoming instruction knows the
1363 // destination vreg to set, the condition code register to branch on, the
1364 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001365 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1366 ilist<MachineBasicBlock>::iterator It = BB;
1367 ++It;
1368
1369 // thisMBB:
1370 // ...
1371 // TrueVal = ...
1372 // cmpTY ccX, r1, r2
1373 // bCC copy1MBB
1374 // fallthrough --> copy0MBB
1375 MachineBasicBlock *thisMBB = BB;
1376 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1377 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1378 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1379 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1380 MachineFunction *F = BB->getParent();
1381 F->getBasicBlockList().insert(It, copy0MBB);
1382 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001383 // Update machine-CFG edges by first adding all successors of the current
1384 // block to the new block which will contain the Phi node for the select.
1385 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1386 e = BB->succ_end(); i != e; ++i)
1387 sinkMBB->addSuccessor(*i);
1388 // Next, remove all successors of the current block, and add the true
1389 // and fallthrough blocks as its successors.
1390 while(!BB->succ_empty())
1391 BB->removeSuccessor(BB->succ_begin());
Evan Cheng0cc39452006-01-16 21:21:29 +00001392 BB->addSuccessor(copy0MBB);
1393 BB->addSuccessor(sinkMBB);
1394
1395 // copy0MBB:
1396 // %FalseValue = ...
1397 // # fallthrough to sinkMBB
1398 BB = copy0MBB;
1399
1400 // Update machine-CFG edges
1401 BB->addSuccessor(sinkMBB);
1402
1403 // sinkMBB:
1404 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1405 // ...
1406 BB = sinkMBB;
1407 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1408 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1409 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001410
Evan Cheng0cc39452006-01-16 21:21:29 +00001411 delete MI; // The pseudo instruction is gone now.
1412 return BB;
1413 }
Evan Cheng4a460802006-01-11 00:33:36 +00001414
Evan Cheng0cc39452006-01-16 21:21:29 +00001415 case X86::FP_TO_INT16_IN_MEM:
1416 case X86::FP_TO_INT32_IN_MEM:
1417 case X86::FP_TO_INT64_IN_MEM: {
1418 // Change the floating point control register to use "round towards zero"
1419 // mode when truncating to an integer value.
1420 MachineFunction *F = BB->getParent();
1421 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1422 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1423
1424 // Load the old value of the high byte of the control word...
1425 unsigned OldCW =
1426 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1427 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1428
1429 // Set the high part to be round to zero...
1430 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1431
1432 // Reload the modified control word now...
1433 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1434
1435 // Restore the memory image of control word to original value
1436 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1437
1438 // Get the X86 opcode to use.
1439 unsigned Opc;
1440 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001441 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001442 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1443 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1444 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1445 }
1446
1447 X86AddressMode AM;
1448 MachineOperand &Op = MI->getOperand(0);
1449 if (Op.isRegister()) {
1450 AM.BaseType = X86AddressMode::RegBase;
1451 AM.Base.Reg = Op.getReg();
1452 } else {
1453 AM.BaseType = X86AddressMode::FrameIndexBase;
1454 AM.Base.FrameIndex = Op.getFrameIndex();
1455 }
1456 Op = MI->getOperand(1);
1457 if (Op.isImmediate())
1458 AM.Scale = Op.getImmedValue();
1459 Op = MI->getOperand(2);
1460 if (Op.isImmediate())
1461 AM.IndexReg = Op.getImmedValue();
1462 Op = MI->getOperand(3);
1463 if (Op.isGlobalAddress()) {
1464 AM.GV = Op.getGlobal();
1465 } else {
1466 AM.Disp = Op.getImmedValue();
1467 }
1468 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1469
1470 // Reload the original control word now.
1471 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1472
1473 delete MI; // The pseudo instruction is gone now.
1474 return BB;
1475 }
1476 }
Evan Cheng4a460802006-01-11 00:33:36 +00001477}
1478
1479
1480//===----------------------------------------------------------------------===//
1481// X86 Custom Lowering Hooks
1482//===----------------------------------------------------------------------===//
1483
Evan Cheng30b37b52006-03-13 23:18:16 +00001484/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1485/// load. For Darwin, external and weak symbols are indirect, loading the value
1486/// at address GV rather then the value of GV itself. This means that the
1487/// GlobalAddress must be in the base or index register of the address, not the
1488/// GV offset field.
1489static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1490 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1491 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1492}
1493
Evan Cheng5ced1d82006-04-06 23:23:56 +00001494/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001495/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001496static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1497 if (Op.getOpcode() == ISD::UNDEF)
1498 return true;
1499
1500 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001501 return (Val >= Low && Val < Hi);
1502}
1503
1504/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1505/// true if Op is undef or if its value equal to the specified value.
1506static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1507 if (Op.getOpcode() == ISD::UNDEF)
1508 return true;
1509 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001510}
1511
Evan Cheng0188ecb2006-03-22 18:59:22 +00001512/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1513/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1514bool X86::isPSHUFDMask(SDNode *N) {
1515 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1516
1517 if (N->getNumOperands() != 4)
1518 return false;
1519
1520 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001521 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001522 SDOperand Arg = N->getOperand(i);
1523 if (Arg.getOpcode() == ISD::UNDEF) continue;
1524 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1525 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00001526 return false;
1527 }
1528
1529 return true;
1530}
1531
1532/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001533/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001534bool X86::isPSHUFHWMask(SDNode *N) {
1535 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1536
1537 if (N->getNumOperands() != 8)
1538 return false;
1539
1540 // Lower quadword copied in order.
1541 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001542 SDOperand Arg = N->getOperand(i);
1543 if (Arg.getOpcode() == ISD::UNDEF) continue;
1544 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1545 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001546 return false;
1547 }
1548
1549 // Upper quadword shuffled.
1550 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001551 SDOperand Arg = N->getOperand(i);
1552 if (Arg.getOpcode() == ISD::UNDEF) continue;
1553 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1554 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001555 if (Val < 4 || Val > 7)
1556 return false;
1557 }
1558
1559 return true;
1560}
1561
1562/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001563/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001564bool X86::isPSHUFLWMask(SDNode *N) {
1565 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1566
1567 if (N->getNumOperands() != 8)
1568 return false;
1569
1570 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001571 for (unsigned i = 4; i != 8; ++i)
1572 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001573 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001574
1575 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001576 for (unsigned i = 0; i != 4; ++i)
1577 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001578 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001579
1580 return true;
1581}
1582
Evan Cheng14aed5e2006-03-24 01:18:28 +00001583/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1584/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng39623da2006-04-20 08:58:49 +00001585static bool isSHUFPMask(std::vector<SDOperand> &N) {
1586 unsigned NumElems = N.size();
1587 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001588
Evan Cheng39623da2006-04-20 08:58:49 +00001589 unsigned Half = NumElems / 2;
1590 for (unsigned i = 0; i < Half; ++i)
1591 if (!isUndefOrInRange(N[i], 0, NumElems))
1592 return false;
1593 for (unsigned i = Half; i < NumElems; ++i)
1594 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1595 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001596
1597 return true;
1598}
1599
Evan Cheng39623da2006-04-20 08:58:49 +00001600bool X86::isSHUFPMask(SDNode *N) {
1601 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1602 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1603 return ::isSHUFPMask(Ops);
1604}
1605
1606/// isCommutedSHUFP - Returns true if the shuffle mask is except
1607/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1608/// half elements to come from vector 1 (which would equal the dest.) and
1609/// the upper half to come from vector 2.
1610static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1611 unsigned NumElems = Ops.size();
1612 if (NumElems != 2 && NumElems != 4) return false;
1613
1614 unsigned Half = NumElems / 2;
1615 for (unsigned i = 0; i < Half; ++i)
1616 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1617 return false;
1618 for (unsigned i = Half; i < NumElems; ++i)
1619 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1620 return false;
1621 return true;
1622}
1623
1624static bool isCommutedSHUFP(SDNode *N) {
1625 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1626 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1627 return isCommutedSHUFP(Ops);
1628}
1629
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001630/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1631/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1632bool X86::isMOVHLPSMask(SDNode *N) {
1633 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1634
Evan Cheng2064a2b2006-03-28 06:50:32 +00001635 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001636 return false;
1637
Evan Cheng2064a2b2006-03-28 06:50:32 +00001638 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001639 return isUndefOrEqual(N->getOperand(0), 6) &&
1640 isUndefOrEqual(N->getOperand(1), 7) &&
1641 isUndefOrEqual(N->getOperand(2), 2) &&
1642 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001643}
1644
Evan Cheng5ced1d82006-04-06 23:23:56 +00001645/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1646/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1647bool X86::isMOVLPMask(SDNode *N) {
1648 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1649
1650 unsigned NumElems = N->getNumOperands();
1651 if (NumElems != 2 && NumElems != 4)
1652 return false;
1653
Evan Chengc5cdff22006-04-07 21:53:05 +00001654 for (unsigned i = 0; i < NumElems/2; ++i)
1655 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1656 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001657
Evan Chengc5cdff22006-04-07 21:53:05 +00001658 for (unsigned i = NumElems/2; i < NumElems; ++i)
1659 if (!isUndefOrEqual(N->getOperand(i), i))
1660 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001661
1662 return true;
1663}
1664
1665/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00001666/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1667/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00001668bool X86::isMOVHPMask(SDNode *N) {
1669 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1670
1671 unsigned NumElems = N->getNumOperands();
1672 if (NumElems != 2 && NumElems != 4)
1673 return false;
1674
Evan Chengc5cdff22006-04-07 21:53:05 +00001675 for (unsigned i = 0; i < NumElems/2; ++i)
1676 if (!isUndefOrEqual(N->getOperand(i), i))
1677 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001678
1679 for (unsigned i = 0; i < NumElems/2; ++i) {
1680 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001681 if (!isUndefOrEqual(Arg, i + NumElems))
1682 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001683 }
1684
1685 return true;
1686}
1687
Evan Cheng0038e592006-03-28 00:39:58 +00001688/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1689/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng39623da2006-04-20 08:58:49 +00001690bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1691 unsigned NumElems = N.size();
Evan Cheng0038e592006-03-28 00:39:58 +00001692 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1693 return false;
1694
1695 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001696 SDOperand BitI = N[i];
1697 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001698 if (!isUndefOrEqual(BitI, j))
1699 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001700 if (V2IsSplat) {
1701 if (isUndefOrEqual(BitI1, NumElems))
1702 return false;
1703 } else {
1704 if (!isUndefOrEqual(BitI1, j + NumElems))
1705 return false;
1706 }
Evan Cheng0038e592006-03-28 00:39:58 +00001707 }
1708
1709 return true;
1710}
1711
Evan Cheng39623da2006-04-20 08:58:49 +00001712bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1713 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1714 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1715 return ::isUNPCKLMask(Ops, V2IsSplat);
1716}
1717
Evan Cheng4fcb9222006-03-28 02:43:26 +00001718/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1719/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng39623da2006-04-20 08:58:49 +00001720bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1721 unsigned NumElems = N.size();
Evan Cheng4fcb9222006-03-28 02:43:26 +00001722 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1723 return false;
1724
1725 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001726 SDOperand BitI = N[i];
1727 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001728 if (!isUndefOrEqual(BitI, j + NumElems/2))
1729 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001730 if (V2IsSplat) {
1731 if (isUndefOrEqual(BitI1, NumElems))
1732 return false;
1733 } else {
1734 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1735 return false;
1736 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001737 }
1738
1739 return true;
1740}
1741
Evan Cheng39623da2006-04-20 08:58:49 +00001742bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1743 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1744 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1745 return ::isUNPCKHMask(Ops, V2IsSplat);
1746}
1747
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001748/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1749/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1750/// <0, 0, 1, 1>
1751bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1752 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1753
1754 unsigned NumElems = N->getNumOperands();
1755 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1756 return false;
1757
1758 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1759 SDOperand BitI = N->getOperand(i);
1760 SDOperand BitI1 = N->getOperand(i+1);
1761
Evan Chengc5cdff22006-04-07 21:53:05 +00001762 if (!isUndefOrEqual(BitI, j))
1763 return false;
1764 if (!isUndefOrEqual(BitI1, j))
1765 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001766 }
1767
1768 return true;
1769}
1770
Evan Cheng017dcc62006-04-21 01:05:10 +00001771/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1772/// specifies a shuffle of elements that is suitable for input to MOVSS,
1773/// MOVSD, and MOVD, i.e. setting the lowest element.
1774static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001775 unsigned NumElems = N.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001776 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001777 return false;
1778
Evan Cheng39623da2006-04-20 08:58:49 +00001779 if (!isUndefOrEqual(N[0], NumElems))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001780 return false;
1781
1782 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00001783 SDOperand Arg = N[i];
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001784 if (!isUndefOrEqual(Arg, i))
1785 return false;
1786 }
1787
1788 return true;
1789}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001790
Evan Cheng017dcc62006-04-21 01:05:10 +00001791bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001792 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1793 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001794 return ::isMOVLMask(Ops);
Evan Cheng39623da2006-04-20 08:58:49 +00001795}
1796
Evan Cheng017dcc62006-04-21 01:05:10 +00001797/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1798/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00001799/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Cheng017dcc62006-04-21 01:05:10 +00001800static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001801 unsigned NumElems = Ops.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001802 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00001803 return false;
1804
1805 if (!isUndefOrEqual(Ops[0], 0))
1806 return false;
1807
1808 for (unsigned i = 1; i < NumElems; ++i) {
1809 SDOperand Arg = Ops[i];
1810 if (V2IsSplat) {
1811 if (!isUndefOrEqual(Arg, NumElems))
1812 return false;
1813 } else {
1814 if (!isUndefOrEqual(Arg, i+NumElems))
1815 return false;
1816 }
1817 }
1818
1819 return true;
1820}
1821
Evan Cheng017dcc62006-04-21 01:05:10 +00001822static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001823 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1824 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001825 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001826}
1827
Evan Chengd9539472006-04-14 21:59:03 +00001828/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1829/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1830bool X86::isMOVSHDUPMask(SDNode *N) {
1831 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1832
1833 if (N->getNumOperands() != 4)
1834 return false;
1835
1836 // Expect 1, 1, 3, 3
1837 for (unsigned i = 0; i < 2; ++i) {
1838 SDOperand Arg = N->getOperand(i);
1839 if (Arg.getOpcode() == ISD::UNDEF) continue;
1840 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1841 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1842 if (Val != 1) return false;
1843 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001844
1845 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001846 for (unsigned i = 2; i < 4; ++i) {
1847 SDOperand Arg = N->getOperand(i);
1848 if (Arg.getOpcode() == ISD::UNDEF) continue;
1849 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1850 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1851 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001852 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001853 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001854
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001855 // Don't use movshdup if it can be done with a shufps.
1856 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001857}
1858
1859/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1860/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1861bool X86::isMOVSLDUPMask(SDNode *N) {
1862 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1863
1864 if (N->getNumOperands() != 4)
1865 return false;
1866
1867 // Expect 0, 0, 2, 2
1868 for (unsigned i = 0; i < 2; ++i) {
1869 SDOperand Arg = N->getOperand(i);
1870 if (Arg.getOpcode() == ISD::UNDEF) continue;
1871 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1872 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1873 if (Val != 0) return false;
1874 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001875
1876 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001877 for (unsigned i = 2; i < 4; ++i) {
1878 SDOperand Arg = N->getOperand(i);
1879 if (Arg.getOpcode() == ISD::UNDEF) continue;
1880 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1881 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1882 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001883 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001884 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001885
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001886 // Don't use movshdup if it can be done with a shufps.
1887 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001888}
1889
Evan Chengb9df0ca2006-03-22 02:53:00 +00001890/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1891/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00001892static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001893 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1894
Evan Chengb9df0ca2006-03-22 02:53:00 +00001895 // This is a splat operation if each element of the permute is the same, and
1896 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001897 unsigned NumElems = N->getNumOperands();
1898 SDOperand ElementBase;
1899 unsigned i = 0;
1900 for (; i != NumElems; ++i) {
1901 SDOperand Elt = N->getOperand(i);
1902 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1903 ElementBase = Elt;
1904 break;
1905 }
1906 }
1907
1908 if (!ElementBase.Val)
1909 return false;
1910
1911 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001912 SDOperand Arg = N->getOperand(i);
1913 if (Arg.getOpcode() == ISD::UNDEF) continue;
1914 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001915 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001916 }
1917
1918 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001919 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001920}
1921
Evan Chengc575ca22006-04-17 20:43:08 +00001922/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1923/// a splat of a single element and it's a 2 or 4 element mask.
1924bool X86::isSplatMask(SDNode *N) {
1925 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1926
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001927 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00001928 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1929 return false;
1930 return ::isSplatMask(N);
1931}
1932
Evan Cheng63d33002006-03-22 08:01:21 +00001933/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1934/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1935/// instructions.
1936unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001937 unsigned NumOperands = N->getNumOperands();
1938 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1939 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001940 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001941 unsigned Val = 0;
1942 SDOperand Arg = N->getOperand(NumOperands-i-1);
1943 if (Arg.getOpcode() != ISD::UNDEF)
1944 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00001945 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001946 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00001947 if (i != NumOperands - 1)
1948 Mask <<= Shift;
1949 }
Evan Cheng63d33002006-03-22 08:01:21 +00001950
1951 return Mask;
1952}
1953
Evan Cheng506d3df2006-03-29 23:07:14 +00001954/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1955/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1956/// instructions.
1957unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1958 unsigned Mask = 0;
1959 // 8 nodes, but we only care about the last 4.
1960 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001961 unsigned Val = 0;
1962 SDOperand Arg = N->getOperand(i);
1963 if (Arg.getOpcode() != ISD::UNDEF)
1964 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001965 Mask |= (Val - 4);
1966 if (i != 4)
1967 Mask <<= 2;
1968 }
1969
1970 return Mask;
1971}
1972
1973/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1974/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1975/// instructions.
1976unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1977 unsigned Mask = 0;
1978 // 8 nodes, but we only care about the first 4.
1979 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001980 unsigned Val = 0;
1981 SDOperand Arg = N->getOperand(i);
1982 if (Arg.getOpcode() != ISD::UNDEF)
1983 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001984 Mask |= Val;
1985 if (i != 0)
1986 Mask <<= 2;
1987 }
1988
1989 return Mask;
1990}
1991
Evan Chengc21a0532006-04-05 01:47:37 +00001992/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1993/// specifies a 8 element shuffle that can be broken into a pair of
1994/// PSHUFHW and PSHUFLW.
1995static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1996 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1997
1998 if (N->getNumOperands() != 8)
1999 return false;
2000
2001 // Lower quadword shuffled.
2002 for (unsigned i = 0; i != 4; ++i) {
2003 SDOperand Arg = N->getOperand(i);
2004 if (Arg.getOpcode() == ISD::UNDEF) continue;
2005 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2006 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2007 if (Val > 4)
2008 return false;
2009 }
2010
2011 // Upper quadword shuffled.
2012 for (unsigned i = 4; i != 8; ++i) {
2013 SDOperand Arg = N->getOperand(i);
2014 if (Arg.getOpcode() == ISD::UNDEF) continue;
2015 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2016 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2017 if (Val < 4 || Val > 7)
2018 return false;
2019 }
2020
2021 return true;
2022}
2023
Evan Cheng5ced1d82006-04-06 23:23:56 +00002024/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2025/// values in ther permute mask.
2026static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2027 SDOperand V1 = Op.getOperand(0);
2028 SDOperand V2 = Op.getOperand(1);
2029 SDOperand Mask = Op.getOperand(2);
2030 MVT::ValueType VT = Op.getValueType();
2031 MVT::ValueType MaskVT = Mask.getValueType();
2032 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2033 unsigned NumElems = Mask.getNumOperands();
2034 std::vector<SDOperand> MaskVec;
2035
2036 for (unsigned i = 0; i != NumElems; ++i) {
2037 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002038 if (Arg.getOpcode() == ISD::UNDEF) {
2039 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2040 continue;
2041 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002042 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2043 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2044 if (Val < NumElems)
2045 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2046 else
2047 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2048 }
2049
2050 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2051 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2052}
2053
Evan Cheng533a0aa2006-04-19 20:35:22 +00002054/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2055/// match movhlps. The lower half elements should come from upper half of
2056/// V1 (and in order), and the upper half elements should come from the upper
2057/// half of V2 (and in order).
2058static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2059 unsigned NumElems = Mask->getNumOperands();
2060 if (NumElems != 4)
2061 return false;
2062 for (unsigned i = 0, e = 2; i != e; ++i)
2063 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2064 return false;
2065 for (unsigned i = 2; i != 4; ++i)
2066 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2067 return false;
2068 return true;
2069}
2070
Evan Cheng5ced1d82006-04-06 23:23:56 +00002071/// isScalarLoadToVector - Returns true if the node is a scalar load that
2072/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002073static inline bool isScalarLoadToVector(SDNode *N) {
2074 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2075 N = N->getOperand(0).Val;
2076 return (N->getOpcode() == ISD::LOAD);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002077 }
2078 return false;
2079}
2080
Evan Cheng533a0aa2006-04-19 20:35:22 +00002081/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2082/// match movlp{s|d}. The lower half elements should come from lower half of
2083/// V1 (and in order), and the upper half elements should come from the upper
2084/// half of V2 (and in order). And since V1 will become the source of the
2085/// MOVLP, it must be either a vector load or a scalar load to vector.
2086static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2087 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2088 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002089
Evan Cheng533a0aa2006-04-19 20:35:22 +00002090 unsigned NumElems = Mask->getNumOperands();
2091 if (NumElems != 2 && NumElems != 4)
2092 return false;
2093 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2094 if (!isUndefOrEqual(Mask->getOperand(i), i))
2095 return false;
2096 for (unsigned i = NumElems/2; i != NumElems; ++i)
2097 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2098 return false;
2099 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002100}
2101
Evan Cheng39623da2006-04-20 08:58:49 +00002102/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2103/// all the same.
2104static bool isSplatVector(SDNode *N) {
2105 if (N->getOpcode() != ISD::BUILD_VECTOR)
2106 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002107
Evan Cheng39623da2006-04-20 08:58:49 +00002108 SDOperand SplatValue = N->getOperand(0);
2109 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2110 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002111 return false;
2112 return true;
2113}
2114
Evan Cheng39623da2006-04-20 08:58:49 +00002115/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2116/// that point to V2 points to its first element.
2117static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2118 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2119
2120 bool Changed = false;
2121 std::vector<SDOperand> MaskVec;
2122 unsigned NumElems = Mask.getNumOperands();
2123 for (unsigned i = 0; i != NumElems; ++i) {
2124 SDOperand Arg = Mask.getOperand(i);
2125 if (Arg.getOpcode() != ISD::UNDEF) {
2126 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2127 if (Val > NumElems) {
2128 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2129 Changed = true;
2130 }
2131 }
2132 MaskVec.push_back(Arg);
2133 }
2134
2135 if (Changed)
2136 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2137 return Mask;
2138}
2139
Evan Cheng017dcc62006-04-21 01:05:10 +00002140/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2141/// operation of specified width.
2142static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002143 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2144 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2145
2146 std::vector<SDOperand> MaskVec;
2147 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2148 for (unsigned i = 1; i != NumElems; ++i)
2149 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2150 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2151}
2152
Evan Chengc575ca22006-04-17 20:43:08 +00002153/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2154/// of specified width.
2155static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2156 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2157 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2158 std::vector<SDOperand> MaskVec;
2159 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2160 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2161 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2162 }
2163 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2164}
2165
Evan Cheng39623da2006-04-20 08:58:49 +00002166/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2167/// of specified width.
2168static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2169 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2170 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2171 unsigned Half = NumElems/2;
2172 std::vector<SDOperand> MaskVec;
2173 for (unsigned i = 0; i != Half; ++i) {
2174 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2175 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2176 }
2177 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2178}
2179
Evan Cheng017dcc62006-04-21 01:05:10 +00002180/// getZeroVector - Returns a vector of specified type with all zero elements.
2181///
2182static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2183 assert(MVT::isVector(VT) && "Expected a vector type");
2184 unsigned NumElems = getVectorNumElements(VT);
2185 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2186 bool isFP = MVT::isFloatingPoint(EVT);
2187 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2188 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2189 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2190}
2191
Evan Chengc575ca22006-04-17 20:43:08 +00002192/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2193///
2194static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2195 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002196 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002197 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002198 unsigned NumElems = Mask.getNumOperands();
2199 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002200 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002201 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002202 NumElems >>= 1;
2203 }
2204 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2205
2206 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002207 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002208 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002209 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002210 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2211}
2212
Evan Cheng017dcc62006-04-21 01:05:10 +00002213/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2214/// constant +0.0.
2215static inline bool isZeroNode(SDOperand Elt) {
2216 return ((isa<ConstantSDNode>(Elt) &&
2217 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2218 (isa<ConstantFPSDNode>(Elt) &&
2219 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2220}
2221
Evan Chengba05f722006-04-21 23:03:30 +00002222/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2223/// vector and zero or undef vector.
2224static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002225 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002226 bool isZero, SelectionDAG &DAG) {
2227 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002228 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2229 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2230 SDOperand Zero = DAG.getConstant(0, EVT);
2231 std::vector<SDOperand> MaskVec(NumElems, Zero);
2232 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2233 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Chengba05f722006-04-21 23:03:30 +00002234 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002235}
2236
Evan Chengc78d3b42006-04-24 18:01:45 +00002237/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2238///
2239static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2240 unsigned NumNonZero, unsigned NumZero,
2241 SelectionDAG &DAG) {
2242 if (NumNonZero > 8)
2243 return SDOperand();
2244
2245 SDOperand V(0, 0);
2246 bool First = true;
2247 for (unsigned i = 0; i < 16; ++i) {
2248 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2249 if (ThisIsNonZero && First) {
2250 if (NumZero)
2251 V = getZeroVector(MVT::v8i16, DAG);
2252 else
2253 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2254 First = false;
2255 }
2256
2257 if ((i & 1) != 0) {
2258 SDOperand ThisElt(0, 0), LastElt(0, 0);
2259 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2260 if (LastIsNonZero) {
2261 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2262 }
2263 if (ThisIsNonZero) {
2264 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2265 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2266 ThisElt, DAG.getConstant(8, MVT::i8));
2267 if (LastIsNonZero)
2268 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2269 } else
2270 ThisElt = LastElt;
2271
2272 if (ThisElt.Val)
2273 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2274 DAG.getConstant(i/2, MVT::i32));
2275 }
2276 }
2277
2278 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2279}
2280
2281/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2282///
2283static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2284 unsigned NumNonZero, unsigned NumZero,
2285 SelectionDAG &DAG) {
2286 if (NumNonZero > 4)
2287 return SDOperand();
2288
2289 SDOperand V(0, 0);
2290 bool First = true;
2291 for (unsigned i = 0; i < 8; ++i) {
2292 bool isNonZero = (NonZeros & (1 << i)) != 0;
2293 if (isNonZero) {
2294 if (First) {
2295 if (NumZero)
2296 V = getZeroVector(MVT::v8i16, DAG);
2297 else
2298 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2299 First = false;
2300 }
2301 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2302 DAG.getConstant(i, MVT::i32));
2303 }
2304 }
2305
2306 return V;
2307}
2308
Evan Cheng0db9fe62006-04-25 20:13:52 +00002309SDOperand
2310X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2311 // All zero's are handled with pxor.
2312 if (ISD::isBuildVectorAllZeros(Op.Val))
2313 return Op;
2314
2315 // All one's are handled with pcmpeqd.
2316 if (ISD::isBuildVectorAllOnes(Op.Val))
2317 return Op;
2318
2319 MVT::ValueType VT = Op.getValueType();
2320 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2321 unsigned EVTBits = MVT::getSizeInBits(EVT);
2322
2323 unsigned NumElems = Op.getNumOperands();
2324 unsigned NumZero = 0;
2325 unsigned NumNonZero = 0;
2326 unsigned NonZeros = 0;
2327 std::set<SDOperand> Values;
2328 for (unsigned i = 0; i < NumElems; ++i) {
2329 SDOperand Elt = Op.getOperand(i);
2330 if (Elt.getOpcode() != ISD::UNDEF) {
2331 Values.insert(Elt);
2332 if (isZeroNode(Elt))
2333 NumZero++;
2334 else {
2335 NonZeros |= (1 << i);
2336 NumNonZero++;
2337 }
2338 }
2339 }
2340
2341 if (NumNonZero == 0)
2342 // Must be a mix of zero and undef. Return a zero vector.
2343 return getZeroVector(VT, DAG);
2344
2345 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2346 if (Values.size() == 1)
2347 return SDOperand();
2348
2349 // Special case for single non-zero element.
2350 if (NumNonZero == 1) {
2351 unsigned Idx = CountTrailingZeros_32(NonZeros);
2352 SDOperand Item = Op.getOperand(Idx);
2353 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2354 if (Idx == 0)
2355 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2356 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2357 NumZero > 0, DAG);
2358
2359 if (EVTBits == 32) {
2360 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2361 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2362 DAG);
2363 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2364 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2365 std::vector<SDOperand> MaskVec;
2366 for (unsigned i = 0; i < NumElems; i++)
2367 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2368 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2369 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2370 DAG.getNode(ISD::UNDEF, VT), Mask);
2371 }
2372 }
2373
2374 // Let legalizer expand 2-widde build_vector's.
2375 if (EVTBits == 64)
2376 return SDOperand();
2377
2378 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2379 if (EVTBits == 8) {
2380 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2381 if (V.Val) return V;
2382 }
2383
2384 if (EVTBits == 16) {
2385 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2386 if (V.Val) return V;
2387 }
2388
2389 // If element VT is == 32 bits, turn it into a number of shuffles.
2390 std::vector<SDOperand> V(NumElems);
2391 if (NumElems == 4 && NumZero > 0) {
2392 for (unsigned i = 0; i < 4; ++i) {
2393 bool isZero = !(NonZeros & (1 << i));
2394 if (isZero)
2395 V[i] = getZeroVector(VT, DAG);
2396 else
2397 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2398 }
2399
2400 for (unsigned i = 0; i < 2; ++i) {
2401 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2402 default: break;
2403 case 0:
2404 V[i] = V[i*2]; // Must be a zero vector.
2405 break;
2406 case 1:
2407 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2408 getMOVLMask(NumElems, DAG));
2409 break;
2410 case 2:
2411 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2412 getMOVLMask(NumElems, DAG));
2413 break;
2414 case 3:
2415 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2416 getUnpacklMask(NumElems, DAG));
2417 break;
2418 }
2419 }
2420
2421 // Take advantage of the fact R32 to VR128 scalar_to_vector (i.e. movd)
2422 // clears the upper bits.
2423 // FIXME: we can do the same for v4f32 case when we know both parts of
2424 // the lower half come from scalar_to_vector (loadf32). We should do
2425 // that in post legalizer dag combiner with target specific hooks.
2426 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2427 return V[0];
2428 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2429 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2430 std::vector<SDOperand> MaskVec;
2431 bool Reverse = (NonZeros & 0x3) == 2;
2432 for (unsigned i = 0; i < 2; ++i)
2433 if (Reverse)
2434 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2435 else
2436 MaskVec.push_back(DAG.getConstant(i, EVT));
2437 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2438 for (unsigned i = 0; i < 2; ++i)
2439 if (Reverse)
2440 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2441 else
2442 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2443 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2444 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2445 }
2446
2447 if (Values.size() > 2) {
2448 // Expand into a number of unpckl*.
2449 // e.g. for v4f32
2450 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2451 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2452 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2453 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2454 for (unsigned i = 0; i < NumElems; ++i)
2455 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2456 NumElems >>= 1;
2457 while (NumElems != 0) {
2458 for (unsigned i = 0; i < NumElems; ++i)
2459 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2460 UnpckMask);
2461 NumElems >>= 1;
2462 }
2463 return V[0];
2464 }
2465
2466 return SDOperand();
2467}
2468
2469SDOperand
2470X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2471 SDOperand V1 = Op.getOperand(0);
2472 SDOperand V2 = Op.getOperand(1);
2473 SDOperand PermMask = Op.getOperand(2);
2474 MVT::ValueType VT = Op.getValueType();
2475 unsigned NumElems = PermMask.getNumOperands();
2476 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2477 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2478
2479 if (isSplatMask(PermMask.Val)) {
2480 if (NumElems <= 4) return Op;
2481 // Promote it to a v4i32 splat.
2482 return PromoteSplat(Op, DAG);
2483 }
2484
2485 if (X86::isMOVLMask(PermMask.Val))
2486 return (V1IsUndef) ? V2 : Op;
2487
2488 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2489 X86::isMOVSLDUPMask(PermMask.Val) ||
2490 X86::isMOVHLPSMask(PermMask.Val) ||
2491 X86::isMOVHPMask(PermMask.Val) ||
2492 X86::isMOVLPMask(PermMask.Val))
2493 return Op;
2494
2495 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2496 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2497 return CommuteVectorShuffle(Op, DAG);
2498
2499 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2500 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2501 if (V1IsSplat && !V2IsSplat) {
2502 Op = CommuteVectorShuffle(Op, DAG);
2503 V1 = Op.getOperand(0);
2504 V2 = Op.getOperand(1);
2505 PermMask = Op.getOperand(2);
2506 V2IsSplat = true;
2507 }
2508
2509 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2510 if (V2IsUndef) return V1;
2511 Op = CommuteVectorShuffle(Op, DAG);
2512 V1 = Op.getOperand(0);
2513 V2 = Op.getOperand(1);
2514 PermMask = Op.getOperand(2);
2515 if (V2IsSplat) {
2516 // V2 is a splat, so the mask may be malformed. That is, it may point
2517 // to any V2 element. The instruction selectior won't like this. Get
2518 // a corrected mask and commute to form a proper MOVS{S|D}.
2519 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2520 if (NewMask.Val != PermMask.Val)
2521 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2522 }
2523 return Op;
2524 }
2525
2526 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2527 X86::isUNPCKLMask(PermMask.Val) ||
2528 X86::isUNPCKHMask(PermMask.Val))
2529 return Op;
2530
2531 if (V2IsSplat) {
2532 // Normalize mask so all entries that point to V2 points to its first
2533 // element then try to match unpck{h|l} again. If match, return a
2534 // new vector_shuffle with the corrected mask.
2535 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2536 if (NewMask.Val != PermMask.Val) {
2537 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2538 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2539 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2540 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2541 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2542 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2543 }
2544 }
2545 }
2546
2547 // Normalize the node to match x86 shuffle ops if needed
2548 if (V2.getOpcode() != ISD::UNDEF)
2549 if (isCommutedSHUFP(PermMask.Val)) {
2550 Op = CommuteVectorShuffle(Op, DAG);
2551 V1 = Op.getOperand(0);
2552 V2 = Op.getOperand(1);
2553 PermMask = Op.getOperand(2);
2554 }
2555
2556 // If VT is integer, try PSHUF* first, then SHUFP*.
2557 if (MVT::isInteger(VT)) {
2558 if (X86::isPSHUFDMask(PermMask.Val) ||
2559 X86::isPSHUFHWMask(PermMask.Val) ||
2560 X86::isPSHUFLWMask(PermMask.Val)) {
2561 if (V2.getOpcode() != ISD::UNDEF)
2562 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2563 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2564 return Op;
2565 }
2566
2567 if (X86::isSHUFPMask(PermMask.Val))
2568 return Op;
2569
2570 // Handle v8i16 shuffle high / low shuffle node pair.
2571 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2572 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2573 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2574 std::vector<SDOperand> MaskVec;
2575 for (unsigned i = 0; i != 4; ++i)
2576 MaskVec.push_back(PermMask.getOperand(i));
2577 for (unsigned i = 4; i != 8; ++i)
2578 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2579 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2580 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2581 MaskVec.clear();
2582 for (unsigned i = 0; i != 4; ++i)
2583 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2584 for (unsigned i = 4; i != 8; ++i)
2585 MaskVec.push_back(PermMask.getOperand(i));
2586 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2587 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2588 }
2589 } else {
2590 // Floating point cases in the other order.
2591 if (X86::isSHUFPMask(PermMask.Val))
2592 return Op;
2593 if (X86::isPSHUFDMask(PermMask.Val) ||
2594 X86::isPSHUFHWMask(PermMask.Val) ||
2595 X86::isPSHUFLWMask(PermMask.Val)) {
2596 if (V2.getOpcode() != ISD::UNDEF)
2597 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2598 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2599 return Op;
2600 }
2601 }
2602
2603 if (NumElems == 4) {
2604 // Break it into (shuffle shuffle_hi, shuffle_lo).
2605 MVT::ValueType MaskVT = PermMask.getValueType();
2606 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2607 std::map<unsigned, std::pair<int, int> > Locs;
2608 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2609 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2610 std::vector<SDOperand> *MaskPtr = &LoMask;
2611 unsigned MaskIdx = 0;
2612 unsigned LoIdx = 0;
2613 unsigned HiIdx = NumElems/2;
2614 for (unsigned i = 0; i != NumElems; ++i) {
2615 if (i == NumElems/2) {
2616 MaskPtr = &HiMask;
2617 MaskIdx = 1;
2618 LoIdx = 0;
2619 HiIdx = NumElems/2;
2620 }
2621 SDOperand Elt = PermMask.getOperand(i);
2622 if (Elt.getOpcode() == ISD::UNDEF) {
2623 Locs[i] = std::make_pair(-1, -1);
2624 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2625 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2626 (*MaskPtr)[LoIdx] = Elt;
2627 LoIdx++;
2628 } else {
2629 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2630 (*MaskPtr)[HiIdx] = Elt;
2631 HiIdx++;
2632 }
2633 }
2634
2635 SDOperand LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2636 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2637 SDOperand HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2638 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
2639 std::vector<SDOperand> MaskOps;
2640 for (unsigned i = 0; i != NumElems; ++i) {
2641 if (Locs[i].first == -1) {
2642 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2643 } else {
2644 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2645 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2646 }
2647 }
2648 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2649 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2650 }
2651
2652 return SDOperand();
2653}
2654
2655SDOperand
2656X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2657 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2658 return SDOperand();
2659
2660 MVT::ValueType VT = Op.getValueType();
2661 // TODO: handle v16i8.
2662 if (MVT::getSizeInBits(VT) == 16) {
2663 // Transform it so it match pextrw which produces a 32-bit result.
2664 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2665 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2666 Op.getOperand(0), Op.getOperand(1));
2667 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2668 DAG.getValueType(VT));
2669 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2670 } else if (MVT::getSizeInBits(VT) == 32) {
2671 SDOperand Vec = Op.getOperand(0);
2672 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2673 if (Idx == 0)
2674 return Op;
2675
2676 // SHUFPS the element to the lowest double word, then movss.
2677 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2678 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2679 MVT::getVectorBaseType(MaskVT));
2680 std::vector<SDOperand> IdxVec;
2681 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2682 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2683 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2684 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2685 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2686 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2687 Vec, Vec, Mask);
2688 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2689 DAG.getConstant(0, MVT::i32));
2690 } else if (MVT::getSizeInBits(VT) == 64) {
2691 SDOperand Vec = Op.getOperand(0);
2692 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2693 if (Idx == 0)
2694 return Op;
2695
2696 // UNPCKHPD the element to the lowest double word, then movsd.
2697 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2698 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2699 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2700 std::vector<SDOperand> IdxVec;
2701 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2702 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2703 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2704 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2705 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2706 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2707 DAG.getConstant(0, MVT::i32));
2708 }
2709
2710 return SDOperand();
2711}
2712
2713SDOperand
2714X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2715 // Transform it so it match pinsrw which expects a 16-bit value in a R32
2716 // as its second argument.
2717 MVT::ValueType VT = Op.getValueType();
2718 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2719 SDOperand N0 = Op.getOperand(0);
2720 SDOperand N1 = Op.getOperand(1);
2721 SDOperand N2 = Op.getOperand(2);
2722 if (MVT::getSizeInBits(BaseVT) == 16) {
2723 if (N1.getValueType() != MVT::i32)
2724 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2725 if (N2.getValueType() != MVT::i32)
2726 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2727 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2728 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2729 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2730 if (Idx == 0) {
2731 // Use a movss.
2732 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2733 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2734 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2735 std::vector<SDOperand> MaskVec;
2736 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2737 for (unsigned i = 1; i <= 3; ++i)
2738 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2739 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2740 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2741 } else {
2742 // Use two pinsrw instructions to insert a 32 bit value.
2743 Idx <<= 1;
2744 if (MVT::isFloatingPoint(N1.getValueType())) {
2745 if (N1.getOpcode() == ISD::LOAD) {
2746 // Just load directly from f32mem to R32.
2747 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2748 N1.getOperand(2));
2749 } else {
2750 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2751 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2752 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2753 DAG.getConstant(0, MVT::i32));
2754 }
2755 }
2756 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2757 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2758 DAG.getConstant(Idx, MVT::i32));
2759 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2760 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2761 DAG.getConstant(Idx+1, MVT::i32));
2762 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2763 }
2764 }
2765
2766 return SDOperand();
2767}
2768
2769SDOperand
2770X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2771 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2772 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2773}
2774
2775// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2776// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2777// one of the above mentioned nodes. It has to be wrapped because otherwise
2778// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2779// be used to form addressing mode. These wrapped nodes will be selected
2780// into MOV32ri.
2781SDOperand
2782X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2783 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2784 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2785 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2786 CP->getAlignment()));
2787 if (Subtarget->isTargetDarwin()) {
2788 // With PIC, the address is actually $g + Offset.
2789 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2790 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2791 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2792 }
2793
2794 return Result;
2795}
2796
2797SDOperand
2798X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2799 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2800 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2801 DAG.getTargetGlobalAddress(GV, getPointerTy()));
2802 if (Subtarget->isTargetDarwin()) {
2803 // With PIC, the address is actually $g + Offset.
2804 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2805 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2806 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2807
2808 // For Darwin, external and weak symbols are indirect, so we want to load
2809 // the value at address GV, not the value of GV itself. This means that
2810 // the GlobalAddress must be in the base or index register of the address,
2811 // not the GV offset field.
2812 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2813 DarwinGVRequiresExtraLoad(GV))
2814 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2815 Result, DAG.getSrcValue(NULL));
2816 }
2817
2818 return Result;
2819}
2820
2821SDOperand
2822X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2823 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2824 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2825 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2826 if (Subtarget->isTargetDarwin()) {
2827 // With PIC, the address is actually $g + Offset.
2828 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2829 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2830 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2831 }
2832
2833 return Result;
2834}
2835
2836SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00002837 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2838 "Not an i64 shift!");
2839 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2840 SDOperand ShOpLo = Op.getOperand(0);
2841 SDOperand ShOpHi = Op.getOperand(1);
2842 SDOperand ShAmt = Op.getOperand(2);
2843 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00002844 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00002845 : DAG.getConstant(0, MVT::i32);
2846
2847 SDOperand Tmp2, Tmp3;
2848 if (Op.getOpcode() == ISD::SHL_PARTS) {
2849 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2850 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2851 } else {
2852 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00002853 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00002854 }
2855
2856 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2857 ShAmt, DAG.getConstant(32, MVT::i8));
2858
2859 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00002860 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00002861
2862 std::vector<MVT::ValueType> Tys;
2863 Tys.push_back(MVT::i32);
2864 Tys.push_back(MVT::Flag);
2865 std::vector<SDOperand> Ops;
2866 if (Op.getOpcode() == ISD::SHL_PARTS) {
2867 Ops.push_back(Tmp2);
2868 Ops.push_back(Tmp3);
2869 Ops.push_back(CC);
2870 Ops.push_back(InFlag);
2871 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2872 InFlag = Hi.getValue(1);
2873
2874 Ops.clear();
2875 Ops.push_back(Tmp3);
2876 Ops.push_back(Tmp1);
2877 Ops.push_back(CC);
2878 Ops.push_back(InFlag);
2879 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2880 } else {
2881 Ops.push_back(Tmp2);
2882 Ops.push_back(Tmp3);
2883 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00002884 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00002885 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2886 InFlag = Lo.getValue(1);
2887
2888 Ops.clear();
2889 Ops.push_back(Tmp3);
2890 Ops.push_back(Tmp1);
2891 Ops.push_back(CC);
2892 Ops.push_back(InFlag);
2893 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2894 }
2895
2896 Tys.clear();
2897 Tys.push_back(MVT::i32);
2898 Tys.push_back(MVT::i32);
2899 Ops.clear();
2900 Ops.push_back(Lo);
2901 Ops.push_back(Hi);
2902 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002903}
Evan Chenga3195e82006-01-12 22:54:21 +00002904
Evan Cheng0db9fe62006-04-25 20:13:52 +00002905SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2906 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2907 Op.getOperand(0).getValueType() >= MVT::i16 &&
2908 "Unknown SINT_TO_FP to lower!");
2909
2910 SDOperand Result;
2911 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2912 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2913 MachineFunction &MF = DAG.getMachineFunction();
2914 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2915 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2916 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2917 DAG.getEntryNode(), Op.getOperand(0),
2918 StackSlot, DAG.getSrcValue(NULL));
2919
2920 // Build the FILD
2921 std::vector<MVT::ValueType> Tys;
2922 Tys.push_back(MVT::f64);
2923 Tys.push_back(MVT::Other);
2924 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2925 std::vector<SDOperand> Ops;
2926 Ops.push_back(Chain);
2927 Ops.push_back(StackSlot);
2928 Ops.push_back(DAG.getValueType(SrcVT));
2929 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
2930 Tys, Ops);
2931
2932 if (X86ScalarSSE) {
2933 Chain = Result.getValue(1);
2934 SDOperand InFlag = Result.getValue(2);
2935
2936 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
2937 // shouldn't be necessary except that RFP cannot be live across
2938 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002939 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00002940 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002941 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00002942 std::vector<MVT::ValueType> Tys;
Evan Cheng6dab0532006-01-30 08:02:57 +00002943 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002944 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00002945 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002946 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002947 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002948 Ops.push_back(DAG.getValueType(Op.getValueType()));
2949 Ops.push_back(InFlag);
2950 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
2951 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
2952 DAG.getSrcValue(NULL));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002953 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002954
Evan Cheng0db9fe62006-04-25 20:13:52 +00002955 return Result;
2956}
2957
2958SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
2959 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
2960 "Unknown FP_TO_SINT to lower!");
2961 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
2962 // stack slot.
2963 MachineFunction &MF = DAG.getMachineFunction();
2964 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
2965 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2966 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2967
2968 unsigned Opc;
2969 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002970 default: assert(0 && "Invalid FP_TO_SINT to lower!");
2971 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
2972 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
2973 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002974 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002975
Evan Cheng0db9fe62006-04-25 20:13:52 +00002976 SDOperand Chain = DAG.getEntryNode();
2977 SDOperand Value = Op.getOperand(0);
2978 if (X86ScalarSSE) {
2979 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
2980 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
2981 DAG.getSrcValue(0));
2982 std::vector<MVT::ValueType> Tys;
2983 Tys.push_back(MVT::f64);
2984 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002985 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00002986 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002987 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002988 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
2989 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2990 Chain = Value.getValue(1);
2991 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2992 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2993 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002994
Evan Cheng0db9fe62006-04-25 20:13:52 +00002995 // Build the FP_TO_INT*_IN_MEM
2996 std::vector<SDOperand> Ops;
2997 Ops.push_back(Chain);
2998 Ops.push_back(Value);
2999 Ops.push_back(StackSlot);
3000 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Chengd9558e02006-01-06 00:43:03 +00003001
Evan Cheng0db9fe62006-04-25 20:13:52 +00003002 // Load the result.
3003 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3004 DAG.getSrcValue(NULL));
3005}
3006
3007SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3008 MVT::ValueType VT = Op.getValueType();
3009 const Type *OpNTy = MVT::getTypeForValueType(VT);
3010 std::vector<Constant*> CV;
3011 if (VT == MVT::f64) {
3012 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3013 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3014 } else {
3015 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3016 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3017 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3018 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3019 }
3020 Constant *CS = ConstantStruct::get(CV);
3021 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3022 SDOperand Mask
3023 = DAG.getNode(X86ISD::LOAD_PACK,
3024 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3025 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3026}
3027
3028SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3029 MVT::ValueType VT = Op.getValueType();
3030 const Type *OpNTy = MVT::getTypeForValueType(VT);
3031 std::vector<Constant*> CV;
3032 if (VT == MVT::f64) {
3033 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3034 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3035 } else {
3036 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3037 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3038 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3039 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3040 }
3041 Constant *CS = ConstantStruct::get(CV);
3042 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3043 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3044 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3045 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3046}
3047
3048SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3049 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3050 SDOperand Cond;
3051 SDOperand CC = Op.getOperand(2);
3052 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3053 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3054 bool Flip;
3055 unsigned X86CC;
3056 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3057 if (Flip)
3058 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3059 Op.getOperand(1), Op.getOperand(0));
3060 else
Evan Cheng6dfa9992006-01-30 23:41:35 +00003061 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3062 Op.getOperand(0), Op.getOperand(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003063 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3064 DAG.getConstant(X86CC, MVT::i8), Cond);
3065 } else {
3066 assert(isFP && "Illegal integer SetCC!");
3067
3068 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3069 Op.getOperand(0), Op.getOperand(1));
3070 std::vector<MVT::ValueType> Tys;
3071 std::vector<SDOperand> Ops;
3072 switch (SetCCOpcode) {
Evan Chengd9558e02006-01-06 00:43:03 +00003073 default: assert(false && "Illegal floating point SetCC!");
3074 case ISD::SETOEQ: { // !PF & ZF
3075 Tys.push_back(MVT::i8);
3076 Tys.push_back(MVT::Flag);
3077 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3078 Ops.push_back(Cond);
3079 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3080 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3081 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3082 Tmp1.getValue(1));
3083 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3084 }
Evan Chengd9558e02006-01-06 00:43:03 +00003085 case ISD::SETUNE: { // PF | !ZF
3086 Tys.push_back(MVT::i8);
3087 Tys.push_back(MVT::Flag);
3088 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3089 Ops.push_back(Cond);
3090 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3091 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3092 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3093 Tmp1.getValue(1));
3094 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3095 }
Evan Chengd9558e02006-01-06 00:43:03 +00003096 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003097 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003098}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003099
Evan Cheng0db9fe62006-04-25 20:13:52 +00003100SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3101 MVT::ValueType VT = Op.getValueType();
3102 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3103 bool addTest = false;
3104 SDOperand Op0 = Op.getOperand(0);
3105 SDOperand Cond, CC;
3106 if (Op0.getOpcode() == ISD::SETCC)
3107 Op0 = LowerOperation(Op0, DAG);
Evan Cheng9bba8942006-01-26 02:13:10 +00003108
Evan Cheng0db9fe62006-04-25 20:13:52 +00003109 if (Op0.getOpcode() == X86ISD::SETCC) {
3110 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3111 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3112 // have another use it will be eliminated.
3113 // If the X86ISD::SETCC has more than one use, then it's probably better
3114 // to use a test instead of duplicating the X86ISD::CMP (for register
3115 // pressure reason).
3116 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3117 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3118 CmpOpc == X86ISD::UCOMI) {
3119 if (!Op0.hasOneUse()) {
3120 std::vector<MVT::ValueType> Tys;
3121 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3122 Tys.push_back(Op0.Val->getValueType(i));
3123 std::vector<SDOperand> Ops;
3124 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3125 Ops.push_back(Op0.getOperand(i));
3126 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3127 }
3128
3129 CC = Op0.getOperand(0);
3130 Cond = Op0.getOperand(1);
3131 // Make a copy as flag result cannot be used by more than one.
3132 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3133 Cond.getOperand(0), Cond.getOperand(1));
3134 addTest =
3135 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00003136 } else
3137 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003138 } else
3139 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00003140
Evan Cheng0db9fe62006-04-25 20:13:52 +00003141 if (addTest) {
3142 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3143 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00003144 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003145
Evan Cheng0db9fe62006-04-25 20:13:52 +00003146 std::vector<MVT::ValueType> Tys;
3147 Tys.push_back(Op.getValueType());
3148 Tys.push_back(MVT::Flag);
3149 std::vector<SDOperand> Ops;
3150 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3151 // condition is true.
3152 Ops.push_back(Op.getOperand(2));
3153 Ops.push_back(Op.getOperand(1));
3154 Ops.push_back(CC);
3155 Ops.push_back(Cond);
3156 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3157}
Evan Cheng9bba8942006-01-26 02:13:10 +00003158
Evan Cheng0db9fe62006-04-25 20:13:52 +00003159SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3160 bool addTest = false;
3161 SDOperand Cond = Op.getOperand(1);
3162 SDOperand Dest = Op.getOperand(2);
3163 SDOperand CC;
3164 if (Cond.getOpcode() == ISD::SETCC)
3165 Cond = LowerOperation(Cond, DAG);
3166
3167 if (Cond.getOpcode() == X86ISD::SETCC) {
3168 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3169 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3170 // have another use it will be eliminated.
3171 // If the X86ISD::SETCC has more than one use, then it's probably better
3172 // to use a test instead of duplicating the X86ISD::CMP (for register
3173 // pressure reason).
3174 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3175 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3176 CmpOpc == X86ISD::UCOMI) {
3177 if (!Cond.hasOneUse()) {
3178 std::vector<MVT::ValueType> Tys;
3179 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3180 Tys.push_back(Cond.Val->getValueType(i));
3181 std::vector<SDOperand> Ops;
3182 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3183 Ops.push_back(Cond.getOperand(i));
3184 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3185 }
3186
3187 CC = Cond.getOperand(0);
3188 Cond = Cond.getOperand(1);
3189 // Make a copy as flag result cannot be used by more than one.
3190 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3191 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00003192 } else
3193 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003194 } else
3195 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00003196
Evan Cheng0db9fe62006-04-25 20:13:52 +00003197 if (addTest) {
3198 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3199 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng898101c2005-12-19 23:12:38 +00003200 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003201 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3202 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3203}
Evan Cheng67f92a72006-01-11 22:15:48 +00003204
Evan Cheng0db9fe62006-04-25 20:13:52 +00003205SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3206 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3207 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3208 DAG.getTargetJumpTable(JT->getIndex(),
3209 getPointerTy()));
3210 if (Subtarget->isTargetDarwin()) {
3211 // With PIC, the address is actually $g + Offset.
3212 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3213 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3214 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng67f92a72006-01-11 22:15:48 +00003215 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00003216
Evan Cheng0db9fe62006-04-25 20:13:52 +00003217 return Result;
3218}
Evan Cheng7ccced62006-02-18 00:15:05 +00003219
Evan Cheng0db9fe62006-04-25 20:13:52 +00003220SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3221 SDOperand Copy;
Nate Begemanee625572006-01-27 21:09:22 +00003222
Evan Cheng0db9fe62006-04-25 20:13:52 +00003223 switch(Op.getNumOperands()) {
Nate Begemanee625572006-01-27 21:09:22 +00003224 default:
3225 assert(0 && "Do not know how to return this many arguments!");
3226 abort();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003227 case 1: // ret void.
Nate Begemanee625572006-01-27 21:09:22 +00003228 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003229 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begemanee625572006-01-27 21:09:22 +00003230 case 2: {
3231 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003232
3233 if (MVT::isVector(ArgVT)) {
3234 // Integer or FP vector result -> XMM0.
3235 if (DAG.getMachineFunction().liveout_empty())
3236 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3237 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3238 SDOperand());
3239 } else if (MVT::isInteger(ArgVT)) {
3240 // Integer result -> EAX
3241 if (DAG.getMachineFunction().liveout_empty())
3242 DAG.getMachineFunction().addLiveOut(X86::EAX);
3243
Nate Begemanee625572006-01-27 21:09:22 +00003244 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3245 SDOperand());
Chris Lattnerb2be4032006-04-17 20:32:50 +00003246 } else if (!X86ScalarSSE) {
3247 // FP return with fp-stack value.
3248 if (DAG.getMachineFunction().liveout_empty())
3249 DAG.getMachineFunction().addLiveOut(X86::ST0);
3250
Nate Begemanee625572006-01-27 21:09:22 +00003251 std::vector<MVT::ValueType> Tys;
3252 Tys.push_back(MVT::Other);
3253 Tys.push_back(MVT::Flag);
3254 std::vector<SDOperand> Ops;
3255 Ops.push_back(Op.getOperand(0));
3256 Ops.push_back(Op.getOperand(1));
3257 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3258 } else {
Chris Lattnerb2be4032006-04-17 20:32:50 +00003259 // FP return with ScalarSSE (return on fp-stack).
3260 if (DAG.getMachineFunction().liveout_empty())
3261 DAG.getMachineFunction().addLiveOut(X86::ST0);
3262
Evan Cheng0d084c92006-02-01 00:20:21 +00003263 SDOperand MemLoc;
3264 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003265 SDOperand Value = Op.getOperand(1);
3266
Evan Cheng760df292006-02-01 01:19:32 +00003267 if (Value.getOpcode() == ISD::LOAD &&
3268 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00003269 Chain = Value.getOperand(0);
3270 MemLoc = Value.getOperand(1);
3271 } else {
3272 // Spill the value to memory and reload it into top of stack.
3273 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3274 MachineFunction &MF = DAG.getMachineFunction();
3275 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3276 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3277 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3278 Value, MemLoc, DAG.getSrcValue(0));
3279 }
Nate Begemanee625572006-01-27 21:09:22 +00003280 std::vector<MVT::ValueType> Tys;
3281 Tys.push_back(MVT::f64);
3282 Tys.push_back(MVT::Other);
3283 std::vector<SDOperand> Ops;
3284 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003285 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00003286 Ops.push_back(DAG.getValueType(ArgVT));
3287 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3288 Tys.clear();
3289 Tys.push_back(MVT::Other);
3290 Tys.push_back(MVT::Flag);
3291 Ops.clear();
3292 Ops.push_back(Copy.getValue(1));
3293 Ops.push_back(Copy);
3294 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3295 }
3296 break;
3297 }
3298 case 3:
Chris Lattnerb2be4032006-04-17 20:32:50 +00003299 if (DAG.getMachineFunction().liveout_empty()) {
3300 DAG.getMachineFunction().addLiveOut(X86::EAX);
3301 DAG.getMachineFunction().addLiveOut(X86::EDX);
3302 }
3303
Nate Begemanee625572006-01-27 21:09:22 +00003304 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3305 SDOperand());
3306 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3307 break;
Nate Begemanee625572006-01-27 21:09:22 +00003308 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003309 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3310 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3311 Copy.getValue(1));
3312}
3313
Evan Cheng1bc78042006-04-26 01:20:17 +00003314SDOperand
3315X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3316 if (FormalArgs.size() == 0) {
3317 unsigned CC = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3318 if (CC == CallingConv::Fast && EnableFastCC)
3319 LowerFastCCArguments(Op, DAG);
3320 else
3321 LowerCCCArguments(Op, DAG);
3322 }
3323 return FormalArgs[Op.ResNo];
3324}
3325
Evan Cheng0db9fe62006-04-25 20:13:52 +00003326SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3327 SDOperand InFlag(0, 0);
3328 SDOperand Chain = Op.getOperand(0);
3329 unsigned Align =
3330 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3331 if (Align == 0) Align = 1;
3332
3333 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3334 // If not DWORD aligned, call memset if size is less than the threshold.
3335 // It knows how to align to the right boundary first.
3336 if ((Align & 3) != 0 ||
3337 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3338 MVT::ValueType IntPtr = getPointerTy();
3339 const Type *IntPtrTy = getTargetData().getIntPtrType();
3340 std::vector<std::pair<SDOperand, const Type*> > Args;
3341 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3342 // Extend the ubyte argument to be an int value for the call.
3343 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3344 Args.push_back(std::make_pair(Val, IntPtrTy));
3345 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3346 std::pair<SDOperand,SDOperand> CallResult =
3347 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3348 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3349 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00003350 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00003351
Evan Cheng0db9fe62006-04-25 20:13:52 +00003352 MVT::ValueType AVT;
3353 SDOperand Count;
3354 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3355 unsigned BytesLeft = 0;
3356 bool TwoRepStos = false;
3357 if (ValC) {
3358 unsigned ValReg;
3359 unsigned Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003360
Evan Cheng0db9fe62006-04-25 20:13:52 +00003361 // If the value is a constant, then we can potentially use larger sets.
3362 switch (Align & 3) {
3363 case 2: // WORD aligned
3364 AVT = MVT::i16;
3365 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3366 BytesLeft = I->getValue() % 2;
3367 Val = (Val << 8) | Val;
3368 ValReg = X86::AX;
3369 break;
3370 case 0: // DWORD aligned
3371 AVT = MVT::i32;
3372 if (I) {
3373 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3374 BytesLeft = I->getValue() % 4;
Evan Cheng80d428c2006-04-19 22:48:17 +00003375 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003376 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3377 DAG.getConstant(2, MVT::i8));
3378 TwoRepStos = true;
Evan Cheng80d428c2006-04-19 22:48:17 +00003379 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003380 Val = (Val << 8) | Val;
3381 Val = (Val << 16) | Val;
3382 ValReg = X86::EAX;
3383 break;
3384 default: // Byte aligned
3385 AVT = MVT::i8;
3386 Count = Op.getOperand(3);
3387 ValReg = X86::AL;
3388 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00003389 }
3390
Evan Cheng0db9fe62006-04-25 20:13:52 +00003391 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3392 InFlag);
3393 InFlag = Chain.getValue(1);
3394 } else {
3395 AVT = MVT::i8;
3396 Count = Op.getOperand(3);
3397 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3398 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00003399 }
Evan Chengc78d3b42006-04-24 18:01:45 +00003400
Evan Cheng0db9fe62006-04-25 20:13:52 +00003401 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3402 InFlag = Chain.getValue(1);
3403 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3404 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00003405
Evan Cheng0db9fe62006-04-25 20:13:52 +00003406 std::vector<MVT::ValueType> Tys;
3407 Tys.push_back(MVT::Other);
3408 Tys.push_back(MVT::Flag);
3409 std::vector<SDOperand> Ops;
3410 Ops.push_back(Chain);
3411 Ops.push_back(DAG.getValueType(AVT));
3412 Ops.push_back(InFlag);
3413 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengc78d3b42006-04-24 18:01:45 +00003414
Evan Cheng0db9fe62006-04-25 20:13:52 +00003415 if (TwoRepStos) {
3416 InFlag = Chain.getValue(1);
3417 Count = Op.getOperand(3);
3418 MVT::ValueType CVT = Count.getValueType();
3419 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3420 DAG.getConstant(3, CVT));
3421 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3422 InFlag = Chain.getValue(1);
3423 Tys.clear();
3424 Tys.push_back(MVT::Other);
3425 Tys.push_back(MVT::Flag);
3426 Ops.clear();
3427 Ops.push_back(Chain);
3428 Ops.push_back(DAG.getValueType(MVT::i8));
3429 Ops.push_back(InFlag);
3430 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3431 } else if (BytesLeft) {
3432 // Issue stores for the last 1 - 3 bytes.
3433 SDOperand Value;
3434 unsigned Val = ValC->getValue() & 255;
3435 unsigned Offset = I->getValue() - BytesLeft;
3436 SDOperand DstAddr = Op.getOperand(1);
3437 MVT::ValueType AddrVT = DstAddr.getValueType();
3438 if (BytesLeft >= 2) {
3439 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3440 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3441 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3442 DAG.getConstant(Offset, AddrVT)),
3443 DAG.getSrcValue(NULL));
3444 BytesLeft -= 2;
3445 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00003446 }
3447
Evan Cheng0db9fe62006-04-25 20:13:52 +00003448 if (BytesLeft == 1) {
3449 Value = DAG.getConstant(Val, MVT::i8);
3450 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3451 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3452 DAG.getConstant(Offset, AddrVT)),
3453 DAG.getSrcValue(NULL));
Evan Chengba05f722006-04-21 23:03:30 +00003454 }
Evan Cheng386031a2006-03-24 07:29:27 +00003455 }
Evan Cheng11e15b32006-04-03 20:53:28 +00003456
Evan Cheng0db9fe62006-04-25 20:13:52 +00003457 return Chain;
3458}
Evan Cheng11e15b32006-04-03 20:53:28 +00003459
Evan Cheng0db9fe62006-04-25 20:13:52 +00003460SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3461 SDOperand Chain = Op.getOperand(0);
3462 unsigned Align =
3463 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3464 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00003465
Evan Cheng0db9fe62006-04-25 20:13:52 +00003466 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3467 // If not DWORD aligned, call memcpy if size is less than the threshold.
3468 // It knows how to align to the right boundary first.
3469 if ((Align & 3) != 0 ||
3470 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3471 MVT::ValueType IntPtr = getPointerTy();
3472 const Type *IntPtrTy = getTargetData().getIntPtrType();
3473 std::vector<std::pair<SDOperand, const Type*> > Args;
3474 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3475 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3476 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3477 std::pair<SDOperand,SDOperand> CallResult =
3478 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3479 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3480 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00003481 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003482
3483 MVT::ValueType AVT;
3484 SDOperand Count;
3485 unsigned BytesLeft = 0;
3486 bool TwoRepMovs = false;
3487 switch (Align & 3) {
3488 case 2: // WORD aligned
3489 AVT = MVT::i16;
3490 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3491 BytesLeft = I->getValue() % 2;
3492 break;
3493 case 0: // DWORD aligned
3494 AVT = MVT::i32;
3495 if (I) {
3496 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3497 BytesLeft = I->getValue() % 4;
Evan Chengcdfc3c82006-04-17 22:45:49 +00003498 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003499 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3500 DAG.getConstant(2, MVT::i8));
3501 TwoRepMovs = true;
Evan Cheng5edb8d22006-04-17 22:04:06 +00003502 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003503 break;
3504 default: // Byte aligned
3505 AVT = MVT::i8;
3506 Count = Op.getOperand(3);
3507 break;
3508 }
3509
3510 SDOperand InFlag(0, 0);
3511 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3512 InFlag = Chain.getValue(1);
3513 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3514 InFlag = Chain.getValue(1);
3515 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3516 InFlag = Chain.getValue(1);
3517
3518 std::vector<MVT::ValueType> Tys;
3519 Tys.push_back(MVT::Other);
3520 Tys.push_back(MVT::Flag);
3521 std::vector<SDOperand> Ops;
3522 Ops.push_back(Chain);
3523 Ops.push_back(DAG.getValueType(AVT));
3524 Ops.push_back(InFlag);
3525 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3526
3527 if (TwoRepMovs) {
3528 InFlag = Chain.getValue(1);
3529 Count = Op.getOperand(3);
3530 MVT::ValueType CVT = Count.getValueType();
3531 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3532 DAG.getConstant(3, CVT));
3533 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3534 InFlag = Chain.getValue(1);
3535 Tys.clear();
3536 Tys.push_back(MVT::Other);
3537 Tys.push_back(MVT::Flag);
3538 Ops.clear();
3539 Ops.push_back(Chain);
3540 Ops.push_back(DAG.getValueType(MVT::i8));
3541 Ops.push_back(InFlag);
3542 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3543 } else if (BytesLeft) {
3544 // Issue loads and stores for the last 1 - 3 bytes.
3545 unsigned Offset = I->getValue() - BytesLeft;
3546 SDOperand DstAddr = Op.getOperand(1);
3547 MVT::ValueType DstVT = DstAddr.getValueType();
3548 SDOperand SrcAddr = Op.getOperand(2);
3549 MVT::ValueType SrcVT = SrcAddr.getValueType();
3550 SDOperand Value;
3551 if (BytesLeft >= 2) {
3552 Value = DAG.getLoad(MVT::i16, Chain,
3553 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3554 DAG.getConstant(Offset, SrcVT)),
3555 DAG.getSrcValue(NULL));
3556 Chain = Value.getValue(1);
3557 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3558 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3559 DAG.getConstant(Offset, DstVT)),
3560 DAG.getSrcValue(NULL));
3561 BytesLeft -= 2;
3562 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00003563 }
3564
Evan Cheng0db9fe62006-04-25 20:13:52 +00003565 if (BytesLeft == 1) {
3566 Value = DAG.getLoad(MVT::i8, Chain,
3567 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3568 DAG.getConstant(Offset, SrcVT)),
3569 DAG.getSrcValue(NULL));
3570 Chain = Value.getValue(1);
3571 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3572 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3573 DAG.getConstant(Offset, DstVT)),
3574 DAG.getSrcValue(NULL));
3575 }
Evan Chengb067a1e2006-03-31 19:22:53 +00003576 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003577
3578 return Chain;
3579}
3580
3581SDOperand
3582X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3583 std::vector<MVT::ValueType> Tys;
3584 Tys.push_back(MVT::Other);
3585 Tys.push_back(MVT::Flag);
3586 std::vector<SDOperand> Ops;
3587 Ops.push_back(Op.getOperand(0));
3588 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3589 Ops.clear();
3590 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3591 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3592 MVT::i32, Ops[0].getValue(2)));
3593 Ops.push_back(Ops[1].getValue(1));
3594 Tys[0] = Tys[1] = MVT::i32;
3595 Tys.push_back(MVT::Other);
3596 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3597}
3598
3599SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3600 // vastart just stores the address of the VarArgsFrameIndex slot into the
3601 // memory location argument.
3602 // FIXME: Replace MVT::i32 with PointerTy
3603 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3604 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3605 Op.getOperand(1), Op.getOperand(2));
3606}
3607
3608SDOperand
3609X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3610 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3611 switch (IntNo) {
3612 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00003613 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003614 case Intrinsic::x86_sse_comieq_ss:
3615 case Intrinsic::x86_sse_comilt_ss:
3616 case Intrinsic::x86_sse_comile_ss:
3617 case Intrinsic::x86_sse_comigt_ss:
3618 case Intrinsic::x86_sse_comige_ss:
3619 case Intrinsic::x86_sse_comineq_ss:
3620 case Intrinsic::x86_sse_ucomieq_ss:
3621 case Intrinsic::x86_sse_ucomilt_ss:
3622 case Intrinsic::x86_sse_ucomile_ss:
3623 case Intrinsic::x86_sse_ucomigt_ss:
3624 case Intrinsic::x86_sse_ucomige_ss:
3625 case Intrinsic::x86_sse_ucomineq_ss:
3626 case Intrinsic::x86_sse2_comieq_sd:
3627 case Intrinsic::x86_sse2_comilt_sd:
3628 case Intrinsic::x86_sse2_comile_sd:
3629 case Intrinsic::x86_sse2_comigt_sd:
3630 case Intrinsic::x86_sse2_comige_sd:
3631 case Intrinsic::x86_sse2_comineq_sd:
3632 case Intrinsic::x86_sse2_ucomieq_sd:
3633 case Intrinsic::x86_sse2_ucomilt_sd:
3634 case Intrinsic::x86_sse2_ucomile_sd:
3635 case Intrinsic::x86_sse2_ucomigt_sd:
3636 case Intrinsic::x86_sse2_ucomige_sd:
3637 case Intrinsic::x86_sse2_ucomineq_sd: {
3638 unsigned Opc = 0;
3639 ISD::CondCode CC = ISD::SETCC_INVALID;
3640 switch (IntNo) {
3641 default: break;
3642 case Intrinsic::x86_sse_comieq_ss:
3643 case Intrinsic::x86_sse2_comieq_sd:
3644 Opc = X86ISD::COMI;
3645 CC = ISD::SETEQ;
3646 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003647 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003648 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003649 Opc = X86ISD::COMI;
3650 CC = ISD::SETLT;
3651 break;
3652 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003653 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003654 Opc = X86ISD::COMI;
3655 CC = ISD::SETLE;
3656 break;
3657 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003658 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003659 Opc = X86ISD::COMI;
3660 CC = ISD::SETGT;
3661 break;
3662 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003663 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003664 Opc = X86ISD::COMI;
3665 CC = ISD::SETGE;
3666 break;
3667 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003668 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003669 Opc = X86ISD::COMI;
3670 CC = ISD::SETNE;
3671 break;
3672 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003673 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 Opc = X86ISD::UCOMI;
3675 CC = ISD::SETEQ;
3676 break;
3677 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003678 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003679 Opc = X86ISD::UCOMI;
3680 CC = ISD::SETLT;
3681 break;
3682 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003683 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003684 Opc = X86ISD::UCOMI;
3685 CC = ISD::SETLE;
3686 break;
3687 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003688 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003689 Opc = X86ISD::UCOMI;
3690 CC = ISD::SETGT;
3691 break;
3692 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003693 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003694 Opc = X86ISD::UCOMI;
3695 CC = ISD::SETGE;
3696 break;
3697 case Intrinsic::x86_sse_ucomineq_ss:
3698 case Intrinsic::x86_sse2_ucomineq_sd:
3699 Opc = X86ISD::UCOMI;
3700 CC = ISD::SETNE;
3701 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003702 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003703 bool Flip;
3704 unsigned X86CC;
3705 translateX86CC(CC, true, X86CC, Flip);
3706 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3707 Op.getOperand(Flip?1:2));
3708 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3709 DAG.getConstant(X86CC, MVT::i8), Cond);
3710 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00003711 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00003712 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003713}
Evan Cheng72261582005-12-20 06:22:03 +00003714
Evan Cheng0db9fe62006-04-25 20:13:52 +00003715/// LowerOperation - Provide custom lowering hooks for some operations.
3716///
3717SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3718 switch (Op.getOpcode()) {
3719 default: assert(0 && "Should not custom lower this!");
3720 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3721 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3722 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3723 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3724 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3725 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3726 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3727 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3728 case ISD::SHL_PARTS:
3729 case ISD::SRA_PARTS:
3730 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3731 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3732 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3733 case ISD::FABS: return LowerFABS(Op, DAG);
3734 case ISD::FNEG: return LowerFNEG(Op, DAG);
3735 case ISD::SETCC: return LowerSETCC(Op, DAG);
3736 case ISD::SELECT: return LowerSELECT(Op, DAG);
3737 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3738 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3739 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003740 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003741 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3742 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3743 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3744 case ISD::VASTART: return LowerVASTART(Op, DAG);
3745 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3746 }
3747}
3748
Evan Cheng72261582005-12-20 06:22:03 +00003749const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3750 switch (Opcode) {
3751 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00003752 case X86ISD::SHLD: return "X86ISD::SHLD";
3753 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00003754 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00003755 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00003756 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00003757 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00003758 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3759 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3760 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00003761 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00003762 case X86ISD::FST: return "X86ISD::FST";
3763 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00003764 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00003765 case X86ISD::CALL: return "X86ISD::CALL";
3766 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3767 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3768 case X86ISD::CMP: return "X86ISD::CMP";
3769 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng6be2c582006-04-05 23:38:46 +00003770 case X86ISD::COMI: return "X86ISD::COMI";
3771 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00003772 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00003773 case X86ISD::CMOV: return "X86ISD::CMOV";
3774 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00003775 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00003776 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3777 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00003778 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00003779 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00003780 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00003781 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00003782 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00003783 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng72261582005-12-20 06:22:03 +00003784 }
3785}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003786
Nate Begeman368e18d2006-02-16 21:11:51 +00003787void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3788 uint64_t Mask,
3789 uint64_t &KnownZero,
3790 uint64_t &KnownOne,
3791 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003792 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00003793 assert((Opc >= ISD::BUILTIN_OP_END ||
3794 Opc == ISD::INTRINSIC_WO_CHAIN ||
3795 Opc == ISD::INTRINSIC_W_CHAIN ||
3796 Opc == ISD::INTRINSIC_VOID) &&
3797 "Should use MaskedValueIsZero if you don't know whether Op"
3798 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003799
Evan Cheng865f0602006-04-05 06:11:20 +00003800 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003801 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00003802 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +00003803 case X86ISD::SETCC:
3804 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3805 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003806 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003807}
Chris Lattner259e97c2006-01-31 19:43:35 +00003808
3809std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00003810getRegClassForInlineAsmConstraint(const std::string &Constraint,
3811 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00003812 if (Constraint.size() == 1) {
3813 // FIXME: not handling fp-stack yet!
3814 // FIXME: not handling MMX registers yet ('y' constraint).
3815 switch (Constraint[0]) { // GCC X86 Constraint Letters
3816 default: break; // Unknown constriant letter
3817 case 'r': // GENERAL_REGS
3818 case 'R': // LEGACY_REGS
3819 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3820 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3821 case 'l': // INDEX_REGS
3822 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3823 X86::ESI, X86::EDI, X86::EBP, 0);
3824 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3825 case 'Q': // Q_REGS
3826 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
3827 case 'x': // SSE_REGS if SSE1 allowed
3828 if (Subtarget->hasSSE1())
3829 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3830 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3831 0);
3832 return std::vector<unsigned>();
3833 case 'Y': // SSE_REGS if SSE2 allowed
3834 if (Subtarget->hasSSE2())
3835 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3836 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3837 0);
3838 return std::vector<unsigned>();
3839 }
3840 }
3841
Chris Lattner1efa40f2006-02-22 00:56:39 +00003842 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00003843}
Evan Cheng30b37b52006-03-13 23:18:16 +00003844
3845/// isLegalAddressImmediate - Return true if the integer value or
3846/// GlobalValue can be used as the offset of the target addressing mode.
3847bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3848 // X86 allows a sign-extended 32-bit immediate field.
3849 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3850}
3851
3852bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00003853 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00003854 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3855 if (RModel == Reloc::Static)
3856 return true;
3857 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00003858 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00003859 else
3860 return false;
3861 } else
3862 return true;
3863}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003864
3865/// isShuffleMaskLegal - Targets can use this to indicate that they only
3866/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3867/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3868/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00003869bool
3870X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3871 // Only do shuffles on 128-bit vector types for now.
3872 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng80d428c2006-04-19 22:48:17 +00003873 return (Mask.Val->getNumOperands() <= 4 ||
Evan Chengc575ca22006-04-17 20:43:08 +00003874 isSplatMask(Mask.Val) ||
Evan Chengc21a0532006-04-05 01:47:37 +00003875 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Chenged4ca7f2006-03-28 08:27:15 +00003876 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00003877 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey2d2a6132006-03-28 10:17:11 +00003878 X86::isUNPCKHMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00003879}
Evan Cheng39623da2006-04-20 08:58:49 +00003880
3881bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3882 MVT::ValueType EVT,
3883 SelectionDAG &DAG) const {
3884 unsigned NumElts = BVOps.size();
3885 // Only do shuffles on 128-bit vector types for now.
3886 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3887 if (NumElts == 2) return true;
3888 if (NumElts == 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00003889 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng39623da2006-04-20 08:58:49 +00003890 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
3891 }
3892 return false;
3893}