blob: 787436d35f2046f10c6853ec8bdd856e603e4ec2 [file] [log] [blame]
Chris Lattner76ac0682005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng72d5c252006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/Function.h"
Evan Cheng78038292006-04-05 23:38:46 +000022#include "llvm/Intrinsics.h"
Evan Chengaf598d22006-03-13 23:18:16 +000023#include "llvm/ADT/VectorExtras.h"
24#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000028#include "llvm/CodeGen/SelectionDAG.h"
29#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000030#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000031#include "llvm/Target/TargetOptions.h"
32using namespace llvm;
33
34// FIXME: temporary.
35#include "llvm/Support/CommandLine.h"
36static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
37 cl::desc("Enable fastcc on X86"));
38
39X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000041 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
43
Chris Lattner76ac0682005-11-15 00:40:23 +000044 // Set up the TargetLowering object.
45
46 // X86 is weird, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
49 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000050 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000051 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000052 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng20931a72006-03-16 21:47:42 +000053
Evan Chengbc047222006-03-22 19:22:18 +000054 if (!Subtarget->isTargetDarwin())
Evan Chengb09a56f2006-03-17 20:31:41 +000055 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
56 setUseUnderscoreSetJmpLongJmp(true);
57
Evan Cheng20931a72006-03-16 21:47:42 +000058 // Add legal addressing mode scale values.
59 addLegalAddressScale(8);
60 addLegalAddressScale(4);
61 addLegalAddressScale(2);
62 // Enter the ones which require both scale + index last. These are more
63 // expensive.
64 addLegalAddressScale(9);
65 addLegalAddressScale(5);
66 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000067
Chris Lattner76ac0682005-11-15 00:40:23 +000068 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000069 addRegisterClass(MVT::i8, X86::R8RegisterClass);
70 addRegisterClass(MVT::i16, X86::R16RegisterClass);
71 addRegisterClass(MVT::i32, X86::R32RegisterClass);
72
73 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
74 // operation.
75 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
77 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000078
79 if (X86ScalarSSE)
80 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
81 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
82 else
83 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000084
85 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
86 // this operation.
87 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
88 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000089 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000090 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000091 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000092 else {
93 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
94 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
95 }
Chris Lattner76ac0682005-11-15 00:40:23 +000096
Evan Cheng5b97fcf2006-01-30 08:02:57 +000097 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
98 // isn't legal.
99 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
100 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
101
Evan Cheng08390f62006-01-30 22:13:22 +0000102 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
103 // this operation.
104 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
105 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
106
107 if (X86ScalarSSE) {
108 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
109 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000111 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000112 }
113
114 // Handle FP_TO_UINT by promoting the destination to a larger signed
115 // conversion.
116 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
118 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
119
Evan Chengd13778e2006-02-18 07:26:17 +0000120 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000121 // Expand FP_TO_UINT into a select.
122 // FIXME: We would like to use a Custom expander here eventually to do
123 // the optimal thing for SSE vs. the default expansion in the legalizer.
124 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
125 else
Evan Chengd13778e2006-02-18 07:26:17 +0000126 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000127 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
128
Evan Cheng08390f62006-01-30 22:13:22 +0000129 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
130 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000131
Evan Cheng593bea72006-02-17 07:01:52 +0000132 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000133 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
134 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000135 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
139 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
140 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
141 setOperationAction(ISD::FREM , MVT::f64 , Expand);
142 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
143 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
146 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
148 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
149 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
150 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000151 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000152 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000153
Chris Lattner76ac0682005-11-15 00:40:23 +0000154 // These should be promoted to a larger select which is supported.
155 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
156 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000157
158 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000159 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
160 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
162 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
167 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000168 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000169 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000170 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000171 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman4ca2ea52006-04-22 18:53:45 +0000172 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000173 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000174 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000175 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000176 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
178 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000179 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000180 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
181 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000182
Chris Lattner9c415362005-11-29 06:16:21 +0000183 // We don't have line number support yet.
184 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000185 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000186 // FIXME - use subtarget debug flags
Evan Chengbc047222006-03-22 19:22:18 +0000187 if (!Subtarget->isTargetDarwin())
Evan Cheng30d7b702006-03-07 02:02:57 +0000188 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000189
Nate Begemane74795c2006-01-25 18:21:52 +0000190 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
191 setOperationAction(ISD::VASTART , MVT::Other, Custom);
192
193 // Use the default implementation.
194 setOperationAction(ISD::VAARG , MVT::Other, Expand);
195 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
196 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000197 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
198 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
199 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000200
Chris Lattner9c7f5032006-03-05 05:08:37 +0000201 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
202 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
203
Chris Lattner76ac0682005-11-15 00:40:23 +0000204 if (X86ScalarSSE) {
205 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000206 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
207 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000208
209 // SSE has no load+extend ops
210 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
211 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
212
Evan Cheng72d5c252006-01-31 22:28:30 +0000213 // Use ANDPD to simulate FABS.
214 setOperationAction(ISD::FABS , MVT::f64, Custom);
215 setOperationAction(ISD::FABS , MVT::f32, Custom);
216
217 // Use XORP to simulate FNEG.
218 setOperationAction(ISD::FNEG , MVT::f64, Custom);
219 setOperationAction(ISD::FNEG , MVT::f32, Custom);
220
Evan Chengd8fba3a2006-02-02 00:28:23 +0000221 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000222 setOperationAction(ISD::FSIN , MVT::f64, Expand);
223 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000224 setOperationAction(ISD::FREM , MVT::f64, Expand);
225 setOperationAction(ISD::FSIN , MVT::f32, Expand);
226 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000227 setOperationAction(ISD::FREM , MVT::f32, Expand);
228
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000229 // Expand FP immediates into loads from the stack, except for the special
230 // cases we handle.
231 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
232 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000233 addLegalFPImmediate(+0.0); // xorps / xorpd
234 } else {
235 // Set up the FP register classes.
236 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000237
238 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
239
Chris Lattner76ac0682005-11-15 00:40:23 +0000240 if (!UnsafeFPMath) {
241 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
242 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
243 }
244
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000245 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000246 addLegalFPImmediate(+0.0); // FLD0
247 addLegalFPImmediate(+1.0); // FLD1
248 addLegalFPImmediate(-0.0); // FLD0/FCHS
249 addLegalFPImmediate(-1.0); // FLD1/FCHS
250 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000251
Evan Cheng19264272006-03-01 01:11:20 +0000252 // First set operation action for all vector types to expand. Then we
253 // will selectively turn on ones that can be effectively codegen'd.
254 for (unsigned VT = (unsigned)MVT::Vector + 1;
255 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
256 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
258 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
259 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000260 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000261 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000262 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000263 }
264
Evan Chengbc047222006-03-22 19:22:18 +0000265 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000266 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
267 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
268 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
269
Evan Cheng19264272006-03-01 01:11:20 +0000270 // FIXME: add MMX packed arithmetics
Evan Chengd5e905d2006-03-21 23:01:21 +0000271 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
272 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
273 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000274 }
275
Evan Chengbc047222006-03-22 19:22:18 +0000276 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000277 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
278
Evan Cheng92232302006-04-12 21:21:57 +0000279 setOperationAction(ISD::AND, MVT::v4f32, Legal);
280 setOperationAction(ISD::OR, MVT::v4f32, Legal);
281 setOperationAction(ISD::XOR, MVT::v4f32, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000282 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
283 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
284 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
285 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
286 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
287 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000288 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000289 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000290 }
291
Evan Chengbc047222006-03-22 19:22:18 +0000292 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000293 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
294 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
295 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
296 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
297 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
298
Evan Cheng617a6a82006-04-10 07:23:14 +0000299 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
300 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
301 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
302 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
303 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
304 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
305 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
306 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chenge4f97cc2006-04-13 05:10:25 +0000307 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000308 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
Evan Cheng92232302006-04-12 21:21:57 +0000309
Evan Cheng617a6a82006-04-10 07:23:14 +0000310 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
311 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000312 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng6e5e2052006-04-17 22:04:06 +0000313 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
314 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
315 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng617a6a82006-04-10 07:23:14 +0000316
Evan Cheng92232302006-04-12 21:21:57 +0000317 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
318 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
319 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
320 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
322 }
323 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
324 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
325 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
326 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
327 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
328 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
329
330 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
331 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
332 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
333 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
334 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
335 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
336 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
337 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Chenge2157c62006-04-12 17:12:36 +0000338 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
339 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng92232302006-04-12 21:21:57 +0000340 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
341 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng617a6a82006-04-10 07:23:14 +0000342 }
Evan Cheng92232302006-04-12 21:21:57 +0000343
344 // Custom lower v2i64 and v2f64 selects.
345 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenge2157c62006-04-12 17:12:36 +0000346 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng617a6a82006-04-10 07:23:14 +0000347 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng92232302006-04-12 21:21:57 +0000348 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000349 }
350
Evan Cheng78038292006-04-05 23:38:46 +0000351 // We want to custom lower some of our intrinsics.
352 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
353
Chris Lattner76ac0682005-11-15 00:40:23 +0000354 computeRegisterProperties();
355
Evan Cheng6a374562006-02-14 08:25:08 +0000356 // FIXME: These should be based on subtarget info. Plus, the values should
357 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000358 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
359 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
360 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000361 allowUnalignedMemoryAccesses = true; // x86 supports it!
362}
363
364std::vector<SDOperand>
365X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000366 std::vector<SDOperand> Args = TargetLowering::LowerArguments(F, DAG);
367
368 FormalArgs.clear();
369 // This sets BytesToPopOnReturn, BytesCallerReserves, etc. which have to be set
370 // before the rest of the function can be lowered.
Chris Lattner76ac0682005-11-15 00:40:23 +0000371 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000372 PreprocessFastCCArguments(Args[0], F, DAG);
373 else
374 PreprocessCCCArguments(Args[0], F, DAG);
375 return Args;
Chris Lattner76ac0682005-11-15 00:40:23 +0000376}
377
378std::pair<SDOperand, SDOperand>
379X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
380 bool isVarArg, unsigned CallingConv,
381 bool isTailCall,
382 SDOperand Callee, ArgListTy &Args,
383 SelectionDAG &DAG) {
384 assert((!isVarArg || CallingConv == CallingConv::C) &&
385 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000386
387 // If the callee is a GlobalAddress node (quite common, every direct call is)
388 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
389 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
390 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000391 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
392 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000393
Chris Lattner76ac0682005-11-15 00:40:23 +0000394 if (CallingConv == CallingConv::Fast && EnableFastCC)
395 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
396 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
397}
398
399//===----------------------------------------------------------------------===//
400// C Calling Convention implementation
401//===----------------------------------------------------------------------===//
402
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000403void X86TargetLowering::PreprocessCCCArguments(SDOperand Op, Function &F,
404 SelectionDAG &DAG) {
405 unsigned NumArgs = Op.Val->getNumValues();
406 MachineFunction &MF = DAG.getMachineFunction();
407 MachineFrameInfo *MFI = MF.getFrameInfo();
Chris Lattner76ac0682005-11-15 00:40:23 +0000408
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000409 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
410 for (unsigned i = 0; i < NumArgs; ++i) {
411 MVT::ValueType ObjectVT = Op.Val->getValueType(i);
412 unsigned ArgIncrement = 4;
413 unsigned ObjSize;
414 switch (ObjectVT) {
415 default: assert(0 && "Unhandled argument type!");
416 case MVT::i1:
417 case MVT::i8: ObjSize = 1; break;
418 case MVT::i16: ObjSize = 2; break;
419 case MVT::i32: ObjSize = 4; break;
420 case MVT::i64: ObjSize = ArgIncrement = 8; break;
421 case MVT::f32: ObjSize = 4; break;
422 case MVT::f64: ObjSize = ArgIncrement = 8; break;
423 }
424 ArgOffset += ArgIncrement; // Move on to the next argument...
425 }
426
427 // If the function takes variable number of arguments, make a frame index for
428 // the start of the first vararg value... for expansion of llvm.va_start.
429 if (F.isVarArg())
430 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
431 ReturnAddrIndex = 0; // No return address slot generated yet.
432 BytesToPopOnReturn = 0; // Callee pops nothing.
433 BytesCallerReserves = ArgOffset;
434}
435
436void X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG) {
437 unsigned NumArgs = Op.Val->getNumValues();
Chris Lattner76ac0682005-11-15 00:40:23 +0000438 MachineFunction &MF = DAG.getMachineFunction();
439 MachineFrameInfo *MFI = MF.getFrameInfo();
440
441 // Add DAG nodes to load the arguments... On entry to a function on the X86,
442 // the stack frame looks like this:
443 //
444 // [ESP] -- return address
445 // [ESP + 4] -- first argument (leftmost lexically)
446 // [ESP + 8] -- second argument, if first argument is four bytes in size
447 // ...
448 //
449 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000450 for (unsigned i = 0; i < NumArgs; ++i) {
451 MVT::ValueType ObjectVT = Op.Val->getValueType(i);
Chris Lattner76ac0682005-11-15 00:40:23 +0000452 unsigned ArgIncrement = 4;
453 unsigned ObjSize;
454 switch (ObjectVT) {
455 default: assert(0 && "Unhandled argument type!");
456 case MVT::i1:
457 case MVT::i8: ObjSize = 1; break;
458 case MVT::i16: ObjSize = 2; break;
459 case MVT::i32: ObjSize = 4; break;
460 case MVT::i64: ObjSize = ArgIncrement = 8; break;
461 case MVT::f32: ObjSize = 4; break;
462 case MVT::f64: ObjSize = ArgIncrement = 8; break;
463 }
464 // Create the frame index object for this incoming parameter...
465 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
466
467 // Create the SelectionDAG nodes corresponding to a load from this parameter
468 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
469
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000470 SDOperand ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
471 DAG.getSrcValue(NULL));
472 FormalArgs.push_back(ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000473 ArgOffset += ArgIncrement; // Move on to the next argument...
474 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000475}
476
477std::pair<SDOperand, SDOperand>
478X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
479 bool isVarArg, bool isTailCall,
480 SDOperand Callee, ArgListTy &Args,
481 SelectionDAG &DAG) {
482 // Count how many bytes are to be pushed on the stack.
483 unsigned NumBytes = 0;
484
485 if (Args.empty()) {
486 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000487 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000488 } else {
489 for (unsigned i = 0, e = Args.size(); i != e; ++i)
490 switch (getValueType(Args[i].second)) {
491 default: assert(0 && "Unknown value type!");
492 case MVT::i1:
493 case MVT::i8:
494 case MVT::i16:
495 case MVT::i32:
496 case MVT::f32:
497 NumBytes += 4;
498 break;
499 case MVT::i64:
500 case MVT::f64:
501 NumBytes += 8;
502 break;
503 }
504
Chris Lattner62c34842006-02-13 09:00:43 +0000505 Chain = DAG.getCALLSEQ_START(Chain,
506 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000507
508 // Arguments go on the stack in reverse order, as specified by the ABI.
509 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000510 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000511 std::vector<SDOperand> Stores;
512
513 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
514 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
515 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
516
517 switch (getValueType(Args[i].second)) {
518 default: assert(0 && "Unexpected ValueType for argument!");
519 case MVT::i1:
520 case MVT::i8:
521 case MVT::i16:
522 // Promote the integer to 32 bits. If the input type is signed use a
523 // sign extend, otherwise use a zero extend.
524 if (Args[i].second->isSigned())
525 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
526 else
527 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
528
529 // FALL THROUGH
530 case MVT::i32:
531 case MVT::f32:
532 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
533 Args[i].first, PtrOff,
534 DAG.getSrcValue(NULL)));
535 ArgOffset += 4;
536 break;
537 case MVT::i64:
538 case MVT::f64:
539 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
540 Args[i].first, PtrOff,
541 DAG.getSrcValue(NULL)));
542 ArgOffset += 8;
543 break;
544 }
545 }
546 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
547 }
548
549 std::vector<MVT::ValueType> RetVals;
550 MVT::ValueType RetTyVT = getValueType(RetTy);
551 RetVals.push_back(MVT::Other);
552
553 // The result values produced have to be legal. Promote the result.
554 switch (RetTyVT) {
555 case MVT::isVoid: break;
556 default:
557 RetVals.push_back(RetTyVT);
558 break;
559 case MVT::i1:
560 case MVT::i8:
561 case MVT::i16:
562 RetVals.push_back(MVT::i32);
563 break;
564 case MVT::f32:
565 if (X86ScalarSSE)
566 RetVals.push_back(MVT::f32);
567 else
568 RetVals.push_back(MVT::f64);
569 break;
570 case MVT::i64:
571 RetVals.push_back(MVT::i32);
572 RetVals.push_back(MVT::i32);
573 break;
574 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000575
Nate Begeman7e5496d2006-02-17 00:03:04 +0000576 std::vector<MVT::ValueType> NodeTys;
577 NodeTys.push_back(MVT::Other); // Returns a chain
578 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
579 std::vector<SDOperand> Ops;
580 Ops.push_back(Chain);
581 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000582
Nate Begeman7e5496d2006-02-17 00:03:04 +0000583 // FIXME: Do not generate X86ISD::TAILCALL for now.
584 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
585 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000586
Nate Begeman7e5496d2006-02-17 00:03:04 +0000587 NodeTys.clear();
588 NodeTys.push_back(MVT::Other); // Returns a chain
589 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
590 Ops.clear();
591 Ops.push_back(Chain);
592 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
593 Ops.push_back(DAG.getConstant(0, getPointerTy()));
594 Ops.push_back(InFlag);
595 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
596 InFlag = Chain.getValue(1);
597
598 SDOperand RetVal;
599 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000600 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000601 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000602 case MVT::i1:
603 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000604 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
605 Chain = RetVal.getValue(1);
606 if (RetTyVT == MVT::i1)
607 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
608 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000609 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000610 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
611 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000612 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000613 case MVT::i32:
614 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
615 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000616 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000617 case MVT::i64: {
618 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
619 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
620 Lo.getValue(2));
621 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
622 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000623 break;
624 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000625 case MVT::f32:
626 case MVT::f64: {
627 std::vector<MVT::ValueType> Tys;
628 Tys.push_back(MVT::f64);
629 Tys.push_back(MVT::Other);
630 Tys.push_back(MVT::Flag);
631 std::vector<SDOperand> Ops;
632 Ops.push_back(Chain);
633 Ops.push_back(InFlag);
634 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
635 Chain = RetVal.getValue(1);
636 InFlag = RetVal.getValue(2);
637 if (X86ScalarSSE) {
638 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
639 // shouldn't be necessary except that RFP cannot be live across
640 // multiple blocks. When stackifier is fixed, they can be uncoupled.
641 MachineFunction &MF = DAG.getMachineFunction();
642 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
643 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
644 Tys.clear();
645 Tys.push_back(MVT::Other);
646 Ops.clear();
647 Ops.push_back(Chain);
648 Ops.push_back(RetVal);
649 Ops.push_back(StackSlot);
650 Ops.push_back(DAG.getValueType(RetTyVT));
651 Ops.push_back(InFlag);
652 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
653 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
654 DAG.getSrcValue(NULL));
655 Chain = RetVal.getValue(1);
656 }
Evan Cheng45e190982006-01-05 00:27:02 +0000657
Nate Begeman7e5496d2006-02-17 00:03:04 +0000658 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
659 // FIXME: we would really like to remember that this FP_ROUND
660 // operation is okay to eliminate if we allow excess FP precision.
661 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
662 break;
663 }
664 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000665 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000666
667 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000668}
669
Chris Lattner76ac0682005-11-15 00:40:23 +0000670//===----------------------------------------------------------------------===//
671// Fast Calling Convention implementation
672//===----------------------------------------------------------------------===//
673//
674// The X86 'fast' calling convention passes up to two integer arguments in
675// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
676// and requires that the callee pop its arguments off the stack (allowing proper
677// tail calls), and has the same return value conventions as C calling convs.
678//
679// This calling convention always arranges for the callee pop value to be 8n+4
680// bytes, which is needed for tail recursion elimination and stack alignment
681// reasons.
682//
683// Note that this can be enhanced in the future to pass fp vals in registers
684// (when we have a global fp allocator) and do other tricks.
685//
686
687/// AddLiveIn - This helper function adds the specified physical register to the
688/// MachineFunction as a live in value. It also creates a corresponding virtual
689/// register for it.
690static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
691 TargetRegisterClass *RC) {
692 assert(RC->contains(PReg) && "Not the correct regclass!");
693 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
694 MF.addLiveIn(PReg, VReg);
695 return VReg;
696}
697
Chris Lattner388fc4d2006-03-17 17:27:47 +0000698// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
699// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
700// EDX". Anything more is illegal.
701//
702// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000703// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000704// a physreg with a virtreg, this increases the size of the physreg's live
705// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000706// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000707// allocator to wedge itself.
708//
709// This code triggers this problem more often if we pass args in registers,
710// so disable it until this is fixed.
711//
712// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
713// about code being dead.
714//
715static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000716
Chris Lattner76ac0682005-11-15 00:40:23 +0000717
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000718void
719X86TargetLowering::PreprocessFastCCArguments(SDOperand Op, Function &F,
720 SelectionDAG &DAG) {
721 unsigned NumArgs = Op.Val->getNumValues();
Chris Lattner76ac0682005-11-15 00:40:23 +0000722 MachineFunction &MF = DAG.getMachineFunction();
723 MachineFrameInfo *MFI = MF.getFrameInfo();
724
Chris Lattner76ac0682005-11-15 00:40:23 +0000725 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
726
727 // Keep track of the number of integer regs passed so far. This can be either
728 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
729 // used).
730 unsigned NumIntRegs = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000731
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000732 for (unsigned i = 0; i < NumArgs; ++i) {
733 MVT::ValueType ObjectVT = Op.Val->getValueType(i);
Chris Lattner76ac0682005-11-15 00:40:23 +0000734 unsigned ArgIncrement = 4;
735 unsigned ObjSize = 0;
736 SDOperand ArgValue;
737
738 switch (ObjectVT) {
739 default: assert(0 && "Unhandled argument type!");
740 case MVT::i1:
741 case MVT::i8:
Chris Lattner43798852006-03-17 05:10:20 +0000742 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000743 ++NumIntRegs;
744 break;
745 }
746
747 ObjSize = 1;
748 break;
749 case MVT::i16:
Chris Lattner43798852006-03-17 05:10:20 +0000750 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000751 ++NumIntRegs;
752 break;
753 }
754 ObjSize = 2;
755 break;
756 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000757 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000758 ++NumIntRegs;
759 break;
760 }
761 ObjSize = 4;
762 break;
763 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000764 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner43798852006-03-17 05:10:20 +0000765 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000766 break;
Chris Lattner43798852006-03-17 05:10:20 +0000767 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000768 ArgOffset += 4;
Chris Lattner43798852006-03-17 05:10:20 +0000769 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000770 break;
771 }
772 ObjSize = ArgIncrement = 8;
773 break;
774 case MVT::f32: ObjSize = 4; break;
775 case MVT::f64: ObjSize = ArgIncrement = 8; break;
776 }
777
Chris Lattner76ac0682005-11-15 00:40:23 +0000778 if (ObjSize)
779 ArgOffset += ArgIncrement; // Move on to the next argument.
780 }
781
782 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
783 // arguments and the arguments after the retaddr has been pushed are aligned.
784 if ((ArgOffset & 7) == 0)
785 ArgOffset += 4;
786
787 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
788 ReturnAddrIndex = 0; // No return address slot generated yet.
789 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
790 BytesCallerReserves = 0;
791
792 // Finally, inform the code generator which regs we return values in.
793 switch (getValueType(F.getReturnType())) {
794 default: assert(0 && "Unknown type!");
795 case MVT::isVoid: break;
796 case MVT::i1:
797 case MVT::i8:
798 case MVT::i16:
799 case MVT::i32:
800 MF.addLiveOut(X86::EAX);
801 break;
802 case MVT::i64:
803 MF.addLiveOut(X86::EAX);
804 MF.addLiveOut(X86::EDX);
805 break;
806 case MVT::f32:
807 case MVT::f64:
808 MF.addLiveOut(X86::ST0);
809 break;
810 }
Evan Chenge0bcfbe2006-04-26 01:20:17 +0000811}
812void
813X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
814 unsigned NumArgs = Op.Val->getNumValues();
815 MachineFunction &MF = DAG.getMachineFunction();
816 MachineFrameInfo *MFI = MF.getFrameInfo();
817
818 // Add DAG nodes to load the arguments... On entry to a function the stack
819 // frame looks like this:
820 //
821 // [ESP] -- return address
822 // [ESP + 4] -- first nonreg argument (leftmost lexically)
823 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
824 // ...
825 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
826
827 // Keep track of the number of integer regs passed so far. This can be either
828 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
829 // used).
830 unsigned NumIntRegs = 0;
831
832 for (unsigned i = 0; i < NumArgs; ++i) {
833 MVT::ValueType ObjectVT = Op.Val->getValueType(i);
834 unsigned ArgIncrement = 4;
835 unsigned ObjSize = 0;
836 SDOperand ArgValue;
837 bool hasUse = !Op.Val->hasNUsesOfValue(0, i);
838
839 switch (ObjectVT) {
840 default: assert(0 && "Unhandled argument type!");
841 case MVT::i1:
842 case MVT::i8:
843 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
844 if (hasUse) {
845 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
846 X86::R8RegisterClass);
847 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
848 DAG.setRoot(ArgValue.getValue(1));
849 if (ObjectVT == MVT::i1)
850 // FIXME: Should insert a assertzext here.
851 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
852 }
853 ++NumIntRegs;
854 break;
855 }
856
857 ObjSize = 1;
858 break;
859 case MVT::i16:
860 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
861 if (hasUse) {
862 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
863 X86::R16RegisterClass);
864 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
865 DAG.setRoot(ArgValue.getValue(1));
866 }
867 ++NumIntRegs;
868 break;
869 }
870 ObjSize = 2;
871 break;
872 case MVT::i32:
873 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
874 if (hasUse) {
875 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
876 X86::R32RegisterClass);
877 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
878 DAG.setRoot(ArgValue.getValue(1));
879 }
880 ++NumIntRegs;
881 break;
882 }
883 ObjSize = 4;
884 break;
885 case MVT::i64:
886 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
887 if (hasUse) {
888 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
889 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
890
891 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
892 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
893 DAG.setRoot(Hi.getValue(1));
894
895 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
896 }
897 NumIntRegs += 2;
898 break;
899 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
900 if (hasUse) {
901 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
902 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
903 DAG.setRoot(Low.getValue(1));
904
905 // Load the high part from memory.
906 // Create the frame index object for this incoming parameter...
907 int FI = MFI->CreateFixedObject(4, ArgOffset);
908 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
909 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
910 DAG.getSrcValue(NULL));
911 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
912 }
913 ArgOffset += 4;
914 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
915 break;
916 }
917 ObjSize = ArgIncrement = 8;
918 break;
919 case MVT::f32: ObjSize = 4; break;
920 case MVT::f64: ObjSize = ArgIncrement = 8; break;
921 }
922
923 if (ObjSize) {
924 // Create the frame index object for this incoming parameter...
925 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
926
927 // Create the SelectionDAG nodes corresponding to a load from this
928 // parameter.
929 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
930
931 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
932 DAG.getSrcValue(NULL));
933 } else if (ArgValue.Val == 0) {
934 if (MVT::isInteger(ObjectVT))
935 ArgValue = DAG.getConstant(0, ObjectVT);
936 else
937 ArgValue = DAG.getConstantFP(0, ObjectVT);
938 }
939 FormalArgs.push_back(ArgValue);
940 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000941}
942
943std::pair<SDOperand, SDOperand>
944X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
945 bool isTailCall, SDOperand Callee,
946 ArgListTy &Args, SelectionDAG &DAG) {
947 // Count how many bytes are to be pushed on the stack.
948 unsigned NumBytes = 0;
949
950 // Keep track of the number of integer regs passed so far. This can be either
951 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
952 // used).
953 unsigned NumIntRegs = 0;
954
955 for (unsigned i = 0, e = Args.size(); i != e; ++i)
956 switch (getValueType(Args[i].second)) {
957 default: assert(0 && "Unknown value type!");
958 case MVT::i1:
959 case MVT::i8:
960 case MVT::i16:
961 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000962 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000963 ++NumIntRegs;
964 break;
965 }
966 // fall through
967 case MVT::f32:
968 NumBytes += 4;
969 break;
970 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000971 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
972 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000973 break;
Chris Lattner43798852006-03-17 05:10:20 +0000974 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
975 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000976 NumBytes += 4;
977 break;
978 }
979
980 // fall through
981 case MVT::f64:
982 NumBytes += 8;
983 break;
984 }
985
986 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
987 // arguments and the arguments after the retaddr has been pushed are aligned.
988 if ((NumBytes & 7) == 0)
989 NumBytes += 4;
990
Chris Lattner62c34842006-02-13 09:00:43 +0000991 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000992
993 // Arguments go on the stack in reverse order, as specified by the ABI.
994 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000995 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000996 NumIntRegs = 0;
997 std::vector<SDOperand> Stores;
998 std::vector<SDOperand> RegValuesToPass;
999 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
1000 switch (getValueType(Args[i].second)) {
1001 default: assert(0 && "Unexpected ValueType for argument!");
1002 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +00001003 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
1004 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +00001005 case MVT::i8:
1006 case MVT::i16:
1007 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +00001008 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001009 RegValuesToPass.push_back(Args[i].first);
1010 ++NumIntRegs;
1011 break;
1012 }
1013 // Fall through
1014 case MVT::f32: {
1015 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1016 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1017 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1018 Args[i].first, PtrOff,
1019 DAG.getSrcValue(NULL)));
1020 ArgOffset += 4;
1021 break;
1022 }
1023 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +00001024 // Can pass (at least) part of it in regs?
1025 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001026 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1027 Args[i].first, DAG.getConstant(1, MVT::i32));
1028 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
1029 Args[i].first, DAG.getConstant(0, MVT::i32));
1030 RegValuesToPass.push_back(Lo);
1031 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +00001032
1033 // Pass both parts in regs?
1034 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +00001035 RegValuesToPass.push_back(Hi);
1036 ++NumIntRegs;
1037 } else {
1038 // Pass the high part in memory.
1039 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1040 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1041 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1042 Hi, PtrOff, DAG.getSrcValue(NULL)));
1043 ArgOffset += 4;
1044 }
1045 break;
1046 }
1047 // Fall through
1048 case MVT::f64:
1049 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
1050 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
1051 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
1052 Args[i].first, PtrOff,
1053 DAG.getSrcValue(NULL)));
1054 ArgOffset += 8;
1055 break;
1056 }
1057 }
1058 if (!Stores.empty())
1059 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
1060
1061 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1062 // arguments and the arguments after the retaddr has been pushed are aligned.
1063 if ((ArgOffset & 7) == 0)
1064 ArgOffset += 4;
1065
1066 std::vector<MVT::ValueType> RetVals;
1067 MVT::ValueType RetTyVT = getValueType(RetTy);
1068
1069 RetVals.push_back(MVT::Other);
1070
1071 // The result values produced have to be legal. Promote the result.
1072 switch (RetTyVT) {
1073 case MVT::isVoid: break;
1074 default:
1075 RetVals.push_back(RetTyVT);
1076 break;
1077 case MVT::i1:
1078 case MVT::i8:
1079 case MVT::i16:
1080 RetVals.push_back(MVT::i32);
1081 break;
1082 case MVT::f32:
1083 if (X86ScalarSSE)
1084 RetVals.push_back(MVT::f32);
1085 else
1086 RetVals.push_back(MVT::f64);
1087 break;
1088 case MVT::i64:
1089 RetVals.push_back(MVT::i32);
1090 RetVals.push_back(MVT::i32);
1091 break;
1092 }
1093
Nate Begeman7e5496d2006-02-17 00:03:04 +00001094 // Build a sequence of copy-to-reg nodes chained together with token chain
1095 // and flag operands which copy the outgoing args into registers.
1096 SDOperand InFlag;
1097 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1098 unsigned CCReg;
1099 SDOperand RegToPass = RegValuesToPass[i];
1100 switch (RegToPass.getValueType()) {
1101 default: assert(0 && "Bad thing to pass in regs");
1102 case MVT::i8:
1103 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001104 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001105 case MVT::i16:
1106 CCReg = (i == 0) ? X86::AX : X86::DX;
1107 break;
1108 case MVT::i32:
1109 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1110 break;
1111 }
1112
1113 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1114 InFlag = Chain.getValue(1);
1115 }
1116
1117 std::vector<MVT::ValueType> NodeTys;
1118 NodeTys.push_back(MVT::Other); // Returns a chain
1119 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1120 std::vector<SDOperand> Ops;
1121 Ops.push_back(Chain);
1122 Ops.push_back(Callee);
1123 if (InFlag.Val)
1124 Ops.push_back(InFlag);
1125
1126 // FIXME: Do not generate X86ISD::TAILCALL for now.
1127 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1128 InFlag = Chain.getValue(1);
1129
1130 NodeTys.clear();
1131 NodeTys.push_back(MVT::Other); // Returns a chain
1132 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1133 Ops.clear();
1134 Ops.push_back(Chain);
1135 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1136 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1137 Ops.push_back(InFlag);
1138 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1139 InFlag = Chain.getValue(1);
1140
1141 SDOperand RetVal;
1142 if (RetTyVT != MVT::isVoid) {
1143 switch (RetTyVT) {
1144 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001145 case MVT::i1:
1146 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001147 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1148 Chain = RetVal.getValue(1);
1149 if (RetTyVT == MVT::i1)
1150 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1151 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001152 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001153 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1154 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001155 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001156 case MVT::i32:
1157 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1158 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001159 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001160 case MVT::i64: {
1161 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1162 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1163 Lo.getValue(2));
1164 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1165 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001166 break;
1167 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001168 case MVT::f32:
1169 case MVT::f64: {
1170 std::vector<MVT::ValueType> Tys;
1171 Tys.push_back(MVT::f64);
1172 Tys.push_back(MVT::Other);
1173 Tys.push_back(MVT::Flag);
1174 std::vector<SDOperand> Ops;
1175 Ops.push_back(Chain);
1176 Ops.push_back(InFlag);
1177 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1178 Chain = RetVal.getValue(1);
1179 InFlag = RetVal.getValue(2);
1180 if (X86ScalarSSE) {
1181 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1182 // shouldn't be necessary except that RFP cannot be live across
1183 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1184 MachineFunction &MF = DAG.getMachineFunction();
1185 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1186 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1187 Tys.clear();
1188 Tys.push_back(MVT::Other);
1189 Ops.clear();
1190 Ops.push_back(Chain);
1191 Ops.push_back(RetVal);
1192 Ops.push_back(StackSlot);
1193 Ops.push_back(DAG.getValueType(RetTyVT));
1194 Ops.push_back(InFlag);
1195 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1196 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1197 DAG.getSrcValue(NULL));
1198 Chain = RetVal.getValue(1);
1199 }
Evan Cheng172fce72006-01-06 00:43:03 +00001200
Nate Begeman7e5496d2006-02-17 00:03:04 +00001201 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1202 // FIXME: we would really like to remember that this FP_ROUND
1203 // operation is okay to eliminate if we allow excess FP precision.
1204 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1205 break;
1206 }
1207 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001208 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001209
1210 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001211}
1212
1213SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1214 if (ReturnAddrIndex == 0) {
1215 // Set up a frame object for the return address.
1216 MachineFunction &MF = DAG.getMachineFunction();
1217 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1218 }
1219
1220 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1221}
1222
1223
1224
1225std::pair<SDOperand, SDOperand> X86TargetLowering::
1226LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1227 SelectionDAG &DAG) {
1228 SDOperand Result;
1229 if (Depth) // Depths > 0 not supported yet!
1230 Result = DAG.getConstant(0, getPointerTy());
1231 else {
1232 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1233 if (!isFrameAddress)
1234 // Just load the return address
1235 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1236 DAG.getSrcValue(NULL));
1237 else
1238 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1239 DAG.getConstant(4, MVT::i32));
1240 }
1241 return std::make_pair(Result, Chain);
1242}
1243
Evan Cheng339edad2006-01-11 00:33:36 +00001244/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1245/// which corresponds to the condition code.
1246static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1247 switch (X86CC) {
1248 default: assert(0 && "Unknown X86 conditional code!");
1249 case X86ISD::COND_A: return X86::JA;
1250 case X86ISD::COND_AE: return X86::JAE;
1251 case X86ISD::COND_B: return X86::JB;
1252 case X86ISD::COND_BE: return X86::JBE;
1253 case X86ISD::COND_E: return X86::JE;
1254 case X86ISD::COND_G: return X86::JG;
1255 case X86ISD::COND_GE: return X86::JGE;
1256 case X86ISD::COND_L: return X86::JL;
1257 case X86ISD::COND_LE: return X86::JLE;
1258 case X86ISD::COND_NE: return X86::JNE;
1259 case X86ISD::COND_NO: return X86::JNO;
1260 case X86ISD::COND_NP: return X86::JNP;
1261 case X86ISD::COND_NS: return X86::JNS;
1262 case X86ISD::COND_O: return X86::JO;
1263 case X86ISD::COND_P: return X86::JP;
1264 case X86ISD::COND_S: return X86::JS;
1265 }
1266}
Chris Lattner76ac0682005-11-15 00:40:23 +00001267
Evan Cheng45df7f82006-01-30 23:41:35 +00001268/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1269/// specific condition code. It returns a false if it cannot do a direct
1270/// translation. X86CC is the translated CondCode. Flip is set to true if the
1271/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001272static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1273 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001274 Flip = false;
1275 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001276 if (!isFP) {
1277 switch (SetCCOpcode) {
1278 default: break;
1279 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1280 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1281 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1282 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1283 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1284 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1285 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1286 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1287 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1288 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1289 }
1290 } else {
1291 // On a floating point condition, the flags are set as follows:
1292 // ZF PF CF op
1293 // 0 | 0 | 0 | X > Y
1294 // 0 | 0 | 1 | X < Y
1295 // 1 | 0 | 0 | X == Y
1296 // 1 | 1 | 1 | unordered
1297 switch (SetCCOpcode) {
1298 default: break;
1299 case ISD::SETUEQ:
1300 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001301 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001302 case ISD::SETOGT:
1303 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001304 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001305 case ISD::SETOGE:
1306 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001307 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001308 case ISD::SETULT:
1309 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Chengb3b41c42006-04-17 07:24:10 +00001310 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001311 case ISD::SETULE:
1312 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1313 case ISD::SETONE:
1314 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1315 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1316 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1317 }
1318 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001319
1320 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001321}
1322
Evan Cheng78038292006-04-05 23:38:46 +00001323static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1324 bool &Flip) {
1325 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1326}
1327
Evan Cheng339edad2006-01-11 00:33:36 +00001328/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1329/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001330/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001331static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001332 switch (X86CC) {
1333 default:
1334 return false;
1335 case X86ISD::COND_B:
1336 case X86ISD::COND_BE:
1337 case X86ISD::COND_E:
1338 case X86ISD::COND_P:
1339 case X86ISD::COND_A:
1340 case X86ISD::COND_AE:
1341 case X86ISD::COND_NE:
1342 case X86ISD::COND_NP:
1343 return true;
1344 }
1345}
1346
Evan Cheng339edad2006-01-11 00:33:36 +00001347MachineBasicBlock *
1348X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1349 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001350 switch (MI->getOpcode()) {
1351 default: assert(false && "Unexpected instr type to insert");
1352 case X86::CMOV_FR32:
Evan Cheng617a6a82006-04-10 07:23:14 +00001353 case X86::CMOV_FR64:
1354 case X86::CMOV_V4F32:
1355 case X86::CMOV_V2F64:
1356 case X86::CMOV_V2I64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001357 // To "insert" a SELECT_CC instruction, we actually have to insert the
1358 // diamond control-flow pattern. The incoming instruction knows the
1359 // destination vreg to set, the condition code register to branch on, the
1360 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001361 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1362 ilist<MachineBasicBlock>::iterator It = BB;
1363 ++It;
1364
1365 // thisMBB:
1366 // ...
1367 // TrueVal = ...
1368 // cmpTY ccX, r1, r2
1369 // bCC copy1MBB
1370 // fallthrough --> copy0MBB
1371 MachineBasicBlock *thisMBB = BB;
1372 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1373 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1374 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1375 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1376 MachineFunction *F = BB->getParent();
1377 F->getBasicBlockList().insert(It, copy0MBB);
1378 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001379 // Update machine-CFG edges by first adding all successors of the current
1380 // block to the new block which will contain the Phi node for the select.
1381 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1382 e = BB->succ_end(); i != e; ++i)
1383 sinkMBB->addSuccessor(*i);
1384 // Next, remove all successors of the current block, and add the true
1385 // and fallthrough blocks as its successors.
1386 while(!BB->succ_empty())
1387 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001388 BB->addSuccessor(copy0MBB);
1389 BB->addSuccessor(sinkMBB);
1390
1391 // copy0MBB:
1392 // %FalseValue = ...
1393 // # fallthrough to sinkMBB
1394 BB = copy0MBB;
1395
1396 // Update machine-CFG edges
1397 BB->addSuccessor(sinkMBB);
1398
1399 // sinkMBB:
1400 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1401 // ...
1402 BB = sinkMBB;
1403 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1404 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1405 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001406
Evan Cheng911c68d2006-01-16 21:21:29 +00001407 delete MI; // The pseudo instruction is gone now.
1408 return BB;
1409 }
Evan Cheng339edad2006-01-11 00:33:36 +00001410
Evan Cheng911c68d2006-01-16 21:21:29 +00001411 case X86::FP_TO_INT16_IN_MEM:
1412 case X86::FP_TO_INT32_IN_MEM:
1413 case X86::FP_TO_INT64_IN_MEM: {
1414 // Change the floating point control register to use "round towards zero"
1415 // mode when truncating to an integer value.
1416 MachineFunction *F = BB->getParent();
1417 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1418 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1419
1420 // Load the old value of the high byte of the control word...
1421 unsigned OldCW =
1422 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1423 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1424
1425 // Set the high part to be round to zero...
1426 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1427
1428 // Reload the modified control word now...
1429 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1430
1431 // Restore the memory image of control word to original value
1432 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1433
1434 // Get the X86 opcode to use.
1435 unsigned Opc;
1436 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001437 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001438 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1439 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1440 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1441 }
1442
1443 X86AddressMode AM;
1444 MachineOperand &Op = MI->getOperand(0);
1445 if (Op.isRegister()) {
1446 AM.BaseType = X86AddressMode::RegBase;
1447 AM.Base.Reg = Op.getReg();
1448 } else {
1449 AM.BaseType = X86AddressMode::FrameIndexBase;
1450 AM.Base.FrameIndex = Op.getFrameIndex();
1451 }
1452 Op = MI->getOperand(1);
1453 if (Op.isImmediate())
1454 AM.Scale = Op.getImmedValue();
1455 Op = MI->getOperand(2);
1456 if (Op.isImmediate())
1457 AM.IndexReg = Op.getImmedValue();
1458 Op = MI->getOperand(3);
1459 if (Op.isGlobalAddress()) {
1460 AM.GV = Op.getGlobal();
1461 } else {
1462 AM.Disp = Op.getImmedValue();
1463 }
1464 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1465
1466 // Reload the original control word now.
1467 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1468
1469 delete MI; // The pseudo instruction is gone now.
1470 return BB;
1471 }
1472 }
Evan Cheng339edad2006-01-11 00:33:36 +00001473}
1474
1475
1476//===----------------------------------------------------------------------===//
1477// X86 Custom Lowering Hooks
1478//===----------------------------------------------------------------------===//
1479
Evan Chengaf598d22006-03-13 23:18:16 +00001480/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1481/// load. For Darwin, external and weak symbols are indirect, loading the value
1482/// at address GV rather then the value of GV itself. This means that the
1483/// GlobalAddress must be in the base or index register of the address, not the
1484/// GV offset field.
1485static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1486 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1487 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1488}
1489
Evan Chengc995b452006-04-06 23:23:56 +00001490/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengac847262006-04-07 21:53:05 +00001491/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Chengc995b452006-04-06 23:23:56 +00001492static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1493 if (Op.getOpcode() == ISD::UNDEF)
1494 return true;
1495
1496 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengac847262006-04-07 21:53:05 +00001497 return (Val >= Low && Val < Hi);
1498}
1499
1500/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1501/// true if Op is undef or if its value equal to the specified value.
1502static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1503 if (Op.getOpcode() == ISD::UNDEF)
1504 return true;
1505 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Chengc995b452006-04-06 23:23:56 +00001506}
1507
Evan Cheng68ad48b2006-03-22 18:59:22 +00001508/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1509/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1510bool X86::isPSHUFDMask(SDNode *N) {
1511 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1512
1513 if (N->getNumOperands() != 4)
1514 return false;
1515
1516 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001517 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001518 SDOperand Arg = N->getOperand(i);
1519 if (Arg.getOpcode() == ISD::UNDEF) continue;
1520 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1521 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001522 return false;
1523 }
1524
1525 return true;
1526}
1527
1528/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001529/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001530bool X86::isPSHUFHWMask(SDNode *N) {
1531 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1532
1533 if (N->getNumOperands() != 8)
1534 return false;
1535
1536 // Lower quadword copied in order.
1537 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001538 SDOperand Arg = N->getOperand(i);
1539 if (Arg.getOpcode() == ISD::UNDEF) continue;
1540 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1541 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001542 return false;
1543 }
1544
1545 // Upper quadword shuffled.
1546 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001547 SDOperand Arg = N->getOperand(i);
1548 if (Arg.getOpcode() == ISD::UNDEF) continue;
1549 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1550 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001551 if (Val < 4 || Val > 7)
1552 return false;
1553 }
1554
1555 return true;
1556}
1557
1558/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001559/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001560bool X86::isPSHUFLWMask(SDNode *N) {
1561 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1562
1563 if (N->getNumOperands() != 8)
1564 return false;
1565
1566 // Upper quadword copied in order.
Evan Chengac847262006-04-07 21:53:05 +00001567 for (unsigned i = 4; i != 8; ++i)
1568 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Chengb7fedff2006-03-29 23:07:14 +00001569 return false;
Evan Chengb7fedff2006-03-29 23:07:14 +00001570
1571 // Lower quadword shuffled.
Evan Chengac847262006-04-07 21:53:05 +00001572 for (unsigned i = 0; i != 4; ++i)
1573 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Chengb7fedff2006-03-29 23:07:14 +00001574 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001575
1576 return true;
1577}
1578
Evan Chengd27fb3e2006-03-24 01:18:28 +00001579/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1580/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Evan Cheng60f0b892006-04-20 08:58:49 +00001581static bool isSHUFPMask(std::vector<SDOperand> &N) {
1582 unsigned NumElems = N.size();
1583 if (NumElems != 2 && NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001584
Evan Cheng60f0b892006-04-20 08:58:49 +00001585 unsigned Half = NumElems / 2;
1586 for (unsigned i = 0; i < Half; ++i)
1587 if (!isUndefOrInRange(N[i], 0, NumElems))
1588 return false;
1589 for (unsigned i = Half; i < NumElems; ++i)
1590 if (!isUndefOrInRange(N[i], NumElems, NumElems*2))
1591 return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001592
1593 return true;
1594}
1595
Evan Cheng60f0b892006-04-20 08:58:49 +00001596bool X86::isSHUFPMask(SDNode *N) {
1597 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1598 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1599 return ::isSHUFPMask(Ops);
1600}
1601
1602/// isCommutedSHUFP - Returns true if the shuffle mask is except
1603/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1604/// half elements to come from vector 1 (which would equal the dest.) and
1605/// the upper half to come from vector 2.
1606static bool isCommutedSHUFP(std::vector<SDOperand> &Ops) {
1607 unsigned NumElems = Ops.size();
1608 if (NumElems != 2 && NumElems != 4) return false;
1609
1610 unsigned Half = NumElems / 2;
1611 for (unsigned i = 0; i < Half; ++i)
1612 if (!isUndefOrInRange(Ops[i], NumElems, NumElems*2))
1613 return false;
1614 for (unsigned i = Half; i < NumElems; ++i)
1615 if (!isUndefOrInRange(Ops[i], 0, NumElems))
1616 return false;
1617 return true;
1618}
1619
1620static bool isCommutedSHUFP(SDNode *N) {
1621 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1622 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1623 return isCommutedSHUFP(Ops);
1624}
1625
Evan Cheng2595a682006-03-24 02:58:06 +00001626/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1627/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1628bool X86::isMOVHLPSMask(SDNode *N) {
1629 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1630
Evan Cheng1a194a52006-03-28 06:50:32 +00001631 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001632 return false;
1633
Evan Cheng1a194a52006-03-28 06:50:32 +00001634 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengac847262006-04-07 21:53:05 +00001635 return isUndefOrEqual(N->getOperand(0), 6) &&
1636 isUndefOrEqual(N->getOperand(1), 7) &&
1637 isUndefOrEqual(N->getOperand(2), 2) &&
1638 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng1a194a52006-03-28 06:50:32 +00001639}
1640
Evan Chengc995b452006-04-06 23:23:56 +00001641/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1642/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1643bool X86::isMOVLPMask(SDNode *N) {
1644 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1645
1646 unsigned NumElems = N->getNumOperands();
1647 if (NumElems != 2 && NumElems != 4)
1648 return false;
1649
Evan Chengac847262006-04-07 21:53:05 +00001650 for (unsigned i = 0; i < NumElems/2; ++i)
1651 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1652 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001653
Evan Chengac847262006-04-07 21:53:05 +00001654 for (unsigned i = NumElems/2; i < NumElems; ++i)
1655 if (!isUndefOrEqual(N->getOperand(i), i))
1656 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001657
1658 return true;
1659}
1660
1661/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng7855e4d2006-04-19 20:35:22 +00001662/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1663/// and MOVLHPS.
Evan Chengc995b452006-04-06 23:23:56 +00001664bool X86::isMOVHPMask(SDNode *N) {
1665 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1666
1667 unsigned NumElems = N->getNumOperands();
1668 if (NumElems != 2 && NumElems != 4)
1669 return false;
1670
Evan Chengac847262006-04-07 21:53:05 +00001671 for (unsigned i = 0; i < NumElems/2; ++i)
1672 if (!isUndefOrEqual(N->getOperand(i), i))
1673 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001674
1675 for (unsigned i = 0; i < NumElems/2; ++i) {
1676 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengac847262006-04-07 21:53:05 +00001677 if (!isUndefOrEqual(Arg, i + NumElems))
1678 return false;
Evan Chengc995b452006-04-06 23:23:56 +00001679 }
1680
1681 return true;
1682}
1683
Evan Cheng5df75882006-03-28 00:39:58 +00001684/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1685/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Evan Cheng60f0b892006-04-20 08:58:49 +00001686bool static isUNPCKLMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1687 unsigned NumElems = N.size();
Evan Cheng5df75882006-03-28 00:39:58 +00001688 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1689 return false;
1690
1691 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001692 SDOperand BitI = N[i];
1693 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001694 if (!isUndefOrEqual(BitI, j))
1695 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001696 if (V2IsSplat) {
1697 if (isUndefOrEqual(BitI1, NumElems))
1698 return false;
1699 } else {
1700 if (!isUndefOrEqual(BitI1, j + NumElems))
1701 return false;
1702 }
Evan Cheng5df75882006-03-28 00:39:58 +00001703 }
1704
1705 return true;
1706}
1707
Evan Cheng60f0b892006-04-20 08:58:49 +00001708bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1709 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1710 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1711 return ::isUNPCKLMask(Ops, V2IsSplat);
1712}
1713
Evan Cheng2bc32802006-03-28 02:43:26 +00001714/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1715/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Evan Cheng60f0b892006-04-20 08:58:49 +00001716bool static isUNPCKHMask(std::vector<SDOperand> &N, bool V2IsSplat = false) {
1717 unsigned NumElems = N.size();
Evan Cheng2bc32802006-03-28 02:43:26 +00001718 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1719 return false;
1720
1721 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001722 SDOperand BitI = N[i];
1723 SDOperand BitI1 = N[i+1];
Evan Chengac847262006-04-07 21:53:05 +00001724 if (!isUndefOrEqual(BitI, j + NumElems/2))
1725 return false;
Evan Cheng60f0b892006-04-20 08:58:49 +00001726 if (V2IsSplat) {
1727 if (isUndefOrEqual(BitI1, NumElems))
1728 return false;
1729 } else {
1730 if (!isUndefOrEqual(BitI1, j + NumElems/2 + NumElems))
1731 return false;
1732 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001733 }
1734
1735 return true;
1736}
1737
Evan Cheng60f0b892006-04-20 08:58:49 +00001738bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1739 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1740 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
1741 return ::isUNPCKHMask(Ops, V2IsSplat);
1742}
1743
Evan Chengf3b52c82006-04-05 07:20:06 +00001744/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1745/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1746/// <0, 0, 1, 1>
1747bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1748 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1749
1750 unsigned NumElems = N->getNumOperands();
1751 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1752 return false;
1753
1754 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1755 SDOperand BitI = N->getOperand(i);
1756 SDOperand BitI1 = N->getOperand(i+1);
1757
Evan Chengac847262006-04-07 21:53:05 +00001758 if (!isUndefOrEqual(BitI, j))
1759 return false;
1760 if (!isUndefOrEqual(BitI1, j))
1761 return false;
Evan Chengf3b52c82006-04-05 07:20:06 +00001762 }
1763
1764 return true;
1765}
1766
Evan Chenge8b51802006-04-21 01:05:10 +00001767/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1768/// specifies a shuffle of elements that is suitable for input to MOVSS,
1769/// MOVSD, and MOVD, i.e. setting the lowest element.
1770static bool isMOVLMask(std::vector<SDOperand> &N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001771 unsigned NumElems = N.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001772 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng12ba3e22006-04-11 00:19:04 +00001773 return false;
1774
Evan Cheng60f0b892006-04-20 08:58:49 +00001775 if (!isUndefOrEqual(N[0], NumElems))
Evan Cheng12ba3e22006-04-11 00:19:04 +00001776 return false;
1777
1778 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001779 SDOperand Arg = N[i];
Evan Cheng12ba3e22006-04-11 00:19:04 +00001780 if (!isUndefOrEqual(Arg, i))
1781 return false;
1782 }
1783
1784 return true;
1785}
Evan Chengf3b52c82006-04-05 07:20:06 +00001786
Evan Chenge8b51802006-04-21 01:05:10 +00001787bool X86::isMOVLMask(SDNode *N) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001788 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1789 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001790 return ::isMOVLMask(Ops);
Evan Cheng60f0b892006-04-20 08:58:49 +00001791}
1792
Evan Chenge8b51802006-04-21 01:05:10 +00001793/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1794/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng60f0b892006-04-20 08:58:49 +00001795/// element of vector 2 and the other elements to come from vector 1 in order.
Evan Chenge8b51802006-04-21 01:05:10 +00001796static bool isCommutedMOVL(std::vector<SDOperand> &Ops, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001797 unsigned NumElems = Ops.size();
Evan Chenge8b51802006-04-21 01:05:10 +00001798 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng60f0b892006-04-20 08:58:49 +00001799 return false;
1800
1801 if (!isUndefOrEqual(Ops[0], 0))
1802 return false;
1803
1804 for (unsigned i = 1; i < NumElems; ++i) {
1805 SDOperand Arg = Ops[i];
1806 if (V2IsSplat) {
1807 if (!isUndefOrEqual(Arg, NumElems))
1808 return false;
1809 } else {
1810 if (!isUndefOrEqual(Arg, i+NumElems))
1811 return false;
1812 }
1813 }
1814
1815 return true;
1816}
1817
Evan Chenge8b51802006-04-21 01:05:10 +00001818static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false) {
Evan Cheng60f0b892006-04-20 08:58:49 +00001819 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1820 std::vector<SDOperand> Ops(N->op_begin(), N->op_end());
Evan Chenge8b51802006-04-21 01:05:10 +00001821 return isCommutedMOVL(Ops, V2IsSplat);
Evan Cheng60f0b892006-04-20 08:58:49 +00001822}
1823
Evan Cheng5d247f82006-04-14 21:59:03 +00001824/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1825/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1826bool X86::isMOVSHDUPMask(SDNode *N) {
1827 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1828
1829 if (N->getNumOperands() != 4)
1830 return false;
1831
1832 // Expect 1, 1, 3, 3
1833 for (unsigned i = 0; i < 2; ++i) {
1834 SDOperand Arg = N->getOperand(i);
1835 if (Arg.getOpcode() == ISD::UNDEF) continue;
1836 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1837 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1838 if (Val != 1) return false;
1839 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001840
1841 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001842 for (unsigned i = 2; i < 4; ++i) {
1843 SDOperand Arg = N->getOperand(i);
1844 if (Arg.getOpcode() == ISD::UNDEF) continue;
1845 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1846 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1847 if (Val != 3) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001848 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001849 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001850
Evan Cheng6222cf22006-04-15 05:37:34 +00001851 // Don't use movshdup if it can be done with a shufps.
1852 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001853}
1854
1855/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1856/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1857bool X86::isMOVSLDUPMask(SDNode *N) {
1858 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1859
1860 if (N->getNumOperands() != 4)
1861 return false;
1862
1863 // Expect 0, 0, 2, 2
1864 for (unsigned i = 0; i < 2; ++i) {
1865 SDOperand Arg = N->getOperand(i);
1866 if (Arg.getOpcode() == ISD::UNDEF) continue;
1867 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1868 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1869 if (Val != 0) return false;
1870 }
Evan Cheng6222cf22006-04-15 05:37:34 +00001871
1872 bool HasHi = false;
Evan Cheng5d247f82006-04-14 21:59:03 +00001873 for (unsigned i = 2; i < 4; ++i) {
1874 SDOperand Arg = N->getOperand(i);
1875 if (Arg.getOpcode() == ISD::UNDEF) continue;
1876 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1877 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1878 if (Val != 2) return false;
Evan Cheng6222cf22006-04-15 05:37:34 +00001879 HasHi = true;
Evan Cheng5d247f82006-04-14 21:59:03 +00001880 }
Evan Cheng65bb7202006-04-15 03:13:24 +00001881
Evan Cheng6222cf22006-04-15 05:37:34 +00001882 // Don't use movshdup if it can be done with a shufps.
1883 return HasHi;
Evan Cheng5d247f82006-04-14 21:59:03 +00001884}
1885
Evan Chengd097e672006-03-22 02:53:00 +00001886/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1887/// a splat of a single element.
Evan Cheng5022b342006-04-17 20:43:08 +00001888static bool isSplatMask(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001889 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1890
Evan Chengd097e672006-03-22 02:53:00 +00001891 // This is a splat operation if each element of the permute is the same, and
1892 // if the value doesn't reference the second vector.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001893 unsigned NumElems = N->getNumOperands();
1894 SDOperand ElementBase;
1895 unsigned i = 0;
1896 for (; i != NumElems; ++i) {
1897 SDOperand Elt = N->getOperand(i);
1898 if (ConstantSDNode *EltV = dyn_cast<ConstantSDNode>(Elt)) {
1899 ElementBase = Elt;
1900 break;
1901 }
1902 }
1903
1904 if (!ElementBase.Val)
1905 return false;
1906
1907 for (; i != NumElems; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001908 SDOperand Arg = N->getOperand(i);
1909 if (Arg.getOpcode() == ISD::UNDEF) continue;
1910 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001911 if (Arg != ElementBase) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001912 }
1913
1914 // Make sure it is a splat of the first vector operand.
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001915 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengd097e672006-03-22 02:53:00 +00001916}
1917
Evan Cheng5022b342006-04-17 20:43:08 +00001918/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1919/// a splat of a single element and it's a 2 or 4 element mask.
1920bool X86::isSplatMask(SDNode *N) {
1921 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1922
Evan Cheng4a1b0d32006-04-19 23:28:59 +00001923 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Cheng5022b342006-04-17 20:43:08 +00001924 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1925 return false;
1926 return ::isSplatMask(N);
1927}
1928
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001929/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1930/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1931/// instructions.
1932unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001933 unsigned NumOperands = N->getNumOperands();
1934 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1935 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001936 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001937 unsigned Val = 0;
1938 SDOperand Arg = N->getOperand(NumOperands-i-1);
1939 if (Arg.getOpcode() != ISD::UNDEF)
1940 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001941 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001942 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001943 if (i != NumOperands - 1)
1944 Mask <<= Shift;
1945 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001946
1947 return Mask;
1948}
1949
Evan Chengb7fedff2006-03-29 23:07:14 +00001950/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1951/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1952/// instructions.
1953unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1954 unsigned Mask = 0;
1955 // 8 nodes, but we only care about the last 4.
1956 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001957 unsigned Val = 0;
1958 SDOperand Arg = N->getOperand(i);
1959 if (Arg.getOpcode() != ISD::UNDEF)
1960 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001961 Mask |= (Val - 4);
1962 if (i != 4)
1963 Mask <<= 2;
1964 }
1965
1966 return Mask;
1967}
1968
1969/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1970/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1971/// instructions.
1972unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1973 unsigned Mask = 0;
1974 // 8 nodes, but we only care about the first 4.
1975 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001976 unsigned Val = 0;
1977 SDOperand Arg = N->getOperand(i);
1978 if (Arg.getOpcode() != ISD::UNDEF)
1979 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001980 Mask |= Val;
1981 if (i != 0)
1982 Mask <<= 2;
1983 }
1984
1985 return Mask;
1986}
1987
Evan Cheng59a63552006-04-05 01:47:37 +00001988/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1989/// specifies a 8 element shuffle that can be broken into a pair of
1990/// PSHUFHW and PSHUFLW.
1991static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1992 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1993
1994 if (N->getNumOperands() != 8)
1995 return false;
1996
1997 // Lower quadword shuffled.
1998 for (unsigned i = 0; i != 4; ++i) {
1999 SDOperand Arg = N->getOperand(i);
2000 if (Arg.getOpcode() == ISD::UNDEF) continue;
2001 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2002 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2003 if (Val > 4)
2004 return false;
2005 }
2006
2007 // Upper quadword shuffled.
2008 for (unsigned i = 4; i != 8; ++i) {
2009 SDOperand Arg = N->getOperand(i);
2010 if (Arg.getOpcode() == ISD::UNDEF) continue;
2011 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2012 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2013 if (Val < 4 || Val > 7)
2014 return false;
2015 }
2016
2017 return true;
2018}
2019
Evan Chengc995b452006-04-06 23:23:56 +00002020/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2021/// values in ther permute mask.
2022static SDOperand CommuteVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
2023 SDOperand V1 = Op.getOperand(0);
2024 SDOperand V2 = Op.getOperand(1);
2025 SDOperand Mask = Op.getOperand(2);
2026 MVT::ValueType VT = Op.getValueType();
2027 MVT::ValueType MaskVT = Mask.getValueType();
2028 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2029 unsigned NumElems = Mask.getNumOperands();
2030 std::vector<SDOperand> MaskVec;
2031
2032 for (unsigned i = 0; i != NumElems; ++i) {
2033 SDOperand Arg = Mask.getOperand(i);
Evan Chenga3caaee2006-04-19 22:48:17 +00002034 if (Arg.getOpcode() == ISD::UNDEF) {
2035 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2036 continue;
2037 }
Evan Chengc995b452006-04-06 23:23:56 +00002038 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2039 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2040 if (Val < NumElems)
2041 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2042 else
2043 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2044 }
2045
2046 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2047 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
2048}
2049
Evan Cheng7855e4d2006-04-19 20:35:22 +00002050/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2051/// match movhlps. The lower half elements should come from upper half of
2052/// V1 (and in order), and the upper half elements should come from the upper
2053/// half of V2 (and in order).
2054static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2055 unsigned NumElems = Mask->getNumOperands();
2056 if (NumElems != 4)
2057 return false;
2058 for (unsigned i = 0, e = 2; i != e; ++i)
2059 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2060 return false;
2061 for (unsigned i = 2; i != 4; ++i)
2062 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2063 return false;
2064 return true;
2065}
2066
Evan Chengc995b452006-04-06 23:23:56 +00002067/// isScalarLoadToVector - Returns true if the node is a scalar load that
2068/// is promoted to a vector.
Evan Cheng7855e4d2006-04-19 20:35:22 +00002069static inline bool isScalarLoadToVector(SDNode *N) {
2070 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2071 N = N->getOperand(0).Val;
2072 return (N->getOpcode() == ISD::LOAD);
Evan Chengc995b452006-04-06 23:23:56 +00002073 }
2074 return false;
2075}
2076
Evan Cheng7855e4d2006-04-19 20:35:22 +00002077/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2078/// match movlp{s|d}. The lower half elements should come from lower half of
2079/// V1 (and in order), and the upper half elements should come from the upper
2080/// half of V2 (and in order). And since V1 will become the source of the
2081/// MOVLP, it must be either a vector load or a scalar load to vector.
2082static bool ShouldXformToMOVLP(SDNode *V1, SDNode *Mask) {
2083 if (V1->getOpcode() != ISD::LOAD && !isScalarLoadToVector(V1))
2084 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002085
Evan Cheng7855e4d2006-04-19 20:35:22 +00002086 unsigned NumElems = Mask->getNumOperands();
2087 if (NumElems != 2 && NumElems != 4)
2088 return false;
2089 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2090 if (!isUndefOrEqual(Mask->getOperand(i), i))
2091 return false;
2092 for (unsigned i = NumElems/2; i != NumElems; ++i)
2093 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2094 return false;
2095 return true;
Evan Chengc995b452006-04-06 23:23:56 +00002096}
2097
Evan Cheng60f0b892006-04-20 08:58:49 +00002098/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2099/// all the same.
2100static bool isSplatVector(SDNode *N) {
2101 if (N->getOpcode() != ISD::BUILD_VECTOR)
2102 return false;
Evan Chengc995b452006-04-06 23:23:56 +00002103
Evan Cheng60f0b892006-04-20 08:58:49 +00002104 SDOperand SplatValue = N->getOperand(0);
2105 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2106 if (N->getOperand(i) != SplatValue)
Evan Chengc995b452006-04-06 23:23:56 +00002107 return false;
2108 return true;
2109}
2110
Evan Cheng60f0b892006-04-20 08:58:49 +00002111/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2112/// that point to V2 points to its first element.
2113static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2114 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2115
2116 bool Changed = false;
2117 std::vector<SDOperand> MaskVec;
2118 unsigned NumElems = Mask.getNumOperands();
2119 for (unsigned i = 0; i != NumElems; ++i) {
2120 SDOperand Arg = Mask.getOperand(i);
2121 if (Arg.getOpcode() != ISD::UNDEF) {
2122 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2123 if (Val > NumElems) {
2124 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2125 Changed = true;
2126 }
2127 }
2128 MaskVec.push_back(Arg);
2129 }
2130
2131 if (Changed)
2132 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
2133 return Mask;
2134}
2135
Evan Chenge8b51802006-04-21 01:05:10 +00002136/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2137/// operation of specified width.
2138static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng60f0b892006-04-20 08:58:49 +00002139 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2140 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2141
2142 std::vector<SDOperand> MaskVec;
2143 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2144 for (unsigned i = 1; i != NumElems; ++i)
2145 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2146 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2147}
2148
Evan Cheng5022b342006-04-17 20:43:08 +00002149/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2150/// of specified width.
2151static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2152 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2153 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2154 std::vector<SDOperand> MaskVec;
2155 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2156 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2157 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2158 }
2159 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2160}
2161
Evan Cheng60f0b892006-04-20 08:58:49 +00002162/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2163/// of specified width.
2164static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2165 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2166 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2167 unsigned Half = NumElems/2;
2168 std::vector<SDOperand> MaskVec;
2169 for (unsigned i = 0; i != Half; ++i) {
2170 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2171 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2172 }
2173 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2174}
2175
Evan Chenge8b51802006-04-21 01:05:10 +00002176/// getZeroVector - Returns a vector of specified type with all zero elements.
2177///
2178static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2179 assert(MVT::isVector(VT) && "Expected a vector type");
2180 unsigned NumElems = getVectorNumElements(VT);
2181 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2182 bool isFP = MVT::isFloatingPoint(EVT);
2183 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2184 std::vector<SDOperand> ZeroVec(NumElems, Zero);
2185 return DAG.getNode(ISD::BUILD_VECTOR, VT, ZeroVec);
2186}
2187
Evan Cheng5022b342006-04-17 20:43:08 +00002188/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2189///
2190static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2191 SDOperand V1 = Op.getOperand(0);
Evan Chenge8b51802006-04-21 01:05:10 +00002192 SDOperand Mask = Op.getOperand(2);
Evan Cheng5022b342006-04-17 20:43:08 +00002193 MVT::ValueType VT = Op.getValueType();
Evan Chenge8b51802006-04-21 01:05:10 +00002194 unsigned NumElems = Mask.getNumOperands();
2195 Mask = getUnpacklMask(NumElems, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002196 while (NumElems != 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00002197 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002198 NumElems >>= 1;
2199 }
2200 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2201
2202 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Chenge8b51802006-04-21 01:05:10 +00002203 Mask = getZeroVector(MaskVT, DAG);
Evan Cheng5022b342006-04-17 20:43:08 +00002204 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Chenge8b51802006-04-21 01:05:10 +00002205 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Cheng5022b342006-04-17 20:43:08 +00002206 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2207}
2208
Evan Chenge8b51802006-04-21 01:05:10 +00002209/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2210/// constant +0.0.
2211static inline bool isZeroNode(SDOperand Elt) {
2212 return ((isa<ConstantSDNode>(Elt) &&
2213 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2214 (isa<ConstantFPSDNode>(Elt) &&
2215 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2216}
2217
Evan Cheng14215c32006-04-21 23:03:30 +00002218/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2219/// vector and zero or undef vector.
2220static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Chenge8b51802006-04-21 01:05:10 +00002221 unsigned NumElems, unsigned Idx,
Evan Cheng14215c32006-04-21 23:03:30 +00002222 bool isZero, SelectionDAG &DAG) {
2223 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Chenge8b51802006-04-21 01:05:10 +00002224 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2225 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2226 SDOperand Zero = DAG.getConstant(0, EVT);
2227 std::vector<SDOperand> MaskVec(NumElems, Zero);
2228 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
2229 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng14215c32006-04-21 23:03:30 +00002230 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Chenge8b51802006-04-21 01:05:10 +00002231}
2232
Evan Chengb0461082006-04-24 18:01:45 +00002233/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2234///
2235static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2236 unsigned NumNonZero, unsigned NumZero,
2237 SelectionDAG &DAG) {
2238 if (NumNonZero > 8)
2239 return SDOperand();
2240
2241 SDOperand V(0, 0);
2242 bool First = true;
2243 for (unsigned i = 0; i < 16; ++i) {
2244 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2245 if (ThisIsNonZero && First) {
2246 if (NumZero)
2247 V = getZeroVector(MVT::v8i16, DAG);
2248 else
2249 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2250 First = false;
2251 }
2252
2253 if ((i & 1) != 0) {
2254 SDOperand ThisElt(0, 0), LastElt(0, 0);
2255 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2256 if (LastIsNonZero) {
2257 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2258 }
2259 if (ThisIsNonZero) {
2260 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2261 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2262 ThisElt, DAG.getConstant(8, MVT::i8));
2263 if (LastIsNonZero)
2264 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2265 } else
2266 ThisElt = LastElt;
2267
2268 if (ThisElt.Val)
2269 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
2270 DAG.getConstant(i/2, MVT::i32));
2271 }
2272 }
2273
2274 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2275}
2276
2277/// LowerBuildVectorv16i8 - Custom lower build_vector of v8i16.
2278///
2279static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2280 unsigned NumNonZero, unsigned NumZero,
2281 SelectionDAG &DAG) {
2282 if (NumNonZero > 4)
2283 return SDOperand();
2284
2285 SDOperand V(0, 0);
2286 bool First = true;
2287 for (unsigned i = 0; i < 8; ++i) {
2288 bool isNonZero = (NonZeros & (1 << i)) != 0;
2289 if (isNonZero) {
2290 if (First) {
2291 if (NumZero)
2292 V = getZeroVector(MVT::v8i16, DAG);
2293 else
2294 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2295 First = false;
2296 }
2297 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
2298 DAG.getConstant(i, MVT::i32));
2299 }
2300 }
2301
2302 return V;
2303}
2304
Evan Chenga9467aa2006-04-25 20:13:52 +00002305SDOperand
2306X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2307 // All zero's are handled with pxor.
2308 if (ISD::isBuildVectorAllZeros(Op.Val))
2309 return Op;
2310
2311 // All one's are handled with pcmpeqd.
2312 if (ISD::isBuildVectorAllOnes(Op.Val))
2313 return Op;
2314
2315 MVT::ValueType VT = Op.getValueType();
2316 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2317 unsigned EVTBits = MVT::getSizeInBits(EVT);
2318
2319 unsigned NumElems = Op.getNumOperands();
2320 unsigned NumZero = 0;
2321 unsigned NumNonZero = 0;
2322 unsigned NonZeros = 0;
2323 std::set<SDOperand> Values;
2324 for (unsigned i = 0; i < NumElems; ++i) {
2325 SDOperand Elt = Op.getOperand(i);
2326 if (Elt.getOpcode() != ISD::UNDEF) {
2327 Values.insert(Elt);
2328 if (isZeroNode(Elt))
2329 NumZero++;
2330 else {
2331 NonZeros |= (1 << i);
2332 NumNonZero++;
2333 }
2334 }
2335 }
2336
2337 if (NumNonZero == 0)
2338 // Must be a mix of zero and undef. Return a zero vector.
2339 return getZeroVector(VT, DAG);
2340
2341 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2342 if (Values.size() == 1)
2343 return SDOperand();
2344
2345 // Special case for single non-zero element.
2346 if (NumNonZero == 1) {
2347 unsigned Idx = CountTrailingZeros_32(NonZeros);
2348 SDOperand Item = Op.getOperand(Idx);
2349 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2350 if (Idx == 0)
2351 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2352 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2353 NumZero > 0, DAG);
2354
2355 if (EVTBits == 32) {
2356 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2357 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2358 DAG);
2359 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2360 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2361 std::vector<SDOperand> MaskVec;
2362 for (unsigned i = 0; i < NumElems; i++)
2363 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
2364 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2365 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2366 DAG.getNode(ISD::UNDEF, VT), Mask);
2367 }
2368 }
2369
2370 // Let legalizer expand 2-widde build_vector's.
2371 if (EVTBits == 64)
2372 return SDOperand();
2373
2374 // If element VT is < 32 bits, convert it to inserts into a zero vector.
2375 if (EVTBits == 8) {
2376 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG);
2377 if (V.Val) return V;
2378 }
2379
2380 if (EVTBits == 16) {
2381 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG);
2382 if (V.Val) return V;
2383 }
2384
2385 // If element VT is == 32 bits, turn it into a number of shuffles.
2386 std::vector<SDOperand> V(NumElems);
2387 if (NumElems == 4 && NumZero > 0) {
2388 for (unsigned i = 0; i < 4; ++i) {
2389 bool isZero = !(NonZeros & (1 << i));
2390 if (isZero)
2391 V[i] = getZeroVector(VT, DAG);
2392 else
2393 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2394 }
2395
2396 for (unsigned i = 0; i < 2; ++i) {
2397 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2398 default: break;
2399 case 0:
2400 V[i] = V[i*2]; // Must be a zero vector.
2401 break;
2402 case 1:
2403 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2404 getMOVLMask(NumElems, DAG));
2405 break;
2406 case 2:
2407 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2408 getMOVLMask(NumElems, DAG));
2409 break;
2410 case 3:
2411 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2412 getUnpacklMask(NumElems, DAG));
2413 break;
2414 }
2415 }
2416
2417 // Take advantage of the fact R32 to VR128 scalar_to_vector (i.e. movd)
2418 // clears the upper bits.
2419 // FIXME: we can do the same for v4f32 case when we know both parts of
2420 // the lower half come from scalar_to_vector (loadf32). We should do
2421 // that in post legalizer dag combiner with target specific hooks.
2422 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
2423 return V[0];
2424 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2425 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2426 std::vector<SDOperand> MaskVec;
2427 bool Reverse = (NonZeros & 0x3) == 2;
2428 for (unsigned i = 0; i < 2; ++i)
2429 if (Reverse)
2430 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2431 else
2432 MaskVec.push_back(DAG.getConstant(i, EVT));
2433 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2434 for (unsigned i = 0; i < 2; ++i)
2435 if (Reverse)
2436 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2437 else
2438 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
2439 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2440 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2441 }
2442
2443 if (Values.size() > 2) {
2444 // Expand into a number of unpckl*.
2445 // e.g. for v4f32
2446 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2447 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2448 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2449 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2450 for (unsigned i = 0; i < NumElems; ++i)
2451 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2452 NumElems >>= 1;
2453 while (NumElems != 0) {
2454 for (unsigned i = 0; i < NumElems; ++i)
2455 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2456 UnpckMask);
2457 NumElems >>= 1;
2458 }
2459 return V[0];
2460 }
2461
2462 return SDOperand();
2463}
2464
2465SDOperand
2466X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2467 SDOperand V1 = Op.getOperand(0);
2468 SDOperand V2 = Op.getOperand(1);
2469 SDOperand PermMask = Op.getOperand(2);
2470 MVT::ValueType VT = Op.getValueType();
2471 unsigned NumElems = PermMask.getNumOperands();
2472 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2473 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
2474
2475 if (isSplatMask(PermMask.Val)) {
2476 if (NumElems <= 4) return Op;
2477 // Promote it to a v4i32 splat.
2478 return PromoteSplat(Op, DAG);
2479 }
2480
2481 if (X86::isMOVLMask(PermMask.Val))
2482 return (V1IsUndef) ? V2 : Op;
2483
2484 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2485 X86::isMOVSLDUPMask(PermMask.Val) ||
2486 X86::isMOVHLPSMask(PermMask.Val) ||
2487 X86::isMOVHPMask(PermMask.Val) ||
2488 X86::isMOVLPMask(PermMask.Val))
2489 return Op;
2490
2491 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2492 ShouldXformToMOVLP(V1.Val, PermMask.Val))
2493 return CommuteVectorShuffle(Op, DAG);
2494
2495 bool V1IsSplat = isSplatVector(V1.Val) || V1.getOpcode() == ISD::UNDEF;
2496 bool V2IsSplat = isSplatVector(V2.Val) || V2.getOpcode() == ISD::UNDEF;
2497 if (V1IsSplat && !V2IsSplat) {
2498 Op = CommuteVectorShuffle(Op, DAG);
2499 V1 = Op.getOperand(0);
2500 V2 = Op.getOperand(1);
2501 PermMask = Op.getOperand(2);
2502 V2IsSplat = true;
2503 }
2504
2505 if (isCommutedMOVL(PermMask.Val, V2IsSplat)) {
2506 if (V2IsUndef) return V1;
2507 Op = CommuteVectorShuffle(Op, DAG);
2508 V1 = Op.getOperand(0);
2509 V2 = Op.getOperand(1);
2510 PermMask = Op.getOperand(2);
2511 if (V2IsSplat) {
2512 // V2 is a splat, so the mask may be malformed. That is, it may point
2513 // to any V2 element. The instruction selectior won't like this. Get
2514 // a corrected mask and commute to form a proper MOVS{S|D}.
2515 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2516 if (NewMask.Val != PermMask.Val)
2517 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2518 }
2519 return Op;
2520 }
2521
2522 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
2523 X86::isUNPCKLMask(PermMask.Val) ||
2524 X86::isUNPCKHMask(PermMask.Val))
2525 return Op;
2526
2527 if (V2IsSplat) {
2528 // Normalize mask so all entries that point to V2 points to its first
2529 // element then try to match unpck{h|l} again. If match, return a
2530 // new vector_shuffle with the corrected mask.
2531 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2532 if (NewMask.Val != PermMask.Val) {
2533 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2534 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2535 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2536 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2537 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2538 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2539 }
2540 }
2541 }
2542
2543 // Normalize the node to match x86 shuffle ops if needed
2544 if (V2.getOpcode() != ISD::UNDEF)
2545 if (isCommutedSHUFP(PermMask.Val)) {
2546 Op = CommuteVectorShuffle(Op, DAG);
2547 V1 = Op.getOperand(0);
2548 V2 = Op.getOperand(1);
2549 PermMask = Op.getOperand(2);
2550 }
2551
2552 // If VT is integer, try PSHUF* first, then SHUFP*.
2553 if (MVT::isInteger(VT)) {
2554 if (X86::isPSHUFDMask(PermMask.Val) ||
2555 X86::isPSHUFHWMask(PermMask.Val) ||
2556 X86::isPSHUFLWMask(PermMask.Val)) {
2557 if (V2.getOpcode() != ISD::UNDEF)
2558 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2559 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2560 return Op;
2561 }
2562
2563 if (X86::isSHUFPMask(PermMask.Val))
2564 return Op;
2565
2566 // Handle v8i16 shuffle high / low shuffle node pair.
2567 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2568 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2569 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2570 std::vector<SDOperand> MaskVec;
2571 for (unsigned i = 0; i != 4; ++i)
2572 MaskVec.push_back(PermMask.getOperand(i));
2573 for (unsigned i = 4; i != 8; ++i)
2574 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2575 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2576 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2577 MaskVec.clear();
2578 for (unsigned i = 0; i != 4; ++i)
2579 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2580 for (unsigned i = 4; i != 8; ++i)
2581 MaskVec.push_back(PermMask.getOperand(i));
2582 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2583 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2584 }
2585 } else {
2586 // Floating point cases in the other order.
2587 if (X86::isSHUFPMask(PermMask.Val))
2588 return Op;
2589 if (X86::isPSHUFDMask(PermMask.Val) ||
2590 X86::isPSHUFHWMask(PermMask.Val) ||
2591 X86::isPSHUFLWMask(PermMask.Val)) {
2592 if (V2.getOpcode() != ISD::UNDEF)
2593 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2594 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2595 return Op;
2596 }
2597 }
2598
2599 if (NumElems == 4) {
2600 // Break it into (shuffle shuffle_hi, shuffle_lo).
2601 MVT::ValueType MaskVT = PermMask.getValueType();
2602 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
2603 std::map<unsigned, std::pair<int, int> > Locs;
2604 std::vector<SDOperand> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2605 std::vector<SDOperand> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2606 std::vector<SDOperand> *MaskPtr = &LoMask;
2607 unsigned MaskIdx = 0;
2608 unsigned LoIdx = 0;
2609 unsigned HiIdx = NumElems/2;
2610 for (unsigned i = 0; i != NumElems; ++i) {
2611 if (i == NumElems/2) {
2612 MaskPtr = &HiMask;
2613 MaskIdx = 1;
2614 LoIdx = 0;
2615 HiIdx = NumElems/2;
2616 }
2617 SDOperand Elt = PermMask.getOperand(i);
2618 if (Elt.getOpcode() == ISD::UNDEF) {
2619 Locs[i] = std::make_pair(-1, -1);
2620 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2621 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2622 (*MaskPtr)[LoIdx] = Elt;
2623 LoIdx++;
2624 } else {
2625 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2626 (*MaskPtr)[HiIdx] = Elt;
2627 HiIdx++;
2628 }
2629 }
2630
2631 SDOperand LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2632 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, LoMask));
2633 SDOperand HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
2634 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, HiMask));
2635 std::vector<SDOperand> MaskOps;
2636 for (unsigned i = 0; i != NumElems; ++i) {
2637 if (Locs[i].first == -1) {
2638 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2639 } else {
2640 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2641 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2642 }
2643 }
2644 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
2645 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskOps));
2646 }
2647
2648 return SDOperand();
2649}
2650
2651SDOperand
2652X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2653 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2654 return SDOperand();
2655
2656 MVT::ValueType VT = Op.getValueType();
2657 // TODO: handle v16i8.
2658 if (MVT::getSizeInBits(VT) == 16) {
2659 // Transform it so it match pextrw which produces a 32-bit result.
2660 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2661 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2662 Op.getOperand(0), Op.getOperand(1));
2663 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2664 DAG.getValueType(VT));
2665 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2666 } else if (MVT::getSizeInBits(VT) == 32) {
2667 SDOperand Vec = Op.getOperand(0);
2668 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2669 if (Idx == 0)
2670 return Op;
2671
2672 // SHUFPS the element to the lowest double word, then movss.
2673 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2674 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2675 MVT::getVectorBaseType(MaskVT));
2676 std::vector<SDOperand> IdxVec;
2677 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2678 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2679 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2680 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2681 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2682 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2683 Vec, Vec, Mask);
2684 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2685 DAG.getConstant(0, MVT::i32));
2686 } else if (MVT::getSizeInBits(VT) == 64) {
2687 SDOperand Vec = Op.getOperand(0);
2688 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2689 if (Idx == 0)
2690 return Op;
2691
2692 // UNPCKHPD the element to the lowest double word, then movsd.
2693 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2694 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2695 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2696 std::vector<SDOperand> IdxVec;
2697 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2698 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2699 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2700 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2701 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2702 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2703 DAG.getConstant(0, MVT::i32));
2704 }
2705
2706 return SDOperand();
2707}
2708
2709SDOperand
2710X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2711 // Transform it so it match pinsrw which expects a 16-bit value in a R32
2712 // as its second argument.
2713 MVT::ValueType VT = Op.getValueType();
2714 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2715 SDOperand N0 = Op.getOperand(0);
2716 SDOperand N1 = Op.getOperand(1);
2717 SDOperand N2 = Op.getOperand(2);
2718 if (MVT::getSizeInBits(BaseVT) == 16) {
2719 if (N1.getValueType() != MVT::i32)
2720 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2721 if (N2.getValueType() != MVT::i32)
2722 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2723 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2724 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2725 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2726 if (Idx == 0) {
2727 // Use a movss.
2728 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2729 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2730 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2731 std::vector<SDOperand> MaskVec;
2732 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2733 for (unsigned i = 1; i <= 3; ++i)
2734 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2735 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
2736 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec));
2737 } else {
2738 // Use two pinsrw instructions to insert a 32 bit value.
2739 Idx <<= 1;
2740 if (MVT::isFloatingPoint(N1.getValueType())) {
2741 if (N1.getOpcode() == ISD::LOAD) {
2742 // Just load directly from f32mem to R32.
2743 N1 = DAG.getLoad(MVT::i32, N1.getOperand(0), N1.getOperand(1),
2744 N1.getOperand(2));
2745 } else {
2746 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2747 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2748 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
2749 DAG.getConstant(0, MVT::i32));
2750 }
2751 }
2752 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2753 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2754 DAG.getConstant(Idx, MVT::i32));
2755 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2756 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
2757 DAG.getConstant(Idx+1, MVT::i32));
2758 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2759 }
2760 }
2761
2762 return SDOperand();
2763}
2764
2765SDOperand
2766X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2767 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2768 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2769}
2770
2771// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
2772// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2773// one of the above mentioned nodes. It has to be wrapped because otherwise
2774// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2775// be used to form addressing mode. These wrapped nodes will be selected
2776// into MOV32ri.
2777SDOperand
2778X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2779 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
2780 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2781 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2782 CP->getAlignment()));
2783 if (Subtarget->isTargetDarwin()) {
2784 // With PIC, the address is actually $g + Offset.
2785 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2786 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2787 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2788 }
2789
2790 return Result;
2791}
2792
2793SDOperand
2794X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2795 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2796 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2797 DAG.getTargetGlobalAddress(GV, getPointerTy()));
2798 if (Subtarget->isTargetDarwin()) {
2799 // With PIC, the address is actually $g + Offset.
2800 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2801 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2802 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2803
2804 // For Darwin, external and weak symbols are indirect, so we want to load
2805 // the value at address GV, not the value of GV itself. This means that
2806 // the GlobalAddress must be in the base or index register of the address,
2807 // not the GV offset field.
2808 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
2809 DarwinGVRequiresExtraLoad(GV))
2810 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
2811 Result, DAG.getSrcValue(NULL));
2812 }
2813
2814 return Result;
2815}
2816
2817SDOperand
2818X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
2819 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2820 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2821 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2822 if (Subtarget->isTargetDarwin()) {
2823 // With PIC, the address is actually $g + Offset.
2824 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2825 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2826 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2827 }
2828
2829 return Result;
2830}
2831
2832SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng9c249c32006-01-09 18:33:28 +00002833 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
2834 "Not an i64 shift!");
2835 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
2836 SDOperand ShOpLo = Op.getOperand(0);
2837 SDOperand ShOpHi = Op.getOperand(1);
2838 SDOperand ShAmt = Op.getOperand(2);
2839 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00002840 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00002841 : DAG.getConstant(0, MVT::i32);
2842
2843 SDOperand Tmp2, Tmp3;
2844 if (Op.getOpcode() == ISD::SHL_PARTS) {
2845 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
2846 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
2847 } else {
2848 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00002849 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00002850 }
2851
2852 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
2853 ShAmt, DAG.getConstant(32, MVT::i8));
2854
2855 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00002856 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00002857
2858 std::vector<MVT::ValueType> Tys;
2859 Tys.push_back(MVT::i32);
2860 Tys.push_back(MVT::Flag);
2861 std::vector<SDOperand> Ops;
2862 if (Op.getOpcode() == ISD::SHL_PARTS) {
2863 Ops.push_back(Tmp2);
2864 Ops.push_back(Tmp3);
2865 Ops.push_back(CC);
2866 Ops.push_back(InFlag);
2867 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2868 InFlag = Hi.getValue(1);
2869
2870 Ops.clear();
2871 Ops.push_back(Tmp3);
2872 Ops.push_back(Tmp1);
2873 Ops.push_back(CC);
2874 Ops.push_back(InFlag);
2875 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2876 } else {
2877 Ops.push_back(Tmp2);
2878 Ops.push_back(Tmp3);
2879 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00002880 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00002881 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2882 InFlag = Lo.getValue(1);
2883
2884 Ops.clear();
2885 Ops.push_back(Tmp3);
2886 Ops.push_back(Tmp1);
2887 Ops.push_back(CC);
2888 Ops.push_back(InFlag);
2889 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
2890 }
2891
2892 Tys.clear();
2893 Tys.push_back(MVT::i32);
2894 Tys.push_back(MVT::i32);
2895 Ops.clear();
2896 Ops.push_back(Lo);
2897 Ops.push_back(Hi);
2898 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Evan Chenga9467aa2006-04-25 20:13:52 +00002899}
Evan Cheng6305e502006-01-12 22:54:21 +00002900
Evan Chenga9467aa2006-04-25 20:13:52 +00002901SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
2902 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
2903 Op.getOperand(0).getValueType() >= MVT::i16 &&
2904 "Unknown SINT_TO_FP to lower!");
2905
2906 SDOperand Result;
2907 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
2908 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
2909 MachineFunction &MF = DAG.getMachineFunction();
2910 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2911 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2912 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
2913 DAG.getEntryNode(), Op.getOperand(0),
2914 StackSlot, DAG.getSrcValue(NULL));
2915
2916 // Build the FILD
2917 std::vector<MVT::ValueType> Tys;
2918 Tys.push_back(MVT::f64);
2919 Tys.push_back(MVT::Other);
2920 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
2921 std::vector<SDOperand> Ops;
2922 Ops.push_back(Chain);
2923 Ops.push_back(StackSlot);
2924 Ops.push_back(DAG.getValueType(SrcVT));
2925 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
2926 Tys, Ops);
2927
2928 if (X86ScalarSSE) {
2929 Chain = Result.getValue(1);
2930 SDOperand InFlag = Result.getValue(2);
2931
2932 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
2933 // shouldn't be necessary except that RFP cannot be live across
2934 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattner76ac0682005-11-15 00:40:23 +00002935 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga9467aa2006-04-25 20:13:52 +00002936 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattner76ac0682005-11-15 00:40:23 +00002937 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00002938 std::vector<MVT::ValueType> Tys;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002939 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00002940 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00002941 Ops.push_back(Chain);
Evan Chenga9467aa2006-04-25 20:13:52 +00002942 Ops.push_back(Result);
Chris Lattner76ac0682005-11-15 00:40:23 +00002943 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00002944 Ops.push_back(DAG.getValueType(Op.getValueType()));
2945 Ops.push_back(InFlag);
2946 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
2947 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
2948 DAG.getSrcValue(NULL));
Chris Lattner76ac0682005-11-15 00:40:23 +00002949 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002950
Evan Chenga9467aa2006-04-25 20:13:52 +00002951 return Result;
2952}
2953
2954SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
2955 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
2956 "Unknown FP_TO_SINT to lower!");
2957 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
2958 // stack slot.
2959 MachineFunction &MF = DAG.getMachineFunction();
2960 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
2961 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2962 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2963
2964 unsigned Opc;
2965 switch (Op.getValueType()) {
Chris Lattner76ac0682005-11-15 00:40:23 +00002966 default: assert(0 && "Invalid FP_TO_SINT to lower!");
2967 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
2968 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
2969 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Chenga9467aa2006-04-25 20:13:52 +00002970 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002971
Evan Chenga9467aa2006-04-25 20:13:52 +00002972 SDOperand Chain = DAG.getEntryNode();
2973 SDOperand Value = Op.getOperand(0);
2974 if (X86ScalarSSE) {
2975 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
2976 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
2977 DAG.getSrcValue(0));
2978 std::vector<MVT::ValueType> Tys;
2979 Tys.push_back(MVT::f64);
2980 Tys.push_back(MVT::Other);
Chris Lattner76ac0682005-11-15 00:40:23 +00002981 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002982 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00002983 Ops.push_back(StackSlot);
Evan Chenga9467aa2006-04-25 20:13:52 +00002984 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
2985 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2986 Chain = Value.getValue(1);
2987 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2988 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2989 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002990
Evan Chenga9467aa2006-04-25 20:13:52 +00002991 // Build the FP_TO_INT*_IN_MEM
2992 std::vector<SDOperand> Ops;
2993 Ops.push_back(Chain);
2994 Ops.push_back(Value);
2995 Ops.push_back(StackSlot);
2996 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
Evan Cheng172fce72006-01-06 00:43:03 +00002997
Evan Chenga9467aa2006-04-25 20:13:52 +00002998 // Load the result.
2999 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
3000 DAG.getSrcValue(NULL));
3001}
3002
3003SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3004 MVT::ValueType VT = Op.getValueType();
3005 const Type *OpNTy = MVT::getTypeForValueType(VT);
3006 std::vector<Constant*> CV;
3007 if (VT == MVT::f64) {
3008 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3009 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3010 } else {
3011 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3012 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3013 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3014 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3015 }
3016 Constant *CS = ConstantStruct::get(CV);
3017 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3018 SDOperand Mask
3019 = DAG.getNode(X86ISD::LOAD_PACK,
3020 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3021 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3022}
3023
3024SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3025 MVT::ValueType VT = Op.getValueType();
3026 const Type *OpNTy = MVT::getTypeForValueType(VT);
3027 std::vector<Constant*> CV;
3028 if (VT == MVT::f64) {
3029 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3030 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3031 } else {
3032 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3033 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3034 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3035 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3036 }
3037 Constant *CS = ConstantStruct::get(CV);
3038 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
3039 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK,
3040 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
3041 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3042}
3043
3044SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
3045 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3046 SDOperand Cond;
3047 SDOperand CC = Op.getOperand(2);
3048 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3049 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3050 bool Flip;
3051 unsigned X86CC;
3052 if (translateX86CC(CC, isFP, X86CC, Flip)) {
3053 if (Flip)
3054 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3055 Op.getOperand(1), Op.getOperand(0));
3056 else
Evan Cheng45df7f82006-01-30 23:41:35 +00003057 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3058 Op.getOperand(0), Op.getOperand(1));
Evan Chenga9467aa2006-04-25 20:13:52 +00003059 return DAG.getNode(X86ISD::SETCC, MVT::i8,
3060 DAG.getConstant(X86CC, MVT::i8), Cond);
3061 } else {
3062 assert(isFP && "Illegal integer SetCC!");
3063
3064 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
3065 Op.getOperand(0), Op.getOperand(1));
3066 std::vector<MVT::ValueType> Tys;
3067 std::vector<SDOperand> Ops;
3068 switch (SetCCOpcode) {
Evan Cheng172fce72006-01-06 00:43:03 +00003069 default: assert(false && "Illegal floating point SetCC!");
3070 case ISD::SETOEQ: { // !PF & ZF
3071 Tys.push_back(MVT::i8);
3072 Tys.push_back(MVT::Flag);
3073 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
3074 Ops.push_back(Cond);
3075 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3076 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3077 DAG.getConstant(X86ISD::COND_E, MVT::i8),
3078 Tmp1.getValue(1));
3079 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3080 }
Evan Cheng172fce72006-01-06 00:43:03 +00003081 case ISD::SETUNE: { // PF | !ZF
3082 Tys.push_back(MVT::i8);
3083 Tys.push_back(MVT::Flag);
3084 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
3085 Ops.push_back(Cond);
3086 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3087 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
3088 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
3089 Tmp1.getValue(1));
3090 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3091 }
Evan Cheng172fce72006-01-06 00:43:03 +00003092 }
Evan Chengc1583db2005-12-21 20:21:51 +00003093 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003094}
Evan Cheng45df7f82006-01-30 23:41:35 +00003095
Evan Chenga9467aa2006-04-25 20:13:52 +00003096SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
3097 MVT::ValueType VT = Op.getValueType();
3098 bool isFPStack = MVT::isFloatingPoint(VT) && !X86ScalarSSE;
3099 bool addTest = false;
3100 SDOperand Op0 = Op.getOperand(0);
3101 SDOperand Cond, CC;
3102 if (Op0.getOpcode() == ISD::SETCC)
3103 Op0 = LowerOperation(Op0, DAG);
Evan Cheng944d1e92006-01-26 02:13:10 +00003104
Evan Chenga9467aa2006-04-25 20:13:52 +00003105 if (Op0.getOpcode() == X86ISD::SETCC) {
3106 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3107 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3108 // have another use it will be eliminated.
3109 // If the X86ISD::SETCC has more than one use, then it's probably better
3110 // to use a test instead of duplicating the X86ISD::CMP (for register
3111 // pressure reason).
3112 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
3113 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3114 CmpOpc == X86ISD::UCOMI) {
3115 if (!Op0.hasOneUse()) {
3116 std::vector<MVT::ValueType> Tys;
3117 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
3118 Tys.push_back(Op0.Val->getValueType(i));
3119 std::vector<SDOperand> Ops;
3120 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
3121 Ops.push_back(Op0.getOperand(i));
3122 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3123 }
3124
3125 CC = Op0.getOperand(0);
3126 Cond = Op0.getOperand(1);
3127 // Make a copy as flag result cannot be used by more than one.
3128 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3129 Cond.getOperand(0), Cond.getOperand(1));
3130 addTest =
3131 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00003132 } else
3133 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003134 } else
3135 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00003136
Evan Chenga9467aa2006-04-25 20:13:52 +00003137 if (addTest) {
3138 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3139 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00003140 }
Evan Cheng45df7f82006-01-30 23:41:35 +00003141
Evan Chenga9467aa2006-04-25 20:13:52 +00003142 std::vector<MVT::ValueType> Tys;
3143 Tys.push_back(Op.getValueType());
3144 Tys.push_back(MVT::Flag);
3145 std::vector<SDOperand> Ops;
3146 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3147 // condition is true.
3148 Ops.push_back(Op.getOperand(2));
3149 Ops.push_back(Op.getOperand(1));
3150 Ops.push_back(CC);
3151 Ops.push_back(Cond);
3152 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
3153}
Evan Cheng944d1e92006-01-26 02:13:10 +00003154
Evan Chenga9467aa2006-04-25 20:13:52 +00003155SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
3156 bool addTest = false;
3157 SDOperand Cond = Op.getOperand(1);
3158 SDOperand Dest = Op.getOperand(2);
3159 SDOperand CC;
3160 if (Cond.getOpcode() == ISD::SETCC)
3161 Cond = LowerOperation(Cond, DAG);
3162
3163 if (Cond.getOpcode() == X86ISD::SETCC) {
3164 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3165 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
3166 // have another use it will be eliminated.
3167 // If the X86ISD::SETCC has more than one use, then it's probably better
3168 // to use a test instead of duplicating the X86ISD::CMP (for register
3169 // pressure reason).
3170 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
3171 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
3172 CmpOpc == X86ISD::UCOMI) {
3173 if (!Cond.hasOneUse()) {
3174 std::vector<MVT::ValueType> Tys;
3175 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
3176 Tys.push_back(Cond.Val->getValueType(i));
3177 std::vector<SDOperand> Ops;
3178 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
3179 Ops.push_back(Cond.getOperand(i));
3180 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
3181 }
3182
3183 CC = Cond.getOperand(0);
3184 Cond = Cond.getOperand(1);
3185 // Make a copy as flag result cannot be used by more than one.
3186 Cond = DAG.getNode(CmpOpc, MVT::Flag,
3187 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00003188 } else
3189 addTest = true;
Evan Chenga9467aa2006-04-25 20:13:52 +00003190 } else
3191 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00003192
Evan Chenga9467aa2006-04-25 20:13:52 +00003193 if (addTest) {
3194 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
3195 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
Evan Cheng6fc31042005-12-19 23:12:38 +00003196 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003197 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
3198 Op.getOperand(0), Op.getOperand(2), CC, Cond);
3199}
Evan Chengae986f12006-01-11 22:15:48 +00003200
Evan Chenga9467aa2006-04-25 20:13:52 +00003201SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3202 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3203 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
3204 DAG.getTargetJumpTable(JT->getIndex(),
3205 getPointerTy()));
3206 if (Subtarget->isTargetDarwin()) {
3207 // With PIC, the address is actually $g + Offset.
3208 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
3209 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3210 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Chengae986f12006-01-11 22:15:48 +00003211 }
Evan Cheng99470012006-02-25 09:55:19 +00003212
Evan Chenga9467aa2006-04-25 20:13:52 +00003213 return Result;
3214}
Evan Cheng5588de92006-02-18 00:15:05 +00003215
Evan Chenga9467aa2006-04-25 20:13:52 +00003216SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
3217 SDOperand Copy;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003218
Evan Chenga9467aa2006-04-25 20:13:52 +00003219 switch(Op.getNumOperands()) {
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003220 default:
3221 assert(0 && "Do not know how to return this many arguments!");
3222 abort();
Chris Lattnerc070c622006-04-17 20:32:50 +00003223 case 1: // ret void.
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003224 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
Evan Chenga9467aa2006-04-25 20:13:52 +00003225 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003226 case 2: {
3227 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
Chris Lattnerc070c622006-04-17 20:32:50 +00003228
3229 if (MVT::isVector(ArgVT)) {
3230 // Integer or FP vector result -> XMM0.
3231 if (DAG.getMachineFunction().liveout_empty())
3232 DAG.getMachineFunction().addLiveOut(X86::XMM0);
3233 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::XMM0, Op.getOperand(1),
3234 SDOperand());
3235 } else if (MVT::isInteger(ArgVT)) {
3236 // Integer result -> EAX
3237 if (DAG.getMachineFunction().liveout_empty())
3238 DAG.getMachineFunction().addLiveOut(X86::EAX);
3239
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003240 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
3241 SDOperand());
Chris Lattnerc070c622006-04-17 20:32:50 +00003242 } else if (!X86ScalarSSE) {
3243 // FP return with fp-stack value.
3244 if (DAG.getMachineFunction().liveout_empty())
3245 DAG.getMachineFunction().addLiveOut(X86::ST0);
3246
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003247 std::vector<MVT::ValueType> Tys;
3248 Tys.push_back(MVT::Other);
3249 Tys.push_back(MVT::Flag);
3250 std::vector<SDOperand> Ops;
3251 Ops.push_back(Op.getOperand(0));
3252 Ops.push_back(Op.getOperand(1));
3253 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3254 } else {
Chris Lattnerc070c622006-04-17 20:32:50 +00003255 // FP return with ScalarSSE (return on fp-stack).
3256 if (DAG.getMachineFunction().liveout_empty())
3257 DAG.getMachineFunction().addLiveOut(X86::ST0);
3258
Evan Chenge1ce4d72006-02-01 00:20:21 +00003259 SDOperand MemLoc;
3260 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00003261 SDOperand Value = Op.getOperand(1);
3262
Evan Chenga24617f2006-02-01 01:19:32 +00003263 if (Value.getOpcode() == ISD::LOAD &&
3264 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00003265 Chain = Value.getOperand(0);
3266 MemLoc = Value.getOperand(1);
3267 } else {
3268 // Spill the value to memory and reload it into top of stack.
3269 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
3270 MachineFunction &MF = DAG.getMachineFunction();
3271 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3272 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
3273 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
3274 Value, MemLoc, DAG.getSrcValue(0));
3275 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003276 std::vector<MVT::ValueType> Tys;
3277 Tys.push_back(MVT::f64);
3278 Tys.push_back(MVT::Other);
3279 std::vector<SDOperand> Ops;
3280 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00003281 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003282 Ops.push_back(DAG.getValueType(ArgVT));
3283 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
3284 Tys.clear();
3285 Tys.push_back(MVT::Other);
3286 Tys.push_back(MVT::Flag);
3287 Ops.clear();
3288 Ops.push_back(Copy.getValue(1));
3289 Ops.push_back(Copy);
3290 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
3291 }
3292 break;
3293 }
3294 case 3:
Chris Lattnerc070c622006-04-17 20:32:50 +00003295 if (DAG.getMachineFunction().liveout_empty()) {
3296 DAG.getMachineFunction().addLiveOut(X86::EAX);
3297 DAG.getMachineFunction().addLiveOut(X86::EDX);
3298 }
3299
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003300 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
3301 SDOperand());
3302 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
3303 break;
Nate Begeman8c47c3a2006-01-27 21:09:22 +00003304 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003305 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
3306 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
3307 Copy.getValue(1));
3308}
3309
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003310SDOperand
3311X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
3312 if (FormalArgs.size() == 0) {
3313 unsigned CC = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3314 if (CC == CallingConv::Fast && EnableFastCC)
3315 LowerFastCCArguments(Op, DAG);
3316 else
3317 LowerCCCArguments(Op, DAG);
3318 }
3319 return FormalArgs[Op.ResNo];
3320}
3321
Evan Chenga9467aa2006-04-25 20:13:52 +00003322SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3323 SDOperand InFlag(0, 0);
3324 SDOperand Chain = Op.getOperand(0);
3325 unsigned Align =
3326 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3327 if (Align == 0) Align = 1;
3328
3329 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3330 // If not DWORD aligned, call memset if size is less than the threshold.
3331 // It knows how to align to the right boundary first.
3332 if ((Align & 3) != 0 ||
3333 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3334 MVT::ValueType IntPtr = getPointerTy();
3335 const Type *IntPtrTy = getTargetData().getIntPtrType();
3336 std::vector<std::pair<SDOperand, const Type*> > Args;
3337 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3338 // Extend the ubyte argument to be an int value for the call.
3339 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3340 Args.push_back(std::make_pair(Val, IntPtrTy));
3341 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3342 std::pair<SDOperand,SDOperand> CallResult =
3343 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3344 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3345 return CallResult.second;
Evan Chengd5e905d2006-03-21 23:01:21 +00003346 }
Evan Chengd097e672006-03-22 02:53:00 +00003347
Evan Chenga9467aa2006-04-25 20:13:52 +00003348 MVT::ValueType AVT;
3349 SDOperand Count;
3350 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3351 unsigned BytesLeft = 0;
3352 bool TwoRepStos = false;
3353 if (ValC) {
3354 unsigned ValReg;
3355 unsigned Val = ValC->getValue() & 255;
Evan Chengc995b452006-04-06 23:23:56 +00003356
Evan Chenga9467aa2006-04-25 20:13:52 +00003357 // If the value is a constant, then we can potentially use larger sets.
3358 switch (Align & 3) {
3359 case 2: // WORD aligned
3360 AVT = MVT::i16;
3361 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3362 BytesLeft = I->getValue() % 2;
3363 Val = (Val << 8) | Val;
3364 ValReg = X86::AX;
3365 break;
3366 case 0: // DWORD aligned
3367 AVT = MVT::i32;
3368 if (I) {
3369 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3370 BytesLeft = I->getValue() % 4;
Evan Chenga3caaee2006-04-19 22:48:17 +00003371 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003372 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3373 DAG.getConstant(2, MVT::i8));
3374 TwoRepStos = true;
Evan Chenga3caaee2006-04-19 22:48:17 +00003375 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003376 Val = (Val << 8) | Val;
3377 Val = (Val << 16) | Val;
3378 ValReg = X86::EAX;
3379 break;
3380 default: // Byte aligned
3381 AVT = MVT::i8;
3382 Count = Op.getOperand(3);
3383 ValReg = X86::AL;
3384 break;
Evan Chenga3caaee2006-04-19 22:48:17 +00003385 }
3386
Evan Chenga9467aa2006-04-25 20:13:52 +00003387 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3388 InFlag);
3389 InFlag = Chain.getValue(1);
3390 } else {
3391 AVT = MVT::i8;
3392 Count = Op.getOperand(3);
3393 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3394 InFlag = Chain.getValue(1);
Evan Chengd097e672006-03-22 02:53:00 +00003395 }
Evan Chengb0461082006-04-24 18:01:45 +00003396
Evan Chenga9467aa2006-04-25 20:13:52 +00003397 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3398 InFlag = Chain.getValue(1);
3399 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3400 InFlag = Chain.getValue(1);
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00003401
Evan Chenga9467aa2006-04-25 20:13:52 +00003402 std::vector<MVT::ValueType> Tys;
3403 Tys.push_back(MVT::Other);
3404 Tys.push_back(MVT::Flag);
3405 std::vector<SDOperand> Ops;
3406 Ops.push_back(Chain);
3407 Ops.push_back(DAG.getValueType(AVT));
3408 Ops.push_back(InFlag);
3409 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
Evan Chengb0461082006-04-24 18:01:45 +00003410
Evan Chenga9467aa2006-04-25 20:13:52 +00003411 if (TwoRepStos) {
3412 InFlag = Chain.getValue(1);
3413 Count = Op.getOperand(3);
3414 MVT::ValueType CVT = Count.getValueType();
3415 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3416 DAG.getConstant(3, CVT));
3417 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3418 InFlag = Chain.getValue(1);
3419 Tys.clear();
3420 Tys.push_back(MVT::Other);
3421 Tys.push_back(MVT::Flag);
3422 Ops.clear();
3423 Ops.push_back(Chain);
3424 Ops.push_back(DAG.getValueType(MVT::i8));
3425 Ops.push_back(InFlag);
3426 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
3427 } else if (BytesLeft) {
3428 // Issue stores for the last 1 - 3 bytes.
3429 SDOperand Value;
3430 unsigned Val = ValC->getValue() & 255;
3431 unsigned Offset = I->getValue() - BytesLeft;
3432 SDOperand DstAddr = Op.getOperand(1);
3433 MVT::ValueType AddrVT = DstAddr.getValueType();
3434 if (BytesLeft >= 2) {
3435 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
3436 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3437 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3438 DAG.getConstant(Offset, AddrVT)),
3439 DAG.getSrcValue(NULL));
3440 BytesLeft -= 2;
3441 Offset += 2;
Evan Cheng082c8782006-03-24 07:29:27 +00003442 }
3443
Evan Chenga9467aa2006-04-25 20:13:52 +00003444 if (BytesLeft == 1) {
3445 Value = DAG.getConstant(Val, MVT::i8);
3446 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3447 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3448 DAG.getConstant(Offset, AddrVT)),
3449 DAG.getSrcValue(NULL));
Evan Cheng14215c32006-04-21 23:03:30 +00003450 }
Evan Cheng082c8782006-03-24 07:29:27 +00003451 }
Evan Chengebf10062006-04-03 20:53:28 +00003452
Evan Chenga9467aa2006-04-25 20:13:52 +00003453 return Chain;
3454}
Evan Chengebf10062006-04-03 20:53:28 +00003455
Evan Chenga9467aa2006-04-25 20:13:52 +00003456SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3457 SDOperand Chain = Op.getOperand(0);
3458 unsigned Align =
3459 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3460 if (Align == 0) Align = 1;
Evan Chengebf10062006-04-03 20:53:28 +00003461
Evan Chenga9467aa2006-04-25 20:13:52 +00003462 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3463 // If not DWORD aligned, call memcpy if size is less than the threshold.
3464 // It knows how to align to the right boundary first.
3465 if ((Align & 3) != 0 ||
3466 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3467 MVT::ValueType IntPtr = getPointerTy();
3468 const Type *IntPtrTy = getTargetData().getIntPtrType();
3469 std::vector<std::pair<SDOperand, const Type*> > Args;
3470 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
3471 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
3472 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
3473 std::pair<SDOperand,SDOperand> CallResult =
3474 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
3475 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3476 return CallResult.second;
Evan Chengcbffa462006-03-31 19:22:53 +00003477 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003478
3479 MVT::ValueType AVT;
3480 SDOperand Count;
3481 unsigned BytesLeft = 0;
3482 bool TwoRepMovs = false;
3483 switch (Align & 3) {
3484 case 2: // WORD aligned
3485 AVT = MVT::i16;
3486 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
3487 BytesLeft = I->getValue() % 2;
3488 break;
3489 case 0: // DWORD aligned
3490 AVT = MVT::i32;
3491 if (I) {
3492 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
3493 BytesLeft = I->getValue() % 4;
Evan Cheng54212062006-04-17 22:45:49 +00003494 } else {
Evan Chenga9467aa2006-04-25 20:13:52 +00003495 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
3496 DAG.getConstant(2, MVT::i8));
3497 TwoRepMovs = true;
Evan Cheng6e5e2052006-04-17 22:04:06 +00003498 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003499 break;
3500 default: // Byte aligned
3501 AVT = MVT::i8;
3502 Count = Op.getOperand(3);
3503 break;
3504 }
3505
3506 SDOperand InFlag(0, 0);
3507 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
3508 InFlag = Chain.getValue(1);
3509 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
3510 InFlag = Chain.getValue(1);
3511 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
3512 InFlag = Chain.getValue(1);
3513
3514 std::vector<MVT::ValueType> Tys;
3515 Tys.push_back(MVT::Other);
3516 Tys.push_back(MVT::Flag);
3517 std::vector<SDOperand> Ops;
3518 Ops.push_back(Chain);
3519 Ops.push_back(DAG.getValueType(AVT));
3520 Ops.push_back(InFlag);
3521 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3522
3523 if (TwoRepMovs) {
3524 InFlag = Chain.getValue(1);
3525 Count = Op.getOperand(3);
3526 MVT::ValueType CVT = Count.getValueType();
3527 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
3528 DAG.getConstant(3, CVT));
3529 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
3530 InFlag = Chain.getValue(1);
3531 Tys.clear();
3532 Tys.push_back(MVT::Other);
3533 Tys.push_back(MVT::Flag);
3534 Ops.clear();
3535 Ops.push_back(Chain);
3536 Ops.push_back(DAG.getValueType(MVT::i8));
3537 Ops.push_back(InFlag);
3538 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
3539 } else if (BytesLeft) {
3540 // Issue loads and stores for the last 1 - 3 bytes.
3541 unsigned Offset = I->getValue() - BytesLeft;
3542 SDOperand DstAddr = Op.getOperand(1);
3543 MVT::ValueType DstVT = DstAddr.getValueType();
3544 SDOperand SrcAddr = Op.getOperand(2);
3545 MVT::ValueType SrcVT = SrcAddr.getValueType();
3546 SDOperand Value;
3547 if (BytesLeft >= 2) {
3548 Value = DAG.getLoad(MVT::i16, Chain,
3549 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3550 DAG.getConstant(Offset, SrcVT)),
3551 DAG.getSrcValue(NULL));
3552 Chain = Value.getValue(1);
3553 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3554 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3555 DAG.getConstant(Offset, DstVT)),
3556 DAG.getSrcValue(NULL));
3557 BytesLeft -= 2;
3558 Offset += 2;
Evan Chengcbffa462006-03-31 19:22:53 +00003559 }
3560
Evan Chenga9467aa2006-04-25 20:13:52 +00003561 if (BytesLeft == 1) {
3562 Value = DAG.getLoad(MVT::i8, Chain,
3563 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3564 DAG.getConstant(Offset, SrcVT)),
3565 DAG.getSrcValue(NULL));
3566 Chain = Value.getValue(1);
3567 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
3568 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3569 DAG.getConstant(Offset, DstVT)),
3570 DAG.getSrcValue(NULL));
3571 }
Evan Chengcbffa462006-03-31 19:22:53 +00003572 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003573
3574 return Chain;
3575}
3576
3577SDOperand
3578X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
3579 std::vector<MVT::ValueType> Tys;
3580 Tys.push_back(MVT::Other);
3581 Tys.push_back(MVT::Flag);
3582 std::vector<SDOperand> Ops;
3583 Ops.push_back(Op.getOperand(0));
3584 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
3585 Ops.clear();
3586 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
3587 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
3588 MVT::i32, Ops[0].getValue(2)));
3589 Ops.push_back(Ops[1].getValue(1));
3590 Tys[0] = Tys[1] = MVT::i32;
3591 Tys.push_back(MVT::Other);
3592 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
3593}
3594
3595SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
3596 // vastart just stores the address of the VarArgsFrameIndex slot into the
3597 // memory location argument.
3598 // FIXME: Replace MVT::i32 with PointerTy
3599 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
3600 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
3601 Op.getOperand(1), Op.getOperand(2));
3602}
3603
3604SDOperand
3605X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3606 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3607 switch (IntNo) {
3608 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng78038292006-04-05 23:38:46 +00003609 // Comparison intrinsics.
Evan Chenga9467aa2006-04-25 20:13:52 +00003610 case Intrinsic::x86_sse_comieq_ss:
3611 case Intrinsic::x86_sse_comilt_ss:
3612 case Intrinsic::x86_sse_comile_ss:
3613 case Intrinsic::x86_sse_comigt_ss:
3614 case Intrinsic::x86_sse_comige_ss:
3615 case Intrinsic::x86_sse_comineq_ss:
3616 case Intrinsic::x86_sse_ucomieq_ss:
3617 case Intrinsic::x86_sse_ucomilt_ss:
3618 case Intrinsic::x86_sse_ucomile_ss:
3619 case Intrinsic::x86_sse_ucomigt_ss:
3620 case Intrinsic::x86_sse_ucomige_ss:
3621 case Intrinsic::x86_sse_ucomineq_ss:
3622 case Intrinsic::x86_sse2_comieq_sd:
3623 case Intrinsic::x86_sse2_comilt_sd:
3624 case Intrinsic::x86_sse2_comile_sd:
3625 case Intrinsic::x86_sse2_comigt_sd:
3626 case Intrinsic::x86_sse2_comige_sd:
3627 case Intrinsic::x86_sse2_comineq_sd:
3628 case Intrinsic::x86_sse2_ucomieq_sd:
3629 case Intrinsic::x86_sse2_ucomilt_sd:
3630 case Intrinsic::x86_sse2_ucomile_sd:
3631 case Intrinsic::x86_sse2_ucomigt_sd:
3632 case Intrinsic::x86_sse2_ucomige_sd:
3633 case Intrinsic::x86_sse2_ucomineq_sd: {
3634 unsigned Opc = 0;
3635 ISD::CondCode CC = ISD::SETCC_INVALID;
3636 switch (IntNo) {
3637 default: break;
3638 case Intrinsic::x86_sse_comieq_ss:
3639 case Intrinsic::x86_sse2_comieq_sd:
3640 Opc = X86ISD::COMI;
3641 CC = ISD::SETEQ;
3642 break;
Evan Cheng78038292006-04-05 23:38:46 +00003643 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003644 case Intrinsic::x86_sse2_comilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003645 Opc = X86ISD::COMI;
3646 CC = ISD::SETLT;
3647 break;
3648 case Intrinsic::x86_sse_comile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003649 case Intrinsic::x86_sse2_comile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003650 Opc = X86ISD::COMI;
3651 CC = ISD::SETLE;
3652 break;
3653 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003654 case Intrinsic::x86_sse2_comigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003655 Opc = X86ISD::COMI;
3656 CC = ISD::SETGT;
3657 break;
3658 case Intrinsic::x86_sse_comige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003659 case Intrinsic::x86_sse2_comige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003660 Opc = X86ISD::COMI;
3661 CC = ISD::SETGE;
3662 break;
3663 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003664 case Intrinsic::x86_sse2_comineq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003665 Opc = X86ISD::COMI;
3666 CC = ISD::SETNE;
3667 break;
3668 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003669 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003670 Opc = X86ISD::UCOMI;
3671 CC = ISD::SETEQ;
3672 break;
3673 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003674 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003675 Opc = X86ISD::UCOMI;
3676 CC = ISD::SETLT;
3677 break;
3678 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003679 case Intrinsic::x86_sse2_ucomile_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003680 Opc = X86ISD::UCOMI;
3681 CC = ISD::SETLE;
3682 break;
3683 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003684 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003685 Opc = X86ISD::UCOMI;
3686 CC = ISD::SETGT;
3687 break;
3688 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng78038292006-04-05 23:38:46 +00003689 case Intrinsic::x86_sse2_ucomige_sd:
Evan Chenga9467aa2006-04-25 20:13:52 +00003690 Opc = X86ISD::UCOMI;
3691 CC = ISD::SETGE;
3692 break;
3693 case Intrinsic::x86_sse_ucomineq_ss:
3694 case Intrinsic::x86_sse2_ucomineq_sd:
3695 Opc = X86ISD::UCOMI;
3696 CC = ISD::SETNE;
3697 break;
Evan Cheng78038292006-04-05 23:38:46 +00003698 }
Evan Chenga9467aa2006-04-25 20:13:52 +00003699 bool Flip;
3700 unsigned X86CC;
3701 translateX86CC(CC, true, X86CC, Flip);
3702 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
3703 Op.getOperand(Flip?1:2));
3704 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
3705 DAG.getConstant(X86CC, MVT::i8), Cond);
3706 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng78038292006-04-05 23:38:46 +00003707 }
Evan Cheng5c59d492005-12-23 07:31:11 +00003708 }
Chris Lattner76ac0682005-11-15 00:40:23 +00003709}
Evan Cheng6af02632005-12-20 06:22:03 +00003710
Evan Chenga9467aa2006-04-25 20:13:52 +00003711/// LowerOperation - Provide custom lowering hooks for some operations.
3712///
3713SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
3714 switch (Op.getOpcode()) {
3715 default: assert(0 && "Should not custom lower this!");
3716 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
3717 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
3718 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
3719 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
3720 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
3721 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
3722 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
3723 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
3724 case ISD::SHL_PARTS:
3725 case ISD::SRA_PARTS:
3726 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
3727 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
3728 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
3729 case ISD::FABS: return LowerFABS(Op, DAG);
3730 case ISD::FNEG: return LowerFNEG(Op, DAG);
3731 case ISD::SETCC: return LowerSETCC(Op, DAG);
3732 case ISD::SELECT: return LowerSELECT(Op, DAG);
3733 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
3734 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
3735 case ISD::RET: return LowerRET(Op, DAG);
Evan Chenge0bcfbe2006-04-26 01:20:17 +00003736 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Chenga9467aa2006-04-25 20:13:52 +00003737 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
3738 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
3739 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
3740 case ISD::VASTART: return LowerVASTART(Op, DAG);
3741 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
3742 }
3743}
3744
Evan Cheng6af02632005-12-20 06:22:03 +00003745const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
3746 switch (Opcode) {
3747 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00003748 case X86ISD::SHLD: return "X86ISD::SHLD";
3749 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00003750 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00003751 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00003752 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00003753 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00003754 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
3755 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
3756 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00003757 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00003758 case X86ISD::FST: return "X86ISD::FST";
3759 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00003760 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00003761 case X86ISD::CALL: return "X86ISD::CALL";
3762 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
3763 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
3764 case X86ISD::CMP: return "X86ISD::CMP";
3765 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00003766 case X86ISD::COMI: return "X86ISD::COMI";
3767 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00003768 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00003769 case X86ISD::CMOV: return "X86ISD::CMOV";
3770 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00003771 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00003772 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
3773 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00003774 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00003775 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00003776 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00003777 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00003778 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00003779 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00003780 }
3781}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003782
Nate Begeman8a77efe2006-02-16 21:11:51 +00003783void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
3784 uint64_t Mask,
3785 uint64_t &KnownZero,
3786 uint64_t &KnownOne,
3787 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003788 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00003789 assert((Opc >= ISD::BUILTIN_OP_END ||
3790 Opc == ISD::INTRINSIC_WO_CHAIN ||
3791 Opc == ISD::INTRINSIC_W_CHAIN ||
3792 Opc == ISD::INTRINSIC_VOID) &&
3793 "Should use MaskedValueIsZero if you don't know whether Op"
3794 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003795
Evan Cheng6d196db2006-04-05 06:11:20 +00003796 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003797 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00003798 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00003799 case X86ISD::SETCC:
3800 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3801 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003802 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003803}
Chris Lattnerc642aa52006-01-31 19:43:35 +00003804
3805std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00003806getRegClassForInlineAsmConstraint(const std::string &Constraint,
3807 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00003808 if (Constraint.size() == 1) {
3809 // FIXME: not handling fp-stack yet!
3810 // FIXME: not handling MMX registers yet ('y' constraint).
3811 switch (Constraint[0]) { // GCC X86 Constraint Letters
3812 default: break; // Unknown constriant letter
3813 case 'r': // GENERAL_REGS
3814 case 'R': // LEGACY_REGS
3815 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3816 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3817 case 'l': // INDEX_REGS
3818 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3819 X86::ESI, X86::EDI, X86::EBP, 0);
3820 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3821 case 'Q': // Q_REGS
3822 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
3823 case 'x': // SSE_REGS if SSE1 allowed
3824 if (Subtarget->hasSSE1())
3825 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3826 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3827 0);
3828 return std::vector<unsigned>();
3829 case 'Y': // SSE_REGS if SSE2 allowed
3830 if (Subtarget->hasSSE2())
3831 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3832 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3833 0);
3834 return std::vector<unsigned>();
3835 }
3836 }
3837
Chris Lattner7ad77df2006-02-22 00:56:39 +00003838 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00003839}
Evan Chengaf598d22006-03-13 23:18:16 +00003840
3841/// isLegalAddressImmediate - Return true if the integer value or
3842/// GlobalValue can be used as the offset of the target addressing mode.
3843bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3844 // X86 allows a sign-extended 32-bit immediate field.
3845 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3846}
3847
3848bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00003849 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00003850 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3851 if (RModel == Reloc::Static)
3852 return true;
3853 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00003854 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00003855 else
3856 return false;
3857 } else
3858 return true;
3859}
Evan Cheng68ad48b2006-03-22 18:59:22 +00003860
3861/// isShuffleMaskLegal - Targets can use this to indicate that they only
3862/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3863/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3864/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00003865bool
3866X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3867 // Only do shuffles on 128-bit vector types for now.
3868 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Chenga3caaee2006-04-19 22:48:17 +00003869 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng5022b342006-04-17 20:43:08 +00003870 isSplatMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00003871 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00003872 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00003873 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00003874 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00003875}
Evan Cheng60f0b892006-04-20 08:58:49 +00003876
3877bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
3878 MVT::ValueType EVT,
3879 SelectionDAG &DAG) const {
3880 unsigned NumElts = BVOps.size();
3881 // Only do shuffles on 128-bit vector types for now.
3882 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
3883 if (NumElts == 2) return true;
3884 if (NumElts == 4) {
Evan Chenge8b51802006-04-21 01:05:10 +00003885 return (isMOVLMask(BVOps) || isCommutedMOVL(BVOps, true) ||
Evan Cheng60f0b892006-04-20 08:58:49 +00003886 isSHUFPMask(BVOps) || isCommutedSHUFP(BVOps));
3887 }
3888 return false;
3889}