blob: 0540f63af9df5017327d596c38bb16fdfa33ccba [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);
Evan Chengd30bf012006-03-01 01:11:20 +0000259 }
260
Evan Cheng470a6ad2006-02-22 02:26:30 +0000261 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
262 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
263 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
264 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
265
Evan Chengd30bf012006-03-01 01:11:20 +0000266 // FIXME: add MMX packed arithmetics
Chris Lattnera064d282006-03-19 01:13:28 +0000267 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
268 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
269 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000270 }
271
272 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
273 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
274
Evan Chengd30bf012006-03-01 01:11:20 +0000275 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
276 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
277 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
278 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Chris Lattnera064d282006-03-19 01:13:28 +0000279 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000280 }
281
282 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
283 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
284 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
285 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
287 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
288
289
Evan Chengd30bf012006-03-01 01:11:20 +0000290 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
291 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
292 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
293 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Chris Lattnera064d282006-03-19 01:13:28 +0000294 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Expand);
295 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Expand);
296 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Expand);
297 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
298 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000299 }
300
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000301 computeRegisterProperties();
302
Evan Cheng87ed7162006-02-14 08:25:08 +0000303 // FIXME: These should be based on subtarget info. Plus, the values should
304 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000305 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
306 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
307 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000308 allowUnalignedMemoryAccesses = true; // x86 supports it!
309}
310
311std::vector<SDOperand>
312X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
313 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
314 return LowerFastCCArguments(F, DAG);
315 return LowerCCCArguments(F, DAG);
316}
317
318std::pair<SDOperand, SDOperand>
319X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
320 bool isVarArg, unsigned CallingConv,
321 bool isTailCall,
322 SDOperand Callee, ArgListTy &Args,
323 SelectionDAG &DAG) {
324 assert((!isVarArg || CallingConv == CallingConv::C) &&
325 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000326
327 // If the callee is a GlobalAddress node (quite common, every direct call is)
328 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
329 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
330 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000331 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
332 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000333
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000334 if (CallingConv == CallingConv::Fast && EnableFastCC)
335 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
336 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
337}
338
339//===----------------------------------------------------------------------===//
340// C Calling Convention implementation
341//===----------------------------------------------------------------------===//
342
343std::vector<SDOperand>
344X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
345 std::vector<SDOperand> ArgValues;
346
347 MachineFunction &MF = DAG.getMachineFunction();
348 MachineFrameInfo *MFI = MF.getFrameInfo();
349
350 // Add DAG nodes to load the arguments... On entry to a function on the X86,
351 // the stack frame looks like this:
352 //
353 // [ESP] -- return address
354 // [ESP + 4] -- first argument (leftmost lexically)
355 // [ESP + 8] -- second argument, if first argument is four bytes in size
356 // ...
357 //
358 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
359 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
360 MVT::ValueType ObjectVT = getValueType(I->getType());
361 unsigned ArgIncrement = 4;
362 unsigned ObjSize;
363 switch (ObjectVT) {
364 default: assert(0 && "Unhandled argument type!");
365 case MVT::i1:
366 case MVT::i8: ObjSize = 1; break;
367 case MVT::i16: ObjSize = 2; break;
368 case MVT::i32: ObjSize = 4; break;
369 case MVT::i64: ObjSize = ArgIncrement = 8; break;
370 case MVT::f32: ObjSize = 4; break;
371 case MVT::f64: ObjSize = ArgIncrement = 8; break;
372 }
373 // Create the frame index object for this incoming parameter...
374 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
375
376 // Create the SelectionDAG nodes corresponding to a load from this parameter
377 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
378
379 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
380 // dead loads.
381 SDOperand ArgValue;
382 if (!I->use_empty())
383 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
384 DAG.getSrcValue(NULL));
385 else {
386 if (MVT::isInteger(ObjectVT))
387 ArgValue = DAG.getConstant(0, ObjectVT);
388 else
389 ArgValue = DAG.getConstantFP(0, ObjectVT);
390 }
391 ArgValues.push_back(ArgValue);
392
393 ArgOffset += ArgIncrement; // Move on to the next argument...
394 }
395
396 // If the function takes variable number of arguments, make a frame index for
397 // the start of the first vararg value... for expansion of llvm.va_start.
398 if (F.isVarArg())
399 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
400 ReturnAddrIndex = 0; // No return address slot generated yet.
401 BytesToPopOnReturn = 0; // Callee pops nothing.
402 BytesCallerReserves = ArgOffset;
403
404 // Finally, inform the code generator which regs we return values in.
405 switch (getValueType(F.getReturnType())) {
406 default: assert(0 && "Unknown type!");
407 case MVT::isVoid: break;
408 case MVT::i1:
409 case MVT::i8:
410 case MVT::i16:
411 case MVT::i32:
412 MF.addLiveOut(X86::EAX);
413 break;
414 case MVT::i64:
415 MF.addLiveOut(X86::EAX);
416 MF.addLiveOut(X86::EDX);
417 break;
418 case MVT::f32:
419 case MVT::f64:
420 MF.addLiveOut(X86::ST0);
421 break;
422 }
423 return ArgValues;
424}
425
426std::pair<SDOperand, SDOperand>
427X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
428 bool isVarArg, bool isTailCall,
429 SDOperand Callee, ArgListTy &Args,
430 SelectionDAG &DAG) {
431 // Count how many bytes are to be pushed on the stack.
432 unsigned NumBytes = 0;
433
434 if (Args.empty()) {
435 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000436 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 } else {
438 for (unsigned i = 0, e = Args.size(); i != e; ++i)
439 switch (getValueType(Args[i].second)) {
440 default: assert(0 && "Unknown value type!");
441 case MVT::i1:
442 case MVT::i8:
443 case MVT::i16:
444 case MVT::i32:
445 case MVT::f32:
446 NumBytes += 4;
447 break;
448 case MVT::i64:
449 case MVT::f64:
450 NumBytes += 8;
451 break;
452 }
453
Chris Lattner94dd2922006-02-13 09:00:43 +0000454 Chain = DAG.getCALLSEQ_START(Chain,
455 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000456
457 // Arguments go on the stack in reverse order, as specified by the ABI.
458 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000459 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000460 std::vector<SDOperand> Stores;
461
462 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
463 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
464 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
465
466 switch (getValueType(Args[i].second)) {
467 default: assert(0 && "Unexpected ValueType for argument!");
468 case MVT::i1:
469 case MVT::i8:
470 case MVT::i16:
471 // Promote the integer to 32 bits. If the input type is signed use a
472 // sign extend, otherwise use a zero extend.
473 if (Args[i].second->isSigned())
474 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
475 else
476 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
477
478 // FALL THROUGH
479 case MVT::i32:
480 case MVT::f32:
481 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
482 Args[i].first, PtrOff,
483 DAG.getSrcValue(NULL)));
484 ArgOffset += 4;
485 break;
486 case MVT::i64:
487 case MVT::f64:
488 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
489 Args[i].first, PtrOff,
490 DAG.getSrcValue(NULL)));
491 ArgOffset += 8;
492 break;
493 }
494 }
495 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
496 }
497
498 std::vector<MVT::ValueType> RetVals;
499 MVT::ValueType RetTyVT = getValueType(RetTy);
500 RetVals.push_back(MVT::Other);
501
502 // The result values produced have to be legal. Promote the result.
503 switch (RetTyVT) {
504 case MVT::isVoid: break;
505 default:
506 RetVals.push_back(RetTyVT);
507 break;
508 case MVT::i1:
509 case MVT::i8:
510 case MVT::i16:
511 RetVals.push_back(MVT::i32);
512 break;
513 case MVT::f32:
514 if (X86ScalarSSE)
515 RetVals.push_back(MVT::f32);
516 else
517 RetVals.push_back(MVT::f64);
518 break;
519 case MVT::i64:
520 RetVals.push_back(MVT::i32);
521 RetVals.push_back(MVT::i32);
522 break;
523 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000524
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000525 std::vector<MVT::ValueType> NodeTys;
526 NodeTys.push_back(MVT::Other); // Returns a chain
527 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
528 std::vector<SDOperand> Ops;
529 Ops.push_back(Chain);
530 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000531
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000532 // FIXME: Do not generate X86ISD::TAILCALL for now.
533 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
534 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000535
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000536 NodeTys.clear();
537 NodeTys.push_back(MVT::Other); // Returns a chain
538 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
539 Ops.clear();
540 Ops.push_back(Chain);
541 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
542 Ops.push_back(DAG.getConstant(0, getPointerTy()));
543 Ops.push_back(InFlag);
544 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
545 InFlag = Chain.getValue(1);
546
547 SDOperand RetVal;
548 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000549 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000550 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000551 case MVT::i1:
552 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000553 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
554 Chain = RetVal.getValue(1);
555 if (RetTyVT == MVT::i1)
556 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
557 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000558 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000559 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
560 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000561 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000562 case MVT::i32:
563 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
564 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000565 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000566 case MVT::i64: {
567 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
568 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
569 Lo.getValue(2));
570 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
571 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000572 break;
573 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000574 case MVT::f32:
575 case MVT::f64: {
576 std::vector<MVT::ValueType> Tys;
577 Tys.push_back(MVT::f64);
578 Tys.push_back(MVT::Other);
579 Tys.push_back(MVT::Flag);
580 std::vector<SDOperand> Ops;
581 Ops.push_back(Chain);
582 Ops.push_back(InFlag);
583 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
584 Chain = RetVal.getValue(1);
585 InFlag = RetVal.getValue(2);
586 if (X86ScalarSSE) {
587 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
588 // shouldn't be necessary except that RFP cannot be live across
589 // multiple blocks. When stackifier is fixed, they can be uncoupled.
590 MachineFunction &MF = DAG.getMachineFunction();
591 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
592 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
593 Tys.clear();
594 Tys.push_back(MVT::Other);
595 Ops.clear();
596 Ops.push_back(Chain);
597 Ops.push_back(RetVal);
598 Ops.push_back(StackSlot);
599 Ops.push_back(DAG.getValueType(RetTyVT));
600 Ops.push_back(InFlag);
601 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
602 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
603 DAG.getSrcValue(NULL));
604 Chain = RetVal.getValue(1);
605 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000606
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000607 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
608 // FIXME: we would really like to remember that this FP_ROUND
609 // operation is okay to eliminate if we allow excess FP precision.
610 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
611 break;
612 }
613 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000615
616 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000617}
618
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000619//===----------------------------------------------------------------------===//
620// Fast Calling Convention implementation
621//===----------------------------------------------------------------------===//
622//
623// The X86 'fast' calling convention passes up to two integer arguments in
624// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
625// and requires that the callee pop its arguments off the stack (allowing proper
626// tail calls), and has the same return value conventions as C calling convs.
627//
628// This calling convention always arranges for the callee pop value to be 8n+4
629// bytes, which is needed for tail recursion elimination and stack alignment
630// reasons.
631//
632// Note that this can be enhanced in the future to pass fp vals in registers
633// (when we have a global fp allocator) and do other tricks.
634//
635
636/// AddLiveIn - This helper function adds the specified physical register to the
637/// MachineFunction as a live in value. It also creates a corresponding virtual
638/// register for it.
639static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
640 TargetRegisterClass *RC) {
641 assert(RC->contains(PReg) && "Not the correct regclass!");
642 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
643 MF.addLiveIn(PReg, VReg);
644 return VReg;
645}
646
Chris Lattner89fad2c2006-03-17 17:27:47 +0000647// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
648// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
649// EDX". Anything more is illegal.
650//
651// FIXME: The linscan register allocator currently has problem with
652// coallescing. At the time of this writing, whenever it decides to coallesce
653// a physreg with a virtreg, this increases the size of the physreg's live
654// range, and the live range cannot ever be reduced. This causes problems if
655// too many physregs are coalleced with virtregs, which can cause the register
656// allocator to wedge itself.
657//
658// This code triggers this problem more often if we pass args in registers,
659// so disable it until this is fixed.
660//
661// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
662// about code being dead.
663//
664static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000665
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000666
667std::vector<SDOperand>
668X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
669 std::vector<SDOperand> ArgValues;
670
671 MachineFunction &MF = DAG.getMachineFunction();
672 MachineFrameInfo *MFI = MF.getFrameInfo();
673
674 // Add DAG nodes to load the arguments... On entry to a function the stack
675 // frame looks like this:
676 //
677 // [ESP] -- return address
678 // [ESP + 4] -- first nonreg argument (leftmost lexically)
679 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
680 // ...
681 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
682
683 // Keep track of the number of integer regs passed so far. This can be either
684 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
685 // used).
686 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000687
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000688 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
689 MVT::ValueType ObjectVT = getValueType(I->getType());
690 unsigned ArgIncrement = 4;
691 unsigned ObjSize = 0;
692 SDOperand ArgValue;
693
694 switch (ObjectVT) {
695 default: assert(0 && "Unhandled argument type!");
696 case MVT::i1:
697 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000698 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000699 if (!I->use_empty()) {
700 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
701 X86::R8RegisterClass);
702 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
703 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000704 if (ObjectVT == MVT::i1)
705 // FIXME: Should insert a assertzext here.
706 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000707 }
708 ++NumIntRegs;
709 break;
710 }
711
712 ObjSize = 1;
713 break;
714 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000715 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000716 if (!I->use_empty()) {
717 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
718 X86::R16RegisterClass);
719 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
720 DAG.setRoot(ArgValue.getValue(1));
721 }
722 ++NumIntRegs;
723 break;
724 }
725 ObjSize = 2;
726 break;
727 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000728 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000729 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000730 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000731 X86::R32RegisterClass);
732 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
733 DAG.setRoot(ArgValue.getValue(1));
734 }
735 ++NumIntRegs;
736 break;
737 }
738 ObjSize = 4;
739 break;
740 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000741 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000742 if (!I->use_empty()) {
743 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
744 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
745
746 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
747 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
748 DAG.setRoot(Hi.getValue(1));
749
750 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
751 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000752 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000753 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000754 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000755 if (!I->use_empty()) {
756 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
757 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
758 DAG.setRoot(Low.getValue(1));
759
760 // Load the high part from memory.
761 // Create the frame index object for this incoming parameter...
762 int FI = MFI->CreateFixedObject(4, ArgOffset);
763 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
764 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
765 DAG.getSrcValue(NULL));
766 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
767 }
768 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000769 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000770 break;
771 }
772 ObjSize = ArgIncrement = 8;
773 break;
774 case MVT::f32: ObjSize = 4; break;
775 case MVT::f64: ObjSize = ArgIncrement = 8; break;
776 }
777
778 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
779 // dead loads.
780 if (ObjSize && !I->use_empty()) {
781 // Create the frame index object for this incoming parameter...
782 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
783
784 // Create the SelectionDAG nodes corresponding to a load from this
785 // parameter.
786 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
787
788 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
789 DAG.getSrcValue(NULL));
790 } else if (ArgValue.Val == 0) {
791 if (MVT::isInteger(ObjectVT))
792 ArgValue = DAG.getConstant(0, ObjectVT);
793 else
794 ArgValue = DAG.getConstantFP(0, ObjectVT);
795 }
796 ArgValues.push_back(ArgValue);
797
798 if (ObjSize)
799 ArgOffset += ArgIncrement; // Move on to the next argument.
800 }
801
802 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
803 // arguments and the arguments after the retaddr has been pushed are aligned.
804 if ((ArgOffset & 7) == 0)
805 ArgOffset += 4;
806
807 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
808 ReturnAddrIndex = 0; // No return address slot generated yet.
809 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
810 BytesCallerReserves = 0;
811
812 // Finally, inform the code generator which regs we return values in.
813 switch (getValueType(F.getReturnType())) {
814 default: assert(0 && "Unknown type!");
815 case MVT::isVoid: break;
816 case MVT::i1:
817 case MVT::i8:
818 case MVT::i16:
819 case MVT::i32:
820 MF.addLiveOut(X86::EAX);
821 break;
822 case MVT::i64:
823 MF.addLiveOut(X86::EAX);
824 MF.addLiveOut(X86::EDX);
825 break;
826 case MVT::f32:
827 case MVT::f64:
828 MF.addLiveOut(X86::ST0);
829 break;
830 }
831 return ArgValues;
832}
833
834std::pair<SDOperand, SDOperand>
835X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
836 bool isTailCall, SDOperand Callee,
837 ArgListTy &Args, SelectionDAG &DAG) {
838 // Count how many bytes are to be pushed on the stack.
839 unsigned NumBytes = 0;
840
841 // Keep track of the number of integer regs passed so far. This can be either
842 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
843 // used).
844 unsigned NumIntRegs = 0;
845
846 for (unsigned i = 0, e = Args.size(); i != e; ++i)
847 switch (getValueType(Args[i].second)) {
848 default: assert(0 && "Unknown value type!");
849 case MVT::i1:
850 case MVT::i8:
851 case MVT::i16:
852 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000853 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000854 ++NumIntRegs;
855 break;
856 }
857 // fall through
858 case MVT::f32:
859 NumBytes += 4;
860 break;
861 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000862 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
863 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000864 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000865 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
866 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000867 NumBytes += 4;
868 break;
869 }
870
871 // fall through
872 case MVT::f64:
873 NumBytes += 8;
874 break;
875 }
876
877 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
878 // arguments and the arguments after the retaddr has been pushed are aligned.
879 if ((NumBytes & 7) == 0)
880 NumBytes += 4;
881
Chris Lattner94dd2922006-02-13 09:00:43 +0000882 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000883
884 // Arguments go on the stack in reverse order, as specified by the ABI.
885 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000886 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000887 NumIntRegs = 0;
888 std::vector<SDOperand> Stores;
889 std::vector<SDOperand> RegValuesToPass;
890 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
891 switch (getValueType(Args[i].second)) {
892 default: assert(0 && "Unexpected ValueType for argument!");
893 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000894 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
895 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000896 case MVT::i8:
897 case MVT::i16:
898 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000899 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000900 RegValuesToPass.push_back(Args[i].first);
901 ++NumIntRegs;
902 break;
903 }
904 // Fall through
905 case MVT::f32: {
906 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
907 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
908 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
909 Args[i].first, PtrOff,
910 DAG.getSrcValue(NULL)));
911 ArgOffset += 4;
912 break;
913 }
914 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000915 // Can pass (at least) part of it in regs?
916 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000917 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
918 Args[i].first, DAG.getConstant(1, MVT::i32));
919 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
920 Args[i].first, DAG.getConstant(0, MVT::i32));
921 RegValuesToPass.push_back(Lo);
922 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000923
924 // Pass both parts in regs?
925 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000926 RegValuesToPass.push_back(Hi);
927 ++NumIntRegs;
928 } else {
929 // Pass the high part in memory.
930 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
931 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
932 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
933 Hi, PtrOff, DAG.getSrcValue(NULL)));
934 ArgOffset += 4;
935 }
936 break;
937 }
938 // Fall through
939 case MVT::f64:
940 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
941 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
942 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
943 Args[i].first, PtrOff,
944 DAG.getSrcValue(NULL)));
945 ArgOffset += 8;
946 break;
947 }
948 }
949 if (!Stores.empty())
950 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
951
952 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
953 // arguments and the arguments after the retaddr has been pushed are aligned.
954 if ((ArgOffset & 7) == 0)
955 ArgOffset += 4;
956
957 std::vector<MVT::ValueType> RetVals;
958 MVT::ValueType RetTyVT = getValueType(RetTy);
959
960 RetVals.push_back(MVT::Other);
961
962 // The result values produced have to be legal. Promote the result.
963 switch (RetTyVT) {
964 case MVT::isVoid: break;
965 default:
966 RetVals.push_back(RetTyVT);
967 break;
968 case MVT::i1:
969 case MVT::i8:
970 case MVT::i16:
971 RetVals.push_back(MVT::i32);
972 break;
973 case MVT::f32:
974 if (X86ScalarSSE)
975 RetVals.push_back(MVT::f32);
976 else
977 RetVals.push_back(MVT::f64);
978 break;
979 case MVT::i64:
980 RetVals.push_back(MVT::i32);
981 RetVals.push_back(MVT::i32);
982 break;
983 }
984
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000985 // Build a sequence of copy-to-reg nodes chained together with token chain
986 // and flag operands which copy the outgoing args into registers.
987 SDOperand InFlag;
988 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
989 unsigned CCReg;
990 SDOperand RegToPass = RegValuesToPass[i];
991 switch (RegToPass.getValueType()) {
992 default: assert(0 && "Bad thing to pass in regs");
993 case MVT::i8:
994 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000995 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000996 case MVT::i16:
997 CCReg = (i == 0) ? X86::AX : X86::DX;
998 break;
999 case MVT::i32:
1000 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1001 break;
1002 }
1003
1004 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1005 InFlag = Chain.getValue(1);
1006 }
1007
1008 std::vector<MVT::ValueType> NodeTys;
1009 NodeTys.push_back(MVT::Other); // Returns a chain
1010 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1011 std::vector<SDOperand> Ops;
1012 Ops.push_back(Chain);
1013 Ops.push_back(Callee);
1014 if (InFlag.Val)
1015 Ops.push_back(InFlag);
1016
1017 // FIXME: Do not generate X86ISD::TAILCALL for now.
1018 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1019 InFlag = Chain.getValue(1);
1020
1021 NodeTys.clear();
1022 NodeTys.push_back(MVT::Other); // Returns a chain
1023 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1024 Ops.clear();
1025 Ops.push_back(Chain);
1026 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1027 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1028 Ops.push_back(InFlag);
1029 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1030 InFlag = Chain.getValue(1);
1031
1032 SDOperand RetVal;
1033 if (RetTyVT != MVT::isVoid) {
1034 switch (RetTyVT) {
1035 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001036 case MVT::i1:
1037 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001038 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1039 Chain = RetVal.getValue(1);
1040 if (RetTyVT == MVT::i1)
1041 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1042 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001043 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001044 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1045 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001046 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001047 case MVT::i32:
1048 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1049 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001050 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001051 case MVT::i64: {
1052 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1053 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1054 Lo.getValue(2));
1055 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1056 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001057 break;
1058 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001059 case MVT::f32:
1060 case MVT::f64: {
1061 std::vector<MVT::ValueType> Tys;
1062 Tys.push_back(MVT::f64);
1063 Tys.push_back(MVT::Other);
1064 Tys.push_back(MVT::Flag);
1065 std::vector<SDOperand> Ops;
1066 Ops.push_back(Chain);
1067 Ops.push_back(InFlag);
1068 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1069 Chain = RetVal.getValue(1);
1070 InFlag = RetVal.getValue(2);
1071 if (X86ScalarSSE) {
1072 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1073 // shouldn't be necessary except that RFP cannot be live across
1074 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1075 MachineFunction &MF = DAG.getMachineFunction();
1076 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1077 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1078 Tys.clear();
1079 Tys.push_back(MVT::Other);
1080 Ops.clear();
1081 Ops.push_back(Chain);
1082 Ops.push_back(RetVal);
1083 Ops.push_back(StackSlot);
1084 Ops.push_back(DAG.getValueType(RetTyVT));
1085 Ops.push_back(InFlag);
1086 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1087 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1088 DAG.getSrcValue(NULL));
1089 Chain = RetVal.getValue(1);
1090 }
Evan Chengd9558e02006-01-06 00:43:03 +00001091
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001092 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1093 // FIXME: we would really like to remember that this FP_ROUND
1094 // operation is okay to eliminate if we allow excess FP precision.
1095 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1096 break;
1097 }
1098 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001099 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001100
1101 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001102}
1103
1104SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1105 if (ReturnAddrIndex == 0) {
1106 // Set up a frame object for the return address.
1107 MachineFunction &MF = DAG.getMachineFunction();
1108 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1109 }
1110
1111 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1112}
1113
1114
1115
1116std::pair<SDOperand, SDOperand> X86TargetLowering::
1117LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1118 SelectionDAG &DAG) {
1119 SDOperand Result;
1120 if (Depth) // Depths > 0 not supported yet!
1121 Result = DAG.getConstant(0, getPointerTy());
1122 else {
1123 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1124 if (!isFrameAddress)
1125 // Just load the return address
1126 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1127 DAG.getSrcValue(NULL));
1128 else
1129 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1130 DAG.getConstant(4, MVT::i32));
1131 }
1132 return std::make_pair(Result, Chain);
1133}
1134
Evan Cheng4a460802006-01-11 00:33:36 +00001135/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1136/// which corresponds to the condition code.
1137static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1138 switch (X86CC) {
1139 default: assert(0 && "Unknown X86 conditional code!");
1140 case X86ISD::COND_A: return X86::JA;
1141 case X86ISD::COND_AE: return X86::JAE;
1142 case X86ISD::COND_B: return X86::JB;
1143 case X86ISD::COND_BE: return X86::JBE;
1144 case X86ISD::COND_E: return X86::JE;
1145 case X86ISD::COND_G: return X86::JG;
1146 case X86ISD::COND_GE: return X86::JGE;
1147 case X86ISD::COND_L: return X86::JL;
1148 case X86ISD::COND_LE: return X86::JLE;
1149 case X86ISD::COND_NE: return X86::JNE;
1150 case X86ISD::COND_NO: return X86::JNO;
1151 case X86ISD::COND_NP: return X86::JNP;
1152 case X86ISD::COND_NS: return X86::JNS;
1153 case X86ISD::COND_O: return X86::JO;
1154 case X86ISD::COND_P: return X86::JP;
1155 case X86ISD::COND_S: return X86::JS;
1156 }
1157}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001158
Evan Cheng6dfa9992006-01-30 23:41:35 +00001159/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1160/// specific condition code. It returns a false if it cannot do a direct
1161/// translation. X86CC is the translated CondCode. Flip is set to true if the
1162/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001163static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1164 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001165 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001166 Flip = false;
1167 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001168 if (!isFP) {
1169 switch (SetCCOpcode) {
1170 default: break;
1171 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1172 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1173 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1174 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1175 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1176 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1177 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1178 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1179 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1180 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1181 }
1182 } else {
1183 // On a floating point condition, the flags are set as follows:
1184 // ZF PF CF op
1185 // 0 | 0 | 0 | X > Y
1186 // 0 | 0 | 1 | X < Y
1187 // 1 | 0 | 0 | X == Y
1188 // 1 | 1 | 1 | unordered
1189 switch (SetCCOpcode) {
1190 default: break;
1191 case ISD::SETUEQ:
1192 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001193 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001194 case ISD::SETOGT:
1195 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001196 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001197 case ISD::SETOGE:
1198 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001199 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001200 case ISD::SETULT:
1201 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001202 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001203 case ISD::SETULE:
1204 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1205 case ISD::SETONE:
1206 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1207 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1208 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1209 }
1210 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001211
1212 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001213}
1214
Evan Cheng4a460802006-01-11 00:33:36 +00001215/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1216/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001217/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001218static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001219 switch (X86CC) {
1220 default:
1221 return false;
1222 case X86ISD::COND_B:
1223 case X86ISD::COND_BE:
1224 case X86ISD::COND_E:
1225 case X86ISD::COND_P:
1226 case X86ISD::COND_A:
1227 case X86ISD::COND_AE:
1228 case X86ISD::COND_NE:
1229 case X86ISD::COND_NP:
1230 return true;
1231 }
1232}
1233
Evan Cheng4a460802006-01-11 00:33:36 +00001234MachineBasicBlock *
1235X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1236 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001237 switch (MI->getOpcode()) {
1238 default: assert(false && "Unexpected instr type to insert");
1239 case X86::CMOV_FR32:
1240 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001241 // To "insert" a SELECT_CC instruction, we actually have to insert the
1242 // diamond control-flow pattern. The incoming instruction knows the
1243 // destination vreg to set, the condition code register to branch on, the
1244 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001245 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1246 ilist<MachineBasicBlock>::iterator It = BB;
1247 ++It;
1248
1249 // thisMBB:
1250 // ...
1251 // TrueVal = ...
1252 // cmpTY ccX, r1, r2
1253 // bCC copy1MBB
1254 // fallthrough --> copy0MBB
1255 MachineBasicBlock *thisMBB = BB;
1256 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1257 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1258 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1259 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1260 MachineFunction *F = BB->getParent();
1261 F->getBasicBlockList().insert(It, copy0MBB);
1262 F->getBasicBlockList().insert(It, sinkMBB);
1263 // Update machine-CFG edges
1264 BB->addSuccessor(copy0MBB);
1265 BB->addSuccessor(sinkMBB);
1266
1267 // copy0MBB:
1268 // %FalseValue = ...
1269 // # fallthrough to sinkMBB
1270 BB = copy0MBB;
1271
1272 // Update machine-CFG edges
1273 BB->addSuccessor(sinkMBB);
1274
1275 // sinkMBB:
1276 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1277 // ...
1278 BB = sinkMBB;
1279 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1280 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1281 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001282
Evan Cheng0cc39452006-01-16 21:21:29 +00001283 delete MI; // The pseudo instruction is gone now.
1284 return BB;
1285 }
Evan Cheng4a460802006-01-11 00:33:36 +00001286
Evan Cheng0cc39452006-01-16 21:21:29 +00001287 case X86::FP_TO_INT16_IN_MEM:
1288 case X86::FP_TO_INT32_IN_MEM:
1289 case X86::FP_TO_INT64_IN_MEM: {
1290 // Change the floating point control register to use "round towards zero"
1291 // mode when truncating to an integer value.
1292 MachineFunction *F = BB->getParent();
1293 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1294 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1295
1296 // Load the old value of the high byte of the control word...
1297 unsigned OldCW =
1298 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1299 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1300
1301 // Set the high part to be round to zero...
1302 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1303
1304 // Reload the modified control word now...
1305 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1306
1307 // Restore the memory image of control word to original value
1308 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1309
1310 // Get the X86 opcode to use.
1311 unsigned Opc;
1312 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001313 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001314 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1315 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1316 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1317 }
1318
1319 X86AddressMode AM;
1320 MachineOperand &Op = MI->getOperand(0);
1321 if (Op.isRegister()) {
1322 AM.BaseType = X86AddressMode::RegBase;
1323 AM.Base.Reg = Op.getReg();
1324 } else {
1325 AM.BaseType = X86AddressMode::FrameIndexBase;
1326 AM.Base.FrameIndex = Op.getFrameIndex();
1327 }
1328 Op = MI->getOperand(1);
1329 if (Op.isImmediate())
1330 AM.Scale = Op.getImmedValue();
1331 Op = MI->getOperand(2);
1332 if (Op.isImmediate())
1333 AM.IndexReg = Op.getImmedValue();
1334 Op = MI->getOperand(3);
1335 if (Op.isGlobalAddress()) {
1336 AM.GV = Op.getGlobal();
1337 } else {
1338 AM.Disp = Op.getImmedValue();
1339 }
1340 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1341
1342 // Reload the original control word now.
1343 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1344
1345 delete MI; // The pseudo instruction is gone now.
1346 return BB;
1347 }
1348 }
Evan Cheng4a460802006-01-11 00:33:36 +00001349}
1350
1351
1352//===----------------------------------------------------------------------===//
1353// X86 Custom Lowering Hooks
1354//===----------------------------------------------------------------------===//
1355
Evan Cheng30b37b52006-03-13 23:18:16 +00001356/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1357/// load. For Darwin, external and weak symbols are indirect, loading the value
1358/// at address GV rather then the value of GV itself. This means that the
1359/// GlobalAddress must be in the base or index register of the address, not the
1360/// GV offset field.
1361static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1362 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1363 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1364}
1365
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001366/// LowerOperation - Provide custom lowering hooks for some operations.
1367///
1368SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1369 switch (Op.getOpcode()) {
1370 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001371 case ISD::SHL_PARTS:
1372 case ISD::SRA_PARTS:
1373 case ISD::SRL_PARTS: {
1374 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1375 "Not an i64 shift!");
1376 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1377 SDOperand ShOpLo = Op.getOperand(0);
1378 SDOperand ShOpHi = Op.getOperand(1);
1379 SDOperand ShAmt = Op.getOperand(2);
1380 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001381 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001382 : DAG.getConstant(0, MVT::i32);
1383
1384 SDOperand Tmp2, Tmp3;
1385 if (Op.getOpcode() == ISD::SHL_PARTS) {
1386 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1387 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1388 } else {
1389 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001390 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001391 }
1392
1393 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1394 ShAmt, DAG.getConstant(32, MVT::i8));
1395
1396 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001397 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001398
1399 std::vector<MVT::ValueType> Tys;
1400 Tys.push_back(MVT::i32);
1401 Tys.push_back(MVT::Flag);
1402 std::vector<SDOperand> Ops;
1403 if (Op.getOpcode() == ISD::SHL_PARTS) {
1404 Ops.push_back(Tmp2);
1405 Ops.push_back(Tmp3);
1406 Ops.push_back(CC);
1407 Ops.push_back(InFlag);
1408 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1409 InFlag = Hi.getValue(1);
1410
1411 Ops.clear();
1412 Ops.push_back(Tmp3);
1413 Ops.push_back(Tmp1);
1414 Ops.push_back(CC);
1415 Ops.push_back(InFlag);
1416 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1417 } else {
1418 Ops.push_back(Tmp2);
1419 Ops.push_back(Tmp3);
1420 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001421 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001422 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1423 InFlag = Lo.getValue(1);
1424
1425 Ops.clear();
1426 Ops.push_back(Tmp3);
1427 Ops.push_back(Tmp1);
1428 Ops.push_back(CC);
1429 Ops.push_back(InFlag);
1430 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1431 }
1432
1433 Tys.clear();
1434 Tys.push_back(MVT::i32);
1435 Tys.push_back(MVT::i32);
1436 Ops.clear();
1437 Ops.push_back(Lo);
1438 Ops.push_back(Hi);
1439 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1440 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001441 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001442 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001443 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001444 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001445
1446 SDOperand Result;
1447 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1448 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001449 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001450 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001451 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001452 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1453 DAG.getEntryNode(), Op.getOperand(0),
1454 StackSlot, DAG.getSrcValue(NULL));
1455
1456 // Build the FILD
1457 std::vector<MVT::ValueType> Tys;
1458 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001459 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001460 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001461 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001462 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001463 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001464 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001465 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1466 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001467
1468 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001469 Chain = Result.getValue(1);
1470 SDOperand InFlag = Result.getValue(2);
1471
Evan Chenge3de85b2006-02-04 02:20:30 +00001472 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001473 // shouldn't be necessary except that RFP cannot be live across
1474 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1475 MachineFunction &MF = DAG.getMachineFunction();
1476 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1477 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1478 std::vector<MVT::ValueType> Tys;
1479 Tys.push_back(MVT::Other);
1480 std::vector<SDOperand> Ops;
1481 Ops.push_back(Chain);
1482 Ops.push_back(Result);
1483 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001484 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001485 Ops.push_back(InFlag);
1486 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1487 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1488 DAG.getSrcValue(NULL));
1489 }
1490
Evan Chenga3195e82006-01-12 22:54:21 +00001491 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001492 }
1493 case ISD::FP_TO_SINT: {
1494 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001495 "Unknown FP_TO_SINT to lower!");
1496 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1497 // stack slot.
1498 MachineFunction &MF = DAG.getMachineFunction();
1499 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1500 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1501 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1502
1503 unsigned Opc;
1504 switch (Op.getValueType()) {
1505 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1506 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1507 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1508 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1509 }
1510
Evan Cheng6dab0532006-01-30 08:02:57 +00001511 SDOperand Chain = DAG.getEntryNode();
1512 SDOperand Value = Op.getOperand(0);
1513 if (X86ScalarSSE) {
1514 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1515 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1516 DAG.getSrcValue(0));
1517 std::vector<MVT::ValueType> Tys;
1518 Tys.push_back(MVT::f64);
1519 Tys.push_back(MVT::Other);
1520 std::vector<SDOperand> Ops;
1521 Ops.push_back(Chain);
1522 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001523 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001524 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1525 Chain = Value.getValue(1);
1526 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1527 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1528 }
1529
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001530 // Build the FP_TO_INT*_IN_MEM
1531 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001532 Ops.push_back(Chain);
1533 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001534 Ops.push_back(StackSlot);
1535 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1536
1537 // Load the result.
1538 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1539 DAG.getSrcValue(NULL));
1540 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001541 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001542 std::vector<MVT::ValueType> Tys;
1543 Tys.push_back(MVT::Other);
1544 Tys.push_back(MVT::Flag);
1545 std::vector<SDOperand> Ops;
1546 Ops.push_back(Op.getOperand(0));
1547 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001548 Ops.clear();
1549 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1550 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1551 MVT::i32, Ops[0].getValue(2)));
1552 Ops.push_back(Ops[1].getValue(1));
1553 Tys[0] = Tys[1] = MVT::i32;
1554 Tys.push_back(MVT::Other);
1555 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001556 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001557 case ISD::FABS: {
1558 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001559 const Type *OpNTy = MVT::getTypeForValueType(VT);
1560 std::vector<Constant*> CV;
1561 if (VT == MVT::f64) {
1562 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1563 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1564 } else {
1565 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1566 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1567 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1568 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1569 }
1570 Constant *CS = ConstantStruct::get(CV);
1571 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1572 SDOperand Mask
1573 = DAG.getNode(X86ISD::LOAD_PACK,
1574 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001575 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1576 }
Evan Cheng223547a2006-01-31 22:28:30 +00001577 case ISD::FNEG: {
1578 MVT::ValueType VT = Op.getValueType();
1579 const Type *OpNTy = MVT::getTypeForValueType(VT);
1580 std::vector<Constant*> CV;
1581 if (VT == MVT::f64) {
1582 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1583 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1584 } else {
1585 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1586 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1587 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1588 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1589 }
1590 Constant *CS = ConstantStruct::get(CV);
1591 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1592 SDOperand Mask
1593 = DAG.getNode(X86ISD::LOAD_PACK,
1594 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1595 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1596 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001597 case ISD::SETCC: {
1598 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001599 SDOperand Cond;
1600 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001601 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1602 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001603 bool Flip;
1604 unsigned X86CC;
1605 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1606 if (Flip)
1607 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1608 Op.getOperand(1), Op.getOperand(0));
1609 else
1610 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1611 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001612 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1613 DAG.getConstant(X86CC, MVT::i8), Cond);
1614 } else {
1615 assert(isFP && "Illegal integer SetCC!");
1616
Evan Cheng6dfa9992006-01-30 23:41:35 +00001617 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1618 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001619 std::vector<MVT::ValueType> Tys;
1620 std::vector<SDOperand> Ops;
1621 switch (SetCCOpcode) {
1622 default: assert(false && "Illegal floating point SetCC!");
1623 case ISD::SETOEQ: { // !PF & ZF
1624 Tys.push_back(MVT::i8);
1625 Tys.push_back(MVT::Flag);
1626 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1627 Ops.push_back(Cond);
1628 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1629 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1630 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1631 Tmp1.getValue(1));
1632 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1633 }
Evan Chengd9558e02006-01-06 00:43:03 +00001634 case ISD::SETUNE: { // PF | !ZF
1635 Tys.push_back(MVT::i8);
1636 Tys.push_back(MVT::Flag);
1637 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1638 Ops.push_back(Cond);
1639 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1640 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1641 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1642 Tmp1.getValue(1));
1643 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1644 }
1645 }
1646 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001647 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001648 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001649 MVT::ValueType VT = Op.getValueType();
1650 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001651 bool isFPStack = isFP && !X86ScalarSSE;
1652 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001653 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001654 SDOperand Op0 = Op.getOperand(0);
1655 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001656 if (Op0.getOpcode() == ISD::SETCC)
1657 Op0 = LowerOperation(Op0, DAG);
1658
Evan Chengaaca22c2006-01-10 20:26:56 +00001659 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001660 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1661 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1662 // have another use it will be eliminated.
1663 // If the X86ISD::SETCC has more than one use, then it's probably better
1664 // to use a test instead of duplicating the X86ISD::CMP (for register
1665 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001666 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1667 if (!Op0.hasOneUse()) {
1668 std::vector<MVT::ValueType> Tys;
1669 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1670 Tys.push_back(Op0.Val->getValueType(i));
1671 std::vector<SDOperand> Ops;
1672 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1673 Ops.push_back(Op0.getOperand(i));
1674 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1675 }
1676
Evan Cheng1bcee362006-01-13 01:03:02 +00001677 CC = Op0.getOperand(0);
1678 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001679 // Make a copy as flag result cannot be used by more than one.
1680 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1681 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001682 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001683 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001684 } else
1685 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001686 } else
1687 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001688
Evan Cheng189d01e2006-01-13 01:06:49 +00001689 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001690 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001691 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001692 }
Evan Chenge3413162006-01-09 18:33:28 +00001693
1694 std::vector<MVT::ValueType> Tys;
1695 Tys.push_back(Op.getValueType());
1696 Tys.push_back(MVT::Flag);
1697 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001698 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1699 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001700 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001701 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001702 Ops.push_back(CC);
1703 Ops.push_back(Cond);
1704 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001705 }
Evan Cheng898101c2005-12-19 23:12:38 +00001706 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001707 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001708 SDOperand Cond = Op.getOperand(1);
1709 SDOperand Dest = Op.getOperand(2);
1710 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001711 if (Cond.getOpcode() == ISD::SETCC)
1712 Cond = LowerOperation(Cond, DAG);
1713
Evan Chengd5781fc2005-12-21 20:21:51 +00001714 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001715 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1716 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1717 // have another use it will be eliminated.
1718 // If the X86ISD::SETCC has more than one use, then it's probably better
1719 // to use a test instead of duplicating the X86ISD::CMP (for register
1720 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001721 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1722 if (!Cond.hasOneUse()) {
1723 std::vector<MVT::ValueType> Tys;
1724 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1725 Tys.push_back(Cond.Val->getValueType(i));
1726 std::vector<SDOperand> Ops;
1727 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1728 Ops.push_back(Cond.getOperand(i));
1729 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1730 }
1731
Evan Cheng1bcee362006-01-13 01:03:02 +00001732 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001733 Cond = Cond.getOperand(1);
1734 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001735 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001736 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001737 } else
1738 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001739 } else
1740 addTest = true;
1741
1742 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001743 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001744 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1745 }
1746 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1747 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1748 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001749 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001750 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001751 SDOperand Chain = Op.getOperand(0);
1752 unsigned Align =
1753 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1754 if (Align == 0) Align = 1;
1755
Evan Cheng18a84522006-02-16 00:21:07 +00001756 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1757 // If not DWORD aligned, call memset if size is less than the threshold.
1758 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001759 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001760 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001761 MVT::ValueType IntPtr = getPointerTy();
1762 const Type *IntPtrTy = getTargetData().getIntPtrType();
1763 std::vector<std::pair<SDOperand, const Type*> > Args;
1764 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1765 // Extend the ubyte argument to be an int value for the call.
1766 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1767 Args.push_back(std::make_pair(Val, IntPtrTy));
1768 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1769 std::pair<SDOperand,SDOperand> CallResult =
1770 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1771 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1772 return CallResult.second;
1773 }
1774
Evan Cheng67f92a72006-01-11 22:15:48 +00001775 MVT::ValueType AVT;
1776 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001777 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1778 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001779 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001780 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001781 unsigned ValReg;
1782 unsigned Val = ValC->getValue() & 255;
1783
1784 // If the value is a constant, then we can potentially use larger sets.
1785 switch (Align & 3) {
1786 case 2: // WORD aligned
1787 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001788 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1789 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001790 Val = (Val << 8) | Val;
1791 ValReg = X86::AX;
1792 break;
1793 case 0: // DWORD aligned
1794 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001795 if (I) {
1796 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1797 BytesLeft = I->getValue() % 4;
1798 } else {
1799 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1800 DAG.getConstant(2, MVT::i8));
1801 TwoRepStos = true;
1802 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001803 Val = (Val << 8) | Val;
1804 Val = (Val << 16) | Val;
1805 ValReg = X86::EAX;
1806 break;
1807 default: // Byte aligned
1808 AVT = MVT::i8;
1809 Count = Op.getOperand(3);
1810 ValReg = X86::AL;
1811 break;
1812 }
1813
1814 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1815 InFlag);
1816 InFlag = Chain.getValue(1);
1817 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001818 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001819 Count = Op.getOperand(3);
1820 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1821 InFlag = Chain.getValue(1);
1822 }
1823
1824 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1825 InFlag = Chain.getValue(1);
1826 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1827 InFlag = Chain.getValue(1);
1828
Evan Chengff909922006-03-07 23:29:39 +00001829 std::vector<MVT::ValueType> Tys;
1830 Tys.push_back(MVT::Other);
1831 Tys.push_back(MVT::Flag);
1832 std::vector<SDOperand> Ops;
1833 Ops.push_back(Chain);
1834 Ops.push_back(DAG.getValueType(AVT));
1835 Ops.push_back(InFlag);
1836 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1837
1838 if (TwoRepStos) {
1839 InFlag = Chain.getValue(1);
1840 Count = Op.getOperand(3);
1841 MVT::ValueType CVT = Count.getValueType();
1842 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1843 DAG.getConstant(3, CVT));
1844 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1845 InFlag = Chain.getValue(1);
1846 Tys.clear();
1847 Tys.push_back(MVT::Other);
1848 Tys.push_back(MVT::Flag);
1849 Ops.clear();
1850 Ops.push_back(Chain);
1851 Ops.push_back(DAG.getValueType(MVT::i8));
1852 Ops.push_back(InFlag);
1853 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1854 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001855 // Issue stores for the last 1 - 3 bytes.
1856 SDOperand Value;
1857 unsigned Val = ValC->getValue() & 255;
1858 unsigned Offset = I->getValue() - BytesLeft;
1859 SDOperand DstAddr = Op.getOperand(1);
1860 MVT::ValueType AddrVT = DstAddr.getValueType();
1861 if (BytesLeft >= 2) {
1862 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1863 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1864 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1865 DAG.getConstant(Offset, AddrVT)),
1866 DAG.getSrcValue(NULL));
1867 BytesLeft -= 2;
1868 Offset += 2;
1869 }
1870
1871 if (BytesLeft == 1) {
1872 Value = DAG.getConstant(Val, MVT::i8);
1873 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1874 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1875 DAG.getConstant(Offset, AddrVT)),
1876 DAG.getSrcValue(NULL));
1877 }
1878 }
1879
1880 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001881 }
1882 case ISD::MEMCPY: {
1883 SDOperand Chain = Op.getOperand(0);
1884 unsigned Align =
1885 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1886 if (Align == 0) Align = 1;
1887
Evan Cheng18a84522006-02-16 00:21:07 +00001888 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1889 // If not DWORD aligned, call memcpy if size is less than the threshold.
1890 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001891 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001892 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001893 MVT::ValueType IntPtr = getPointerTy();
1894 const Type *IntPtrTy = getTargetData().getIntPtrType();
1895 std::vector<std::pair<SDOperand, const Type*> > Args;
1896 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1897 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1898 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1899 std::pair<SDOperand,SDOperand> CallResult =
1900 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1901 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1902 return CallResult.second;
1903 }
1904
Evan Cheng67f92a72006-01-11 22:15:48 +00001905 MVT::ValueType AVT;
1906 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001907 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001908 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001909 switch (Align & 3) {
1910 case 2: // WORD aligned
1911 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001912 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1913 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001914 break;
1915 case 0: // DWORD aligned
1916 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001917 if (I) {
1918 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1919 BytesLeft = I->getValue() % 4;
1920 } else {
1921 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1922 DAG.getConstant(2, MVT::i8));
1923 TwoRepMovs = true;
1924 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001925 break;
1926 default: // Byte aligned
1927 AVT = MVT::i8;
1928 Count = Op.getOperand(3);
1929 break;
1930 }
1931
Evan Cheng62bec2c2006-03-04 02:48:56 +00001932 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001933 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1934 InFlag = Chain.getValue(1);
1935 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1936 InFlag = Chain.getValue(1);
1937 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1938 InFlag = Chain.getValue(1);
1939
Evan Chengff909922006-03-07 23:29:39 +00001940 std::vector<MVT::ValueType> Tys;
1941 Tys.push_back(MVT::Other);
1942 Tys.push_back(MVT::Flag);
1943 std::vector<SDOperand> Ops;
1944 Ops.push_back(Chain);
1945 Ops.push_back(DAG.getValueType(AVT));
1946 Ops.push_back(InFlag);
1947 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1948
1949 if (TwoRepMovs) {
1950 InFlag = Chain.getValue(1);
1951 Count = Op.getOperand(3);
1952 MVT::ValueType CVT = Count.getValueType();
1953 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1954 DAG.getConstant(3, CVT));
1955 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1956 InFlag = Chain.getValue(1);
1957 Tys.clear();
1958 Tys.push_back(MVT::Other);
1959 Tys.push_back(MVT::Flag);
1960 Ops.clear();
1961 Ops.push_back(Chain);
1962 Ops.push_back(DAG.getValueType(MVT::i8));
1963 Ops.push_back(InFlag);
1964 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1965 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001966 // Issue loads and stores for the last 1 - 3 bytes.
1967 unsigned Offset = I->getValue() - BytesLeft;
1968 SDOperand DstAddr = Op.getOperand(1);
1969 MVT::ValueType DstVT = DstAddr.getValueType();
1970 SDOperand SrcAddr = Op.getOperand(2);
1971 MVT::ValueType SrcVT = SrcAddr.getValueType();
1972 SDOperand Value;
1973 if (BytesLeft >= 2) {
1974 Value = DAG.getLoad(MVT::i16, Chain,
1975 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1976 DAG.getConstant(Offset, SrcVT)),
1977 DAG.getSrcValue(NULL));
1978 Chain = Value.getValue(1);
1979 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1980 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1981 DAG.getConstant(Offset, DstVT)),
1982 DAG.getSrcValue(NULL));
1983 BytesLeft -= 2;
1984 Offset += 2;
1985 }
1986
1987 if (BytesLeft == 1) {
1988 Value = DAG.getLoad(MVT::i8, Chain,
1989 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1990 DAG.getConstant(Offset, SrcVT)),
1991 DAG.getSrcValue(NULL));
1992 Chain = Value.getValue(1);
1993 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1994 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1995 DAG.getConstant(Offset, DstVT)),
1996 DAG.getSrcValue(NULL));
1997 }
1998 }
1999
2000 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002001 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002002
2003 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2004 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2005 // one of the above mentioned nodes. It has to be wrapped because otherwise
2006 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2007 // be used to form addressing mode. These wrapped nodes will be selected
2008 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002009 case ISD::ConstantPool: {
2010 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002011 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2012 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2013 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00002014 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002015 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002016 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002017 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2018 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2019 }
2020
2021 return Result;
2022 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002023 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002024 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2025 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2026 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00002027 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00002028 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002029 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002030 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002031 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2032 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002033
2034 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002035 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002036 // the GlobalAddress must be in the base or index register of the address,
2037 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002038 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002039 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002040 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002041 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002042 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002043
Evan Cheng002fe9b2006-01-12 07:56:47 +00002044 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002045 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002046 case ISD::ExternalSymbol: {
2047 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2048 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2049 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2050 if (getTargetMachine().
2051 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2052 // With PIC, the address is actually $g + Offset.
2053 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2054 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2055 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2056 }
2057
2058 return Result;
2059 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002060 case ISD::VASTART: {
2061 // vastart just stores the address of the VarArgsFrameIndex slot into the
2062 // memory location argument.
2063 // FIXME: Replace MVT::i32 with PointerTy
2064 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2065 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2066 Op.getOperand(1), Op.getOperand(2));
2067 }
Nate Begemanee625572006-01-27 21:09:22 +00002068 case ISD::RET: {
2069 SDOperand Copy;
2070
2071 switch(Op.getNumOperands()) {
2072 default:
2073 assert(0 && "Do not know how to return this many arguments!");
2074 abort();
2075 case 1:
2076 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2077 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2078 case 2: {
2079 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2080 if (MVT::isInteger(ArgVT))
2081 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2082 SDOperand());
2083 else if (!X86ScalarSSE) {
2084 std::vector<MVT::ValueType> Tys;
2085 Tys.push_back(MVT::Other);
2086 Tys.push_back(MVT::Flag);
2087 std::vector<SDOperand> Ops;
2088 Ops.push_back(Op.getOperand(0));
2089 Ops.push_back(Op.getOperand(1));
2090 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2091 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002092 SDOperand MemLoc;
2093 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002094 SDOperand Value = Op.getOperand(1);
2095
Evan Cheng760df292006-02-01 01:19:32 +00002096 if (Value.getOpcode() == ISD::LOAD &&
2097 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002098 Chain = Value.getOperand(0);
2099 MemLoc = Value.getOperand(1);
2100 } else {
2101 // Spill the value to memory and reload it into top of stack.
2102 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2103 MachineFunction &MF = DAG.getMachineFunction();
2104 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2105 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2106 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2107 Value, MemLoc, DAG.getSrcValue(0));
2108 }
Nate Begemanee625572006-01-27 21:09:22 +00002109 std::vector<MVT::ValueType> Tys;
2110 Tys.push_back(MVT::f64);
2111 Tys.push_back(MVT::Other);
2112 std::vector<SDOperand> Ops;
2113 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002114 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002115 Ops.push_back(DAG.getValueType(ArgVT));
2116 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2117 Tys.clear();
2118 Tys.push_back(MVT::Other);
2119 Tys.push_back(MVT::Flag);
2120 Ops.clear();
2121 Ops.push_back(Copy.getValue(1));
2122 Ops.push_back(Copy);
2123 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2124 }
2125 break;
2126 }
2127 case 3:
2128 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2129 SDOperand());
2130 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2131 break;
2132 }
2133 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2134 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2135 Copy.getValue(1));
2136 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002137 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002138}
Evan Cheng72261582005-12-20 06:22:03 +00002139
2140const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2141 switch (Opcode) {
2142 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002143 case X86ISD::SHLD: return "X86ISD::SHLD";
2144 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002145 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002146 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002147 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002148 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002149 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2150 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2151 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002152 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002153 case X86ISD::FST: return "X86ISD::FST";
2154 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002155 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002156 case X86ISD::CALL: return "X86ISD::CALL";
2157 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2158 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2159 case X86ISD::CMP: return "X86ISD::CMP";
2160 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002161 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002162 case X86ISD::CMOV: return "X86ISD::CMOV";
2163 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002164 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002165 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2166 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002167 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002168 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002169 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng72261582005-12-20 06:22:03 +00002170 }
2171}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002172
Nate Begeman368e18d2006-02-16 21:11:51 +00002173void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2174 uint64_t Mask,
2175 uint64_t &KnownZero,
2176 uint64_t &KnownOne,
2177 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002178
2179 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002180 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002181
2182 switch (Opc) {
2183 default:
2184 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2185 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002186 case X86ISD::SETCC:
2187 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2188 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002189 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002190}
Chris Lattner259e97c2006-01-31 19:43:35 +00002191
2192std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002193getRegClassForInlineAsmConstraint(const std::string &Constraint,
2194 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002195 if (Constraint.size() == 1) {
2196 // FIXME: not handling fp-stack yet!
2197 // FIXME: not handling MMX registers yet ('y' constraint).
2198 switch (Constraint[0]) { // GCC X86 Constraint Letters
2199 default: break; // Unknown constriant letter
2200 case 'r': // GENERAL_REGS
2201 case 'R': // LEGACY_REGS
2202 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2203 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2204 case 'l': // INDEX_REGS
2205 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2206 X86::ESI, X86::EDI, X86::EBP, 0);
2207 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2208 case 'Q': // Q_REGS
2209 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2210 case 'x': // SSE_REGS if SSE1 allowed
2211 if (Subtarget->hasSSE1())
2212 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2213 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2214 0);
2215 return std::vector<unsigned>();
2216 case 'Y': // SSE_REGS if SSE2 allowed
2217 if (Subtarget->hasSSE2())
2218 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2219 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2220 0);
2221 return std::vector<unsigned>();
2222 }
2223 }
2224
Chris Lattner1efa40f2006-02-22 00:56:39 +00002225 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002226}
Evan Cheng30b37b52006-03-13 23:18:16 +00002227
2228/// isLegalAddressImmediate - Return true if the integer value or
2229/// GlobalValue can be used as the offset of the target addressing mode.
2230bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2231 // X86 allows a sign-extended 32-bit immediate field.
2232 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2233}
2234
2235bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2236 if (getTargetMachine().
2237 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2238 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2239 if (RModel == Reloc::Static)
2240 return true;
2241 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002242 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002243 else
2244 return false;
2245 } else
2246 return true;
2247}