blob: 1b0be8acc800de4e7a0b802957611c1fb3efd36d [file] [log] [blame]
Chris Lattnerc6d05672006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
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"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000023#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000024#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000025#include "llvm/ADT/VectorExtras.h"
26#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000028#include "llvm/CodeGen/MachineFunction.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/SelectionDAG.h"
31#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000032#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000033#include "llvm/Target/TargetOptions.h"
34using namespace llvm;
35
36// FIXME: temporary.
37#include "llvm/Support/CommandLine.h"
38static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
39 cl::desc("Enable fastcc on X86"));
40
41X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000043 Subtarget = &TM.getSubtarget<X86Subtarget>();
44 X86ScalarSSE = Subtarget->hasSSE2();
45
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000046 // Set up the TargetLowering object.
47
48 // X86 is weird, it always uses i8 for shift amounts and setcc results.
49 setShiftAmountType(MVT::i8);
50 setSetCCResultType(MVT::i8);
51 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000052 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000054 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng714554d2006-03-16 21:47:42 +000055
Evan Chenga88973f2006-03-22 19:22:18 +000056 if (!Subtarget->isTargetDarwin())
Evan Chengdf57fa02006-03-17 20:31:41 +000057 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
58 setUseUnderscoreSetJmpLongJmp(true);
59
Evan Cheng714554d2006-03-16 21:47:42 +000060 // Add legal addressing mode scale values.
61 addLegalAddressScale(8);
62 addLegalAddressScale(4);
63 addLegalAddressScale(2);
64 // Enter the ones which require both scale + index last. These are more
65 // expensive.
66 addLegalAddressScale(9);
67 addLegalAddressScale(5);
68 addLegalAddressScale(3);
Chris Lattnera54aa942006-01-29 06:26:08 +000069
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000070 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000071 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
72 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
73 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000074
75 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
76 // operation.
77 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
78 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
79 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000080
81 if (X86ScalarSSE)
82 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
83 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
84 else
85 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
87 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
88 // this operation.
89 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
90 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000091 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000092 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000093 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000094 else {
95 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
96 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
97 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000098
Evan Cheng6dab0532006-01-30 08:02:57 +000099 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
100 // isn't legal.
101 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
102 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
103
Evan Cheng02568ff2006-01-30 22:13:22 +0000104 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
105 // this operation.
106 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
107 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
108
109 if (X86ScalarSSE) {
110 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
111 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000113 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000114 }
115
116 // Handle FP_TO_UINT by promoting the destination to a larger signed
117 // conversion.
118 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
119 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
120 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
121
Evan Cheng45af8fd2006-02-18 07:26:17 +0000122 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000123 // Expand FP_TO_UINT into a select.
124 // FIXME: We would like to use a Custom expander here eventually to do
125 // the optimal thing for SSE vs. the default expansion in the legalizer.
126 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
127 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000128 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000129 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
130
Evan Cheng02568ff2006-01-30 22:13:22 +0000131 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
132 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000133
Evan Cheng5298bcc2006-02-17 07:01:52 +0000134 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000135 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
136 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000137 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000139 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000140 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
141 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
142 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
143 setOperationAction(ISD::FREM , MVT::f64 , Expand);
144 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
145 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
146 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
147 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
148 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
149 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
150 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
151 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
152 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000153 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000154 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000155
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000156 // These should be promoted to a larger select which is supported.
157 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
158 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000159
160 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000161 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
162 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
163 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
164 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
166 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
167 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
168 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
169 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000170 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000171 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000172 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000173 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000174 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000175 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000176 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000177 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000178 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
179 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
180 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000181 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000182 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
183 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000184
Chris Lattnerf73bae12005-11-29 06:16:21 +0000185 // We don't have line number support yet.
186 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000187 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000188 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000189 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000190 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000191
Nate Begemanacc398c2006-01-25 18:21:52 +0000192 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
193 setOperationAction(ISD::VASTART , MVT::Other, Custom);
194
195 // Use the default implementation.
196 setOperationAction(ISD::VAARG , MVT::Other, Expand);
197 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
198 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000199 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
200 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
201 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000202
Chris Lattner9601a862006-03-05 05:08:37 +0000203 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
204 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
205
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000206 if (X86ScalarSSE) {
207 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000208 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
209 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000210
Evan Cheng223547a2006-01-31 22:28:30 +0000211 // Use ANDPD to simulate FABS.
212 setOperationAction(ISD::FABS , MVT::f64, Custom);
213 setOperationAction(ISD::FABS , MVT::f32, Custom);
214
215 // Use XORP to simulate FNEG.
216 setOperationAction(ISD::FNEG , MVT::f64, Custom);
217 setOperationAction(ISD::FNEG , MVT::f32, Custom);
218
Evan Chengd25e9e82006-02-02 00:28:23 +0000219 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 setOperationAction(ISD::FSIN , MVT::f64, Expand);
221 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 setOperationAction(ISD::FREM , MVT::f64, Expand);
223 setOperationAction(ISD::FSIN , MVT::f32, Expand);
224 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f32, Expand);
226
Chris Lattnera54aa942006-01-29 06:26:08 +0000227 // Expand FP immediates into loads from the stack, except for the special
228 // cases we handle.
229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231 addLegalFPImmediate(+0.0); // xorps / xorpd
232 } else {
233 // Set up the FP register classes.
234 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000235
236 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
237
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 if (!UnsafeFPMath) {
239 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
240 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
241 }
242
Chris Lattnera54aa942006-01-29 06:26:08 +0000243 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244 addLegalFPImmediate(+0.0); // FLD0
245 addLegalFPImmediate(+1.0); // FLD1
246 addLegalFPImmediate(-0.0); // FLD0/FCHS
247 addLegalFPImmediate(-1.0); // FLD1/FCHS
248 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000249
Evan Chengd30bf012006-03-01 01:11:20 +0000250 // First set operation action for all vector types to expand. Then we
251 // will selectively turn on ones that can be effectively codegen'd.
252 for (unsigned VT = (unsigned)MVT::Vector + 1;
253 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000258 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000259 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000260 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000261 }
262
Evan Chenga88973f2006-03-22 19:22:18 +0000263 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000264 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
266 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
267
Evan Chengd30bf012006-03-01 01:11:20 +0000268 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000269 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
271 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000272 }
273
Evan Chenga88973f2006-03-22 19:22:18 +0000274 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000275 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
276
Evan Cheng2c3ae372006-04-12 21:21:57 +0000277 setOperationAction(ISD::AND, MVT::v4f32, Legal);
278 setOperationAction(ISD::OR, MVT::v4f32, Legal);
279 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000280 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
281 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
282 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
283 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
284 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
285 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000286 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000287 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000288 }
289
Evan Chenga88973f2006-03-22 19:22:18 +0000290 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000291 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
292 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
293 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
295 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
296
Evan Chengf7c378e2006-04-10 07:23:14 +0000297 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
298 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
299 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
300 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
301 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
302 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
303 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
304 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000305 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000306 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000307
Evan Chengf7c378e2006-04-10 07:23:14 +0000308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000310 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000311 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
312 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
313 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000314
Evan Cheng2c3ae372006-04-12 21:21:57 +0000315 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
316 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
317 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
318 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
319 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
320 }
321 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
322 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
323 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
324 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
325 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
326 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
327
328 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
329 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
330 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
331 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
332 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
333 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
334 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
335 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000336 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
337 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000338 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
339 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000340 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000341
342 // Custom lower v2i64 and v2f64 selects.
343 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000344 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000345 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000346 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000347 }
348
Evan Cheng6be2c582006-04-05 23:38:46 +0000349 // We want to custom lower some of our intrinsics.
350 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
351
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000352 computeRegisterProperties();
353
Evan Cheng87ed7162006-02-14 08:25:08 +0000354 // FIXME: These should be based on subtarget info. Plus, the values should
355 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000356 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
357 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
358 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000359 allowUnalignedMemoryAccesses = true; // x86 supports it!
360}
361
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000362//===----------------------------------------------------------------------===//
363// C Calling Convention implementation
364//===----------------------------------------------------------------------===//
365
Evan Cheng85e38002006-04-27 05:35:28 +0000366/// AddLiveIn - This helper function adds the specified physical register to the
367/// MachineFunction as a live in value. It also creates a corresponding virtual
368/// register for it.
369static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
370 TargetRegisterClass *RC) {
371 assert(RC->contains(PReg) && "Not the correct regclass!");
372 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
373 MF.addLiveIn(PReg, VReg);
374 return VReg;
375}
376
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000377/// HowToPassCCCArgument - Returns how an formal argument of the specified type
378/// should be passed. If it is through stack, returns the size of the stack
Evan Chengf9ff7c52006-05-26 18:25:43 +0000379/// slot; if it is through XMM register, returns the number of XMM registers
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000380/// are needed.
381static void
382HowToPassCCCArgument(MVT::ValueType ObjectVT, unsigned NumXMMRegs,
383 unsigned &ObjSize, unsigned &ObjXMMRegs) {
Evan Cheng26755342006-06-01 05:53:27 +0000384 ObjXMMRegs = 0;
Evan Chengcc1fc222006-05-25 23:31:23 +0000385
Evan Chengeda65fa2006-04-27 01:32:22 +0000386 switch (ObjectVT) {
387 default: assert(0 && "Unhandled argument type!");
Evan Chengeda65fa2006-04-27 01:32:22 +0000388 case MVT::i8: ObjSize = 1; break;
389 case MVT::i16: ObjSize = 2; break;
390 case MVT::i32: ObjSize = 4; break;
391 case MVT::i64: ObjSize = 8; break;
392 case MVT::f32: ObjSize = 4; break;
393 case MVT::f64: ObjSize = 8; break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000394 case MVT::v16i8:
395 case MVT::v8i16:
396 case MVT::v4i32:
397 case MVT::v2i64:
398 case MVT::v4f32:
399 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000400 if (NumXMMRegs < 4)
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000401 ObjXMMRegs = 1;
402 else
403 ObjSize = 16;
404 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000405 }
Evan Chengeda65fa2006-04-27 01:32:22 +0000406}
407
Evan Cheng25caf632006-05-23 21:06:34 +0000408SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
409 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000410 MachineFunction &MF = DAG.getMachineFunction();
411 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000412 SDOperand Root = Op.getOperand(0);
413 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000414
Evan Chengeda65fa2006-04-27 01:32:22 +0000415 // Add DAG nodes to load the arguments... On entry to a function on the X86,
416 // the stack frame looks like this:
417 //
418 // [ESP] -- return address
419 // [ESP + 4] -- first argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +0000420 // [ESP + 8] -- second argument, if first argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +0000421 // ...
422 //
Evan Cheng1bc78042006-04-26 01:20:17 +0000423 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000424 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000425 static const unsigned XMMArgRegs[] = {
426 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
427 };
Evan Cheng1bc78042006-04-26 01:20:17 +0000428 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +0000429 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
430 unsigned ArgIncrement = 4;
431 unsigned ObjSize = 0;
432 unsigned ObjXMMRegs = 0;
433 HowToPassCCCArgument(ObjectVT, NumXMMRegs, ObjSize, ObjXMMRegs);
Evan Cheng052fb512006-05-26 18:39:59 +0000434 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +0000435 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +0000436
Evan Cheng25caf632006-05-23 21:06:34 +0000437 SDOperand ArgValue;
438 if (ObjXMMRegs) {
439 // Passed in a XMM register.
440 unsigned Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000441 X86::VR128RegisterClass);
Evan Cheng25caf632006-05-23 21:06:34 +0000442 ArgValue= DAG.getCopyFromReg(Root, Reg, ObjectVT);
443 ArgValues.push_back(ArgValue);
444 NumXMMRegs += ObjXMMRegs;
445 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +0000446 // XMM arguments have to be aligned on 16-byte boundary.
447 if (ObjSize == 16)
448 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng25caf632006-05-23 21:06:34 +0000449 // Create the frame index object for this incoming parameter...
450 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
451 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
452 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
453 DAG.getSrcValue(NULL));
454 ArgValues.push_back(ArgValue);
455 ArgOffset += ArgIncrement; // Move on to the next argument...
Evan Cheng1bc78042006-04-26 01:20:17 +0000456 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000457 }
458
Evan Cheng25caf632006-05-23 21:06:34 +0000459 ArgValues.push_back(Root);
460
Evan Cheng1bc78042006-04-26 01:20:17 +0000461 // If the function takes variable number of arguments, make a frame index for
462 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000463 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
464 if (isVarArg)
Evan Cheng1bc78042006-04-26 01:20:17 +0000465 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
466 ReturnAddrIndex = 0; // No return address slot generated yet.
467 BytesToPopOnReturn = 0; // Callee pops nothing.
468 BytesCallerReserves = ArgOffset;
Evan Cheng25caf632006-05-23 21:06:34 +0000469
Chris Lattner2d297092006-05-23 18:50:38 +0000470 // If this is a struct return on Darwin/X86, the callee pops the hidden struct
471 // pointer.
Evan Cheng25caf632006-05-23 21:06:34 +0000472 if (MF.getFunction()->getCallingConv() == CallingConv::CSRet &&
Chris Lattner2d297092006-05-23 18:50:38 +0000473 Subtarget->isTargetDarwin())
474 BytesToPopOnReturn = 4;
Evan Cheng1bc78042006-04-26 01:20:17 +0000475
Evan Cheng25caf632006-05-23 21:06:34 +0000476 // Return the new list of results.
477 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
478 Op.Val->value_end());
479 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000480}
481
Evan Cheng32fe1032006-05-25 00:59:30 +0000482
483SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG) {
484 SDOperand Chain = Op.getOperand(0);
485 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
486 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
487 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
488 SDOperand Callee = Op.getOperand(4);
489 MVT::ValueType RetVT= Op.Val->getValueType(0);
490 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000491
Evan Cheng347d5f72006-04-28 21:29:37 +0000492 // Keep track of the number of XMM regs passed so far.
493 unsigned NumXMMRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000494 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000495 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000496 };
Evan Cheng347d5f72006-04-28 21:29:37 +0000497
Evan Cheng32fe1032006-05-25 00:59:30 +0000498 // Count how many bytes are to be pushed on the stack.
499 unsigned NumBytes = 0;
500 for (unsigned i = 0; i != NumOps; ++i) {
501 SDOperand Arg = Op.getOperand(5+2*i);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000502
Evan Cheng32fe1032006-05-25 00:59:30 +0000503 switch (Arg.getValueType()) {
504 default: assert(0 && "Unexpected ValueType for argument!");
505 case MVT::i8:
506 case MVT::i16:
507 case MVT::i32:
508 case MVT::f32:
509 NumBytes += 4;
510 break;
511 case MVT::i64:
512 case MVT::f64:
513 NumBytes += 8;
514 break;
515 case MVT::v16i8:
516 case MVT::v8i16:
517 case MVT::v4i32:
518 case MVT::v2i64:
519 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000520 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000521 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +0000522 ++NumXMMRegs;
Evan Cheng3fddf242006-05-26 20:37:47 +0000523 else {
524 // XMM arguments have to be aligned on 16-byte boundary.
525 NumBytes = ((NumBytes + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +0000526 NumBytes += 16;
Evan Cheng3fddf242006-05-26 20:37:47 +0000527 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000528 break;
529 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000530 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000531
Evan Cheng32fe1032006-05-25 00:59:30 +0000532 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000533
Evan Cheng32fe1032006-05-25 00:59:30 +0000534 // Arguments go on the stack in reverse order, as specified by the ABI.
535 unsigned ArgOffset = 0;
536 NumXMMRegs = 0;
537 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
538 std::vector<SDOperand> MemOpChains;
539 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
540 for (unsigned i = 0; i != NumOps; ++i) {
541 SDOperand Arg = Op.getOperand(5+2*i);
542
543 switch (Arg.getValueType()) {
544 default: assert(0 && "Unexpected ValueType for argument!");
545 case MVT::i8:
Evan Cheng6b5783d2006-05-25 18:56:34 +0000546 case MVT::i16: {
Evan Cheng32fe1032006-05-25 00:59:30 +0000547 // Promote the integer to 32 bits. If the input type is signed use a
548 // sign extend, otherwise use a zero extend.
549 unsigned ExtOp =
550 dyn_cast<ConstantSDNode>(Op.getOperand(5+2*i+1))->getValue() ?
551 ISD::SIGN_EXTEND : ISD::ZERO_EXTEND;
552 Arg = DAG.getNode(ExtOp, MVT::i32, Arg);
Evan Cheng6b5783d2006-05-25 18:56:34 +0000553 }
554 // Fallthrough
Evan Cheng32fe1032006-05-25 00:59:30 +0000555
556 case MVT::i32:
557 case MVT::f32: {
558 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
559 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
560 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
561 Arg, PtrOff, DAG.getSrcValue(NULL)));
562 ArgOffset += 4;
563 break;
564 }
565 case MVT::i64:
566 case MVT::f64: {
567 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
568 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
569 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
570 Arg, PtrOff, DAG.getSrcValue(NULL)));
571 ArgOffset += 8;
572 break;
573 }
574 case MVT::v16i8:
575 case MVT::v8i16:
576 case MVT::v4i32:
577 case MVT::v2i64:
578 case MVT::v4f32:
Evan Cheng25e71d12006-05-25 22:38:31 +0000579 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000580 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000581 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
582 NumXMMRegs++;
583 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +0000584 // XMM arguments have to be aligned on 16-byte boundary.
585 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng347d5f72006-04-28 21:29:37 +0000586 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +0000587 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
588 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
589 Arg, PtrOff, DAG.getSrcValue(NULL)));
590 ArgOffset += 16;
Evan Cheng347d5f72006-04-28 21:29:37 +0000591 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000592 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000593 }
594
Evan Cheng32fe1032006-05-25 00:59:30 +0000595 if (!MemOpChains.empty())
596 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000597
Evan Cheng347d5f72006-04-28 21:29:37 +0000598 // Build a sequence of copy-to-reg nodes chained together with token chain
599 // and flag operands which copy the outgoing args into registers.
600 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000601 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
602 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
603 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000604 InFlag = Chain.getValue(1);
605 }
606
Evan Cheng32fe1032006-05-25 00:59:30 +0000607 // If the callee is a GlobalAddress node (quite common, every direct call is)
608 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
609 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
610 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
611 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
612 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
613
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000614 std::vector<MVT::ValueType> NodeTys;
615 NodeTys.push_back(MVT::Other); // Returns a chain
616 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
617 std::vector<SDOperand> Ops;
618 Ops.push_back(Chain);
619 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +0000620
621 // Add argument registers to the end of the list so that they are known live
622 // into the call.
623 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
624 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
625 RegsToPass[i].second.getValueType()));
626
Evan Cheng347d5f72006-04-28 21:29:37 +0000627 if (InFlag.Val)
628 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000629
Evan Cheng32fe1032006-05-25 00:59:30 +0000630 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
631 NodeTys, Ops);
Evan Cheng347d5f72006-04-28 21:29:37 +0000632 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000633
Chris Lattner2d297092006-05-23 18:50:38 +0000634 // Create the CALLSEQ_END node.
635 unsigned NumBytesForCalleeToPush = 0;
636
637 // If this is is a call to a struct-return function on Darwin/X86, the callee
638 // pops the hidden struct pointer, so we have to push it back.
639 if (CallingConv == CallingConv::CSRet && Subtarget->isTargetDarwin())
640 NumBytesForCalleeToPush = 4;
641
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000642 NodeTys.clear();
643 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +0000644 if (RetVT != MVT::Other)
645 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000646 Ops.clear();
647 Ops.push_back(Chain);
648 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +0000649 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000650 Ops.push_back(InFlag);
651 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng32fe1032006-05-25 00:59:30 +0000652 if (RetVT != MVT::Other)
653 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000654
Evan Cheng32fe1032006-05-25 00:59:30 +0000655 std::vector<SDOperand> ResultVals;
656 NodeTys.clear();
657 switch (RetVT) {
658 default: assert(0 && "Unknown value type to return!");
659 case MVT::Other: break;
660 case MVT::i8:
661 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
662 ResultVals.push_back(Chain.getValue(0));
663 NodeTys.push_back(MVT::i8);
664 break;
665 case MVT::i16:
666 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
667 ResultVals.push_back(Chain.getValue(0));
668 NodeTys.push_back(MVT::i16);
669 break;
670 case MVT::i32:
671 if (Op.Val->getValueType(1) == MVT::i32) {
672 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
673 ResultVals.push_back(Chain.getValue(0));
674 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
675 Chain.getValue(2)).getValue(1);
676 ResultVals.push_back(Chain.getValue(0));
677 NodeTys.push_back(MVT::i32);
678 } else {
679 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
680 ResultVals.push_back(Chain.getValue(0));
Evan Chengd90eb7f2006-01-05 00:27:02 +0000681 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000682 NodeTys.push_back(MVT::i32);
683 break;
684 case MVT::v16i8:
685 case MVT::v8i16:
686 case MVT::v4i32:
687 case MVT::v2i64:
688 case MVT::v4f32:
689 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +0000690 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
691 ResultVals.push_back(Chain.getValue(0));
692 NodeTys.push_back(RetVT);
693 break;
694 case MVT::f32:
695 case MVT::f64: {
696 std::vector<MVT::ValueType> Tys;
697 Tys.push_back(MVT::f64);
698 Tys.push_back(MVT::Other);
699 Tys.push_back(MVT::Flag);
700 std::vector<SDOperand> Ops;
701 Ops.push_back(Chain);
702 Ops.push_back(InFlag);
703 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
704 Chain = RetVal.getValue(1);
705 InFlag = RetVal.getValue(2);
706 if (X86ScalarSSE) {
707 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
708 // shouldn't be necessary except that RFP cannot be live across
709 // multiple blocks. When stackifier is fixed, they can be uncoupled.
710 MachineFunction &MF = DAG.getMachineFunction();
711 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
712 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
713 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000714 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +0000715 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000716 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +0000717 Ops.push_back(RetVal);
718 Ops.push_back(StackSlot);
719 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000720 Ops.push_back(InFlag);
Evan Cheng32fe1032006-05-25 00:59:30 +0000721 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
722 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
723 DAG.getSrcValue(NULL));
Evan Cheng347d5f72006-04-28 21:29:37 +0000724 Chain = RetVal.getValue(1);
Evan Cheng347d5f72006-04-28 21:29:37 +0000725 }
Evan Cheng32fe1032006-05-25 00:59:30 +0000726
727 if (RetVT == MVT::f32 && !X86ScalarSSE)
728 // FIXME: we would really like to remember that this FP_ROUND
729 // operation is okay to eliminate if we allow excess FP precision.
730 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
731 ResultVals.push_back(RetVal);
732 NodeTys.push_back(RetVT);
733 break;
734 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000735 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000736
Evan Cheng32fe1032006-05-25 00:59:30 +0000737 // If the function returns void, just return the chain.
738 if (ResultVals.empty())
739 return Chain;
740
741 // Otherwise, merge everything together with a MERGE_VALUES node.
742 NodeTys.push_back(MVT::Other);
743 ResultVals.push_back(Chain);
744 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
745 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000746}
747
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000748//===----------------------------------------------------------------------===//
749// Fast Calling Convention implementation
750//===----------------------------------------------------------------------===//
751//
752// The X86 'fast' calling convention passes up to two integer arguments in
753// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
754// and requires that the callee pop its arguments off the stack (allowing proper
755// tail calls), and has the same return value conventions as C calling convs.
756//
757// This calling convention always arranges for the callee pop value to be 8n+4
758// bytes, which is needed for tail recursion elimination and stack alignment
759// reasons.
760//
761// Note that this can be enhanced in the future to pass fp vals in registers
762// (when we have a global fp allocator) and do other tricks.
763//
764
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000765/// HowToPassFastCCArgument - Returns how an formal argument of the specified
766/// type should be passed. If it is through stack, returns the size of the stack
Evan Chengf9ff7c52006-05-26 18:25:43 +0000767/// slot; if it is through integer or XMM register, returns the number of
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000768/// integer or XMM registers are needed.
Evan Chengeda65fa2006-04-27 01:32:22 +0000769static void
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000770HowToPassFastCCArgument(MVT::ValueType ObjectVT,
771 unsigned NumIntRegs, unsigned NumXMMRegs,
772 unsigned &ObjSize, unsigned &ObjIntRegs,
773 unsigned &ObjXMMRegs) {
Evan Chengeda65fa2006-04-27 01:32:22 +0000774 ObjSize = 0;
Evan Cheng26755342006-06-01 05:53:27 +0000775 ObjIntRegs = 0;
776 ObjXMMRegs = 0;
Evan Chengeda65fa2006-04-27 01:32:22 +0000777
778 switch (ObjectVT) {
779 default: assert(0 && "Unhandled argument type!");
Evan Chengeda65fa2006-04-27 01:32:22 +0000780 case MVT::i8:
Evan Chengda08d2c2006-06-24 08:36:10 +0000781#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +0000782 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000783 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000784 else
Evan Chengda08d2c2006-06-24 08:36:10 +0000785#endif
Evan Chengeda65fa2006-04-27 01:32:22 +0000786 ObjSize = 1;
787 break;
788 case MVT::i16:
Evan Chengda08d2c2006-06-24 08:36:10 +0000789#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +0000790 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000791 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000792 else
Evan Chengda08d2c2006-06-24 08:36:10 +0000793#endif
Evan Chengeda65fa2006-04-27 01:32:22 +0000794 ObjSize = 2;
795 break;
796 case MVT::i32:
Evan Chengda08d2c2006-06-24 08:36:10 +0000797#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +0000798 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS)
Evan Cheng85e38002006-04-27 05:35:28 +0000799 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000800 else
Evan Chengda08d2c2006-06-24 08:36:10 +0000801#endif
Evan Chengeda65fa2006-04-27 01:32:22 +0000802 ObjSize = 4;
803 break;
804 case MVT::i64:
Evan Chengda08d2c2006-06-24 08:36:10 +0000805#if FASTCC_NUM_INT_ARGS_INREGS > 0
Evan Chengeda65fa2006-04-27 01:32:22 +0000806 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +0000807 ObjIntRegs = 2;
Evan Chengeda65fa2006-04-27 01:32:22 +0000808 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng85e38002006-04-27 05:35:28 +0000809 ObjIntRegs = 1;
Evan Chengeda65fa2006-04-27 01:32:22 +0000810 ObjSize = 4;
811 } else
Evan Chengda08d2c2006-06-24 08:36:10 +0000812#endif
Evan Chengeda65fa2006-04-27 01:32:22 +0000813 ObjSize = 8;
814 case MVT::f32:
815 ObjSize = 4;
816 break;
817 case MVT::f64:
818 ObjSize = 8;
819 break;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000820 case MVT::v16i8:
821 case MVT::v8i16:
822 case MVT::v4i32:
823 case MVT::v2i64:
824 case MVT::v4f32:
825 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000826 if (NumXMMRegs < 4)
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000827 ObjXMMRegs = 1;
828 else
829 ObjSize = 16;
830 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000831 }
832}
833
Evan Cheng25caf632006-05-23 21:06:34 +0000834SDOperand
835X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
836 unsigned NumArgs = Op.Val->getNumValues()-1;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000837 MachineFunction &MF = DAG.getMachineFunction();
838 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000839 SDOperand Root = Op.getOperand(0);
840 std::vector<SDOperand> ArgValues;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000841
Evan Chengeda65fa2006-04-27 01:32:22 +0000842 // Add DAG nodes to load the arguments... On entry to a function the stack
843 // frame looks like this:
844 //
845 // [ESP] -- return address
846 // [ESP + 4] -- first nonreg argument (leftmost lexically)
Evan Chengf9d62dc2006-05-26 18:37:16 +0000847 // [ESP + 8] -- second nonreg argument, if 1st argument is <= 4 bytes in size
Evan Chengeda65fa2006-04-27 01:32:22 +0000848 // ...
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000849 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
850
851 // Keep track of the number of integer regs passed so far. This can be either
852 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
853 // used).
854 unsigned NumIntRegs = 0;
Evan Cheng2fdd95e2006-04-27 08:31:10 +0000855 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Evan Cheng32fe1032006-05-25 00:59:30 +0000856
857 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +0000858 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +0000859 };
Chris Lattner1c636e92006-03-17 05:10:20 +0000860
Evan Cheng1bc78042006-04-26 01:20:17 +0000861 for (unsigned i = 0; i < NumArgs; ++i) {
Evan Cheng25caf632006-05-23 21:06:34 +0000862 MVT::ValueType ObjectVT = Op.getValue(i).getValueType();
863 unsigned ArgIncrement = 4;
864 unsigned ObjSize = 0;
865 unsigned ObjIntRegs = 0;
866 unsigned ObjXMMRegs = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000867
Evan Cheng25caf632006-05-23 21:06:34 +0000868 HowToPassFastCCArgument(ObjectVT, NumIntRegs, NumXMMRegs,
869 ObjSize, ObjIntRegs, ObjXMMRegs);
Evan Cheng052fb512006-05-26 18:39:59 +0000870 if (ObjSize > 4)
Evan Cheng25caf632006-05-23 21:06:34 +0000871 ArgIncrement = ObjSize;
Evan Chengeda65fa2006-04-27 01:32:22 +0000872
Evan Cheng04b25622006-06-01 00:30:39 +0000873 unsigned Reg = 0;
Evan Cheng25caf632006-05-23 21:06:34 +0000874 SDOperand ArgValue;
875 if (ObjIntRegs || ObjXMMRegs) {
876 switch (ObjectVT) {
877 default: assert(0 && "Unhandled argument type!");
Evan Cheng25caf632006-05-23 21:06:34 +0000878 case MVT::i8:
879 Reg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
880 X86::GR8RegisterClass);
881 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i8);
882 break;
883 case MVT::i16:
884 Reg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
885 X86::GR16RegisterClass);
886 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i16);
887 break;
888 case MVT::i32:
889 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
890 X86::GR32RegisterClass);
891 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
892 break;
893 case MVT::i64:
894 Reg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
895 X86::GR32RegisterClass);
896 ArgValue = DAG.getCopyFromReg(Root, Reg, MVT::i32);
897 if (ObjIntRegs == 2) {
898 Reg = AddLiveIn(MF, X86::EDX, X86::GR32RegisterClass);
899 SDOperand ArgValue2 = DAG.getCopyFromReg(Root, Reg, MVT::i32);
900 ArgValue= DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
Evan Cheng85e38002006-04-27 05:35:28 +0000901 }
Evan Cheng25caf632006-05-23 21:06:34 +0000902 break;
903 case MVT::v16i8:
904 case MVT::v8i16:
905 case MVT::v4i32:
906 case MVT::v2i64:
907 case MVT::v4f32:
908 case MVT::v2f64:
909 Reg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs], X86::VR128RegisterClass);
910 ArgValue = DAG.getCopyFromReg(Root, Reg, ObjectVT);
911 break;
Evan Chengeda65fa2006-04-27 01:32:22 +0000912 }
Evan Cheng25caf632006-05-23 21:06:34 +0000913 NumIntRegs += ObjIntRegs;
914 NumXMMRegs += ObjXMMRegs;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000915 }
Evan Cheng25caf632006-05-23 21:06:34 +0000916
917 if (ObjSize) {
Evan Cheng3fddf242006-05-26 20:37:47 +0000918 // XMM arguments have to be aligned on 16-byte boundary.
919 if (ObjSize == 16)
920 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng25caf632006-05-23 21:06:34 +0000921 // Create the SelectionDAG nodes corresponding to a load from this
922 // parameter.
923 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
924 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
925 if (ObjectVT == MVT::i64 && ObjIntRegs) {
926 SDOperand ArgValue2 = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
927 DAG.getSrcValue(NULL));
928 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, ArgValue, ArgValue2);
929 } else
930 ArgValue = DAG.getLoad(Op.Val->getValueType(i), Root, FIN,
931 DAG.getSrcValue(NULL));
932 ArgOffset += ArgIncrement; // Move on to the next argument.
933 }
934
935 ArgValues.push_back(ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000936 }
937
Evan Cheng25caf632006-05-23 21:06:34 +0000938 ArgValues.push_back(Root);
939
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000940 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
941 // arguments and the arguments after the retaddr has been pushed are aligned.
942 if ((ArgOffset & 7) == 0)
943 ArgOffset += 4;
944
945 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
946 ReturnAddrIndex = 0; // No return address slot generated yet.
947 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
948 BytesCallerReserves = 0;
949
950 // Finally, inform the code generator which regs we return values in.
Evan Cheng25caf632006-05-23 21:06:34 +0000951 switch (getValueType(MF.getFunction()->getReturnType())) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000952 default: assert(0 && "Unknown type!");
953 case MVT::isVoid: break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000954 case MVT::i8:
955 case MVT::i16:
956 case MVT::i32:
957 MF.addLiveOut(X86::EAX);
958 break;
959 case MVT::i64:
960 MF.addLiveOut(X86::EAX);
961 MF.addLiveOut(X86::EDX);
962 break;
963 case MVT::f32:
964 case MVT::f64:
965 MF.addLiveOut(X86::ST0);
966 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +0000967 case MVT::v16i8:
968 case MVT::v8i16:
969 case MVT::v4i32:
970 case MVT::v2i64:
971 case MVT::v4f32:
972 case MVT::v2f64:
Evan Cheng347d5f72006-04-28 21:29:37 +0000973 MF.addLiveOut(X86::XMM0);
974 break;
975 }
Evan Cheng347d5f72006-04-28 21:29:37 +0000976
Evan Cheng25caf632006-05-23 21:06:34 +0000977 // Return the new list of results.
978 std::vector<MVT::ValueType> RetVTs(Op.Val->value_begin(),
979 Op.Val->value_end());
980 return DAG.getNode(ISD::MERGE_VALUES, RetVTs, ArgValues);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000981}
982
Evan Chengb69d1132006-06-14 18:17:40 +0000983SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000984 SDOperand Chain = Op.getOperand(0);
985 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
986 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
987 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
988 SDOperand Callee = Op.getOperand(4);
989 MVT::ValueType RetVT= Op.Val->getValueType(0);
990 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
991
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000992 // Count how many bytes are to be pushed on the stack.
993 unsigned NumBytes = 0;
994
995 // Keep track of the number of integer regs passed so far. This can be either
996 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
997 // used).
998 unsigned NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000999 unsigned NumXMMRegs = 0; // XMM regs used for parameter passing.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001000
Evan Cheng32fe1032006-05-25 00:59:30 +00001001 static const unsigned GPRArgRegs[][2] = {
1002 { X86::AL, X86::DL },
1003 { X86::AX, X86::DX },
1004 { X86::EAX, X86::EDX }
1005 };
1006 static const unsigned XMMArgRegs[] = {
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001007 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
Evan Cheng32fe1032006-05-25 00:59:30 +00001008 };
1009
1010 for (unsigned i = 0; i != NumOps; ++i) {
1011 SDOperand Arg = Op.getOperand(5+2*i);
1012
1013 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001014 default: assert(0 && "Unknown value type!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001015 case MVT::i8:
1016 case MVT::i16:
1017 case MVT::i32:
Evan Chengda08d2c2006-06-24 08:36:10 +00001018#if FASTCC_NUM_INT_ARGS_INREGS > 0
Chris Lattner1c636e92006-03-17 05:10:20 +00001019 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001020 ++NumIntRegs;
1021 break;
1022 }
Evan Chengda08d2c2006-06-24 08:36:10 +00001023#endif
Evan Cheng25e71d12006-05-25 22:38:31 +00001024 // Fall through
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001025 case MVT::f32:
1026 NumBytes += 4;
1027 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001028 case MVT::f64:
1029 NumBytes += 8;
1030 break;
Evan Cheng32fe1032006-05-25 00:59:30 +00001031 case MVT::v16i8:
1032 case MVT::v8i16:
1033 case MVT::v4i32:
1034 case MVT::v2i64:
1035 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001036 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001037 if (NumXMMRegs < 4)
Evan Cheng32fe1032006-05-25 00:59:30 +00001038 NumXMMRegs++;
Evan Cheng3fddf242006-05-26 20:37:47 +00001039 else {
1040 // XMM arguments have to be aligned on 16-byte boundary.
1041 NumBytes = ((NumBytes + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +00001042 NumBytes += 16;
Evan Cheng3fddf242006-05-26 20:37:47 +00001043 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001044 break;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001045 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001046 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001047
1048 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1049 // arguments and the arguments after the retaddr has been pushed are aligned.
1050 if ((NumBytes & 7) == 0)
1051 NumBytes += 4;
1052
Chris Lattner94dd2922006-02-13 09:00:43 +00001053 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001054
1055 // Arguments go on the stack in reverse order, as specified by the ABI.
1056 unsigned ArgOffset = 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001057 NumIntRegs = 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001058 std::vector<std::pair<unsigned, SDOperand> > RegsToPass;
1059 std::vector<SDOperand> MemOpChains;
1060 SDOperand StackPtr = DAG.getRegister(X86::ESP, getPointerTy());
1061 for (unsigned i = 0; i != NumOps; ++i) {
1062 SDOperand Arg = Op.getOperand(5+2*i);
1063
1064 switch (Arg.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001065 default: assert(0 && "Unexpected ValueType for argument!");
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001066 case MVT::i8:
1067 case MVT::i16:
1068 case MVT::i32:
Evan Chengda08d2c2006-06-24 08:36:10 +00001069#if FASTCC_NUM_INT_ARGS_INREGS > 0
Chris Lattner1c636e92006-03-17 05:10:20 +00001070 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001071 RegsToPass.push_back(
1072 std::make_pair(GPRArgRegs[Arg.getValueType()-MVT::i8][NumIntRegs],
1073 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001074 ++NumIntRegs;
1075 break;
1076 }
Evan Chengda08d2c2006-06-24 08:36:10 +00001077#endif
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001078 // Fall through
1079 case MVT::f32: {
1080 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001081 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1082 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1083 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001084 ArgOffset += 4;
1085 break;
1086 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001087 case MVT::f64: {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001088 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001089 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1090 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1091 Arg, PtrOff, DAG.getSrcValue(NULL)));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001092 ArgOffset += 8;
1093 break;
1094 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001095 case MVT::v16i8:
1096 case MVT::v8i16:
1097 case MVT::v4i32:
1098 case MVT::v2i64:
1099 case MVT::v4f32:
Evan Cheng6b5783d2006-05-25 18:56:34 +00001100 case MVT::v2f64:
Evan Cheng1d6a9b32006-05-26 19:22:06 +00001101 if (NumXMMRegs < 4) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001102 RegsToPass.push_back(std::make_pair(XMMArgRegs[NumXMMRegs], Arg));
1103 NumXMMRegs++;
1104 } else {
Evan Cheng3fddf242006-05-26 20:37:47 +00001105 // XMM arguments have to be aligned on 16-byte boundary.
1106 ArgOffset = ((ArgOffset + 15) / 16) * 16;
Evan Cheng32fe1032006-05-25 00:59:30 +00001107 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1108 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1109 MemOpChains.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1110 Arg, PtrOff, DAG.getSrcValue(NULL)));
1111 ArgOffset += 16;
1112 }
1113 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001114 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001115
Evan Cheng32fe1032006-05-25 00:59:30 +00001116 if (!MemOpChains.empty())
1117 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, MemOpChains);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001119 // Build a sequence of copy-to-reg nodes chained together with token chain
1120 // and flag operands which copy the outgoing args into registers.
1121 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001122 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1123 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1124 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001125 InFlag = Chain.getValue(1);
1126 }
1127
Evan Cheng32fe1032006-05-25 00:59:30 +00001128 // If the callee is a GlobalAddress node (quite common, every direct call is)
1129 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1130 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1131 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1132 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
1133 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1134
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001135 std::vector<MVT::ValueType> NodeTys;
1136 NodeTys.push_back(MVT::Other); // Returns a chain
1137 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1138 std::vector<SDOperand> Ops;
1139 Ops.push_back(Chain);
1140 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001141
1142 // Add argument registers to the end of the list so that they are known live
1143 // into the call.
1144 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1145 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1146 RegsToPass[i].second.getValueType()));
1147
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001148 if (InFlag.Val)
1149 Ops.push_back(InFlag);
1150
1151 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001152 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1153 NodeTys, Ops);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001154 InFlag = Chain.getValue(1);
1155
1156 NodeTys.clear();
1157 NodeTys.push_back(MVT::Other); // Returns a chain
Evan Cheng32fe1032006-05-25 00:59:30 +00001158 if (RetVT != MVT::Other)
1159 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001160 Ops.clear();
1161 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001162 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1163 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001164 Ops.push_back(InFlag);
1165 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
Evan Cheng32fe1032006-05-25 00:59:30 +00001166 if (RetVT != MVT::Other)
1167 InFlag = Chain.getValue(1);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001168
Evan Cheng32fe1032006-05-25 00:59:30 +00001169 std::vector<SDOperand> ResultVals;
1170 NodeTys.clear();
1171 switch (RetVT) {
1172 default: assert(0 && "Unknown value type to return!");
1173 case MVT::Other: break;
1174 case MVT::i8:
1175 Chain = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag).getValue(1);
1176 ResultVals.push_back(Chain.getValue(0));
1177 NodeTys.push_back(MVT::i8);
1178 break;
1179 case MVT::i16:
1180 Chain = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag).getValue(1);
1181 ResultVals.push_back(Chain.getValue(0));
1182 NodeTys.push_back(MVT::i16);
1183 break;
1184 case MVT::i32:
1185 if (Op.Val->getValueType(1) == MVT::i32) {
1186 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1187 ResultVals.push_back(Chain.getValue(0));
1188 Chain = DAG.getCopyFromReg(Chain, X86::EDX, MVT::i32,
1189 Chain.getValue(2)).getValue(1);
1190 ResultVals.push_back(Chain.getValue(0));
1191 NodeTys.push_back(MVT::i32);
1192 } else {
1193 Chain = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag).getValue(1);
1194 ResultVals.push_back(Chain.getValue(0));
Evan Chengd9558e02006-01-06 00:43:03 +00001195 }
Evan Cheng32fe1032006-05-25 00:59:30 +00001196 NodeTys.push_back(MVT::i32);
1197 break;
1198 case MVT::v16i8:
1199 case MVT::v8i16:
1200 case MVT::v4i32:
1201 case MVT::v2i64:
1202 case MVT::v4f32:
1203 case MVT::v2f64:
Evan Cheng32fe1032006-05-25 00:59:30 +00001204 Chain = DAG.getCopyFromReg(Chain, X86::XMM0, RetVT, InFlag).getValue(1);
1205 ResultVals.push_back(Chain.getValue(0));
1206 NodeTys.push_back(RetVT);
1207 break;
1208 case MVT::f32:
1209 case MVT::f64: {
1210 std::vector<MVT::ValueType> Tys;
1211 Tys.push_back(MVT::f64);
1212 Tys.push_back(MVT::Other);
1213 Tys.push_back(MVT::Flag);
1214 std::vector<SDOperand> Ops;
1215 Ops.push_back(Chain);
1216 Ops.push_back(InFlag);
1217 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1218 Chain = RetVal.getValue(1);
1219 InFlag = RetVal.getValue(2);
1220 if (X86ScalarSSE) {
1221 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1222 // shouldn't be necessary except that RFP cannot be live across
1223 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1224 MachineFunction &MF = DAG.getMachineFunction();
1225 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1226 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1227 Tys.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001228 Tys.push_back(MVT::Other);
Evan Cheng32fe1032006-05-25 00:59:30 +00001229 Ops.clear();
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001230 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001231 Ops.push_back(RetVal);
1232 Ops.push_back(StackSlot);
1233 Ops.push_back(DAG.getValueType(RetVT));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001234 Ops.push_back(InFlag);
Evan Cheng32fe1032006-05-25 00:59:30 +00001235 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1236 RetVal = DAG.getLoad(RetVT, Chain, StackSlot,
1237 DAG.getSrcValue(NULL));
1238 Chain = RetVal.getValue(1);
1239 }
Evan Chengd9558e02006-01-06 00:43:03 +00001240
Evan Cheng32fe1032006-05-25 00:59:30 +00001241 if (RetVT == MVT::f32 && !X86ScalarSSE)
1242 // FIXME: we would really like to remember that this FP_ROUND
1243 // operation is okay to eliminate if we allow excess FP precision.
1244 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1245 ResultVals.push_back(RetVal);
1246 NodeTys.push_back(RetVT);
1247 break;
1248 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001249 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001250
Evan Cheng32fe1032006-05-25 00:59:30 +00001251
1252 // If the function returns void, just return the chain.
1253 if (ResultVals.empty())
1254 return Chain;
1255
1256 // Otherwise, merge everything together with a MERGE_VALUES node.
1257 NodeTys.push_back(MVT::Other);
1258 ResultVals.push_back(Chain);
1259 SDOperand Res = DAG.getNode(ISD::MERGE_VALUES, NodeTys, ResultVals);
1260 return Res.getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001261}
1262
1263SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1264 if (ReturnAddrIndex == 0) {
1265 // Set up a frame object for the return address.
1266 MachineFunction &MF = DAG.getMachineFunction();
1267 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1268 }
1269
1270 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1271}
1272
1273
1274
1275std::pair<SDOperand, SDOperand> X86TargetLowering::
1276LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1277 SelectionDAG &DAG) {
1278 SDOperand Result;
1279 if (Depth) // Depths > 0 not supported yet!
1280 Result = DAG.getConstant(0, getPointerTy());
1281 else {
1282 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1283 if (!isFrameAddress)
1284 // Just load the return address
1285 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1286 DAG.getSrcValue(NULL));
1287 else
1288 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1289 DAG.getConstant(4, MVT::i32));
1290 }
1291 return std::make_pair(Result, Chain);
1292}
1293
Evan Cheng4a460802006-01-11 00:33:36 +00001294/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1295/// which corresponds to the condition code.
1296static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1297 switch (X86CC) {
1298 default: assert(0 && "Unknown X86 conditional code!");
1299 case X86ISD::COND_A: return X86::JA;
1300 case X86ISD::COND_AE: return X86::JAE;
1301 case X86ISD::COND_B: return X86::JB;
1302 case X86ISD::COND_BE: return X86::JBE;
1303 case X86ISD::COND_E: return X86::JE;
1304 case X86ISD::COND_G: return X86::JG;
1305 case X86ISD::COND_GE: return X86::JGE;
1306 case X86ISD::COND_L: return X86::JL;
1307 case X86ISD::COND_LE: return X86::JLE;
1308 case X86ISD::COND_NE: return X86::JNE;
1309 case X86ISD::COND_NO: return X86::JNO;
1310 case X86ISD::COND_NP: return X86::JNP;
1311 case X86ISD::COND_NS: return X86::JNS;
1312 case X86ISD::COND_O: return X86::JO;
1313 case X86ISD::COND_P: return X86::JP;
1314 case X86ISD::COND_S: return X86::JS;
1315 }
1316}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001317
Evan Cheng6dfa9992006-01-30 23:41:35 +00001318/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1319/// specific condition code. It returns a false if it cannot do a direct
1320/// translation. X86CC is the translated CondCode. Flip is set to true if the
1321/// the order of comparison operands should be flipped.
Evan Cheng6be2c582006-04-05 23:38:46 +00001322static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1323 unsigned &X86CC, bool &Flip) {
Evan Cheng6dfa9992006-01-30 23:41:35 +00001324 Flip = false;
1325 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001326 if (!isFP) {
1327 switch (SetCCOpcode) {
1328 default: break;
1329 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1330 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1331 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1332 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1333 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1334 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1335 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1336 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1337 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1338 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1339 }
1340 } else {
1341 // On a floating point condition, the flags are set as follows:
1342 // ZF PF CF op
1343 // 0 | 0 | 0 | X > Y
1344 // 0 | 0 | 1 | X < Y
1345 // 1 | 0 | 0 | X == Y
1346 // 1 | 1 | 1 | unordered
1347 switch (SetCCOpcode) {
1348 default: break;
1349 case ISD::SETUEQ:
1350 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001351 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001352 case ISD::SETOGT:
1353 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001354 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001355 case ISD::SETOGE:
1356 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001357 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001358 case ISD::SETULT:
1359 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001360 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001361 case ISD::SETULE:
1362 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1363 case ISD::SETONE:
1364 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1365 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1366 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1367 }
1368 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001369
1370 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001371}
1372
Evan Cheng6be2c582006-04-05 23:38:46 +00001373static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1374 bool &Flip) {
1375 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1376}
1377
Evan Cheng4a460802006-01-11 00:33:36 +00001378/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1379/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001380/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001381static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001382 switch (X86CC) {
1383 default:
1384 return false;
1385 case X86ISD::COND_B:
1386 case X86ISD::COND_BE:
1387 case X86ISD::COND_E:
1388 case X86ISD::COND_P:
1389 case X86ISD::COND_A:
1390 case X86ISD::COND_AE:
1391 case X86ISD::COND_NE:
1392 case X86ISD::COND_NP:
1393 return true;
1394 }
1395}
1396
Evan Cheng4a460802006-01-11 00:33:36 +00001397MachineBasicBlock *
1398X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1399 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001400 switch (MI->getOpcode()) {
1401 default: assert(false && "Unexpected instr type to insert");
1402 case X86::CMOV_FR32:
Evan Chengf7c378e2006-04-10 07:23:14 +00001403 case X86::CMOV_FR64:
1404 case X86::CMOV_V4F32:
1405 case X86::CMOV_V2F64:
1406 case X86::CMOV_V2I64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001407 // To "insert" a SELECT_CC instruction, we actually have to insert the
1408 // diamond control-flow pattern. The incoming instruction knows the
1409 // destination vreg to set, the condition code register to branch on, the
1410 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001411 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1412 ilist<MachineBasicBlock>::iterator It = BB;
1413 ++It;
1414
1415 // thisMBB:
1416 // ...
1417 // TrueVal = ...
1418 // cmpTY ccX, r1, r2
1419 // bCC copy1MBB
1420 // fallthrough --> copy0MBB
1421 MachineBasicBlock *thisMBB = BB;
1422 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1423 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1424 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1425 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1426 MachineFunction *F = BB->getParent();
1427 F->getBasicBlockList().insert(It, copy0MBB);
1428 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001429 // Update machine-CFG edges by first adding all successors of the current
1430 // block to the new block which will contain the Phi node for the select.
1431 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1432 e = BB->succ_end(); i != e; ++i)
1433 sinkMBB->addSuccessor(*i);
1434 // Next, remove all successors of the current block, and add the true
1435 // and fallthrough blocks as its successors.
1436 while(!BB->succ_empty())
1437 BB->removeSuccessor(BB->succ_begin());
Evan Cheng0cc39452006-01-16 21:21:29 +00001438 BB->addSuccessor(copy0MBB);
1439 BB->addSuccessor(sinkMBB);
1440
1441 // copy0MBB:
1442 // %FalseValue = ...
1443 // # fallthrough to sinkMBB
1444 BB = copy0MBB;
1445
1446 // Update machine-CFG edges
1447 BB->addSuccessor(sinkMBB);
1448
1449 // sinkMBB:
1450 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1451 // ...
1452 BB = sinkMBB;
1453 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1454 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1455 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001456
Evan Cheng0cc39452006-01-16 21:21:29 +00001457 delete MI; // The pseudo instruction is gone now.
1458 return BB;
1459 }
Evan Cheng4a460802006-01-11 00:33:36 +00001460
Evan Cheng0cc39452006-01-16 21:21:29 +00001461 case X86::FP_TO_INT16_IN_MEM:
1462 case X86::FP_TO_INT32_IN_MEM:
1463 case X86::FP_TO_INT64_IN_MEM: {
1464 // Change the floating point control register to use "round towards zero"
1465 // mode when truncating to an integer value.
1466 MachineFunction *F = BB->getParent();
1467 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1468 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1469
1470 // Load the old value of the high byte of the control word...
1471 unsigned OldCW =
Evan Cheng069287d2006-05-16 07:21:53 +00001472 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Cheng0cc39452006-01-16 21:21:29 +00001473 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1474
1475 // Set the high part to be round to zero...
1476 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1477
1478 // Reload the modified control word now...
1479 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1480
1481 // Restore the memory image of control word to original value
1482 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1483
1484 // Get the X86 opcode to use.
1485 unsigned Opc;
1486 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001487 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001488 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1489 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1490 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1491 }
1492
1493 X86AddressMode AM;
1494 MachineOperand &Op = MI->getOperand(0);
1495 if (Op.isRegister()) {
1496 AM.BaseType = X86AddressMode::RegBase;
1497 AM.Base.Reg = Op.getReg();
1498 } else {
1499 AM.BaseType = X86AddressMode::FrameIndexBase;
1500 AM.Base.FrameIndex = Op.getFrameIndex();
1501 }
1502 Op = MI->getOperand(1);
1503 if (Op.isImmediate())
1504 AM.Scale = Op.getImmedValue();
1505 Op = MI->getOperand(2);
1506 if (Op.isImmediate())
1507 AM.IndexReg = Op.getImmedValue();
1508 Op = MI->getOperand(3);
1509 if (Op.isGlobalAddress()) {
1510 AM.GV = Op.getGlobal();
1511 } else {
1512 AM.Disp = Op.getImmedValue();
1513 }
1514 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1515
1516 // Reload the original control word now.
1517 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1518
1519 delete MI; // The pseudo instruction is gone now.
1520 return BB;
1521 }
1522 }
Evan Cheng4a460802006-01-11 00:33:36 +00001523}
1524
1525
1526//===----------------------------------------------------------------------===//
1527// X86 Custom Lowering Hooks
1528//===----------------------------------------------------------------------===//
1529
Evan Cheng30b37b52006-03-13 23:18:16 +00001530/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1531/// load. For Darwin, external and weak symbols are indirect, loading the value
1532/// at address GV rather then the value of GV itself. This means that the
1533/// GlobalAddress must be in the base or index register of the address, not the
1534/// GV offset field.
1535static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1536 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1537 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1538}
1539
Evan Cheng5ced1d82006-04-06 23:23:56 +00001540/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001541/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001542static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1543 if (Op.getOpcode() == ISD::UNDEF)
1544 return true;
1545
1546 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001547 return (Val >= Low && Val < Hi);
1548}
1549
1550/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1551/// true if Op is undef or if its value equal to the specified value.
1552static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1553 if (Op.getOpcode() == ISD::UNDEF)
1554 return true;
1555 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001556}
1557
Evan Cheng0188ecb2006-03-22 18:59:22 +00001558/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1559/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1560bool X86::isPSHUFDMask(SDNode *N) {
1561 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1562
1563 if (N->getNumOperands() != 4)
1564 return false;
1565
1566 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001567 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001568 SDOperand Arg = N->getOperand(i);
1569 if (Arg.getOpcode() == ISD::UNDEF) continue;
1570 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1571 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00001572 return false;
1573 }
1574
1575 return true;
1576}
1577
1578/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001579/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001580bool X86::isPSHUFHWMask(SDNode *N) {
1581 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1582
1583 if (N->getNumOperands() != 8)
1584 return false;
1585
1586 // Lower quadword copied in order.
1587 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001588 SDOperand Arg = N->getOperand(i);
1589 if (Arg.getOpcode() == ISD::UNDEF) continue;
1590 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1591 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001592 return false;
1593 }
1594
1595 // Upper quadword shuffled.
1596 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001597 SDOperand Arg = N->getOperand(i);
1598 if (Arg.getOpcode() == ISD::UNDEF) continue;
1599 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1600 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001601 if (Val < 4 || Val > 7)
1602 return false;
1603 }
1604
1605 return true;
1606}
1607
1608/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001609/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001610bool X86::isPSHUFLWMask(SDNode *N) {
1611 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1612
1613 if (N->getNumOperands() != 8)
1614 return false;
1615
1616 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001617 for (unsigned i = 4; i != 8; ++i)
1618 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001619 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001620
1621 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001622 for (unsigned i = 0; i != 4; ++i)
1623 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001624 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001625
1626 return true;
1627}
1628
Evan Cheng14aed5e2006-03-24 01:18:28 +00001629/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1630/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng39623da2006-04-20 08:58:49 +00001631static bool isSHUFPMask(std::vector<SDOperand> &N) {
1632 unsigned NumElems = N.size();
1633 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001634
Evan Cheng39623da2006-04-20 08:58:49 +00001635 unsigned Half = NumElems / 2;
1636 for (unsigned i = 0; i < Half; ++i)
1637 if (!isUndefOrInRange(N[i], 0, NumElems))
1638 return false;
1639 for (unsigned i = Half; i < NumElems; ++i)
1640 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1641 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001642
1643 return true;
1644}
1645
Evan Cheng39623da2006-04-20 08:58:49 +00001646bool X86::isSHUFPMask(SDNode *N) {
1647 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1648 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1649 return ::isSHUFPMask(Ops);
1650}
1651
1652/// isCommutedSHUFP - Returns true if the shuffle mask is except
1653/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1654/// half elements to come from vector 1 (which would equal the dest.) and
1655/// the upper half to come from vector 2.
1656static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1657 unsigned NumElems = Ops.size();
1658 if (NumElems != 2 && NumElems != 4) return false;
1659
1660 unsigned Half = NumElems / 2;
1661 for (unsigned i = 0; i < Half; ++i)
1662 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1663 return false;
1664 for (unsigned i = Half; i < NumElems; ++i)
1665 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1666 return false;
1667 return true;
1668}
1669
1670static bool isCommutedSHUFP(SDNode *N) {
1671 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1672 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1673 return isCommutedSHUFP(Ops);
1674}
1675
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001676/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1677/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1678bool X86::isMOVHLPSMask(SDNode *N) {
1679 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1680
Evan Cheng2064a2b2006-03-28 06:50:32 +00001681 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001682 return false;
1683
Evan Cheng2064a2b2006-03-28 06:50:32 +00001684 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001685 return isUndefOrEqual(N->getOperand(0), 6) &&
1686 isUndefOrEqual(N->getOperand(1), 7) &&
1687 isUndefOrEqual(N->getOperand(2), 2) &&
1688 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001689}
1690
Evan Cheng5ced1d82006-04-06 23:23:56 +00001691/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1692/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1693bool X86::isMOVLPMask(SDNode *N) {
1694 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1695
1696 unsigned NumElems = N->getNumOperands();
1697 if (NumElems != 2 && NumElems != 4)
1698 return false;
1699
Evan Chengc5cdff22006-04-07 21:53:05 +00001700 for (unsigned i = 0; i < NumElems/2; ++i)
1701 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1702 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001703
Evan Chengc5cdff22006-04-07 21:53:05 +00001704 for (unsigned i = NumElems/2; i < NumElems; ++i)
1705 if (!isUndefOrEqual(N->getOperand(i), i))
1706 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001707
1708 return true;
1709}
1710
1711/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00001712/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1713/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00001714bool X86::isMOVHPMask(SDNode *N) {
1715 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1716
1717 unsigned NumElems = N->getNumOperands();
1718 if (NumElems != 2 && NumElems != 4)
1719 return false;
1720
Evan Chengc5cdff22006-04-07 21:53:05 +00001721 for (unsigned i = 0; i < NumElems/2; ++i)
1722 if (!isUndefOrEqual(N->getOperand(i), i))
1723 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001724
1725 for (unsigned i = 0; i < NumElems/2; ++i) {
1726 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001727 if (!isUndefOrEqual(Arg, i + NumElems))
1728 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001729 }
1730
1731 return true;
1732}
1733
Evan Cheng0038e592006-03-28 00:39:58 +00001734/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1735/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng39623da2006-04-20 08:58:49 +00001736bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1737 unsigned NumElems = N.size();
Evan Cheng0038e592006-03-28 00:39:58 +00001738 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1739 return false;
1740
1741 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001742 SDOperand BitI = N[i];
1743 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001744 if (!isUndefOrEqual(BitI, j))
1745 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001746 if (V2IsSplat) {
1747 if (isUndefOrEqual(BitI1, NumElems))
1748 return false;
1749 } else {
1750 if (!isUndefOrEqual(BitI1, j + NumElems))
1751 return false;
1752 }
Evan Cheng0038e592006-03-28 00:39:58 +00001753 }
1754
1755 return true;
1756}
1757
Evan Cheng39623da2006-04-20 08:58:49 +00001758bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1759 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1760 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1761 return ::isUNPCKLMask(Ops, V2IsSplat);
1762}
1763
Evan Cheng4fcb9222006-03-28 02:43:26 +00001764/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1765/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng39623da2006-04-20 08:58:49 +00001766bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1767 unsigned NumElems = N.size();
Evan Cheng4fcb9222006-03-28 02:43:26 +00001768 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1769 return false;
1770
1771 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng39623da2006-04-20 08:58:49 +00001772 SDOperand BitI = N[i];
1773 SDOperand BitI1 = N[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001774 if (!isUndefOrEqual(BitI, j + NumElems/2))
1775 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001776 if (V2IsSplat) {
1777 if (isUndefOrEqual(BitI1, NumElems))
1778 return false;
1779 } else {
1780 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1781 return false;
1782 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001783 }
1784
1785 return true;
1786}
1787
Evan Cheng39623da2006-04-20 08:58:49 +00001788bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1789 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1790 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1791 return ::isUNPCKHMask(Ops, V2IsSplat);
1792}
1793
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001794/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1795/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1796/// <0, 0, 1, 1>
1797bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1798 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1799
1800 unsigned NumElems = N->getNumOperands();
1801 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1802 return false;
1803
1804 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1805 SDOperand BitI = N->getOperand(i);
1806 SDOperand BitI1 = N->getOperand(i+1);
1807
Evan Chengc5cdff22006-04-07 21:53:05 +00001808 if (!isUndefOrEqual(BitI, j))
1809 return false;
1810 if (!isUndefOrEqual(BitI1, j))
1811 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001812 }
1813
1814 return true;
1815}
1816
Evan Cheng017dcc62006-04-21 01:05:10 +00001817/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1818/// specifies a shuffle of elements that is suitable for input to MOVSS,
1819/// MOVSD, and MOVD, i.e. setting the lowest element.
1820static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001821 unsigned NumElems = N.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001822 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001823 return false;
1824
Evan Cheng39623da2006-04-20 08:58:49 +00001825 if (!isUndefOrEqual(N[0], NumElems))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001826 return false;
1827
1828 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00001829 SDOperand Arg = N[i];
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001830 if (!isUndefOrEqual(Arg, i))
1831 return false;
1832 }
1833
1834 return true;
1835}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001836
Evan Cheng017dcc62006-04-21 01:05:10 +00001837bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001838 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1839 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001840 return ::isMOVLMask(Ops);
Evan Cheng39623da2006-04-20 08:58:49 +00001841}
1842
Evan Cheng017dcc62006-04-21 01:05:10 +00001843/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1844/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00001845/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Cheng017dcc62006-04-21 01:05:10 +00001846static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001847 unsigned NumElems = Ops.size();
Evan Cheng017dcc62006-04-21 01:05:10 +00001848 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00001849 return false;
1850
1851 if (!isUndefOrEqual(Ops[0], 0))
1852 return false;
1853
1854 for (unsigned i = 1; i < NumElems; ++i) {
1855 SDOperand Arg = Ops[i];
1856 if (V2IsSplat) {
1857 if (!isUndefOrEqual(Arg, NumElems))
1858 return false;
1859 } else {
1860 if (!isUndefOrEqual(Arg, i+NumElems))
1861 return false;
1862 }
1863 }
1864
1865 return true;
1866}
1867
Evan Cheng017dcc62006-04-21 01:05:10 +00001868static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001869 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1870 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Cheng017dcc62006-04-21 01:05:10 +00001871 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001872}
1873
Evan Chengd9539472006-04-14 21:59:03 +00001874/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1875/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1876bool X86::isMOVSHDUPMask(SDNode *N) {
1877 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1878
1879 if (N->getNumOperands() != 4)
1880 return false;
1881
1882 // Expect 1, 1, 3, 3
1883 for (unsigned i = 0; i < 2; ++i) {
1884 SDOperand Arg = N->getOperand(i);
1885 if (Arg.getOpcode() == ISD::UNDEF) continue;
1886 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1887 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1888 if (Val != 1) return false;
1889 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001890
1891 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001892 for (unsigned i = 2; i < 4; ++i) {
1893 SDOperand Arg = N->getOperand(i);
1894 if (Arg.getOpcode() == ISD::UNDEF) continue;
1895 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1896 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1897 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001898 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001899 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001900
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001901 // Don't use movshdup if it can be done with a shufps.
1902 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001903}
1904
1905/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1906/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1907bool X86::isMOVSLDUPMask(SDNode *N) {
1908 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1909
1910 if (N->getNumOperands() != 4)
1911 return false;
1912
1913 // Expect 0, 0, 2, 2
1914 for (unsigned i = 0; i < 2; ++i) {
1915 SDOperand Arg = N->getOperand(i);
1916 if (Arg.getOpcode() == ISD::UNDEF) continue;
1917 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1918 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1919 if (Val != 0) return false;
1920 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001921
1922 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001923 for (unsigned i = 2; i < 4; ++i) {
1924 SDOperand Arg = N->getOperand(i);
1925 if (Arg.getOpcode() == ISD::UNDEF) continue;
1926 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1927 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1928 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001929 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001930 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001931
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001932 // Don't use movshdup if it can be done with a shufps.
1933 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001934}
1935
Evan Chengb9df0ca2006-03-22 02:53:00 +00001936/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1937/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00001938static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001939 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1940
Evan Chengb9df0ca2006-03-22 02:53:00 +00001941 // This is a splat operation if each element of the permute is the same, and
1942 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001943 unsigned NumElems = N->getNumOperands();
1944 SDOperand ElementBase;
1945 unsigned i = 0;
1946 for (; i != NumElems; ++i) {
1947 SDOperand Elt = N->getOperand(i);
1948 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1949 ElementBase = Elt;
1950 break;
1951 }
1952 }
1953
1954 if (!ElementBase.Val)
1955 return false;
1956
1957 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001958 SDOperand Arg = N->getOperand(i);
1959 if (Arg.getOpcode() == ISD::UNDEF) continue;
1960 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001961 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001962 }
1963
1964 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001965 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001966}
1967
Evan Chengc575ca22006-04-17 20:43:08 +00001968/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1969/// a splat of a single element and it's a 2 or 4 element mask.
1970bool X86::isSplatMask(SDNode *N) {
1971 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1972
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001973 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00001974 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1975 return false;
1976 return ::isSplatMask(N);
1977}
1978
Evan Cheng63d33002006-03-22 08:01:21 +00001979/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1980/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1981/// instructions.
1982unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001983 unsigned NumOperands = N->getNumOperands();
1984 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1985 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001986 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001987 unsigned Val = 0;
1988 SDOperand Arg = N->getOperand(NumOperands-i-1);
1989 if (Arg.getOpcode() != ISD::UNDEF)
1990 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00001991 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001992 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00001993 if (i != NumOperands - 1)
1994 Mask <<= Shift;
1995 }
Evan Cheng63d33002006-03-22 08:01:21 +00001996
1997 return Mask;
1998}
1999
Evan Cheng506d3df2006-03-29 23:07:14 +00002000/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2001/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2002/// instructions.
2003unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2004 unsigned Mask = 0;
2005 // 8 nodes, but we only care about the last 4.
2006 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002007 unsigned Val = 0;
2008 SDOperand Arg = N->getOperand(i);
2009 if (Arg.getOpcode() != ISD::UNDEF)
2010 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002011 Mask |= (Val - 4);
2012 if (i != 4)
2013 Mask <<= 2;
2014 }
2015
2016 return Mask;
2017}
2018
2019/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2020/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2021/// instructions.
2022unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2023 unsigned Mask = 0;
2024 // 8 nodes, but we only care about the first 4.
2025 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002026 unsigned Val = 0;
2027 SDOperand Arg = N->getOperand(i);
2028 if (Arg.getOpcode() != ISD::UNDEF)
2029 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002030 Mask |= Val;
2031 if (i != 0)
2032 Mask <<= 2;
2033 }
2034
2035 return Mask;
2036}
2037
Evan Chengc21a0532006-04-05 01:47:37 +00002038/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2039/// specifies a 8 element shuffle that can be broken into a pair of
2040/// PSHUFHW and PSHUFLW.
2041static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2042 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2043
2044 if (N->getNumOperands() != 8)
2045 return false;
2046
2047 // Lower quadword shuffled.
2048 for (unsigned i = 0; i != 4; ++i) {
2049 SDOperand Arg = N->getOperand(i);
2050 if (Arg.getOpcode() == ISD::UNDEF) continue;
2051 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2052 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2053 if (Val > 4)
2054 return false;
2055 }
2056
2057 // Upper quadword shuffled.
2058 for (unsigned i = 4; i != 8; ++i) {
2059 SDOperand Arg = N->getOperand(i);
2060 if (Arg.getOpcode() == ISD::UNDEF) continue;
2061 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2062 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2063 if (Val < 4 || Val > 7)
2064 return false;
2065 }
2066
2067 return true;
2068}
2069
Evan Cheng5ced1d82006-04-06 23:23:56 +00002070/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2071/// values in ther permute mask.
2072static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2073 SDOperand V1 = Op.getOperand(0);
2074 SDOperand V2 = Op.getOperand(1);
2075 SDOperand Mask = Op.getOperand(2);
2076 MVT::ValueType VT = Op.getValueType();
2077 MVT::ValueType MaskVT = Mask.getValueType();
2078 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2079 unsigned NumElems = Mask.getNumOperands();
2080 std::vector<SDOperand> MaskVec;
2081
2082 for (unsigned i = 0; i != NumElems; ++i) {
2083 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002084 if (Arg.getOpcode() == ISD::UNDEF) {
2085 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2086 continue;
2087 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002088 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2089 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2090 if (Val < NumElems)
2091 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2092 else
2093 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2094 }
2095
2096 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2097 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2098}
2099
Evan Cheng533a0aa2006-04-19 20:35:22 +00002100/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2101/// match movhlps. The lower half elements should come from upper half of
2102/// V1 (and in order), and the upper half elements should come from the upper
2103/// half of V2 (and in order).
2104static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2105 unsigned NumElems = Mask->getNumOperands();
2106 if (NumElems != 4)
2107 return false;
2108 for (unsigned i = 0, e = 2; i != e; ++i)
2109 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2110 return false;
2111 for (unsigned i = 2; i != 4; ++i)
2112 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2113 return false;
2114 return true;
2115}
2116
Evan Cheng5ced1d82006-04-06 23:23:56 +00002117/// isScalarLoadToVector - Returns true if the node is a scalar load that
2118/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002119static inline bool isScalarLoadToVector(SDNode *N) {
2120 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2121 N = N->getOperand(0).Val;
2122 return (N->getOpcode() == ISD::LOAD);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002123 }
2124 return false;
2125}
2126
Evan Cheng533a0aa2006-04-19 20:35:22 +00002127/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2128/// match movlp{s|d}. The lower half elements should come from lower half of
2129/// V1 (and in order), and the upper half elements should come from the upper
2130/// half of V2 (and in order). And since V1 will become the source of the
2131/// MOVLP, it must be either a vector load or a scalar load to vector.
2132static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2133 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2134 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002135
Evan Cheng533a0aa2006-04-19 20:35:22 +00002136 unsigned NumElems = Mask->getNumOperands();
2137 if (NumElems != 2 && NumElems != 4)
2138 return false;
2139 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2140 if (!isUndefOrEqual(Mask->getOperand(i), i))
2141 return false;
2142 for (unsigned i = NumElems/2; i != NumElems; ++i)
2143 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2144 return false;
2145 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002146}
2147
Evan Cheng39623da2006-04-20 08:58:49 +00002148/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2149/// all the same.
2150static bool isSplatVector(SDNode *N) {
2151 if (N->getOpcode() != ISD::BUILD_VECTOR)
2152 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002153
Evan Cheng39623da2006-04-20 08:58:49 +00002154 SDOperand SplatValue = N->getOperand(0);
2155 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2156 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002157 return false;
2158 return true;
2159}
2160
Evan Cheng39623da2006-04-20 08:58:49 +00002161/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2162/// that point to V2 points to its first element.
2163static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2164 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2165
2166 bool Changed = false;
2167 std::vector<SDOperand> MaskVec;
2168 unsigned NumElems = Mask.getNumOperands();
2169 for (unsigned i = 0; i != NumElems; ++i) {
2170 SDOperand Arg = Mask.getOperand(i);
2171 if (Arg.getOpcode() != ISD::UNDEF) {
2172 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2173 if (Val > NumElems) {
2174 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2175 Changed = true;
2176 }
2177 }
2178 MaskVec.push_back(Arg);
2179 }
2180
2181 if (Changed)
2182 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2183 return Mask;
2184}
2185
Evan Cheng017dcc62006-04-21 01:05:10 +00002186/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2187/// operation of specified width.
2188static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002189 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2190 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2191
2192 std::vector<SDOperand> MaskVec;
2193 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2194 for (unsigned i = 1; i != NumElems; ++i)
2195 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2196 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2197}
2198
Evan Chengc575ca22006-04-17 20:43:08 +00002199/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2200/// of specified width.
2201static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2202 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2203 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2204 std::vector<SDOperand> MaskVec;
2205 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2206 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2207 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2208 }
2209 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2210}
2211
Evan Cheng39623da2006-04-20 08:58:49 +00002212/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2213/// of specified width.
2214static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2215 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2216 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2217 unsigned Half = NumElems/2;
2218 std::vector<SDOperand> MaskVec;
2219 for (unsigned i = 0; i != Half; ++i) {
2220 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2221 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2222 }
2223 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2224}
2225
Evan Cheng017dcc62006-04-21 01:05:10 +00002226/// getZeroVector - Returns a vector of specified type with all zero elements.
2227///
2228static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2229 assert(MVT::isVector(VT) && "Expected a vector type");
2230 unsigned NumElems = getVectorNumElements(VT);
2231 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2232 bool isFP = MVT::isFloatingPoint(EVT);
2233 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2234 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2235 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2236}
2237
Evan Chengc575ca22006-04-17 20:43:08 +00002238/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2239///
2240static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2241 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002242 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002243 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002244 unsigned NumElems = Mask.getNumOperands();
2245 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002246 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002247 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002248 NumElems >>= 1;
2249 }
2250 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2251
2252 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002253 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002254 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002255 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002256 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2257}
2258
Evan Cheng017dcc62006-04-21 01:05:10 +00002259/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2260/// constant +0.0.
2261static inline bool isZeroNode(SDOperand Elt) {
2262 return ((isa<ConstantSDNode>(Elt) &&
2263 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2264 (isa<ConstantFPSDNode>(Elt) &&
2265 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2266}
2267
Evan Chengba05f722006-04-21 23:03:30 +00002268/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2269/// vector and zero or undef vector.
2270static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002271 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002272 bool isZero, SelectionDAG &DAG) {
2273 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002274 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2275 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2276 SDOperand Zero = DAG.getConstant(0, EVT);
2277 std::vector<SDOperand> MaskVec(NumElems, Zero);
2278 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2279 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Chengba05f722006-04-21 23:03:30 +00002280 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002281}
2282
Evan Chengc78d3b42006-04-24 18:01:45 +00002283/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2284///
2285static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2286 unsigned NumNonZero, unsigned NumZero,
2287 SelectionDAG &DAG) {
2288 if (NumNonZero > 8)
2289 return SDOperand();
2290
2291 SDOperand V(0, 0);
2292 bool First = true;
2293 for (unsigned i = 0; i < 16; ++i) {
2294 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2295 if (ThisIsNonZero && First) {
2296 if (NumZero)
2297 V = getZeroVector(MVT::v8i16, DAG);
2298 else
2299 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2300 First = false;
2301 }
2302
2303 if ((i & 1) != 0) {
2304 SDOperand ThisElt(0, 0), LastElt(0, 0);
2305 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2306 if (LastIsNonZero) {
2307 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2308 }
2309 if (ThisIsNonZero) {
2310 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2311 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2312 ThisElt, DAG.getConstant(8, MVT::i8));
2313 if (LastIsNonZero)
2314 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2315 } else
2316 ThisElt = LastElt;
2317
2318 if (ThisElt.Val)
2319 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2320 DAG.getConstant(i/2, MVT::i32));
2321 }
2322 }
2323
2324 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2325}
2326
2327/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2328///
2329static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2330 unsigned NumNonZero, unsigned NumZero,
2331 SelectionDAG &DAG) {
2332 if (NumNonZero > 4)
2333 return SDOperand();
2334
2335 SDOperand V(0, 0);
2336 bool First = true;
2337 for (unsigned i = 0; i < 8; ++i) {
2338 bool isNonZero = (NonZeros & (1 << i)) != 0;
2339 if (isNonZero) {
2340 if (First) {
2341 if (NumZero)
2342 V = getZeroVector(MVT::v8i16, DAG);
2343 else
2344 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2345 First = false;
2346 }
2347 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2348 DAG.getConstant(i, MVT::i32));
2349 }
2350 }
2351
2352 return V;
2353}
2354
Evan Cheng0db9fe62006-04-25 20:13:52 +00002355SDOperand
2356X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2357 // All zero's are handled with pxor.
2358 if (ISD::isBuildVectorAllZeros(Op.Val))
2359 return Op;
2360
2361 // All one's are handled with pcmpeqd.
2362 if (ISD::isBuildVectorAllOnes(Op.Val))
2363 return Op;
2364
2365 MVT::ValueType VT = Op.getValueType();
2366 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2367 unsigned EVTBits = MVT::getSizeInBits(EVT);
2368
2369 unsigned NumElems = Op.getNumOperands();
2370 unsigned NumZero = 0;
2371 unsigned NumNonZero = 0;
2372 unsigned NonZeros = 0;
2373 std::set<SDOperand> Values;
2374 for (unsigned i = 0; i < NumElems; ++i) {
2375 SDOperand Elt = Op.getOperand(i);
2376 if (Elt.getOpcode() != ISD::UNDEF) {
2377 Values.insert(Elt);
2378 if (isZeroNode(Elt))
2379 NumZero++;
2380 else {
2381 NonZeros |= (1 << i);
2382 NumNonZero++;
2383 }
2384 }
2385 }
2386
2387 if (NumNonZero == 0)
2388 // Must be a mix of zero and undef. Return a zero vector.
2389 return getZeroVector(VT, DAG);
2390
2391 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2392 if (Values.size() == 1)
2393 return SDOperand();
2394
2395 // Special case for single non-zero element.
2396 if (NumNonZero == 1) {
2397 unsigned Idx = CountTrailingZeros_32(NonZeros);
2398 SDOperand Item = Op.getOperand(Idx);
2399 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2400 if (Idx == 0)
2401 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2402 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2403 NumZero > 0, DAG);
2404
2405 if (EVTBits == 32) {
2406 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2407 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2408 DAG);
2409 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2410 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2411 std::vector<SDOperand> MaskVec;
2412 for (unsigned i = 0; i < NumElems; i++)
2413 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2414 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2415 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2416 DAG.getNode(ISD::UNDEF, VT), Mask);
2417 }
2418 }
2419
2420 // Let legalizer expand 2-widde build_vector's.
2421 if (EVTBits == 64)
2422 return SDOperand();
2423
2424 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2425 if (EVTBits == 8) {
2426 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2427 if (V.Val) return V;
2428 }
2429
2430 if (EVTBits == 16) {
2431 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2432 if (V.Val) return V;
2433 }
2434
2435 // If element VT is == 32 bits, turn it into a number of shuffles.
2436 std::vector<SDOperand> V(NumElems);
2437 if (NumElems == 4 && NumZero > 0) {
2438 for (unsigned i = 0; i < 4; ++i) {
2439 bool isZero = !(NonZeros & (1 << i));
2440 if (isZero)
2441 V[i] = getZeroVector(VT, DAG);
2442 else
2443 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2444 }
2445
2446 for (unsigned i = 0; i < 2; ++i) {
2447 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2448 default: break;
2449 case 0:
2450 V[i] = V[i*2]; // Must be a zero vector.
2451 break;
2452 case 1:
2453 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2454 getMOVLMask(NumElems, DAG));
2455 break;
2456 case 2:
2457 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2458 getMOVLMask(NumElems, DAG));
2459 break;
2460 case 3:
2461 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2462 getUnpacklMask(NumElems, DAG));
2463 break;
2464 }
2465 }
2466
Evan Cheng069287d2006-05-16 07:21:53 +00002467 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Evan Cheng0db9fe62006-04-25 20:13:52 +00002468 // clears the upper bits.
2469 // FIXME: we can do the same for v4f32 case when we know both parts of
2470 // the lower half come from scalar_to_vector (loadf32). We should do
2471 // that in post legalizer dag combiner with target specific hooks.
2472 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2473 return V[0];
2474 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2475 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2476 std::vector<SDOperand> MaskVec;
2477 bool Reverse = (NonZeros & 0x3) == 2;
2478 for (unsigned i = 0; i < 2; ++i)
2479 if (Reverse)
2480 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2481 else
2482 MaskVec.push_back(DAG.getConstant(i, EVT));
2483 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2484 for (unsigned i = 0; i < 2; ++i)
2485 if (Reverse)
2486 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2487 else
2488 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2489 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2490 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2491 }
2492
2493 if (Values.size() > 2) {
2494 // Expand into a number of unpckl*.
2495 // e.g. for v4f32
2496 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2497 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2498 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2499 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2500 for (unsigned i = 0; i < NumElems; ++i)
2501 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2502 NumElems >>= 1;
2503 while (NumElems != 0) {
2504 for (unsigned i = 0; i < NumElems; ++i)
2505 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2506 UnpckMask);
2507 NumElems >>= 1;
2508 }
2509 return V[0];
2510 }
2511
2512 return SDOperand();
2513}
2514
2515SDOperand
2516X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2517 SDOperand V1 = Op.getOperand(0);
2518 SDOperand V2 = Op.getOperand(1);
2519 SDOperand PermMask = Op.getOperand(2);
2520 MVT::ValueType VT = Op.getValueType();
2521 unsigned NumElems = PermMask.getNumOperands();
2522 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2523 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2524
2525 if (isSplatMask(PermMask.Val)) {
2526 if (NumElems <= 4) return Op;
2527 // Promote it to a v4i32 splat.
2528 return PromoteSplat(Op, DAG);
2529 }
2530
2531 if (X86::isMOVLMask(PermMask.Val))
2532 return (V1IsUndef) ? V2 : Op;
2533
2534 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2535 X86::isMOVSLDUPMask(PermMask.Val) ||
2536 X86::isMOVHLPSMask(PermMask.Val) ||
2537 X86::isMOVHPMask(PermMask.Val) ||
2538 X86::isMOVLPMask(PermMask.Val))
2539 return Op;
2540
2541 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2542 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2543 return CommuteVectorShuffle(Op, DAG);
2544
2545 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2546 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2547 if (V1IsSplat && !V2IsSplat) {
2548 Op = CommuteVectorShuffle(Op, DAG);
2549 V1 = Op.getOperand(0);
2550 V2 = Op.getOperand(1);
2551 PermMask = Op.getOperand(2);
2552 V2IsSplat = true;
2553 }
2554
2555 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2556 if (V2IsUndef) return V1;
2557 Op = CommuteVectorShuffle(Op, DAG);
2558 V1 = Op.getOperand(0);
2559 V2 = Op.getOperand(1);
2560 PermMask = Op.getOperand(2);
2561 if (V2IsSplat) {
2562 // V2 is a splat, so the mask may be malformed. That is, it may point
2563 // to any V2 element. The instruction selectior won't like this. Get
2564 // a corrected mask and commute to form a proper MOVS{S|D}.
2565 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2566 if (NewMask.Val != PermMask.Val)
2567 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2568 }
2569 return Op;
2570 }
2571
2572 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2573 X86::isUNPCKLMask(PermMask.Val) ||
2574 X86::isUNPCKHMask(PermMask.Val))
2575 return Op;
2576
2577 if (V2IsSplat) {
2578 // Normalize mask so all entries that point to V2 points to its first
2579 // element then try to match unpck{h|l} again. If match, return a
2580 // new vector_shuffle with the corrected mask.
2581 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2582 if (NewMask.Val != PermMask.Val) {
2583 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2584 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2585 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2586 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2587 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2588 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2589 }
2590 }
2591 }
2592
2593 // Normalize the node to match x86 shuffle ops if needed
2594 if (V2.getOpcode() != ISD::UNDEF)
2595 if (isCommutedSHUFP(PermMask.Val)) {
2596 Op = CommuteVectorShuffle(Op, DAG);
2597 V1 = Op.getOperand(0);
2598 V2 = Op.getOperand(1);
2599 PermMask = Op.getOperand(2);
2600 }
2601
2602 // If VT is integer, try PSHUF* first, then SHUFP*.
2603 if (MVT::isInteger(VT)) {
2604 if (X86::isPSHUFDMask(PermMask.Val) ||
2605 X86::isPSHUFHWMask(PermMask.Val) ||
2606 X86::isPSHUFLWMask(PermMask.Val)) {
2607 if (V2.getOpcode() != ISD::UNDEF)
2608 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2609 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2610 return Op;
2611 }
2612
2613 if (X86::isSHUFPMask(PermMask.Val))
2614 return Op;
2615
2616 // Handle v8i16 shuffle high / low shuffle node pair.
2617 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2618 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2619 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2620 std::vector<SDOperand> MaskVec;
2621 for (unsigned i = 0; i != 4; ++i)
2622 MaskVec.push_back(PermMask.getOperand(i));
2623 for (unsigned i = 4; i != 8; ++i)
2624 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2625 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2626 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2627 MaskVec.clear();
2628 for (unsigned i = 0; i != 4; ++i)
2629 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2630 for (unsigned i = 4; i != 8; ++i)
2631 MaskVec.push_back(PermMask.getOperand(i));
2632 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2633 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2634 }
2635 } else {
2636 // Floating point cases in the other order.
2637 if (X86::isSHUFPMask(PermMask.Val))
2638 return Op;
2639 if (X86::isPSHUFDMask(PermMask.Val) ||
2640 X86::isPSHUFHWMask(PermMask.Val) ||
2641 X86::isPSHUFLWMask(PermMask.Val)) {
2642 if (V2.getOpcode() != ISD::UNDEF)
2643 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2644 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2645 return Op;
2646 }
2647 }
2648
2649 if (NumElems == 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002650 MVT::ValueType MaskVT = PermMask.getValueType();
2651 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Evan Cheng43f3bd32006-04-28 07:03:38 +00002652 std::vector<std::pair<int, int> > Locs;
2653 Locs.reserve(NumElems);
2654 std::vector<SDOperand> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2655 std::vector<SDOperand> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2656 unsigned NumHi = 0;
2657 unsigned NumLo = 0;
2658 // If no more than two elements come from either vector. This can be
2659 // implemented with two shuffles. First shuffle gather the elements.
2660 // The second shuffle, which takes the first shuffle as both of its
2661 // vector operands, put the elements into the right order.
2662 for (unsigned i = 0; i != NumElems; ++i) {
2663 SDOperand Elt = PermMask.getOperand(i);
2664 if (Elt.getOpcode() == ISD::UNDEF) {
2665 Locs[i] = std::make_pair(-1, -1);
2666 } else {
2667 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2668 if (Val < NumElems) {
2669 Locs[i] = std::make_pair(0, NumLo);
2670 Mask1[NumLo] = Elt;
2671 NumLo++;
2672 } else {
2673 Locs[i] = std::make_pair(1, NumHi);
2674 if (2+NumHi < NumElems)
2675 Mask1[2+NumHi] = Elt;
2676 NumHi++;
2677 }
2678 }
2679 }
2680 if (NumLo <= 2 && NumHi <= 2) {
2681 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2682 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask1));
2683 for (unsigned i = 0; i != NumElems; ++i) {
2684 if (Locs[i].first == -1)
2685 continue;
2686 else {
2687 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2688 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2689 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2690 }
2691 }
2692
2693 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
2694 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, Mask2));
2695 }
2696
2697 // Break it into (shuffle shuffle_hi, shuffle_lo).
2698 Locs.clear();
Evan Cheng0db9fe62006-04-25 20:13:52 +00002699 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2700 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2701 std::vector<SDOperand> *MaskPtr = &LoMask;
2702 unsigned MaskIdx = 0;
2703 unsigned LoIdx = 0;
2704 unsigned HiIdx = NumElems/2;
2705 for (unsigned i = 0; i != NumElems; ++i) {
2706 if (i == NumElems/2) {
2707 MaskPtr = &HiMask;
2708 MaskIdx = 1;
2709 LoIdx = 0;
2710 HiIdx = NumElems/2;
2711 }
2712 SDOperand Elt = PermMask.getOperand(i);
2713 if (Elt.getOpcode() == ISD::UNDEF) {
2714 Locs[i] = std::make_pair(-1, -1);
2715 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2716 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2717 (*MaskPtr)[LoIdx] = Elt;
2718 LoIdx++;
2719 } else {
2720 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2721 (*MaskPtr)[HiIdx] = Elt;
2722 HiIdx++;
2723 }
2724 }
2725
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002726 SDOperand LoShuffle =
2727 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2728 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2729 SDOperand HiShuffle =
2730 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2731 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002732 std::vector<SDOperand> MaskOps;
2733 for (unsigned i = 0; i != NumElems; ++i) {
2734 if (Locs[i].first == -1) {
2735 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2736 } else {
2737 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2738 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2739 }
2740 }
2741 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2742 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2743 }
2744
2745 return SDOperand();
2746}
2747
2748SDOperand
2749X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2750 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2751 return SDOperand();
2752
2753 MVT::ValueType VT = Op.getValueType();
2754 // TODO: handle v16i8.
2755 if (MVT::getSizeInBits(VT) == 16) {
2756 // Transform it so it match pextrw which produces a 32-bit result.
2757 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2758 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2759 Op.getOperand(0), Op.getOperand(1));
2760 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2761 DAG.getValueType(VT));
2762 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2763 } else if (MVT::getSizeInBits(VT) == 32) {
2764 SDOperand Vec = Op.getOperand(0);
2765 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2766 if (Idx == 0)
2767 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002768 // SHUFPS the element to the lowest double word, then movss.
2769 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002770 std::vector<SDOperand> IdxVec;
2771 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2772 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2773 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2774 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2775 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2776 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2777 Vec, Vec, Mask);
2778 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00002779 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002780 } else if (MVT::getSizeInBits(VT) == 64) {
2781 SDOperand Vec = Op.getOperand(0);
2782 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2783 if (Idx == 0)
2784 return Op;
2785
2786 // UNPCKHPD the element to the lowest double word, then movsd.
2787 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2788 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2789 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2790 std::vector<SDOperand> IdxVec;
2791 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2792 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2793 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2794 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2795 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2796 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00002797 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002798 }
2799
2800 return SDOperand();
2801}
2802
2803SDOperand
2804X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00002805 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00002806 // as its second argument.
2807 MVT::ValueType VT = Op.getValueType();
2808 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2809 SDOperand N0 = Op.getOperand(0);
2810 SDOperand N1 = Op.getOperand(1);
2811 SDOperand N2 = Op.getOperand(2);
2812 if (MVT::getSizeInBits(BaseVT) == 16) {
2813 if (N1.getValueType() != MVT::i32)
2814 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2815 if (N2.getValueType() != MVT::i32)
2816 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2817 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2818 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2819 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2820 if (Idx == 0) {
2821 // Use a movss.
2822 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2823 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2824 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2825 std::vector<SDOperand> MaskVec;
2826 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2827 for (unsigned i = 1; i <= 3; ++i)
2828 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2829 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2830 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2831 } else {
2832 // Use two pinsrw instructions to insert a 32 bit value.
2833 Idx <<= 1;
2834 if (MVT::isFloatingPoint(N1.getValueType())) {
2835 if (N1.getOpcode() == ISD::LOAD) {
Evan Cheng069287d2006-05-16 07:21:53 +00002836 // Just load directly from f32mem to GR32.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002837 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2838 N1.getOperand(2));
2839 } else {
2840 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2841 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2842 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00002843 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002844 }
2845 }
2846 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2847 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00002848 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002849 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2850 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00002851 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002852 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2853 }
2854 }
2855
2856 return SDOperand();
2857}
2858
2859SDOperand
2860X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2861 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2862 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2863}
2864
2865// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2866// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2867// one of the above mentioned nodes. It has to be wrapped because otherwise
2868// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2869// be used to form addressing mode. These wrapped nodes will be selected
2870// into MOV32ri.
2871SDOperand
2872X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2873 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2874 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2875 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2876 CP->getAlignment()));
2877 if (Subtarget->isTargetDarwin()) {
2878 // With PIC, the address is actually $g + Offset.
2879 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2880 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2881 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2882 }
2883
2884 return Result;
2885}
2886
2887SDOperand
2888X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2889 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2890 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002891 DAG.getTargetGlobalAddress(GV,
2892 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002893 if (Subtarget->isTargetDarwin()) {
2894 // With PIC, the address is actually $g + Offset.
2895 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2896 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002897 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2898 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002899
2900 // For Darwin, external and weak symbols are indirect, so we want to load
2901 // the value at address GV, not the value of GV itself. This means that
2902 // the GlobalAddress must be in the base or index register of the address,
2903 // not the GV offset field.
2904 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2905 DarwinGVRequiresExtraLoad(GV))
2906 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2907 Result, DAG.getSrcValue(NULL));
2908 }
2909
2910 return Result;
2911}
2912
2913SDOperand
2914X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2915 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2916 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002917 DAG.getTargetExternalSymbol(Sym,
2918 getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002919 if (Subtarget->isTargetDarwin()) {
2920 // With PIC, the address is actually $g + Offset.
2921 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2922 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002923 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2924 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002925 }
2926
2927 return Result;
2928}
2929
2930SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00002931 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2932 "Not an i64 shift!");
2933 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2934 SDOperand ShOpLo = Op.getOperand(0);
2935 SDOperand ShOpHi = Op.getOperand(1);
2936 SDOperand ShAmt = Op.getOperand(2);
2937 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00002938 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00002939 : DAG.getConstant(0, MVT::i32);
2940
2941 SDOperand Tmp2, Tmp3;
2942 if (Op.getOpcode() == ISD::SHL_PARTS) {
2943 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2944 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2945 } else {
2946 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00002947 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00002948 }
2949
2950 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2951 ShAmt, DAG.getConstant(32, MVT::i8));
2952
2953 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00002954 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00002955
2956 std::vector<MVT::ValueType> Tys;
2957 Tys.push_back(MVT::i32);
2958 Tys.push_back(MVT::Flag);
2959 std::vector<SDOperand> Ops;
2960 if (Op.getOpcode() == ISD::SHL_PARTS) {
2961 Ops.push_back(Tmp2);
2962 Ops.push_back(Tmp3);
2963 Ops.push_back(CC);
2964 Ops.push_back(InFlag);
2965 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2966 InFlag = Hi.getValue(1);
2967
2968 Ops.clear();
2969 Ops.push_back(Tmp3);
2970 Ops.push_back(Tmp1);
2971 Ops.push_back(CC);
2972 Ops.push_back(InFlag);
2973 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2974 } else {
2975 Ops.push_back(Tmp2);
2976 Ops.push_back(Tmp3);
2977 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00002978 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00002979 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2980 InFlag = Lo.getValue(1);
2981
2982 Ops.clear();
2983 Ops.push_back(Tmp3);
2984 Ops.push_back(Tmp1);
2985 Ops.push_back(CC);
2986 Ops.push_back(InFlag);
2987 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2988 }
2989
2990 Tys.clear();
2991 Tys.push_back(MVT::i32);
2992 Tys.push_back(MVT::i32);
2993 Ops.clear();
2994 Ops.push_back(Lo);
2995 Ops.push_back(Hi);
2996 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002997}
Evan Chenga3195e82006-01-12 22:54:21 +00002998
Evan Cheng0db9fe62006-04-25 20:13:52 +00002999SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3000 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3001 Op.getOperand(0).getValueType() >= MVT::i16 &&
3002 "Unknown SINT_TO_FP to lower!");
3003
3004 SDOperand Result;
3005 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3006 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3007 MachineFunction &MF = DAG.getMachineFunction();
3008 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3009 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3010 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
3011 DAG.getEntryNode(), Op.getOperand(0),
3012 StackSlot, DAG.getSrcValue(NULL));
3013
3014 // Build the FILD
3015 std::vector<MVT::ValueType> Tys;
3016 Tys.push_back(MVT::f64);
3017 Tys.push_back(MVT::Other);
3018 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
3019 std::vector<SDOperand> Ops;
3020 Ops.push_back(Chain);
3021 Ops.push_back(StackSlot);
3022 Ops.push_back(DAG.getValueType(SrcVT));
3023 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
3024 Tys, Ops);
3025
3026 if (X86ScalarSSE) {
3027 Chain = Result.getValue(1);
3028 SDOperand InFlag = Result.getValue(2);
3029
3030 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3031 // shouldn't be necessary except that RFP cannot be live across
3032 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003033 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003034 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003035 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00003036 std::vector<MVT::ValueType> Tys;
Evan Cheng6dab0532006-01-30 08:02:57 +00003037 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003038 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003039 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003040 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003041 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003042 Ops.push_back(DAG.getValueType(Op.getValueType()));
3043 Ops.push_back(InFlag);
3044 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
3045 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
3046 DAG.getSrcValue(NULL));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003047 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003048
Evan Cheng0db9fe62006-04-25 20:13:52 +00003049 return Result;
3050}
3051
3052SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3053 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3054 "Unknown FP_TO_SINT to lower!");
3055 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3056 // stack slot.
3057 MachineFunction &MF = DAG.getMachineFunction();
3058 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3059 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3060 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3061
3062 unsigned Opc;
3063 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003064 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3065 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3066 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3067 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003068 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003069
Evan Cheng0db9fe62006-04-25 20:13:52 +00003070 SDOperand Chain = DAG.getEntryNode();
3071 SDOperand Value = Op.getOperand(0);
3072 if (X86ScalarSSE) {
3073 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
3074 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
3075 DAG.getSrcValue(0));
3076 std::vector<MVT::ValueType> Tys;
3077 Tys.push_back(MVT::f64);
3078 Tys.push_back(MVT::Other);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003079 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00003080 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003081 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003082 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
3083 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
3084 Chain = Value.getValue(1);
3085 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3086 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3087 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003088
Evan Cheng0db9fe62006-04-25 20:13:52 +00003089 // Build the FP_TO_INT*_IN_MEM
3090 std::vector<SDOperand> Ops;
3091 Ops.push_back(Chain);
3092 Ops.push_back(Value);
3093 Ops.push_back(StackSlot);
3094 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Chengd9558e02006-01-06 00:43:03 +00003095
Evan Cheng0db9fe62006-04-25 20:13:52 +00003096 // Load the result.
3097 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3098 DAG.getSrcValue(NULL));
3099}
3100
3101SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3102 MVT::ValueType VT = Op.getValueType();
3103 const Type *OpNTy = MVT::getTypeForValueType(VT);
3104 std::vector<Constant*> CV;
3105 if (VT == MVT::f64) {
3106 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3107 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3108 } else {
3109 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3110 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3111 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3112 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3113 }
3114 Constant *CS = ConstantStruct::get(CV);
3115 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3116 SDOperand Mask
3117 = DAG.getNode(X86ISD::LOAD_PACK,
3118 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3119 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3120}
3121
3122SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3123 MVT::ValueType VT = Op.getValueType();
3124 const Type *OpNTy = MVT::getTypeForValueType(VT);
3125 std::vector<Constant*> CV;
3126 if (VT == MVT::f64) {
3127 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3128 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3129 } else {
3130 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3131 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3132 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3133 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3134 }
3135 Constant *CS = ConstantStruct::get(CV);
3136 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3137 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3138 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3139 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3140}
3141
3142SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3143 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3144 SDOperand Cond;
3145 SDOperand CC = Op.getOperand(2);
3146 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3147 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3148 bool Flip;
3149 unsigned X86CC;
3150 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3151 if (Flip)
3152 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3153 Op.getOperand(1), Op.getOperand(0));
3154 else
Evan Cheng6dfa9992006-01-30 23:41:35 +00003155 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3156 Op.getOperand(0), Op.getOperand(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003157 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3158 DAG.getConstant(X86CC, MVT::i8), Cond);
3159 } else {
3160 assert(isFP && "Illegal integer SetCC!");
3161
3162 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3163 Op.getOperand(0), Op.getOperand(1));
3164 std::vector<MVT::ValueType> Tys;
3165 std::vector<SDOperand> Ops;
3166 switch (SetCCOpcode) {
Evan Chengd9558e02006-01-06 00:43:03 +00003167 default: assert(false && "Illegal floating point SetCC!");
3168 case ISD::SETOEQ: { // !PF & ZF
3169 Tys.push_back(MVT::i8);
3170 Tys.push_back(MVT::Flag);
3171 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3172 Ops.push_back(Cond);
3173 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3174 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3175 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3176 Tmp1.getValue(1));
3177 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3178 }
Evan Chengd9558e02006-01-06 00:43:03 +00003179 case ISD::SETUNE: { // PF | !ZF
3180 Tys.push_back(MVT::i8);
3181 Tys.push_back(MVT::Flag);
3182 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3183 Ops.push_back(Cond);
3184 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3185 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3186 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3187 Tmp1.getValue(1));
3188 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3189 }
Evan Chengd9558e02006-01-06 00:43:03 +00003190 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003191 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003192}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003193
Evan Cheng0db9fe62006-04-25 20:13:52 +00003194SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3195 MVT::ValueType VT = Op.getValueType();
3196 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3197 bool addTest = false;
3198 SDOperand Op0 = Op.getOperand(0);
3199 SDOperand Cond, CC;
3200 if (Op0.getOpcode() == ISD::SETCC)
3201 Op0 = LowerOperation(Op0, DAG);
Evan Cheng9bba8942006-01-26 02:13:10 +00003202
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203 if (Op0.getOpcode() == X86ISD::SETCC) {
3204 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3205 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3206 // have another use it will be eliminated.
3207 // If the X86ISD::SETCC has more than one use, then it's probably better
3208 // to use a test instead of duplicating the X86ISD::CMP (for register
3209 // pressure reason).
3210 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3211 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3212 CmpOpc == X86ISD::UCOMI) {
3213 if (!Op0.hasOneUse()) {
3214 std::vector<MVT::ValueType> Tys;
3215 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3216 Tys.push_back(Op0.Val->getValueType(i));
3217 std::vector<SDOperand> Ops;
3218 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3219 Ops.push_back(Op0.getOperand(i));
3220 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3221 }
3222
3223 CC = Op0.getOperand(0);
3224 Cond = Op0.getOperand(1);
3225 // Make a copy as flag result cannot be used by more than one.
3226 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3227 Cond.getOperand(0), Cond.getOperand(1));
3228 addTest =
3229 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00003230 } else
3231 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003232 } else
3233 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00003234
Evan Cheng0db9fe62006-04-25 20:13:52 +00003235 if (addTest) {
3236 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3237 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00003238 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003239
Evan Cheng0db9fe62006-04-25 20:13:52 +00003240 std::vector<MVT::ValueType> Tys;
3241 Tys.push_back(Op.getValueType());
3242 Tys.push_back(MVT::Flag);
3243 std::vector<SDOperand> Ops;
3244 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3245 // condition is true.
3246 Ops.push_back(Op.getOperand(2));
3247 Ops.push_back(Op.getOperand(1));
3248 Ops.push_back(CC);
3249 Ops.push_back(Cond);
3250 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3251}
Evan Cheng9bba8942006-01-26 02:13:10 +00003252
Evan Cheng0db9fe62006-04-25 20:13:52 +00003253SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3254 bool addTest = false;
3255 SDOperand Cond = Op.getOperand(1);
3256 SDOperand Dest = Op.getOperand(2);
3257 SDOperand CC;
3258 if (Cond.getOpcode() == ISD::SETCC)
3259 Cond = LowerOperation(Cond, DAG);
3260
3261 if (Cond.getOpcode() == X86ISD::SETCC) {
3262 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3263 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3264 // have another use it will be eliminated.
3265 // If the X86ISD::SETCC has more than one use, then it's probably better
3266 // to use a test instead of duplicating the X86ISD::CMP (for register
3267 // pressure reason).
3268 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3269 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3270 CmpOpc == X86ISD::UCOMI) {
3271 if (!Cond.hasOneUse()) {
3272 std::vector<MVT::ValueType> Tys;
3273 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3274 Tys.push_back(Cond.Val->getValueType(i));
3275 std::vector<SDOperand> Ops;
3276 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3277 Ops.push_back(Cond.getOperand(i));
3278 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3279 }
3280
3281 CC = Cond.getOperand(0);
3282 Cond = Cond.getOperand(1);
3283 // Make a copy as flag result cannot be used by more than one.
3284 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3285 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00003286 } else
3287 addTest = true;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003288 } else
3289 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00003290
Evan Cheng0db9fe62006-04-25 20:13:52 +00003291 if (addTest) {
3292 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3293 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng898101c2005-12-19 23:12:38 +00003294 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003295 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3296 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3297}
Evan Cheng67f92a72006-01-11 22:15:48 +00003298
Evan Cheng0db9fe62006-04-25 20:13:52 +00003299SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3300 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3301 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3302 DAG.getTargetJumpTable(JT->getIndex(),
3303 getPointerTy()));
3304 if (Subtarget->isTargetDarwin()) {
3305 // With PIC, the address is actually $g + Offset.
3306 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3307 Result = DAG.getNode(ISD::ADD, getPointerTy(),
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003308 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3309 Result);
Evan Cheng67f92a72006-01-11 22:15:48 +00003310 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00003311
Evan Cheng0db9fe62006-04-25 20:13:52 +00003312 return Result;
3313}
Evan Cheng7ccced62006-02-18 00:15:05 +00003314
Evan Cheng32fe1032006-05-25 00:59:30 +00003315SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3316 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3317 if (CallingConv == CallingConv::Fast && EnableFastCC)
3318 return LowerFastCCCallTo(Op, DAG);
3319 else
3320 return LowerCCCCallTo(Op, DAG);
3321}
3322
Evan Cheng0db9fe62006-04-25 20:13:52 +00003323SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3324 SDOperand Copy;
Nate Begemanee625572006-01-27 21:09:22 +00003325
Evan Cheng0db9fe62006-04-25 20:13:52 +00003326 switch(Op.getNumOperands()) {
Nate Begemanee625572006-01-27 21:09:22 +00003327 default:
3328 assert(0 && "Do not know how to return this many arguments!");
3329 abort();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003330 case 1: // ret void.
Nate Begemanee625572006-01-27 21:09:22 +00003331 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Cheng0db9fe62006-04-25 20:13:52 +00003332 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Evan Cheng6848be12006-05-26 23:10:12 +00003333 case 3: {
Nate Begemanee625572006-01-27 21:09:22 +00003334 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerb2be4032006-04-17 20:32:50 +00003335
3336 if (MVT::isVector(ArgVT)) {
3337 // Integer or FP vector result -> XMM0.
3338 if (DAG.getMachineFunction().liveout_empty())
3339 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3340 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3341 SDOperand());
3342 } else if (MVT::isInteger(ArgVT)) {
3343 // Integer result -> EAX
3344 if (DAG.getMachineFunction().liveout_empty())
3345 DAG.getMachineFunction().addLiveOut(X86::EAX);
3346
Nate Begemanee625572006-01-27 21:09:22 +00003347 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3348 SDOperand());
Chris Lattnerb2be4032006-04-17 20:32:50 +00003349 } else if (!X86ScalarSSE) {
3350 // FP return with fp-stack value.
3351 if (DAG.getMachineFunction().liveout_empty())
3352 DAG.getMachineFunction().addLiveOut(X86::ST0);
3353
Nate Begemanee625572006-01-27 21:09:22 +00003354 std::vector<MVT::ValueType> Tys;
3355 Tys.push_back(MVT::Other);
3356 Tys.push_back(MVT::Flag);
3357 std::vector<SDOperand> Ops;
3358 Ops.push_back(Op.getOperand(0));
3359 Ops.push_back(Op.getOperand(1));
3360 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3361 } else {
Chris Lattnerb2be4032006-04-17 20:32:50 +00003362 // FP return with ScalarSSE (return on fp-stack).
3363 if (DAG.getMachineFunction().liveout_empty())
3364 DAG.getMachineFunction().addLiveOut(X86::ST0);
3365
Evan Cheng0d084c92006-02-01 00:20:21 +00003366 SDOperand MemLoc;
3367 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003368 SDOperand Value = Op.getOperand(1);
3369
Evan Cheng760df292006-02-01 01:19:32 +00003370 if (Value.getOpcode() == ISD::LOAD &&
3371 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00003372 Chain = Value.getOperand(0);
3373 MemLoc = Value.getOperand(1);
3374 } else {
3375 // Spill the value to memory and reload it into top of stack.
3376 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3377 MachineFunction &MF = DAG.getMachineFunction();
3378 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3379 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3380 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3381 Value, MemLoc, DAG.getSrcValue(0));
3382 }
Nate Begemanee625572006-01-27 21:09:22 +00003383 std::vector<MVT::ValueType> Tys;
3384 Tys.push_back(MVT::f64);
3385 Tys.push_back(MVT::Other);
3386 std::vector<SDOperand> Ops;
3387 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00003388 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00003389 Ops.push_back(DAG.getValueType(ArgVT));
3390 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3391 Tys.clear();
3392 Tys.push_back(MVT::Other);
3393 Tys.push_back(MVT::Flag);
3394 Ops.clear();
3395 Ops.push_back(Copy.getValue(1));
3396 Ops.push_back(Copy);
3397 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3398 }
3399 break;
3400 }
Evan Cheng6848be12006-05-26 23:10:12 +00003401 case 5:
Chris Lattnerb2be4032006-04-17 20:32:50 +00003402 if (DAG.getMachineFunction().liveout_empty()) {
3403 DAG.getMachineFunction().addLiveOut(X86::EAX);
3404 DAG.getMachineFunction().addLiveOut(X86::EDX);
3405 }
3406
Evan Cheng6848be12006-05-26 23:10:12 +00003407 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(3),
Nate Begemanee625572006-01-27 21:09:22 +00003408 SDOperand());
3409 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3410 break;
Nate Begemanee625572006-01-27 21:09:22 +00003411 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003412 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3413 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3414 Copy.getValue(1));
3415}
3416
Evan Cheng1bc78042006-04-26 01:20:17 +00003417SDOperand
3418X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00003419 MachineFunction &MF = DAG.getMachineFunction();
3420 const Function* Fn = MF.getFunction();
3421 if (Fn->hasExternalLinkage() &&
Evan Chengb12223e2006-06-09 06:24:42 +00003422 Subtarget->TargetType == X86Subtarget::isCygwin &&
3423 Fn->getName() == "main")
Evan Chenge8bd0a32006-06-06 23:30:24 +00003424 MF.getInfo<X86FunctionInfo>()->setForceFramePointer(true);
3425
Evan Cheng25caf632006-05-23 21:06:34 +00003426 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3427 if (CC == CallingConv::Fast && EnableFastCC)
3428 return LowerFastCCArguments(Op, DAG);
3429 else
3430 return LowerCCCArguments(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003431}
3432
Evan Cheng0db9fe62006-04-25 20:13:52 +00003433SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3434 SDOperand InFlag(0, 0);
3435 SDOperand Chain = Op.getOperand(0);
3436 unsigned Align =
3437 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3438 if (Align == 0) Align = 1;
3439
3440 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3441 // If not DWORD aligned, call memset if size is less than the threshold.
3442 // It knows how to align to the right boundary first.
3443 if ((Align & 3) != 0 ||
3444 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3445 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003446 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003447 std::vector<std::pair<SDOperand, const Type*> > Args;
3448 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3449 // Extend the ubyte argument to be an int value for the call.
3450 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3451 Args.push_back(std::make_pair(Val, IntPtrTy));
3452 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3453 std::pair<SDOperand,SDOperand> CallResult =
3454 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3455 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3456 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00003457 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00003458
Evan Cheng0db9fe62006-04-25 20:13:52 +00003459 MVT::ValueType AVT;
3460 SDOperand Count;
3461 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3462 unsigned BytesLeft = 0;
3463 bool TwoRepStos = false;
3464 if (ValC) {
3465 unsigned ValReg;
3466 unsigned Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003467
Evan Cheng0db9fe62006-04-25 20:13:52 +00003468 // If the value is a constant, then we can potentially use larger sets.
3469 switch (Align & 3) {
3470 case 2: // WORD aligned
3471 AVT = MVT::i16;
3472 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3473 BytesLeft = I->getValue() % 2;
3474 Val = (Val << 8) | Val;
3475 ValReg = X86::AX;
3476 break;
3477 case 0: // DWORD aligned
3478 AVT = MVT::i32;
3479 if (I) {
3480 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3481 BytesLeft = I->getValue() % 4;
Evan Cheng80d428c2006-04-19 22:48:17 +00003482 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003483 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3484 DAG.getConstant(2, MVT::i8));
3485 TwoRepStos = true;
Evan Cheng80d428c2006-04-19 22:48:17 +00003486 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003487 Val = (Val << 8) | Val;
3488 Val = (Val << 16) | Val;
3489 ValReg = X86::EAX;
3490 break;
3491 default: // Byte aligned
3492 AVT = MVT::i8;
3493 Count = Op.getOperand(3);
3494 ValReg = X86::AL;
3495 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00003496 }
3497
Evan Cheng0db9fe62006-04-25 20:13:52 +00003498 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3499 InFlag);
3500 InFlag = Chain.getValue(1);
3501 } else {
3502 AVT = MVT::i8;
3503 Count = Op.getOperand(3);
3504 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3505 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00003506 }
Evan Chengc78d3b42006-04-24 18:01:45 +00003507
Evan Cheng0db9fe62006-04-25 20:13:52 +00003508 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3509 InFlag = Chain.getValue(1);
3510 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3511 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00003512
Evan Cheng0db9fe62006-04-25 20:13:52 +00003513 std::vector<MVT::ValueType> Tys;
3514 Tys.push_back(MVT::Other);
3515 Tys.push_back(MVT::Flag);
3516 std::vector<SDOperand> Ops;
3517 Ops.push_back(Chain);
3518 Ops.push_back(DAG.getValueType(AVT));
3519 Ops.push_back(InFlag);
3520 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengc78d3b42006-04-24 18:01:45 +00003521
Evan Cheng0db9fe62006-04-25 20:13:52 +00003522 if (TwoRepStos) {
3523 InFlag = Chain.getValue(1);
3524 Count = Op.getOperand(3);
3525 MVT::ValueType CVT = Count.getValueType();
3526 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3527 DAG.getConstant(3, CVT));
3528 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3529 InFlag = Chain.getValue(1);
3530 Tys.clear();
3531 Tys.push_back(MVT::Other);
3532 Tys.push_back(MVT::Flag);
3533 Ops.clear();
3534 Ops.push_back(Chain);
3535 Ops.push_back(DAG.getValueType(MVT::i8));
3536 Ops.push_back(InFlag);
3537 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3538 } else if (BytesLeft) {
3539 // Issue stores for the last 1 - 3 bytes.
3540 SDOperand Value;
3541 unsigned Val = ValC->getValue() & 255;
3542 unsigned Offset = I->getValue() - BytesLeft;
3543 SDOperand DstAddr = Op.getOperand(1);
3544 MVT::ValueType AddrVT = DstAddr.getValueType();
3545 if (BytesLeft >= 2) {
3546 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3547 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3548 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3549 DAG.getConstant(Offset, AddrVT)),
3550 DAG.getSrcValue(NULL));
3551 BytesLeft -= 2;
3552 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00003553 }
3554
Evan Cheng0db9fe62006-04-25 20:13:52 +00003555 if (BytesLeft == 1) {
3556 Value = DAG.getConstant(Val, MVT::i8);
3557 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3558 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3559 DAG.getConstant(Offset, AddrVT)),
3560 DAG.getSrcValue(NULL));
Evan Chengba05f722006-04-21 23:03:30 +00003561 }
Evan Cheng386031a2006-03-24 07:29:27 +00003562 }
Evan Cheng11e15b32006-04-03 20:53:28 +00003563
Evan Cheng0db9fe62006-04-25 20:13:52 +00003564 return Chain;
3565}
Evan Cheng11e15b32006-04-03 20:53:28 +00003566
Evan Cheng0db9fe62006-04-25 20:13:52 +00003567SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3568 SDOperand Chain = Op.getOperand(0);
3569 unsigned Align =
3570 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3571 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00003572
Evan Cheng0db9fe62006-04-25 20:13:52 +00003573 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3574 // If not DWORD aligned, call memcpy if size is less than the threshold.
3575 // It knows how to align to the right boundary first.
3576 if ((Align & 3) != 0 ||
3577 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3578 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003579 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003580 std::vector<std::pair<SDOperand, const Type*> > Args;
3581 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3582 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3583 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3584 std::pair<SDOperand,SDOperand> CallResult =
3585 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3586 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3587 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00003588 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003589
3590 MVT::ValueType AVT;
3591 SDOperand Count;
3592 unsigned BytesLeft = 0;
3593 bool TwoRepMovs = false;
3594 switch (Align & 3) {
3595 case 2: // WORD aligned
3596 AVT = MVT::i16;
3597 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3598 BytesLeft = I->getValue() % 2;
3599 break;
3600 case 0: // DWORD aligned
3601 AVT = MVT::i32;
3602 if (I) {
3603 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3604 BytesLeft = I->getValue() % 4;
Evan Chengcdfc3c82006-04-17 22:45:49 +00003605 } else {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003606 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3607 DAG.getConstant(2, MVT::i8));
3608 TwoRepMovs = true;
Evan Cheng5edb8d22006-04-17 22:04:06 +00003609 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003610 break;
3611 default: // Byte aligned
3612 AVT = MVT::i8;
3613 Count = Op.getOperand(3);
3614 break;
3615 }
3616
3617 SDOperand InFlag(0, 0);
3618 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3619 InFlag = Chain.getValue(1);
3620 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3621 InFlag = Chain.getValue(1);
3622 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3623 InFlag = Chain.getValue(1);
3624
3625 std::vector<MVT::ValueType> Tys;
3626 Tys.push_back(MVT::Other);
3627 Tys.push_back(MVT::Flag);
3628 std::vector<SDOperand> Ops;
3629 Ops.push_back(Chain);
3630 Ops.push_back(DAG.getValueType(AVT));
3631 Ops.push_back(InFlag);
3632 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3633
3634 if (TwoRepMovs) {
3635 InFlag = Chain.getValue(1);
3636 Count = Op.getOperand(3);
3637 MVT::ValueType CVT = Count.getValueType();
3638 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3639 DAG.getConstant(3, CVT));
3640 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3641 InFlag = Chain.getValue(1);
3642 Tys.clear();
3643 Tys.push_back(MVT::Other);
3644 Tys.push_back(MVT::Flag);
3645 Ops.clear();
3646 Ops.push_back(Chain);
3647 Ops.push_back(DAG.getValueType(MVT::i8));
3648 Ops.push_back(InFlag);
3649 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3650 } else if (BytesLeft) {
3651 // Issue loads and stores for the last 1 - 3 bytes.
3652 unsigned Offset = I->getValue() - BytesLeft;
3653 SDOperand DstAddr = Op.getOperand(1);
3654 MVT::ValueType DstVT = DstAddr.getValueType();
3655 SDOperand SrcAddr = Op.getOperand(2);
3656 MVT::ValueType SrcVT = SrcAddr.getValueType();
3657 SDOperand Value;
3658 if (BytesLeft >= 2) {
3659 Value = DAG.getLoad(MVT::i16, Chain,
3660 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3661 DAG.getConstant(Offset, SrcVT)),
3662 DAG.getSrcValue(NULL));
3663 Chain = Value.getValue(1);
3664 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3665 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3666 DAG.getConstant(Offset, DstVT)),
3667 DAG.getSrcValue(NULL));
3668 BytesLeft -= 2;
3669 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00003670 }
3671
Evan Cheng0db9fe62006-04-25 20:13:52 +00003672 if (BytesLeft == 1) {
3673 Value = DAG.getLoad(MVT::i8, Chain,
3674 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3675 DAG.getConstant(Offset, SrcVT)),
3676 DAG.getSrcValue(NULL));
3677 Chain = Value.getValue(1);
3678 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3679 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3680 DAG.getConstant(Offset, DstVT)),
3681 DAG.getSrcValue(NULL));
3682 }
Evan Chengb067a1e2006-03-31 19:22:53 +00003683 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003684
3685 return Chain;
3686}
3687
3688SDOperand
3689X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3690 std::vector<MVT::ValueType> Tys;
3691 Tys.push_back(MVT::Other);
3692 Tys.push_back(MVT::Flag);
3693 std::vector<SDOperand> Ops;
3694 Ops.push_back(Op.getOperand(0));
3695 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3696 Ops.clear();
3697 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3698 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3699 MVT::i32, Ops[0].getValue(2)));
3700 Ops.push_back(Ops[1].getValue(1));
3701 Tys[0] = Tys[1] = MVT::i32;
3702 Tys.push_back(MVT::Other);
3703 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3704}
3705
3706SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3707 // vastart just stores the address of the VarArgsFrameIndex slot into the
3708 // memory location argument.
3709 // FIXME: Replace MVT::i32 with PointerTy
3710 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3711 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3712 Op.getOperand(1), Op.getOperand(2));
3713}
3714
3715SDOperand
3716X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3717 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3718 switch (IntNo) {
3719 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00003720 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003721 case Intrinsic::x86_sse_comieq_ss:
3722 case Intrinsic::x86_sse_comilt_ss:
3723 case Intrinsic::x86_sse_comile_ss:
3724 case Intrinsic::x86_sse_comigt_ss:
3725 case Intrinsic::x86_sse_comige_ss:
3726 case Intrinsic::x86_sse_comineq_ss:
3727 case Intrinsic::x86_sse_ucomieq_ss:
3728 case Intrinsic::x86_sse_ucomilt_ss:
3729 case Intrinsic::x86_sse_ucomile_ss:
3730 case Intrinsic::x86_sse_ucomigt_ss:
3731 case Intrinsic::x86_sse_ucomige_ss:
3732 case Intrinsic::x86_sse_ucomineq_ss:
3733 case Intrinsic::x86_sse2_comieq_sd:
3734 case Intrinsic::x86_sse2_comilt_sd:
3735 case Intrinsic::x86_sse2_comile_sd:
3736 case Intrinsic::x86_sse2_comigt_sd:
3737 case Intrinsic::x86_sse2_comige_sd:
3738 case Intrinsic::x86_sse2_comineq_sd:
3739 case Intrinsic::x86_sse2_ucomieq_sd:
3740 case Intrinsic::x86_sse2_ucomilt_sd:
3741 case Intrinsic::x86_sse2_ucomile_sd:
3742 case Intrinsic::x86_sse2_ucomigt_sd:
3743 case Intrinsic::x86_sse2_ucomige_sd:
3744 case Intrinsic::x86_sse2_ucomineq_sd: {
3745 unsigned Opc = 0;
3746 ISD::CondCode CC = ISD::SETCC_INVALID;
3747 switch (IntNo) {
3748 default: break;
3749 case Intrinsic::x86_sse_comieq_ss:
3750 case Intrinsic::x86_sse2_comieq_sd:
3751 Opc = X86ISD::COMI;
3752 CC = ISD::SETEQ;
3753 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003754 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003755 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003756 Opc = X86ISD::COMI;
3757 CC = ISD::SETLT;
3758 break;
3759 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003760 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003761 Opc = X86ISD::COMI;
3762 CC = ISD::SETLE;
3763 break;
3764 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003765 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003766 Opc = X86ISD::COMI;
3767 CC = ISD::SETGT;
3768 break;
3769 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003770 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003771 Opc = X86ISD::COMI;
3772 CC = ISD::SETGE;
3773 break;
3774 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003775 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003776 Opc = X86ISD::COMI;
3777 CC = ISD::SETNE;
3778 break;
3779 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003780 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003781 Opc = X86ISD::UCOMI;
3782 CC = ISD::SETEQ;
3783 break;
3784 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003785 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003786 Opc = X86ISD::UCOMI;
3787 CC = ISD::SETLT;
3788 break;
3789 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003790 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003791 Opc = X86ISD::UCOMI;
3792 CC = ISD::SETLE;
3793 break;
3794 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003795 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003796 Opc = X86ISD::UCOMI;
3797 CC = ISD::SETGT;
3798 break;
3799 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00003800 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00003801 Opc = X86ISD::UCOMI;
3802 CC = ISD::SETGE;
3803 break;
3804 case Intrinsic::x86_sse_ucomineq_ss:
3805 case Intrinsic::x86_sse2_ucomineq_sd:
3806 Opc = X86ISD::UCOMI;
3807 CC = ISD::SETNE;
3808 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00003809 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003810 bool Flip;
3811 unsigned X86CC;
3812 translateX86CC(CC, true, X86CC, Flip);
3813 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3814 Op.getOperand(Flip?1:2));
3815 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3816 DAG.getConstant(X86CC, MVT::i8), Cond);
3817 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00003818 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00003819 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003820}
Evan Cheng72261582005-12-20 06:22:03 +00003821
Evan Cheng0db9fe62006-04-25 20:13:52 +00003822/// LowerOperation - Provide custom lowering hooks for some operations.
3823///
3824SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3825 switch (Op.getOpcode()) {
3826 default: assert(0 && "Should not custom lower this!");
3827 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3828 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3829 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3830 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3831 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3832 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3833 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3834 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3835 case ISD::SHL_PARTS:
3836 case ISD::SRA_PARTS:
3837 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3838 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3839 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3840 case ISD::FABS: return LowerFABS(Op, DAG);
3841 case ISD::FNEG: return LowerFNEG(Op, DAG);
3842 case ISD::SETCC: return LowerSETCC(Op, DAG);
3843 case ISD::SELECT: return LowerSELECT(Op, DAG);
3844 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3845 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00003846 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003847 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00003848 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003849 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3850 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3851 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3852 case ISD::VASTART: return LowerVASTART(Op, DAG);
3853 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3854 }
3855}
3856
Evan Cheng72261582005-12-20 06:22:03 +00003857const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3858 switch (Opcode) {
3859 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00003860 case X86ISD::SHLD: return "X86ISD::SHLD";
3861 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00003862 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00003863 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00003864 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00003865 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00003866 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3867 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3868 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00003869 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00003870 case X86ISD::FST: return "X86ISD::FST";
3871 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00003872 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00003873 case X86ISD::CALL: return "X86ISD::CALL";
3874 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3875 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3876 case X86ISD::CMP: return "X86ISD::CMP";
3877 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng6be2c582006-04-05 23:38:46 +00003878 case X86ISD::COMI: return "X86ISD::COMI";
3879 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00003880 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00003881 case X86ISD::CMOV: return "X86ISD::CMOV";
3882 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00003883 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00003884 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3885 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00003886 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00003887 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00003888 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00003889 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00003890 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00003891 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng72261582005-12-20 06:22:03 +00003892 }
3893}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003894
Nate Begeman368e18d2006-02-16 21:11:51 +00003895void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3896 uint64_t Mask,
3897 uint64_t &KnownZero,
3898 uint64_t &KnownOne,
3899 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003900 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00003901 assert((Opc >= ISD::BUILTIN_OP_END ||
3902 Opc == ISD::INTRINSIC_WO_CHAIN ||
3903 Opc == ISD::INTRINSIC_W_CHAIN ||
3904 Opc == ISD::INTRINSIC_VOID) &&
3905 "Should use MaskedValueIsZero if you don't know whether Op"
3906 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003907
Evan Cheng865f0602006-04-05 06:11:20 +00003908 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003909 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00003910 default: break;
Nate Begeman368e18d2006-02-16 21:11:51 +00003911 case X86ISD::SETCC:
3912 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3913 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003914 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00003915}
Chris Lattner259e97c2006-01-31 19:43:35 +00003916
3917std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00003918getRegClassForInlineAsmConstraint(const std::string &Constraint,
3919 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00003920 if (Constraint.size() == 1) {
3921 // FIXME: not handling fp-stack yet!
3922 // FIXME: not handling MMX registers yet ('y' constraint).
3923 switch (Constraint[0]) { // GCC X86 Constraint Letters
3924 default: break; // Unknown constriant letter
3925 case 'r': // GENERAL_REGS
3926 case 'R': // LEGACY_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003927 if (VT == MVT::i32)
3928 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3929 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3930 else if (VT == MVT::i16)
3931 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3932 X86::SI, X86::DI, X86::BP, X86::SP, 0);
3933 else if (VT == MVT::i8)
3934 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3935 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003936 case 'l': // INDEX_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003937 if (VT == MVT::i32)
3938 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
3939 X86::ESI, X86::EDI, X86::EBP, 0);
3940 else if (VT == MVT::i16)
3941 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
3942 X86::SI, X86::DI, X86::BP, 0);
3943 else if (VT == MVT::i8)
3944 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3945 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003946 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3947 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00003948 if (VT == MVT::i32)
3949 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
3950 else if (VT == MVT::i16)
3951 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
3952 else if (VT == MVT::i8)
3953 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
3954 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00003955 case 'x': // SSE_REGS if SSE1 allowed
3956 if (Subtarget->hasSSE1())
3957 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3958 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3959 0);
3960 return std::vector<unsigned>();
3961 case 'Y': // SSE_REGS if SSE2 allowed
3962 if (Subtarget->hasSSE2())
3963 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3964 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3965 0);
3966 return std::vector<unsigned>();
3967 }
3968 }
3969
Chris Lattner1efa40f2006-02-22 00:56:39 +00003970 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00003971}
Evan Cheng30b37b52006-03-13 23:18:16 +00003972
3973/// isLegalAddressImmediate - Return true if the integer value or
3974/// GlobalValue can be used as the offset of the target addressing mode.
3975bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3976 // X86 allows a sign-extended 32-bit immediate field.
3977 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3978}
3979
3980bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00003981 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00003982 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3983 if (RModel == Reloc::Static)
3984 return true;
3985 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00003986 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00003987 else
3988 return false;
3989 } else
3990 return true;
3991}
Evan Cheng0188ecb2006-03-22 18:59:22 +00003992
3993/// isShuffleMaskLegal - Targets can use this to indicate that they only
3994/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3995/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3996/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00003997bool
3998X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3999 // Only do shuffles on 128-bit vector types for now.
4000 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng80d428c2006-04-19 22:48:17 +00004001 return (Mask.Val->getNumOperands() <= 4 ||
Evan Chengc575ca22006-04-17 20:43:08 +00004002 isSplatMask(Mask.Val) ||
Evan Chengc21a0532006-04-05 01:47:37 +00004003 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Chenged4ca7f2006-03-28 08:27:15 +00004004 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00004005 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey2d2a6132006-03-28 10:17:11 +00004006 X86::isUNPCKHMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00004007}
Evan Cheng39623da2006-04-20 08:58:49 +00004008
4009bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4010 MVT::ValueType EVT,
4011 SelectionDAG &DAG) const {
4012 unsigned NumElts = BVOps.size();
4013 // Only do shuffles on 128-bit vector types for now.
4014 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4015 if (NumElts == 2) return true;
4016 if (NumElts == 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00004017 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng39623da2006-04-20 08:58:49 +00004018 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
4019 }
4020 return false;
4021}