blob: 7f2954c67b2fc02d62319080a91a565affb033f5 [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);
Chris Lattner39afef32006-03-20 06:18:01 +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 Chengd30bf012006-03-01 01:11:20 +0000260 }
261
Evan Chenga88973f2006-03-22 19:22:18 +0000262 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000263 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
265 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
266
Evan Chengd30bf012006-03-01 01:11:20 +0000267 // FIXME: add MMX packed arithmetics
Evan Cheng48090aa2006-03-21 23:01:21 +0000268 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
270 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000271 }
272
Evan Chenga88973f2006-03-22 19:22:18 +0000273 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000274 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
275
Evan Cheng48090aa2006-03-21 23:01:21 +0000276 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
277 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
278 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
279 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
Evan Cheng386031a2006-03-24 07:29:27 +0000280 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000281 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000282 }
283
Evan Chenga88973f2006-03-22 19:22:18 +0000284 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000285 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
287 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
288 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
289 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
290
291
Evan Cheng48090aa2006-03-21 23:01:21 +0000292 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-03-23 01:57:24 +0000293 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
294 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
295 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng48090aa2006-03-21 23:01:21 +0000296 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
Evan Cheng7b1d34b2006-03-25 01:33:37 +0000297 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
298 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
299 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng48090aa2006-03-21 23:01:21 +0000300 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
301 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chenga971f6f2006-03-23 01:57:24 +0000302 setOperationAction(ISD::LOAD, MVT::v16i8, Legal);
303 setOperationAction(ISD::LOAD, MVT::v8i16, Legal);
304 setOperationAction(ISD::LOAD, MVT::v4i32, Legal);
305 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng0038e592006-03-28 00:39:58 +0000306 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
307 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Cheng386031a2006-03-24 07:29:27 +0000308 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
309 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
310 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
311 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
312 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000313 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng0038e592006-03-28 00:39:58 +0000314 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
315 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i16, Custom);
316 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
317 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000318 }
319
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000320 computeRegisterProperties();
321
Evan Cheng87ed7162006-02-14 08:25:08 +0000322 // FIXME: These should be based on subtarget info. Plus, the values should
323 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000324 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
325 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
326 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000327 allowUnalignedMemoryAccesses = true; // x86 supports it!
328}
329
330std::vector<SDOperand>
331X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
332 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
333 return LowerFastCCArguments(F, DAG);
334 return LowerCCCArguments(F, DAG);
335}
336
337std::pair<SDOperand, SDOperand>
338X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
339 bool isVarArg, unsigned CallingConv,
340 bool isTailCall,
341 SDOperand Callee, ArgListTy &Args,
342 SelectionDAG &DAG) {
343 assert((!isVarArg || CallingConv == CallingConv::C) &&
344 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000345
346 // If the callee is a GlobalAddress node (quite common, every direct call is)
347 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
348 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
349 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000350 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
351 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000352
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000353 if (CallingConv == CallingConv::Fast && EnableFastCC)
354 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
355 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
356}
357
358//===----------------------------------------------------------------------===//
359// C Calling Convention implementation
360//===----------------------------------------------------------------------===//
361
362std::vector<SDOperand>
363X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
364 std::vector<SDOperand> ArgValues;
365
366 MachineFunction &MF = DAG.getMachineFunction();
367 MachineFrameInfo *MFI = MF.getFrameInfo();
368
369 // Add DAG nodes to load the arguments... On entry to a function on the X86,
370 // the stack frame looks like this:
371 //
372 // [ESP] -- return address
373 // [ESP + 4] -- first argument (leftmost lexically)
374 // [ESP + 8] -- second argument, if first argument is four bytes in size
375 // ...
376 //
377 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
378 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
379 MVT::ValueType ObjectVT = getValueType(I->getType());
380 unsigned ArgIncrement = 4;
381 unsigned ObjSize;
382 switch (ObjectVT) {
383 default: assert(0 && "Unhandled argument type!");
384 case MVT::i1:
385 case MVT::i8: ObjSize = 1; break;
386 case MVT::i16: ObjSize = 2; break;
387 case MVT::i32: ObjSize = 4; break;
388 case MVT::i64: ObjSize = ArgIncrement = 8; break;
389 case MVT::f32: ObjSize = 4; break;
390 case MVT::f64: ObjSize = ArgIncrement = 8; break;
391 }
392 // Create the frame index object for this incoming parameter...
393 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
394
395 // Create the SelectionDAG nodes corresponding to a load from this parameter
396 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
397
398 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
399 // dead loads.
400 SDOperand ArgValue;
401 if (!I->use_empty())
402 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
403 DAG.getSrcValue(NULL));
404 else {
405 if (MVT::isInteger(ObjectVT))
406 ArgValue = DAG.getConstant(0, ObjectVT);
407 else
408 ArgValue = DAG.getConstantFP(0, ObjectVT);
409 }
410 ArgValues.push_back(ArgValue);
411
412 ArgOffset += ArgIncrement; // Move on to the next argument...
413 }
414
415 // If the function takes variable number of arguments, make a frame index for
416 // the start of the first vararg value... for expansion of llvm.va_start.
417 if (F.isVarArg())
418 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
419 ReturnAddrIndex = 0; // No return address slot generated yet.
420 BytesToPopOnReturn = 0; // Callee pops nothing.
421 BytesCallerReserves = ArgOffset;
422
423 // Finally, inform the code generator which regs we return values in.
424 switch (getValueType(F.getReturnType())) {
425 default: assert(0 && "Unknown type!");
426 case MVT::isVoid: break;
427 case MVT::i1:
428 case MVT::i8:
429 case MVT::i16:
430 case MVT::i32:
431 MF.addLiveOut(X86::EAX);
432 break;
433 case MVT::i64:
434 MF.addLiveOut(X86::EAX);
435 MF.addLiveOut(X86::EDX);
436 break;
437 case MVT::f32:
438 case MVT::f64:
439 MF.addLiveOut(X86::ST0);
440 break;
441 }
442 return ArgValues;
443}
444
445std::pair<SDOperand, SDOperand>
446X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
447 bool isVarArg, bool isTailCall,
448 SDOperand Callee, ArgListTy &Args,
449 SelectionDAG &DAG) {
450 // Count how many bytes are to be pushed on the stack.
451 unsigned NumBytes = 0;
452
453 if (Args.empty()) {
454 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000455 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000456 } else {
457 for (unsigned i = 0, e = Args.size(); i != e; ++i)
458 switch (getValueType(Args[i].second)) {
459 default: assert(0 && "Unknown value type!");
460 case MVT::i1:
461 case MVT::i8:
462 case MVT::i16:
463 case MVT::i32:
464 case MVT::f32:
465 NumBytes += 4;
466 break;
467 case MVT::i64:
468 case MVT::f64:
469 NumBytes += 8;
470 break;
471 }
472
Chris Lattner94dd2922006-02-13 09:00:43 +0000473 Chain = DAG.getCALLSEQ_START(Chain,
474 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000475
476 // Arguments go on the stack in reverse order, as specified by the ABI.
477 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000478 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000479 std::vector<SDOperand> Stores;
480
481 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
482 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
483 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
484
485 switch (getValueType(Args[i].second)) {
486 default: assert(0 && "Unexpected ValueType for argument!");
487 case MVT::i1:
488 case MVT::i8:
489 case MVT::i16:
490 // Promote the integer to 32 bits. If the input type is signed use a
491 // sign extend, otherwise use a zero extend.
492 if (Args[i].second->isSigned())
493 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
494 else
495 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
496
497 // FALL THROUGH
498 case MVT::i32:
499 case MVT::f32:
500 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
501 Args[i].first, PtrOff,
502 DAG.getSrcValue(NULL)));
503 ArgOffset += 4;
504 break;
505 case MVT::i64:
506 case MVT::f64:
507 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
508 Args[i].first, PtrOff,
509 DAG.getSrcValue(NULL)));
510 ArgOffset += 8;
511 break;
512 }
513 }
514 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
515 }
516
517 std::vector<MVT::ValueType> RetVals;
518 MVT::ValueType RetTyVT = getValueType(RetTy);
519 RetVals.push_back(MVT::Other);
520
521 // The result values produced have to be legal. Promote the result.
522 switch (RetTyVT) {
523 case MVT::isVoid: break;
524 default:
525 RetVals.push_back(RetTyVT);
526 break;
527 case MVT::i1:
528 case MVT::i8:
529 case MVT::i16:
530 RetVals.push_back(MVT::i32);
531 break;
532 case MVT::f32:
533 if (X86ScalarSSE)
534 RetVals.push_back(MVT::f32);
535 else
536 RetVals.push_back(MVT::f64);
537 break;
538 case MVT::i64:
539 RetVals.push_back(MVT::i32);
540 RetVals.push_back(MVT::i32);
541 break;
542 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000543
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000544 std::vector<MVT::ValueType> NodeTys;
545 NodeTys.push_back(MVT::Other); // Returns a chain
546 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
547 std::vector<SDOperand> Ops;
548 Ops.push_back(Chain);
549 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000550
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000551 // FIXME: Do not generate X86ISD::TAILCALL for now.
552 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
553 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000554
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000555 NodeTys.clear();
556 NodeTys.push_back(MVT::Other); // Returns a chain
557 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
558 Ops.clear();
559 Ops.push_back(Chain);
560 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
561 Ops.push_back(DAG.getConstant(0, getPointerTy()));
562 Ops.push_back(InFlag);
563 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
564 InFlag = Chain.getValue(1);
565
566 SDOperand RetVal;
567 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000568 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000569 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000570 case MVT::i1:
571 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000572 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
573 Chain = RetVal.getValue(1);
574 if (RetTyVT == MVT::i1)
575 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
576 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000577 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000578 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
579 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000580 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000581 case MVT::i32:
582 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
583 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000584 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000585 case MVT::i64: {
586 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
587 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
588 Lo.getValue(2));
589 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
590 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000591 break;
592 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000593 case MVT::f32:
594 case MVT::f64: {
595 std::vector<MVT::ValueType> Tys;
596 Tys.push_back(MVT::f64);
597 Tys.push_back(MVT::Other);
598 Tys.push_back(MVT::Flag);
599 std::vector<SDOperand> Ops;
600 Ops.push_back(Chain);
601 Ops.push_back(InFlag);
602 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
603 Chain = RetVal.getValue(1);
604 InFlag = RetVal.getValue(2);
605 if (X86ScalarSSE) {
606 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
607 // shouldn't be necessary except that RFP cannot be live across
608 // multiple blocks. When stackifier is fixed, they can be uncoupled.
609 MachineFunction &MF = DAG.getMachineFunction();
610 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
611 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
612 Tys.clear();
613 Tys.push_back(MVT::Other);
614 Ops.clear();
615 Ops.push_back(Chain);
616 Ops.push_back(RetVal);
617 Ops.push_back(StackSlot);
618 Ops.push_back(DAG.getValueType(RetTyVT));
619 Ops.push_back(InFlag);
620 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
621 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
622 DAG.getSrcValue(NULL));
623 Chain = RetVal.getValue(1);
624 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000625
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000626 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
627 // FIXME: we would really like to remember that this FP_ROUND
628 // operation is okay to eliminate if we allow excess FP precision.
629 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
630 break;
631 }
632 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000633 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000634
635 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000636}
637
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000638//===----------------------------------------------------------------------===//
639// Fast Calling Convention implementation
640//===----------------------------------------------------------------------===//
641//
642// The X86 'fast' calling convention passes up to two integer arguments in
643// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
644// and requires that the callee pop its arguments off the stack (allowing proper
645// tail calls), and has the same return value conventions as C calling convs.
646//
647// This calling convention always arranges for the callee pop value to be 8n+4
648// bytes, which is needed for tail recursion elimination and stack alignment
649// reasons.
650//
651// Note that this can be enhanced in the future to pass fp vals in registers
652// (when we have a global fp allocator) and do other tricks.
653//
654
655/// AddLiveIn - This helper function adds the specified physical register to the
656/// MachineFunction as a live in value. It also creates a corresponding virtual
657/// register for it.
658static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
659 TargetRegisterClass *RC) {
660 assert(RC->contains(PReg) && "Not the correct regclass!");
661 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
662 MF.addLiveIn(PReg, VReg);
663 return VReg;
664}
665
Chris Lattner89fad2c2006-03-17 17:27:47 +0000666// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
667// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
668// EDX". Anything more is illegal.
669//
670// FIXME: The linscan register allocator currently has problem with
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000671// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner89fad2c2006-03-17 17:27:47 +0000672// a physreg with a virtreg, this increases the size of the physreg's live
673// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattner9d5da1d2006-03-24 07:12:19 +0000674// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner89fad2c2006-03-17 17:27:47 +0000675// allocator to wedge itself.
676//
677// This code triggers this problem more often if we pass args in registers,
678// so disable it until this is fixed.
679//
680// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
681// about code being dead.
682//
683static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000684
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000685
686std::vector<SDOperand>
687X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
688 std::vector<SDOperand> ArgValues;
689
690 MachineFunction &MF = DAG.getMachineFunction();
691 MachineFrameInfo *MFI = MF.getFrameInfo();
692
693 // Add DAG nodes to load the arguments... On entry to a function the stack
694 // frame looks like this:
695 //
696 // [ESP] -- return address
697 // [ESP + 4] -- first nonreg argument (leftmost lexically)
698 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
699 // ...
700 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
701
702 // Keep track of the number of integer regs passed so far. This can be either
703 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
704 // used).
705 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000706
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000707 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
708 MVT::ValueType ObjectVT = getValueType(I->getType());
709 unsigned ArgIncrement = 4;
710 unsigned ObjSize = 0;
711 SDOperand ArgValue;
712
713 switch (ObjectVT) {
714 default: assert(0 && "Unhandled argument type!");
715 case MVT::i1:
716 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000717 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000718 if (!I->use_empty()) {
719 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
720 X86::R8RegisterClass);
721 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
722 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000723 if (ObjectVT == MVT::i1)
724 // FIXME: Should insert a assertzext here.
725 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000726 }
727 ++NumIntRegs;
728 break;
729 }
730
731 ObjSize = 1;
732 break;
733 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000734 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000735 if (!I->use_empty()) {
736 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
737 X86::R16RegisterClass);
738 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
739 DAG.setRoot(ArgValue.getValue(1));
740 }
741 ++NumIntRegs;
742 break;
743 }
744 ObjSize = 2;
745 break;
746 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000747 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000748 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000749 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000750 X86::R32RegisterClass);
751 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
752 DAG.setRoot(ArgValue.getValue(1));
753 }
754 ++NumIntRegs;
755 break;
756 }
757 ObjSize = 4;
758 break;
759 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000760 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000761 if (!I->use_empty()) {
762 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
763 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
764
765 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
766 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
767 DAG.setRoot(Hi.getValue(1));
768
769 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
770 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000771 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000772 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000773 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000774 if (!I->use_empty()) {
775 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
776 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
777 DAG.setRoot(Low.getValue(1));
778
779 // Load the high part from memory.
780 // Create the frame index object for this incoming parameter...
781 int FI = MFI->CreateFixedObject(4, ArgOffset);
782 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
783 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
784 DAG.getSrcValue(NULL));
785 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
786 }
787 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000788 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000789 break;
790 }
791 ObjSize = ArgIncrement = 8;
792 break;
793 case MVT::f32: ObjSize = 4; break;
794 case MVT::f64: ObjSize = ArgIncrement = 8; break;
795 }
796
797 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
798 // dead loads.
799 if (ObjSize && !I->use_empty()) {
800 // Create the frame index object for this incoming parameter...
801 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
802
803 // Create the SelectionDAG nodes corresponding to a load from this
804 // parameter.
805 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
806
807 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
808 DAG.getSrcValue(NULL));
809 } else if (ArgValue.Val == 0) {
810 if (MVT::isInteger(ObjectVT))
811 ArgValue = DAG.getConstant(0, ObjectVT);
812 else
813 ArgValue = DAG.getConstantFP(0, ObjectVT);
814 }
815 ArgValues.push_back(ArgValue);
816
817 if (ObjSize)
818 ArgOffset += ArgIncrement; // Move on to the next argument.
819 }
820
821 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
822 // arguments and the arguments after the retaddr has been pushed are aligned.
823 if ((ArgOffset & 7) == 0)
824 ArgOffset += 4;
825
826 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
827 ReturnAddrIndex = 0; // No return address slot generated yet.
828 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
829 BytesCallerReserves = 0;
830
831 // Finally, inform the code generator which regs we return values in.
832 switch (getValueType(F.getReturnType())) {
833 default: assert(0 && "Unknown type!");
834 case MVT::isVoid: break;
835 case MVT::i1:
836 case MVT::i8:
837 case MVT::i16:
838 case MVT::i32:
839 MF.addLiveOut(X86::EAX);
840 break;
841 case MVT::i64:
842 MF.addLiveOut(X86::EAX);
843 MF.addLiveOut(X86::EDX);
844 break;
845 case MVT::f32:
846 case MVT::f64:
847 MF.addLiveOut(X86::ST0);
848 break;
849 }
850 return ArgValues;
851}
852
853std::pair<SDOperand, SDOperand>
854X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
855 bool isTailCall, SDOperand Callee,
856 ArgListTy &Args, SelectionDAG &DAG) {
857 // Count how many bytes are to be pushed on the stack.
858 unsigned NumBytes = 0;
859
860 // Keep track of the number of integer regs passed so far. This can be either
861 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
862 // used).
863 unsigned NumIntRegs = 0;
864
865 for (unsigned i = 0, e = Args.size(); i != e; ++i)
866 switch (getValueType(Args[i].second)) {
867 default: assert(0 && "Unknown value type!");
868 case MVT::i1:
869 case MVT::i8:
870 case MVT::i16:
871 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000872 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000873 ++NumIntRegs;
874 break;
875 }
876 // fall through
877 case MVT::f32:
878 NumBytes += 4;
879 break;
880 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000881 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
882 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000883 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000884 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
885 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000886 NumBytes += 4;
887 break;
888 }
889
890 // fall through
891 case MVT::f64:
892 NumBytes += 8;
893 break;
894 }
895
896 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
897 // arguments and the arguments after the retaddr has been pushed are aligned.
898 if ((NumBytes & 7) == 0)
899 NumBytes += 4;
900
Chris Lattner94dd2922006-02-13 09:00:43 +0000901 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000902
903 // Arguments go on the stack in reverse order, as specified by the ABI.
904 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000905 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000906 NumIntRegs = 0;
907 std::vector<SDOperand> Stores;
908 std::vector<SDOperand> RegValuesToPass;
909 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
910 switch (getValueType(Args[i].second)) {
911 default: assert(0 && "Unexpected ValueType for argument!");
912 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000913 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
914 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000915 case MVT::i8:
916 case MVT::i16:
917 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000918 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000919 RegValuesToPass.push_back(Args[i].first);
920 ++NumIntRegs;
921 break;
922 }
923 // Fall through
924 case MVT::f32: {
925 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
926 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
927 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
928 Args[i].first, PtrOff,
929 DAG.getSrcValue(NULL)));
930 ArgOffset += 4;
931 break;
932 }
933 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000934 // Can pass (at least) part of it in regs?
935 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000936 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
937 Args[i].first, DAG.getConstant(1, MVT::i32));
938 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
939 Args[i].first, DAG.getConstant(0, MVT::i32));
940 RegValuesToPass.push_back(Lo);
941 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000942
943 // Pass both parts in regs?
944 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000945 RegValuesToPass.push_back(Hi);
946 ++NumIntRegs;
947 } else {
948 // Pass the high part in memory.
949 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
950 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
951 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
952 Hi, PtrOff, DAG.getSrcValue(NULL)));
953 ArgOffset += 4;
954 }
955 break;
956 }
957 // Fall through
958 case MVT::f64:
959 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
960 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
961 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
962 Args[i].first, PtrOff,
963 DAG.getSrcValue(NULL)));
964 ArgOffset += 8;
965 break;
966 }
967 }
968 if (!Stores.empty())
969 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
970
971 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
972 // arguments and the arguments after the retaddr has been pushed are aligned.
973 if ((ArgOffset & 7) == 0)
974 ArgOffset += 4;
975
976 std::vector<MVT::ValueType> RetVals;
977 MVT::ValueType RetTyVT = getValueType(RetTy);
978
979 RetVals.push_back(MVT::Other);
980
981 // The result values produced have to be legal. Promote the result.
982 switch (RetTyVT) {
983 case MVT::isVoid: break;
984 default:
985 RetVals.push_back(RetTyVT);
986 break;
987 case MVT::i1:
988 case MVT::i8:
989 case MVT::i16:
990 RetVals.push_back(MVT::i32);
991 break;
992 case MVT::f32:
993 if (X86ScalarSSE)
994 RetVals.push_back(MVT::f32);
995 else
996 RetVals.push_back(MVT::f64);
997 break;
998 case MVT::i64:
999 RetVals.push_back(MVT::i32);
1000 RetVals.push_back(MVT::i32);
1001 break;
1002 }
1003
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001004 // Build a sequence of copy-to-reg nodes chained together with token chain
1005 // and flag operands which copy the outgoing args into registers.
1006 SDOperand InFlag;
1007 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1008 unsigned CCReg;
1009 SDOperand RegToPass = RegValuesToPass[i];
1010 switch (RegToPass.getValueType()) {
1011 default: assert(0 && "Bad thing to pass in regs");
1012 case MVT::i8:
1013 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +00001014 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001015 case MVT::i16:
1016 CCReg = (i == 0) ? X86::AX : X86::DX;
1017 break;
1018 case MVT::i32:
1019 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1020 break;
1021 }
1022
1023 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1024 InFlag = Chain.getValue(1);
1025 }
1026
1027 std::vector<MVT::ValueType> NodeTys;
1028 NodeTys.push_back(MVT::Other); // Returns a chain
1029 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1030 std::vector<SDOperand> Ops;
1031 Ops.push_back(Chain);
1032 Ops.push_back(Callee);
1033 if (InFlag.Val)
1034 Ops.push_back(InFlag);
1035
1036 // FIXME: Do not generate X86ISD::TAILCALL for now.
1037 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1038 InFlag = Chain.getValue(1);
1039
1040 NodeTys.clear();
1041 NodeTys.push_back(MVT::Other); // Returns a chain
1042 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1043 Ops.clear();
1044 Ops.push_back(Chain);
1045 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1046 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1047 Ops.push_back(InFlag);
1048 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1049 InFlag = Chain.getValue(1);
1050
1051 SDOperand RetVal;
1052 if (RetTyVT != MVT::isVoid) {
1053 switch (RetTyVT) {
1054 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001055 case MVT::i1:
1056 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001057 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1058 Chain = RetVal.getValue(1);
1059 if (RetTyVT == MVT::i1)
1060 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1061 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001062 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001063 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1064 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001065 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001066 case MVT::i32:
1067 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1068 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001069 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001070 case MVT::i64: {
1071 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1072 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1073 Lo.getValue(2));
1074 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1075 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001076 break;
1077 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001078 case MVT::f32:
1079 case MVT::f64: {
1080 std::vector<MVT::ValueType> Tys;
1081 Tys.push_back(MVT::f64);
1082 Tys.push_back(MVT::Other);
1083 Tys.push_back(MVT::Flag);
1084 std::vector<SDOperand> Ops;
1085 Ops.push_back(Chain);
1086 Ops.push_back(InFlag);
1087 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1088 Chain = RetVal.getValue(1);
1089 InFlag = RetVal.getValue(2);
1090 if (X86ScalarSSE) {
1091 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1092 // shouldn't be necessary except that RFP cannot be live across
1093 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1094 MachineFunction &MF = DAG.getMachineFunction();
1095 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1096 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1097 Tys.clear();
1098 Tys.push_back(MVT::Other);
1099 Ops.clear();
1100 Ops.push_back(Chain);
1101 Ops.push_back(RetVal);
1102 Ops.push_back(StackSlot);
1103 Ops.push_back(DAG.getValueType(RetTyVT));
1104 Ops.push_back(InFlag);
1105 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1106 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1107 DAG.getSrcValue(NULL));
1108 Chain = RetVal.getValue(1);
1109 }
Evan Chengd9558e02006-01-06 00:43:03 +00001110
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001111 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1112 // FIXME: we would really like to remember that this FP_ROUND
1113 // operation is okay to eliminate if we allow excess FP precision.
1114 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1115 break;
1116 }
1117 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001119
1120 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001121}
1122
1123SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1124 if (ReturnAddrIndex == 0) {
1125 // Set up a frame object for the return address.
1126 MachineFunction &MF = DAG.getMachineFunction();
1127 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1128 }
1129
1130 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1131}
1132
1133
1134
1135std::pair<SDOperand, SDOperand> X86TargetLowering::
1136LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1137 SelectionDAG &DAG) {
1138 SDOperand Result;
1139 if (Depth) // Depths > 0 not supported yet!
1140 Result = DAG.getConstant(0, getPointerTy());
1141 else {
1142 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1143 if (!isFrameAddress)
1144 // Just load the return address
1145 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1146 DAG.getSrcValue(NULL));
1147 else
1148 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1149 DAG.getConstant(4, MVT::i32));
1150 }
1151 return std::make_pair(Result, Chain);
1152}
1153
Evan Cheng4a460802006-01-11 00:33:36 +00001154/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1155/// which corresponds to the condition code.
1156static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1157 switch (X86CC) {
1158 default: assert(0 && "Unknown X86 conditional code!");
1159 case X86ISD::COND_A: return X86::JA;
1160 case X86ISD::COND_AE: return X86::JAE;
1161 case X86ISD::COND_B: return X86::JB;
1162 case X86ISD::COND_BE: return X86::JBE;
1163 case X86ISD::COND_E: return X86::JE;
1164 case X86ISD::COND_G: return X86::JG;
1165 case X86ISD::COND_GE: return X86::JGE;
1166 case X86ISD::COND_L: return X86::JL;
1167 case X86ISD::COND_LE: return X86::JLE;
1168 case X86ISD::COND_NE: return X86::JNE;
1169 case X86ISD::COND_NO: return X86::JNO;
1170 case X86ISD::COND_NP: return X86::JNP;
1171 case X86ISD::COND_NS: return X86::JNS;
1172 case X86ISD::COND_O: return X86::JO;
1173 case X86ISD::COND_P: return X86::JP;
1174 case X86ISD::COND_S: return X86::JS;
1175 }
1176}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001177
Evan Cheng6dfa9992006-01-30 23:41:35 +00001178/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1179/// specific condition code. It returns a false if it cannot do a direct
1180/// translation. X86CC is the translated CondCode. Flip is set to true if the
1181/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001182static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1183 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001184 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001185 Flip = false;
1186 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001187 if (!isFP) {
1188 switch (SetCCOpcode) {
1189 default: break;
1190 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1191 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1192 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1193 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1194 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1195 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1196 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1197 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1198 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1199 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1200 }
1201 } else {
1202 // On a floating point condition, the flags are set as follows:
1203 // ZF PF CF op
1204 // 0 | 0 | 0 | X > Y
1205 // 0 | 0 | 1 | X < Y
1206 // 1 | 0 | 0 | X == Y
1207 // 1 | 1 | 1 | unordered
1208 switch (SetCCOpcode) {
1209 default: break;
1210 case ISD::SETUEQ:
1211 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001212 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001213 case ISD::SETOGT:
1214 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001215 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001216 case ISD::SETOGE:
1217 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001218 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001219 case ISD::SETULT:
1220 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001221 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001222 case ISD::SETULE:
1223 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1224 case ISD::SETONE:
1225 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1226 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1227 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1228 }
1229 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001230
1231 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001232}
1233
Evan Cheng4a460802006-01-11 00:33:36 +00001234/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1235/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001236/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001237static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001238 switch (X86CC) {
1239 default:
1240 return false;
1241 case X86ISD::COND_B:
1242 case X86ISD::COND_BE:
1243 case X86ISD::COND_E:
1244 case X86ISD::COND_P:
1245 case X86ISD::COND_A:
1246 case X86ISD::COND_AE:
1247 case X86ISD::COND_NE:
1248 case X86ISD::COND_NP:
1249 return true;
1250 }
1251}
1252
Evan Cheng4a460802006-01-11 00:33:36 +00001253MachineBasicBlock *
1254X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1255 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001256 switch (MI->getOpcode()) {
1257 default: assert(false && "Unexpected instr type to insert");
1258 case X86::CMOV_FR32:
1259 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001260 // To "insert" a SELECT_CC instruction, we actually have to insert the
1261 // diamond control-flow pattern. The incoming instruction knows the
1262 // destination vreg to set, the condition code register to branch on, the
1263 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001264 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1265 ilist<MachineBasicBlock>::iterator It = BB;
1266 ++It;
1267
1268 // thisMBB:
1269 // ...
1270 // TrueVal = ...
1271 // cmpTY ccX, r1, r2
1272 // bCC copy1MBB
1273 // fallthrough --> copy0MBB
1274 MachineBasicBlock *thisMBB = BB;
1275 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1276 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1277 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1278 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1279 MachineFunction *F = BB->getParent();
1280 F->getBasicBlockList().insert(It, copy0MBB);
1281 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemanf15485a2006-03-27 01:32:24 +00001282 // Update machine-CFG edges by first adding all successors of the current
1283 // block to the new block which will contain the Phi node for the select.
1284 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1285 e = BB->succ_end(); i != e; ++i)
1286 sinkMBB->addSuccessor(*i);
1287 // Next, remove all successors of the current block, and add the true
1288 // and fallthrough blocks as its successors.
1289 while(!BB->succ_empty())
1290 BB->removeSuccessor(BB->succ_begin());
Evan Cheng0cc39452006-01-16 21:21:29 +00001291 BB->addSuccessor(copy0MBB);
1292 BB->addSuccessor(sinkMBB);
1293
1294 // copy0MBB:
1295 // %FalseValue = ...
1296 // # fallthrough to sinkMBB
1297 BB = copy0MBB;
1298
1299 // Update machine-CFG edges
1300 BB->addSuccessor(sinkMBB);
1301
1302 // sinkMBB:
1303 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1304 // ...
1305 BB = sinkMBB;
1306 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1307 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1308 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001309
Evan Cheng0cc39452006-01-16 21:21:29 +00001310 delete MI; // The pseudo instruction is gone now.
1311 return BB;
1312 }
Evan Cheng4a460802006-01-11 00:33:36 +00001313
Evan Cheng0cc39452006-01-16 21:21:29 +00001314 case X86::FP_TO_INT16_IN_MEM:
1315 case X86::FP_TO_INT32_IN_MEM:
1316 case X86::FP_TO_INT64_IN_MEM: {
1317 // Change the floating point control register to use "round towards zero"
1318 // mode when truncating to an integer value.
1319 MachineFunction *F = BB->getParent();
1320 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1321 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1322
1323 // Load the old value of the high byte of the control word...
1324 unsigned OldCW =
1325 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1326 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1327
1328 // Set the high part to be round to zero...
1329 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1330
1331 // Reload the modified control word now...
1332 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1333
1334 // Restore the memory image of control word to original value
1335 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1336
1337 // Get the X86 opcode to use.
1338 unsigned Opc;
1339 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001340 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001341 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1342 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1343 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1344 }
1345
1346 X86AddressMode AM;
1347 MachineOperand &Op = MI->getOperand(0);
1348 if (Op.isRegister()) {
1349 AM.BaseType = X86AddressMode::RegBase;
1350 AM.Base.Reg = Op.getReg();
1351 } else {
1352 AM.BaseType = X86AddressMode::FrameIndexBase;
1353 AM.Base.FrameIndex = Op.getFrameIndex();
1354 }
1355 Op = MI->getOperand(1);
1356 if (Op.isImmediate())
1357 AM.Scale = Op.getImmedValue();
1358 Op = MI->getOperand(2);
1359 if (Op.isImmediate())
1360 AM.IndexReg = Op.getImmedValue();
1361 Op = MI->getOperand(3);
1362 if (Op.isGlobalAddress()) {
1363 AM.GV = Op.getGlobal();
1364 } else {
1365 AM.Disp = Op.getImmedValue();
1366 }
1367 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1368
1369 // Reload the original control word now.
1370 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1371
1372 delete MI; // The pseudo instruction is gone now.
1373 return BB;
1374 }
1375 }
Evan Cheng4a460802006-01-11 00:33:36 +00001376}
1377
1378
1379//===----------------------------------------------------------------------===//
1380// X86 Custom Lowering Hooks
1381//===----------------------------------------------------------------------===//
1382
Evan Cheng30b37b52006-03-13 23:18:16 +00001383/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1384/// load. For Darwin, external and weak symbols are indirect, loading the value
1385/// at address GV rather then the value of GV itself. This means that the
1386/// GlobalAddress must be in the base or index register of the address, not the
1387/// GV offset field.
1388static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1389 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1390 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1391}
1392
Evan Cheng0188ecb2006-03-22 18:59:22 +00001393/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1394/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1395bool X86::isPSHUFDMask(SDNode *N) {
1396 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1397
1398 if (N->getNumOperands() != 4)
1399 return false;
1400
1401 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001402 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng0188ecb2006-03-22 18:59:22 +00001403 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1404 "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng506d3df2006-03-29 23:07:14 +00001405 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() >= 4)
1406 return false;
1407 }
1408
1409 return true;
1410}
1411
1412/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
1413/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1414bool X86::isPSHUFHWMask(SDNode *N) {
1415 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1416
1417 if (N->getNumOperands() != 8)
1418 return false;
1419
1420 // Lower quadword copied in order.
1421 for (unsigned i = 0; i != 4; ++i) {
1422 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1423 "Invalid VECTOR_SHUFFLE mask!");
1424 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() != i)
1425 return false;
1426 }
1427
1428 // Upper quadword shuffled.
1429 for (unsigned i = 4; i != 8; ++i) {
1430 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1431 "Invalid VECTOR_SHUFFLE mask!");
1432 unsigned Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1433 if (Val < 4 || Val > 7)
1434 return false;
1435 }
1436
1437 return true;
1438}
1439
1440/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
1441/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1442bool X86::isPSHUFLWMask(SDNode *N) {
1443 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1444
1445 if (N->getNumOperands() != 8)
1446 return false;
1447
1448 // Upper quadword copied in order.
1449 for (unsigned i = 4; i != 8; ++i) {
1450 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1451 "Invalid VECTOR_SHUFFLE mask!");
1452 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() != i)
1453 return false;
1454 }
1455
1456 // Lower quadword shuffled.
1457 for (unsigned i = 0; i != 4; ++i) {
1458 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1459 "Invalid VECTOR_SHUFFLE mask!");
1460 unsigned Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1461 if (Val > 4)
1462 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001463 }
1464
1465 return true;
1466}
1467
Evan Cheng14aed5e2006-03-24 01:18:28 +00001468/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1469/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1470bool X86::isSHUFPMask(SDNode *N) {
1471 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1472
Evan Chengbc4832b2006-03-24 23:15:12 +00001473 unsigned NumElems = N->getNumOperands();
1474 if (NumElems == 2) {
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001475 // The only case that ought be handled by SHUFPD is
1476 // Dest { 2, 1 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
1477 // Expect bit 0 == 1, bit1 == 2
1478 SDOperand Bit0 = N->getOperand(0);
1479 SDOperand Bit1 = N->getOperand(1);
1480 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1481 "Invalid VECTOR_SHUFFLE mask!");
1482 return (cast<ConstantSDNode>(Bit0)->getValue() == 1 &&
1483 cast<ConstantSDNode>(Bit1)->getValue() == 2);
1484 }
1485
Evan Chengbc4832b2006-03-24 23:15:12 +00001486 if (NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001487
1488 // Each half must refer to only one of the vector.
Evan Cheng7d9061e2006-03-30 19:54:57 +00001489 for (unsigned i = 0; i < 2; ++i) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001490 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1491 "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng7d9061e2006-03-30 19:54:57 +00001492 unsigned Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1493 if (Val >= 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001494 }
Evan Cheng7d9061e2006-03-30 19:54:57 +00001495 for (unsigned i = 2; i < 4; ++i) {
Evan Cheng14aed5e2006-03-24 01:18:28 +00001496 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1497 "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng7d9061e2006-03-30 19:54:57 +00001498 unsigned Val = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1499 if (Val < 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001500 }
1501
1502 return true;
1503}
1504
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001505/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1506/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1507bool X86::isMOVHLPSMask(SDNode *N) {
1508 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1509
Evan Cheng2064a2b2006-03-28 06:50:32 +00001510 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001511 return false;
1512
Evan Cheng2064a2b2006-03-28 06:50:32 +00001513 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001514 SDOperand Bit0 = N->getOperand(0);
1515 SDOperand Bit1 = N->getOperand(1);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001516 SDOperand Bit2 = N->getOperand(2);
1517 SDOperand Bit3 = N->getOperand(3);
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001518 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
Evan Cheng2064a2b2006-03-28 06:50:32 +00001519 isa<ConstantSDNode>(Bit2) && isa<ConstantSDNode>(Bit3) &&
1520 "Invalid VECTOR_SHUFFLE mask!");
1521 return (cast<ConstantSDNode>(Bit0)->getValue() == 6 &&
1522 cast<ConstantSDNode>(Bit1)->getValue() == 7 &&
1523 cast<ConstantSDNode>(Bit2)->getValue() == 2 &&
1524 cast<ConstantSDNode>(Bit3)->getValue() == 3);
1525}
1526
1527/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
1528/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1529bool X86::isMOVLHPSMask(SDNode *N) {
1530 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1531
1532 if (N->getNumOperands() != 4)
1533 return false;
1534
1535 // Expect bit0 == 0, bit1 == 1, bit2 == 4, bit3 == 5
1536 SDOperand Bit0 = N->getOperand(0);
1537 SDOperand Bit1 = N->getOperand(1);
1538 SDOperand Bit2 = N->getOperand(2);
1539 SDOperand Bit3 = N->getOperand(3);
1540 assert(isa<ConstantSDNode>(Bit0) && isa<ConstantSDNode>(Bit1) &&
1541 isa<ConstantSDNode>(Bit2) && isa<ConstantSDNode>(Bit3) &&
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001542 "Invalid VECTOR_SHUFFLE mask!");
1543 return (cast<ConstantSDNode>(Bit0)->getValue() == 0 &&
Evan Cheng2064a2b2006-03-28 06:50:32 +00001544 cast<ConstantSDNode>(Bit1)->getValue() == 1 &&
1545 cast<ConstantSDNode>(Bit2)->getValue() == 4 &&
1546 cast<ConstantSDNode>(Bit3)->getValue() == 5);
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001547}
1548
Evan Cheng0038e592006-03-28 00:39:58 +00001549/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1550/// specifies a shuffle of elements that is suitable for input to UNPCKL.
1551bool X86::isUNPCKLMask(SDNode *N) {
1552 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1553
1554 unsigned NumElems = N->getNumOperands();
1555 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1556 return false;
1557
1558 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1559 SDOperand BitI = N->getOperand(i);
1560 SDOperand BitI1 = N->getOperand(i+1);
1561 assert(isa<ConstantSDNode>(BitI) && isa<ConstantSDNode>(BitI1) &&
1562 "Invalid VECTOR_SHUFFLE mask!");
1563 if (cast<ConstantSDNode>(BitI)->getValue() != j)
1564 return false;
1565 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems)
1566 return false;
1567 }
1568
1569 return true;
1570}
1571
Evan Cheng4fcb9222006-03-28 02:43:26 +00001572/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1573/// specifies a shuffle of elements that is suitable for input to UNPCKH.
1574bool X86::isUNPCKHMask(SDNode *N) {
1575 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1576
1577 unsigned NumElems = N->getNumOperands();
1578 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1579 return false;
1580
1581 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1582 SDOperand BitI = N->getOperand(i);
1583 SDOperand BitI1 = N->getOperand(i+1);
1584 assert(isa<ConstantSDNode>(BitI) && isa<ConstantSDNode>(BitI1) &&
1585 "Invalid VECTOR_SHUFFLE mask!");
1586 if (cast<ConstantSDNode>(BitI)->getValue() != j + NumElems/2)
1587 return false;
1588 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems/2 + NumElems)
1589 return false;
1590 }
1591
1592 return true;
1593}
1594
Evan Chengb9df0ca2006-03-22 02:53:00 +00001595/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1596/// a splat of a single element.
1597bool X86::isSplatMask(SDNode *N) {
1598 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1599
1600 // We can only splat 64-bit, and 32-bit quantities.
1601 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1602 return false;
1603
1604 // This is a splat operation if each element of the permute is the same, and
1605 // if the value doesn't reference the second vector.
1606 SDOperand Elt = N->getOperand(0);
1607 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1608 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1609 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1610 "Invalid VECTOR_SHUFFLE mask!");
1611 if (N->getOperand(i) != Elt) return false;
1612 }
1613
1614 // Make sure it is a splat of the first vector operand.
1615 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1616}
1617
Evan Cheng63d33002006-03-22 08:01:21 +00001618/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1619/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1620/// instructions.
1621unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001622 unsigned NumOperands = N->getNumOperands();
1623 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1624 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001625 for (unsigned i = 0; i < NumOperands; ++i) {
1626 unsigned Val
1627 = cast<ConstantSDNode>(N->getOperand(NumOperands-i-1))->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00001628 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00001629 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00001630 if (i != NumOperands - 1)
1631 Mask <<= Shift;
1632 }
Evan Cheng63d33002006-03-22 08:01:21 +00001633
1634 return Mask;
1635}
1636
Evan Cheng506d3df2006-03-29 23:07:14 +00001637/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1638/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1639/// instructions.
1640unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1641 unsigned Mask = 0;
1642 // 8 nodes, but we only care about the last 4.
1643 for (unsigned i = 7; i >= 4; --i) {
1644 unsigned Val
1645 = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1646 Mask |= (Val - 4);
1647 if (i != 4)
1648 Mask <<= 2;
1649 }
1650
1651 return Mask;
1652}
1653
1654/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1655/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1656/// instructions.
1657unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1658 unsigned Mask = 0;
1659 // 8 nodes, but we only care about the first 4.
1660 for (int i = 3; i >= 0; --i) {
1661 unsigned Val
1662 = cast<ConstantSDNode>(N->getOperand(i))->getValue();
1663 Mask |= Val;
1664 if (i != 0)
1665 Mask <<= 2;
1666 }
1667
1668 return Mask;
1669}
1670
Evan Cheng4f563382006-03-29 01:30:51 +00001671/// NormalizeVectorShuffle - Swap vector_shuffle operands (as well as
1672/// values in ther permute mask if needed. Use V1 as second vector if it is
1673/// undef. Return an empty SDOperand is it is already well formed.
1674static SDOperand NormalizeVectorShuffle(SDOperand V1, SDOperand V2,
1675 SDOperand Mask, MVT::ValueType VT,
1676 SelectionDAG &DAG) {
Evan Cheng2064a2b2006-03-28 06:50:32 +00001677 unsigned NumElems = Mask.getNumOperands();
1678 SDOperand Half1 = Mask.getOperand(0);
1679 SDOperand Half2 = Mask.getOperand(NumElems/2);
Evan Cheng4f563382006-03-29 01:30:51 +00001680 bool V2Undef = false;
1681 if (V2.getOpcode() == ISD::UNDEF) {
1682 V2Undef = true;
1683 V2 = V1;
1684 }
1685
Evan Cheng2064a2b2006-03-28 06:50:32 +00001686 if (cast<ConstantSDNode>(Half1)->getValue() >= NumElems &&
1687 cast<ConstantSDNode>(Half2)->getValue() < NumElems) {
1688 // Swap the operands and change mask.
1689 std::vector<SDOperand> MaskVec;
1690 for (unsigned i = NumElems / 2; i != NumElems; ++i)
1691 MaskVec.push_back(Mask.getOperand(i));
1692 for (unsigned i = 0; i != NumElems / 2; ++i)
1693 MaskVec.push_back(Mask.getOperand(i));
1694 Mask =
1695 DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
1696 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1697 }
Evan Cheng4f563382006-03-29 01:30:51 +00001698
1699 if (V2Undef)
1700 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
1701
Evan Cheng2064a2b2006-03-28 06:50:32 +00001702 return SDOperand();
1703}
1704
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001705/// LowerOperation - Provide custom lowering hooks for some operations.
1706///
1707SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1708 switch (Op.getOpcode()) {
1709 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001710 case ISD::SHL_PARTS:
1711 case ISD::SRA_PARTS:
1712 case ISD::SRL_PARTS: {
1713 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1714 "Not an i64 shift!");
1715 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1716 SDOperand ShOpLo = Op.getOperand(0);
1717 SDOperand ShOpHi = Op.getOperand(1);
1718 SDOperand ShAmt = Op.getOperand(2);
1719 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001720 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001721 : DAG.getConstant(0, MVT::i32);
1722
1723 SDOperand Tmp2, Tmp3;
1724 if (Op.getOpcode() == ISD::SHL_PARTS) {
1725 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1726 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1727 } else {
1728 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001729 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001730 }
1731
1732 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1733 ShAmt, DAG.getConstant(32, MVT::i8));
1734
1735 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001736 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001737
1738 std::vector<MVT::ValueType> Tys;
1739 Tys.push_back(MVT::i32);
1740 Tys.push_back(MVT::Flag);
1741 std::vector<SDOperand> Ops;
1742 if (Op.getOpcode() == ISD::SHL_PARTS) {
1743 Ops.push_back(Tmp2);
1744 Ops.push_back(Tmp3);
1745 Ops.push_back(CC);
1746 Ops.push_back(InFlag);
1747 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1748 InFlag = Hi.getValue(1);
1749
1750 Ops.clear();
1751 Ops.push_back(Tmp3);
1752 Ops.push_back(Tmp1);
1753 Ops.push_back(CC);
1754 Ops.push_back(InFlag);
1755 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1756 } else {
1757 Ops.push_back(Tmp2);
1758 Ops.push_back(Tmp3);
1759 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001760 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001761 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1762 InFlag = Lo.getValue(1);
1763
1764 Ops.clear();
1765 Ops.push_back(Tmp3);
1766 Ops.push_back(Tmp1);
1767 Ops.push_back(CC);
1768 Ops.push_back(InFlag);
1769 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1770 }
1771
1772 Tys.clear();
1773 Tys.push_back(MVT::i32);
1774 Tys.push_back(MVT::i32);
1775 Ops.clear();
1776 Ops.push_back(Lo);
1777 Ops.push_back(Hi);
1778 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1779 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001780 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001781 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001782 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001783 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001784
1785 SDOperand Result;
1786 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1787 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001788 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001789 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001790 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001791 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1792 DAG.getEntryNode(), Op.getOperand(0),
1793 StackSlot, DAG.getSrcValue(NULL));
1794
1795 // Build the FILD
1796 std::vector<MVT::ValueType> Tys;
1797 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001798 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001799 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001800 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001801 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001802 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001803 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001804 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1805 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001806
1807 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001808 Chain = Result.getValue(1);
1809 SDOperand InFlag = Result.getValue(2);
1810
Evan Chenge3de85b2006-02-04 02:20:30 +00001811 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001812 // shouldn't be necessary except that RFP cannot be live across
1813 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1814 MachineFunction &MF = DAG.getMachineFunction();
1815 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1816 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1817 std::vector<MVT::ValueType> Tys;
1818 Tys.push_back(MVT::Other);
1819 std::vector<SDOperand> Ops;
1820 Ops.push_back(Chain);
1821 Ops.push_back(Result);
1822 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001823 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001824 Ops.push_back(InFlag);
1825 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1826 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1827 DAG.getSrcValue(NULL));
1828 }
1829
Evan Chenga3195e82006-01-12 22:54:21 +00001830 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001831 }
1832 case ISD::FP_TO_SINT: {
1833 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001834 "Unknown FP_TO_SINT to lower!");
1835 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1836 // stack slot.
1837 MachineFunction &MF = DAG.getMachineFunction();
1838 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1839 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1840 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1841
1842 unsigned Opc;
1843 switch (Op.getValueType()) {
1844 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1845 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1846 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1847 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1848 }
1849
Evan Cheng6dab0532006-01-30 08:02:57 +00001850 SDOperand Chain = DAG.getEntryNode();
1851 SDOperand Value = Op.getOperand(0);
1852 if (X86ScalarSSE) {
1853 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1854 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1855 DAG.getSrcValue(0));
1856 std::vector<MVT::ValueType> Tys;
1857 Tys.push_back(MVT::f64);
1858 Tys.push_back(MVT::Other);
1859 std::vector<SDOperand> Ops;
1860 Ops.push_back(Chain);
1861 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001862 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001863 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1864 Chain = Value.getValue(1);
1865 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1866 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1867 }
1868
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001869 // Build the FP_TO_INT*_IN_MEM
1870 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001871 Ops.push_back(Chain);
1872 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001873 Ops.push_back(StackSlot);
1874 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1875
1876 // Load the result.
1877 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1878 DAG.getSrcValue(NULL));
1879 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001880 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001881 std::vector<MVT::ValueType> Tys;
1882 Tys.push_back(MVT::Other);
1883 Tys.push_back(MVT::Flag);
1884 std::vector<SDOperand> Ops;
1885 Ops.push_back(Op.getOperand(0));
1886 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001887 Ops.clear();
1888 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1889 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1890 MVT::i32, Ops[0].getValue(2)));
1891 Ops.push_back(Ops[1].getValue(1));
1892 Tys[0] = Tys[1] = MVT::i32;
1893 Tys.push_back(MVT::Other);
1894 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001895 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001896 case ISD::FABS: {
1897 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001898 const Type *OpNTy = MVT::getTypeForValueType(VT);
1899 std::vector<Constant*> CV;
1900 if (VT == MVT::f64) {
1901 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1902 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1903 } else {
1904 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1905 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1906 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1907 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1908 }
1909 Constant *CS = ConstantStruct::get(CV);
1910 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1911 SDOperand Mask
1912 = DAG.getNode(X86ISD::LOAD_PACK,
1913 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001914 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1915 }
Evan Cheng223547a2006-01-31 22:28:30 +00001916 case ISD::FNEG: {
1917 MVT::ValueType VT = Op.getValueType();
1918 const Type *OpNTy = MVT::getTypeForValueType(VT);
1919 std::vector<Constant*> CV;
1920 if (VT == MVT::f64) {
1921 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1922 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1923 } else {
1924 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1925 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1926 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1927 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1928 }
1929 Constant *CS = ConstantStruct::get(CV);
1930 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1931 SDOperand Mask
1932 = DAG.getNode(X86ISD::LOAD_PACK,
1933 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1934 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1935 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001936 case ISD::SETCC: {
1937 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001938 SDOperand Cond;
1939 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001940 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1941 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001942 bool Flip;
1943 unsigned X86CC;
1944 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1945 if (Flip)
1946 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1947 Op.getOperand(1), Op.getOperand(0));
1948 else
1949 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1950 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001951 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1952 DAG.getConstant(X86CC, MVT::i8), Cond);
1953 } else {
1954 assert(isFP && "Illegal integer SetCC!");
1955
Evan Cheng6dfa9992006-01-30 23:41:35 +00001956 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1957 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001958 std::vector<MVT::ValueType> Tys;
1959 std::vector<SDOperand> Ops;
1960 switch (SetCCOpcode) {
1961 default: assert(false && "Illegal floating point SetCC!");
1962 case ISD::SETOEQ: { // !PF & ZF
1963 Tys.push_back(MVT::i8);
1964 Tys.push_back(MVT::Flag);
1965 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1966 Ops.push_back(Cond);
1967 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1968 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1969 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1970 Tmp1.getValue(1));
1971 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1972 }
Evan Chengd9558e02006-01-06 00:43:03 +00001973 case ISD::SETUNE: { // PF | !ZF
1974 Tys.push_back(MVT::i8);
1975 Tys.push_back(MVT::Flag);
1976 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1977 Ops.push_back(Cond);
1978 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1979 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1980 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1981 Tmp1.getValue(1));
1982 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1983 }
1984 }
1985 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001986 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001987 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001988 MVT::ValueType VT = Op.getValueType();
1989 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001990 bool isFPStack = isFP && !X86ScalarSSE;
1991 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001992 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001993 SDOperand Op0 = Op.getOperand(0);
1994 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001995 if (Op0.getOpcode() == ISD::SETCC)
1996 Op0 = LowerOperation(Op0, DAG);
1997
Evan Chengaaca22c2006-01-10 20:26:56 +00001998 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001999 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2000 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2001 // have another use it will be eliminated.
2002 // If the X86ISD::SETCC has more than one use, then it's probably better
2003 // to use a test instead of duplicating the X86ISD::CMP (for register
2004 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00002005 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
2006 if (!Op0.hasOneUse()) {
2007 std::vector<MVT::ValueType> Tys;
2008 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2009 Tys.push_back(Op0.Val->getValueType(i));
2010 std::vector<SDOperand> Ops;
2011 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2012 Ops.push_back(Op0.getOperand(i));
2013 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2014 }
2015
Evan Cheng1bcee362006-01-13 01:03:02 +00002016 CC = Op0.getOperand(0);
2017 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00002018 // Make a copy as flag result cannot be used by more than one.
2019 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2020 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00002021 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00002022 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00002023 } else
2024 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00002025 } else
2026 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00002027
Evan Cheng189d01e2006-01-13 01:06:49 +00002028 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00002029 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00002030 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00002031 }
Evan Chenge3413162006-01-09 18:33:28 +00002032
2033 std::vector<MVT::ValueType> Tys;
2034 Tys.push_back(Op.getValueType());
2035 Tys.push_back(MVT::Flag);
2036 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00002037 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2038 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00002039 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00002040 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00002041 Ops.push_back(CC);
2042 Ops.push_back(Cond);
2043 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00002044 }
Evan Cheng898101c2005-12-19 23:12:38 +00002045 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00002046 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00002047 SDOperand Cond = Op.getOperand(1);
2048 SDOperand Dest = Op.getOperand(2);
2049 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00002050 if (Cond.getOpcode() == ISD::SETCC)
2051 Cond = LowerOperation(Cond, DAG);
2052
Evan Chengd5781fc2005-12-21 20:21:51 +00002053 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00002054 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2055 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2056 // have another use it will be eliminated.
2057 // If the X86ISD::SETCC has more than one use, then it's probably better
2058 // to use a test instead of duplicating the X86ISD::CMP (for register
2059 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00002060 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
2061 if (!Cond.hasOneUse()) {
2062 std::vector<MVT::ValueType> Tys;
2063 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2064 Tys.push_back(Cond.Val->getValueType(i));
2065 std::vector<SDOperand> Ops;
2066 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2067 Ops.push_back(Cond.getOperand(i));
2068 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2069 }
2070
Evan Cheng1bcee362006-01-13 01:03:02 +00002071 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00002072 Cond = Cond.getOperand(1);
2073 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00002074 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00002075 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00002076 } else
2077 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00002078 } else
2079 addTest = true;
2080
2081 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00002082 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00002083 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2084 }
2085 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2086 Op.getOperand(0), Op.getOperand(2), CC, Cond);
2087 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002088 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002089 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002090 SDOperand Chain = Op.getOperand(0);
2091 unsigned Align =
2092 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2093 if (Align == 0) Align = 1;
2094
Evan Cheng18a84522006-02-16 00:21:07 +00002095 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2096 // If not DWORD aligned, call memset if size is less than the threshold.
2097 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002098 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002099 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002100 MVT::ValueType IntPtr = getPointerTy();
2101 const Type *IntPtrTy = getTargetData().getIntPtrType();
2102 std::vector<std::pair<SDOperand, const Type*> > Args;
2103 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2104 // Extend the ubyte argument to be an int value for the call.
2105 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2106 Args.push_back(std::make_pair(Val, IntPtrTy));
2107 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2108 std::pair<SDOperand,SDOperand> CallResult =
2109 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2110 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2111 return CallResult.second;
2112 }
2113
Evan Cheng67f92a72006-01-11 22:15:48 +00002114 MVT::ValueType AVT;
2115 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002116 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2117 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002118 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002119 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00002120 unsigned ValReg;
2121 unsigned Val = ValC->getValue() & 255;
2122
2123 // If the value is a constant, then we can potentially use larger sets.
2124 switch (Align & 3) {
2125 case 2: // WORD aligned
2126 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002127 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2128 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002129 Val = (Val << 8) | Val;
2130 ValReg = X86::AX;
2131 break;
2132 case 0: // DWORD aligned
2133 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002134 if (I) {
2135 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2136 BytesLeft = I->getValue() % 4;
2137 } else {
2138 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2139 DAG.getConstant(2, MVT::i8));
2140 TwoRepStos = true;
2141 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002142 Val = (Val << 8) | Val;
2143 Val = (Val << 16) | Val;
2144 ValReg = X86::EAX;
2145 break;
2146 default: // Byte aligned
2147 AVT = MVT::i8;
2148 Count = Op.getOperand(3);
2149 ValReg = X86::AL;
2150 break;
2151 }
2152
2153 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2154 InFlag);
2155 InFlag = Chain.getValue(1);
2156 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00002157 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00002158 Count = Op.getOperand(3);
2159 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2160 InFlag = Chain.getValue(1);
2161 }
2162
2163 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2164 InFlag = Chain.getValue(1);
2165 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2166 InFlag = Chain.getValue(1);
2167
Evan Chengff909922006-03-07 23:29:39 +00002168 std::vector<MVT::ValueType> Tys;
2169 Tys.push_back(MVT::Other);
2170 Tys.push_back(MVT::Flag);
2171 std::vector<SDOperand> Ops;
2172 Ops.push_back(Chain);
2173 Ops.push_back(DAG.getValueType(AVT));
2174 Ops.push_back(InFlag);
2175 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2176
2177 if (TwoRepStos) {
2178 InFlag = Chain.getValue(1);
2179 Count = Op.getOperand(3);
2180 MVT::ValueType CVT = Count.getValueType();
2181 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2182 DAG.getConstant(3, CVT));
2183 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2184 InFlag = Chain.getValue(1);
2185 Tys.clear();
2186 Tys.push_back(MVT::Other);
2187 Tys.push_back(MVT::Flag);
2188 Ops.clear();
2189 Ops.push_back(Chain);
2190 Ops.push_back(DAG.getValueType(MVT::i8));
2191 Ops.push_back(InFlag);
2192 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2193 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002194 // Issue stores for the last 1 - 3 bytes.
2195 SDOperand Value;
2196 unsigned Val = ValC->getValue() & 255;
2197 unsigned Offset = I->getValue() - BytesLeft;
2198 SDOperand DstAddr = Op.getOperand(1);
2199 MVT::ValueType AddrVT = DstAddr.getValueType();
2200 if (BytesLeft >= 2) {
2201 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2202 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2203 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2204 DAG.getConstant(Offset, AddrVT)),
2205 DAG.getSrcValue(NULL));
2206 BytesLeft -= 2;
2207 Offset += 2;
2208 }
2209
2210 if (BytesLeft == 1) {
2211 Value = DAG.getConstant(Val, MVT::i8);
2212 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2213 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2214 DAG.getConstant(Offset, AddrVT)),
2215 DAG.getSrcValue(NULL));
2216 }
2217 }
2218
2219 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002220 }
2221 case ISD::MEMCPY: {
2222 SDOperand Chain = Op.getOperand(0);
2223 unsigned Align =
2224 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2225 if (Align == 0) Align = 1;
2226
Evan Cheng18a84522006-02-16 00:21:07 +00002227 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2228 // If not DWORD aligned, call memcpy if size is less than the threshold.
2229 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00002230 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00002231 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00002232 MVT::ValueType IntPtr = getPointerTy();
2233 const Type *IntPtrTy = getTargetData().getIntPtrType();
2234 std::vector<std::pair<SDOperand, const Type*> > Args;
2235 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2236 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2237 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2238 std::pair<SDOperand,SDOperand> CallResult =
2239 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2240 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2241 return CallResult.second;
2242 }
2243
Evan Cheng67f92a72006-01-11 22:15:48 +00002244 MVT::ValueType AVT;
2245 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002246 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00002247 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00002248 switch (Align & 3) {
2249 case 2: // WORD aligned
2250 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00002251 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2252 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00002253 break;
2254 case 0: // DWORD aligned
2255 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00002256 if (I) {
2257 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2258 BytesLeft = I->getValue() % 4;
2259 } else {
2260 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2261 DAG.getConstant(2, MVT::i8));
2262 TwoRepMovs = true;
2263 }
Evan Cheng67f92a72006-01-11 22:15:48 +00002264 break;
2265 default: // Byte aligned
2266 AVT = MVT::i8;
2267 Count = Op.getOperand(3);
2268 break;
2269 }
2270
Evan Cheng62bec2c2006-03-04 02:48:56 +00002271 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00002272 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2273 InFlag = Chain.getValue(1);
2274 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2275 InFlag = Chain.getValue(1);
2276 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2277 InFlag = Chain.getValue(1);
2278
Evan Chengff909922006-03-07 23:29:39 +00002279 std::vector<MVT::ValueType> Tys;
2280 Tys.push_back(MVT::Other);
2281 Tys.push_back(MVT::Flag);
2282 std::vector<SDOperand> Ops;
2283 Ops.push_back(Chain);
2284 Ops.push_back(DAG.getValueType(AVT));
2285 Ops.push_back(InFlag);
2286 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2287
2288 if (TwoRepMovs) {
2289 InFlag = Chain.getValue(1);
2290 Count = Op.getOperand(3);
2291 MVT::ValueType CVT = Count.getValueType();
2292 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2293 DAG.getConstant(3, CVT));
2294 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2295 InFlag = Chain.getValue(1);
2296 Tys.clear();
2297 Tys.push_back(MVT::Other);
2298 Tys.push_back(MVT::Flag);
2299 Ops.clear();
2300 Ops.push_back(Chain);
2301 Ops.push_back(DAG.getValueType(MVT::i8));
2302 Ops.push_back(InFlag);
2303 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2304 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002305 // Issue loads and stores for the last 1 - 3 bytes.
2306 unsigned Offset = I->getValue() - BytesLeft;
2307 SDOperand DstAddr = Op.getOperand(1);
2308 MVT::ValueType DstVT = DstAddr.getValueType();
2309 SDOperand SrcAddr = Op.getOperand(2);
2310 MVT::ValueType SrcVT = SrcAddr.getValueType();
2311 SDOperand Value;
2312 if (BytesLeft >= 2) {
2313 Value = DAG.getLoad(MVT::i16, Chain,
2314 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2315 DAG.getConstant(Offset, SrcVT)),
2316 DAG.getSrcValue(NULL));
2317 Chain = Value.getValue(1);
2318 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2319 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2320 DAG.getConstant(Offset, DstVT)),
2321 DAG.getSrcValue(NULL));
2322 BytesLeft -= 2;
2323 Offset += 2;
2324 }
2325
2326 if (BytesLeft == 1) {
2327 Value = DAG.getLoad(MVT::i8, Chain,
2328 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2329 DAG.getConstant(Offset, SrcVT)),
2330 DAG.getSrcValue(NULL));
2331 Chain = Value.getValue(1);
2332 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2333 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2334 DAG.getConstant(Offset, DstVT)),
2335 DAG.getSrcValue(NULL));
2336 }
2337 }
2338
2339 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002340 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002341
2342 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2343 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2344 // one of the above mentioned nodes. It has to be wrapped because otherwise
2345 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2346 // be used to form addressing mode. These wrapped nodes will be selected
2347 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002348 case ISD::ConstantPool: {
2349 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002350 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2351 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2352 CP->getAlignment()));
Evan Chenga88973f2006-03-22 19:22:18 +00002353 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002354 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002355 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002356 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2357 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2358 }
2359
2360 return Result;
2361 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002362 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002363 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2364 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2365 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002366 if (Subtarget->isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002367 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002368 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002369 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2370 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002371
2372 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002373 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002374 // the GlobalAddress must be in the base or index register of the address,
2375 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002376 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002377 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002378 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002379 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002380 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002381
Evan Cheng002fe9b2006-01-12 07:56:47 +00002382 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002383 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002384 case ISD::ExternalSymbol: {
2385 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2386 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2387 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chenga88973f2006-03-22 19:22:18 +00002388 if (Subtarget->isTargetDarwin()) {
Evan Cheng020d2e82006-02-23 20:41:18 +00002389 // With PIC, the address is actually $g + Offset.
2390 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2391 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2392 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2393 }
2394
2395 return Result;
2396 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002397 case ISD::VASTART: {
2398 // vastart just stores the address of the VarArgsFrameIndex slot into the
2399 // memory location argument.
2400 // FIXME: Replace MVT::i32 with PointerTy
2401 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2402 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2403 Op.getOperand(1), Op.getOperand(2));
2404 }
Nate Begemanee625572006-01-27 21:09:22 +00002405 case ISD::RET: {
2406 SDOperand Copy;
2407
2408 switch(Op.getNumOperands()) {
2409 default:
2410 assert(0 && "Do not know how to return this many arguments!");
2411 abort();
2412 case 1:
2413 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2414 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2415 case 2: {
2416 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2417 if (MVT::isInteger(ArgVT))
2418 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2419 SDOperand());
2420 else if (!X86ScalarSSE) {
2421 std::vector<MVT::ValueType> Tys;
2422 Tys.push_back(MVT::Other);
2423 Tys.push_back(MVT::Flag);
2424 std::vector<SDOperand> Ops;
2425 Ops.push_back(Op.getOperand(0));
2426 Ops.push_back(Op.getOperand(1));
2427 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2428 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002429 SDOperand MemLoc;
2430 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002431 SDOperand Value = Op.getOperand(1);
2432
Evan Cheng760df292006-02-01 01:19:32 +00002433 if (Value.getOpcode() == ISD::LOAD &&
2434 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002435 Chain = Value.getOperand(0);
2436 MemLoc = Value.getOperand(1);
2437 } else {
2438 // Spill the value to memory and reload it into top of stack.
2439 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2440 MachineFunction &MF = DAG.getMachineFunction();
2441 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2442 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2443 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2444 Value, MemLoc, DAG.getSrcValue(0));
2445 }
Nate Begemanee625572006-01-27 21:09:22 +00002446 std::vector<MVT::ValueType> Tys;
2447 Tys.push_back(MVT::f64);
2448 Tys.push_back(MVT::Other);
2449 std::vector<SDOperand> Ops;
2450 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002451 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002452 Ops.push_back(DAG.getValueType(ArgVT));
2453 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2454 Tys.clear();
2455 Tys.push_back(MVT::Other);
2456 Tys.push_back(MVT::Flag);
2457 Ops.clear();
2458 Ops.push_back(Copy.getValue(1));
2459 Ops.push_back(Copy);
2460 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2461 }
2462 break;
2463 }
2464 case 3:
2465 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2466 SDOperand());
2467 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2468 break;
2469 }
2470 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2471 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2472 Copy.getValue(1));
2473 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002474 case ISD::SCALAR_TO_VECTOR: {
2475 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chengbc4832b2006-03-24 23:15:12 +00002476 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Cheng48090aa2006-03-21 23:01:21 +00002477 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002478 case ISD::VECTOR_SHUFFLE: {
2479 SDOperand V1 = Op.getOperand(0);
2480 SDOperand V2 = Op.getOperand(1);
2481 SDOperand PermMask = Op.getOperand(2);
2482 MVT::ValueType VT = Op.getValueType();
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002483 unsigned NumElems = PermMask.getNumOperands();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002484
Evan Cheng691c9232006-03-29 19:02:40 +00002485 // Splat && PSHUFD's 2nd vector must be undef.
Evan Cheng7d9061e2006-03-30 19:54:57 +00002486 if (X86::isSplatMask(PermMask.Val)) {
Evan Cheng475aecf2006-03-29 03:04:49 +00002487 if (V2.getOpcode() != ISD::UNDEF)
Evan Cheng4f563382006-03-29 01:30:51 +00002488 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
Evan Cheng475aecf2006-03-29 03:04:49 +00002489 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2490 return SDOperand();
2491 }
Evan Cheng4f563382006-03-29 01:30:51 +00002492
Evan Cheng691c9232006-03-29 19:02:40 +00002493 if (X86::isUNPCKLMask(PermMask.Val) ||
2494 X86::isUNPCKHMask(PermMask.Val))
2495 // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
2496 return SDOperand();
2497
Evan Cheng7d9061e2006-03-30 19:54:57 +00002498 if (NumElems == 2)
Evan Cheng4f563382006-03-29 01:30:51 +00002499 return NormalizeVectorShuffle(V1, V2, PermMask, VT, DAG);
Evan Cheng7d9061e2006-03-30 19:54:57 +00002500
2501 // If VT is integer, try PSHUF* first, then SHUFP*.
2502 if (MVT::isInteger(VT)) {
2503 if (X86::isPSHUFDMask(PermMask.Val) ||
2504 X86::isPSHUFHWMask(PermMask.Val) ||
2505 X86::isPSHUFLWMask(PermMask.Val)) {
2506 if (V2.getOpcode() != ISD::UNDEF)
2507 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2508 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2509 return SDOperand();
2510 }
2511
2512 if (X86::isSHUFPMask(PermMask.Val))
2513 return NormalizeVectorShuffle(V1, V2, PermMask, VT, DAG);
2514 } else {
2515 // Floating point cases in the other order.
2516 if (X86::isSHUFPMask(PermMask.Val))
2517 return NormalizeVectorShuffle(V1, V2, PermMask, VT, DAG);
2518 if (X86::isPSHUFDMask(PermMask.Val) ||
2519 X86::isPSHUFHWMask(PermMask.Val) ||
2520 X86::isPSHUFLWMask(PermMask.Val)) {
2521 if (V2.getOpcode() != ISD::UNDEF)
2522 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2523 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2524 return SDOperand();
2525 }
Evan Cheng4f563382006-03-29 01:30:51 +00002526 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002527
Evan Cheng386031a2006-03-24 07:29:27 +00002528 assert(0 && "Unexpected VECTOR_SHUFFLE to lower");
Chris Lattner6df11542006-03-22 04:18:34 +00002529 abort();
Evan Chengb9df0ca2006-03-22 02:53:00 +00002530 }
Evan Cheng386031a2006-03-24 07:29:27 +00002531 case ISD::BUILD_VECTOR: {
Evan Chenga0b3afb2006-03-27 07:00:16 +00002532 // All one's are handled with pcmpeqd.
2533 if (ISD::isBuildVectorAllOnes(Op.Val))
2534 return Op;
2535
Evan Chengc60bd972006-03-25 09:37:23 +00002536 std::set<SDOperand> Values;
Evan Chengbc4832b2006-03-24 23:15:12 +00002537 SDOperand Elt0 = Op.getOperand(0);
Evan Chengc60bd972006-03-25 09:37:23 +00002538 Values.insert(Elt0);
Evan Chengbc4832b2006-03-24 23:15:12 +00002539 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2540 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2541 (isa<ConstantFPSDNode>(Elt0) &&
2542 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2543 bool RestAreZero = true;
Evan Cheng386031a2006-03-24 07:29:27 +00002544 unsigned NumElems = Op.getNumOperands();
Evan Chengbc4832b2006-03-24 23:15:12 +00002545 for (unsigned i = 1; i < NumElems; ++i) {
Evan Chengc60bd972006-03-25 09:37:23 +00002546 SDOperand Elt = Op.getOperand(i);
2547 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002548 if (!FPC->isExactlyValue(+0.0))
Evan Chengbc4832b2006-03-24 23:15:12 +00002549 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002550 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Evan Cheng386031a2006-03-24 07:29:27 +00002551 if (!C->isNullValue())
Evan Chengbc4832b2006-03-24 23:15:12 +00002552 RestAreZero = false;
Evan Cheng386031a2006-03-24 07:29:27 +00002553 } else
Evan Chengbc4832b2006-03-24 23:15:12 +00002554 RestAreZero = false;
Evan Chengc60bd972006-03-25 09:37:23 +00002555 Values.insert(Elt);
Evan Cheng386031a2006-03-24 07:29:27 +00002556 }
2557
Evan Chengbc4832b2006-03-24 23:15:12 +00002558 if (RestAreZero) {
2559 if (Elt0IsZero) return Op;
2560
2561 // Zero extend a scalar to a vector.
2562 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2563 }
2564
Evan Chengc60bd972006-03-25 09:37:23 +00002565 if (Values.size() > 2) {
2566 // Expand into a number of unpckl*.
2567 // e.g. for v4f32
2568 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2569 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2570 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2571 MVT::ValueType VT = Op.getValueType();
Evan Cheng0038e592006-03-28 00:39:58 +00002572 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2573 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2574 std::vector<SDOperand> MaskVec;
2575 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2576 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2577 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2578 }
2579 SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Chengc60bd972006-03-25 09:37:23 +00002580 std::vector<SDOperand> V(NumElems);
2581 for (unsigned i = 0; i < NumElems; ++i)
2582 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2583 NumElems >>= 1;
2584 while (NumElems != 0) {
2585 for (unsigned i = 0; i < NumElems; ++i)
Evan Cheng0038e592006-03-28 00:39:58 +00002586 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2587 PermMask);
Evan Chengc60bd972006-03-25 09:37:23 +00002588 NumElems >>= 1;
2589 }
2590 return V[0];
2591 }
2592
Evan Cheng386031a2006-03-24 07:29:27 +00002593 return SDOperand();
2594 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002595 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002596}
Evan Cheng72261582005-12-20 06:22:03 +00002597
2598const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2599 switch (Opcode) {
2600 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002601 case X86ISD::SHLD: return "X86ISD::SHLD";
2602 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002603 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002604 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002605 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002606 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002607 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2608 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2609 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002610 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002611 case X86ISD::FST: return "X86ISD::FST";
2612 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002613 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002614 case X86ISD::CALL: return "X86ISD::CALL";
2615 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2616 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2617 case X86ISD::CMP: return "X86ISD::CMP";
2618 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002619 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002620 case X86ISD::CMOV: return "X86ISD::CMOV";
2621 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002622 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002623 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2624 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002625 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002626 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002627 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00002628 case X86ISD::S2VEC: return "X86ISD::S2VEC";
2629 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Cheng72261582005-12-20 06:22:03 +00002630 }
2631}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002632
Nate Begeman368e18d2006-02-16 21:11:51 +00002633void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2634 uint64_t Mask,
2635 uint64_t &KnownZero,
2636 uint64_t &KnownOne,
2637 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002638
2639 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002640 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002641
2642 switch (Opc) {
2643 default:
2644 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2645 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002646 case X86ISD::SETCC:
2647 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2648 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002649 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002650}
Chris Lattner259e97c2006-01-31 19:43:35 +00002651
2652std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002653getRegClassForInlineAsmConstraint(const std::string &Constraint,
2654 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002655 if (Constraint.size() == 1) {
2656 // FIXME: not handling fp-stack yet!
2657 // FIXME: not handling MMX registers yet ('y' constraint).
2658 switch (Constraint[0]) { // GCC X86 Constraint Letters
2659 default: break; // Unknown constriant letter
2660 case 'r': // GENERAL_REGS
2661 case 'R': // LEGACY_REGS
2662 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2663 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2664 case 'l': // INDEX_REGS
2665 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2666 X86::ESI, X86::EDI, X86::EBP, 0);
2667 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2668 case 'Q': // Q_REGS
2669 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2670 case 'x': // SSE_REGS if SSE1 allowed
2671 if (Subtarget->hasSSE1())
2672 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2673 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2674 0);
2675 return std::vector<unsigned>();
2676 case 'Y': // SSE_REGS if SSE2 allowed
2677 if (Subtarget->hasSSE2())
2678 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2679 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2680 0);
2681 return std::vector<unsigned>();
2682 }
2683 }
2684
Chris Lattner1efa40f2006-02-22 00:56:39 +00002685 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002686}
Evan Cheng30b37b52006-03-13 23:18:16 +00002687
2688/// isLegalAddressImmediate - Return true if the integer value or
2689/// GlobalValue can be used as the offset of the target addressing mode.
2690bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2691 // X86 allows a sign-extended 32-bit immediate field.
2692 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2693}
2694
2695bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chenga88973f2006-03-22 19:22:18 +00002696 if (Subtarget->isTargetDarwin()) {
Evan Cheng30b37b52006-03-13 23:18:16 +00002697 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2698 if (RModel == Reloc::Static)
2699 return true;
2700 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002701 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002702 else
2703 return false;
2704 } else
2705 return true;
2706}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002707
2708/// isShuffleMaskLegal - Targets can use this to indicate that they only
2709/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
2710/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
2711/// are assumed to be legal.
Evan Chengca6e8ea2006-03-22 22:07:06 +00002712bool
2713X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
2714 // Only do shuffles on 128-bit vector types for now.
2715 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002716 return (Mask.Val->getNumOperands() == 2 ||
2717 X86::isSplatMask(Mask.Val) ||
Evan Cheng14aed5e2006-03-24 01:18:28 +00002718 X86::isPSHUFDMask(Mask.Val) ||
Evan Cheng506d3df2006-03-29 23:07:14 +00002719 X86::isPSHUFHWMask(Mask.Val) ||
2720 X86::isPSHUFLWMask(Mask.Val) ||
Evan Cheng0038e592006-03-28 00:39:58 +00002721 X86::isSHUFPMask(Mask.Val) ||
Evan Chenged4ca7f2006-03-28 08:27:15 +00002722 X86::isUNPCKLMask(Mask.Val) ||
Jim Laskey2d2a6132006-03-28 10:17:11 +00002723 X86::isUNPCKHMask(Mask.Val));
Evan Cheng0188ecb2006-03-22 18:59:22 +00002724}