blob: 87438d0b6bee1f42978b97ebead97141f0d33c8b [file] [log] [blame]
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001//===-- X86ISelLowering.h - X86 DAG Lowering Interface ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/Function.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000022#include "llvm/ADT/VectorExtras.h"
23#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000025#include "llvm/CodeGen/MachineFunction.h"
26#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000027#include "llvm/CodeGen/SelectionDAG.h"
28#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000029#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-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 Cheng559806f2006-01-27 08:10:46 +000040 Subtarget = &TM.getSubtarget<X86Subtarget>();
41 X86ScalarSSE = Subtarget->hasSSE2();
42
Chris Lattnerdbdbf0c2005-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 Cheng0b2afbd2006-01-25 09:15:17 +000049 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000050 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000051 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng714554d2006-03-16 21:47:42 +000052
Evan Chenga88973f2006-03-22 19:22:18 +000053 if (!Subtarget->isTargetDarwin())
Evan Chengdf57fa02006-03-17 20:31:41 +000054 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
55 setUseUnderscoreSetJmpLongJmp(true);
56
Evan Cheng714554d2006-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 Lattnera54aa942006-01-29 06:26:08 +000066
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000067 // Set up the register classes.
Chris Lattnerdbdbf0c2005-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 Cheng6892f282006-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 Lattnerdbdbf0c2005-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 Begeman4c5dcf52006-02-17 00:03:04 +000088 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000089 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000090 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +000095
Evan Cheng6dab0532006-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 Cheng02568ff2006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000109 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-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 Cheng45af8fd2006-02-18 07:26:17 +0000119 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-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 Cheng45af8fd2006-02-18 07:26:17 +0000125 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
127
Evan Cheng02568ff2006-01-30 22:13:22 +0000128 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
129 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000130
Evan Cheng5298bcc2006-02-17 07:01:52 +0000131 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000132 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
133 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000134 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
135 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-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 Lenharthb873ff32005-11-20 21:41:10 +0000150 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000151 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000152
Chris Lattnerdbdbf0c2005-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 Begeman4c5dcf52006-02-17 00:03:04 +0000156
157 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-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 Begeman4c5dcf52006-02-17 00:03:04 +0000167 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000168 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000169 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000170 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000171 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000172 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000173 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-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 Begeman4c5dcf52006-02-17 00:03:04 +0000177 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000178 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
179 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000180
Chris Lattnerf73bae12005-11-29 06:16:21 +0000181 // We don't have line number support yet.
182 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000183 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000184 // FIXME - use subtarget debug flags
Evan Chenga88973f2006-03-22 19:22:18 +0000185 if (!Subtarget->isTargetDarwin())
Evan Cheng3c992d22006-03-07 02:02:57 +0000186 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000187
Nate Begemanacc398c2006-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 Lattnere1125522006-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 Lattnerb99329e2006-01-13 02:42:53 +0000198
Chris Lattner9601a862006-03-05 05:08:37 +0000199 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
200 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
201
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000202 if (X86ScalarSSE) {
203 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000204 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
205 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-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 Cheng223547a2006-01-31 22:28:30 +0000211 // Use ANDPD to simulate FABS.
212 setOperationAction(ISD::FABS , MVT::f64, Custom);
213 setOperationAction(ISD::FABS , MVT::f32, Custom);
214
215 // Use XORP to simulate FNEG.
216 setOperationAction(ISD::FNEG , MVT::f64, Custom);
217 setOperationAction(ISD::FNEG , MVT::f32, Custom);
218
Evan Chengd25e9e82006-02-02 00:28:23 +0000219 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 setOperationAction(ISD::FSIN , MVT::f64, Expand);
221 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 setOperationAction(ISD::FREM , MVT::f64, Expand);
223 setOperationAction(ISD::FSIN , MVT::f32, Expand);
224 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::FREM , MVT::f32, Expand);
226
Chris Lattnera54aa942006-01-29 06:26:08 +0000227 // Expand FP immediates into loads from the stack, except for the special
228 // cases we handle.
229 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
230 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000231 addLegalFPImmediate(+0.0); // xorps / xorpd
232 } else {
233 // Set up the FP register classes.
234 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000235
236 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
237
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 if (!UnsafeFPMath) {
239 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
240 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
241 }
242
Chris Lattnera54aa942006-01-29 06:26:08 +0000243 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000244 addLegalFPImmediate(+0.0); // FLD0
245 addLegalFPImmediate(+1.0); // FLD1
246 addLegalFPImmediate(-0.0); // FLD0/FCHS
247 addLegalFPImmediate(-1.0); // FLD1/FCHS
248 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000249
Evan Chengd30bf012006-03-01 01:11:20 +0000250 // First set operation action for all vector types to expand. Then we
251 // will selectively turn on ones that can be effectively codegen'd.
252 for (unsigned VT = (unsigned)MVT::Vector + 1;
253 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
254 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
255 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000258 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000259 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000260 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000261 }
262
Evan Chenga88973f2006-03-22 19:22:18 +0000263 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000264 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
266 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
267
Evan Chengd30bf012006-03-01 01:11:20 +0000268 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000269 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
271 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000272 }
273
Evan Chenga88973f2006-03-22 19:22:18 +0000274 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000275 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
276
Evan Cheng48090aa2006-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 Cheng386031a2006-03-24 07:29:27 +0000281 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000282 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000283 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000284 }
285
Evan Chenga88973f2006-03-22 19:22:18 +0000286 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-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 Cheng48090aa2006-03-21 23:01:21 +0000294 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-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 Cheng48090aa2006-03-21 23:01:21 +0000298 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
Evan Cheng7b1d34b2006-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 Cheng48090aa2006-03-21 23:01:21 +0000302 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
303 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-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 Cheng0038e592006-03-28 00:39:58 +0000308 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Cheng386031a2006-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 Chengb9df0ca2006-03-22 02:53:00 +0000315 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng0038e592006-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 Cheng11e15b32006-04-03 20:53:28 +0000320 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000322 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000323 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000324 }
325
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000326 computeRegisterProperties();
327
Evan Cheng87ed7162006-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 Chenga03a5dc2006-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 Lattnerdbdbf0c2005-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 Chengd9558e02006-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 Cheng8700e142006-01-11 06:09:51 +0000356 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
357 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000358
Chris Lattnerdbdbf0c2005-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 Lattner94dd2922006-02-13 09:00:43 +0000461 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-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 Lattner94dd2922006-02-13 09:00:43 +0000479 Chain = DAG.getCALLSEQ_START(Chain,
480 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-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 Cheng8700e142006-01-11 06:09:51 +0000484 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000549
Nate Begeman4c5dcf52006-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 Chengd90eb7f2006-01-05 00:27:02 +0000556
Nate Begeman4c5dcf52006-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 Chengd90eb7f2006-01-05 00:27:02 +0000560
Nate Begeman4c5dcf52006-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 Chengd90eb7f2006-01-05 00:27:02 +0000574 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000575 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000576 case MVT::i1:
577 case MVT::i8:
Nate Begeman4c5dcf52006-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 Chengd90eb7f2006-01-05 00:27:02 +0000583 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000584 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
585 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000586 break;
Nate Begeman4c5dcf52006-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 Chengd90eb7f2006-01-05 00:27:02 +0000590 break;
Nate Begeman4c5dcf52006-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 Chengd90eb7f2006-01-05 00:27:02 +0000597 break;
598 }
Nate Begeman4c5dcf52006-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 Chengd90eb7f2006-01-05 00:27:02 +0000631
Nate Begeman4c5dcf52006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000639 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000640
641 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000642}
643
Chris Lattnerdbdbf0c2005-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 Lattner89fad2c2006-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 Lattner9d5da1d2006-03-24 07:12:19 +0000677// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-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 Lattner9d5da1d2006-03-24 07:12:19 +0000680// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-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 Lattner1c636e92006-03-17 05:10:20 +0000690
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000712
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000723 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Lattnerf31d1932005-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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000732 }
733 ++NumIntRegs;
734 break;
735 }
736
737 ObjSize = 1;
738 break;
739 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000740 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000753 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000754 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000755 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000766 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000777 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000778 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000779 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000794 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000878 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000887 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
888 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000889 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000890 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
891 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-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 Lattner94dd2922006-02-13 09:00:43 +0000907 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-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 Lattner91cacc82006-01-24 06:14:44 +0000911 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-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 Lattnerf31d1932005-12-27 03:02:18 +0000919 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
920 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000921 case MVT::i8:
922 case MVT::i16:
923 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000924 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000940 // Can pass (at least) part of it in regs?
941 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Lattner1c636e92006-03-17 05:10:20 +0000948
949 // Pass both parts in regs?
950 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-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 Begeman4c5dcf52006-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 Chengd9558e02006-01-06 00:43:03 +00001020 break;
Nate Begeman4c5dcf52006-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 Chengd9558e02006-01-06 00:43:03 +00001061 case MVT::i1:
1062 case MVT::i8:
Nate Begeman4c5dcf52006-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 Chengd9558e02006-01-06 00:43:03 +00001068 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001069 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1070 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001071 break;
Nate Begeman4c5dcf52006-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 Chengd9558e02006-01-06 00:43:03 +00001075 break;
Nate Begeman4c5dcf52006-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 Chengd9558e02006-01-06 00:43:03 +00001082 break;
1083 }
Nate Begeman4c5dcf52006-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 Chengd9558e02006-01-06 00:43:03 +00001116
Nate Begeman4c5dcf52006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +00001124 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001125
1126 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-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 Cheng4a460802006-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 Lattnerdbdbf0c2005-11-15 00:40:23 +00001183
Evan Cheng6dfa9992006-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 Lattner259e97c2006-01-31 19:43:35 +00001188static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1189 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001190 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001191 Flip = false;
1192 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-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 Cheng6dfa9992006-01-30 23:41:35 +00001218 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001219 case ISD::SETOGT:
1220 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001221 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001222 case ISD::SETOGE:
1223 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001224 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001225 case ISD::SETULT:
1226 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001227 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-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 Cheng6dfa9992006-01-30 23:41:35 +00001236
1237 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001238}
1239
Evan Cheng4a460802006-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 Chengaaca22c2006-01-10 20:26:56 +00001242/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001243static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-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 Cheng4a460802006-01-11 00:33:36 +00001259MachineBasicBlock *
1260X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1261 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-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 Lattner259e97c2006-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 Cheng0cc39452006-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 Begemanf15485a2006-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 Cheng0cc39452006-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 Cheng4a460802006-01-11 00:33:36 +00001315
Evan Cheng0cc39452006-01-16 21:21:29 +00001316 delete MI; // The pseudo instruction is gone now.
1317 return BB;
1318 }
Evan Cheng4a460802006-01-11 00:33:36 +00001319
Evan Cheng0cc39452006-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 Lattner6b2469c2006-01-28 10:34:47 +00001346 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-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 Cheng4a460802006-01-11 00:33:36 +00001382}
1383
1384
1385//===----------------------------------------------------------------------===//
1386// X86 Custom Lowering Hooks
1387//===----------------------------------------------------------------------===//
1388
Evan Cheng30b37b52006-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 Cheng0188ecb2006-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 Cheng506d3df2006-03-29 23:07:14 +00001408 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-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 Cheng506d3df2006-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
1420/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1421bool 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 Chengef698ca2006-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 Cheng506d3df2006-03-29 23:07:14 +00001433 return false;
1434 }
1435
1436 // Upper quadword shuffled.
1437 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-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 Cheng506d3df2006-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
1450/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1451bool 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 Chengef698ca2006-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 Cheng506d3df2006-03-29 23:07:14 +00001463 return false;
1464 }
1465
1466 // Lower quadword shuffled.
1467 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-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 Cheng506d3df2006-03-29 23:07:14 +00001472 if (Val > 4)
1473 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001474 }
1475
1476 return true;
1477}
1478
Evan Cheng14aed5e2006-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 Chengbc4832b2006-03-24 23:15:12 +00001484 unsigned NumElems = N->getNumOperands();
1485 if (NumElems == 2) {
Evan Cheng2c0dbd02006-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 Cheng11e15b32006-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 Cheng2c0dbd02006-03-24 02:58:06 +00001496 SDOperand Bit1 = N->getOperand(1);
Evan Cheng11e15b32006-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 Cheng2c0dbd02006-03-24 02:58:06 +00001504 }
1505
Evan Chengbc4832b2006-03-24 23:15:12 +00001506 if (NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001507
1508 // Each half must refer to only one of the vector.
Evan Cheng7d9061e2006-03-30 19:54:57 +00001509 for (unsigned i = 0; i < 2; ++i) {
Evan Chengef698ca2006-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 Cheng7d9061e2006-03-30 19:54:57 +00001514 if (Val >= 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001515 }
Evan Cheng7d9061e2006-03-30 19:54:57 +00001516 for (unsigned i = 2; i < 4; ++i) {
Evan Chengef698ca2006-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 Cheng7d9061e2006-03-30 19:54:57 +00001521 if (Val < 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001522 }
1523
1524 return true;
1525}
1526
Evan Cheng2c0dbd02006-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 Cheng2064a2b2006-03-28 06:50:32 +00001532 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001533 return false;
1534
Evan Cheng2064a2b2006-03-28 06:50:32 +00001535 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001536 SDOperand Bit0 = N->getOperand(0);
1537 SDOperand Bit1 = N->getOperand(1);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001538 SDOperand Bit2 = N->getOperand(2);
1539 SDOperand Bit3 = N->getOperand(3);
Evan Chengef698ca2006-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 Cheng2064a2b2006-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 Chengef698ca2006-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 Cheng2c0dbd02006-03-24 02:58:06 +00001607}
1608
Evan Cheng0038e592006-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 Chengef698ca2006-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 Chengfb47a9b2006-03-31 00:33:57 +00001630 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems)
Evan Chengef698ca2006-03-31 00:30:29 +00001631 return false;
1632 }
Evan Cheng0038e592006-03-28 00:39:58 +00001633 }
1634
1635 return true;
1636}
1637
Evan Cheng4fcb9222006-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 Chengef698ca2006-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 Chengfb47a9b2006-03-31 00:33:57 +00001659 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems/2 + NumElems)
Evan Chengef698ca2006-03-31 00:30:29 +00001660 return false;
1661 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001662 }
1663
1664 return true;
1665}
1666
Evan Chengb9df0ca2006-03-22 02:53:00 +00001667/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1668/// a splat of a single element.
1669bool X86::isSplatMask(SDNode *N) {
1670 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1671
1672 // We can only splat 64-bit, and 32-bit quantities.
1673 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1674 return false;
1675
1676 // This is a splat operation if each element of the permute is the same, and
1677 // if the value doesn't reference the second vector.
1678 SDOperand Elt = N->getOperand(0);
1679 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1680 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001681 SDOperand Arg = N->getOperand(i);
1682 if (Arg.getOpcode() == ISD::UNDEF) continue;
1683 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1684 if (Arg != Elt) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001685 }
1686
1687 // Make sure it is a splat of the first vector operand.
1688 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1689}
1690
Evan Cheng63d33002006-03-22 08:01:21 +00001691/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1692/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1693/// instructions.
1694unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001695 unsigned NumOperands = N->getNumOperands();
1696 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1697 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001698 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001699 unsigned Val = 0;
1700 SDOperand Arg = N->getOperand(NumOperands-i-1);
1701 if (Arg.getOpcode() != ISD::UNDEF)
1702 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00001703 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001704 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00001705 if (i != NumOperands - 1)
1706 Mask <<= Shift;
1707 }
Evan Cheng63d33002006-03-22 08:01:21 +00001708
1709 return Mask;
1710}
1711
Evan Cheng506d3df2006-03-29 23:07:14 +00001712/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1713/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1714/// instructions.
1715unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1716 unsigned Mask = 0;
1717 // 8 nodes, but we only care about the last 4.
1718 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001719 unsigned Val = 0;
1720 SDOperand Arg = N->getOperand(i);
1721 if (Arg.getOpcode() != ISD::UNDEF)
1722 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001723 Mask |= (Val - 4);
1724 if (i != 4)
1725 Mask <<= 2;
1726 }
1727
1728 return Mask;
1729}
1730
1731/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1732/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1733/// instructions.
1734unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1735 unsigned Mask = 0;
1736 // 8 nodes, but we only care about the first 4.
1737 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001738 unsigned Val = 0;
1739 SDOperand Arg = N->getOperand(i);
1740 if (Arg.getOpcode() != ISD::UNDEF)
1741 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001742 Mask |= Val;
1743 if (i != 0)
1744 Mask <<= 2;
1745 }
1746
1747 return Mask;
1748}
1749
Evan Cheng4f563382006-03-29 01:30:51 +00001750/// NormalizeVectorShuffle - Swap vector_shuffle operands (as well as
1751/// values in ther permute mask if needed. Use V1 as second vector if it is
1752/// undef. Return an empty SDOperand is it is already well formed.
1753static SDOperand NormalizeVectorShuffle(SDOperand V1, SDOperand V2,
1754 SDOperand Mask, MVT::ValueType VT,
1755 SelectionDAG &DAG) {
Evan Cheng2064a2b2006-03-28 06:50:32 +00001756 unsigned NumElems = Mask.getNumOperands();
1757 SDOperand Half1 = Mask.getOperand(0);
1758 SDOperand Half2 = Mask.getOperand(NumElems/2);
Evan Cheng4f563382006-03-29 01:30:51 +00001759 bool V2Undef = false;
1760 if (V2.getOpcode() == ISD::UNDEF) {
1761 V2Undef = true;
1762 V2 = V1;
1763 }
1764
Evan Cheng2064a2b2006-03-28 06:50:32 +00001765 if (cast<ConstantSDNode>(Half1)->getValue() >= NumElems &&
1766 cast<ConstantSDNode>(Half2)->getValue() < NumElems) {
1767 // Swap the operands and change mask.
1768 std::vector<SDOperand> MaskVec;
1769 for (unsigned i = NumElems / 2; i != NumElems; ++i)
1770 MaskVec.push_back(Mask.getOperand(i));
1771 for (unsigned i = 0; i != NumElems / 2; ++i)
1772 MaskVec.push_back(Mask.getOperand(i));
1773 Mask =
1774 DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
1775 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1776 }
Evan Cheng4f563382006-03-29 01:30:51 +00001777
1778 if (V2Undef)
1779 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
1780
Evan Cheng2064a2b2006-03-28 06:50:32 +00001781 return SDOperand();
1782}
1783
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001784/// LowerOperation - Provide custom lowering hooks for some operations.
1785///
1786SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1787 switch (Op.getOpcode()) {
1788 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001789 case ISD::SHL_PARTS:
1790 case ISD::SRA_PARTS:
1791 case ISD::SRL_PARTS: {
1792 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1793 "Not an i64 shift!");
1794 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1795 SDOperand ShOpLo = Op.getOperand(0);
1796 SDOperand ShOpHi = Op.getOperand(1);
1797 SDOperand ShAmt = Op.getOperand(2);
1798 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001799 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001800 : DAG.getConstant(0, MVT::i32);
1801
1802 SDOperand Tmp2, Tmp3;
1803 if (Op.getOpcode() == ISD::SHL_PARTS) {
1804 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1805 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1806 } else {
1807 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001808 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001809 }
1810
1811 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1812 ShAmt, DAG.getConstant(32, MVT::i8));
1813
1814 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001815 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001816
1817 std::vector<MVT::ValueType> Tys;
1818 Tys.push_back(MVT::i32);
1819 Tys.push_back(MVT::Flag);
1820 std::vector<SDOperand> Ops;
1821 if (Op.getOpcode() == ISD::SHL_PARTS) {
1822 Ops.push_back(Tmp2);
1823 Ops.push_back(Tmp3);
1824 Ops.push_back(CC);
1825 Ops.push_back(InFlag);
1826 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1827 InFlag = Hi.getValue(1);
1828
1829 Ops.clear();
1830 Ops.push_back(Tmp3);
1831 Ops.push_back(Tmp1);
1832 Ops.push_back(CC);
1833 Ops.push_back(InFlag);
1834 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1835 } else {
1836 Ops.push_back(Tmp2);
1837 Ops.push_back(Tmp3);
1838 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001839 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001840 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1841 InFlag = Lo.getValue(1);
1842
1843 Ops.clear();
1844 Ops.push_back(Tmp3);
1845 Ops.push_back(Tmp1);
1846 Ops.push_back(CC);
1847 Ops.push_back(InFlag);
1848 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1849 }
1850
1851 Tys.clear();
1852 Tys.push_back(MVT::i32);
1853 Tys.push_back(MVT::i32);
1854 Ops.clear();
1855 Ops.push_back(Lo);
1856 Ops.push_back(Hi);
1857 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1858 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001859 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001860 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001861 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001862 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001863
1864 SDOperand Result;
1865 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1866 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001867 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001868 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001869 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001870 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1871 DAG.getEntryNode(), Op.getOperand(0),
1872 StackSlot, DAG.getSrcValue(NULL));
1873
1874 // Build the FILD
1875 std::vector<MVT::ValueType> Tys;
1876 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001877 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001878 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001879 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001880 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001881 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001882 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001883 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1884 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001885
1886 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001887 Chain = Result.getValue(1);
1888 SDOperand InFlag = Result.getValue(2);
1889
Evan Chenge3de85b2006-02-04 02:20:30 +00001890 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001891 // shouldn't be necessary except that RFP cannot be live across
1892 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1893 MachineFunction &MF = DAG.getMachineFunction();
1894 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1895 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1896 std::vector<MVT::ValueType> Tys;
1897 Tys.push_back(MVT::Other);
1898 std::vector<SDOperand> Ops;
1899 Ops.push_back(Chain);
1900 Ops.push_back(Result);
1901 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001902 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001903 Ops.push_back(InFlag);
1904 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1905 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1906 DAG.getSrcValue(NULL));
1907 }
1908
Evan Chenga3195e82006-01-12 22:54:21 +00001909 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001910 }
1911 case ISD::FP_TO_SINT: {
1912 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001913 "Unknown FP_TO_SINT to lower!");
1914 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1915 // stack slot.
1916 MachineFunction &MF = DAG.getMachineFunction();
1917 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1918 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1919 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1920
1921 unsigned Opc;
1922 switch (Op.getValueType()) {
1923 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1924 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1925 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1926 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1927 }
1928
Evan Cheng6dab0532006-01-30 08:02:57 +00001929 SDOperand Chain = DAG.getEntryNode();
1930 SDOperand Value = Op.getOperand(0);
1931 if (X86ScalarSSE) {
1932 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1933 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1934 DAG.getSrcValue(0));
1935 std::vector<MVT::ValueType> Tys;
1936 Tys.push_back(MVT::f64);
1937 Tys.push_back(MVT::Other);
1938 std::vector<SDOperand> Ops;
1939 Ops.push_back(Chain);
1940 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001941 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001942 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1943 Chain = Value.getValue(1);
1944 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1945 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1946 }
1947
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001948 // Build the FP_TO_INT*_IN_MEM
1949 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001950 Ops.push_back(Chain);
1951 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001952 Ops.push_back(StackSlot);
1953 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1954
1955 // Load the result.
1956 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1957 DAG.getSrcValue(NULL));
1958 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001959 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001960 std::vector<MVT::ValueType> Tys;
1961 Tys.push_back(MVT::Other);
1962 Tys.push_back(MVT::Flag);
1963 std::vector<SDOperand> Ops;
1964 Ops.push_back(Op.getOperand(0));
1965 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001966 Ops.clear();
1967 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1968 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1969 MVT::i32, Ops[0].getValue(2)));
1970 Ops.push_back(Ops[1].getValue(1));
1971 Tys[0] = Tys[1] = MVT::i32;
1972 Tys.push_back(MVT::Other);
1973 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001974 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001975 case ISD::FABS: {
1976 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001977 const Type *OpNTy = MVT::getTypeForValueType(VT);
1978 std::vector<Constant*> CV;
1979 if (VT == MVT::f64) {
1980 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1981 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1982 } else {
1983 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1984 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1985 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1986 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1987 }
1988 Constant *CS = ConstantStruct::get(CV);
1989 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1990 SDOperand Mask
1991 = DAG.getNode(X86ISD::LOAD_PACK,
1992 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001993 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1994 }
Evan Cheng223547a2006-01-31 22:28:30 +00001995 case ISD::FNEG: {
1996 MVT::ValueType VT = Op.getValueType();
1997 const Type *OpNTy = MVT::getTypeForValueType(VT);
1998 std::vector<Constant*> CV;
1999 if (VT == MVT::f64) {
2000 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2001 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2002 } else {
2003 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
2004 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2005 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2006 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2007 }
2008 Constant *CS = ConstantStruct::get(CV);
2009 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2010 SDOperand Mask
2011 = DAG.getNode(X86ISD::LOAD_PACK,
2012 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2013 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
2014 }
Evan Chengd5781fc2005-12-21 20:21:51 +00002015 case ISD::SETCC: {
2016 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00002017 SDOperand Cond;
2018 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00002019 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2020 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00002021 bool Flip;
2022 unsigned X86CC;
2023 if (translateX86CC(CC, isFP, X86CC, Flip)) {
2024 if (Flip)
2025 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2026 Op.getOperand(1), Op.getOperand(0));
2027 else
2028 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2029 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00002030 return DAG.getNode(X86ISD::SETCC, MVT::i8,
2031 DAG.getConstant(X86CC, MVT::i8), Cond);
2032 } else {
2033 assert(isFP && "Illegal integer SetCC!");
2034
Evan Cheng6dfa9992006-01-30 23:41:35 +00002035 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2036 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00002037 std::vector<MVT::ValueType> Tys;
2038 std::vector<SDOperand> Ops;
2039 switch (SetCCOpcode) {
2040 default: assert(false && "Illegal floating point SetCC!");
2041 case ISD::SETOEQ: { // !PF & ZF
2042 Tys.push_back(MVT::i8);
2043 Tys.push_back(MVT::Flag);
2044 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
2045 Ops.push_back(Cond);
2046 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2047 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2048 DAG.getConstant(X86ISD::COND_E, MVT::i8),
2049 Tmp1.getValue(1));
2050 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
2051 }
Evan Chengd9558e02006-01-06 00:43:03 +00002052 case ISD::SETUNE: { // PF | !ZF
2053 Tys.push_back(MVT::i8);
2054 Tys.push_back(MVT::Flag);
2055 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
2056 Ops.push_back(Cond);
2057 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2058 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2059 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
2060 Tmp1.getValue(1));
2061 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
2062 }
2063 }
2064 }
Evan Chengd5781fc2005-12-21 20:21:51 +00002065 }
Evan Cheng7df96d62005-12-17 01:21:05 +00002066 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00002067 MVT::ValueType VT = Op.getValueType();
2068 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00002069 bool isFPStack = isFP && !X86ScalarSSE;
2070 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00002071 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00002072 SDOperand Op0 = Op.getOperand(0);
2073 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00002074 if (Op0.getOpcode() == ISD::SETCC)
2075 Op0 = LowerOperation(Op0, DAG);
2076
Evan Chengaaca22c2006-01-10 20:26:56 +00002077 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00002078 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2079 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2080 // have another use it will be eliminated.
2081 // If the X86ISD::SETCC has more than one use, then it's probably better
2082 // to use a test instead of duplicating the X86ISD::CMP (for register
2083 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00002084 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
2085 if (!Op0.hasOneUse()) {
2086 std::vector<MVT::ValueType> Tys;
2087 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2088 Tys.push_back(Op0.Val->getValueType(i));
2089 std::vector<SDOperand> Ops;
2090 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2091 Ops.push_back(Op0.getOperand(i));
2092 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2093 }
2094
Evan Cheng1bcee362006-01-13 01:03:02 +00002095 CC = Op0.getOperand(0);
2096 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00002097 // Make a copy as flag result cannot be used by more than one.
2098 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2099 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00002100 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00002101 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00002102 } else
2103 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00002104 } else
2105 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00002106
Evan Cheng189d01e2006-01-13 01:06:49 +00002107 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00002108 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00002109 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00002110 }
Evan Chenge3413162006-01-09 18:33:28 +00002111
2112 std::vector<MVT::ValueType> Tys;
2113 Tys.push_back(Op.getValueType());
2114 Tys.push_back(MVT::Flag);
2115 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00002116 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2117 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00002118 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00002119 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00002120 Ops.push_back(CC);
2121 Ops.push_back(Cond);
2122 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00002123 }
Evan Cheng898101c2005-12-19 23:12:38 +00002124 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00002125 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00002126 SDOperand Cond = Op.getOperand(1);
2127 SDOperand Dest = Op.getOperand(2);
2128 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00002129 if (Cond.getOpcode() == ISD::SETCC)
2130 Cond = LowerOperation(Cond, DAG);
2131
Evan Chengd5781fc2005-12-21 20:21:51 +00002132 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00002133 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2134 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2135 // have another use it will be eliminated.
2136 // If the X86ISD::SETCC has more than one use, then it's probably better
2137 // to use a test instead of duplicating the X86ISD::CMP (for register
2138 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00002139 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
2140 if (!Cond.hasOneUse()) {
2141 std::vector<MVT::ValueType> Tys;
2142 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2143 Tys.push_back(Cond.Val->getValueType(i));
2144 std::vector<SDOperand> Ops;
2145 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2146 Ops.push_back(Cond.getOperand(i));
2147 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2148 }
2149
Evan Cheng1bcee362006-01-13 01:03:02 +00002150 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00002151 Cond = Cond.getOperand(1);
2152 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00002153 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00002154 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00002155 } else
2156 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00002157 } else
2158 addTest = true;
2159
2160 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00002161 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00002162 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2163 }
2164 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2165 Op.getOperand(0), Op.getOperand(2), CC, Cond);
2166 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002167 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002168 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002169 SDOperand Chain = Op.getOperand(0);
2170 unsigned Align =
2171 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2172 if (Align == 0) Align = 1;
2173
Evan Cheng18a84522006-02-16 00:21:07 +00002174 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2175 // If not DWORD aligned, call memset if size is less than the threshold.
2176 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002177 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002178 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002179 MVT::ValueType IntPtr = getPointerTy();
2180 const Type *IntPtrTy = getTargetData().getIntPtrType();
2181 std::vector<std::pair<SDOperand, const Type*> > Args;
2182 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2183 // Extend the ubyte argument to be an int value for the call.
2184 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2185 Args.push_back(std::make_pair(Val, IntPtrTy));
2186 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2187 std::pair<SDOperand,SDOperand> CallResult =
2188 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2189 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2190 return CallResult.second;
2191 }
2192
Evan Cheng67f92a72006-01-11 22:15:48 +00002193 MVT::ValueType AVT;
2194 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002195 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2196 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002197 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002198 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00002199 unsigned ValReg;
2200 unsigned Val = ValC->getValue() & 255;
2201
2202 // If the value is a constant, then we can potentially use larger sets.
2203 switch (Align & 3) {
2204 case 2: // WORD aligned
2205 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002206 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2207 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002208 Val = (Val << 8) | Val;
2209 ValReg = X86::AX;
2210 break;
2211 case 0: // DWORD aligned
2212 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002213 if (I) {
2214 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2215 BytesLeft = I->getValue() % 4;
2216 } else {
2217 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2218 DAG.getConstant(2, MVT::i8));
2219 TwoRepStos = true;
2220 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002221 Val = (Val << 8) | Val;
2222 Val = (Val << 16) | Val;
2223 ValReg = X86::EAX;
2224 break;
2225 default: // Byte aligned
2226 AVT = MVT::i8;
2227 Count = Op.getOperand(3);
2228 ValReg = X86::AL;
2229 break;
2230 }
2231
2232 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2233 InFlag);
2234 InFlag = Chain.getValue(1);
2235 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00002236 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00002237 Count = Op.getOperand(3);
2238 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2239 InFlag = Chain.getValue(1);
2240 }
2241
2242 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2243 InFlag = Chain.getValue(1);
2244 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2245 InFlag = Chain.getValue(1);
2246
Evan Chengff909922006-03-07 23:29:39 +00002247 std::vector<MVT::ValueType> Tys;
2248 Tys.push_back(MVT::Other);
2249 Tys.push_back(MVT::Flag);
2250 std::vector<SDOperand> Ops;
2251 Ops.push_back(Chain);
2252 Ops.push_back(DAG.getValueType(AVT));
2253 Ops.push_back(InFlag);
2254 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2255
2256 if (TwoRepStos) {
2257 InFlag = Chain.getValue(1);
2258 Count = Op.getOperand(3);
2259 MVT::ValueType CVT = Count.getValueType();
2260 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2261 DAG.getConstant(3, CVT));
2262 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2263 InFlag = Chain.getValue(1);
2264 Tys.clear();
2265 Tys.push_back(MVT::Other);
2266 Tys.push_back(MVT::Flag);
2267 Ops.clear();
2268 Ops.push_back(Chain);
2269 Ops.push_back(DAG.getValueType(MVT::i8));
2270 Ops.push_back(InFlag);
2271 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2272 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002273 // Issue stores for the last 1 - 3 bytes.
2274 SDOperand Value;
2275 unsigned Val = ValC->getValue() & 255;
2276 unsigned Offset = I->getValue() - BytesLeft;
2277 SDOperand DstAddr = Op.getOperand(1);
2278 MVT::ValueType AddrVT = DstAddr.getValueType();
2279 if (BytesLeft >= 2) {
2280 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2281 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2282 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2283 DAG.getConstant(Offset, AddrVT)),
2284 DAG.getSrcValue(NULL));
2285 BytesLeft -= 2;
2286 Offset += 2;
2287 }
2288
2289 if (BytesLeft == 1) {
2290 Value = DAG.getConstant(Val, MVT::i8);
2291 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2292 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2293 DAG.getConstant(Offset, AddrVT)),
2294 DAG.getSrcValue(NULL));
2295 }
2296 }
2297
2298 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002299 }
2300 case ISD::MEMCPY: {
2301 SDOperand Chain = Op.getOperand(0);
2302 unsigned Align =
2303 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2304 if (Align == 0) Align = 1;
2305
Evan Cheng18a84522006-02-16 00:21:07 +00002306 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2307 // If not DWORD aligned, call memcpy if size is less than the threshold.
2308 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002309 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002310 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002311 MVT::ValueType IntPtr = getPointerTy();
2312 const Type *IntPtrTy = getTargetData().getIntPtrType();
2313 std::vector<std::pair<SDOperand, const Type*> > Args;
2314 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2315 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2316 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2317 std::pair<SDOperand,SDOperand> CallResult =
2318 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2319 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2320 return CallResult.second;
2321 }
2322
Evan Cheng67f92a72006-01-11 22:15:48 +00002323 MVT::ValueType AVT;
2324 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002325 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002326 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00002327 switch (Align & 3) {
2328 case 2: // WORD aligned
2329 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002330 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2331 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002332 break;
2333 case 0: // DWORD aligned
2334 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002335 if (I) {
2336 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2337 BytesLeft = I->getValue() % 4;
2338 } else {
2339 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2340 DAG.getConstant(2, MVT::i8));
2341 TwoRepMovs = true;
2342 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002343 break;
2344 default: // Byte aligned
2345 AVT = MVT::i8;
2346 Count = Op.getOperand(3);
2347 break;
2348 }
2349
Evan Cheng62bec2c2006-03-04 02:48:56 +00002350 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002351 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2352 InFlag = Chain.getValue(1);
2353 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2354 InFlag = Chain.getValue(1);
2355 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2356 InFlag = Chain.getValue(1);
2357
Evan Chengff909922006-03-07 23:29:39 +00002358 std::vector<MVT::ValueType> Tys;
2359 Tys.push_back(MVT::Other);
2360 Tys.push_back(MVT::Flag);
2361 std::vector<SDOperand> Ops;
2362 Ops.push_back(Chain);
2363 Ops.push_back(DAG.getValueType(AVT));
2364 Ops.push_back(InFlag);
2365 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2366
2367 if (TwoRepMovs) {
2368 InFlag = Chain.getValue(1);
2369 Count = Op.getOperand(3);
2370 MVT::ValueType CVT = Count.getValueType();
2371 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2372 DAG.getConstant(3, CVT));
2373 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2374 InFlag = Chain.getValue(1);
2375 Tys.clear();
2376 Tys.push_back(MVT::Other);
2377 Tys.push_back(MVT::Flag);
2378 Ops.clear();
2379 Ops.push_back(Chain);
2380 Ops.push_back(DAG.getValueType(MVT::i8));
2381 Ops.push_back(InFlag);
2382 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2383 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002384 // Issue loads and stores for the last 1 - 3 bytes.
2385 unsigned Offset = I->getValue() - BytesLeft;
2386 SDOperand DstAddr = Op.getOperand(1);
2387 MVT::ValueType DstVT = DstAddr.getValueType();
2388 SDOperand SrcAddr = Op.getOperand(2);
2389 MVT::ValueType SrcVT = SrcAddr.getValueType();
2390 SDOperand Value;
2391 if (BytesLeft >= 2) {
2392 Value = DAG.getLoad(MVT::i16, Chain,
2393 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2394 DAG.getConstant(Offset, SrcVT)),
2395 DAG.getSrcValue(NULL));
2396 Chain = Value.getValue(1);
2397 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2398 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2399 DAG.getConstant(Offset, DstVT)),
2400 DAG.getSrcValue(NULL));
2401 BytesLeft -= 2;
2402 Offset += 2;
2403 }
2404
2405 if (BytesLeft == 1) {
2406 Value = DAG.getLoad(MVT::i8, Chain,
2407 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2408 DAG.getConstant(Offset, SrcVT)),
2409 DAG.getSrcValue(NULL));
2410 Chain = Value.getValue(1);
2411 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2412 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2413 DAG.getConstant(Offset, DstVT)),
2414 DAG.getSrcValue(NULL));
2415 }
2416 }
2417
2418 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002419 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002420
2421 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2422 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2423 // one of the above mentioned nodes. It has to be wrapped because otherwise
2424 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2425 // be used to form addressing mode. These wrapped nodes will be selected
2426 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002427 case ISD::ConstantPool: {
2428 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002429 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2430 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2431 CP->getAlignment()));
Evan Chenga88973f2006-03-22 19:22:18 +00002432 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002433 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002434 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002435 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2436 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2437 }
2438
2439 return Result;
2440 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002441 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002442 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2443 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2444 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002445 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002446 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002447 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002448 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2449 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002450
2451 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002452 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002453 // the GlobalAddress must be in the base or index register of the address,
2454 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002455 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002456 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002457 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002458 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002459 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002460
Evan Cheng002fe9b2006-01-12 07:56:47 +00002461 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002462 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002463 case ISD::ExternalSymbol: {
2464 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2465 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2466 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002467 if (Subtarget->isTargetDarwin()) {
Evan Cheng020d2e82006-02-23 20:41:18 +00002468 // With PIC, the address is actually $g + Offset.
2469 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2470 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2471 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2472 }
2473
2474 return Result;
2475 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002476 case ISD::VASTART: {
2477 // vastart just stores the address of the VarArgsFrameIndex slot into the
2478 // memory location argument.
2479 // FIXME: Replace MVT::i32 with PointerTy
2480 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2481 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2482 Op.getOperand(1), Op.getOperand(2));
2483 }
Nate Begemanee625572006-01-27 21:09:22 +00002484 case ISD::RET: {
2485 SDOperand Copy;
2486
2487 switch(Op.getNumOperands()) {
2488 default:
2489 assert(0 && "Do not know how to return this many arguments!");
2490 abort();
2491 case 1:
2492 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2493 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2494 case 2: {
2495 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2496 if (MVT::isInteger(ArgVT))
2497 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2498 SDOperand());
2499 else if (!X86ScalarSSE) {
2500 std::vector<MVT::ValueType> Tys;
2501 Tys.push_back(MVT::Other);
2502 Tys.push_back(MVT::Flag);
2503 std::vector<SDOperand> Ops;
2504 Ops.push_back(Op.getOperand(0));
2505 Ops.push_back(Op.getOperand(1));
2506 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2507 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002508 SDOperand MemLoc;
2509 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002510 SDOperand Value = Op.getOperand(1);
2511
Evan Cheng760df292006-02-01 01:19:32 +00002512 if (Value.getOpcode() == ISD::LOAD &&
2513 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002514 Chain = Value.getOperand(0);
2515 MemLoc = Value.getOperand(1);
2516 } else {
2517 // Spill the value to memory and reload it into top of stack.
2518 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2519 MachineFunction &MF = DAG.getMachineFunction();
2520 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2521 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2522 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2523 Value, MemLoc, DAG.getSrcValue(0));
2524 }
Nate Begemanee625572006-01-27 21:09:22 +00002525 std::vector<MVT::ValueType> Tys;
2526 Tys.push_back(MVT::f64);
2527 Tys.push_back(MVT::Other);
2528 std::vector<SDOperand> Ops;
2529 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002530 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002531 Ops.push_back(DAG.getValueType(ArgVT));
2532 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2533 Tys.clear();
2534 Tys.push_back(MVT::Other);
2535 Tys.push_back(MVT::Flag);
2536 Ops.clear();
2537 Ops.push_back(Copy.getValue(1));
2538 Ops.push_back(Copy);
2539 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2540 }
2541 break;
2542 }
2543 case 3:
2544 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2545 SDOperand());
2546 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2547 break;
2548 }
2549 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2550 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2551 Copy.getValue(1));
2552 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002553 case ISD::SCALAR_TO_VECTOR: {
2554 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengbc4832b2006-03-24 23:15:12 +00002555 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Cheng48090aa2006-03-21 23:01:21 +00002556 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002557 case ISD::VECTOR_SHUFFLE: {
2558 SDOperand V1 = Op.getOperand(0);
2559 SDOperand V2 = Op.getOperand(1);
2560 SDOperand PermMask = Op.getOperand(2);
2561 MVT::ValueType VT = Op.getValueType();
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002562 unsigned NumElems = PermMask.getNumOperands();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002563
Evan Cheng691c9232006-03-29 19:02:40 +00002564 // Splat && PSHUFD's 2nd vector must be undef.
Evan Cheng7d9061e2006-03-30 19:54:57 +00002565 if (X86::isSplatMask(PermMask.Val)) {
Evan Cheng475aecf2006-03-29 03:04:49 +00002566 if (V2.getOpcode() != ISD::UNDEF)
Evan Cheng4f563382006-03-29 01:30:51 +00002567 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
Evan Cheng475aecf2006-03-29 03:04:49 +00002568 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2569 return SDOperand();
2570 }
Evan Cheng4f563382006-03-29 01:30:51 +00002571
Evan Cheng691c9232006-03-29 19:02:40 +00002572 if (X86::isUNPCKLMask(PermMask.Val) ||
2573 X86::isUNPCKHMask(PermMask.Val))
2574 // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
2575 return SDOperand();
2576
Evan Cheng7d9061e2006-03-30 19:54:57 +00002577 if (NumElems == 2)
Evan Cheng4f563382006-03-29 01:30:51 +00002578 return NormalizeVectorShuffle(V1, V2, PermMask, VT, DAG);
Evan Cheng7d9061e2006-03-30 19:54:57 +00002579
2580 // If VT is integer, try PSHUF* first, then SHUFP*.
2581 if (MVT::isInteger(VT)) {
2582 if (X86::isPSHUFDMask(PermMask.Val) ||
2583 X86::isPSHUFHWMask(PermMask.Val) ||
2584 X86::isPSHUFLWMask(PermMask.Val)) {
2585 if (V2.getOpcode() != ISD::UNDEF)
2586 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2587 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2588 return SDOperand();
2589 }
2590
2591 if (X86::isSHUFPMask(PermMask.Val))
2592 return NormalizeVectorShuffle(V1, V2, PermMask, VT, DAG);
2593 } else {
2594 // Floating point cases in the other order.
2595 if (X86::isSHUFPMask(PermMask.Val))
2596 return NormalizeVectorShuffle(V1, V2, PermMask, VT, DAG);
2597 if (X86::isPSHUFDMask(PermMask.Val) ||
2598 X86::isPSHUFHWMask(PermMask.Val) ||
2599 X86::isPSHUFLWMask(PermMask.Val)) {
2600 if (V2.getOpcode() != ISD::UNDEF)
2601 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2602 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2603 return SDOperand();
2604 }
Evan Cheng4f563382006-03-29 01:30:51 +00002605 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002606
Evan Cheng386031a2006-03-24 07:29:27 +00002607 assert(0 && "Unexpected VECTOR_SHUFFLE to lower");
Chris Lattner6df11542006-03-22 04:18:34 +00002608 abort();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002609 }
Evan Cheng386031a2006-03-24 07:29:27 +00002610 case ISD::BUILD_VECTOR: {
Evan Chenga0b3afb2006-03-27 07:00:16 +00002611 // All one's are handled with pcmpeqd.
2612 if (ISD::isBuildVectorAllOnes(Op.Val))
2613 return Op;
2614
Evan Chengc60bd972006-03-25 09:37:23 +00002615 std::set<SDOperand> Values;
Evan Chengbc4832b2006-03-24 23:15:12 +00002616 SDOperand Elt0 = Op.getOperand(0);
Evan Chengc60bd972006-03-25 09:37:23 +00002617 Values.insert(Elt0);
Evan Chengbc4832b2006-03-24 23:15:12 +00002618 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2619 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2620 (isa<ConstantFPSDNode>(Elt0) &&
2621 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2622 bool RestAreZero = true;
Evan Cheng386031a2006-03-24 07:29:27 +00002623 unsigned NumElems = Op.getNumOperands();
Evan Chengbc4832b2006-03-24 23:15:12 +00002624 for (unsigned i = 1; i < NumElems; ++i) {
Evan Chengc60bd972006-03-25 09:37:23 +00002625 SDOperand Elt = Op.getOperand(i);
2626 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002627 if (!FPC->isExactlyValue(+0.0))
Evan Chengbc4832b2006-03-24 23:15:12 +00002628 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002629 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002630 if (!C->isNullValue())
Evan Chengbc4832b2006-03-24 23:15:12 +00002631 RestAreZero = false;
Evan Cheng386031a2006-03-24 07:29:27 +00002632 } else
Evan Chengbc4832b2006-03-24 23:15:12 +00002633 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002634 Values.insert(Elt);
Evan Cheng386031a2006-03-24 07:29:27 +00002635 }
2636
Evan Chengbc4832b2006-03-24 23:15:12 +00002637 if (RestAreZero) {
2638 if (Elt0IsZero) return Op;
2639
2640 // Zero extend a scalar to a vector.
2641 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2642 }
2643
Evan Chengc60bd972006-03-25 09:37:23 +00002644 if (Values.size() > 2) {
2645 // Expand into a number of unpckl*.
2646 // e.g. for v4f32
2647 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2648 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2649 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2650 MVT::ValueType VT = Op.getValueType();
Evan Cheng0038e592006-03-28 00:39:58 +00002651 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2652 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2653 std::vector<SDOperand> MaskVec;
2654 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2655 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2656 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2657 }
2658 SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Chengc60bd972006-03-25 09:37:23 +00002659 std::vector<SDOperand> V(NumElems);
2660 for (unsigned i = 0; i < NumElems; ++i)
2661 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2662 NumElems >>= 1;
2663 while (NumElems != 0) {
2664 for (unsigned i = 0; i < NumElems; ++i)
Evan Cheng0038e592006-03-28 00:39:58 +00002665 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2666 PermMask);
Evan Chengc60bd972006-03-25 09:37:23 +00002667 NumElems >>= 1;
2668 }
2669 return V[0];
2670 }
2671
Evan Cheng386031a2006-03-24 07:29:27 +00002672 return SDOperand();
2673 }
Evan Chengb067a1e2006-03-31 19:22:53 +00002674 case ISD::EXTRACT_VECTOR_ELT: {
Evan Cheng11e15b32006-04-03 20:53:28 +00002675 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2676 return SDOperand();
2677
Evan Chengb067a1e2006-03-31 19:22:53 +00002678 MVT::ValueType VT = Op.getValueType();
2679 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng11e15b32006-04-03 20:53:28 +00002680 // Transform it so it match pextrw which produces a 32-bit result.
Evan Chengb067a1e2006-03-31 19:22:53 +00002681 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2682 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2683 Op.getOperand(0), Op.getOperand(1));
2684 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2685 DAG.getValueType(VT));
2686 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng11e15b32006-04-03 20:53:28 +00002687 } else if (MVT::getSizeInBits(VT) == 32) {
2688 SDOperand Vec = Op.getOperand(0);
2689 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2690 if (Idx == 0)
2691 return Op;
2692
2693 // TODO: if Idex == 2, we can use unpckhps
2694 // SHUFPS the element to the lowest double word, then movss.
2695 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2696 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2697 MVT::getVectorBaseType(MaskVT));
2698 std::vector<SDOperand> IdxVec;
2699 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2700 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2701 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2702 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2703 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2704 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2705 Vec, Vec, Mask);
2706 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2707 DAG.getConstant(0, MVT::i32));
2708 } else if (MVT::getSizeInBits(VT) == 64) {
2709 SDOperand Vec = Op.getOperand(0);
2710 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2711 if (Idx == 0)
2712 return Op;
2713
2714 // UNPCKHPD the element to the lowest double word, then movsd.
Evan Cheng20e3ed12006-04-03 22:30:54 +00002715 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2716 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Evan Cheng11e15b32006-04-03 20:53:28 +00002717 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2718 std::vector<SDOperand> IdxVec;
2719 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2720 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2721 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2722 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2723 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2724 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2725 DAG.getConstant(0, MVT::i32));
Evan Chengb067a1e2006-03-31 19:22:53 +00002726 }
2727
2728 return SDOperand();
2729 }
2730 case ISD::INSERT_VECTOR_ELT: {
2731 // Transform it so it match pinsrw which expects a 16-bit value in a R32
2732 // as its second argument.
2733 MVT::ValueType VT = Op.getValueType();
2734 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2735 if (MVT::getSizeInBits(BaseVT) == 16) {
2736 SDOperand N1 = Op.getOperand(1);
2737 SDOperand N2 = Op.getOperand(2);
2738 if (N1.getValueType() != MVT::i32)
2739 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2740 if (N2.getValueType() != MVT::i32)
2741 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
Evan Cheng653159f2006-03-31 21:55:24 +00002742 return DAG.getNode(X86ISD::PINSRW, VT, Op.getOperand(0), N1, N2);
Evan Chengb067a1e2006-03-31 19:22:53 +00002743 }
2744
2745 return SDOperand();
2746 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002747 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002748}
Evan Cheng72261582005-12-20 06:22:03 +00002749
2750const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2751 switch (Opcode) {
2752 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002753 case X86ISD::SHLD: return "X86ISD::SHLD";
2754 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002755 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002756 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002757 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002758 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002759 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2760 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2761 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002762 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002763 case X86ISD::FST: return "X86ISD::FST";
2764 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002765 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002766 case X86ISD::CALL: return "X86ISD::CALL";
2767 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2768 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2769 case X86ISD::CMP: return "X86ISD::CMP";
2770 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002771 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002772 case X86ISD::CMOV: return "X86ISD::CMOV";
2773 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002774 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002775 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2776 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002777 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002778 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002779 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00002780 case X86ISD::S2VEC: return "X86ISD::S2VEC";
2781 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00002782 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00002783 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng72261582005-12-20 06:22:03 +00002784 }
2785}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002786
Nate Begeman368e18d2006-02-16 21:11:51 +00002787void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2788 uint64_t Mask,
2789 uint64_t &KnownZero,
2790 uint64_t &KnownOne,
2791 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002792
2793 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002794 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002795
2796 switch (Opc) {
2797 default:
2798 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2799 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002800 case X86ISD::SETCC:
2801 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2802 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002803 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002804}
Chris Lattner259e97c2006-01-31 19:43:35 +00002805
2806std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002807getRegClassForInlineAsmConstraint(const std::string &Constraint,
2808 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002809 if (Constraint.size() == 1) {
2810 // FIXME: not handling fp-stack yet!
2811 // FIXME: not handling MMX registers yet ('y' constraint).
2812 switch (Constraint[0]) { // GCC X86 Constraint Letters
2813 default: break; // Unknown constriant letter
2814 case 'r': // GENERAL_REGS
2815 case 'R': // LEGACY_REGS
2816 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2817 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2818 case 'l': // INDEX_REGS
2819 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2820 X86::ESI, X86::EDI, X86::EBP, 0);
2821 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2822 case 'Q': // Q_REGS
2823 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2824 case 'x': // SSE_REGS if SSE1 allowed
2825 if (Subtarget->hasSSE1())
2826 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2827 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2828 0);
2829 return std::vector<unsigned>();
2830 case 'Y': // SSE_REGS if SSE2 allowed
2831 if (Subtarget->hasSSE2())
2832 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2833 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2834 0);
2835 return std::vector<unsigned>();
2836 }
2837 }
2838
Chris Lattner1efa40f2006-02-22 00:56:39 +00002839 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002840}
Evan Cheng30b37b52006-03-13 23:18:16 +00002841
2842/// isLegalAddressImmediate - Return true if the integer value or
2843/// GlobalValue can be used as the offset of the target addressing mode.
2844bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2845 // X86 allows a sign-extended 32-bit immediate field.
2846 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2847}
2848
2849bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00002850 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00002851 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2852 if (RModel == Reloc::Static)
2853 return true;
2854 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002855 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002856 else
2857 return false;
2858 } else
2859 return true;
2860}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002861
2862/// isShuffleMaskLegal - Targets can use this to indicate that they only
2863/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2864/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2865/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00002866bool
2867X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2868 // Only do shuffles on 128-bit vector types for now.
2869 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002870 return (Mask.Val->getNumOperands() == 2 ||
2871 X86::isSplatMask(Mask.Val) ||
Evan Cheng14aed5e2006-03-24 01:18:28 +00002872 X86::isPSHUFDMask(Mask.Val) ||
Evan Cheng506d3df2006-03-29 23:07:14 +00002873 X86::isPSHUFHWMask(Mask.Val) ||
2874 X86::isPSHUFLWMask(Mask.Val) ||
Evan Cheng0038e592006-03-28 00:39:58 +00002875 X86::isSHUFPMask(Mask.Val) ||
Evan Chenged4ca7f2006-03-28 08:27:15 +00002876 X86::isUNPCKLMask(Mask.Val) ||
Jim Laskey2d2a6132006-03-28 10:17:11 +00002877 X86::isUNPCKHMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00002878}