blob: 976286678da84994d28bcf9be4ac077c39b22456 [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 Chengaf598d22006-03-13 23:18:16 +000022#include "llvm/ADT/VectorExtras.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000027#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000029#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000030#include "llvm/Target/TargetOptions.h"
31using namespace llvm;
32
33// FIXME: temporary.
34#include "llvm/Support/CommandLine.h"
35static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
36 cl::desc("Enable fastcc on X86"));
37
38X86TargetLowering::X86TargetLowering(TargetMachine &TM)
39 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000040 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
42
Chris Lattner76ac0682005-11-15 00:40:23 +000043 // Set up the TargetLowering object.
44
45 // X86 is weird, it always uses i8 for shift amounts and setcc results.
46 setShiftAmountType(MVT::i8);
47 setSetCCResultType(MVT::i8);
48 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000049 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000051 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng20931a72006-03-16 21:47:42 +000052
Evan Chengbc047222006-03-22 19:22:18 +000053 if (!Subtarget->isTargetDarwin())
Evan Chengb09a56f2006-03-17 20:31:41 +000054 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
55 setUseUnderscoreSetJmpLongJmp(true);
56
Evan Cheng20931a72006-03-16 21:47:42 +000057 // Add legal addressing mode scale values.
58 addLegalAddressScale(8);
59 addLegalAddressScale(4);
60 addLegalAddressScale(2);
61 // Enter the ones which require both scale + index last. These are more
62 // expensive.
63 addLegalAddressScale(9);
64 addLegalAddressScale(5);
65 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000066
Chris Lattner76ac0682005-11-15 00:40:23 +000067 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000068 addRegisterClass(MVT::i8, X86::R8RegisterClass);
69 addRegisterClass(MVT::i16, X86::R16RegisterClass);
70 addRegisterClass(MVT::i32, X86::R32RegisterClass);
71
72 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
73 // operation.
74 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
75 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000077
78 if (X86ScalarSSE)
79 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
80 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
81 else
82 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000083
84 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
85 // this operation.
86 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
87 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000088 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000089 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000090 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000091 else {
92 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
93 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
94 }
Chris Lattner76ac0682005-11-15 00:40:23 +000095
Evan Cheng5b97fcf2006-01-30 08:02:57 +000096 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
97 // isn't legal.
98 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
99 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
100
Evan Cheng08390f62006-01-30 22:13:22 +0000101 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
102 // this operation.
103 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
104 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
105
106 if (X86ScalarSSE) {
107 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
108 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000111 }
112
113 // Handle FP_TO_UINT by promoting the destination to a larger signed
114 // conversion.
115 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
116 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
118
Evan Chengd13778e2006-02-18 07:26:17 +0000119 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000120 // Expand FP_TO_UINT into a select.
121 // FIXME: We would like to use a Custom expander here eventually to do
122 // the optimal thing for SSE vs. the default expansion in the legalizer.
123 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
124 else
Evan Chengd13778e2006-02-18 07:26:17 +0000125 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000126 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
127
Evan Cheng08390f62006-01-30 22:13:22 +0000128 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
129 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000130
Evan Cheng593bea72006-02-17 07:01:52 +0000131 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000132 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
133 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000134 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
135 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
138 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
139 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
140 setOperationAction(ISD::FREM , MVT::f64 , Expand);
141 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
142 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
143 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
145 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
146 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
148 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
149 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000150 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000151 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000152
Chris Lattner76ac0682005-11-15 00:40:23 +0000153 // These should be promoted to a larger select which is supported.
154 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
155 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000156
157 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000158 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
159 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
160 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
162 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
165 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000167 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000168 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000169 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000170 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000171 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000172 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000173 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000174 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
175 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000177 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000178 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
179 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000180
Chris Lattner9c415362005-11-29 06:16:21 +0000181 // We don't have line number support yet.
182 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000183 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000184 // FIXME - use subtarget debug flags
Evan Chengbc047222006-03-22 19:22:18 +0000185 if (!Subtarget->isTargetDarwin())
Evan Cheng30d7b702006-03-07 02:02:57 +0000186 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000187
Nate Begemane74795c2006-01-25 18:21:52 +0000188 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
189 setOperationAction(ISD::VASTART , MVT::Other, Custom);
190
191 // Use the default implementation.
192 setOperationAction(ISD::VAARG , MVT::Other, Expand);
193 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
194 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000195 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
196 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
197 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000198
Chris Lattner9c7f5032006-03-05 05:08:37 +0000199 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
200 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
201
Chris Lattner76ac0682005-11-15 00:40:23 +0000202 if (X86ScalarSSE) {
203 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000204 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
205 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000206
207 // SSE has no load+extend ops
208 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
209 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
210
Evan Cheng72d5c252006-01-31 22:28:30 +0000211 // Use ANDPD to simulate FABS.
212 setOperationAction(ISD::FABS , MVT::f64, Custom);
213 setOperationAction(ISD::FABS , MVT::f32, Custom);
214
215 // Use XORP to simulate FNEG.
216 setOperationAction(ISD::FNEG , MVT::f64, Custom);
217 setOperationAction(ISD::FNEG , MVT::f32, Custom);
218
Evan Chengd8fba3a2006-02-02 00:28:23 +0000219 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000220 setOperationAction(ISD::FSIN , MVT::f64, Expand);
221 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000222 setOperationAction(ISD::FREM , MVT::f64, Expand);
223 setOperationAction(ISD::FSIN , MVT::f32, Expand);
224 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f32, Expand);
226
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000227 // Expand FP immediates into loads from the stack, except for the special
228 // cases we handle.
229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000231 addLegalFPImmediate(+0.0); // xorps / xorpd
232 } else {
233 // Set up the FP register classes.
234 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000235
236 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
237
Chris Lattner76ac0682005-11-15 00:40:23 +0000238 if (!UnsafeFPMath) {
239 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
240 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
241 }
242
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000243 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000244 addLegalFPImmediate(+0.0); // FLD0
245 addLegalFPImmediate(+1.0); // FLD1
246 addLegalFPImmediate(-0.0); // FLD0/FCHS
247 addLegalFPImmediate(-1.0); // FLD1/FCHS
248 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000249
Evan Cheng19264272006-03-01 01:11:20 +0000250 // First set operation action for all vector types to expand. Then we
251 // will selectively turn on ones that can be effectively codegen'd.
252 for (unsigned VT = (unsigned)MVT::Vector + 1;
253 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000258 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000259 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000260 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000261 }
262
Evan Chengbc047222006-03-22 19:22:18 +0000263 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000264 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
266 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
267
Evan Cheng19264272006-03-01 01:11:20 +0000268 // FIXME: add MMX packed arithmetics
Evan Chengd5e905d2006-03-21 23:01:21 +0000269 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
271 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000272 }
273
Evan Chengbc047222006-03-22 19:22:18 +0000274 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000275 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
276
Evan Chengd5e905d2006-03-21 23:01:21 +0000277 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
278 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
279 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
280 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
Evan Cheng082c8782006-03-24 07:29:27 +0000281 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Evan Chengd097e672006-03-22 02:53:00 +0000282 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000283 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000284 }
285
Evan Chengbc047222006-03-22 19:22:18 +0000286 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000287 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
288 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
289 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
290 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
291 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
292
293
Evan Chengd5e905d2006-03-21 23:01:21 +0000294 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
Evan Chengb9b05502006-03-23 01:57:24 +0000295 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
296 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
297 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Chengd5e905d2006-03-21 23:01:21 +0000298 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
Evan Cheng6f7d31e2006-03-25 01:33:37 +0000299 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
300 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
301 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengd5e905d2006-03-21 23:01:21 +0000302 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
303 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chengb9b05502006-03-23 01:57:24 +0000304 setOperationAction(ISD::LOAD, MVT::v16i8, Legal);
305 setOperationAction(ISD::LOAD, MVT::v8i16, Legal);
306 setOperationAction(ISD::LOAD, MVT::v4i32, Legal);
307 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng5df75882006-03-28 00:39:58 +0000308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Cheng082c8782006-03-24 07:29:27 +0000310 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
311 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
312 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
313 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
314 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
Evan Chengd097e672006-03-22 02:53:00 +0000315 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng5df75882006-03-28 00:39:58 +0000316 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
317 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i16, Custom);
318 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
319 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000322 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000323 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000324 }
325
Chris Lattner76ac0682005-11-15 00:40:23 +0000326 computeRegisterProperties();
327
Evan Cheng6a374562006-02-14 08:25:08 +0000328 // FIXME: These should be based on subtarget info. Plus, the values should
329 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000330 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
331 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
332 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000333 allowUnalignedMemoryAccesses = true; // x86 supports it!
334}
335
336std::vector<SDOperand>
337X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
338 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
339 return LowerFastCCArguments(F, DAG);
340 return LowerCCCArguments(F, DAG);
341}
342
343std::pair<SDOperand, SDOperand>
344X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
345 bool isVarArg, unsigned CallingConv,
346 bool isTailCall,
347 SDOperand Callee, ArgListTy &Args,
348 SelectionDAG &DAG) {
349 assert((!isVarArg || CallingConv == CallingConv::C) &&
350 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000351
352 // If the callee is a GlobalAddress node (quite common, every direct call is)
353 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
354 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
355 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000356 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
357 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000358
Chris Lattner76ac0682005-11-15 00:40:23 +0000359 if (CallingConv == CallingConv::Fast && EnableFastCC)
360 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
361 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
362}
363
364//===----------------------------------------------------------------------===//
365// C Calling Convention implementation
366//===----------------------------------------------------------------------===//
367
368std::vector<SDOperand>
369X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
370 std::vector<SDOperand> ArgValues;
371
372 MachineFunction &MF = DAG.getMachineFunction();
373 MachineFrameInfo *MFI = MF.getFrameInfo();
374
375 // Add DAG nodes to load the arguments... On entry to a function on the X86,
376 // the stack frame looks like this:
377 //
378 // [ESP] -- return address
379 // [ESP + 4] -- first argument (leftmost lexically)
380 // [ESP + 8] -- second argument, if first argument is four bytes in size
381 // ...
382 //
383 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
384 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
385 MVT::ValueType ObjectVT = getValueType(I->getType());
386 unsigned ArgIncrement = 4;
387 unsigned ObjSize;
388 switch (ObjectVT) {
389 default: assert(0 && "Unhandled argument type!");
390 case MVT::i1:
391 case MVT::i8: ObjSize = 1; break;
392 case MVT::i16: ObjSize = 2; break;
393 case MVT::i32: ObjSize = 4; break;
394 case MVT::i64: ObjSize = ArgIncrement = 8; break;
395 case MVT::f32: ObjSize = 4; break;
396 case MVT::f64: ObjSize = ArgIncrement = 8; break;
397 }
398 // Create the frame index object for this incoming parameter...
399 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
400
401 // Create the SelectionDAG nodes corresponding to a load from this parameter
402 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
403
404 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
405 // dead loads.
406 SDOperand ArgValue;
407 if (!I->use_empty())
408 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
409 DAG.getSrcValue(NULL));
410 else {
411 if (MVT::isInteger(ObjectVT))
412 ArgValue = DAG.getConstant(0, ObjectVT);
413 else
414 ArgValue = DAG.getConstantFP(0, ObjectVT);
415 }
416 ArgValues.push_back(ArgValue);
417
418 ArgOffset += ArgIncrement; // Move on to the next argument...
419 }
420
421 // If the function takes variable number of arguments, make a frame index for
422 // the start of the first vararg value... for expansion of llvm.va_start.
423 if (F.isVarArg())
424 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
425 ReturnAddrIndex = 0; // No return address slot generated yet.
426 BytesToPopOnReturn = 0; // Callee pops nothing.
427 BytesCallerReserves = ArgOffset;
428
429 // Finally, inform the code generator which regs we return values in.
430 switch (getValueType(F.getReturnType())) {
431 default: assert(0 && "Unknown type!");
432 case MVT::isVoid: break;
433 case MVT::i1:
434 case MVT::i8:
435 case MVT::i16:
436 case MVT::i32:
437 MF.addLiveOut(X86::EAX);
438 break;
439 case MVT::i64:
440 MF.addLiveOut(X86::EAX);
441 MF.addLiveOut(X86::EDX);
442 break;
443 case MVT::f32:
444 case MVT::f64:
445 MF.addLiveOut(X86::ST0);
446 break;
447 }
448 return ArgValues;
449}
450
451std::pair<SDOperand, SDOperand>
452X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
453 bool isVarArg, bool isTailCall,
454 SDOperand Callee, ArgListTy &Args,
455 SelectionDAG &DAG) {
456 // Count how many bytes are to be pushed on the stack.
457 unsigned NumBytes = 0;
458
459 if (Args.empty()) {
460 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000461 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000462 } else {
463 for (unsigned i = 0, e = Args.size(); i != e; ++i)
464 switch (getValueType(Args[i].second)) {
465 default: assert(0 && "Unknown value type!");
466 case MVT::i1:
467 case MVT::i8:
468 case MVT::i16:
469 case MVT::i32:
470 case MVT::f32:
471 NumBytes += 4;
472 break;
473 case MVT::i64:
474 case MVT::f64:
475 NumBytes += 8;
476 break;
477 }
478
Chris Lattner62c34842006-02-13 09:00:43 +0000479 Chain = DAG.getCALLSEQ_START(Chain,
480 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000481
482 // Arguments go on the stack in reverse order, as specified by the ABI.
483 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000484 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000485 std::vector<SDOperand> Stores;
486
487 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
488 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
489 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
490
491 switch (getValueType(Args[i].second)) {
492 default: assert(0 && "Unexpected ValueType for argument!");
493 case MVT::i1:
494 case MVT::i8:
495 case MVT::i16:
496 // Promote the integer to 32 bits. If the input type is signed use a
497 // sign extend, otherwise use a zero extend.
498 if (Args[i].second->isSigned())
499 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
500 else
501 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
502
503 // FALL THROUGH
504 case MVT::i32:
505 case MVT::f32:
506 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
507 Args[i].first, PtrOff,
508 DAG.getSrcValue(NULL)));
509 ArgOffset += 4;
510 break;
511 case MVT::i64:
512 case MVT::f64:
513 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
514 Args[i].first, PtrOff,
515 DAG.getSrcValue(NULL)));
516 ArgOffset += 8;
517 break;
518 }
519 }
520 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
521 }
522
523 std::vector<MVT::ValueType> RetVals;
524 MVT::ValueType RetTyVT = getValueType(RetTy);
525 RetVals.push_back(MVT::Other);
526
527 // The result values produced have to be legal. Promote the result.
528 switch (RetTyVT) {
529 case MVT::isVoid: break;
530 default:
531 RetVals.push_back(RetTyVT);
532 break;
533 case MVT::i1:
534 case MVT::i8:
535 case MVT::i16:
536 RetVals.push_back(MVT::i32);
537 break;
538 case MVT::f32:
539 if (X86ScalarSSE)
540 RetVals.push_back(MVT::f32);
541 else
542 RetVals.push_back(MVT::f64);
543 break;
544 case MVT::i64:
545 RetVals.push_back(MVT::i32);
546 RetVals.push_back(MVT::i32);
547 break;
548 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000549
Nate Begeman7e5496d2006-02-17 00:03:04 +0000550 std::vector<MVT::ValueType> NodeTys;
551 NodeTys.push_back(MVT::Other); // Returns a chain
552 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
553 std::vector<SDOperand> Ops;
554 Ops.push_back(Chain);
555 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000556
Nate Begeman7e5496d2006-02-17 00:03:04 +0000557 // FIXME: Do not generate X86ISD::TAILCALL for now.
558 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
559 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000560
Nate Begeman7e5496d2006-02-17 00:03:04 +0000561 NodeTys.clear();
562 NodeTys.push_back(MVT::Other); // Returns a chain
563 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
564 Ops.clear();
565 Ops.push_back(Chain);
566 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
567 Ops.push_back(DAG.getConstant(0, getPointerTy()));
568 Ops.push_back(InFlag);
569 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
570 InFlag = Chain.getValue(1);
571
572 SDOperand RetVal;
573 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000574 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000575 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000576 case MVT::i1:
577 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000578 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
579 Chain = RetVal.getValue(1);
580 if (RetTyVT == MVT::i1)
581 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
582 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000583 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000584 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
585 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000586 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000587 case MVT::i32:
588 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
589 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000590 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000591 case MVT::i64: {
592 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
593 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
594 Lo.getValue(2));
595 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
596 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000597 break;
598 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000599 case MVT::f32:
600 case MVT::f64: {
601 std::vector<MVT::ValueType> Tys;
602 Tys.push_back(MVT::f64);
603 Tys.push_back(MVT::Other);
604 Tys.push_back(MVT::Flag);
605 std::vector<SDOperand> Ops;
606 Ops.push_back(Chain);
607 Ops.push_back(InFlag);
608 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
609 Chain = RetVal.getValue(1);
610 InFlag = RetVal.getValue(2);
611 if (X86ScalarSSE) {
612 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
613 // shouldn't be necessary except that RFP cannot be live across
614 // multiple blocks. When stackifier is fixed, they can be uncoupled.
615 MachineFunction &MF = DAG.getMachineFunction();
616 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
617 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
618 Tys.clear();
619 Tys.push_back(MVT::Other);
620 Ops.clear();
621 Ops.push_back(Chain);
622 Ops.push_back(RetVal);
623 Ops.push_back(StackSlot);
624 Ops.push_back(DAG.getValueType(RetTyVT));
625 Ops.push_back(InFlag);
626 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
627 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
628 DAG.getSrcValue(NULL));
629 Chain = RetVal.getValue(1);
630 }
Evan Cheng45e190982006-01-05 00:27:02 +0000631
Nate Begeman7e5496d2006-02-17 00:03:04 +0000632 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
633 // FIXME: we would really like to remember that this FP_ROUND
634 // operation is okay to eliminate if we allow excess FP precision.
635 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
636 break;
637 }
638 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000639 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000640
641 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000642}
643
Chris Lattner76ac0682005-11-15 00:40:23 +0000644//===----------------------------------------------------------------------===//
645// Fast Calling Convention implementation
646//===----------------------------------------------------------------------===//
647//
648// The X86 'fast' calling convention passes up to two integer arguments in
649// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
650// and requires that the callee pop its arguments off the stack (allowing proper
651// tail calls), and has the same return value conventions as C calling convs.
652//
653// This calling convention always arranges for the callee pop value to be 8n+4
654// bytes, which is needed for tail recursion elimination and stack alignment
655// reasons.
656//
657// Note that this can be enhanced in the future to pass fp vals in registers
658// (when we have a global fp allocator) and do other tricks.
659//
660
661/// AddLiveIn - This helper function adds the specified physical register to the
662/// MachineFunction as a live in value. It also creates a corresponding virtual
663/// register for it.
664static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
665 TargetRegisterClass *RC) {
666 assert(RC->contains(PReg) && "Not the correct regclass!");
667 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
668 MF.addLiveIn(PReg, VReg);
669 return VReg;
670}
671
Chris Lattner388fc4d2006-03-17 17:27:47 +0000672// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
673// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
674// EDX". Anything more is illegal.
675//
676// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000677// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000678// a physreg with a virtreg, this increases the size of the physreg's live
679// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000680// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000681// allocator to wedge itself.
682//
683// This code triggers this problem more often if we pass args in registers,
684// so disable it until this is fixed.
685//
686// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
687// about code being dead.
688//
689static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000690
Chris Lattner76ac0682005-11-15 00:40:23 +0000691
692std::vector<SDOperand>
693X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
694 std::vector<SDOperand> ArgValues;
695
696 MachineFunction &MF = DAG.getMachineFunction();
697 MachineFrameInfo *MFI = MF.getFrameInfo();
698
699 // Add DAG nodes to load the arguments... On entry to a function the stack
700 // frame looks like this:
701 //
702 // [ESP] -- return address
703 // [ESP + 4] -- first nonreg argument (leftmost lexically)
704 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
705 // ...
706 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
707
708 // Keep track of the number of integer regs passed so far. This can be either
709 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
710 // used).
711 unsigned NumIntRegs = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000712
Chris Lattner76ac0682005-11-15 00:40:23 +0000713 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
714 MVT::ValueType ObjectVT = getValueType(I->getType());
715 unsigned ArgIncrement = 4;
716 unsigned ObjSize = 0;
717 SDOperand ArgValue;
718
719 switch (ObjectVT) {
720 default: assert(0 && "Unhandled argument type!");
721 case MVT::i1:
722 case MVT::i8:
Chris Lattner43798852006-03-17 05:10:20 +0000723 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000724 if (!I->use_empty()) {
725 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
726 X86::R8RegisterClass);
727 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
728 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000729 if (ObjectVT == MVT::i1)
730 // FIXME: Should insert a assertzext here.
731 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000732 }
733 ++NumIntRegs;
734 break;
735 }
736
737 ObjSize = 1;
738 break;
739 case MVT::i16:
Chris Lattner43798852006-03-17 05:10:20 +0000740 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000741 if (!I->use_empty()) {
742 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
743 X86::R16RegisterClass);
744 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
745 DAG.setRoot(ArgValue.getValue(1));
746 }
747 ++NumIntRegs;
748 break;
749 }
750 ObjSize = 2;
751 break;
752 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000753 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000754 if (!I->use_empty()) {
Chris Lattner43798852006-03-17 05:10:20 +0000755 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattner76ac0682005-11-15 00:40:23 +0000756 X86::R32RegisterClass);
757 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
758 DAG.setRoot(ArgValue.getValue(1));
759 }
760 ++NumIntRegs;
761 break;
762 }
763 ObjSize = 4;
764 break;
765 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000766 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000767 if (!I->use_empty()) {
768 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
769 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
770
771 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
772 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
773 DAG.setRoot(Hi.getValue(1));
774
775 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
776 }
Chris Lattner43798852006-03-17 05:10:20 +0000777 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000778 break;
Chris Lattner43798852006-03-17 05:10:20 +0000779 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000780 if (!I->use_empty()) {
781 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
782 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
783 DAG.setRoot(Low.getValue(1));
784
785 // Load the high part from memory.
786 // Create the frame index object for this incoming parameter...
787 int FI = MFI->CreateFixedObject(4, ArgOffset);
788 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
789 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
790 DAG.getSrcValue(NULL));
791 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
792 }
793 ArgOffset += 4;
Chris Lattner43798852006-03-17 05:10:20 +0000794 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000795 break;
796 }
797 ObjSize = ArgIncrement = 8;
798 break;
799 case MVT::f32: ObjSize = 4; break;
800 case MVT::f64: ObjSize = ArgIncrement = 8; break;
801 }
802
803 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
804 // dead loads.
805 if (ObjSize && !I->use_empty()) {
806 // Create the frame index object for this incoming parameter...
807 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
808
809 // Create the SelectionDAG nodes corresponding to a load from this
810 // parameter.
811 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
812
813 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
814 DAG.getSrcValue(NULL));
815 } else if (ArgValue.Val == 0) {
816 if (MVT::isInteger(ObjectVT))
817 ArgValue = DAG.getConstant(0, ObjectVT);
818 else
819 ArgValue = DAG.getConstantFP(0, ObjectVT);
820 }
821 ArgValues.push_back(ArgValue);
822
823 if (ObjSize)
824 ArgOffset += ArgIncrement; // Move on to the next argument.
825 }
826
827 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
828 // arguments and the arguments after the retaddr has been pushed are aligned.
829 if ((ArgOffset & 7) == 0)
830 ArgOffset += 4;
831
832 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
833 ReturnAddrIndex = 0; // No return address slot generated yet.
834 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
835 BytesCallerReserves = 0;
836
837 // Finally, inform the code generator which regs we return values in.
838 switch (getValueType(F.getReturnType())) {
839 default: assert(0 && "Unknown type!");
840 case MVT::isVoid: break;
841 case MVT::i1:
842 case MVT::i8:
843 case MVT::i16:
844 case MVT::i32:
845 MF.addLiveOut(X86::EAX);
846 break;
847 case MVT::i64:
848 MF.addLiveOut(X86::EAX);
849 MF.addLiveOut(X86::EDX);
850 break;
851 case MVT::f32:
852 case MVT::f64:
853 MF.addLiveOut(X86::ST0);
854 break;
855 }
856 return ArgValues;
857}
858
859std::pair<SDOperand, SDOperand>
860X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
861 bool isTailCall, SDOperand Callee,
862 ArgListTy &Args, SelectionDAG &DAG) {
863 // Count how many bytes are to be pushed on the stack.
864 unsigned NumBytes = 0;
865
866 // Keep track of the number of integer regs passed so far. This can be either
867 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
868 // used).
869 unsigned NumIntRegs = 0;
870
871 for (unsigned i = 0, e = Args.size(); i != e; ++i)
872 switch (getValueType(Args[i].second)) {
873 default: assert(0 && "Unknown value type!");
874 case MVT::i1:
875 case MVT::i8:
876 case MVT::i16:
877 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000878 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000879 ++NumIntRegs;
880 break;
881 }
882 // fall through
883 case MVT::f32:
884 NumBytes += 4;
885 break;
886 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000887 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
888 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000889 break;
Chris Lattner43798852006-03-17 05:10:20 +0000890 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
891 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000892 NumBytes += 4;
893 break;
894 }
895
896 // fall through
897 case MVT::f64:
898 NumBytes += 8;
899 break;
900 }
901
902 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
903 // arguments and the arguments after the retaddr has been pushed are aligned.
904 if ((NumBytes & 7) == 0)
905 NumBytes += 4;
906
Chris Lattner62c34842006-02-13 09:00:43 +0000907 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000908
909 // Arguments go on the stack in reverse order, as specified by the ABI.
910 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000911 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000912 NumIntRegs = 0;
913 std::vector<SDOperand> Stores;
914 std::vector<SDOperand> RegValuesToPass;
915 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
916 switch (getValueType(Args[i].second)) {
917 default: assert(0 && "Unexpected ValueType for argument!");
918 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000919 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
920 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000921 case MVT::i8:
922 case MVT::i16:
923 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000924 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000925 RegValuesToPass.push_back(Args[i].first);
926 ++NumIntRegs;
927 break;
928 }
929 // Fall through
930 case MVT::f32: {
931 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
932 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
933 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
934 Args[i].first, PtrOff,
935 DAG.getSrcValue(NULL)));
936 ArgOffset += 4;
937 break;
938 }
939 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000940 // Can pass (at least) part of it in regs?
941 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000942 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
943 Args[i].first, DAG.getConstant(1, MVT::i32));
944 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
945 Args[i].first, DAG.getConstant(0, MVT::i32));
946 RegValuesToPass.push_back(Lo);
947 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +0000948
949 // Pass both parts in regs?
950 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000951 RegValuesToPass.push_back(Hi);
952 ++NumIntRegs;
953 } else {
954 // Pass the high part in memory.
955 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
956 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
957 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
958 Hi, PtrOff, DAG.getSrcValue(NULL)));
959 ArgOffset += 4;
960 }
961 break;
962 }
963 // Fall through
964 case MVT::f64:
965 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
966 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
967 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
968 Args[i].first, PtrOff,
969 DAG.getSrcValue(NULL)));
970 ArgOffset += 8;
971 break;
972 }
973 }
974 if (!Stores.empty())
975 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
976
977 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
978 // arguments and the arguments after the retaddr has been pushed are aligned.
979 if ((ArgOffset & 7) == 0)
980 ArgOffset += 4;
981
982 std::vector<MVT::ValueType> RetVals;
983 MVT::ValueType RetTyVT = getValueType(RetTy);
984
985 RetVals.push_back(MVT::Other);
986
987 // The result values produced have to be legal. Promote the result.
988 switch (RetTyVT) {
989 case MVT::isVoid: break;
990 default:
991 RetVals.push_back(RetTyVT);
992 break;
993 case MVT::i1:
994 case MVT::i8:
995 case MVT::i16:
996 RetVals.push_back(MVT::i32);
997 break;
998 case MVT::f32:
999 if (X86ScalarSSE)
1000 RetVals.push_back(MVT::f32);
1001 else
1002 RetVals.push_back(MVT::f64);
1003 break;
1004 case MVT::i64:
1005 RetVals.push_back(MVT::i32);
1006 RetVals.push_back(MVT::i32);
1007 break;
1008 }
1009
Nate Begeman7e5496d2006-02-17 00:03:04 +00001010 // Build a sequence of copy-to-reg nodes chained together with token chain
1011 // and flag operands which copy the outgoing args into registers.
1012 SDOperand InFlag;
1013 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1014 unsigned CCReg;
1015 SDOperand RegToPass = RegValuesToPass[i];
1016 switch (RegToPass.getValueType()) {
1017 default: assert(0 && "Bad thing to pass in regs");
1018 case MVT::i8:
1019 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001020 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001021 case MVT::i16:
1022 CCReg = (i == 0) ? X86::AX : X86::DX;
1023 break;
1024 case MVT::i32:
1025 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1026 break;
1027 }
1028
1029 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1030 InFlag = Chain.getValue(1);
1031 }
1032
1033 std::vector<MVT::ValueType> NodeTys;
1034 NodeTys.push_back(MVT::Other); // Returns a chain
1035 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1036 std::vector<SDOperand> Ops;
1037 Ops.push_back(Chain);
1038 Ops.push_back(Callee);
1039 if (InFlag.Val)
1040 Ops.push_back(InFlag);
1041
1042 // FIXME: Do not generate X86ISD::TAILCALL for now.
1043 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1044 InFlag = Chain.getValue(1);
1045
1046 NodeTys.clear();
1047 NodeTys.push_back(MVT::Other); // Returns a chain
1048 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1049 Ops.clear();
1050 Ops.push_back(Chain);
1051 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1052 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1053 Ops.push_back(InFlag);
1054 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1055 InFlag = Chain.getValue(1);
1056
1057 SDOperand RetVal;
1058 if (RetTyVT != MVT::isVoid) {
1059 switch (RetTyVT) {
1060 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001061 case MVT::i1:
1062 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001063 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1064 Chain = RetVal.getValue(1);
1065 if (RetTyVT == MVT::i1)
1066 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1067 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001068 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001069 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1070 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001071 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001072 case MVT::i32:
1073 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1074 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001075 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001076 case MVT::i64: {
1077 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1078 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1079 Lo.getValue(2));
1080 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1081 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001082 break;
1083 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001084 case MVT::f32:
1085 case MVT::f64: {
1086 std::vector<MVT::ValueType> Tys;
1087 Tys.push_back(MVT::f64);
1088 Tys.push_back(MVT::Other);
1089 Tys.push_back(MVT::Flag);
1090 std::vector<SDOperand> Ops;
1091 Ops.push_back(Chain);
1092 Ops.push_back(InFlag);
1093 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1094 Chain = RetVal.getValue(1);
1095 InFlag = RetVal.getValue(2);
1096 if (X86ScalarSSE) {
1097 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1098 // shouldn't be necessary except that RFP cannot be live across
1099 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1100 MachineFunction &MF = DAG.getMachineFunction();
1101 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1102 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1103 Tys.clear();
1104 Tys.push_back(MVT::Other);
1105 Ops.clear();
1106 Ops.push_back(Chain);
1107 Ops.push_back(RetVal);
1108 Ops.push_back(StackSlot);
1109 Ops.push_back(DAG.getValueType(RetTyVT));
1110 Ops.push_back(InFlag);
1111 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1112 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1113 DAG.getSrcValue(NULL));
1114 Chain = RetVal.getValue(1);
1115 }
Evan Cheng172fce72006-01-06 00:43:03 +00001116
Nate Begeman7e5496d2006-02-17 00:03:04 +00001117 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1118 // FIXME: we would really like to remember that this FP_ROUND
1119 // operation is okay to eliminate if we allow excess FP precision.
1120 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1121 break;
1122 }
1123 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001124 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001125
1126 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001127}
1128
1129SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1130 if (ReturnAddrIndex == 0) {
1131 // Set up a frame object for the return address.
1132 MachineFunction &MF = DAG.getMachineFunction();
1133 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1134 }
1135
1136 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1137}
1138
1139
1140
1141std::pair<SDOperand, SDOperand> X86TargetLowering::
1142LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1143 SelectionDAG &DAG) {
1144 SDOperand Result;
1145 if (Depth) // Depths > 0 not supported yet!
1146 Result = DAG.getConstant(0, getPointerTy());
1147 else {
1148 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1149 if (!isFrameAddress)
1150 // Just load the return address
1151 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1152 DAG.getSrcValue(NULL));
1153 else
1154 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1155 DAG.getConstant(4, MVT::i32));
1156 }
1157 return std::make_pair(Result, Chain);
1158}
1159
Evan Cheng339edad2006-01-11 00:33:36 +00001160/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1161/// which corresponds to the condition code.
1162static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1163 switch (X86CC) {
1164 default: assert(0 && "Unknown X86 conditional code!");
1165 case X86ISD::COND_A: return X86::JA;
1166 case X86ISD::COND_AE: return X86::JAE;
1167 case X86ISD::COND_B: return X86::JB;
1168 case X86ISD::COND_BE: return X86::JBE;
1169 case X86ISD::COND_E: return X86::JE;
1170 case X86ISD::COND_G: return X86::JG;
1171 case X86ISD::COND_GE: return X86::JGE;
1172 case X86ISD::COND_L: return X86::JL;
1173 case X86ISD::COND_LE: return X86::JLE;
1174 case X86ISD::COND_NE: return X86::JNE;
1175 case X86ISD::COND_NO: return X86::JNO;
1176 case X86ISD::COND_NP: return X86::JNP;
1177 case X86ISD::COND_NS: return X86::JNS;
1178 case X86ISD::COND_O: return X86::JO;
1179 case X86ISD::COND_P: return X86::JP;
1180 case X86ISD::COND_S: return X86::JS;
1181 }
1182}
Chris Lattner76ac0682005-11-15 00:40:23 +00001183
Evan Cheng45df7f82006-01-30 23:41:35 +00001184/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1185/// specific condition code. It returns a false if it cannot do a direct
1186/// translation. X86CC is the translated CondCode. Flip is set to true if the
1187/// the order of comparison operands should be flipped.
Chris Lattnerc642aa52006-01-31 19:43:35 +00001188static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1189 bool &Flip) {
Evan Cheng172fce72006-01-06 00:43:03 +00001190 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng45df7f82006-01-30 23:41:35 +00001191 Flip = false;
1192 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001193 if (!isFP) {
1194 switch (SetCCOpcode) {
1195 default: break;
1196 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1197 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1198 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1199 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1200 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1201 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1202 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1203 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1204 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1205 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1206 }
1207 } else {
1208 // On a floating point condition, the flags are set as follows:
1209 // ZF PF CF op
1210 // 0 | 0 | 0 | X > Y
1211 // 0 | 0 | 1 | X < Y
1212 // 1 | 0 | 0 | X == Y
1213 // 1 | 1 | 1 | unordered
1214 switch (SetCCOpcode) {
1215 default: break;
1216 case ISD::SETUEQ:
1217 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001218 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001219 case ISD::SETOGT:
1220 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001221 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001222 case ISD::SETOGE:
1223 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001224 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001225 case ISD::SETULT:
1226 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001227 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001228 case ISD::SETULE:
1229 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1230 case ISD::SETONE:
1231 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1232 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1233 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1234 }
1235 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001236
1237 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001238}
1239
Evan Cheng339edad2006-01-11 00:33:36 +00001240/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1241/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001242/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001243static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001244 switch (X86CC) {
1245 default:
1246 return false;
1247 case X86ISD::COND_B:
1248 case X86ISD::COND_BE:
1249 case X86ISD::COND_E:
1250 case X86ISD::COND_P:
1251 case X86ISD::COND_A:
1252 case X86ISD::COND_AE:
1253 case X86ISD::COND_NE:
1254 case X86ISD::COND_NP:
1255 return true;
1256 }
1257}
1258
Evan Cheng339edad2006-01-11 00:33:36 +00001259MachineBasicBlock *
1260X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1261 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001262 switch (MI->getOpcode()) {
1263 default: assert(false && "Unexpected instr type to insert");
1264 case X86::CMOV_FR32:
1265 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001266 // To "insert" a SELECT_CC instruction, we actually have to insert the
1267 // diamond control-flow pattern. The incoming instruction knows the
1268 // destination vreg to set, the condition code register to branch on, the
1269 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001270 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1271 ilist<MachineBasicBlock>::iterator It = BB;
1272 ++It;
1273
1274 // thisMBB:
1275 // ...
1276 // TrueVal = ...
1277 // cmpTY ccX, r1, r2
1278 // bCC copy1MBB
1279 // fallthrough --> copy0MBB
1280 MachineBasicBlock *thisMBB = BB;
1281 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1282 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1283 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1284 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1285 MachineFunction *F = BB->getParent();
1286 F->getBasicBlockList().insert(It, copy0MBB);
1287 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001288 // Update machine-CFG edges by first adding all successors of the current
1289 // block to the new block which will contain the Phi node for the select.
1290 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1291 e = BB->succ_end(); i != e; ++i)
1292 sinkMBB->addSuccessor(*i);
1293 // Next, remove all successors of the current block, and add the true
1294 // and fallthrough blocks as its successors.
1295 while(!BB->succ_empty())
1296 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001297 BB->addSuccessor(copy0MBB);
1298 BB->addSuccessor(sinkMBB);
1299
1300 // copy0MBB:
1301 // %FalseValue = ...
1302 // # fallthrough to sinkMBB
1303 BB = copy0MBB;
1304
1305 // Update machine-CFG edges
1306 BB->addSuccessor(sinkMBB);
1307
1308 // sinkMBB:
1309 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1310 // ...
1311 BB = sinkMBB;
1312 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1313 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1314 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001315
Evan Cheng911c68d2006-01-16 21:21:29 +00001316 delete MI; // The pseudo instruction is gone now.
1317 return BB;
1318 }
Evan Cheng339edad2006-01-11 00:33:36 +00001319
Evan Cheng911c68d2006-01-16 21:21:29 +00001320 case X86::FP_TO_INT16_IN_MEM:
1321 case X86::FP_TO_INT32_IN_MEM:
1322 case X86::FP_TO_INT64_IN_MEM: {
1323 // Change the floating point control register to use "round towards zero"
1324 // mode when truncating to an integer value.
1325 MachineFunction *F = BB->getParent();
1326 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1327 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1328
1329 // Load the old value of the high byte of the control word...
1330 unsigned OldCW =
1331 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1332 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1333
1334 // Set the high part to be round to zero...
1335 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1336
1337 // Reload the modified control word now...
1338 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1339
1340 // Restore the memory image of control word to original value
1341 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1342
1343 // Get the X86 opcode to use.
1344 unsigned Opc;
1345 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001346 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001347 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1348 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1349 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1350 }
1351
1352 X86AddressMode AM;
1353 MachineOperand &Op = MI->getOperand(0);
1354 if (Op.isRegister()) {
1355 AM.BaseType = X86AddressMode::RegBase;
1356 AM.Base.Reg = Op.getReg();
1357 } else {
1358 AM.BaseType = X86AddressMode::FrameIndexBase;
1359 AM.Base.FrameIndex = Op.getFrameIndex();
1360 }
1361 Op = MI->getOperand(1);
1362 if (Op.isImmediate())
1363 AM.Scale = Op.getImmedValue();
1364 Op = MI->getOperand(2);
1365 if (Op.isImmediate())
1366 AM.IndexReg = Op.getImmedValue();
1367 Op = MI->getOperand(3);
1368 if (Op.isGlobalAddress()) {
1369 AM.GV = Op.getGlobal();
1370 } else {
1371 AM.Disp = Op.getImmedValue();
1372 }
1373 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1374
1375 // Reload the original control word now.
1376 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1377
1378 delete MI; // The pseudo instruction is gone now.
1379 return BB;
1380 }
1381 }
Evan Cheng339edad2006-01-11 00:33:36 +00001382}
1383
1384
1385//===----------------------------------------------------------------------===//
1386// X86 Custom Lowering Hooks
1387//===----------------------------------------------------------------------===//
1388
Evan Chengaf598d22006-03-13 23:18:16 +00001389/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1390/// load. For Darwin, external and weak symbols are indirect, loading the value
1391/// at address GV rather then the value of GV itself. This means that the
1392/// GlobalAddress must be in the base or index register of the address, not the
1393/// GV offset field.
1394static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1395 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1396 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1397}
1398
Evan Cheng68ad48b2006-03-22 18:59:22 +00001399/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1400/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1401bool X86::isPSHUFDMask(SDNode *N) {
1402 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1403
1404 if (N->getNumOperands() != 4)
1405 return false;
1406
1407 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001408 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001409 SDOperand Arg = N->getOperand(i);
1410 if (Arg.getOpcode() == ISD::UNDEF) continue;
1411 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1412 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001413 return false;
1414 }
1415
1416 return true;
1417}
1418
1419/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001420/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001421bool X86::isPSHUFHWMask(SDNode *N) {
1422 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1423
1424 if (N->getNumOperands() != 8)
1425 return false;
1426
1427 // Lower quadword copied in order.
1428 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001429 SDOperand Arg = N->getOperand(i);
1430 if (Arg.getOpcode() == ISD::UNDEF) continue;
1431 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1432 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001433 return false;
1434 }
1435
1436 // Upper quadword shuffled.
1437 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001438 SDOperand Arg = N->getOperand(i);
1439 if (Arg.getOpcode() == ISD::UNDEF) continue;
1440 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1441 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001442 if (Val < 4 || Val > 7)
1443 return false;
1444 }
1445
1446 return true;
1447}
1448
1449/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001450/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001451bool X86::isPSHUFLWMask(SDNode *N) {
1452 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1453
1454 if (N->getNumOperands() != 8)
1455 return false;
1456
1457 // Upper quadword copied in order.
1458 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001459 SDOperand Arg = N->getOperand(i);
1460 if (Arg.getOpcode() == ISD::UNDEF) continue;
1461 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1462 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001463 return false;
1464 }
1465
1466 // Lower quadword shuffled.
1467 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001468 SDOperand Arg = N->getOperand(i);
1469 if (Arg.getOpcode() == ISD::UNDEF) continue;
1470 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1471 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001472 if (Val > 4)
1473 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001474 }
1475
1476 return true;
1477}
1478
Evan Chengd27fb3e2006-03-24 01:18:28 +00001479/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1480/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1481bool X86::isSHUFPMask(SDNode *N) {
1482 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1483
Evan Chenge7ee6a52006-03-24 23:15:12 +00001484 unsigned NumElems = N->getNumOperands();
1485 if (NumElems == 2) {
Evan Cheng2595a682006-03-24 02:58:06 +00001486 // The only case that ought be handled by SHUFPD is
1487 // Dest { 2, 1 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
1488 // Expect bit 0 == 1, bit1 == 2
1489 SDOperand Bit0 = N->getOperand(0);
Evan Chengebf10062006-04-03 20:53:28 +00001490 if (Bit0.getOpcode() != ISD::UNDEF) {
1491 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1492 if (cast<ConstantSDNode>(Bit0)->getValue() != 1)
1493 return false;
1494 }
1495
Evan Cheng2595a682006-03-24 02:58:06 +00001496 SDOperand Bit1 = N->getOperand(1);
Evan Chengebf10062006-04-03 20:53:28 +00001497 if (Bit1.getOpcode() != ISD::UNDEF) {
1498 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1499 if (cast<ConstantSDNode>(Bit1)->getValue() != 2)
1500 return false;
1501 }
1502
1503 return true;
Evan Cheng2595a682006-03-24 02:58:06 +00001504 }
1505
Evan Chenge7ee6a52006-03-24 23:15:12 +00001506 if (NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001507
1508 // Each half must refer to only one of the vector.
Evan Cheng7e2ff112006-03-30 19:54:57 +00001509 for (unsigned i = 0; i < 2; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001510 SDOperand Arg = N->getOperand(i);
1511 if (Arg.getOpcode() == ISD::UNDEF) continue;
1512 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1513 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7e2ff112006-03-30 19:54:57 +00001514 if (Val >= 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001515 }
Evan Cheng7e2ff112006-03-30 19:54:57 +00001516 for (unsigned i = 2; i < 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001517 SDOperand Arg = N->getOperand(i);
1518 if (Arg.getOpcode() == ISD::UNDEF) continue;
1519 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1520 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7e2ff112006-03-30 19:54:57 +00001521 if (Val < 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001522 }
1523
1524 return true;
1525}
1526
Evan Cheng2595a682006-03-24 02:58:06 +00001527/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1528/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1529bool X86::isMOVHLPSMask(SDNode *N) {
1530 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1531
Evan Cheng1a194a52006-03-28 06:50:32 +00001532 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001533 return false;
1534
Evan Cheng1a194a52006-03-28 06:50:32 +00001535 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Cheng2595a682006-03-24 02:58:06 +00001536 SDOperand Bit0 = N->getOperand(0);
1537 SDOperand Bit1 = N->getOperand(1);
Evan Cheng1a194a52006-03-28 06:50:32 +00001538 SDOperand Bit2 = N->getOperand(2);
1539 SDOperand Bit3 = N->getOperand(3);
Evan Cheng99d72052006-03-31 00:30:29 +00001540
1541 if (Bit0.getOpcode() != ISD::UNDEF) {
1542 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1543 if (cast<ConstantSDNode>(Bit0)->getValue() != 6)
1544 return false;
1545 }
1546
1547 if (Bit1.getOpcode() != ISD::UNDEF) {
1548 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1549 if (cast<ConstantSDNode>(Bit1)->getValue() != 7)
1550 return false;
1551 }
1552
1553 if (Bit2.getOpcode() != ISD::UNDEF) {
1554 assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1555 if (cast<ConstantSDNode>(Bit2)->getValue() != 2)
1556 return false;
1557 }
1558
1559 if (Bit3.getOpcode() != ISD::UNDEF) {
1560 assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1561 if (cast<ConstantSDNode>(Bit3)->getValue() != 3)
1562 return false;
1563 }
1564
1565 return true;
Evan Cheng1a194a52006-03-28 06:50:32 +00001566}
1567
1568/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
1569/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1570bool X86::isMOVLHPSMask(SDNode *N) {
1571 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1572
1573 if (N->getNumOperands() != 4)
1574 return false;
1575
1576 // Expect bit0 == 0, bit1 == 1, bit2 == 4, bit3 == 5
1577 SDOperand Bit0 = N->getOperand(0);
1578 SDOperand Bit1 = N->getOperand(1);
1579 SDOperand Bit2 = N->getOperand(2);
1580 SDOperand Bit3 = N->getOperand(3);
Evan Cheng99d72052006-03-31 00:30:29 +00001581
1582 if (Bit0.getOpcode() != ISD::UNDEF) {
1583 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1584 if (cast<ConstantSDNode>(Bit0)->getValue() != 0)
1585 return false;
1586 }
1587
1588 if (Bit1.getOpcode() != ISD::UNDEF) {
1589 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1590 if (cast<ConstantSDNode>(Bit1)->getValue() != 1)
1591 return false;
1592 }
1593
1594 if (Bit2.getOpcode() != ISD::UNDEF) {
1595 assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1596 if (cast<ConstantSDNode>(Bit2)->getValue() != 4)
1597 return false;
1598 }
1599
1600 if (Bit3.getOpcode() != ISD::UNDEF) {
1601 assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1602 if (cast<ConstantSDNode>(Bit3)->getValue() != 5)
1603 return false;
1604 }
1605
1606 return true;
Evan Cheng2595a682006-03-24 02:58:06 +00001607}
1608
Evan Cheng5df75882006-03-28 00:39:58 +00001609/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1610/// specifies a shuffle of elements that is suitable for input to UNPCKL.
1611bool X86::isUNPCKLMask(SDNode *N) {
1612 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1613
1614 unsigned NumElems = N->getNumOperands();
1615 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1616 return false;
1617
1618 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1619 SDOperand BitI = N->getOperand(i);
1620 SDOperand BitI1 = N->getOperand(i+1);
Evan Cheng99d72052006-03-31 00:30:29 +00001621
1622 if (BitI.getOpcode() != ISD::UNDEF) {
1623 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1624 if (cast<ConstantSDNode>(BitI)->getValue() != j)
1625 return false;
1626 }
1627
1628 if (BitI1.getOpcode() != ISD::UNDEF) {
1629 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengd9d0bbb2006-03-31 00:33:57 +00001630 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems)
Evan Cheng99d72052006-03-31 00:30:29 +00001631 return false;
1632 }
Evan Cheng5df75882006-03-28 00:39:58 +00001633 }
1634
1635 return true;
1636}
1637
Evan Cheng2bc32802006-03-28 02:43:26 +00001638/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1639/// specifies a shuffle of elements that is suitable for input to UNPCKH.
1640bool X86::isUNPCKHMask(SDNode *N) {
1641 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1642
1643 unsigned NumElems = N->getNumOperands();
1644 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1645 return false;
1646
1647 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1648 SDOperand BitI = N->getOperand(i);
1649 SDOperand BitI1 = N->getOperand(i+1);
Evan Cheng99d72052006-03-31 00:30:29 +00001650
1651 if (BitI.getOpcode() != ISD::UNDEF) {
1652 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1653 if (cast<ConstantSDNode>(BitI)->getValue() != j + NumElems/2)
1654 return false;
1655 }
1656
1657 if (BitI1.getOpcode() != ISD::UNDEF) {
1658 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengd9d0bbb2006-03-31 00:33:57 +00001659 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems/2 + NumElems)
Evan Cheng99d72052006-03-31 00:30:29 +00001660 return false;
1661 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001662 }
1663
1664 return true;
1665}
1666
Evan Chengf3b52c82006-04-05 07:20:06 +00001667/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1668/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1669/// <0, 0, 1, 1>
1670bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1671 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1672
1673 unsigned NumElems = N->getNumOperands();
1674 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1675 return false;
1676
1677 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1678 SDOperand BitI = N->getOperand(i);
1679 SDOperand BitI1 = N->getOperand(i+1);
1680
1681 if (BitI.getOpcode() != ISD::UNDEF) {
1682 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1683 if (cast<ConstantSDNode>(BitI)->getValue() != j)
1684 return false;
1685 }
1686
1687 if (BitI1.getOpcode() != ISD::UNDEF) {
1688 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
1689 if (cast<ConstantSDNode>(BitI1)->getValue() != j)
1690 return false;
1691 }
1692 }
1693
1694 return true;
1695}
1696
1697
Evan Chengd097e672006-03-22 02:53:00 +00001698/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1699/// a splat of a single element.
1700bool X86::isSplatMask(SDNode *N) {
1701 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1702
1703 // We can only splat 64-bit, and 32-bit quantities.
1704 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1705 return false;
1706
1707 // This is a splat operation if each element of the permute is the same, and
1708 // if the value doesn't reference the second vector.
1709 SDOperand Elt = N->getOperand(0);
1710 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1711 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001712 SDOperand Arg = N->getOperand(i);
1713 if (Arg.getOpcode() == ISD::UNDEF) continue;
1714 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1715 if (Arg != Elt) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001716 }
1717
1718 // Make sure it is a splat of the first vector operand.
1719 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1720}
1721
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001722/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1723/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1724/// instructions.
1725unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001726 unsigned NumOperands = N->getNumOperands();
1727 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1728 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001729 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001730 unsigned Val = 0;
1731 SDOperand Arg = N->getOperand(NumOperands-i-1);
1732 if (Arg.getOpcode() != ISD::UNDEF)
1733 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001734 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001735 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001736 if (i != NumOperands - 1)
1737 Mask <<= Shift;
1738 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001739
1740 return Mask;
1741}
1742
Evan Chengb7fedff2006-03-29 23:07:14 +00001743/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1744/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1745/// instructions.
1746unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1747 unsigned Mask = 0;
1748 // 8 nodes, but we only care about the last 4.
1749 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001750 unsigned Val = 0;
1751 SDOperand Arg = N->getOperand(i);
1752 if (Arg.getOpcode() != ISD::UNDEF)
1753 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001754 Mask |= (Val - 4);
1755 if (i != 4)
1756 Mask <<= 2;
1757 }
1758
1759 return Mask;
1760}
1761
1762/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1763/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1764/// instructions.
1765unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1766 unsigned Mask = 0;
1767 // 8 nodes, but we only care about the first 4.
1768 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001769 unsigned Val = 0;
1770 SDOperand Arg = N->getOperand(i);
1771 if (Arg.getOpcode() != ISD::UNDEF)
1772 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001773 Mask |= Val;
1774 if (i != 0)
1775 Mask <<= 2;
1776 }
1777
1778 return Mask;
1779}
1780
Evan Chengda59b0d2006-03-29 01:30:51 +00001781/// NormalizeVectorShuffle - Swap vector_shuffle operands (as well as
1782/// values in ther permute mask if needed. Use V1 as second vector if it is
1783/// undef. Return an empty SDOperand is it is already well formed.
Evan Cheng2cf42322006-04-05 06:09:26 +00001784static SDOperand NormalizeVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1785 SDOperand V1 = Op.getOperand(0);
1786 SDOperand V2 = Op.getOperand(1);
1787 SDOperand Mask = Op.getOperand(2);
1788 MVT::ValueType VT = Op.getValueType();
Evan Cheng1a194a52006-03-28 06:50:32 +00001789 unsigned NumElems = Mask.getNumOperands();
1790 SDOperand Half1 = Mask.getOperand(0);
1791 SDOperand Half2 = Mask.getOperand(NumElems/2);
Evan Chengda59b0d2006-03-29 01:30:51 +00001792 bool V2Undef = false;
1793 if (V2.getOpcode() == ISD::UNDEF) {
1794 V2Undef = true;
1795 V2 = V1;
1796 }
1797
Evan Cheng1a194a52006-03-28 06:50:32 +00001798 if (cast<ConstantSDNode>(Half1)->getValue() >= NumElems &&
1799 cast<ConstantSDNode>(Half2)->getValue() < NumElems) {
1800 // Swap the operands and change mask.
1801 std::vector<SDOperand> MaskVec;
1802 for (unsigned i = NumElems / 2; i != NumElems; ++i)
1803 MaskVec.push_back(Mask.getOperand(i));
1804 for (unsigned i = 0; i != NumElems / 2; ++i)
1805 MaskVec.push_back(Mask.getOperand(i));
1806 Mask =
1807 DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
1808 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1809 }
Evan Chengda59b0d2006-03-29 01:30:51 +00001810
1811 if (V2Undef)
1812 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
1813
Evan Cheng2cf42322006-04-05 06:09:26 +00001814 return Op;
Evan Cheng1a194a52006-03-28 06:50:32 +00001815}
1816
Evan Cheng59a63552006-04-05 01:47:37 +00001817/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1818/// specifies a 8 element shuffle that can be broken into a pair of
1819/// PSHUFHW and PSHUFLW.
1820static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1821 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1822
1823 if (N->getNumOperands() != 8)
1824 return false;
1825
1826 // Lower quadword shuffled.
1827 for (unsigned i = 0; i != 4; ++i) {
1828 SDOperand Arg = N->getOperand(i);
1829 if (Arg.getOpcode() == ISD::UNDEF) continue;
1830 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1831 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1832 if (Val > 4)
1833 return false;
1834 }
1835
1836 // Upper quadword shuffled.
1837 for (unsigned i = 4; i != 8; ++i) {
1838 SDOperand Arg = N->getOperand(i);
1839 if (Arg.getOpcode() == ISD::UNDEF) continue;
1840 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1841 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1842 if (Val < 4 || Val > 7)
1843 return false;
1844 }
1845
1846 return true;
1847}
1848
Chris Lattner76ac0682005-11-15 00:40:23 +00001849/// LowerOperation - Provide custom lowering hooks for some operations.
1850///
1851SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1852 switch (Op.getOpcode()) {
1853 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001854 case ISD::SHL_PARTS:
1855 case ISD::SRA_PARTS:
1856 case ISD::SRL_PARTS: {
1857 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1858 "Not an i64 shift!");
1859 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1860 SDOperand ShOpLo = Op.getOperand(0);
1861 SDOperand ShOpHi = Op.getOperand(1);
1862 SDOperand ShAmt = Op.getOperand(2);
1863 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001864 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001865 : DAG.getConstant(0, MVT::i32);
1866
1867 SDOperand Tmp2, Tmp3;
1868 if (Op.getOpcode() == ISD::SHL_PARTS) {
1869 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1870 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1871 } else {
1872 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001873 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001874 }
1875
1876 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1877 ShAmt, DAG.getConstant(32, MVT::i8));
1878
1879 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001880 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001881
1882 std::vector<MVT::ValueType> Tys;
1883 Tys.push_back(MVT::i32);
1884 Tys.push_back(MVT::Flag);
1885 std::vector<SDOperand> Ops;
1886 if (Op.getOpcode() == ISD::SHL_PARTS) {
1887 Ops.push_back(Tmp2);
1888 Ops.push_back(Tmp3);
1889 Ops.push_back(CC);
1890 Ops.push_back(InFlag);
1891 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1892 InFlag = Hi.getValue(1);
1893
1894 Ops.clear();
1895 Ops.push_back(Tmp3);
1896 Ops.push_back(Tmp1);
1897 Ops.push_back(CC);
1898 Ops.push_back(InFlag);
1899 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1900 } else {
1901 Ops.push_back(Tmp2);
1902 Ops.push_back(Tmp3);
1903 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001904 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001905 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1906 InFlag = Lo.getValue(1);
1907
1908 Ops.clear();
1909 Ops.push_back(Tmp3);
1910 Ops.push_back(Tmp1);
1911 Ops.push_back(CC);
1912 Ops.push_back(InFlag);
1913 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1914 }
1915
1916 Tys.clear();
1917 Tys.push_back(MVT::i32);
1918 Tys.push_back(MVT::i32);
1919 Ops.clear();
1920 Ops.push_back(Lo);
1921 Ops.push_back(Hi);
1922 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1923 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001924 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001925 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001926 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001927 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001928
1929 SDOperand Result;
1930 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1931 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001932 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001933 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001934 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001935 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1936 DAG.getEntryNode(), Op.getOperand(0),
1937 StackSlot, DAG.getSrcValue(NULL));
1938
1939 // Build the FILD
1940 std::vector<MVT::ValueType> Tys;
1941 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001942 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001943 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001944 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001945 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001946 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001947 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001948 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1949 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001950
1951 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001952 Chain = Result.getValue(1);
1953 SDOperand InFlag = Result.getValue(2);
1954
Evan Cheng11613a52006-02-04 02:20:30 +00001955 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001956 // shouldn't be necessary except that RFP cannot be live across
1957 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1958 MachineFunction &MF = DAG.getMachineFunction();
1959 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1960 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1961 std::vector<MVT::ValueType> Tys;
1962 Tys.push_back(MVT::Other);
1963 std::vector<SDOperand> Ops;
1964 Ops.push_back(Chain);
1965 Ops.push_back(Result);
1966 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001967 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001968 Ops.push_back(InFlag);
1969 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1970 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1971 DAG.getSrcValue(NULL));
1972 }
1973
Evan Cheng6305e502006-01-12 22:54:21 +00001974 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001975 }
1976 case ISD::FP_TO_SINT: {
1977 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001978 "Unknown FP_TO_SINT to lower!");
1979 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1980 // stack slot.
1981 MachineFunction &MF = DAG.getMachineFunction();
1982 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1983 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1984 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1985
1986 unsigned Opc;
1987 switch (Op.getValueType()) {
1988 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1989 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1990 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1991 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1992 }
1993
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001994 SDOperand Chain = DAG.getEntryNode();
1995 SDOperand Value = Op.getOperand(0);
1996 if (X86ScalarSSE) {
1997 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1998 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1999 DAG.getSrcValue(0));
2000 std::vector<MVT::ValueType> Tys;
2001 Tys.push_back(MVT::f64);
2002 Tys.push_back(MVT::Other);
2003 std::vector<SDOperand> Ops;
2004 Ops.push_back(Chain);
2005 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00002006 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002007 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2008 Chain = Value.getValue(1);
2009 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2010 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2011 }
2012
Chris Lattner76ac0682005-11-15 00:40:23 +00002013 // Build the FP_TO_INT*_IN_MEM
2014 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002015 Ops.push_back(Chain);
2016 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00002017 Ops.push_back(StackSlot);
2018 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
2019
2020 // Load the result.
2021 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
2022 DAG.getSrcValue(NULL));
2023 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00002024 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00002025 std::vector<MVT::ValueType> Tys;
2026 Tys.push_back(MVT::Other);
2027 Tys.push_back(MVT::Flag);
2028 std::vector<SDOperand> Ops;
2029 Ops.push_back(Op.getOperand(0));
2030 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00002031 Ops.clear();
2032 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
2033 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
2034 MVT::i32, Ops[0].getValue(2)));
2035 Ops.push_back(Ops[1].getValue(1));
2036 Tys[0] = Tys[1] = MVT::i32;
2037 Tys.push_back(MVT::Other);
2038 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00002039 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00002040 case ISD::FABS: {
2041 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00002042 const Type *OpNTy = MVT::getTypeForValueType(VT);
2043 std::vector<Constant*> CV;
2044 if (VT == MVT::f64) {
2045 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2046 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2047 } else {
2048 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2049 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2050 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2051 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2052 }
2053 Constant *CS = ConstantStruct::get(CV);
2054 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2055 SDOperand Mask
2056 = DAG.getNode(X86ISD::LOAD_PACK,
2057 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00002058 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
2059 }
Evan Cheng72d5c252006-01-31 22:28:30 +00002060 case ISD::FNEG: {
2061 MVT::ValueType VT = Op.getValueType();
2062 const Type *OpNTy = MVT::getTypeForValueType(VT);
2063 std::vector<Constant*> CV;
2064 if (VT == MVT::f64) {
2065 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2066 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2067 } else {
2068 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
2069 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2070 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2071 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2072 }
2073 Constant *CS = ConstantStruct::get(CV);
2074 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2075 SDOperand Mask
2076 = DAG.getNode(X86ISD::LOAD_PACK,
2077 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2078 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
2079 }
Evan Chengc1583db2005-12-21 20:21:51 +00002080 case ISD::SETCC: {
2081 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00002082 SDOperand Cond;
2083 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00002084 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2085 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00002086 bool Flip;
2087 unsigned X86CC;
2088 if (translateX86CC(CC, isFP, X86CC, Flip)) {
2089 if (Flip)
2090 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2091 Op.getOperand(1), Op.getOperand(0));
2092 else
2093 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2094 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00002095 return DAG.getNode(X86ISD::SETCC, MVT::i8,
2096 DAG.getConstant(X86CC, MVT::i8), Cond);
2097 } else {
2098 assert(isFP && "Illegal integer SetCC!");
2099
Evan Cheng45df7f82006-01-30 23:41:35 +00002100 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2101 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00002102 std::vector<MVT::ValueType> Tys;
2103 std::vector<SDOperand> Ops;
2104 switch (SetCCOpcode) {
2105 default: assert(false && "Illegal floating point SetCC!");
2106 case ISD::SETOEQ: { // !PF & ZF
2107 Tys.push_back(MVT::i8);
2108 Tys.push_back(MVT::Flag);
2109 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
2110 Ops.push_back(Cond);
2111 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2112 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2113 DAG.getConstant(X86ISD::COND_E, MVT::i8),
2114 Tmp1.getValue(1));
2115 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
2116 }
Evan Cheng172fce72006-01-06 00:43:03 +00002117 case ISD::SETUNE: { // PF | !ZF
2118 Tys.push_back(MVT::i8);
2119 Tys.push_back(MVT::Flag);
2120 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
2121 Ops.push_back(Cond);
2122 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2123 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2124 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
2125 Tmp1.getValue(1));
2126 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
2127 }
2128 }
2129 }
Evan Chengc1583db2005-12-21 20:21:51 +00002130 }
Evan Cheng225a4d02005-12-17 01:21:05 +00002131 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00002132 MVT::ValueType VT = Op.getValueType();
2133 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00002134 bool isFPStack = isFP && !X86ScalarSSE;
2135 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00002136 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00002137 SDOperand Op0 = Op.getOperand(0);
2138 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00002139 if (Op0.getOpcode() == ISD::SETCC)
2140 Op0 = LowerOperation(Op0, DAG);
2141
Evan Cheng73a1ad92006-01-10 20:26:56 +00002142 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00002143 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2144 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2145 // have another use it will be eliminated.
2146 // If the X86ISD::SETCC has more than one use, then it's probably better
2147 // to use a test instead of duplicating the X86ISD::CMP (for register
2148 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00002149 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
2150 if (!Op0.hasOneUse()) {
2151 std::vector<MVT::ValueType> Tys;
2152 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2153 Tys.push_back(Op0.Val->getValueType(i));
2154 std::vector<SDOperand> Ops;
2155 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2156 Ops.push_back(Op0.getOperand(i));
2157 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2158 }
2159
Evan Chengfb22e862006-01-13 01:03:02 +00002160 CC = Op0.getOperand(0);
2161 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00002162 // Make a copy as flag result cannot be used by more than one.
2163 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2164 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00002165 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00002166 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00002167 } else
2168 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00002169 } else
2170 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00002171
Evan Cheng731423f2006-01-13 01:06:49 +00002172 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00002173 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00002174 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00002175 }
Evan Cheng9c249c32006-01-09 18:33:28 +00002176
2177 std::vector<MVT::ValueType> Tys;
2178 Tys.push_back(Op.getValueType());
2179 Tys.push_back(MVT::Flag);
2180 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00002181 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2182 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00002183 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00002184 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00002185 Ops.push_back(CC);
2186 Ops.push_back(Cond);
2187 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00002188 }
Evan Cheng6fc31042005-12-19 23:12:38 +00002189 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00002190 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00002191 SDOperand Cond = Op.getOperand(1);
2192 SDOperand Dest = Op.getOperand(2);
2193 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00002194 if (Cond.getOpcode() == ISD::SETCC)
2195 Cond = LowerOperation(Cond, DAG);
2196
Evan Chengc1583db2005-12-21 20:21:51 +00002197 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00002198 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2199 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2200 // have another use it will be eliminated.
2201 // If the X86ISD::SETCC has more than one use, then it's probably better
2202 // to use a test instead of duplicating the X86ISD::CMP (for register
2203 // pressure reason).
Evan Cheng944d1e92006-01-26 02:13:10 +00002204 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
2205 if (!Cond.hasOneUse()) {
2206 std::vector<MVT::ValueType> Tys;
2207 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2208 Tys.push_back(Cond.Val->getValueType(i));
2209 std::vector<SDOperand> Ops;
2210 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2211 Ops.push_back(Cond.getOperand(i));
2212 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2213 }
2214
Evan Chengfb22e862006-01-13 01:03:02 +00002215 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00002216 Cond = Cond.getOperand(1);
2217 // Make a copy as flag result cannot be used by more than one.
Evan Chengfb22e862006-01-13 01:03:02 +00002218 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00002219 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00002220 } else
2221 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00002222 } else
2223 addTest = true;
2224
2225 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00002226 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00002227 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2228 }
2229 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2230 Op.getOperand(0), Op.getOperand(2), CC, Cond);
2231 }
Evan Chengae986f12006-01-11 22:15:48 +00002232 case ISD::MEMSET: {
Evan Cheng6dc73292006-03-04 02:48:56 +00002233 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00002234 SDOperand Chain = Op.getOperand(0);
2235 unsigned Align =
2236 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2237 if (Align == 0) Align = 1;
2238
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002239 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2240 // If not DWORD aligned, call memset if size is less than the threshold.
2241 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00002242 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00002243 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002244 MVT::ValueType IntPtr = getPointerTy();
2245 const Type *IntPtrTy = getTargetData().getIntPtrType();
2246 std::vector<std::pair<SDOperand, const Type*> > Args;
2247 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2248 // Extend the ubyte argument to be an int value for the call.
2249 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2250 Args.push_back(std::make_pair(Val, IntPtrTy));
2251 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2252 std::pair<SDOperand,SDOperand> CallResult =
2253 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2254 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2255 return CallResult.second;
2256 }
2257
Evan Chengae986f12006-01-11 22:15:48 +00002258 MVT::ValueType AVT;
2259 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00002260 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2261 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00002262 bool TwoRepStos = false;
Evan Cheng6dc73292006-03-04 02:48:56 +00002263 if (ValC) {
Evan Chengae986f12006-01-11 22:15:48 +00002264 unsigned ValReg;
2265 unsigned Val = ValC->getValue() & 255;
2266
2267 // If the value is a constant, then we can potentially use larger sets.
2268 switch (Align & 3) {
2269 case 2: // WORD aligned
2270 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00002271 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2272 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00002273 Val = (Val << 8) | Val;
2274 ValReg = X86::AX;
2275 break;
2276 case 0: // DWORD aligned
2277 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00002278 if (I) {
2279 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2280 BytesLeft = I->getValue() % 4;
2281 } else {
2282 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2283 DAG.getConstant(2, MVT::i8));
2284 TwoRepStos = true;
2285 }
Evan Chengae986f12006-01-11 22:15:48 +00002286 Val = (Val << 8) | Val;
2287 Val = (Val << 16) | Val;
2288 ValReg = X86::EAX;
2289 break;
2290 default: // Byte aligned
2291 AVT = MVT::i8;
2292 Count = Op.getOperand(3);
2293 ValReg = X86::AL;
2294 break;
2295 }
2296
2297 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2298 InFlag);
2299 InFlag = Chain.getValue(1);
2300 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002301 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00002302 Count = Op.getOperand(3);
2303 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2304 InFlag = Chain.getValue(1);
2305 }
2306
2307 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2308 InFlag = Chain.getValue(1);
2309 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2310 InFlag = Chain.getValue(1);
2311
Evan Chengadc70932006-03-07 23:29:39 +00002312 std::vector<MVT::ValueType> Tys;
2313 Tys.push_back(MVT::Other);
2314 Tys.push_back(MVT::Flag);
2315 std::vector<SDOperand> Ops;
2316 Ops.push_back(Chain);
2317 Ops.push_back(DAG.getValueType(AVT));
2318 Ops.push_back(InFlag);
2319 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2320
2321 if (TwoRepStos) {
2322 InFlag = Chain.getValue(1);
2323 Count = Op.getOperand(3);
2324 MVT::ValueType CVT = Count.getValueType();
2325 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2326 DAG.getConstant(3, CVT));
2327 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2328 InFlag = Chain.getValue(1);
2329 Tys.clear();
2330 Tys.push_back(MVT::Other);
2331 Tys.push_back(MVT::Flag);
2332 Ops.clear();
2333 Ops.push_back(Chain);
2334 Ops.push_back(DAG.getValueType(MVT::i8));
2335 Ops.push_back(InFlag);
2336 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2337 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00002338 // Issue stores for the last 1 - 3 bytes.
2339 SDOperand Value;
2340 unsigned Val = ValC->getValue() & 255;
2341 unsigned Offset = I->getValue() - BytesLeft;
2342 SDOperand DstAddr = Op.getOperand(1);
2343 MVT::ValueType AddrVT = DstAddr.getValueType();
2344 if (BytesLeft >= 2) {
2345 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2346 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2347 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2348 DAG.getConstant(Offset, AddrVT)),
2349 DAG.getSrcValue(NULL));
2350 BytesLeft -= 2;
2351 Offset += 2;
2352 }
2353
2354 if (BytesLeft == 1) {
2355 Value = DAG.getConstant(Val, MVT::i8);
2356 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2357 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2358 DAG.getConstant(Offset, AddrVT)),
2359 DAG.getSrcValue(NULL));
2360 }
2361 }
2362
2363 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002364 }
2365 case ISD::MEMCPY: {
2366 SDOperand Chain = Op.getOperand(0);
2367 unsigned Align =
2368 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2369 if (Align == 0) Align = 1;
2370
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002371 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2372 // If not DWORD aligned, call memcpy if size is less than the threshold.
2373 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00002374 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00002375 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002376 MVT::ValueType IntPtr = getPointerTy();
2377 const Type *IntPtrTy = getTargetData().getIntPtrType();
2378 std::vector<std::pair<SDOperand, const Type*> > Args;
2379 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2380 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2381 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2382 std::pair<SDOperand,SDOperand> CallResult =
2383 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2384 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2385 return CallResult.second;
2386 }
2387
Evan Chengae986f12006-01-11 22:15:48 +00002388 MVT::ValueType AVT;
2389 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00002390 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00002391 bool TwoRepMovs = false;
Evan Chengae986f12006-01-11 22:15:48 +00002392 switch (Align & 3) {
2393 case 2: // WORD aligned
2394 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00002395 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2396 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00002397 break;
2398 case 0: // DWORD aligned
2399 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00002400 if (I) {
2401 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2402 BytesLeft = I->getValue() % 4;
2403 } else {
2404 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2405 DAG.getConstant(2, MVT::i8));
2406 TwoRepMovs = true;
2407 }
Evan Chengae986f12006-01-11 22:15:48 +00002408 break;
2409 default: // Byte aligned
2410 AVT = MVT::i8;
2411 Count = Op.getOperand(3);
2412 break;
2413 }
2414
Evan Cheng6dc73292006-03-04 02:48:56 +00002415 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00002416 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2417 InFlag = Chain.getValue(1);
2418 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2419 InFlag = Chain.getValue(1);
2420 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2421 InFlag = Chain.getValue(1);
2422
Evan Chengadc70932006-03-07 23:29:39 +00002423 std::vector<MVT::ValueType> Tys;
2424 Tys.push_back(MVT::Other);
2425 Tys.push_back(MVT::Flag);
2426 std::vector<SDOperand> Ops;
2427 Ops.push_back(Chain);
2428 Ops.push_back(DAG.getValueType(AVT));
2429 Ops.push_back(InFlag);
2430 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2431
2432 if (TwoRepMovs) {
2433 InFlag = Chain.getValue(1);
2434 Count = Op.getOperand(3);
2435 MVT::ValueType CVT = Count.getValueType();
2436 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2437 DAG.getConstant(3, CVT));
2438 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2439 InFlag = Chain.getValue(1);
2440 Tys.clear();
2441 Tys.push_back(MVT::Other);
2442 Tys.push_back(MVT::Flag);
2443 Ops.clear();
2444 Ops.push_back(Chain);
2445 Ops.push_back(DAG.getValueType(MVT::i8));
2446 Ops.push_back(InFlag);
2447 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2448 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00002449 // Issue loads and stores for the last 1 - 3 bytes.
2450 unsigned Offset = I->getValue() - BytesLeft;
2451 SDOperand DstAddr = Op.getOperand(1);
2452 MVT::ValueType DstVT = DstAddr.getValueType();
2453 SDOperand SrcAddr = Op.getOperand(2);
2454 MVT::ValueType SrcVT = SrcAddr.getValueType();
2455 SDOperand Value;
2456 if (BytesLeft >= 2) {
2457 Value = DAG.getLoad(MVT::i16, Chain,
2458 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2459 DAG.getConstant(Offset, SrcVT)),
2460 DAG.getSrcValue(NULL));
2461 Chain = Value.getValue(1);
2462 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2463 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2464 DAG.getConstant(Offset, DstVT)),
2465 DAG.getSrcValue(NULL));
2466 BytesLeft -= 2;
2467 Offset += 2;
2468 }
2469
2470 if (BytesLeft == 1) {
2471 Value = DAG.getLoad(MVT::i8, Chain,
2472 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2473 DAG.getConstant(Offset, SrcVT)),
2474 DAG.getSrcValue(NULL));
2475 Chain = Value.getValue(1);
2476 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2477 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2478 DAG.getConstant(Offset, DstVT)),
2479 DAG.getSrcValue(NULL));
2480 }
2481 }
2482
2483 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002484 }
Evan Cheng99470012006-02-25 09:55:19 +00002485
2486 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2487 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2488 // one of the above mentioned nodes. It has to be wrapped because otherwise
2489 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2490 // be used to form addressing mode. These wrapped nodes will be selected
2491 // into MOV32ri.
Evan Cheng5588de92006-02-18 00:15:05 +00002492 case ISD::ConstantPool: {
2493 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002494 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2495 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2496 CP->getAlignment()));
Evan Chengbc047222006-03-22 19:22:18 +00002497 if (Subtarget->isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002498 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002499 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +00002500 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2501 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2502 }
2503
2504 return Result;
2505 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002506 case ISD::GlobalAddress: {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002507 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2508 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2509 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengbc047222006-03-22 19:22:18 +00002510 if (Subtarget->isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002511 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002512 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng1f342c22006-02-23 02:43:52 +00002513 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2514 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng5588de92006-02-18 00:15:05 +00002515
2516 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Chengaf598d22006-03-13 23:18:16 +00002517 // the value at address GV, not the value of GV itself. This means that
Evan Cheng5588de92006-02-18 00:15:05 +00002518 // the GlobalAddress must be in the base or index register of the address,
2519 // not the GV offset field.
Evan Cheng73136df2006-02-22 20:19:42 +00002520 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Chengaf598d22006-03-13 23:18:16 +00002521 DarwinGVRequiresExtraLoad(GV))
Evan Cheng5a766802006-02-07 08:38:37 +00002522 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng1f342c22006-02-23 02:43:52 +00002523 Result, DAG.getSrcValue(NULL));
Evan Cheng5a766802006-02-07 08:38:37 +00002524 }
Evan Cheng5588de92006-02-18 00:15:05 +00002525
Evan Chengb94db9e2006-01-12 07:56:47 +00002526 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00002527 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002528 case ISD::ExternalSymbol: {
2529 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2530 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2531 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chengbc047222006-03-22 19:22:18 +00002532 if (Subtarget->isTargetDarwin()) {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002533 // With PIC, the address is actually $g + Offset.
2534 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2535 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2536 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2537 }
2538
2539 return Result;
2540 }
Nate Begemane74795c2006-01-25 18:21:52 +00002541 case ISD::VASTART: {
2542 // vastart just stores the address of the VarArgsFrameIndex slot into the
2543 // memory location argument.
2544 // FIXME: Replace MVT::i32 with PointerTy
2545 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2546 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2547 Op.getOperand(1), Op.getOperand(2));
2548 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002549 case ISD::RET: {
2550 SDOperand Copy;
2551
2552 switch(Op.getNumOperands()) {
2553 default:
2554 assert(0 && "Do not know how to return this many arguments!");
2555 abort();
2556 case 1:
2557 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2558 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2559 case 2: {
2560 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2561 if (MVT::isInteger(ArgVT))
2562 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2563 SDOperand());
2564 else if (!X86ScalarSSE) {
2565 std::vector<MVT::ValueType> Tys;
2566 Tys.push_back(MVT::Other);
2567 Tys.push_back(MVT::Flag);
2568 std::vector<SDOperand> Ops;
2569 Ops.push_back(Op.getOperand(0));
2570 Ops.push_back(Op.getOperand(1));
2571 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2572 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00002573 SDOperand MemLoc;
2574 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00002575 SDOperand Value = Op.getOperand(1);
2576
Evan Chenga24617f2006-02-01 01:19:32 +00002577 if (Value.getOpcode() == ISD::LOAD &&
2578 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00002579 Chain = Value.getOperand(0);
2580 MemLoc = Value.getOperand(1);
2581 } else {
2582 // Spill the value to memory and reload it into top of stack.
2583 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2584 MachineFunction &MF = DAG.getMachineFunction();
2585 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2586 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2587 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2588 Value, MemLoc, DAG.getSrcValue(0));
2589 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002590 std::vector<MVT::ValueType> Tys;
2591 Tys.push_back(MVT::f64);
2592 Tys.push_back(MVT::Other);
2593 std::vector<SDOperand> Ops;
2594 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00002595 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002596 Ops.push_back(DAG.getValueType(ArgVT));
2597 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2598 Tys.clear();
2599 Tys.push_back(MVT::Other);
2600 Tys.push_back(MVT::Flag);
2601 Ops.clear();
2602 Ops.push_back(Copy.getValue(1));
2603 Ops.push_back(Copy);
2604 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2605 }
2606 break;
2607 }
2608 case 3:
2609 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2610 SDOperand());
2611 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2612 break;
2613 }
2614 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2615 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2616 Copy.getValue(1));
2617 }
Evan Chengd5e905d2006-03-21 23:01:21 +00002618 case ISD::SCALAR_TO_VECTOR: {
2619 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chenge7ee6a52006-03-24 23:15:12 +00002620 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Chengd5e905d2006-03-21 23:01:21 +00002621 }
Evan Chengd097e672006-03-22 02:53:00 +00002622 case ISD::VECTOR_SHUFFLE: {
2623 SDOperand V1 = Op.getOperand(0);
2624 SDOperand V2 = Op.getOperand(1);
2625 SDOperand PermMask = Op.getOperand(2);
2626 MVT::ValueType VT = Op.getValueType();
Evan Cheng2595a682006-03-24 02:58:06 +00002627 unsigned NumElems = PermMask.getNumOperands();
Evan Chengd097e672006-03-22 02:53:00 +00002628
Evan Chengacc33642006-03-29 19:02:40 +00002629 // Splat && PSHUFD's 2nd vector must be undef.
Evan Cheng7e2ff112006-03-30 19:54:57 +00002630 if (X86::isSplatMask(PermMask.Val)) {
Evan Cheng500ec162006-03-29 03:04:49 +00002631 if (V2.getOpcode() != ISD::UNDEF)
Evan Chengda59b0d2006-03-29 01:30:51 +00002632 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
Evan Cheng500ec162006-03-29 03:04:49 +00002633 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002634 return Op;
Evan Cheng500ec162006-03-29 03:04:49 +00002635 }
Evan Chengda59b0d2006-03-29 01:30:51 +00002636
Evan Chengacc33642006-03-29 19:02:40 +00002637 if (X86::isUNPCKLMask(PermMask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00002638 X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Evan Chengacc33642006-03-29 19:02:40 +00002639 X86::isUNPCKHMask(PermMask.Val))
2640 // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
Evan Cheng2cf42322006-04-05 06:09:26 +00002641 return Op;
Evan Chengacc33642006-03-29 19:02:40 +00002642
Evan Cheng7e2ff112006-03-30 19:54:57 +00002643 if (NumElems == 2)
Evan Cheng2cf42322006-04-05 06:09:26 +00002644 return NormalizeVectorShuffle(Op, DAG);
Evan Cheng7e2ff112006-03-30 19:54:57 +00002645
2646 // If VT is integer, try PSHUF* first, then SHUFP*.
2647 if (MVT::isInteger(VT)) {
2648 if (X86::isPSHUFDMask(PermMask.Val) ||
2649 X86::isPSHUFHWMask(PermMask.Val) ||
2650 X86::isPSHUFLWMask(PermMask.Val)) {
2651 if (V2.getOpcode() != ISD::UNDEF)
2652 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2653 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002654 return Op;
Evan Cheng7e2ff112006-03-30 19:54:57 +00002655 }
2656
2657 if (X86::isSHUFPMask(PermMask.Val))
Evan Cheng2cf42322006-04-05 06:09:26 +00002658 return NormalizeVectorShuffle(Op, DAG);
Evan Cheng59a63552006-04-05 01:47:37 +00002659
2660 // Handle v8i16 shuffle high / low shuffle node pair.
2661 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2662 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2663 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2664 std::vector<SDOperand> MaskVec;
2665 for (unsigned i = 0; i != 4; ++i)
2666 MaskVec.push_back(PermMask.getOperand(i));
2667 for (unsigned i = 4; i != 8; ++i)
2668 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2669 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2670 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2671 MaskVec.clear();
2672 for (unsigned i = 0; i != 4; ++i)
2673 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2674 for (unsigned i = 4; i != 8; ++i)
2675 MaskVec.push_back(PermMask.getOperand(i));
2676 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2677 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2678 }
Evan Cheng7e2ff112006-03-30 19:54:57 +00002679 } else {
2680 // Floating point cases in the other order.
2681 if (X86::isSHUFPMask(PermMask.Val))
Evan Cheng2cf42322006-04-05 06:09:26 +00002682 return NormalizeVectorShuffle(Op, DAG);
Evan Cheng7e2ff112006-03-30 19:54:57 +00002683 if (X86::isPSHUFDMask(PermMask.Val) ||
2684 X86::isPSHUFHWMask(PermMask.Val) ||
2685 X86::isPSHUFLWMask(PermMask.Val)) {
2686 if (V2.getOpcode() != ISD::UNDEF)
2687 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2688 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002689 return Op;
Evan Cheng7e2ff112006-03-30 19:54:57 +00002690 }
Evan Chengda59b0d2006-03-29 01:30:51 +00002691 }
Evan Chengd097e672006-03-22 02:53:00 +00002692
Evan Cheng2cf42322006-04-05 06:09:26 +00002693 return SDOperand();
Evan Chengd097e672006-03-22 02:53:00 +00002694 }
Evan Cheng082c8782006-03-24 07:29:27 +00002695 case ISD::BUILD_VECTOR: {
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00002696 // All one's are handled with pcmpeqd.
2697 if (ISD::isBuildVectorAllOnes(Op.Val))
2698 return Op;
2699
Evan Cheng2bc09412006-03-25 09:37:23 +00002700 std::set<SDOperand> Values;
Evan Chenge7ee6a52006-03-24 23:15:12 +00002701 SDOperand Elt0 = Op.getOperand(0);
Evan Cheng2bc09412006-03-25 09:37:23 +00002702 Values.insert(Elt0);
Evan Chenge7ee6a52006-03-24 23:15:12 +00002703 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2704 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2705 (isa<ConstantFPSDNode>(Elt0) &&
2706 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2707 bool RestAreZero = true;
Evan Cheng082c8782006-03-24 07:29:27 +00002708 unsigned NumElems = Op.getNumOperands();
Evan Chenge7ee6a52006-03-24 23:15:12 +00002709 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng2bc09412006-03-25 09:37:23 +00002710 SDOperand Elt = Op.getOperand(i);
2711 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
Evan Cheng082c8782006-03-24 07:29:27 +00002712 if (!FPC->isExactlyValue(+0.0))
Evan Chenge7ee6a52006-03-24 23:15:12 +00002713 RestAreZero = false;
Evan Cheng2bc09412006-03-25 09:37:23 +00002714 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Evan Cheng082c8782006-03-24 07:29:27 +00002715 if (!C->isNullValue())
Evan Chenge7ee6a52006-03-24 23:15:12 +00002716 RestAreZero = false;
Evan Cheng082c8782006-03-24 07:29:27 +00002717 } else
Evan Chenge7ee6a52006-03-24 23:15:12 +00002718 RestAreZero = false;
Evan Cheng2bc09412006-03-25 09:37:23 +00002719 Values.insert(Elt);
Evan Cheng082c8782006-03-24 07:29:27 +00002720 }
2721
Evan Chenge7ee6a52006-03-24 23:15:12 +00002722 if (RestAreZero) {
2723 if (Elt0IsZero) return Op;
2724
2725 // Zero extend a scalar to a vector.
2726 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2727 }
2728
Evan Cheng2bc09412006-03-25 09:37:23 +00002729 if (Values.size() > 2) {
2730 // Expand into a number of unpckl*.
2731 // e.g. for v4f32
2732 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2733 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2734 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2735 MVT::ValueType VT = Op.getValueType();
Evan Cheng5df75882006-03-28 00:39:58 +00002736 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2737 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2738 std::vector<SDOperand> MaskVec;
2739 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2740 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2741 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2742 }
2743 SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng2bc09412006-03-25 09:37:23 +00002744 std::vector<SDOperand> V(NumElems);
2745 for (unsigned i = 0; i < NumElems; ++i)
2746 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2747 NumElems >>= 1;
2748 while (NumElems != 0) {
2749 for (unsigned i = 0; i < NumElems; ++i)
Evan Cheng5df75882006-03-28 00:39:58 +00002750 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2751 PermMask);
Evan Cheng2bc09412006-03-25 09:37:23 +00002752 NumElems >>= 1;
2753 }
2754 return V[0];
2755 }
2756
Evan Cheng082c8782006-03-24 07:29:27 +00002757 return SDOperand();
2758 }
Evan Chengcbffa462006-03-31 19:22:53 +00002759 case ISD::EXTRACT_VECTOR_ELT: {
Evan Chengebf10062006-04-03 20:53:28 +00002760 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2761 return SDOperand();
2762
Evan Chengcbffa462006-03-31 19:22:53 +00002763 MVT::ValueType VT = Op.getValueType();
2764 if (MVT::getSizeInBits(VT) == 16) {
Evan Chengebf10062006-04-03 20:53:28 +00002765 // Transform it so it match pextrw which produces a 32-bit result.
Evan Chengcbffa462006-03-31 19:22:53 +00002766 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2767 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2768 Op.getOperand(0), Op.getOperand(1));
2769 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2770 DAG.getValueType(VT));
2771 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Chengebf10062006-04-03 20:53:28 +00002772 } else if (MVT::getSizeInBits(VT) == 32) {
2773 SDOperand Vec = Op.getOperand(0);
2774 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2775 if (Idx == 0)
2776 return Op;
2777
2778 // TODO: if Idex == 2, we can use unpckhps
2779 // SHUFPS the element to the lowest double word, then movss.
2780 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2781 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2782 MVT::getVectorBaseType(MaskVT));
2783 std::vector<SDOperand> IdxVec;
2784 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2785 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2786 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2787 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2788 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2789 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2790 Vec, Vec, Mask);
2791 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2792 DAG.getConstant(0, MVT::i32));
2793 } else if (MVT::getSizeInBits(VT) == 64) {
2794 SDOperand Vec = Op.getOperand(0);
2795 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2796 if (Idx == 0)
2797 return Op;
2798
2799 // UNPCKHPD the element to the lowest double word, then movsd.
Evan Chengb64827e2006-04-03 22:30:54 +00002800 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2801 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Evan Chengebf10062006-04-03 20:53:28 +00002802 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2803 std::vector<SDOperand> IdxVec;
2804 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2805 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2806 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2807 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2808 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2809 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2810 DAG.getConstant(0, MVT::i32));
Evan Chengcbffa462006-03-31 19:22:53 +00002811 }
2812
2813 return SDOperand();
2814 }
2815 case ISD::INSERT_VECTOR_ELT: {
2816 // Transform it so it match pinsrw which expects a 16-bit value in a R32
2817 // as its second argument.
2818 MVT::ValueType VT = Op.getValueType();
2819 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2820 if (MVT::getSizeInBits(BaseVT) == 16) {
2821 SDOperand N1 = Op.getOperand(1);
2822 SDOperand N2 = Op.getOperand(2);
2823 if (N1.getValueType() != MVT::i32)
2824 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2825 if (N2.getValueType() != MVT::i32)
2826 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
Evan Cheng5fd7c692006-03-31 21:55:24 +00002827 return DAG.getNode(X86ISD::PINSRW, VT, Op.getOperand(0), N1, N2);
Evan Chengcbffa462006-03-31 19:22:53 +00002828 }
2829
2830 return SDOperand();
2831 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002832 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002833}
Evan Cheng6af02632005-12-20 06:22:03 +00002834
2835const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2836 switch (Opcode) {
2837 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00002838 case X86ISD::SHLD: return "X86ISD::SHLD";
2839 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00002840 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00002841 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00002842 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00002843 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00002844 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2845 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2846 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00002847 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00002848 case X86ISD::FST: return "X86ISD::FST";
2849 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00002850 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00002851 case X86ISD::CALL: return "X86ISD::CALL";
2852 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2853 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2854 case X86ISD::CMP: return "X86ISD::CMP";
2855 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengc1583db2005-12-21 20:21:51 +00002856 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00002857 case X86ISD::CMOV: return "X86ISD::CMOV";
2858 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00002859 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00002860 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2861 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00002862 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00002863 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002864 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00002865 case X86ISD::S2VEC: return "X86ISD::S2VEC";
2866 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00002867 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00002868 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00002869 }
2870}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002871
Nate Begeman8a77efe2006-02-16 21:11:51 +00002872void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2873 uint64_t Mask,
2874 uint64_t &KnownZero,
2875 uint64_t &KnownOne,
2876 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002877 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00002878 assert((Opc >= ISD::BUILTIN_OP_END ||
2879 Opc == ISD::INTRINSIC_WO_CHAIN ||
2880 Opc == ISD::INTRINSIC_W_CHAIN ||
2881 Opc == ISD::INTRINSIC_VOID) &&
2882 "Should use MaskedValueIsZero if you don't know whether Op"
2883 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002884
Evan Cheng6d196db2006-04-05 06:11:20 +00002885 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002886 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00002887 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00002888 case X86ISD::SETCC:
2889 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2890 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002891 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002892}
Chris Lattnerc642aa52006-01-31 19:43:35 +00002893
2894std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00002895getRegClassForInlineAsmConstraint(const std::string &Constraint,
2896 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00002897 if (Constraint.size() == 1) {
2898 // FIXME: not handling fp-stack yet!
2899 // FIXME: not handling MMX registers yet ('y' constraint).
2900 switch (Constraint[0]) { // GCC X86 Constraint Letters
2901 default: break; // Unknown constriant letter
2902 case 'r': // GENERAL_REGS
2903 case 'R': // LEGACY_REGS
2904 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2905 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2906 case 'l': // INDEX_REGS
2907 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2908 X86::ESI, X86::EDI, X86::EBP, 0);
2909 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2910 case 'Q': // Q_REGS
2911 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2912 case 'x': // SSE_REGS if SSE1 allowed
2913 if (Subtarget->hasSSE1())
2914 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2915 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2916 0);
2917 return std::vector<unsigned>();
2918 case 'Y': // SSE_REGS if SSE2 allowed
2919 if (Subtarget->hasSSE2())
2920 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2921 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2922 0);
2923 return std::vector<unsigned>();
2924 }
2925 }
2926
Chris Lattner7ad77df2006-02-22 00:56:39 +00002927 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00002928}
Evan Chengaf598d22006-03-13 23:18:16 +00002929
2930/// isLegalAddressImmediate - Return true if the integer value or
2931/// GlobalValue can be used as the offset of the target addressing mode.
2932bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2933 // X86 allows a sign-extended 32-bit immediate field.
2934 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2935}
2936
2937bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00002938 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00002939 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2940 if (RModel == Reloc::Static)
2941 return true;
2942 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00002943 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00002944 else
2945 return false;
2946 } else
2947 return true;
2948}
Evan Cheng68ad48b2006-03-22 18:59:22 +00002949
2950/// isShuffleMaskLegal - Targets can use this to indicate that they only
2951/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2952/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2953/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00002954bool
2955X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2956 // Only do shuffles on 128-bit vector types for now.
2957 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2595a682006-03-24 02:58:06 +00002958 return (Mask.Val->getNumOperands() == 2 ||
2959 X86::isSplatMask(Mask.Val) ||
Evan Chengd27fb3e2006-03-24 01:18:28 +00002960 X86::isPSHUFDMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00002961 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng5df75882006-03-28 00:39:58 +00002962 X86::isSHUFPMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00002963 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00002964 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00002965 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00002966}