blob: 68189e7d29e9597e09ca3c5f1e77d67b1eec5eeb [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 Chengdf57fa02006-03-17 20:31:41 +000053 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
54 // 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
185 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
186 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 Cheng470a6ad2006-02-22 02:26:30 +0000262 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
263 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
273 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
274 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);
280 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000281 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000282 }
283
284 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
285 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);
293 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
294 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
295 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
296 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Expand);
297 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Expand);
298 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Expand);
299 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
300 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Expand);
301 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
302 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb9df0ca2006-03-22 02:53:00 +0000303 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000304 }
305
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000306 computeRegisterProperties();
307
Evan Cheng87ed7162006-02-14 08:25:08 +0000308 // FIXME: These should be based on subtarget info. Plus, the values should
309 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000310 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
311 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
312 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000313 allowUnalignedMemoryAccesses = true; // x86 supports it!
314}
315
316std::vector<SDOperand>
317X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
318 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
319 return LowerFastCCArguments(F, DAG);
320 return LowerCCCArguments(F, DAG);
321}
322
323std::pair<SDOperand, SDOperand>
324X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
325 bool isVarArg, unsigned CallingConv,
326 bool isTailCall,
327 SDOperand Callee, ArgListTy &Args,
328 SelectionDAG &DAG) {
329 assert((!isVarArg || CallingConv == CallingConv::C) &&
330 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000331
332 // If the callee is a GlobalAddress node (quite common, every direct call is)
333 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
334 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
335 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000336 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
337 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000338
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000339 if (CallingConv == CallingConv::Fast && EnableFastCC)
340 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
341 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
342}
343
344//===----------------------------------------------------------------------===//
345// C Calling Convention implementation
346//===----------------------------------------------------------------------===//
347
348std::vector<SDOperand>
349X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
350 std::vector<SDOperand> ArgValues;
351
352 MachineFunction &MF = DAG.getMachineFunction();
353 MachineFrameInfo *MFI = MF.getFrameInfo();
354
355 // Add DAG nodes to load the arguments... On entry to a function on the X86,
356 // the stack frame looks like this:
357 //
358 // [ESP] -- return address
359 // [ESP + 4] -- first argument (leftmost lexically)
360 // [ESP + 8] -- second argument, if first argument is four bytes in size
361 // ...
362 //
363 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
364 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
365 MVT::ValueType ObjectVT = getValueType(I->getType());
366 unsigned ArgIncrement = 4;
367 unsigned ObjSize;
368 switch (ObjectVT) {
369 default: assert(0 && "Unhandled argument type!");
370 case MVT::i1:
371 case MVT::i8: ObjSize = 1; break;
372 case MVT::i16: ObjSize = 2; break;
373 case MVT::i32: ObjSize = 4; break;
374 case MVT::i64: ObjSize = ArgIncrement = 8; break;
375 case MVT::f32: ObjSize = 4; break;
376 case MVT::f64: ObjSize = ArgIncrement = 8; break;
377 }
378 // Create the frame index object for this incoming parameter...
379 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
380
381 // Create the SelectionDAG nodes corresponding to a load from this parameter
382 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
383
384 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
385 // dead loads.
386 SDOperand ArgValue;
387 if (!I->use_empty())
388 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
389 DAG.getSrcValue(NULL));
390 else {
391 if (MVT::isInteger(ObjectVT))
392 ArgValue = DAG.getConstant(0, ObjectVT);
393 else
394 ArgValue = DAG.getConstantFP(0, ObjectVT);
395 }
396 ArgValues.push_back(ArgValue);
397
398 ArgOffset += ArgIncrement; // Move on to the next argument...
399 }
400
401 // If the function takes variable number of arguments, make a frame index for
402 // the start of the first vararg value... for expansion of llvm.va_start.
403 if (F.isVarArg())
404 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
405 ReturnAddrIndex = 0; // No return address slot generated yet.
406 BytesToPopOnReturn = 0; // Callee pops nothing.
407 BytesCallerReserves = ArgOffset;
408
409 // Finally, inform the code generator which regs we return values in.
410 switch (getValueType(F.getReturnType())) {
411 default: assert(0 && "Unknown type!");
412 case MVT::isVoid: break;
413 case MVT::i1:
414 case MVT::i8:
415 case MVT::i16:
416 case MVT::i32:
417 MF.addLiveOut(X86::EAX);
418 break;
419 case MVT::i64:
420 MF.addLiveOut(X86::EAX);
421 MF.addLiveOut(X86::EDX);
422 break;
423 case MVT::f32:
424 case MVT::f64:
425 MF.addLiveOut(X86::ST0);
426 break;
427 }
428 return ArgValues;
429}
430
431std::pair<SDOperand, SDOperand>
432X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
433 bool isVarArg, bool isTailCall,
434 SDOperand Callee, ArgListTy &Args,
435 SelectionDAG &DAG) {
436 // Count how many bytes are to be pushed on the stack.
437 unsigned NumBytes = 0;
438
439 if (Args.empty()) {
440 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000441 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000442 } else {
443 for (unsigned i = 0, e = Args.size(); i != e; ++i)
444 switch (getValueType(Args[i].second)) {
445 default: assert(0 && "Unknown value type!");
446 case MVT::i1:
447 case MVT::i8:
448 case MVT::i16:
449 case MVT::i32:
450 case MVT::f32:
451 NumBytes += 4;
452 break;
453 case MVT::i64:
454 case MVT::f64:
455 NumBytes += 8;
456 break;
457 }
458
Chris Lattner94dd2922006-02-13 09:00:43 +0000459 Chain = DAG.getCALLSEQ_START(Chain,
460 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461
462 // Arguments go on the stack in reverse order, as specified by the ABI.
463 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000464 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000465 std::vector<SDOperand> Stores;
466
467 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
468 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
469 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
470
471 switch (getValueType(Args[i].second)) {
472 default: assert(0 && "Unexpected ValueType for argument!");
473 case MVT::i1:
474 case MVT::i8:
475 case MVT::i16:
476 // Promote the integer to 32 bits. If the input type is signed use a
477 // sign extend, otherwise use a zero extend.
478 if (Args[i].second->isSigned())
479 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
480 else
481 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
482
483 // FALL THROUGH
484 case MVT::i32:
485 case MVT::f32:
486 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
487 Args[i].first, PtrOff,
488 DAG.getSrcValue(NULL)));
489 ArgOffset += 4;
490 break;
491 case MVT::i64:
492 case MVT::f64:
493 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
494 Args[i].first, PtrOff,
495 DAG.getSrcValue(NULL)));
496 ArgOffset += 8;
497 break;
498 }
499 }
500 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
501 }
502
503 std::vector<MVT::ValueType> RetVals;
504 MVT::ValueType RetTyVT = getValueType(RetTy);
505 RetVals.push_back(MVT::Other);
506
507 // The result values produced have to be legal. Promote the result.
508 switch (RetTyVT) {
509 case MVT::isVoid: break;
510 default:
511 RetVals.push_back(RetTyVT);
512 break;
513 case MVT::i1:
514 case MVT::i8:
515 case MVT::i16:
516 RetVals.push_back(MVT::i32);
517 break;
518 case MVT::f32:
519 if (X86ScalarSSE)
520 RetVals.push_back(MVT::f32);
521 else
522 RetVals.push_back(MVT::f64);
523 break;
524 case MVT::i64:
525 RetVals.push_back(MVT::i32);
526 RetVals.push_back(MVT::i32);
527 break;
528 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000529
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000530 std::vector<MVT::ValueType> NodeTys;
531 NodeTys.push_back(MVT::Other); // Returns a chain
532 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
533 std::vector<SDOperand> Ops;
534 Ops.push_back(Chain);
535 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000536
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000537 // FIXME: Do not generate X86ISD::TAILCALL for now.
538 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
539 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000540
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000541 NodeTys.clear();
542 NodeTys.push_back(MVT::Other); // Returns a chain
543 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
544 Ops.clear();
545 Ops.push_back(Chain);
546 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
547 Ops.push_back(DAG.getConstant(0, getPointerTy()));
548 Ops.push_back(InFlag);
549 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
550 InFlag = Chain.getValue(1);
551
552 SDOperand RetVal;
553 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000554 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000555 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000556 case MVT::i1:
557 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000558 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
559 Chain = RetVal.getValue(1);
560 if (RetTyVT == MVT::i1)
561 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
562 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000563 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000564 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
565 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000566 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000567 case MVT::i32:
568 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
569 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000570 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000571 case MVT::i64: {
572 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
573 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
574 Lo.getValue(2));
575 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
576 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000577 break;
578 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000579 case MVT::f32:
580 case MVT::f64: {
581 std::vector<MVT::ValueType> Tys;
582 Tys.push_back(MVT::f64);
583 Tys.push_back(MVT::Other);
584 Tys.push_back(MVT::Flag);
585 std::vector<SDOperand> Ops;
586 Ops.push_back(Chain);
587 Ops.push_back(InFlag);
588 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
589 Chain = RetVal.getValue(1);
590 InFlag = RetVal.getValue(2);
591 if (X86ScalarSSE) {
592 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
593 // shouldn't be necessary except that RFP cannot be live across
594 // multiple blocks. When stackifier is fixed, they can be uncoupled.
595 MachineFunction &MF = DAG.getMachineFunction();
596 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
597 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
598 Tys.clear();
599 Tys.push_back(MVT::Other);
600 Ops.clear();
601 Ops.push_back(Chain);
602 Ops.push_back(RetVal);
603 Ops.push_back(StackSlot);
604 Ops.push_back(DAG.getValueType(RetTyVT));
605 Ops.push_back(InFlag);
606 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
607 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
608 DAG.getSrcValue(NULL));
609 Chain = RetVal.getValue(1);
610 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000611
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000612 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
613 // FIXME: we would really like to remember that this FP_ROUND
614 // operation is okay to eliminate if we allow excess FP precision.
615 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
616 break;
617 }
618 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000619 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000620
621 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622}
623
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000624//===----------------------------------------------------------------------===//
625// Fast Calling Convention implementation
626//===----------------------------------------------------------------------===//
627//
628// The X86 'fast' calling convention passes up to two integer arguments in
629// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
630// and requires that the callee pop its arguments off the stack (allowing proper
631// tail calls), and has the same return value conventions as C calling convs.
632//
633// This calling convention always arranges for the callee pop value to be 8n+4
634// bytes, which is needed for tail recursion elimination and stack alignment
635// reasons.
636//
637// Note that this can be enhanced in the future to pass fp vals in registers
638// (when we have a global fp allocator) and do other tricks.
639//
640
641/// AddLiveIn - This helper function adds the specified physical register to the
642/// MachineFunction as a live in value. It also creates a corresponding virtual
643/// register for it.
644static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
645 TargetRegisterClass *RC) {
646 assert(RC->contains(PReg) && "Not the correct regclass!");
647 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
648 MF.addLiveIn(PReg, VReg);
649 return VReg;
650}
651
Chris Lattner89fad2c2006-03-17 17:27:47 +0000652// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
653// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
654// EDX". Anything more is illegal.
655//
656// FIXME: The linscan register allocator currently has problem with
657// coallescing. At the time of this writing, whenever it decides to coallesce
658// a physreg with a virtreg, this increases the size of the physreg's live
659// range, and the live range cannot ever be reduced. This causes problems if
660// too many physregs are coalleced with virtregs, which can cause the register
661// allocator to wedge itself.
662//
663// This code triggers this problem more often if we pass args in registers,
664// so disable it until this is fixed.
665//
666// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
667// about code being dead.
668//
669static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000670
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000671
672std::vector<SDOperand>
673X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
674 std::vector<SDOperand> ArgValues;
675
676 MachineFunction &MF = DAG.getMachineFunction();
677 MachineFrameInfo *MFI = MF.getFrameInfo();
678
679 // Add DAG nodes to load the arguments... On entry to a function the stack
680 // frame looks like this:
681 //
682 // [ESP] -- return address
683 // [ESP + 4] -- first nonreg argument (leftmost lexically)
684 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
685 // ...
686 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
687
688 // Keep track of the number of integer regs passed so far. This can be either
689 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
690 // used).
691 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000692
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000693 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
694 MVT::ValueType ObjectVT = getValueType(I->getType());
695 unsigned ArgIncrement = 4;
696 unsigned ObjSize = 0;
697 SDOperand ArgValue;
698
699 switch (ObjectVT) {
700 default: assert(0 && "Unhandled argument type!");
701 case MVT::i1:
702 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000703 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000704 if (!I->use_empty()) {
705 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
706 X86::R8RegisterClass);
707 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
708 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000709 if (ObjectVT == MVT::i1)
710 // FIXME: Should insert a assertzext here.
711 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000712 }
713 ++NumIntRegs;
714 break;
715 }
716
717 ObjSize = 1;
718 break;
719 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000720 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000721 if (!I->use_empty()) {
722 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
723 X86::R16RegisterClass);
724 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
725 DAG.setRoot(ArgValue.getValue(1));
726 }
727 ++NumIntRegs;
728 break;
729 }
730 ObjSize = 2;
731 break;
732 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000733 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000734 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000735 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000736 X86::R32RegisterClass);
737 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
738 DAG.setRoot(ArgValue.getValue(1));
739 }
740 ++NumIntRegs;
741 break;
742 }
743 ObjSize = 4;
744 break;
745 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000746 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000747 if (!I->use_empty()) {
748 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
749 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
750
751 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
752 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
753 DAG.setRoot(Hi.getValue(1));
754
755 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
756 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000757 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000758 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000759 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000760 if (!I->use_empty()) {
761 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
762 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
763 DAG.setRoot(Low.getValue(1));
764
765 // Load the high part from memory.
766 // Create the frame index object for this incoming parameter...
767 int FI = MFI->CreateFixedObject(4, ArgOffset);
768 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
769 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
770 DAG.getSrcValue(NULL));
771 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
772 }
773 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000774 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000775 break;
776 }
777 ObjSize = ArgIncrement = 8;
778 break;
779 case MVT::f32: ObjSize = 4; break;
780 case MVT::f64: ObjSize = ArgIncrement = 8; break;
781 }
782
783 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
784 // dead loads.
785 if (ObjSize && !I->use_empty()) {
786 // Create the frame index object for this incoming parameter...
787 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
788
789 // Create the SelectionDAG nodes corresponding to a load from this
790 // parameter.
791 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
792
793 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
794 DAG.getSrcValue(NULL));
795 } else if (ArgValue.Val == 0) {
796 if (MVT::isInteger(ObjectVT))
797 ArgValue = DAG.getConstant(0, ObjectVT);
798 else
799 ArgValue = DAG.getConstantFP(0, ObjectVT);
800 }
801 ArgValues.push_back(ArgValue);
802
803 if (ObjSize)
804 ArgOffset += ArgIncrement; // Move on to the next argument.
805 }
806
807 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
808 // arguments and the arguments after the retaddr has been pushed are aligned.
809 if ((ArgOffset & 7) == 0)
810 ArgOffset += 4;
811
812 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
813 ReturnAddrIndex = 0; // No return address slot generated yet.
814 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
815 BytesCallerReserves = 0;
816
817 // Finally, inform the code generator which regs we return values in.
818 switch (getValueType(F.getReturnType())) {
819 default: assert(0 && "Unknown type!");
820 case MVT::isVoid: break;
821 case MVT::i1:
822 case MVT::i8:
823 case MVT::i16:
824 case MVT::i32:
825 MF.addLiveOut(X86::EAX);
826 break;
827 case MVT::i64:
828 MF.addLiveOut(X86::EAX);
829 MF.addLiveOut(X86::EDX);
830 break;
831 case MVT::f32:
832 case MVT::f64:
833 MF.addLiveOut(X86::ST0);
834 break;
835 }
836 return ArgValues;
837}
838
839std::pair<SDOperand, SDOperand>
840X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
841 bool isTailCall, SDOperand Callee,
842 ArgListTy &Args, SelectionDAG &DAG) {
843 // Count how many bytes are to be pushed on the stack.
844 unsigned NumBytes = 0;
845
846 // Keep track of the number of integer regs passed so far. This can be either
847 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
848 // used).
849 unsigned NumIntRegs = 0;
850
851 for (unsigned i = 0, e = Args.size(); i != e; ++i)
852 switch (getValueType(Args[i].second)) {
853 default: assert(0 && "Unknown value type!");
854 case MVT::i1:
855 case MVT::i8:
856 case MVT::i16:
857 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000858 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000859 ++NumIntRegs;
860 break;
861 }
862 // fall through
863 case MVT::f32:
864 NumBytes += 4;
865 break;
866 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000867 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
868 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000869 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000870 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
871 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000872 NumBytes += 4;
873 break;
874 }
875
876 // fall through
877 case MVT::f64:
878 NumBytes += 8;
879 break;
880 }
881
882 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
883 // arguments and the arguments after the retaddr has been pushed are aligned.
884 if ((NumBytes & 7) == 0)
885 NumBytes += 4;
886
Chris Lattner94dd2922006-02-13 09:00:43 +0000887 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000888
889 // Arguments go on the stack in reverse order, as specified by the ABI.
890 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000891 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000892 NumIntRegs = 0;
893 std::vector<SDOperand> Stores;
894 std::vector<SDOperand> RegValuesToPass;
895 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
896 switch (getValueType(Args[i].second)) {
897 default: assert(0 && "Unexpected ValueType for argument!");
898 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000899 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
900 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000901 case MVT::i8:
902 case MVT::i16:
903 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000904 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000905 RegValuesToPass.push_back(Args[i].first);
906 ++NumIntRegs;
907 break;
908 }
909 // Fall through
910 case MVT::f32: {
911 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
912 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
913 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
914 Args[i].first, PtrOff,
915 DAG.getSrcValue(NULL)));
916 ArgOffset += 4;
917 break;
918 }
919 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000920 // Can pass (at least) part of it in regs?
921 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000922 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
923 Args[i].first, DAG.getConstant(1, MVT::i32));
924 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
925 Args[i].first, DAG.getConstant(0, MVT::i32));
926 RegValuesToPass.push_back(Lo);
927 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000928
929 // Pass both parts in regs?
930 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000931 RegValuesToPass.push_back(Hi);
932 ++NumIntRegs;
933 } else {
934 // Pass the high part in memory.
935 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
936 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
937 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
938 Hi, PtrOff, DAG.getSrcValue(NULL)));
939 ArgOffset += 4;
940 }
941 break;
942 }
943 // Fall through
944 case MVT::f64:
945 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
946 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
947 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
948 Args[i].first, PtrOff,
949 DAG.getSrcValue(NULL)));
950 ArgOffset += 8;
951 break;
952 }
953 }
954 if (!Stores.empty())
955 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
956
957 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
958 // arguments and the arguments after the retaddr has been pushed are aligned.
959 if ((ArgOffset & 7) == 0)
960 ArgOffset += 4;
961
962 std::vector<MVT::ValueType> RetVals;
963 MVT::ValueType RetTyVT = getValueType(RetTy);
964
965 RetVals.push_back(MVT::Other);
966
967 // The result values produced have to be legal. Promote the result.
968 switch (RetTyVT) {
969 case MVT::isVoid: break;
970 default:
971 RetVals.push_back(RetTyVT);
972 break;
973 case MVT::i1:
974 case MVT::i8:
975 case MVT::i16:
976 RetVals.push_back(MVT::i32);
977 break;
978 case MVT::f32:
979 if (X86ScalarSSE)
980 RetVals.push_back(MVT::f32);
981 else
982 RetVals.push_back(MVT::f64);
983 break;
984 case MVT::i64:
985 RetVals.push_back(MVT::i32);
986 RetVals.push_back(MVT::i32);
987 break;
988 }
989
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000990 // Build a sequence of copy-to-reg nodes chained together with token chain
991 // and flag operands which copy the outgoing args into registers.
992 SDOperand InFlag;
993 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
994 unsigned CCReg;
995 SDOperand RegToPass = RegValuesToPass[i];
996 switch (RegToPass.getValueType()) {
997 default: assert(0 && "Bad thing to pass in regs");
998 case MVT::i8:
999 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +00001000 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001001 case MVT::i16:
1002 CCReg = (i == 0) ? X86::AX : X86::DX;
1003 break;
1004 case MVT::i32:
1005 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1006 break;
1007 }
1008
1009 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1010 InFlag = Chain.getValue(1);
1011 }
1012
1013 std::vector<MVT::ValueType> NodeTys;
1014 NodeTys.push_back(MVT::Other); // Returns a chain
1015 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1016 std::vector<SDOperand> Ops;
1017 Ops.push_back(Chain);
1018 Ops.push_back(Callee);
1019 if (InFlag.Val)
1020 Ops.push_back(InFlag);
1021
1022 // FIXME: Do not generate X86ISD::TAILCALL for now.
1023 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1024 InFlag = Chain.getValue(1);
1025
1026 NodeTys.clear();
1027 NodeTys.push_back(MVT::Other); // Returns a chain
1028 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1029 Ops.clear();
1030 Ops.push_back(Chain);
1031 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1032 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1033 Ops.push_back(InFlag);
1034 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1035 InFlag = Chain.getValue(1);
1036
1037 SDOperand RetVal;
1038 if (RetTyVT != MVT::isVoid) {
1039 switch (RetTyVT) {
1040 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001041 case MVT::i1:
1042 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001043 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1044 Chain = RetVal.getValue(1);
1045 if (RetTyVT == MVT::i1)
1046 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1047 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001048 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001049 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1050 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001051 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001052 case MVT::i32:
1053 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1054 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001055 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001056 case MVT::i64: {
1057 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1058 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1059 Lo.getValue(2));
1060 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1061 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001062 break;
1063 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001064 case MVT::f32:
1065 case MVT::f64: {
1066 std::vector<MVT::ValueType> Tys;
1067 Tys.push_back(MVT::f64);
1068 Tys.push_back(MVT::Other);
1069 Tys.push_back(MVT::Flag);
1070 std::vector<SDOperand> Ops;
1071 Ops.push_back(Chain);
1072 Ops.push_back(InFlag);
1073 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1074 Chain = RetVal.getValue(1);
1075 InFlag = RetVal.getValue(2);
1076 if (X86ScalarSSE) {
1077 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1078 // shouldn't be necessary except that RFP cannot be live across
1079 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1080 MachineFunction &MF = DAG.getMachineFunction();
1081 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1082 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1083 Tys.clear();
1084 Tys.push_back(MVT::Other);
1085 Ops.clear();
1086 Ops.push_back(Chain);
1087 Ops.push_back(RetVal);
1088 Ops.push_back(StackSlot);
1089 Ops.push_back(DAG.getValueType(RetTyVT));
1090 Ops.push_back(InFlag);
1091 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1092 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1093 DAG.getSrcValue(NULL));
1094 Chain = RetVal.getValue(1);
1095 }
Evan Chengd9558e02006-01-06 00:43:03 +00001096
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001097 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1098 // FIXME: we would really like to remember that this FP_ROUND
1099 // operation is okay to eliminate if we allow excess FP precision.
1100 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1101 break;
1102 }
1103 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001104 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001105
1106 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001107}
1108
1109SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1110 if (ReturnAddrIndex == 0) {
1111 // Set up a frame object for the return address.
1112 MachineFunction &MF = DAG.getMachineFunction();
1113 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1114 }
1115
1116 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1117}
1118
1119
1120
1121std::pair<SDOperand, SDOperand> X86TargetLowering::
1122LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1123 SelectionDAG &DAG) {
1124 SDOperand Result;
1125 if (Depth) // Depths > 0 not supported yet!
1126 Result = DAG.getConstant(0, getPointerTy());
1127 else {
1128 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1129 if (!isFrameAddress)
1130 // Just load the return address
1131 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1132 DAG.getSrcValue(NULL));
1133 else
1134 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1135 DAG.getConstant(4, MVT::i32));
1136 }
1137 return std::make_pair(Result, Chain);
1138}
1139
Evan Cheng4a460802006-01-11 00:33:36 +00001140/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1141/// which corresponds to the condition code.
1142static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1143 switch (X86CC) {
1144 default: assert(0 && "Unknown X86 conditional code!");
1145 case X86ISD::COND_A: return X86::JA;
1146 case X86ISD::COND_AE: return X86::JAE;
1147 case X86ISD::COND_B: return X86::JB;
1148 case X86ISD::COND_BE: return X86::JBE;
1149 case X86ISD::COND_E: return X86::JE;
1150 case X86ISD::COND_G: return X86::JG;
1151 case X86ISD::COND_GE: return X86::JGE;
1152 case X86ISD::COND_L: return X86::JL;
1153 case X86ISD::COND_LE: return X86::JLE;
1154 case X86ISD::COND_NE: return X86::JNE;
1155 case X86ISD::COND_NO: return X86::JNO;
1156 case X86ISD::COND_NP: return X86::JNP;
1157 case X86ISD::COND_NS: return X86::JNS;
1158 case X86ISD::COND_O: return X86::JO;
1159 case X86ISD::COND_P: return X86::JP;
1160 case X86ISD::COND_S: return X86::JS;
1161 }
1162}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001163
Evan Cheng6dfa9992006-01-30 23:41:35 +00001164/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1165/// specific condition code. It returns a false if it cannot do a direct
1166/// translation. X86CC is the translated CondCode. Flip is set to true if the
1167/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001168static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1169 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001170 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001171 Flip = false;
1172 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001173 if (!isFP) {
1174 switch (SetCCOpcode) {
1175 default: break;
1176 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1177 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1178 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1179 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1180 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1181 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1182 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1183 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1184 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1185 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1186 }
1187 } else {
1188 // On a floating point condition, the flags are set as follows:
1189 // ZF PF CF op
1190 // 0 | 0 | 0 | X > Y
1191 // 0 | 0 | 1 | X < Y
1192 // 1 | 0 | 0 | X == Y
1193 // 1 | 1 | 1 | unordered
1194 switch (SetCCOpcode) {
1195 default: break;
1196 case ISD::SETUEQ:
1197 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001198 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001199 case ISD::SETOGT:
1200 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001201 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001202 case ISD::SETOGE:
1203 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001204 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001205 case ISD::SETULT:
1206 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001207 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001208 case ISD::SETULE:
1209 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1210 case ISD::SETONE:
1211 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1212 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1213 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1214 }
1215 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001216
1217 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001218}
1219
Evan Cheng4a460802006-01-11 00:33:36 +00001220/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1221/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001222/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001223static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001224 switch (X86CC) {
1225 default:
1226 return false;
1227 case X86ISD::COND_B:
1228 case X86ISD::COND_BE:
1229 case X86ISD::COND_E:
1230 case X86ISD::COND_P:
1231 case X86ISD::COND_A:
1232 case X86ISD::COND_AE:
1233 case X86ISD::COND_NE:
1234 case X86ISD::COND_NP:
1235 return true;
1236 }
1237}
1238
Evan Cheng4a460802006-01-11 00:33:36 +00001239MachineBasicBlock *
1240X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1241 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001242 switch (MI->getOpcode()) {
1243 default: assert(false && "Unexpected instr type to insert");
1244 case X86::CMOV_FR32:
1245 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001246 // To "insert" a SELECT_CC instruction, we actually have to insert the
1247 // diamond control-flow pattern. The incoming instruction knows the
1248 // destination vreg to set, the condition code register to branch on, the
1249 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001250 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1251 ilist<MachineBasicBlock>::iterator It = BB;
1252 ++It;
1253
1254 // thisMBB:
1255 // ...
1256 // TrueVal = ...
1257 // cmpTY ccX, r1, r2
1258 // bCC copy1MBB
1259 // fallthrough --> copy0MBB
1260 MachineBasicBlock *thisMBB = BB;
1261 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1262 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1263 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1264 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1265 MachineFunction *F = BB->getParent();
1266 F->getBasicBlockList().insert(It, copy0MBB);
1267 F->getBasicBlockList().insert(It, sinkMBB);
1268 // Update machine-CFG edges
1269 BB->addSuccessor(copy0MBB);
1270 BB->addSuccessor(sinkMBB);
1271
1272 // copy0MBB:
1273 // %FalseValue = ...
1274 // # fallthrough to sinkMBB
1275 BB = copy0MBB;
1276
1277 // Update machine-CFG edges
1278 BB->addSuccessor(sinkMBB);
1279
1280 // sinkMBB:
1281 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1282 // ...
1283 BB = sinkMBB;
1284 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1285 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1286 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001287
Evan Cheng0cc39452006-01-16 21:21:29 +00001288 delete MI; // The pseudo instruction is gone now.
1289 return BB;
1290 }
Evan Cheng4a460802006-01-11 00:33:36 +00001291
Evan Cheng0cc39452006-01-16 21:21:29 +00001292 case X86::FP_TO_INT16_IN_MEM:
1293 case X86::FP_TO_INT32_IN_MEM:
1294 case X86::FP_TO_INT64_IN_MEM: {
1295 // Change the floating point control register to use "round towards zero"
1296 // mode when truncating to an integer value.
1297 MachineFunction *F = BB->getParent();
1298 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1299 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1300
1301 // Load the old value of the high byte of the control word...
1302 unsigned OldCW =
1303 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1304 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1305
1306 // Set the high part to be round to zero...
1307 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1308
1309 // Reload the modified control word now...
1310 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1311
1312 // Restore the memory image of control word to original value
1313 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1314
1315 // Get the X86 opcode to use.
1316 unsigned Opc;
1317 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001318 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001319 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1320 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1321 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1322 }
1323
1324 X86AddressMode AM;
1325 MachineOperand &Op = MI->getOperand(0);
1326 if (Op.isRegister()) {
1327 AM.BaseType = X86AddressMode::RegBase;
1328 AM.Base.Reg = Op.getReg();
1329 } else {
1330 AM.BaseType = X86AddressMode::FrameIndexBase;
1331 AM.Base.FrameIndex = Op.getFrameIndex();
1332 }
1333 Op = MI->getOperand(1);
1334 if (Op.isImmediate())
1335 AM.Scale = Op.getImmedValue();
1336 Op = MI->getOperand(2);
1337 if (Op.isImmediate())
1338 AM.IndexReg = Op.getImmedValue();
1339 Op = MI->getOperand(3);
1340 if (Op.isGlobalAddress()) {
1341 AM.GV = Op.getGlobal();
1342 } else {
1343 AM.Disp = Op.getImmedValue();
1344 }
1345 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1346
1347 // Reload the original control word now.
1348 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1349
1350 delete MI; // The pseudo instruction is gone now.
1351 return BB;
1352 }
1353 }
Evan Cheng4a460802006-01-11 00:33:36 +00001354}
1355
1356
1357//===----------------------------------------------------------------------===//
1358// X86 Custom Lowering Hooks
1359//===----------------------------------------------------------------------===//
1360
Evan Cheng30b37b52006-03-13 23:18:16 +00001361/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1362/// load. For Darwin, external and weak symbols are indirect, loading the value
1363/// at address GV rather then the value of GV itself. This means that the
1364/// GlobalAddress must be in the base or index register of the address, not the
1365/// GV offset field.
1366static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1367 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1368 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1369}
1370
Evan Chengb9df0ca2006-03-22 02:53:00 +00001371/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1372/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1373bool X86::isPSHUFDMask(SDNode *N) {
1374 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1375
1376 if (N->getNumOperands() != 4)
1377 return false;
1378
1379 // This is a splat operation if each element of the permute is the same, and
1380 // if the value doesn't reference the second vector.
1381 SDOperand Elt = N->getOperand(0);
1382 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1383 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1384 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1385 "Invalid VECTOR_SHUFFLE mask!");
1386 if (cast<ConstantSDNode>(N->getOperand(i))->getValue() >= 4) return false;
1387 }
1388
1389 return true;
1390}
1391
1392/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1393/// a splat of a single element.
1394bool X86::isSplatMask(SDNode *N) {
1395 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1396
1397 // We can only splat 64-bit, and 32-bit quantities.
1398 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1399 return false;
1400
1401 // This is a splat operation if each element of the permute is the same, and
1402 // if the value doesn't reference the second vector.
1403 SDOperand Elt = N->getOperand(0);
1404 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1405 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
1406 assert(isa<ConstantSDNode>(N->getOperand(i)) &&
1407 "Invalid VECTOR_SHUFFLE mask!");
1408 if (N->getOperand(i) != Elt) return false;
1409 }
1410
1411 // Make sure it is a splat of the first vector operand.
1412 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1413}
1414
1415/// getShuffleImmediate - Return the appropriate immediate to shuffle
1416/// the specified isShuffleMask VECTOR_SHUFFLE mask.
1417unsigned X86::getShuffleImmediate(SDNode *N) {
1418 unsigned NumOperands = N->getNumOperands();
1419 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1420 unsigned Mask = 0;
1421 unsigned i = NumOperands - 1;
1422 do {
1423 Mask |= cast<ConstantSDNode>(N->getOperand(i))->getValue();
1424 Mask <<= Shift;
1425 --i;
1426 } while (i != 0);
1427
1428 return Mask;
1429}
1430
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001431/// LowerOperation - Provide custom lowering hooks for some operations.
1432///
1433SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1434 switch (Op.getOpcode()) {
1435 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001436 case ISD::SHL_PARTS:
1437 case ISD::SRA_PARTS:
1438 case ISD::SRL_PARTS: {
1439 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1440 "Not an i64 shift!");
1441 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1442 SDOperand ShOpLo = Op.getOperand(0);
1443 SDOperand ShOpHi = Op.getOperand(1);
1444 SDOperand ShAmt = Op.getOperand(2);
1445 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001446 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001447 : DAG.getConstant(0, MVT::i32);
1448
1449 SDOperand Tmp2, Tmp3;
1450 if (Op.getOpcode() == ISD::SHL_PARTS) {
1451 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1452 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1453 } else {
1454 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001455 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001456 }
1457
1458 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1459 ShAmt, DAG.getConstant(32, MVT::i8));
1460
1461 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001462 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001463
1464 std::vector<MVT::ValueType> Tys;
1465 Tys.push_back(MVT::i32);
1466 Tys.push_back(MVT::Flag);
1467 std::vector<SDOperand> Ops;
1468 if (Op.getOpcode() == ISD::SHL_PARTS) {
1469 Ops.push_back(Tmp2);
1470 Ops.push_back(Tmp3);
1471 Ops.push_back(CC);
1472 Ops.push_back(InFlag);
1473 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1474 InFlag = Hi.getValue(1);
1475
1476 Ops.clear();
1477 Ops.push_back(Tmp3);
1478 Ops.push_back(Tmp1);
1479 Ops.push_back(CC);
1480 Ops.push_back(InFlag);
1481 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1482 } else {
1483 Ops.push_back(Tmp2);
1484 Ops.push_back(Tmp3);
1485 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001486 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001487 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1488 InFlag = Lo.getValue(1);
1489
1490 Ops.clear();
1491 Ops.push_back(Tmp3);
1492 Ops.push_back(Tmp1);
1493 Ops.push_back(CC);
1494 Ops.push_back(InFlag);
1495 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1496 }
1497
1498 Tys.clear();
1499 Tys.push_back(MVT::i32);
1500 Tys.push_back(MVT::i32);
1501 Ops.clear();
1502 Ops.push_back(Lo);
1503 Ops.push_back(Hi);
1504 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1505 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001506 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001507 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001508 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001509 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001510
1511 SDOperand Result;
1512 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1513 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001514 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001515 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001516 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001517 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1518 DAG.getEntryNode(), Op.getOperand(0),
1519 StackSlot, DAG.getSrcValue(NULL));
1520
1521 // Build the FILD
1522 std::vector<MVT::ValueType> Tys;
1523 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001524 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001525 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001526 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001527 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001528 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001529 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001530 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1531 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001532
1533 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001534 Chain = Result.getValue(1);
1535 SDOperand InFlag = Result.getValue(2);
1536
Evan Chenge3de85b2006-02-04 02:20:30 +00001537 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001538 // shouldn't be necessary except that RFP cannot be live across
1539 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1540 MachineFunction &MF = DAG.getMachineFunction();
1541 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1542 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1543 std::vector<MVT::ValueType> Tys;
1544 Tys.push_back(MVT::Other);
1545 std::vector<SDOperand> Ops;
1546 Ops.push_back(Chain);
1547 Ops.push_back(Result);
1548 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001549 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001550 Ops.push_back(InFlag);
1551 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1552 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1553 DAG.getSrcValue(NULL));
1554 }
1555
Evan Chenga3195e82006-01-12 22:54:21 +00001556 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001557 }
1558 case ISD::FP_TO_SINT: {
1559 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001560 "Unknown FP_TO_SINT to lower!");
1561 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1562 // stack slot.
1563 MachineFunction &MF = DAG.getMachineFunction();
1564 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1565 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1566 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1567
1568 unsigned Opc;
1569 switch (Op.getValueType()) {
1570 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1571 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1572 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1573 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1574 }
1575
Evan Cheng6dab0532006-01-30 08:02:57 +00001576 SDOperand Chain = DAG.getEntryNode();
1577 SDOperand Value = Op.getOperand(0);
1578 if (X86ScalarSSE) {
1579 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1580 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1581 DAG.getSrcValue(0));
1582 std::vector<MVT::ValueType> Tys;
1583 Tys.push_back(MVT::f64);
1584 Tys.push_back(MVT::Other);
1585 std::vector<SDOperand> Ops;
1586 Ops.push_back(Chain);
1587 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001588 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001589 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1590 Chain = Value.getValue(1);
1591 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1592 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1593 }
1594
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001595 // Build the FP_TO_INT*_IN_MEM
1596 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001597 Ops.push_back(Chain);
1598 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001599 Ops.push_back(StackSlot);
1600 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1601
1602 // Load the result.
1603 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1604 DAG.getSrcValue(NULL));
1605 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001606 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001607 std::vector<MVT::ValueType> Tys;
1608 Tys.push_back(MVT::Other);
1609 Tys.push_back(MVT::Flag);
1610 std::vector<SDOperand> Ops;
1611 Ops.push_back(Op.getOperand(0));
1612 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001613 Ops.clear();
1614 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1615 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1616 MVT::i32, Ops[0].getValue(2)));
1617 Ops.push_back(Ops[1].getValue(1));
1618 Tys[0] = Tys[1] = MVT::i32;
1619 Tys.push_back(MVT::Other);
1620 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001621 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001622 case ISD::FABS: {
1623 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001624 const Type *OpNTy = MVT::getTypeForValueType(VT);
1625 std::vector<Constant*> CV;
1626 if (VT == MVT::f64) {
1627 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1628 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1629 } else {
1630 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1631 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1632 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1633 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1634 }
1635 Constant *CS = ConstantStruct::get(CV);
1636 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1637 SDOperand Mask
1638 = DAG.getNode(X86ISD::LOAD_PACK,
1639 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001640 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1641 }
Evan Cheng223547a2006-01-31 22:28:30 +00001642 case ISD::FNEG: {
1643 MVT::ValueType VT = Op.getValueType();
1644 const Type *OpNTy = MVT::getTypeForValueType(VT);
1645 std::vector<Constant*> CV;
1646 if (VT == MVT::f64) {
1647 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1648 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1649 } else {
1650 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1651 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1652 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1653 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1654 }
1655 Constant *CS = ConstantStruct::get(CV);
1656 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1657 SDOperand Mask
1658 = DAG.getNode(X86ISD::LOAD_PACK,
1659 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1660 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1661 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001662 case ISD::SETCC: {
1663 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001664 SDOperand Cond;
1665 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001666 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1667 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001668 bool Flip;
1669 unsigned X86CC;
1670 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1671 if (Flip)
1672 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1673 Op.getOperand(1), Op.getOperand(0));
1674 else
1675 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1676 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001677 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1678 DAG.getConstant(X86CC, MVT::i8), Cond);
1679 } else {
1680 assert(isFP && "Illegal integer SetCC!");
1681
Evan Cheng6dfa9992006-01-30 23:41:35 +00001682 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1683 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001684 std::vector<MVT::ValueType> Tys;
1685 std::vector<SDOperand> Ops;
1686 switch (SetCCOpcode) {
1687 default: assert(false && "Illegal floating point SetCC!");
1688 case ISD::SETOEQ: { // !PF & ZF
1689 Tys.push_back(MVT::i8);
1690 Tys.push_back(MVT::Flag);
1691 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1692 Ops.push_back(Cond);
1693 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1694 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1695 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1696 Tmp1.getValue(1));
1697 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1698 }
Evan Chengd9558e02006-01-06 00:43:03 +00001699 case ISD::SETUNE: { // PF | !ZF
1700 Tys.push_back(MVT::i8);
1701 Tys.push_back(MVT::Flag);
1702 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1703 Ops.push_back(Cond);
1704 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1705 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1706 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1707 Tmp1.getValue(1));
1708 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1709 }
1710 }
1711 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001712 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001713 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001714 MVT::ValueType VT = Op.getValueType();
1715 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001716 bool isFPStack = isFP && !X86ScalarSSE;
1717 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001718 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001719 SDOperand Op0 = Op.getOperand(0);
1720 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001721 if (Op0.getOpcode() == ISD::SETCC)
1722 Op0 = LowerOperation(Op0, DAG);
1723
Evan Chengaaca22c2006-01-10 20:26:56 +00001724 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001725 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1726 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1727 // have another use it will be eliminated.
1728 // If the X86ISD::SETCC has more than one use, then it's probably better
1729 // to use a test instead of duplicating the X86ISD::CMP (for register
1730 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001731 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1732 if (!Op0.hasOneUse()) {
1733 std::vector<MVT::ValueType> Tys;
1734 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1735 Tys.push_back(Op0.Val->getValueType(i));
1736 std::vector<SDOperand> Ops;
1737 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1738 Ops.push_back(Op0.getOperand(i));
1739 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1740 }
1741
Evan Cheng1bcee362006-01-13 01:03:02 +00001742 CC = Op0.getOperand(0);
1743 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001744 // Make a copy as flag result cannot be used by more than one.
1745 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1746 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001747 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001748 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001749 } else
1750 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001751 } else
1752 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001753
Evan Cheng189d01e2006-01-13 01:06:49 +00001754 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001755 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001756 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001757 }
Evan Chenge3413162006-01-09 18:33:28 +00001758
1759 std::vector<MVT::ValueType> Tys;
1760 Tys.push_back(Op.getValueType());
1761 Tys.push_back(MVT::Flag);
1762 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001763 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1764 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001765 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001766 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001767 Ops.push_back(CC);
1768 Ops.push_back(Cond);
1769 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001770 }
Evan Cheng898101c2005-12-19 23:12:38 +00001771 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001772 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001773 SDOperand Cond = Op.getOperand(1);
1774 SDOperand Dest = Op.getOperand(2);
1775 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001776 if (Cond.getOpcode() == ISD::SETCC)
1777 Cond = LowerOperation(Cond, DAG);
1778
Evan Chengd5781fc2005-12-21 20:21:51 +00001779 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001780 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1781 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1782 // have another use it will be eliminated.
1783 // If the X86ISD::SETCC has more than one use, then it's probably better
1784 // to use a test instead of duplicating the X86ISD::CMP (for register
1785 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001786 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1787 if (!Cond.hasOneUse()) {
1788 std::vector<MVT::ValueType> Tys;
1789 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1790 Tys.push_back(Cond.Val->getValueType(i));
1791 std::vector<SDOperand> Ops;
1792 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1793 Ops.push_back(Cond.getOperand(i));
1794 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1795 }
1796
Evan Cheng1bcee362006-01-13 01:03:02 +00001797 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001798 Cond = Cond.getOperand(1);
1799 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001800 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001801 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001802 } else
1803 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001804 } else
1805 addTest = true;
1806
1807 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001808 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001809 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1810 }
1811 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1812 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1813 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001814 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001815 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001816 SDOperand Chain = Op.getOperand(0);
1817 unsigned Align =
1818 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1819 if (Align == 0) Align = 1;
1820
Evan Cheng18a84522006-02-16 00:21:07 +00001821 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1822 // If not DWORD aligned, call memset if size is less than the threshold.
1823 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001824 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001825 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001826 MVT::ValueType IntPtr = getPointerTy();
1827 const Type *IntPtrTy = getTargetData().getIntPtrType();
1828 std::vector<std::pair<SDOperand, const Type*> > Args;
1829 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1830 // Extend the ubyte argument to be an int value for the call.
1831 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1832 Args.push_back(std::make_pair(Val, IntPtrTy));
1833 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1834 std::pair<SDOperand,SDOperand> CallResult =
1835 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1836 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1837 return CallResult.second;
1838 }
1839
Evan Cheng67f92a72006-01-11 22:15:48 +00001840 MVT::ValueType AVT;
1841 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001842 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1843 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001844 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001845 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001846 unsigned ValReg;
1847 unsigned Val = ValC->getValue() & 255;
1848
1849 // If the value is a constant, then we can potentially use larger sets.
1850 switch (Align & 3) {
1851 case 2: // WORD aligned
1852 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001853 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1854 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001855 Val = (Val << 8) | Val;
1856 ValReg = X86::AX;
1857 break;
1858 case 0: // DWORD aligned
1859 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001860 if (I) {
1861 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1862 BytesLeft = I->getValue() % 4;
1863 } else {
1864 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1865 DAG.getConstant(2, MVT::i8));
1866 TwoRepStos = true;
1867 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001868 Val = (Val << 8) | Val;
1869 Val = (Val << 16) | Val;
1870 ValReg = X86::EAX;
1871 break;
1872 default: // Byte aligned
1873 AVT = MVT::i8;
1874 Count = Op.getOperand(3);
1875 ValReg = X86::AL;
1876 break;
1877 }
1878
1879 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1880 InFlag);
1881 InFlag = Chain.getValue(1);
1882 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001883 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001884 Count = Op.getOperand(3);
1885 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1886 InFlag = Chain.getValue(1);
1887 }
1888
1889 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1890 InFlag = Chain.getValue(1);
1891 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1892 InFlag = Chain.getValue(1);
1893
Evan Chengff909922006-03-07 23:29:39 +00001894 std::vector<MVT::ValueType> Tys;
1895 Tys.push_back(MVT::Other);
1896 Tys.push_back(MVT::Flag);
1897 std::vector<SDOperand> Ops;
1898 Ops.push_back(Chain);
1899 Ops.push_back(DAG.getValueType(AVT));
1900 Ops.push_back(InFlag);
1901 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1902
1903 if (TwoRepStos) {
1904 InFlag = Chain.getValue(1);
1905 Count = Op.getOperand(3);
1906 MVT::ValueType CVT = Count.getValueType();
1907 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1908 DAG.getConstant(3, CVT));
1909 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1910 InFlag = Chain.getValue(1);
1911 Tys.clear();
1912 Tys.push_back(MVT::Other);
1913 Tys.push_back(MVT::Flag);
1914 Ops.clear();
1915 Ops.push_back(Chain);
1916 Ops.push_back(DAG.getValueType(MVT::i8));
1917 Ops.push_back(InFlag);
1918 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1919 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001920 // Issue stores for the last 1 - 3 bytes.
1921 SDOperand Value;
1922 unsigned Val = ValC->getValue() & 255;
1923 unsigned Offset = I->getValue() - BytesLeft;
1924 SDOperand DstAddr = Op.getOperand(1);
1925 MVT::ValueType AddrVT = DstAddr.getValueType();
1926 if (BytesLeft >= 2) {
1927 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1928 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1929 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1930 DAG.getConstant(Offset, AddrVT)),
1931 DAG.getSrcValue(NULL));
1932 BytesLeft -= 2;
1933 Offset += 2;
1934 }
1935
1936 if (BytesLeft == 1) {
1937 Value = DAG.getConstant(Val, MVT::i8);
1938 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1939 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1940 DAG.getConstant(Offset, AddrVT)),
1941 DAG.getSrcValue(NULL));
1942 }
1943 }
1944
1945 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001946 }
1947 case ISD::MEMCPY: {
1948 SDOperand Chain = Op.getOperand(0);
1949 unsigned Align =
1950 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1951 if (Align == 0) Align = 1;
1952
Evan Cheng18a84522006-02-16 00:21:07 +00001953 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1954 // If not DWORD aligned, call memcpy if size is less than the threshold.
1955 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001956 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001957 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001958 MVT::ValueType IntPtr = getPointerTy();
1959 const Type *IntPtrTy = getTargetData().getIntPtrType();
1960 std::vector<std::pair<SDOperand, const Type*> > Args;
1961 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1962 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1963 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1964 std::pair<SDOperand,SDOperand> CallResult =
1965 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1966 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1967 return CallResult.second;
1968 }
1969
Evan Cheng67f92a72006-01-11 22:15:48 +00001970 MVT::ValueType AVT;
1971 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001972 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001973 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001974 switch (Align & 3) {
1975 case 2: // WORD aligned
1976 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001977 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1978 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001979 break;
1980 case 0: // DWORD aligned
1981 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001982 if (I) {
1983 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1984 BytesLeft = I->getValue() % 4;
1985 } else {
1986 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1987 DAG.getConstant(2, MVT::i8));
1988 TwoRepMovs = true;
1989 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001990 break;
1991 default: // Byte aligned
1992 AVT = MVT::i8;
1993 Count = Op.getOperand(3);
1994 break;
1995 }
1996
Evan Cheng62bec2c2006-03-04 02:48:56 +00001997 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001998 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1999 InFlag = Chain.getValue(1);
2000 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2001 InFlag = Chain.getValue(1);
2002 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2003 InFlag = Chain.getValue(1);
2004
Evan Chengff909922006-03-07 23:29:39 +00002005 std::vector<MVT::ValueType> Tys;
2006 Tys.push_back(MVT::Other);
2007 Tys.push_back(MVT::Flag);
2008 std::vector<SDOperand> Ops;
2009 Ops.push_back(Chain);
2010 Ops.push_back(DAG.getValueType(AVT));
2011 Ops.push_back(InFlag);
2012 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2013
2014 if (TwoRepMovs) {
2015 InFlag = Chain.getValue(1);
2016 Count = Op.getOperand(3);
2017 MVT::ValueType CVT = Count.getValueType();
2018 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2019 DAG.getConstant(3, CVT));
2020 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2021 InFlag = Chain.getValue(1);
2022 Tys.clear();
2023 Tys.push_back(MVT::Other);
2024 Tys.push_back(MVT::Flag);
2025 Ops.clear();
2026 Ops.push_back(Chain);
2027 Ops.push_back(DAG.getValueType(MVT::i8));
2028 Ops.push_back(InFlag);
2029 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2030 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00002031 // Issue loads and stores for the last 1 - 3 bytes.
2032 unsigned Offset = I->getValue() - BytesLeft;
2033 SDOperand DstAddr = Op.getOperand(1);
2034 MVT::ValueType DstVT = DstAddr.getValueType();
2035 SDOperand SrcAddr = Op.getOperand(2);
2036 MVT::ValueType SrcVT = SrcAddr.getValueType();
2037 SDOperand Value;
2038 if (BytesLeft >= 2) {
2039 Value = DAG.getLoad(MVT::i16, Chain,
2040 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2041 DAG.getConstant(Offset, SrcVT)),
2042 DAG.getSrcValue(NULL));
2043 Chain = Value.getValue(1);
2044 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2045 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2046 DAG.getConstant(Offset, DstVT)),
2047 DAG.getSrcValue(NULL));
2048 BytesLeft -= 2;
2049 Offset += 2;
2050 }
2051
2052 if (BytesLeft == 1) {
2053 Value = DAG.getLoad(MVT::i8, Chain,
2054 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2055 DAG.getConstant(Offset, SrcVT)),
2056 DAG.getSrcValue(NULL));
2057 Chain = Value.getValue(1);
2058 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2059 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2060 DAG.getConstant(Offset, DstVT)),
2061 DAG.getSrcValue(NULL));
2062 }
2063 }
2064
2065 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002066 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002067
2068 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2069 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2070 // one of the above mentioned nodes. It has to be wrapped because otherwise
2071 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2072 // be used to form addressing mode. These wrapped nodes will be selected
2073 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002074 case ISD::ConstantPool: {
2075 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002076 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2077 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2078 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00002079 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002080 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002081 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002082 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2083 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2084 }
2085
2086 return Result;
2087 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002088 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002089 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2090 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2091 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00002092 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00002093 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002094 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002095 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002096 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2097 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002098
2099 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002100 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002101 // the GlobalAddress must be in the base or index register of the address,
2102 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002103 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002104 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002105 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002106 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002107 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002108
Evan Cheng002fe9b2006-01-12 07:56:47 +00002109 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002110 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002111 case ISD::ExternalSymbol: {
2112 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2113 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2114 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2115 if (getTargetMachine().
2116 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2117 // With PIC, the address is actually $g + Offset.
2118 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2119 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2120 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2121 }
2122
2123 return Result;
2124 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002125 case ISD::VASTART: {
2126 // vastart just stores the address of the VarArgsFrameIndex slot into the
2127 // memory location argument.
2128 // FIXME: Replace MVT::i32 with PointerTy
2129 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2130 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2131 Op.getOperand(1), Op.getOperand(2));
2132 }
Nate Begemanee625572006-01-27 21:09:22 +00002133 case ISD::RET: {
2134 SDOperand Copy;
2135
2136 switch(Op.getNumOperands()) {
2137 default:
2138 assert(0 && "Do not know how to return this many arguments!");
2139 abort();
2140 case 1:
2141 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2142 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2143 case 2: {
2144 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2145 if (MVT::isInteger(ArgVT))
2146 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2147 SDOperand());
2148 else if (!X86ScalarSSE) {
2149 std::vector<MVT::ValueType> Tys;
2150 Tys.push_back(MVT::Other);
2151 Tys.push_back(MVT::Flag);
2152 std::vector<SDOperand> Ops;
2153 Ops.push_back(Op.getOperand(0));
2154 Ops.push_back(Op.getOperand(1));
2155 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2156 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002157 SDOperand MemLoc;
2158 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002159 SDOperand Value = Op.getOperand(1);
2160
Evan Cheng760df292006-02-01 01:19:32 +00002161 if (Value.getOpcode() == ISD::LOAD &&
2162 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002163 Chain = Value.getOperand(0);
2164 MemLoc = Value.getOperand(1);
2165 } else {
2166 // Spill the value to memory and reload it into top of stack.
2167 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2168 MachineFunction &MF = DAG.getMachineFunction();
2169 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2170 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2171 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2172 Value, MemLoc, DAG.getSrcValue(0));
2173 }
Nate Begemanee625572006-01-27 21:09:22 +00002174 std::vector<MVT::ValueType> Tys;
2175 Tys.push_back(MVT::f64);
2176 Tys.push_back(MVT::Other);
2177 std::vector<SDOperand> Ops;
2178 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002179 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002180 Ops.push_back(DAG.getValueType(ArgVT));
2181 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2182 Tys.clear();
2183 Tys.push_back(MVT::Other);
2184 Tys.push_back(MVT::Flag);
2185 Ops.clear();
2186 Ops.push_back(Copy.getValue(1));
2187 Ops.push_back(Copy);
2188 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2189 }
2190 break;
2191 }
2192 case 3:
2193 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2194 SDOperand());
2195 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2196 break;
2197 }
2198 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2199 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2200 Copy.getValue(1));
2201 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002202 case ISD::SCALAR_TO_VECTOR: {
2203 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2204 return DAG.getNode(X86ISD::SCALAR_TO_VECTOR, Op.getValueType(), AnyExt);
2205 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00002206 case ISD::VECTOR_SHUFFLE: {
2207 SDOperand V1 = Op.getOperand(0);
2208 SDOperand V2 = Op.getOperand(1);
2209 SDOperand PermMask = Op.getOperand(2);
2210 MVT::ValueType VT = Op.getValueType();
2211
2212 if (V2.getOpcode() == ISD::UNDEF) {
2213 // Handle splat cases.
2214 if (X86::isSplatMask(PermMask.Val)) {
2215 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2216 // Use unpcklpd
2217 return DAG.getNode(X86ISD::UNPCKLP, VT, V1, V1);
2218 // Leave the VECTOR_SHUFFLE alone. It matches SHUFP*.
2219 break;
2220 } else if (VT == MVT::v4f32 && X86::isPSHUFDMask(PermMask.Val))
2221 // Leave the VECTOR_SHUFFLE alone. It matches PSHUFD.
2222 break;
2223 }
2224
2225 // TODO.
2226 assert(0);
2227 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002228 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002229}
Evan Cheng72261582005-12-20 06:22:03 +00002230
2231const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2232 switch (Opcode) {
2233 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002234 case X86ISD::SHLD: return "X86ISD::SHLD";
2235 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002236 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002237 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002238 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002239 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002240 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2241 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2242 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002243 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002244 case X86ISD::FST: return "X86ISD::FST";
2245 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002246 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002247 case X86ISD::CALL: return "X86ISD::CALL";
2248 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2249 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2250 case X86ISD::CMP: return "X86ISD::CMP";
2251 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002252 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002253 case X86ISD::CMOV: return "X86ISD::CMOV";
2254 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002255 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002256 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2257 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002258 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002259 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002260 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng48090aa2006-03-21 23:01:21 +00002261 case X86ISD::SCALAR_TO_VECTOR: return "X86ISD::SCALAR_TO_VECTOR";
Evan Chengb9df0ca2006-03-22 02:53:00 +00002262 case X86ISD::UNPCKLP: return "X86ISD::UNPCKLP";
Evan Cheng72261582005-12-20 06:22:03 +00002263 }
2264}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002265
Nate Begeman368e18d2006-02-16 21:11:51 +00002266void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2267 uint64_t Mask,
2268 uint64_t &KnownZero,
2269 uint64_t &KnownOne,
2270 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002271
2272 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002273 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002274
2275 switch (Opc) {
2276 default:
2277 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2278 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002279 case X86ISD::SETCC:
2280 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2281 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002282 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002283}
Chris Lattner259e97c2006-01-31 19:43:35 +00002284
2285std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002286getRegClassForInlineAsmConstraint(const std::string &Constraint,
2287 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002288 if (Constraint.size() == 1) {
2289 // FIXME: not handling fp-stack yet!
2290 // FIXME: not handling MMX registers yet ('y' constraint).
2291 switch (Constraint[0]) { // GCC X86 Constraint Letters
2292 default: break; // Unknown constriant letter
2293 case 'r': // GENERAL_REGS
2294 case 'R': // LEGACY_REGS
2295 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2296 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2297 case 'l': // INDEX_REGS
2298 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2299 X86::ESI, X86::EDI, X86::EBP, 0);
2300 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2301 case 'Q': // Q_REGS
2302 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2303 case 'x': // SSE_REGS if SSE1 allowed
2304 if (Subtarget->hasSSE1())
2305 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2306 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2307 0);
2308 return std::vector<unsigned>();
2309 case 'Y': // SSE_REGS if SSE2 allowed
2310 if (Subtarget->hasSSE2())
2311 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2312 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2313 0);
2314 return std::vector<unsigned>();
2315 }
2316 }
2317
Chris Lattner1efa40f2006-02-22 00:56:39 +00002318 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002319}
Evan Cheng30b37b52006-03-13 23:18:16 +00002320
2321/// isLegalAddressImmediate - Return true if the integer value or
2322/// GlobalValue can be used as the offset of the target addressing mode.
2323bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2324 // X86 allows a sign-extended 32-bit immediate field.
2325 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2326}
2327
2328bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2329 if (getTargetMachine().
2330 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2331 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2332 if (RModel == Reloc::Static)
2333 return true;
2334 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002335 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002336 else
2337 return false;
2338 } else
2339 return true;
2340}