blob: 1d4221414fff5f2864cdb17ceefafcdf71937703 [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 Cheng470a6ad2006-02-22 02:26:30 +0000281 }
282
283 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
284 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
285 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
287 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
288 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
289
290
Evan Cheng48090aa2006-03-21 23:01:21 +0000291 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
292 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
293 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
294 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
295 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Expand);
296 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Expand);
297 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Expand);
298 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
299 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Expand);
300 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
301 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000302 }
303
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000304 computeRegisterProperties();
305
Evan Cheng87ed7162006-02-14 08:25:08 +0000306 // FIXME: These should be based on subtarget info. Plus, the values should
307 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000308 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
309 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
310 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000311 allowUnalignedMemoryAccesses = true; // x86 supports it!
312}
313
314std::vector<SDOperand>
315X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
316 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
317 return LowerFastCCArguments(F, DAG);
318 return LowerCCCArguments(F, DAG);
319}
320
321std::pair<SDOperand, SDOperand>
322X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
323 bool isVarArg, unsigned CallingConv,
324 bool isTailCall,
325 SDOperand Callee, ArgListTy &Args,
326 SelectionDAG &DAG) {
327 assert((!isVarArg || CallingConv == CallingConv::C) &&
328 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000329
330 // If the callee is a GlobalAddress node (quite common, every direct call is)
331 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
332 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
333 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000334 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
335 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000336
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000337 if (CallingConv == CallingConv::Fast && EnableFastCC)
338 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
339 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
340}
341
342//===----------------------------------------------------------------------===//
343// C Calling Convention implementation
344//===----------------------------------------------------------------------===//
345
346std::vector<SDOperand>
347X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
348 std::vector<SDOperand> ArgValues;
349
350 MachineFunction &MF = DAG.getMachineFunction();
351 MachineFrameInfo *MFI = MF.getFrameInfo();
352
353 // Add DAG nodes to load the arguments... On entry to a function on the X86,
354 // the stack frame looks like this:
355 //
356 // [ESP] -- return address
357 // [ESP + 4] -- first argument (leftmost lexically)
358 // [ESP + 8] -- second argument, if first argument is four bytes in size
359 // ...
360 //
361 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
362 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
363 MVT::ValueType ObjectVT = getValueType(I->getType());
364 unsigned ArgIncrement = 4;
365 unsigned ObjSize;
366 switch (ObjectVT) {
367 default: assert(0 && "Unhandled argument type!");
368 case MVT::i1:
369 case MVT::i8: ObjSize = 1; break;
370 case MVT::i16: ObjSize = 2; break;
371 case MVT::i32: ObjSize = 4; break;
372 case MVT::i64: ObjSize = ArgIncrement = 8; break;
373 case MVT::f32: ObjSize = 4; break;
374 case MVT::f64: ObjSize = ArgIncrement = 8; break;
375 }
376 // Create the frame index object for this incoming parameter...
377 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
378
379 // Create the SelectionDAG nodes corresponding to a load from this parameter
380 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
381
382 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
383 // dead loads.
384 SDOperand ArgValue;
385 if (!I->use_empty())
386 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
387 DAG.getSrcValue(NULL));
388 else {
389 if (MVT::isInteger(ObjectVT))
390 ArgValue = DAG.getConstant(0, ObjectVT);
391 else
392 ArgValue = DAG.getConstantFP(0, ObjectVT);
393 }
394 ArgValues.push_back(ArgValue);
395
396 ArgOffset += ArgIncrement; // Move on to the next argument...
397 }
398
399 // If the function takes variable number of arguments, make a frame index for
400 // the start of the first vararg value... for expansion of llvm.va_start.
401 if (F.isVarArg())
402 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
403 ReturnAddrIndex = 0; // No return address slot generated yet.
404 BytesToPopOnReturn = 0; // Callee pops nothing.
405 BytesCallerReserves = ArgOffset;
406
407 // Finally, inform the code generator which regs we return values in.
408 switch (getValueType(F.getReturnType())) {
409 default: assert(0 && "Unknown type!");
410 case MVT::isVoid: break;
411 case MVT::i1:
412 case MVT::i8:
413 case MVT::i16:
414 case MVT::i32:
415 MF.addLiveOut(X86::EAX);
416 break;
417 case MVT::i64:
418 MF.addLiveOut(X86::EAX);
419 MF.addLiveOut(X86::EDX);
420 break;
421 case MVT::f32:
422 case MVT::f64:
423 MF.addLiveOut(X86::ST0);
424 break;
425 }
426 return ArgValues;
427}
428
429std::pair<SDOperand, SDOperand>
430X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
431 bool isVarArg, bool isTailCall,
432 SDOperand Callee, ArgListTy &Args,
433 SelectionDAG &DAG) {
434 // Count how many bytes are to be pushed on the stack.
435 unsigned NumBytes = 0;
436
437 if (Args.empty()) {
438 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000439 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000440 } else {
441 for (unsigned i = 0, e = Args.size(); i != e; ++i)
442 switch (getValueType(Args[i].second)) {
443 default: assert(0 && "Unknown value type!");
444 case MVT::i1:
445 case MVT::i8:
446 case MVT::i16:
447 case MVT::i32:
448 case MVT::f32:
449 NumBytes += 4;
450 break;
451 case MVT::i64:
452 case MVT::f64:
453 NumBytes += 8;
454 break;
455 }
456
Chris Lattner94dd2922006-02-13 09:00:43 +0000457 Chain = DAG.getCALLSEQ_START(Chain,
458 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000459
460 // Arguments go on the stack in reverse order, as specified by the ABI.
461 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000462 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000463 std::vector<SDOperand> Stores;
464
465 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
466 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
467 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
468
469 switch (getValueType(Args[i].second)) {
470 default: assert(0 && "Unexpected ValueType for argument!");
471 case MVT::i1:
472 case MVT::i8:
473 case MVT::i16:
474 // Promote the integer to 32 bits. If the input type is signed use a
475 // sign extend, otherwise use a zero extend.
476 if (Args[i].second->isSigned())
477 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
478 else
479 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
480
481 // FALL THROUGH
482 case MVT::i32:
483 case MVT::f32:
484 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
485 Args[i].first, PtrOff,
486 DAG.getSrcValue(NULL)));
487 ArgOffset += 4;
488 break;
489 case MVT::i64:
490 case MVT::f64:
491 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
492 Args[i].first, PtrOff,
493 DAG.getSrcValue(NULL)));
494 ArgOffset += 8;
495 break;
496 }
497 }
498 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
499 }
500
501 std::vector<MVT::ValueType> RetVals;
502 MVT::ValueType RetTyVT = getValueType(RetTy);
503 RetVals.push_back(MVT::Other);
504
505 // The result values produced have to be legal. Promote the result.
506 switch (RetTyVT) {
507 case MVT::isVoid: break;
508 default:
509 RetVals.push_back(RetTyVT);
510 break;
511 case MVT::i1:
512 case MVT::i8:
513 case MVT::i16:
514 RetVals.push_back(MVT::i32);
515 break;
516 case MVT::f32:
517 if (X86ScalarSSE)
518 RetVals.push_back(MVT::f32);
519 else
520 RetVals.push_back(MVT::f64);
521 break;
522 case MVT::i64:
523 RetVals.push_back(MVT::i32);
524 RetVals.push_back(MVT::i32);
525 break;
526 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000527
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000528 std::vector<MVT::ValueType> NodeTys;
529 NodeTys.push_back(MVT::Other); // Returns a chain
530 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
531 std::vector<SDOperand> Ops;
532 Ops.push_back(Chain);
533 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000534
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000535 // FIXME: Do not generate X86ISD::TAILCALL for now.
536 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
537 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000538
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000539 NodeTys.clear();
540 NodeTys.push_back(MVT::Other); // Returns a chain
541 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
542 Ops.clear();
543 Ops.push_back(Chain);
544 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
545 Ops.push_back(DAG.getConstant(0, getPointerTy()));
546 Ops.push_back(InFlag);
547 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
548 InFlag = Chain.getValue(1);
549
550 SDOperand RetVal;
551 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000552 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000553 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000554 case MVT::i1:
555 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000556 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
557 Chain = RetVal.getValue(1);
558 if (RetTyVT == MVT::i1)
559 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
560 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000561 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000562 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
563 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000564 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000565 case MVT::i32:
566 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
567 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000568 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000569 case MVT::i64: {
570 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
571 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
572 Lo.getValue(2));
573 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
574 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000575 break;
576 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000577 case MVT::f32:
578 case MVT::f64: {
579 std::vector<MVT::ValueType> Tys;
580 Tys.push_back(MVT::f64);
581 Tys.push_back(MVT::Other);
582 Tys.push_back(MVT::Flag);
583 std::vector<SDOperand> Ops;
584 Ops.push_back(Chain);
585 Ops.push_back(InFlag);
586 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
587 Chain = RetVal.getValue(1);
588 InFlag = RetVal.getValue(2);
589 if (X86ScalarSSE) {
590 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
591 // shouldn't be necessary except that RFP cannot be live across
592 // multiple blocks. When stackifier is fixed, they can be uncoupled.
593 MachineFunction &MF = DAG.getMachineFunction();
594 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
595 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
596 Tys.clear();
597 Tys.push_back(MVT::Other);
598 Ops.clear();
599 Ops.push_back(Chain);
600 Ops.push_back(RetVal);
601 Ops.push_back(StackSlot);
602 Ops.push_back(DAG.getValueType(RetTyVT));
603 Ops.push_back(InFlag);
604 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
605 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
606 DAG.getSrcValue(NULL));
607 Chain = RetVal.getValue(1);
608 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000609
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000610 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
611 // FIXME: we would really like to remember that this FP_ROUND
612 // operation is okay to eliminate if we allow excess FP precision.
613 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
614 break;
615 }
616 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000618
619 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000620}
621
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000622//===----------------------------------------------------------------------===//
623// Fast Calling Convention implementation
624//===----------------------------------------------------------------------===//
625//
626// The X86 'fast' calling convention passes up to two integer arguments in
627// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
628// and requires that the callee pop its arguments off the stack (allowing proper
629// tail calls), and has the same return value conventions as C calling convs.
630//
631// This calling convention always arranges for the callee pop value to be 8n+4
632// bytes, which is needed for tail recursion elimination and stack alignment
633// reasons.
634//
635// Note that this can be enhanced in the future to pass fp vals in registers
636// (when we have a global fp allocator) and do other tricks.
637//
638
639/// AddLiveIn - This helper function adds the specified physical register to the
640/// MachineFunction as a live in value. It also creates a corresponding virtual
641/// register for it.
642static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
643 TargetRegisterClass *RC) {
644 assert(RC->contains(PReg) && "Not the correct regclass!");
645 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
646 MF.addLiveIn(PReg, VReg);
647 return VReg;
648}
649
Chris Lattner89fad2c2006-03-17 17:27:47 +0000650// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
651// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
652// EDX". Anything more is illegal.
653//
654// FIXME: The linscan register allocator currently has problem with
655// coallescing. At the time of this writing, whenever it decides to coallesce
656// a physreg with a virtreg, this increases the size of the physreg's live
657// range, and the live range cannot ever be reduced. This causes problems if
658// too many physregs are coalleced with virtregs, which can cause the register
659// allocator to wedge itself.
660//
661// This code triggers this problem more often if we pass args in registers,
662// so disable it until this is fixed.
663//
664// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
665// about code being dead.
666//
667static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000668
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000669
670std::vector<SDOperand>
671X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
672 std::vector<SDOperand> ArgValues;
673
674 MachineFunction &MF = DAG.getMachineFunction();
675 MachineFrameInfo *MFI = MF.getFrameInfo();
676
677 // Add DAG nodes to load the arguments... On entry to a function the stack
678 // frame looks like this:
679 //
680 // [ESP] -- return address
681 // [ESP + 4] -- first nonreg argument (leftmost lexically)
682 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
683 // ...
684 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
685
686 // Keep track of the number of integer regs passed so far. This can be either
687 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
688 // used).
689 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000690
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000691 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
692 MVT::ValueType ObjectVT = getValueType(I->getType());
693 unsigned ArgIncrement = 4;
694 unsigned ObjSize = 0;
695 SDOperand ArgValue;
696
697 switch (ObjectVT) {
698 default: assert(0 && "Unhandled argument type!");
699 case MVT::i1:
700 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000701 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000702 if (!I->use_empty()) {
703 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
704 X86::R8RegisterClass);
705 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
706 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000707 if (ObjectVT == MVT::i1)
708 // FIXME: Should insert a assertzext here.
709 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000710 }
711 ++NumIntRegs;
712 break;
713 }
714
715 ObjSize = 1;
716 break;
717 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000718 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000719 if (!I->use_empty()) {
720 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
721 X86::R16RegisterClass);
722 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
723 DAG.setRoot(ArgValue.getValue(1));
724 }
725 ++NumIntRegs;
726 break;
727 }
728 ObjSize = 2;
729 break;
730 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000731 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000732 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000733 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000734 X86::R32RegisterClass);
735 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
736 DAG.setRoot(ArgValue.getValue(1));
737 }
738 ++NumIntRegs;
739 break;
740 }
741 ObjSize = 4;
742 break;
743 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000744 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000745 if (!I->use_empty()) {
746 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
747 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
748
749 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
750 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
751 DAG.setRoot(Hi.getValue(1));
752
753 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
754 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000755 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000756 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000757 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000758 if (!I->use_empty()) {
759 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
760 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
761 DAG.setRoot(Low.getValue(1));
762
763 // Load the high part from memory.
764 // Create the frame index object for this incoming parameter...
765 int FI = MFI->CreateFixedObject(4, ArgOffset);
766 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
767 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
768 DAG.getSrcValue(NULL));
769 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
770 }
771 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000772 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000773 break;
774 }
775 ObjSize = ArgIncrement = 8;
776 break;
777 case MVT::f32: ObjSize = 4; break;
778 case MVT::f64: ObjSize = ArgIncrement = 8; break;
779 }
780
781 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
782 // dead loads.
783 if (ObjSize && !I->use_empty()) {
784 // Create the frame index object for this incoming parameter...
785 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
786
787 // Create the SelectionDAG nodes corresponding to a load from this
788 // parameter.
789 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
790
791 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
792 DAG.getSrcValue(NULL));
793 } else if (ArgValue.Val == 0) {
794 if (MVT::isInteger(ObjectVT))
795 ArgValue = DAG.getConstant(0, ObjectVT);
796 else
797 ArgValue = DAG.getConstantFP(0, ObjectVT);
798 }
799 ArgValues.push_back(ArgValue);
800
801 if (ObjSize)
802 ArgOffset += ArgIncrement; // Move on to the next argument.
803 }
804
805 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
806 // arguments and the arguments after the retaddr has been pushed are aligned.
807 if ((ArgOffset & 7) == 0)
808 ArgOffset += 4;
809
810 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
811 ReturnAddrIndex = 0; // No return address slot generated yet.
812 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
813 BytesCallerReserves = 0;
814
815 // Finally, inform the code generator which regs we return values in.
816 switch (getValueType(F.getReturnType())) {
817 default: assert(0 && "Unknown type!");
818 case MVT::isVoid: break;
819 case MVT::i1:
820 case MVT::i8:
821 case MVT::i16:
822 case MVT::i32:
823 MF.addLiveOut(X86::EAX);
824 break;
825 case MVT::i64:
826 MF.addLiveOut(X86::EAX);
827 MF.addLiveOut(X86::EDX);
828 break;
829 case MVT::f32:
830 case MVT::f64:
831 MF.addLiveOut(X86::ST0);
832 break;
833 }
834 return ArgValues;
835}
836
837std::pair<SDOperand, SDOperand>
838X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
839 bool isTailCall, SDOperand Callee,
840 ArgListTy &Args, SelectionDAG &DAG) {
841 // Count how many bytes are to be pushed on the stack.
842 unsigned NumBytes = 0;
843
844 // Keep track of the number of integer regs passed so far. This can be either
845 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
846 // used).
847 unsigned NumIntRegs = 0;
848
849 for (unsigned i = 0, e = Args.size(); i != e; ++i)
850 switch (getValueType(Args[i].second)) {
851 default: assert(0 && "Unknown value type!");
852 case MVT::i1:
853 case MVT::i8:
854 case MVT::i16:
855 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000856 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000857 ++NumIntRegs;
858 break;
859 }
860 // fall through
861 case MVT::f32:
862 NumBytes += 4;
863 break;
864 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000865 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
866 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000867 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000868 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
869 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000870 NumBytes += 4;
871 break;
872 }
873
874 // fall through
875 case MVT::f64:
876 NumBytes += 8;
877 break;
878 }
879
880 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
881 // arguments and the arguments after the retaddr has been pushed are aligned.
882 if ((NumBytes & 7) == 0)
883 NumBytes += 4;
884
Chris Lattner94dd2922006-02-13 09:00:43 +0000885 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000886
887 // Arguments go on the stack in reverse order, as specified by the ABI.
888 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000889 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000890 NumIntRegs = 0;
891 std::vector<SDOperand> Stores;
892 std::vector<SDOperand> RegValuesToPass;
893 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
894 switch (getValueType(Args[i].second)) {
895 default: assert(0 && "Unexpected ValueType for argument!");
896 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000897 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
898 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000899 case MVT::i8:
900 case MVT::i16:
901 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000902 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000903 RegValuesToPass.push_back(Args[i].first);
904 ++NumIntRegs;
905 break;
906 }
907 // Fall through
908 case MVT::f32: {
909 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
910 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
911 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
912 Args[i].first, PtrOff,
913 DAG.getSrcValue(NULL)));
914 ArgOffset += 4;
915 break;
916 }
917 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000918 // Can pass (at least) part of it in regs?
919 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000920 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
921 Args[i].first, DAG.getConstant(1, MVT::i32));
922 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
923 Args[i].first, DAG.getConstant(0, MVT::i32));
924 RegValuesToPass.push_back(Lo);
925 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000926
927 // Pass both parts in regs?
928 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000929 RegValuesToPass.push_back(Hi);
930 ++NumIntRegs;
931 } else {
932 // Pass the high part in memory.
933 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
934 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
935 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
936 Hi, PtrOff, DAG.getSrcValue(NULL)));
937 ArgOffset += 4;
938 }
939 break;
940 }
941 // Fall through
942 case MVT::f64:
943 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
944 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
945 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
946 Args[i].first, PtrOff,
947 DAG.getSrcValue(NULL)));
948 ArgOffset += 8;
949 break;
950 }
951 }
952 if (!Stores.empty())
953 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
954
955 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
956 // arguments and the arguments after the retaddr has been pushed are aligned.
957 if ((ArgOffset & 7) == 0)
958 ArgOffset += 4;
959
960 std::vector<MVT::ValueType> RetVals;
961 MVT::ValueType RetTyVT = getValueType(RetTy);
962
963 RetVals.push_back(MVT::Other);
964
965 // The result values produced have to be legal. Promote the result.
966 switch (RetTyVT) {
967 case MVT::isVoid: break;
968 default:
969 RetVals.push_back(RetTyVT);
970 break;
971 case MVT::i1:
972 case MVT::i8:
973 case MVT::i16:
974 RetVals.push_back(MVT::i32);
975 break;
976 case MVT::f32:
977 if (X86ScalarSSE)
978 RetVals.push_back(MVT::f32);
979 else
980 RetVals.push_back(MVT::f64);
981 break;
982 case MVT::i64:
983 RetVals.push_back(MVT::i32);
984 RetVals.push_back(MVT::i32);
985 break;
986 }
987
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000988 // Build a sequence of copy-to-reg nodes chained together with token chain
989 // and flag operands which copy the outgoing args into registers.
990 SDOperand InFlag;
991 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
992 unsigned CCReg;
993 SDOperand RegToPass = RegValuesToPass[i];
994 switch (RegToPass.getValueType()) {
995 default: assert(0 && "Bad thing to pass in regs");
996 case MVT::i8:
997 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000998 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000999 case MVT::i16:
1000 CCReg = (i == 0) ? X86::AX : X86::DX;
1001 break;
1002 case MVT::i32:
1003 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1004 break;
1005 }
1006
1007 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1008 InFlag = Chain.getValue(1);
1009 }
1010
1011 std::vector<MVT::ValueType> NodeTys;
1012 NodeTys.push_back(MVT::Other); // Returns a chain
1013 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1014 std::vector<SDOperand> Ops;
1015 Ops.push_back(Chain);
1016 Ops.push_back(Callee);
1017 if (InFlag.Val)
1018 Ops.push_back(InFlag);
1019
1020 // FIXME: Do not generate X86ISD::TAILCALL for now.
1021 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1022 InFlag = Chain.getValue(1);
1023
1024 NodeTys.clear();
1025 NodeTys.push_back(MVT::Other); // Returns a chain
1026 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1027 Ops.clear();
1028 Ops.push_back(Chain);
1029 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1030 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1031 Ops.push_back(InFlag);
1032 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1033 InFlag = Chain.getValue(1);
1034
1035 SDOperand RetVal;
1036 if (RetTyVT != MVT::isVoid) {
1037 switch (RetTyVT) {
1038 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001039 case MVT::i1:
1040 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001041 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1042 Chain = RetVal.getValue(1);
1043 if (RetTyVT == MVT::i1)
1044 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1045 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001046 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001047 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1048 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001049 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001050 case MVT::i32:
1051 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1052 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001053 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001054 case MVT::i64: {
1055 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1056 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1057 Lo.getValue(2));
1058 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1059 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001060 break;
1061 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001062 case MVT::f32:
1063 case MVT::f64: {
1064 std::vector<MVT::ValueType> Tys;
1065 Tys.push_back(MVT::f64);
1066 Tys.push_back(MVT::Other);
1067 Tys.push_back(MVT::Flag);
1068 std::vector<SDOperand> Ops;
1069 Ops.push_back(Chain);
1070 Ops.push_back(InFlag);
1071 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1072 Chain = RetVal.getValue(1);
1073 InFlag = RetVal.getValue(2);
1074 if (X86ScalarSSE) {
1075 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1076 // shouldn't be necessary except that RFP cannot be live across
1077 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1078 MachineFunction &MF = DAG.getMachineFunction();
1079 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1080 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1081 Tys.clear();
1082 Tys.push_back(MVT::Other);
1083 Ops.clear();
1084 Ops.push_back(Chain);
1085 Ops.push_back(RetVal);
1086 Ops.push_back(StackSlot);
1087 Ops.push_back(DAG.getValueType(RetTyVT));
1088 Ops.push_back(InFlag);
1089 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1090 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1091 DAG.getSrcValue(NULL));
1092 Chain = RetVal.getValue(1);
1093 }
Evan Chengd9558e02006-01-06 00:43:03 +00001094
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001095 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1096 // FIXME: we would really like to remember that this FP_ROUND
1097 // operation is okay to eliminate if we allow excess FP precision.
1098 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1099 break;
1100 }
1101 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001102 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001103
1104 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001105}
1106
1107SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1108 if (ReturnAddrIndex == 0) {
1109 // Set up a frame object for the return address.
1110 MachineFunction &MF = DAG.getMachineFunction();
1111 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1112 }
1113
1114 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1115}
1116
1117
1118
1119std::pair<SDOperand, SDOperand> X86TargetLowering::
1120LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1121 SelectionDAG &DAG) {
1122 SDOperand Result;
1123 if (Depth) // Depths > 0 not supported yet!
1124 Result = DAG.getConstant(0, getPointerTy());
1125 else {
1126 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1127 if (!isFrameAddress)
1128 // Just load the return address
1129 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1130 DAG.getSrcValue(NULL));
1131 else
1132 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1133 DAG.getConstant(4, MVT::i32));
1134 }
1135 return std::make_pair(Result, Chain);
1136}
1137
Evan Cheng4a460802006-01-11 00:33:36 +00001138/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1139/// which corresponds to the condition code.
1140static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1141 switch (X86CC) {
1142 default: assert(0 && "Unknown X86 conditional code!");
1143 case X86ISD::COND_A: return X86::JA;
1144 case X86ISD::COND_AE: return X86::JAE;
1145 case X86ISD::COND_B: return X86::JB;
1146 case X86ISD::COND_BE: return X86::JBE;
1147 case X86ISD::COND_E: return X86::JE;
1148 case X86ISD::COND_G: return X86::JG;
1149 case X86ISD::COND_GE: return X86::JGE;
1150 case X86ISD::COND_L: return X86::JL;
1151 case X86ISD::COND_LE: return X86::JLE;
1152 case X86ISD::COND_NE: return X86::JNE;
1153 case X86ISD::COND_NO: return X86::JNO;
1154 case X86ISD::COND_NP: return X86::JNP;
1155 case X86ISD::COND_NS: return X86::JNS;
1156 case X86ISD::COND_O: return X86::JO;
1157 case X86ISD::COND_P: return X86::JP;
1158 case X86ISD::COND_S: return X86::JS;
1159 }
1160}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001161
Evan Cheng6dfa9992006-01-30 23:41:35 +00001162/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1163/// specific condition code. It returns a false if it cannot do a direct
1164/// translation. X86CC is the translated CondCode. Flip is set to true if the
1165/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001166static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1167 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001168 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001169 Flip = false;
1170 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001171 if (!isFP) {
1172 switch (SetCCOpcode) {
1173 default: break;
1174 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1175 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1176 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1177 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1178 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1179 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1180 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1181 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1182 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1183 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1184 }
1185 } else {
1186 // On a floating point condition, the flags are set as follows:
1187 // ZF PF CF op
1188 // 0 | 0 | 0 | X > Y
1189 // 0 | 0 | 1 | X < Y
1190 // 1 | 0 | 0 | X == Y
1191 // 1 | 1 | 1 | unordered
1192 switch (SetCCOpcode) {
1193 default: break;
1194 case ISD::SETUEQ:
1195 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001196 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001197 case ISD::SETOGT:
1198 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001199 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001200 case ISD::SETOGE:
1201 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001202 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001203 case ISD::SETULT:
1204 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001205 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001206 case ISD::SETULE:
1207 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1208 case ISD::SETONE:
1209 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1210 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1211 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1212 }
1213 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001214
1215 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001216}
1217
Evan Cheng4a460802006-01-11 00:33:36 +00001218/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1219/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001220/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001221static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001222 switch (X86CC) {
1223 default:
1224 return false;
1225 case X86ISD::COND_B:
1226 case X86ISD::COND_BE:
1227 case X86ISD::COND_E:
1228 case X86ISD::COND_P:
1229 case X86ISD::COND_A:
1230 case X86ISD::COND_AE:
1231 case X86ISD::COND_NE:
1232 case X86ISD::COND_NP:
1233 return true;
1234 }
1235}
1236
Evan Cheng4a460802006-01-11 00:33:36 +00001237MachineBasicBlock *
1238X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1239 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001240 switch (MI->getOpcode()) {
1241 default: assert(false && "Unexpected instr type to insert");
1242 case X86::CMOV_FR32:
1243 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001244 // To "insert" a SELECT_CC instruction, we actually have to insert the
1245 // diamond control-flow pattern. The incoming instruction knows the
1246 // destination vreg to set, the condition code register to branch on, the
1247 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001248 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1249 ilist<MachineBasicBlock>::iterator It = BB;
1250 ++It;
1251
1252 // thisMBB:
1253 // ...
1254 // TrueVal = ...
1255 // cmpTY ccX, r1, r2
1256 // bCC copy1MBB
1257 // fallthrough --> copy0MBB
1258 MachineBasicBlock *thisMBB = BB;
1259 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1260 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1261 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1262 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1263 MachineFunction *F = BB->getParent();
1264 F->getBasicBlockList().insert(It, copy0MBB);
1265 F->getBasicBlockList().insert(It, sinkMBB);
1266 // Update machine-CFG edges
1267 BB->addSuccessor(copy0MBB);
1268 BB->addSuccessor(sinkMBB);
1269
1270 // copy0MBB:
1271 // %FalseValue = ...
1272 // # fallthrough to sinkMBB
1273 BB = copy0MBB;
1274
1275 // Update machine-CFG edges
1276 BB->addSuccessor(sinkMBB);
1277
1278 // sinkMBB:
1279 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1280 // ...
1281 BB = sinkMBB;
1282 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1283 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1284 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001285
Evan Cheng0cc39452006-01-16 21:21:29 +00001286 delete MI; // The pseudo instruction is gone now.
1287 return BB;
1288 }
Evan Cheng4a460802006-01-11 00:33:36 +00001289
Evan Cheng0cc39452006-01-16 21:21:29 +00001290 case X86::FP_TO_INT16_IN_MEM:
1291 case X86::FP_TO_INT32_IN_MEM:
1292 case X86::FP_TO_INT64_IN_MEM: {
1293 // Change the floating point control register to use "round towards zero"
1294 // mode when truncating to an integer value.
1295 MachineFunction *F = BB->getParent();
1296 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1297 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1298
1299 // Load the old value of the high byte of the control word...
1300 unsigned OldCW =
1301 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1302 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1303
1304 // Set the high part to be round to zero...
1305 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1306
1307 // Reload the modified control word now...
1308 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1309
1310 // Restore the memory image of control word to original value
1311 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1312
1313 // Get the X86 opcode to use.
1314 unsigned Opc;
1315 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001316 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001317 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1318 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1319 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1320 }
1321
1322 X86AddressMode AM;
1323 MachineOperand &Op = MI->getOperand(0);
1324 if (Op.isRegister()) {
1325 AM.BaseType = X86AddressMode::RegBase;
1326 AM.Base.Reg = Op.getReg();
1327 } else {
1328 AM.BaseType = X86AddressMode::FrameIndexBase;
1329 AM.Base.FrameIndex = Op.getFrameIndex();
1330 }
1331 Op = MI->getOperand(1);
1332 if (Op.isImmediate())
1333 AM.Scale = Op.getImmedValue();
1334 Op = MI->getOperand(2);
1335 if (Op.isImmediate())
1336 AM.IndexReg = Op.getImmedValue();
1337 Op = MI->getOperand(3);
1338 if (Op.isGlobalAddress()) {
1339 AM.GV = Op.getGlobal();
1340 } else {
1341 AM.Disp = Op.getImmedValue();
1342 }
1343 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1344
1345 // Reload the original control word now.
1346 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1347
1348 delete MI; // The pseudo instruction is gone now.
1349 return BB;
1350 }
1351 }
Evan Cheng4a460802006-01-11 00:33:36 +00001352}
1353
1354
1355//===----------------------------------------------------------------------===//
1356// X86 Custom Lowering Hooks
1357//===----------------------------------------------------------------------===//
1358
Evan Cheng30b37b52006-03-13 23:18:16 +00001359/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1360/// load. For Darwin, external and weak symbols are indirect, loading the value
1361/// at address GV rather then the value of GV itself. This means that the
1362/// GlobalAddress must be in the base or index register of the address, not the
1363/// GV offset field.
1364static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1365 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1366 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1367}
1368
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001369/// LowerOperation - Provide custom lowering hooks for some operations.
1370///
1371SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1372 switch (Op.getOpcode()) {
1373 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001374 case ISD::SHL_PARTS:
1375 case ISD::SRA_PARTS:
1376 case ISD::SRL_PARTS: {
1377 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1378 "Not an i64 shift!");
1379 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1380 SDOperand ShOpLo = Op.getOperand(0);
1381 SDOperand ShOpHi = Op.getOperand(1);
1382 SDOperand ShAmt = Op.getOperand(2);
1383 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001384 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001385 : DAG.getConstant(0, MVT::i32);
1386
1387 SDOperand Tmp2, Tmp3;
1388 if (Op.getOpcode() == ISD::SHL_PARTS) {
1389 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1390 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1391 } else {
1392 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001393 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001394 }
1395
1396 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1397 ShAmt, DAG.getConstant(32, MVT::i8));
1398
1399 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001400 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001401
1402 std::vector<MVT::ValueType> Tys;
1403 Tys.push_back(MVT::i32);
1404 Tys.push_back(MVT::Flag);
1405 std::vector<SDOperand> Ops;
1406 if (Op.getOpcode() == ISD::SHL_PARTS) {
1407 Ops.push_back(Tmp2);
1408 Ops.push_back(Tmp3);
1409 Ops.push_back(CC);
1410 Ops.push_back(InFlag);
1411 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1412 InFlag = Hi.getValue(1);
1413
1414 Ops.clear();
1415 Ops.push_back(Tmp3);
1416 Ops.push_back(Tmp1);
1417 Ops.push_back(CC);
1418 Ops.push_back(InFlag);
1419 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1420 } else {
1421 Ops.push_back(Tmp2);
1422 Ops.push_back(Tmp3);
1423 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001424 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001425 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1426 InFlag = Lo.getValue(1);
1427
1428 Ops.clear();
1429 Ops.push_back(Tmp3);
1430 Ops.push_back(Tmp1);
1431 Ops.push_back(CC);
1432 Ops.push_back(InFlag);
1433 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1434 }
1435
1436 Tys.clear();
1437 Tys.push_back(MVT::i32);
1438 Tys.push_back(MVT::i32);
1439 Ops.clear();
1440 Ops.push_back(Lo);
1441 Ops.push_back(Hi);
1442 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1443 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001444 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001445 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001446 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001447 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001448
1449 SDOperand Result;
1450 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1451 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001452 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001453 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001454 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001455 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1456 DAG.getEntryNode(), Op.getOperand(0),
1457 StackSlot, DAG.getSrcValue(NULL));
1458
1459 // Build the FILD
1460 std::vector<MVT::ValueType> Tys;
1461 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001462 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001463 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001464 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001465 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001466 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001467 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001468 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1469 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001470
1471 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001472 Chain = Result.getValue(1);
1473 SDOperand InFlag = Result.getValue(2);
1474
Evan Chenge3de85b2006-02-04 02:20:30 +00001475 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001476 // shouldn't be necessary except that RFP cannot be live across
1477 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1478 MachineFunction &MF = DAG.getMachineFunction();
1479 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1480 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1481 std::vector<MVT::ValueType> Tys;
1482 Tys.push_back(MVT::Other);
1483 std::vector<SDOperand> Ops;
1484 Ops.push_back(Chain);
1485 Ops.push_back(Result);
1486 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001487 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001488 Ops.push_back(InFlag);
1489 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1490 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1491 DAG.getSrcValue(NULL));
1492 }
1493
Evan Chenga3195e82006-01-12 22:54:21 +00001494 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001495 }
1496 case ISD::FP_TO_SINT: {
1497 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001498 "Unknown FP_TO_SINT to lower!");
1499 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1500 // stack slot.
1501 MachineFunction &MF = DAG.getMachineFunction();
1502 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1503 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1504 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1505
1506 unsigned Opc;
1507 switch (Op.getValueType()) {
1508 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1509 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1510 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1511 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1512 }
1513
Evan Cheng6dab0532006-01-30 08:02:57 +00001514 SDOperand Chain = DAG.getEntryNode();
1515 SDOperand Value = Op.getOperand(0);
1516 if (X86ScalarSSE) {
1517 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1518 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1519 DAG.getSrcValue(0));
1520 std::vector<MVT::ValueType> Tys;
1521 Tys.push_back(MVT::f64);
1522 Tys.push_back(MVT::Other);
1523 std::vector<SDOperand> Ops;
1524 Ops.push_back(Chain);
1525 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001526 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001527 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1528 Chain = Value.getValue(1);
1529 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1530 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1531 }
1532
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001533 // Build the FP_TO_INT*_IN_MEM
1534 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001535 Ops.push_back(Chain);
1536 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001537 Ops.push_back(StackSlot);
1538 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1539
1540 // Load the result.
1541 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1542 DAG.getSrcValue(NULL));
1543 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001544 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001545 std::vector<MVT::ValueType> Tys;
1546 Tys.push_back(MVT::Other);
1547 Tys.push_back(MVT::Flag);
1548 std::vector<SDOperand> Ops;
1549 Ops.push_back(Op.getOperand(0));
1550 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001551 Ops.clear();
1552 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1553 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1554 MVT::i32, Ops[0].getValue(2)));
1555 Ops.push_back(Ops[1].getValue(1));
1556 Tys[0] = Tys[1] = MVT::i32;
1557 Tys.push_back(MVT::Other);
1558 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001559 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001560 case ISD::FABS: {
1561 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001562 const Type *OpNTy = MVT::getTypeForValueType(VT);
1563 std::vector<Constant*> CV;
1564 if (VT == MVT::f64) {
1565 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1566 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1567 } else {
1568 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1569 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1570 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1571 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1572 }
1573 Constant *CS = ConstantStruct::get(CV);
1574 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1575 SDOperand Mask
1576 = DAG.getNode(X86ISD::LOAD_PACK,
1577 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001578 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1579 }
Evan Cheng223547a2006-01-31 22:28:30 +00001580 case ISD::FNEG: {
1581 MVT::ValueType VT = Op.getValueType();
1582 const Type *OpNTy = MVT::getTypeForValueType(VT);
1583 std::vector<Constant*> CV;
1584 if (VT == MVT::f64) {
1585 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1586 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1587 } else {
1588 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1589 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1590 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1591 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1592 }
1593 Constant *CS = ConstantStruct::get(CV);
1594 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1595 SDOperand Mask
1596 = DAG.getNode(X86ISD::LOAD_PACK,
1597 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1598 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1599 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001600 case ISD::SETCC: {
1601 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001602 SDOperand Cond;
1603 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001604 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1605 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001606 bool Flip;
1607 unsigned X86CC;
1608 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1609 if (Flip)
1610 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1611 Op.getOperand(1), Op.getOperand(0));
1612 else
1613 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1614 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001615 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1616 DAG.getConstant(X86CC, MVT::i8), Cond);
1617 } else {
1618 assert(isFP && "Illegal integer SetCC!");
1619
Evan Cheng6dfa9992006-01-30 23:41:35 +00001620 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1621 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001622 std::vector<MVT::ValueType> Tys;
1623 std::vector<SDOperand> Ops;
1624 switch (SetCCOpcode) {
1625 default: assert(false && "Illegal floating point SetCC!");
1626 case ISD::SETOEQ: { // !PF & ZF
1627 Tys.push_back(MVT::i8);
1628 Tys.push_back(MVT::Flag);
1629 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1630 Ops.push_back(Cond);
1631 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1632 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1633 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1634 Tmp1.getValue(1));
1635 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1636 }
Evan Chengd9558e02006-01-06 00:43:03 +00001637 case ISD::SETUNE: { // PF | !ZF
1638 Tys.push_back(MVT::i8);
1639 Tys.push_back(MVT::Flag);
1640 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1641 Ops.push_back(Cond);
1642 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1643 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1644 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1645 Tmp1.getValue(1));
1646 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1647 }
1648 }
1649 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001650 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001651 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001652 MVT::ValueType VT = Op.getValueType();
1653 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001654 bool isFPStack = isFP && !X86ScalarSSE;
1655 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001656 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001657 SDOperand Op0 = Op.getOperand(0);
1658 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001659 if (Op0.getOpcode() == ISD::SETCC)
1660 Op0 = LowerOperation(Op0, DAG);
1661
Evan Chengaaca22c2006-01-10 20:26:56 +00001662 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001663 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1664 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1665 // have another use it will be eliminated.
1666 // If the X86ISD::SETCC has more than one use, then it's probably better
1667 // to use a test instead of duplicating the X86ISD::CMP (for register
1668 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001669 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1670 if (!Op0.hasOneUse()) {
1671 std::vector<MVT::ValueType> Tys;
1672 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1673 Tys.push_back(Op0.Val->getValueType(i));
1674 std::vector<SDOperand> Ops;
1675 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1676 Ops.push_back(Op0.getOperand(i));
1677 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1678 }
1679
Evan Cheng1bcee362006-01-13 01:03:02 +00001680 CC = Op0.getOperand(0);
1681 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001682 // Make a copy as flag result cannot be used by more than one.
1683 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1684 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001685 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001686 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001687 } else
1688 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001689 } else
1690 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001691
Evan Cheng189d01e2006-01-13 01:06:49 +00001692 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001693 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001694 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001695 }
Evan Chenge3413162006-01-09 18:33:28 +00001696
1697 std::vector<MVT::ValueType> Tys;
1698 Tys.push_back(Op.getValueType());
1699 Tys.push_back(MVT::Flag);
1700 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001701 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1702 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001703 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001704 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001705 Ops.push_back(CC);
1706 Ops.push_back(Cond);
1707 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001708 }
Evan Cheng898101c2005-12-19 23:12:38 +00001709 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001710 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001711 SDOperand Cond = Op.getOperand(1);
1712 SDOperand Dest = Op.getOperand(2);
1713 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001714 if (Cond.getOpcode() == ISD::SETCC)
1715 Cond = LowerOperation(Cond, DAG);
1716
Evan Chengd5781fc2005-12-21 20:21:51 +00001717 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001718 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1719 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1720 // have another use it will be eliminated.
1721 // If the X86ISD::SETCC has more than one use, then it's probably better
1722 // to use a test instead of duplicating the X86ISD::CMP (for register
1723 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001724 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1725 if (!Cond.hasOneUse()) {
1726 std::vector<MVT::ValueType> Tys;
1727 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1728 Tys.push_back(Cond.Val->getValueType(i));
1729 std::vector<SDOperand> Ops;
1730 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1731 Ops.push_back(Cond.getOperand(i));
1732 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1733 }
1734
Evan Cheng1bcee362006-01-13 01:03:02 +00001735 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001736 Cond = Cond.getOperand(1);
1737 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001738 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001739 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001740 } else
1741 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001742 } else
1743 addTest = true;
1744
1745 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001746 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001747 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1748 }
1749 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1750 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1751 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001752 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001753 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001754 SDOperand Chain = Op.getOperand(0);
1755 unsigned Align =
1756 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1757 if (Align == 0) Align = 1;
1758
Evan Cheng18a84522006-02-16 00:21:07 +00001759 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1760 // If not DWORD aligned, call memset if size is less than the threshold.
1761 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001762 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001763 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001764 MVT::ValueType IntPtr = getPointerTy();
1765 const Type *IntPtrTy = getTargetData().getIntPtrType();
1766 std::vector<std::pair<SDOperand, const Type*> > Args;
1767 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1768 // Extend the ubyte argument to be an int value for the call.
1769 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1770 Args.push_back(std::make_pair(Val, IntPtrTy));
1771 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1772 std::pair<SDOperand,SDOperand> CallResult =
1773 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1774 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1775 return CallResult.second;
1776 }
1777
Evan Cheng67f92a72006-01-11 22:15:48 +00001778 MVT::ValueType AVT;
1779 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001780 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1781 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001782 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001783 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001784 unsigned ValReg;
1785 unsigned Val = ValC->getValue() & 255;
1786
1787 // If the value is a constant, then we can potentially use larger sets.
1788 switch (Align & 3) {
1789 case 2: // WORD aligned
1790 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001791 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1792 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001793 Val = (Val << 8) | Val;
1794 ValReg = X86::AX;
1795 break;
1796 case 0: // DWORD aligned
1797 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001798 if (I) {
1799 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1800 BytesLeft = I->getValue() % 4;
1801 } else {
1802 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1803 DAG.getConstant(2, MVT::i8));
1804 TwoRepStos = true;
1805 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001806 Val = (Val << 8) | Val;
1807 Val = (Val << 16) | Val;
1808 ValReg = X86::EAX;
1809 break;
1810 default: // Byte aligned
1811 AVT = MVT::i8;
1812 Count = Op.getOperand(3);
1813 ValReg = X86::AL;
1814 break;
1815 }
1816
1817 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1818 InFlag);
1819 InFlag = Chain.getValue(1);
1820 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001821 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001822 Count = Op.getOperand(3);
1823 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1824 InFlag = Chain.getValue(1);
1825 }
1826
1827 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1828 InFlag = Chain.getValue(1);
1829 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1830 InFlag = Chain.getValue(1);
1831
Evan Chengff909922006-03-07 23:29:39 +00001832 std::vector<MVT::ValueType> Tys;
1833 Tys.push_back(MVT::Other);
1834 Tys.push_back(MVT::Flag);
1835 std::vector<SDOperand> Ops;
1836 Ops.push_back(Chain);
1837 Ops.push_back(DAG.getValueType(AVT));
1838 Ops.push_back(InFlag);
1839 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1840
1841 if (TwoRepStos) {
1842 InFlag = Chain.getValue(1);
1843 Count = Op.getOperand(3);
1844 MVT::ValueType CVT = Count.getValueType();
1845 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1846 DAG.getConstant(3, CVT));
1847 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1848 InFlag = Chain.getValue(1);
1849 Tys.clear();
1850 Tys.push_back(MVT::Other);
1851 Tys.push_back(MVT::Flag);
1852 Ops.clear();
1853 Ops.push_back(Chain);
1854 Ops.push_back(DAG.getValueType(MVT::i8));
1855 Ops.push_back(InFlag);
1856 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1857 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001858 // Issue stores for the last 1 - 3 bytes.
1859 SDOperand Value;
1860 unsigned Val = ValC->getValue() & 255;
1861 unsigned Offset = I->getValue() - BytesLeft;
1862 SDOperand DstAddr = Op.getOperand(1);
1863 MVT::ValueType AddrVT = DstAddr.getValueType();
1864 if (BytesLeft >= 2) {
1865 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1866 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1867 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1868 DAG.getConstant(Offset, AddrVT)),
1869 DAG.getSrcValue(NULL));
1870 BytesLeft -= 2;
1871 Offset += 2;
1872 }
1873
1874 if (BytesLeft == 1) {
1875 Value = DAG.getConstant(Val, MVT::i8);
1876 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1877 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1878 DAG.getConstant(Offset, AddrVT)),
1879 DAG.getSrcValue(NULL));
1880 }
1881 }
1882
1883 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001884 }
1885 case ISD::MEMCPY: {
1886 SDOperand Chain = Op.getOperand(0);
1887 unsigned Align =
1888 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1889 if (Align == 0) Align = 1;
1890
Evan Cheng18a84522006-02-16 00:21:07 +00001891 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1892 // If not DWORD aligned, call memcpy if size is less than the threshold.
1893 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001894 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001895 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001896 MVT::ValueType IntPtr = getPointerTy();
1897 const Type *IntPtrTy = getTargetData().getIntPtrType();
1898 std::vector<std::pair<SDOperand, const Type*> > Args;
1899 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1900 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1901 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1902 std::pair<SDOperand,SDOperand> CallResult =
1903 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1904 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1905 return CallResult.second;
1906 }
1907
Evan Cheng67f92a72006-01-11 22:15:48 +00001908 MVT::ValueType AVT;
1909 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001910 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001911 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001912 switch (Align & 3) {
1913 case 2: // WORD aligned
1914 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001915 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1916 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001917 break;
1918 case 0: // DWORD aligned
1919 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001920 if (I) {
1921 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1922 BytesLeft = I->getValue() % 4;
1923 } else {
1924 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1925 DAG.getConstant(2, MVT::i8));
1926 TwoRepMovs = true;
1927 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001928 break;
1929 default: // Byte aligned
1930 AVT = MVT::i8;
1931 Count = Op.getOperand(3);
1932 break;
1933 }
1934
Evan Cheng62bec2c2006-03-04 02:48:56 +00001935 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001936 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1937 InFlag = Chain.getValue(1);
1938 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1939 InFlag = Chain.getValue(1);
1940 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1941 InFlag = Chain.getValue(1);
1942
Evan Chengff909922006-03-07 23:29:39 +00001943 std::vector<MVT::ValueType> Tys;
1944 Tys.push_back(MVT::Other);
1945 Tys.push_back(MVT::Flag);
1946 std::vector<SDOperand> Ops;
1947 Ops.push_back(Chain);
1948 Ops.push_back(DAG.getValueType(AVT));
1949 Ops.push_back(InFlag);
1950 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1951
1952 if (TwoRepMovs) {
1953 InFlag = Chain.getValue(1);
1954 Count = Op.getOperand(3);
1955 MVT::ValueType CVT = Count.getValueType();
1956 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1957 DAG.getConstant(3, CVT));
1958 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1959 InFlag = Chain.getValue(1);
1960 Tys.clear();
1961 Tys.push_back(MVT::Other);
1962 Tys.push_back(MVT::Flag);
1963 Ops.clear();
1964 Ops.push_back(Chain);
1965 Ops.push_back(DAG.getValueType(MVT::i8));
1966 Ops.push_back(InFlag);
1967 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1968 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001969 // Issue loads and stores for the last 1 - 3 bytes.
1970 unsigned Offset = I->getValue() - BytesLeft;
1971 SDOperand DstAddr = Op.getOperand(1);
1972 MVT::ValueType DstVT = DstAddr.getValueType();
1973 SDOperand SrcAddr = Op.getOperand(2);
1974 MVT::ValueType SrcVT = SrcAddr.getValueType();
1975 SDOperand Value;
1976 if (BytesLeft >= 2) {
1977 Value = DAG.getLoad(MVT::i16, Chain,
1978 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1979 DAG.getConstant(Offset, SrcVT)),
1980 DAG.getSrcValue(NULL));
1981 Chain = Value.getValue(1);
1982 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1983 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1984 DAG.getConstant(Offset, DstVT)),
1985 DAG.getSrcValue(NULL));
1986 BytesLeft -= 2;
1987 Offset += 2;
1988 }
1989
1990 if (BytesLeft == 1) {
1991 Value = DAG.getLoad(MVT::i8, Chain,
1992 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1993 DAG.getConstant(Offset, SrcVT)),
1994 DAG.getSrcValue(NULL));
1995 Chain = Value.getValue(1);
1996 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1997 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1998 DAG.getConstant(Offset, DstVT)),
1999 DAG.getSrcValue(NULL));
2000 }
2001 }
2002
2003 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002004 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002005
2006 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2007 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2008 // one of the above mentioned nodes. It has to be wrapped because otherwise
2009 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2010 // be used to form addressing mode. These wrapped nodes will be selected
2011 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002012 case ISD::ConstantPool: {
2013 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002014 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2015 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2016 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00002017 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002018 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002019 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002020 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2021 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2022 }
2023
2024 return Result;
2025 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002026 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002027 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2028 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2029 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00002030 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00002031 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002032 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002033 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002034 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2035 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002036
2037 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002038 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002039 // the GlobalAddress must be in the base or index register of the address,
2040 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002041 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002042 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002043 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002044 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002045 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002046
Evan Cheng002fe9b2006-01-12 07:56:47 +00002047 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002048 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002049 case ISD::ExternalSymbol: {
2050 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2051 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2052 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2053 if (getTargetMachine().
2054 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2055 // With PIC, the address is actually $g + Offset.
2056 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2057 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2058 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2059 }
2060
2061 return Result;
2062 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002063 case ISD::VASTART: {
2064 // vastart just stores the address of the VarArgsFrameIndex slot into the
2065 // memory location argument.
2066 // FIXME: Replace MVT::i32 with PointerTy
2067 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2068 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2069 Op.getOperand(1), Op.getOperand(2));
2070 }
Nate Begemanee625572006-01-27 21:09:22 +00002071 case ISD::RET: {
2072 SDOperand Copy;
2073
2074 switch(Op.getNumOperands()) {
2075 default:
2076 assert(0 && "Do not know how to return this many arguments!");
2077 abort();
2078 case 1:
2079 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2080 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2081 case 2: {
2082 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2083 if (MVT::isInteger(ArgVT))
2084 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2085 SDOperand());
2086 else if (!X86ScalarSSE) {
2087 std::vector<MVT::ValueType> Tys;
2088 Tys.push_back(MVT::Other);
2089 Tys.push_back(MVT::Flag);
2090 std::vector<SDOperand> Ops;
2091 Ops.push_back(Op.getOperand(0));
2092 Ops.push_back(Op.getOperand(1));
2093 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2094 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002095 SDOperand MemLoc;
2096 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002097 SDOperand Value = Op.getOperand(1);
2098
Evan Cheng760df292006-02-01 01:19:32 +00002099 if (Value.getOpcode() == ISD::LOAD &&
2100 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002101 Chain = Value.getOperand(0);
2102 MemLoc = Value.getOperand(1);
2103 } else {
2104 // Spill the value to memory and reload it into top of stack.
2105 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2106 MachineFunction &MF = DAG.getMachineFunction();
2107 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2108 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2109 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2110 Value, MemLoc, DAG.getSrcValue(0));
2111 }
Nate Begemanee625572006-01-27 21:09:22 +00002112 std::vector<MVT::ValueType> Tys;
2113 Tys.push_back(MVT::f64);
2114 Tys.push_back(MVT::Other);
2115 std::vector<SDOperand> Ops;
2116 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002117 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002118 Ops.push_back(DAG.getValueType(ArgVT));
2119 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2120 Tys.clear();
2121 Tys.push_back(MVT::Other);
2122 Tys.push_back(MVT::Flag);
2123 Ops.clear();
2124 Ops.push_back(Copy.getValue(1));
2125 Ops.push_back(Copy);
2126 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2127 }
2128 break;
2129 }
2130 case 3:
2131 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2132 SDOperand());
2133 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2134 break;
2135 }
2136 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2137 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2138 Copy.getValue(1));
2139 }
Evan Cheng48090aa2006-03-21 23:01:21 +00002140 case ISD::SCALAR_TO_VECTOR: {
2141 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2142 return DAG.getNode(X86ISD::SCALAR_TO_VECTOR, Op.getValueType(), AnyExt);
2143 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002144 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002145}
Evan Cheng72261582005-12-20 06:22:03 +00002146
2147const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2148 switch (Opcode) {
2149 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002150 case X86ISD::SHLD: return "X86ISD::SHLD";
2151 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002152 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002153 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002154 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002155 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002156 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2157 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2158 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002159 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002160 case X86ISD::FST: return "X86ISD::FST";
2161 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002162 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002163 case X86ISD::CALL: return "X86ISD::CALL";
2164 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2165 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2166 case X86ISD::CMP: return "X86ISD::CMP";
2167 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002168 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002169 case X86ISD::CMOV: return "X86ISD::CMOV";
2170 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002171 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002172 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2173 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002174 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002175 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002176 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng48090aa2006-03-21 23:01:21 +00002177 case X86ISD::SCALAR_TO_VECTOR: return "X86ISD::SCALAR_TO_VECTOR";
Evan Cheng72261582005-12-20 06:22:03 +00002178 }
2179}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002180
Nate Begeman368e18d2006-02-16 21:11:51 +00002181void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2182 uint64_t Mask,
2183 uint64_t &KnownZero,
2184 uint64_t &KnownOne,
2185 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002186
2187 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002188 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002189
2190 switch (Opc) {
2191 default:
2192 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2193 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002194 case X86ISD::SETCC:
2195 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2196 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002197 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002198}
Chris Lattner259e97c2006-01-31 19:43:35 +00002199
2200std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002201getRegClassForInlineAsmConstraint(const std::string &Constraint,
2202 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002203 if (Constraint.size() == 1) {
2204 // FIXME: not handling fp-stack yet!
2205 // FIXME: not handling MMX registers yet ('y' constraint).
2206 switch (Constraint[0]) { // GCC X86 Constraint Letters
2207 default: break; // Unknown constriant letter
2208 case 'r': // GENERAL_REGS
2209 case 'R': // LEGACY_REGS
2210 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2211 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2212 case 'l': // INDEX_REGS
2213 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2214 X86::ESI, X86::EDI, X86::EBP, 0);
2215 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2216 case 'Q': // Q_REGS
2217 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2218 case 'x': // SSE_REGS if SSE1 allowed
2219 if (Subtarget->hasSSE1())
2220 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2221 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2222 0);
2223 return std::vector<unsigned>();
2224 case 'Y': // SSE_REGS if SSE2 allowed
2225 if (Subtarget->hasSSE2())
2226 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2227 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2228 0);
2229 return std::vector<unsigned>();
2230 }
2231 }
2232
Chris Lattner1efa40f2006-02-22 00:56:39 +00002233 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002234}
Evan Cheng30b37b52006-03-13 23:18:16 +00002235
2236/// isLegalAddressImmediate - Return true if the integer value or
2237/// GlobalValue can be used as the offset of the target addressing mode.
2238bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2239 // X86 allows a sign-extended 32-bit immediate field.
2240 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2241}
2242
2243bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2244 if (getTargetMachine().
2245 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2246 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2247 if (RModel == Reloc::Static)
2248 return true;
2249 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002250 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002251 else
2252 return false;
2253 } else
2254 return true;
2255}