blob: 4414c6880eb1bdd673deb2dc63576fdab5cb6415 [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);
258 }
259
Evan Cheng470a6ad2006-02-22 02:26:30 +0000260 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
261 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
262 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
263 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
264
Evan Chengd30bf012006-03-01 01:11:20 +0000265 // FIXME: add MMX packed arithmetics
Chris Lattnera064d282006-03-19 01:13:28 +0000266 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
267 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
268 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000269 }
270
271 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
272 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
273
Evan Chengd30bf012006-03-01 01:11:20 +0000274 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
275 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
276 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
277 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Chris Lattnera064d282006-03-19 01:13:28 +0000278 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000279 }
280
281 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
282 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
283 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
284 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
285 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
286 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
287
288
Evan Chengd30bf012006-03-01 01:11:20 +0000289 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
290 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
291 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
292 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Chris Lattnera064d282006-03-19 01:13:28 +0000293 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Expand);
294 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Expand);
295 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Expand);
296 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Expand);
297 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Expand);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000298 }
299
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000300 computeRegisterProperties();
301
Evan Cheng87ed7162006-02-14 08:25:08 +0000302 // FIXME: These should be based on subtarget info. Plus, the values should
303 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000304 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
305 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
306 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000307 allowUnalignedMemoryAccesses = true; // x86 supports it!
308}
309
310std::vector<SDOperand>
311X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
312 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
313 return LowerFastCCArguments(F, DAG);
314 return LowerCCCArguments(F, DAG);
315}
316
317std::pair<SDOperand, SDOperand>
318X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
319 bool isVarArg, unsigned CallingConv,
320 bool isTailCall,
321 SDOperand Callee, ArgListTy &Args,
322 SelectionDAG &DAG) {
323 assert((!isVarArg || CallingConv == CallingConv::C) &&
324 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000325
326 // If the callee is a GlobalAddress node (quite common, every direct call is)
327 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
328 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
329 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000330 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
331 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000332
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000333 if (CallingConv == CallingConv::Fast && EnableFastCC)
334 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
335 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
336}
337
338//===----------------------------------------------------------------------===//
339// C Calling Convention implementation
340//===----------------------------------------------------------------------===//
341
342std::vector<SDOperand>
343X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
344 std::vector<SDOperand> ArgValues;
345
346 MachineFunction &MF = DAG.getMachineFunction();
347 MachineFrameInfo *MFI = MF.getFrameInfo();
348
349 // Add DAG nodes to load the arguments... On entry to a function on the X86,
350 // the stack frame looks like this:
351 //
352 // [ESP] -- return address
353 // [ESP + 4] -- first argument (leftmost lexically)
354 // [ESP + 8] -- second argument, if first argument is four bytes in size
355 // ...
356 //
357 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
358 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
359 MVT::ValueType ObjectVT = getValueType(I->getType());
360 unsigned ArgIncrement = 4;
361 unsigned ObjSize;
362 switch (ObjectVT) {
363 default: assert(0 && "Unhandled argument type!");
364 case MVT::i1:
365 case MVT::i8: ObjSize = 1; break;
366 case MVT::i16: ObjSize = 2; break;
367 case MVT::i32: ObjSize = 4; break;
368 case MVT::i64: ObjSize = ArgIncrement = 8; break;
369 case MVT::f32: ObjSize = 4; break;
370 case MVT::f64: ObjSize = ArgIncrement = 8; break;
371 }
372 // Create the frame index object for this incoming parameter...
373 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
374
375 // Create the SelectionDAG nodes corresponding to a load from this parameter
376 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
377
378 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
379 // dead loads.
380 SDOperand ArgValue;
381 if (!I->use_empty())
382 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
383 DAG.getSrcValue(NULL));
384 else {
385 if (MVT::isInteger(ObjectVT))
386 ArgValue = DAG.getConstant(0, ObjectVT);
387 else
388 ArgValue = DAG.getConstantFP(0, ObjectVT);
389 }
390 ArgValues.push_back(ArgValue);
391
392 ArgOffset += ArgIncrement; // Move on to the next argument...
393 }
394
395 // If the function takes variable number of arguments, make a frame index for
396 // the start of the first vararg value... for expansion of llvm.va_start.
397 if (F.isVarArg())
398 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
399 ReturnAddrIndex = 0; // No return address slot generated yet.
400 BytesToPopOnReturn = 0; // Callee pops nothing.
401 BytesCallerReserves = ArgOffset;
402
403 // Finally, inform the code generator which regs we return values in.
404 switch (getValueType(F.getReturnType())) {
405 default: assert(0 && "Unknown type!");
406 case MVT::isVoid: break;
407 case MVT::i1:
408 case MVT::i8:
409 case MVT::i16:
410 case MVT::i32:
411 MF.addLiveOut(X86::EAX);
412 break;
413 case MVT::i64:
414 MF.addLiveOut(X86::EAX);
415 MF.addLiveOut(X86::EDX);
416 break;
417 case MVT::f32:
418 case MVT::f64:
419 MF.addLiveOut(X86::ST0);
420 break;
421 }
422 return ArgValues;
423}
424
425std::pair<SDOperand, SDOperand>
426X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
427 bool isVarArg, bool isTailCall,
428 SDOperand Callee, ArgListTy &Args,
429 SelectionDAG &DAG) {
430 // Count how many bytes are to be pushed on the stack.
431 unsigned NumBytes = 0;
432
433 if (Args.empty()) {
434 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000435 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000436 } else {
437 for (unsigned i = 0, e = Args.size(); i != e; ++i)
438 switch (getValueType(Args[i].second)) {
439 default: assert(0 && "Unknown value type!");
440 case MVT::i1:
441 case MVT::i8:
442 case MVT::i16:
443 case MVT::i32:
444 case MVT::f32:
445 NumBytes += 4;
446 break;
447 case MVT::i64:
448 case MVT::f64:
449 NumBytes += 8;
450 break;
451 }
452
Chris Lattner94dd2922006-02-13 09:00:43 +0000453 Chain = DAG.getCALLSEQ_START(Chain,
454 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000455
456 // Arguments go on the stack in reverse order, as specified by the ABI.
457 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000458 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000459 std::vector<SDOperand> Stores;
460
461 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
462 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
463 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
464
465 switch (getValueType(Args[i].second)) {
466 default: assert(0 && "Unexpected ValueType for argument!");
467 case MVT::i1:
468 case MVT::i8:
469 case MVT::i16:
470 // Promote the integer to 32 bits. If the input type is signed use a
471 // sign extend, otherwise use a zero extend.
472 if (Args[i].second->isSigned())
473 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
474 else
475 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
476
477 // FALL THROUGH
478 case MVT::i32:
479 case MVT::f32:
480 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
481 Args[i].first, PtrOff,
482 DAG.getSrcValue(NULL)));
483 ArgOffset += 4;
484 break;
485 case MVT::i64:
486 case MVT::f64:
487 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
488 Args[i].first, PtrOff,
489 DAG.getSrcValue(NULL)));
490 ArgOffset += 8;
491 break;
492 }
493 }
494 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
495 }
496
497 std::vector<MVT::ValueType> RetVals;
498 MVT::ValueType RetTyVT = getValueType(RetTy);
499 RetVals.push_back(MVT::Other);
500
501 // The result values produced have to be legal. Promote the result.
502 switch (RetTyVT) {
503 case MVT::isVoid: break;
504 default:
505 RetVals.push_back(RetTyVT);
506 break;
507 case MVT::i1:
508 case MVT::i8:
509 case MVT::i16:
510 RetVals.push_back(MVT::i32);
511 break;
512 case MVT::f32:
513 if (X86ScalarSSE)
514 RetVals.push_back(MVT::f32);
515 else
516 RetVals.push_back(MVT::f64);
517 break;
518 case MVT::i64:
519 RetVals.push_back(MVT::i32);
520 RetVals.push_back(MVT::i32);
521 break;
522 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000523
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000524 std::vector<MVT::ValueType> NodeTys;
525 NodeTys.push_back(MVT::Other); // Returns a chain
526 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
527 std::vector<SDOperand> Ops;
528 Ops.push_back(Chain);
529 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000530
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000531 // FIXME: Do not generate X86ISD::TAILCALL for now.
532 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
533 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000534
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000535 NodeTys.clear();
536 NodeTys.push_back(MVT::Other); // Returns a chain
537 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
538 Ops.clear();
539 Ops.push_back(Chain);
540 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
541 Ops.push_back(DAG.getConstant(0, getPointerTy()));
542 Ops.push_back(InFlag);
543 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
544 InFlag = Chain.getValue(1);
545
546 SDOperand RetVal;
547 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000548 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000549 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000550 case MVT::i1:
551 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000552 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
553 Chain = RetVal.getValue(1);
554 if (RetTyVT == MVT::i1)
555 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
556 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000557 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000558 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
559 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000560 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000561 case MVT::i32:
562 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
563 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000564 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000565 case MVT::i64: {
566 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
567 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
568 Lo.getValue(2));
569 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
570 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000571 break;
572 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000573 case MVT::f32:
574 case MVT::f64: {
575 std::vector<MVT::ValueType> Tys;
576 Tys.push_back(MVT::f64);
577 Tys.push_back(MVT::Other);
578 Tys.push_back(MVT::Flag);
579 std::vector<SDOperand> Ops;
580 Ops.push_back(Chain);
581 Ops.push_back(InFlag);
582 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
583 Chain = RetVal.getValue(1);
584 InFlag = RetVal.getValue(2);
585 if (X86ScalarSSE) {
586 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
587 // shouldn't be necessary except that RFP cannot be live across
588 // multiple blocks. When stackifier is fixed, they can be uncoupled.
589 MachineFunction &MF = DAG.getMachineFunction();
590 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
591 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
592 Tys.clear();
593 Tys.push_back(MVT::Other);
594 Ops.clear();
595 Ops.push_back(Chain);
596 Ops.push_back(RetVal);
597 Ops.push_back(StackSlot);
598 Ops.push_back(DAG.getValueType(RetTyVT));
599 Ops.push_back(InFlag);
600 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
601 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
602 DAG.getSrcValue(NULL));
603 Chain = RetVal.getValue(1);
604 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000605
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000606 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
607 // FIXME: we would really like to remember that this FP_ROUND
608 // operation is okay to eliminate if we allow excess FP precision.
609 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
610 break;
611 }
612 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000613 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000614
615 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000616}
617
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000618//===----------------------------------------------------------------------===//
619// Fast Calling Convention implementation
620//===----------------------------------------------------------------------===//
621//
622// The X86 'fast' calling convention passes up to two integer arguments in
623// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
624// and requires that the callee pop its arguments off the stack (allowing proper
625// tail calls), and has the same return value conventions as C calling convs.
626//
627// This calling convention always arranges for the callee pop value to be 8n+4
628// bytes, which is needed for tail recursion elimination and stack alignment
629// reasons.
630//
631// Note that this can be enhanced in the future to pass fp vals in registers
632// (when we have a global fp allocator) and do other tricks.
633//
634
635/// AddLiveIn - This helper function adds the specified physical register to the
636/// MachineFunction as a live in value. It also creates a corresponding virtual
637/// register for it.
638static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
639 TargetRegisterClass *RC) {
640 assert(RC->contains(PReg) && "Not the correct regclass!");
641 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
642 MF.addLiveIn(PReg, VReg);
643 return VReg;
644}
645
Chris Lattner89fad2c2006-03-17 17:27:47 +0000646// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
647// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
648// EDX". Anything more is illegal.
649//
650// FIXME: The linscan register allocator currently has problem with
651// coallescing. At the time of this writing, whenever it decides to coallesce
652// a physreg with a virtreg, this increases the size of the physreg's live
653// range, and the live range cannot ever be reduced. This causes problems if
654// too many physregs are coalleced with virtregs, which can cause the register
655// allocator to wedge itself.
656//
657// This code triggers this problem more often if we pass args in registers,
658// so disable it until this is fixed.
659//
660// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
661// about code being dead.
662//
663static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000664
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000665
666std::vector<SDOperand>
667X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
668 std::vector<SDOperand> ArgValues;
669
670 MachineFunction &MF = DAG.getMachineFunction();
671 MachineFrameInfo *MFI = MF.getFrameInfo();
672
673 // Add DAG nodes to load the arguments... On entry to a function the stack
674 // frame looks like this:
675 //
676 // [ESP] -- return address
677 // [ESP + 4] -- first nonreg argument (leftmost lexically)
678 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
679 // ...
680 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
681
682 // Keep track of the number of integer regs passed so far. This can be either
683 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
684 // used).
685 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000686
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000687 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
688 MVT::ValueType ObjectVT = getValueType(I->getType());
689 unsigned ArgIncrement = 4;
690 unsigned ObjSize = 0;
691 SDOperand ArgValue;
692
693 switch (ObjectVT) {
694 default: assert(0 && "Unhandled argument type!");
695 case MVT::i1:
696 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000697 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000698 if (!I->use_empty()) {
699 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
700 X86::R8RegisterClass);
701 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
702 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000703 if (ObjectVT == MVT::i1)
704 // FIXME: Should insert a assertzext here.
705 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000706 }
707 ++NumIntRegs;
708 break;
709 }
710
711 ObjSize = 1;
712 break;
713 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000714 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000715 if (!I->use_empty()) {
716 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
717 X86::R16RegisterClass);
718 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
719 DAG.setRoot(ArgValue.getValue(1));
720 }
721 ++NumIntRegs;
722 break;
723 }
724 ObjSize = 2;
725 break;
726 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000727 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000728 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000729 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000730 X86::R32RegisterClass);
731 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
732 DAG.setRoot(ArgValue.getValue(1));
733 }
734 ++NumIntRegs;
735 break;
736 }
737 ObjSize = 4;
738 break;
739 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000740 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000741 if (!I->use_empty()) {
742 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
743 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
744
745 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
746 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
747 DAG.setRoot(Hi.getValue(1));
748
749 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
750 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000751 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000752 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000753 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000754 if (!I->use_empty()) {
755 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
756 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
757 DAG.setRoot(Low.getValue(1));
758
759 // Load the high part from memory.
760 // Create the frame index object for this incoming parameter...
761 int FI = MFI->CreateFixedObject(4, ArgOffset);
762 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
763 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
764 DAG.getSrcValue(NULL));
765 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
766 }
767 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000768 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000769 break;
770 }
771 ObjSize = ArgIncrement = 8;
772 break;
773 case MVT::f32: ObjSize = 4; break;
774 case MVT::f64: ObjSize = ArgIncrement = 8; break;
775 }
776
777 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
778 // dead loads.
779 if (ObjSize && !I->use_empty()) {
780 // Create the frame index object for this incoming parameter...
781 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
782
783 // Create the SelectionDAG nodes corresponding to a load from this
784 // parameter.
785 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
786
787 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
788 DAG.getSrcValue(NULL));
789 } else if (ArgValue.Val == 0) {
790 if (MVT::isInteger(ObjectVT))
791 ArgValue = DAG.getConstant(0, ObjectVT);
792 else
793 ArgValue = DAG.getConstantFP(0, ObjectVT);
794 }
795 ArgValues.push_back(ArgValue);
796
797 if (ObjSize)
798 ArgOffset += ArgIncrement; // Move on to the next argument.
799 }
800
801 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
802 // arguments and the arguments after the retaddr has been pushed are aligned.
803 if ((ArgOffset & 7) == 0)
804 ArgOffset += 4;
805
806 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
807 ReturnAddrIndex = 0; // No return address slot generated yet.
808 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
809 BytesCallerReserves = 0;
810
811 // Finally, inform the code generator which regs we return values in.
812 switch (getValueType(F.getReturnType())) {
813 default: assert(0 && "Unknown type!");
814 case MVT::isVoid: break;
815 case MVT::i1:
816 case MVT::i8:
817 case MVT::i16:
818 case MVT::i32:
819 MF.addLiveOut(X86::EAX);
820 break;
821 case MVT::i64:
822 MF.addLiveOut(X86::EAX);
823 MF.addLiveOut(X86::EDX);
824 break;
825 case MVT::f32:
826 case MVT::f64:
827 MF.addLiveOut(X86::ST0);
828 break;
829 }
830 return ArgValues;
831}
832
833std::pair<SDOperand, SDOperand>
834X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
835 bool isTailCall, SDOperand Callee,
836 ArgListTy &Args, SelectionDAG &DAG) {
837 // Count how many bytes are to be pushed on the stack.
838 unsigned NumBytes = 0;
839
840 // Keep track of the number of integer regs passed so far. This can be either
841 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
842 // used).
843 unsigned NumIntRegs = 0;
844
845 for (unsigned i = 0, e = Args.size(); i != e; ++i)
846 switch (getValueType(Args[i].second)) {
847 default: assert(0 && "Unknown value type!");
848 case MVT::i1:
849 case MVT::i8:
850 case MVT::i16:
851 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000852 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000853 ++NumIntRegs;
854 break;
855 }
856 // fall through
857 case MVT::f32:
858 NumBytes += 4;
859 break;
860 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000861 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
862 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000863 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000864 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
865 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000866 NumBytes += 4;
867 break;
868 }
869
870 // fall through
871 case MVT::f64:
872 NumBytes += 8;
873 break;
874 }
875
876 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
877 // arguments and the arguments after the retaddr has been pushed are aligned.
878 if ((NumBytes & 7) == 0)
879 NumBytes += 4;
880
Chris Lattner94dd2922006-02-13 09:00:43 +0000881 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000882
883 // Arguments go on the stack in reverse order, as specified by the ABI.
884 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000885 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000886 NumIntRegs = 0;
887 std::vector<SDOperand> Stores;
888 std::vector<SDOperand> RegValuesToPass;
889 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
890 switch (getValueType(Args[i].second)) {
891 default: assert(0 && "Unexpected ValueType for argument!");
892 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000893 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
894 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000895 case MVT::i8:
896 case MVT::i16:
897 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000898 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000899 RegValuesToPass.push_back(Args[i].first);
900 ++NumIntRegs;
901 break;
902 }
903 // Fall through
904 case MVT::f32: {
905 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
906 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
907 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
908 Args[i].first, PtrOff,
909 DAG.getSrcValue(NULL)));
910 ArgOffset += 4;
911 break;
912 }
913 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000914 // Can pass (at least) part of it in regs?
915 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000916 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
917 Args[i].first, DAG.getConstant(1, MVT::i32));
918 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
919 Args[i].first, DAG.getConstant(0, MVT::i32));
920 RegValuesToPass.push_back(Lo);
921 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000922
923 // Pass both parts in regs?
924 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000925 RegValuesToPass.push_back(Hi);
926 ++NumIntRegs;
927 } else {
928 // Pass the high part in memory.
929 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
930 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
931 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
932 Hi, PtrOff, DAG.getSrcValue(NULL)));
933 ArgOffset += 4;
934 }
935 break;
936 }
937 // Fall through
938 case MVT::f64:
939 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
940 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
941 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
942 Args[i].first, PtrOff,
943 DAG.getSrcValue(NULL)));
944 ArgOffset += 8;
945 break;
946 }
947 }
948 if (!Stores.empty())
949 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
950
951 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
952 // arguments and the arguments after the retaddr has been pushed are aligned.
953 if ((ArgOffset & 7) == 0)
954 ArgOffset += 4;
955
956 std::vector<MVT::ValueType> RetVals;
957 MVT::ValueType RetTyVT = getValueType(RetTy);
958
959 RetVals.push_back(MVT::Other);
960
961 // The result values produced have to be legal. Promote the result.
962 switch (RetTyVT) {
963 case MVT::isVoid: break;
964 default:
965 RetVals.push_back(RetTyVT);
966 break;
967 case MVT::i1:
968 case MVT::i8:
969 case MVT::i16:
970 RetVals.push_back(MVT::i32);
971 break;
972 case MVT::f32:
973 if (X86ScalarSSE)
974 RetVals.push_back(MVT::f32);
975 else
976 RetVals.push_back(MVT::f64);
977 break;
978 case MVT::i64:
979 RetVals.push_back(MVT::i32);
980 RetVals.push_back(MVT::i32);
981 break;
982 }
983
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000984 // Build a sequence of copy-to-reg nodes chained together with token chain
985 // and flag operands which copy the outgoing args into registers.
986 SDOperand InFlag;
987 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
988 unsigned CCReg;
989 SDOperand RegToPass = RegValuesToPass[i];
990 switch (RegToPass.getValueType()) {
991 default: assert(0 && "Bad thing to pass in regs");
992 case MVT::i8:
993 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000994 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000995 case MVT::i16:
996 CCReg = (i == 0) ? X86::AX : X86::DX;
997 break;
998 case MVT::i32:
999 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1000 break;
1001 }
1002
1003 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1004 InFlag = Chain.getValue(1);
1005 }
1006
1007 std::vector<MVT::ValueType> NodeTys;
1008 NodeTys.push_back(MVT::Other); // Returns a chain
1009 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1010 std::vector<SDOperand> Ops;
1011 Ops.push_back(Chain);
1012 Ops.push_back(Callee);
1013 if (InFlag.Val)
1014 Ops.push_back(InFlag);
1015
1016 // FIXME: Do not generate X86ISD::TAILCALL for now.
1017 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1018 InFlag = Chain.getValue(1);
1019
1020 NodeTys.clear();
1021 NodeTys.push_back(MVT::Other); // Returns a chain
1022 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1023 Ops.clear();
1024 Ops.push_back(Chain);
1025 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1026 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1027 Ops.push_back(InFlag);
1028 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1029 InFlag = Chain.getValue(1);
1030
1031 SDOperand RetVal;
1032 if (RetTyVT != MVT::isVoid) {
1033 switch (RetTyVT) {
1034 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001035 case MVT::i1:
1036 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001037 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1038 Chain = RetVal.getValue(1);
1039 if (RetTyVT == MVT::i1)
1040 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1041 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001042 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001043 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1044 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001045 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001046 case MVT::i32:
1047 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1048 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001049 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001050 case MVT::i64: {
1051 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1052 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1053 Lo.getValue(2));
1054 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1055 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001056 break;
1057 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001058 case MVT::f32:
1059 case MVT::f64: {
1060 std::vector<MVT::ValueType> Tys;
1061 Tys.push_back(MVT::f64);
1062 Tys.push_back(MVT::Other);
1063 Tys.push_back(MVT::Flag);
1064 std::vector<SDOperand> Ops;
1065 Ops.push_back(Chain);
1066 Ops.push_back(InFlag);
1067 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1068 Chain = RetVal.getValue(1);
1069 InFlag = RetVal.getValue(2);
1070 if (X86ScalarSSE) {
1071 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1072 // shouldn't be necessary except that RFP cannot be live across
1073 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1074 MachineFunction &MF = DAG.getMachineFunction();
1075 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1076 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1077 Tys.clear();
1078 Tys.push_back(MVT::Other);
1079 Ops.clear();
1080 Ops.push_back(Chain);
1081 Ops.push_back(RetVal);
1082 Ops.push_back(StackSlot);
1083 Ops.push_back(DAG.getValueType(RetTyVT));
1084 Ops.push_back(InFlag);
1085 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1086 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1087 DAG.getSrcValue(NULL));
1088 Chain = RetVal.getValue(1);
1089 }
Evan Chengd9558e02006-01-06 00:43:03 +00001090
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001091 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1092 // FIXME: we would really like to remember that this FP_ROUND
1093 // operation is okay to eliminate if we allow excess FP precision.
1094 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1095 break;
1096 }
1097 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001098 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001099
1100 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001101}
1102
1103SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1104 if (ReturnAddrIndex == 0) {
1105 // Set up a frame object for the return address.
1106 MachineFunction &MF = DAG.getMachineFunction();
1107 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1108 }
1109
1110 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1111}
1112
1113
1114
1115std::pair<SDOperand, SDOperand> X86TargetLowering::
1116LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1117 SelectionDAG &DAG) {
1118 SDOperand Result;
1119 if (Depth) // Depths > 0 not supported yet!
1120 Result = DAG.getConstant(0, getPointerTy());
1121 else {
1122 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1123 if (!isFrameAddress)
1124 // Just load the return address
1125 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1126 DAG.getSrcValue(NULL));
1127 else
1128 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1129 DAG.getConstant(4, MVT::i32));
1130 }
1131 return std::make_pair(Result, Chain);
1132}
1133
Evan Cheng4a460802006-01-11 00:33:36 +00001134/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1135/// which corresponds to the condition code.
1136static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1137 switch (X86CC) {
1138 default: assert(0 && "Unknown X86 conditional code!");
1139 case X86ISD::COND_A: return X86::JA;
1140 case X86ISD::COND_AE: return X86::JAE;
1141 case X86ISD::COND_B: return X86::JB;
1142 case X86ISD::COND_BE: return X86::JBE;
1143 case X86ISD::COND_E: return X86::JE;
1144 case X86ISD::COND_G: return X86::JG;
1145 case X86ISD::COND_GE: return X86::JGE;
1146 case X86ISD::COND_L: return X86::JL;
1147 case X86ISD::COND_LE: return X86::JLE;
1148 case X86ISD::COND_NE: return X86::JNE;
1149 case X86ISD::COND_NO: return X86::JNO;
1150 case X86ISD::COND_NP: return X86::JNP;
1151 case X86ISD::COND_NS: return X86::JNS;
1152 case X86ISD::COND_O: return X86::JO;
1153 case X86ISD::COND_P: return X86::JP;
1154 case X86ISD::COND_S: return X86::JS;
1155 }
1156}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001157
Evan Cheng6dfa9992006-01-30 23:41:35 +00001158/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1159/// specific condition code. It returns a false if it cannot do a direct
1160/// translation. X86CC is the translated CondCode. Flip is set to true if the
1161/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001162static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1163 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001164 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001165 Flip = false;
1166 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001167 if (!isFP) {
1168 switch (SetCCOpcode) {
1169 default: break;
1170 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1171 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1172 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1173 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1174 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1175 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1176 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1177 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1178 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1179 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1180 }
1181 } else {
1182 // On a floating point condition, the flags are set as follows:
1183 // ZF PF CF op
1184 // 0 | 0 | 0 | X > Y
1185 // 0 | 0 | 1 | X < Y
1186 // 1 | 0 | 0 | X == Y
1187 // 1 | 1 | 1 | unordered
1188 switch (SetCCOpcode) {
1189 default: break;
1190 case ISD::SETUEQ:
1191 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001192 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001193 case ISD::SETOGT:
1194 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001195 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001196 case ISD::SETOGE:
1197 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001198 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001199 case ISD::SETULT:
1200 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001201 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001202 case ISD::SETULE:
1203 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1204 case ISD::SETONE:
1205 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1206 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1207 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1208 }
1209 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001210
1211 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001212}
1213
Evan Cheng4a460802006-01-11 00:33:36 +00001214/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1215/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001216/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001217static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001218 switch (X86CC) {
1219 default:
1220 return false;
1221 case X86ISD::COND_B:
1222 case X86ISD::COND_BE:
1223 case X86ISD::COND_E:
1224 case X86ISD::COND_P:
1225 case X86ISD::COND_A:
1226 case X86ISD::COND_AE:
1227 case X86ISD::COND_NE:
1228 case X86ISD::COND_NP:
1229 return true;
1230 }
1231}
1232
Evan Cheng4a460802006-01-11 00:33:36 +00001233MachineBasicBlock *
1234X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1235 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001236 switch (MI->getOpcode()) {
1237 default: assert(false && "Unexpected instr type to insert");
1238 case X86::CMOV_FR32:
1239 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001240 // To "insert" a SELECT_CC instruction, we actually have to insert the
1241 // diamond control-flow pattern. The incoming instruction knows the
1242 // destination vreg to set, the condition code register to branch on, the
1243 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001244 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1245 ilist<MachineBasicBlock>::iterator It = BB;
1246 ++It;
1247
1248 // thisMBB:
1249 // ...
1250 // TrueVal = ...
1251 // cmpTY ccX, r1, r2
1252 // bCC copy1MBB
1253 // fallthrough --> copy0MBB
1254 MachineBasicBlock *thisMBB = BB;
1255 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1256 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1257 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1258 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1259 MachineFunction *F = BB->getParent();
1260 F->getBasicBlockList().insert(It, copy0MBB);
1261 F->getBasicBlockList().insert(It, sinkMBB);
1262 // Update machine-CFG edges
1263 BB->addSuccessor(copy0MBB);
1264 BB->addSuccessor(sinkMBB);
1265
1266 // copy0MBB:
1267 // %FalseValue = ...
1268 // # fallthrough to sinkMBB
1269 BB = copy0MBB;
1270
1271 // Update machine-CFG edges
1272 BB->addSuccessor(sinkMBB);
1273
1274 // sinkMBB:
1275 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1276 // ...
1277 BB = sinkMBB;
1278 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1279 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1280 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001281
Evan Cheng0cc39452006-01-16 21:21:29 +00001282 delete MI; // The pseudo instruction is gone now.
1283 return BB;
1284 }
Evan Cheng4a460802006-01-11 00:33:36 +00001285
Evan Cheng0cc39452006-01-16 21:21:29 +00001286 case X86::FP_TO_INT16_IN_MEM:
1287 case X86::FP_TO_INT32_IN_MEM:
1288 case X86::FP_TO_INT64_IN_MEM: {
1289 // Change the floating point control register to use "round towards zero"
1290 // mode when truncating to an integer value.
1291 MachineFunction *F = BB->getParent();
1292 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1293 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1294
1295 // Load the old value of the high byte of the control word...
1296 unsigned OldCW =
1297 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1298 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1299
1300 // Set the high part to be round to zero...
1301 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1302
1303 // Reload the modified control word now...
1304 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1305
1306 // Restore the memory image of control word to original value
1307 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1308
1309 // Get the X86 opcode to use.
1310 unsigned Opc;
1311 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001312 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001313 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1314 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1315 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1316 }
1317
1318 X86AddressMode AM;
1319 MachineOperand &Op = MI->getOperand(0);
1320 if (Op.isRegister()) {
1321 AM.BaseType = X86AddressMode::RegBase;
1322 AM.Base.Reg = Op.getReg();
1323 } else {
1324 AM.BaseType = X86AddressMode::FrameIndexBase;
1325 AM.Base.FrameIndex = Op.getFrameIndex();
1326 }
1327 Op = MI->getOperand(1);
1328 if (Op.isImmediate())
1329 AM.Scale = Op.getImmedValue();
1330 Op = MI->getOperand(2);
1331 if (Op.isImmediate())
1332 AM.IndexReg = Op.getImmedValue();
1333 Op = MI->getOperand(3);
1334 if (Op.isGlobalAddress()) {
1335 AM.GV = Op.getGlobal();
1336 } else {
1337 AM.Disp = Op.getImmedValue();
1338 }
1339 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1340
1341 // Reload the original control word now.
1342 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1343
1344 delete MI; // The pseudo instruction is gone now.
1345 return BB;
1346 }
1347 }
Evan Cheng4a460802006-01-11 00:33:36 +00001348}
1349
1350
1351//===----------------------------------------------------------------------===//
1352// X86 Custom Lowering Hooks
1353//===----------------------------------------------------------------------===//
1354
Evan Cheng30b37b52006-03-13 23:18:16 +00001355/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1356/// load. For Darwin, external and weak symbols are indirect, loading the value
1357/// at address GV rather then the value of GV itself. This means that the
1358/// GlobalAddress must be in the base or index register of the address, not the
1359/// GV offset field.
1360static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1361 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1362 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1363}
1364
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001365/// LowerOperation - Provide custom lowering hooks for some operations.
1366///
1367SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1368 switch (Op.getOpcode()) {
1369 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001370 case ISD::SHL_PARTS:
1371 case ISD::SRA_PARTS:
1372 case ISD::SRL_PARTS: {
1373 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1374 "Not an i64 shift!");
1375 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1376 SDOperand ShOpLo = Op.getOperand(0);
1377 SDOperand ShOpHi = Op.getOperand(1);
1378 SDOperand ShAmt = Op.getOperand(2);
1379 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001380 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001381 : DAG.getConstant(0, MVT::i32);
1382
1383 SDOperand Tmp2, Tmp3;
1384 if (Op.getOpcode() == ISD::SHL_PARTS) {
1385 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1386 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1387 } else {
1388 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001389 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001390 }
1391
1392 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1393 ShAmt, DAG.getConstant(32, MVT::i8));
1394
1395 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001396 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001397
1398 std::vector<MVT::ValueType> Tys;
1399 Tys.push_back(MVT::i32);
1400 Tys.push_back(MVT::Flag);
1401 std::vector<SDOperand> Ops;
1402 if (Op.getOpcode() == ISD::SHL_PARTS) {
1403 Ops.push_back(Tmp2);
1404 Ops.push_back(Tmp3);
1405 Ops.push_back(CC);
1406 Ops.push_back(InFlag);
1407 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1408 InFlag = Hi.getValue(1);
1409
1410 Ops.clear();
1411 Ops.push_back(Tmp3);
1412 Ops.push_back(Tmp1);
1413 Ops.push_back(CC);
1414 Ops.push_back(InFlag);
1415 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1416 } else {
1417 Ops.push_back(Tmp2);
1418 Ops.push_back(Tmp3);
1419 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001420 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001421 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1422 InFlag = Lo.getValue(1);
1423
1424 Ops.clear();
1425 Ops.push_back(Tmp3);
1426 Ops.push_back(Tmp1);
1427 Ops.push_back(CC);
1428 Ops.push_back(InFlag);
1429 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1430 }
1431
1432 Tys.clear();
1433 Tys.push_back(MVT::i32);
1434 Tys.push_back(MVT::i32);
1435 Ops.clear();
1436 Ops.push_back(Lo);
1437 Ops.push_back(Hi);
1438 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1439 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001440 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001441 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001442 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001443 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001444
1445 SDOperand Result;
1446 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1447 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001448 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001449 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001450 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001451 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1452 DAG.getEntryNode(), Op.getOperand(0),
1453 StackSlot, DAG.getSrcValue(NULL));
1454
1455 // Build the FILD
1456 std::vector<MVT::ValueType> Tys;
1457 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001458 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001459 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001460 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001461 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001462 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001463 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001464 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1465 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001466
1467 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001468 Chain = Result.getValue(1);
1469 SDOperand InFlag = Result.getValue(2);
1470
Evan Chenge3de85b2006-02-04 02:20:30 +00001471 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001472 // shouldn't be necessary except that RFP cannot be live across
1473 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1474 MachineFunction &MF = DAG.getMachineFunction();
1475 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1476 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1477 std::vector<MVT::ValueType> Tys;
1478 Tys.push_back(MVT::Other);
1479 std::vector<SDOperand> Ops;
1480 Ops.push_back(Chain);
1481 Ops.push_back(Result);
1482 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001483 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001484 Ops.push_back(InFlag);
1485 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1486 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1487 DAG.getSrcValue(NULL));
1488 }
1489
Evan Chenga3195e82006-01-12 22:54:21 +00001490 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001491 }
1492 case ISD::FP_TO_SINT: {
1493 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001494 "Unknown FP_TO_SINT to lower!");
1495 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1496 // stack slot.
1497 MachineFunction &MF = DAG.getMachineFunction();
1498 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1499 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1500 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1501
1502 unsigned Opc;
1503 switch (Op.getValueType()) {
1504 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1505 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1506 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1507 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1508 }
1509
Evan Cheng6dab0532006-01-30 08:02:57 +00001510 SDOperand Chain = DAG.getEntryNode();
1511 SDOperand Value = Op.getOperand(0);
1512 if (X86ScalarSSE) {
1513 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1514 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1515 DAG.getSrcValue(0));
1516 std::vector<MVT::ValueType> Tys;
1517 Tys.push_back(MVT::f64);
1518 Tys.push_back(MVT::Other);
1519 std::vector<SDOperand> Ops;
1520 Ops.push_back(Chain);
1521 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001522 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001523 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1524 Chain = Value.getValue(1);
1525 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1526 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1527 }
1528
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001529 // Build the FP_TO_INT*_IN_MEM
1530 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001531 Ops.push_back(Chain);
1532 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001533 Ops.push_back(StackSlot);
1534 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1535
1536 // Load the result.
1537 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1538 DAG.getSrcValue(NULL));
1539 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001540 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001541 std::vector<MVT::ValueType> Tys;
1542 Tys.push_back(MVT::Other);
1543 Tys.push_back(MVT::Flag);
1544 std::vector<SDOperand> Ops;
1545 Ops.push_back(Op.getOperand(0));
1546 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001547 Ops.clear();
1548 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1549 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1550 MVT::i32, Ops[0].getValue(2)));
1551 Ops.push_back(Ops[1].getValue(1));
1552 Tys[0] = Tys[1] = MVT::i32;
1553 Tys.push_back(MVT::Other);
1554 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001555 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001556 case ISD::FABS: {
1557 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001558 const Type *OpNTy = MVT::getTypeForValueType(VT);
1559 std::vector<Constant*> CV;
1560 if (VT == MVT::f64) {
1561 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1562 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1563 } else {
1564 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1565 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1566 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1567 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1568 }
1569 Constant *CS = ConstantStruct::get(CV);
1570 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1571 SDOperand Mask
1572 = DAG.getNode(X86ISD::LOAD_PACK,
1573 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001574 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1575 }
Evan Cheng223547a2006-01-31 22:28:30 +00001576 case ISD::FNEG: {
1577 MVT::ValueType VT = Op.getValueType();
1578 const Type *OpNTy = MVT::getTypeForValueType(VT);
1579 std::vector<Constant*> CV;
1580 if (VT == MVT::f64) {
1581 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1582 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1583 } else {
1584 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1585 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1586 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1587 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1588 }
1589 Constant *CS = ConstantStruct::get(CV);
1590 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1591 SDOperand Mask
1592 = DAG.getNode(X86ISD::LOAD_PACK,
1593 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1594 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1595 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001596 case ISD::SETCC: {
1597 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001598 SDOperand Cond;
1599 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001600 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1601 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001602 bool Flip;
1603 unsigned X86CC;
1604 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1605 if (Flip)
1606 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1607 Op.getOperand(1), Op.getOperand(0));
1608 else
1609 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1610 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001611 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1612 DAG.getConstant(X86CC, MVT::i8), Cond);
1613 } else {
1614 assert(isFP && "Illegal integer SetCC!");
1615
Evan Cheng6dfa9992006-01-30 23:41:35 +00001616 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1617 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001618 std::vector<MVT::ValueType> Tys;
1619 std::vector<SDOperand> Ops;
1620 switch (SetCCOpcode) {
1621 default: assert(false && "Illegal floating point SetCC!");
1622 case ISD::SETOEQ: { // !PF & ZF
1623 Tys.push_back(MVT::i8);
1624 Tys.push_back(MVT::Flag);
1625 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1626 Ops.push_back(Cond);
1627 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1628 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1629 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1630 Tmp1.getValue(1));
1631 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1632 }
Evan Chengd9558e02006-01-06 00:43:03 +00001633 case ISD::SETUNE: { // PF | !ZF
1634 Tys.push_back(MVT::i8);
1635 Tys.push_back(MVT::Flag);
1636 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1637 Ops.push_back(Cond);
1638 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1639 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1640 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1641 Tmp1.getValue(1));
1642 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1643 }
1644 }
1645 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001646 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001647 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001648 MVT::ValueType VT = Op.getValueType();
1649 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001650 bool isFPStack = isFP && !X86ScalarSSE;
1651 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001652 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001653 SDOperand Op0 = Op.getOperand(0);
1654 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001655 if (Op0.getOpcode() == ISD::SETCC)
1656 Op0 = LowerOperation(Op0, DAG);
1657
Evan Chengaaca22c2006-01-10 20:26:56 +00001658 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001659 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1660 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1661 // have another use it will be eliminated.
1662 // If the X86ISD::SETCC has more than one use, then it's probably better
1663 // to use a test instead of duplicating the X86ISD::CMP (for register
1664 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001665 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1666 if (!Op0.hasOneUse()) {
1667 std::vector<MVT::ValueType> Tys;
1668 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1669 Tys.push_back(Op0.Val->getValueType(i));
1670 std::vector<SDOperand> Ops;
1671 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1672 Ops.push_back(Op0.getOperand(i));
1673 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1674 }
1675
Evan Cheng1bcee362006-01-13 01:03:02 +00001676 CC = Op0.getOperand(0);
1677 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001678 // Make a copy as flag result cannot be used by more than one.
1679 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1680 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001681 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001682 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001683 } else
1684 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001685 } else
1686 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001687
Evan Cheng189d01e2006-01-13 01:06:49 +00001688 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001689 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001690 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001691 }
Evan Chenge3413162006-01-09 18:33:28 +00001692
1693 std::vector<MVT::ValueType> Tys;
1694 Tys.push_back(Op.getValueType());
1695 Tys.push_back(MVT::Flag);
1696 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001697 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1698 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001699 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001700 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001701 Ops.push_back(CC);
1702 Ops.push_back(Cond);
1703 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001704 }
Evan Cheng898101c2005-12-19 23:12:38 +00001705 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001706 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001707 SDOperand Cond = Op.getOperand(1);
1708 SDOperand Dest = Op.getOperand(2);
1709 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001710 if (Cond.getOpcode() == ISD::SETCC)
1711 Cond = LowerOperation(Cond, DAG);
1712
Evan Chengd5781fc2005-12-21 20:21:51 +00001713 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001714 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1715 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1716 // have another use it will be eliminated.
1717 // If the X86ISD::SETCC has more than one use, then it's probably better
1718 // to use a test instead of duplicating the X86ISD::CMP (for register
1719 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001720 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1721 if (!Cond.hasOneUse()) {
1722 std::vector<MVT::ValueType> Tys;
1723 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1724 Tys.push_back(Cond.Val->getValueType(i));
1725 std::vector<SDOperand> Ops;
1726 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1727 Ops.push_back(Cond.getOperand(i));
1728 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1729 }
1730
Evan Cheng1bcee362006-01-13 01:03:02 +00001731 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001732 Cond = Cond.getOperand(1);
1733 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001734 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001735 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001736 } else
1737 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001738 } else
1739 addTest = true;
1740
1741 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001742 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001743 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1744 }
1745 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1746 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1747 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001748 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001749 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001750 SDOperand Chain = Op.getOperand(0);
1751 unsigned Align =
1752 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1753 if (Align == 0) Align = 1;
1754
Evan Cheng18a84522006-02-16 00:21:07 +00001755 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1756 // If not DWORD aligned, call memset if size is less than the threshold.
1757 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001758 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001759 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001760 MVT::ValueType IntPtr = getPointerTy();
1761 const Type *IntPtrTy = getTargetData().getIntPtrType();
1762 std::vector<std::pair<SDOperand, const Type*> > Args;
1763 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1764 // Extend the ubyte argument to be an int value for the call.
1765 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1766 Args.push_back(std::make_pair(Val, IntPtrTy));
1767 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1768 std::pair<SDOperand,SDOperand> CallResult =
1769 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1770 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1771 return CallResult.second;
1772 }
1773
Evan Cheng67f92a72006-01-11 22:15:48 +00001774 MVT::ValueType AVT;
1775 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001776 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1777 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001778 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001779 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001780 unsigned ValReg;
1781 unsigned Val = ValC->getValue() & 255;
1782
1783 // If the value is a constant, then we can potentially use larger sets.
1784 switch (Align & 3) {
1785 case 2: // WORD aligned
1786 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001787 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1788 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001789 Val = (Val << 8) | Val;
1790 ValReg = X86::AX;
1791 break;
1792 case 0: // DWORD aligned
1793 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001794 if (I) {
1795 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1796 BytesLeft = I->getValue() % 4;
1797 } else {
1798 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1799 DAG.getConstant(2, MVT::i8));
1800 TwoRepStos = true;
1801 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001802 Val = (Val << 8) | Val;
1803 Val = (Val << 16) | Val;
1804 ValReg = X86::EAX;
1805 break;
1806 default: // Byte aligned
1807 AVT = MVT::i8;
1808 Count = Op.getOperand(3);
1809 ValReg = X86::AL;
1810 break;
1811 }
1812
1813 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1814 InFlag);
1815 InFlag = Chain.getValue(1);
1816 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001817 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001818 Count = Op.getOperand(3);
1819 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1820 InFlag = Chain.getValue(1);
1821 }
1822
1823 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1824 InFlag = Chain.getValue(1);
1825 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1826 InFlag = Chain.getValue(1);
1827
Evan Chengff909922006-03-07 23:29:39 +00001828 std::vector<MVT::ValueType> Tys;
1829 Tys.push_back(MVT::Other);
1830 Tys.push_back(MVT::Flag);
1831 std::vector<SDOperand> Ops;
1832 Ops.push_back(Chain);
1833 Ops.push_back(DAG.getValueType(AVT));
1834 Ops.push_back(InFlag);
1835 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1836
1837 if (TwoRepStos) {
1838 InFlag = Chain.getValue(1);
1839 Count = Op.getOperand(3);
1840 MVT::ValueType CVT = Count.getValueType();
1841 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1842 DAG.getConstant(3, CVT));
1843 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1844 InFlag = Chain.getValue(1);
1845 Tys.clear();
1846 Tys.push_back(MVT::Other);
1847 Tys.push_back(MVT::Flag);
1848 Ops.clear();
1849 Ops.push_back(Chain);
1850 Ops.push_back(DAG.getValueType(MVT::i8));
1851 Ops.push_back(InFlag);
1852 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1853 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001854 // Issue stores for the last 1 - 3 bytes.
1855 SDOperand Value;
1856 unsigned Val = ValC->getValue() & 255;
1857 unsigned Offset = I->getValue() - BytesLeft;
1858 SDOperand DstAddr = Op.getOperand(1);
1859 MVT::ValueType AddrVT = DstAddr.getValueType();
1860 if (BytesLeft >= 2) {
1861 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1862 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1863 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1864 DAG.getConstant(Offset, AddrVT)),
1865 DAG.getSrcValue(NULL));
1866 BytesLeft -= 2;
1867 Offset += 2;
1868 }
1869
1870 if (BytesLeft == 1) {
1871 Value = DAG.getConstant(Val, MVT::i8);
1872 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1873 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1874 DAG.getConstant(Offset, AddrVT)),
1875 DAG.getSrcValue(NULL));
1876 }
1877 }
1878
1879 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001880 }
1881 case ISD::MEMCPY: {
1882 SDOperand Chain = Op.getOperand(0);
1883 unsigned Align =
1884 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1885 if (Align == 0) Align = 1;
1886
Evan Cheng18a84522006-02-16 00:21:07 +00001887 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1888 // If not DWORD aligned, call memcpy if size is less than the threshold.
1889 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001890 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001891 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001892 MVT::ValueType IntPtr = getPointerTy();
1893 const Type *IntPtrTy = getTargetData().getIntPtrType();
1894 std::vector<std::pair<SDOperand, const Type*> > Args;
1895 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1896 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1897 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1898 std::pair<SDOperand,SDOperand> CallResult =
1899 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1900 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1901 return CallResult.second;
1902 }
1903
Evan Cheng67f92a72006-01-11 22:15:48 +00001904 MVT::ValueType AVT;
1905 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001906 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001907 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001908 switch (Align & 3) {
1909 case 2: // WORD aligned
1910 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001911 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1912 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001913 break;
1914 case 0: // DWORD aligned
1915 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001916 if (I) {
1917 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1918 BytesLeft = I->getValue() % 4;
1919 } else {
1920 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1921 DAG.getConstant(2, MVT::i8));
1922 TwoRepMovs = true;
1923 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001924 break;
1925 default: // Byte aligned
1926 AVT = MVT::i8;
1927 Count = Op.getOperand(3);
1928 break;
1929 }
1930
Evan Cheng62bec2c2006-03-04 02:48:56 +00001931 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001932 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1933 InFlag = Chain.getValue(1);
1934 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1935 InFlag = Chain.getValue(1);
1936 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1937 InFlag = Chain.getValue(1);
1938
Evan Chengff909922006-03-07 23:29:39 +00001939 std::vector<MVT::ValueType> Tys;
1940 Tys.push_back(MVT::Other);
1941 Tys.push_back(MVT::Flag);
1942 std::vector<SDOperand> Ops;
1943 Ops.push_back(Chain);
1944 Ops.push_back(DAG.getValueType(AVT));
1945 Ops.push_back(InFlag);
1946 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1947
1948 if (TwoRepMovs) {
1949 InFlag = Chain.getValue(1);
1950 Count = Op.getOperand(3);
1951 MVT::ValueType CVT = Count.getValueType();
1952 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1953 DAG.getConstant(3, CVT));
1954 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1955 InFlag = Chain.getValue(1);
1956 Tys.clear();
1957 Tys.push_back(MVT::Other);
1958 Tys.push_back(MVT::Flag);
1959 Ops.clear();
1960 Ops.push_back(Chain);
1961 Ops.push_back(DAG.getValueType(MVT::i8));
1962 Ops.push_back(InFlag);
1963 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1964 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001965 // Issue loads and stores for the last 1 - 3 bytes.
1966 unsigned Offset = I->getValue() - BytesLeft;
1967 SDOperand DstAddr = Op.getOperand(1);
1968 MVT::ValueType DstVT = DstAddr.getValueType();
1969 SDOperand SrcAddr = Op.getOperand(2);
1970 MVT::ValueType SrcVT = SrcAddr.getValueType();
1971 SDOperand Value;
1972 if (BytesLeft >= 2) {
1973 Value = DAG.getLoad(MVT::i16, Chain,
1974 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1975 DAG.getConstant(Offset, SrcVT)),
1976 DAG.getSrcValue(NULL));
1977 Chain = Value.getValue(1);
1978 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1979 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1980 DAG.getConstant(Offset, DstVT)),
1981 DAG.getSrcValue(NULL));
1982 BytesLeft -= 2;
1983 Offset += 2;
1984 }
1985
1986 if (BytesLeft == 1) {
1987 Value = DAG.getLoad(MVT::i8, Chain,
1988 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1989 DAG.getConstant(Offset, SrcVT)),
1990 DAG.getSrcValue(NULL));
1991 Chain = Value.getValue(1);
1992 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1993 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1994 DAG.getConstant(Offset, DstVT)),
1995 DAG.getSrcValue(NULL));
1996 }
1997 }
1998
1999 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00002000 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00002001
2002 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2003 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2004 // one of the above mentioned nodes. It has to be wrapped because otherwise
2005 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2006 // be used to form addressing mode. These wrapped nodes will be selected
2007 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002008 case ISD::ConstantPool: {
2009 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002010 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2011 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2012 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00002013 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002014 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002015 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002016 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2017 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2018 }
2019
2020 return Result;
2021 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002022 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002023 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2024 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2025 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00002026 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00002027 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002028 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002029 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002030 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2031 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002032
2033 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002034 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002035 // the GlobalAddress must be in the base or index register of the address,
2036 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002037 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002038 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002039 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002040 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002041 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002042
Evan Cheng002fe9b2006-01-12 07:56:47 +00002043 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002044 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002045 case ISD::ExternalSymbol: {
2046 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2047 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2048 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2049 if (getTargetMachine().
2050 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2051 // With PIC, the address is actually $g + Offset.
2052 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2053 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2054 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2055 }
2056
2057 return Result;
2058 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002059 case ISD::VASTART: {
2060 // vastart just stores the address of the VarArgsFrameIndex slot into the
2061 // memory location argument.
2062 // FIXME: Replace MVT::i32 with PointerTy
2063 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2064 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2065 Op.getOperand(1), Op.getOperand(2));
2066 }
Nate Begemanee625572006-01-27 21:09:22 +00002067 case ISD::RET: {
2068 SDOperand Copy;
2069
2070 switch(Op.getNumOperands()) {
2071 default:
2072 assert(0 && "Do not know how to return this many arguments!");
2073 abort();
2074 case 1:
2075 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2076 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2077 case 2: {
2078 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2079 if (MVT::isInteger(ArgVT))
2080 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2081 SDOperand());
2082 else if (!X86ScalarSSE) {
2083 std::vector<MVT::ValueType> Tys;
2084 Tys.push_back(MVT::Other);
2085 Tys.push_back(MVT::Flag);
2086 std::vector<SDOperand> Ops;
2087 Ops.push_back(Op.getOperand(0));
2088 Ops.push_back(Op.getOperand(1));
2089 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2090 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002091 SDOperand MemLoc;
2092 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002093 SDOperand Value = Op.getOperand(1);
2094
Evan Cheng760df292006-02-01 01:19:32 +00002095 if (Value.getOpcode() == ISD::LOAD &&
2096 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002097 Chain = Value.getOperand(0);
2098 MemLoc = Value.getOperand(1);
2099 } else {
2100 // Spill the value to memory and reload it into top of stack.
2101 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2102 MachineFunction &MF = DAG.getMachineFunction();
2103 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2104 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2105 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2106 Value, MemLoc, DAG.getSrcValue(0));
2107 }
Nate Begemanee625572006-01-27 21:09:22 +00002108 std::vector<MVT::ValueType> Tys;
2109 Tys.push_back(MVT::f64);
2110 Tys.push_back(MVT::Other);
2111 std::vector<SDOperand> Ops;
2112 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002113 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002114 Ops.push_back(DAG.getValueType(ArgVT));
2115 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2116 Tys.clear();
2117 Tys.push_back(MVT::Other);
2118 Tys.push_back(MVT::Flag);
2119 Ops.clear();
2120 Ops.push_back(Copy.getValue(1));
2121 Ops.push_back(Copy);
2122 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2123 }
2124 break;
2125 }
2126 case 3:
2127 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2128 SDOperand());
2129 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2130 break;
2131 }
2132 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2133 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2134 Copy.getValue(1));
2135 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002136 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002137}
Evan Cheng72261582005-12-20 06:22:03 +00002138
2139const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2140 switch (Opcode) {
2141 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002142 case X86ISD::SHLD: return "X86ISD::SHLD";
2143 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002144 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002145 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002146 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002147 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002148 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2149 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2150 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002151 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002152 case X86ISD::FST: return "X86ISD::FST";
2153 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002154 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002155 case X86ISD::CALL: return "X86ISD::CALL";
2156 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2157 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2158 case X86ISD::CMP: return "X86ISD::CMP";
2159 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002160 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002161 case X86ISD::CMOV: return "X86ISD::CMOV";
2162 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002163 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002164 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2165 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002166 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002167 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002168 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng72261582005-12-20 06:22:03 +00002169 }
2170}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002171
Nate Begeman368e18d2006-02-16 21:11:51 +00002172void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2173 uint64_t Mask,
2174 uint64_t &KnownZero,
2175 uint64_t &KnownOne,
2176 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002177
2178 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002179 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002180
2181 switch (Opc) {
2182 default:
2183 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2184 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002185 case X86ISD::SETCC:
2186 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2187 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002188 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002189}
Chris Lattner259e97c2006-01-31 19:43:35 +00002190
2191std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002192getRegClassForInlineAsmConstraint(const std::string &Constraint,
2193 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002194 if (Constraint.size() == 1) {
2195 // FIXME: not handling fp-stack yet!
2196 // FIXME: not handling MMX registers yet ('y' constraint).
2197 switch (Constraint[0]) { // GCC X86 Constraint Letters
2198 default: break; // Unknown constriant letter
2199 case 'r': // GENERAL_REGS
2200 case 'R': // LEGACY_REGS
2201 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2202 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2203 case 'l': // INDEX_REGS
2204 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2205 X86::ESI, X86::EDI, X86::EBP, 0);
2206 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2207 case 'Q': // Q_REGS
2208 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2209 case 'x': // SSE_REGS if SSE1 allowed
2210 if (Subtarget->hasSSE1())
2211 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2212 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2213 0);
2214 return std::vector<unsigned>();
2215 case 'Y': // SSE_REGS if SSE2 allowed
2216 if (Subtarget->hasSSE2())
2217 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2218 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2219 0);
2220 return std::vector<unsigned>();
2221 }
2222 }
2223
Chris Lattner1efa40f2006-02-22 00:56:39 +00002224 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002225}
Evan Cheng30b37b52006-03-13 23:18:16 +00002226
2227/// isLegalAddressImmediate - Return true if the integer value or
2228/// GlobalValue can be used as the offset of the target addressing mode.
2229bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2230 // X86 allows a sign-extended 32-bit immediate field.
2231 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2232}
2233
2234bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2235 if (getTargetMachine().
2236 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2237 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2238 if (RModel == Reloc::Static)
2239 return true;
2240 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002241 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002242 else
2243 return false;
2244 } else
2245 return true;
2246}