blob: 7374855c4abc81d363a49b75386942d0ad8b98ea [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
53 // Add legal addressing mode scale values.
54 addLegalAddressScale(8);
55 addLegalAddressScale(4);
56 addLegalAddressScale(2);
57 // Enter the ones which require both scale + index last. These are more
58 // expensive.
59 addLegalAddressScale(9);
60 addLegalAddressScale(5);
61 addLegalAddressScale(3);
Chris Lattnera54aa942006-01-29 06:26:08 +000062
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000063 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 addRegisterClass(MVT::i8, X86::R8RegisterClass);
65 addRegisterClass(MVT::i16, X86::R16RegisterClass);
66 addRegisterClass(MVT::i32, X86::R32RegisterClass);
67
68 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
69 // operation.
70 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
71 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
72 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000073
74 if (X86ScalarSSE)
75 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
76 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
77 else
78 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000079
80 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
81 // this operation.
82 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
83 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000084 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000085 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000086 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000087 else {
88 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
89 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
90 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000091
Evan Cheng6dab0532006-01-30 08:02:57 +000092 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
93 // isn't legal.
94 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
95 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
96
Evan Cheng02568ff2006-01-30 22:13:22 +000097 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
98 // this operation.
99 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
100 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
101
102 if (X86ScalarSSE) {
103 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
104 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000106 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000107 }
108
109 // Handle FP_TO_UINT by promoting the destination to a larger signed
110 // conversion.
111 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
112 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
113 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
114
Evan Cheng45af8fd2006-02-18 07:26:17 +0000115 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000116 // Expand FP_TO_UINT into a select.
117 // FIXME: We would like to use a Custom expander here eventually to do
118 // the optimal thing for SSE vs. the default expansion in the legalizer.
119 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
120 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000121 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000122 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
123
Evan Cheng02568ff2006-01-30 22:13:22 +0000124 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
125 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000126
Evan Cheng5298bcc2006-02-17 07:01:52 +0000127 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000128 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
129 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000130 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
131 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000132 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000133 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
134 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
135 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
136 setOperationAction(ISD::FREM , MVT::f64 , Expand);
137 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
138 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
139 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
140 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
141 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
142 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
143 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
144 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
145 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000146 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000147 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000148
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000149 // These should be promoted to a larger select which is supported.
150 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
151 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000152
153 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000154 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
155 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
156 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
157 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
158 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
159 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
160 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
161 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
162 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000163 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000164 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000165 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000166 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000167 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000168 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000169 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000170 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
171 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
172 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000173 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000174 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
175 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000176
Chris Lattnerf73bae12005-11-29 06:16:21 +0000177 // We don't have line number support yet.
178 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000179 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000180 // FIXME - use subtarget debug flags
181 if (!TM.getSubtarget<X86Subtarget>().isTargetDarwin())
182 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000183
Nate Begemanacc398c2006-01-25 18:21:52 +0000184 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
185 setOperationAction(ISD::VASTART , MVT::Other, Custom);
186
187 // Use the default implementation.
188 setOperationAction(ISD::VAARG , MVT::Other, Expand);
189 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
190 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000191 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
192 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
193 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000194
Chris Lattner9601a862006-03-05 05:08:37 +0000195 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
196 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
197
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000198 if (X86ScalarSSE) {
199 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000200 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
201 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000202
203 // SSE has no load+extend ops
204 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
205 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
206
Evan Cheng223547a2006-01-31 22:28:30 +0000207 // Use ANDPD to simulate FABS.
208 setOperationAction(ISD::FABS , MVT::f64, Custom);
209 setOperationAction(ISD::FABS , MVT::f32, Custom);
210
211 // Use XORP to simulate FNEG.
212 setOperationAction(ISD::FNEG , MVT::f64, Custom);
213 setOperationAction(ISD::FNEG , MVT::f32, Custom);
214
Evan Chengd25e9e82006-02-02 00:28:23 +0000215 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000216 setOperationAction(ISD::FSIN , MVT::f64, Expand);
217 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 setOperationAction(ISD::FREM , MVT::f64, Expand);
219 setOperationAction(ISD::FSIN , MVT::f32, Expand);
220 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000221 setOperationAction(ISD::FREM , MVT::f32, Expand);
222
Chris Lattnera54aa942006-01-29 06:26:08 +0000223 // Expand FP immediates into loads from the stack, except for the special
224 // cases we handle.
225 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
226 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000227 addLegalFPImmediate(+0.0); // xorps / xorpd
228 } else {
229 // Set up the FP register classes.
230 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000231
232 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
233
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000234 if (!UnsafeFPMath) {
235 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
236 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
237 }
238
Chris Lattnera54aa942006-01-29 06:26:08 +0000239 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 addLegalFPImmediate(+0.0); // FLD0
241 addLegalFPImmediate(+1.0); // FLD1
242 addLegalFPImmediate(-0.0); // FLD0/FCHS
243 addLegalFPImmediate(-1.0); // FLD1/FCHS
244 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000245
Evan Chengd30bf012006-03-01 01:11:20 +0000246 // First set operation action for all vector types to expand. Then we
247 // will selectively turn on ones that can be effectively codegen'd.
248 for (unsigned VT = (unsigned)MVT::Vector + 1;
249 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
250 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
251 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
252 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
253 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
254 }
255
Evan Cheng470a6ad2006-02-22 02:26:30 +0000256 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
257 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
258 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
259 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
260
Evan Chengd30bf012006-03-01 01:11:20 +0000261 // FIXME: add MMX packed arithmetics
Evan Cheng470a6ad2006-02-22 02:26:30 +0000262 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
263 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
264 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
265 }
266
267 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
268 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
269
Evan Chengd30bf012006-03-01 01:11:20 +0000270 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
271 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
272 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
273 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000274 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
275 }
276
277 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
278 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
279 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
280 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
281 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
282 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
283
284
Evan Chengd30bf012006-03-01 01:11:20 +0000285 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
286 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
287 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
288 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000289 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
290 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
291 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
292 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
293 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
294 }
295
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000296 computeRegisterProperties();
297
Evan Cheng87ed7162006-02-14 08:25:08 +0000298 // FIXME: These should be based on subtarget info. Plus, the values should
299 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000300 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
301 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
302 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000303 allowUnalignedMemoryAccesses = true; // x86 supports it!
304}
305
306std::vector<SDOperand>
307X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
308 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
309 return LowerFastCCArguments(F, DAG);
310 return LowerCCCArguments(F, DAG);
311}
312
313std::pair<SDOperand, SDOperand>
314X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
315 bool isVarArg, unsigned CallingConv,
316 bool isTailCall,
317 SDOperand Callee, ArgListTy &Args,
318 SelectionDAG &DAG) {
319 assert((!isVarArg || CallingConv == CallingConv::C) &&
320 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000321
322 // If the callee is a GlobalAddress node (quite common, every direct call is)
323 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
324 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
325 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000326 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
327 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000328
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000329 if (CallingConv == CallingConv::Fast && EnableFastCC)
330 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
331 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
332}
333
334//===----------------------------------------------------------------------===//
335// C Calling Convention implementation
336//===----------------------------------------------------------------------===//
337
338std::vector<SDOperand>
339X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
340 std::vector<SDOperand> ArgValues;
341
342 MachineFunction &MF = DAG.getMachineFunction();
343 MachineFrameInfo *MFI = MF.getFrameInfo();
344
345 // Add DAG nodes to load the arguments... On entry to a function on the X86,
346 // the stack frame looks like this:
347 //
348 // [ESP] -- return address
349 // [ESP + 4] -- first argument (leftmost lexically)
350 // [ESP + 8] -- second argument, if first argument is four bytes in size
351 // ...
352 //
353 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
354 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
355 MVT::ValueType ObjectVT = getValueType(I->getType());
356 unsigned ArgIncrement = 4;
357 unsigned ObjSize;
358 switch (ObjectVT) {
359 default: assert(0 && "Unhandled argument type!");
360 case MVT::i1:
361 case MVT::i8: ObjSize = 1; break;
362 case MVT::i16: ObjSize = 2; break;
363 case MVT::i32: ObjSize = 4; break;
364 case MVT::i64: ObjSize = ArgIncrement = 8; break;
365 case MVT::f32: ObjSize = 4; break;
366 case MVT::f64: ObjSize = ArgIncrement = 8; break;
367 }
368 // Create the frame index object for this incoming parameter...
369 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
370
371 // Create the SelectionDAG nodes corresponding to a load from this parameter
372 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
373
374 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
375 // dead loads.
376 SDOperand ArgValue;
377 if (!I->use_empty())
378 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
379 DAG.getSrcValue(NULL));
380 else {
381 if (MVT::isInteger(ObjectVT))
382 ArgValue = DAG.getConstant(0, ObjectVT);
383 else
384 ArgValue = DAG.getConstantFP(0, ObjectVT);
385 }
386 ArgValues.push_back(ArgValue);
387
388 ArgOffset += ArgIncrement; // Move on to the next argument...
389 }
390
391 // If the function takes variable number of arguments, make a frame index for
392 // the start of the first vararg value... for expansion of llvm.va_start.
393 if (F.isVarArg())
394 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
395 ReturnAddrIndex = 0; // No return address slot generated yet.
396 BytesToPopOnReturn = 0; // Callee pops nothing.
397 BytesCallerReserves = ArgOffset;
398
399 // Finally, inform the code generator which regs we return values in.
400 switch (getValueType(F.getReturnType())) {
401 default: assert(0 && "Unknown type!");
402 case MVT::isVoid: break;
403 case MVT::i1:
404 case MVT::i8:
405 case MVT::i16:
406 case MVT::i32:
407 MF.addLiveOut(X86::EAX);
408 break;
409 case MVT::i64:
410 MF.addLiveOut(X86::EAX);
411 MF.addLiveOut(X86::EDX);
412 break;
413 case MVT::f32:
414 case MVT::f64:
415 MF.addLiveOut(X86::ST0);
416 break;
417 }
418 return ArgValues;
419}
420
421std::pair<SDOperand, SDOperand>
422X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
423 bool isVarArg, bool isTailCall,
424 SDOperand Callee, ArgListTy &Args,
425 SelectionDAG &DAG) {
426 // Count how many bytes are to be pushed on the stack.
427 unsigned NumBytes = 0;
428
429 if (Args.empty()) {
430 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000431 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432 } else {
433 for (unsigned i = 0, e = Args.size(); i != e; ++i)
434 switch (getValueType(Args[i].second)) {
435 default: assert(0 && "Unknown value type!");
436 case MVT::i1:
437 case MVT::i8:
438 case MVT::i16:
439 case MVT::i32:
440 case MVT::f32:
441 NumBytes += 4;
442 break;
443 case MVT::i64:
444 case MVT::f64:
445 NumBytes += 8;
446 break;
447 }
448
Chris Lattner94dd2922006-02-13 09:00:43 +0000449 Chain = DAG.getCALLSEQ_START(Chain,
450 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000451
452 // Arguments go on the stack in reverse order, as specified by the ABI.
453 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000454 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000455 std::vector<SDOperand> Stores;
456
457 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
458 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
459 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
460
461 switch (getValueType(Args[i].second)) {
462 default: assert(0 && "Unexpected ValueType for argument!");
463 case MVT::i1:
464 case MVT::i8:
465 case MVT::i16:
466 // Promote the integer to 32 bits. If the input type is signed use a
467 // sign extend, otherwise use a zero extend.
468 if (Args[i].second->isSigned())
469 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
470 else
471 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
472
473 // FALL THROUGH
474 case MVT::i32:
475 case MVT::f32:
476 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
477 Args[i].first, PtrOff,
478 DAG.getSrcValue(NULL)));
479 ArgOffset += 4;
480 break;
481 case MVT::i64:
482 case MVT::f64:
483 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
484 Args[i].first, PtrOff,
485 DAG.getSrcValue(NULL)));
486 ArgOffset += 8;
487 break;
488 }
489 }
490 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
491 }
492
493 std::vector<MVT::ValueType> RetVals;
494 MVT::ValueType RetTyVT = getValueType(RetTy);
495 RetVals.push_back(MVT::Other);
496
497 // The result values produced have to be legal. Promote the result.
498 switch (RetTyVT) {
499 case MVT::isVoid: break;
500 default:
501 RetVals.push_back(RetTyVT);
502 break;
503 case MVT::i1:
504 case MVT::i8:
505 case MVT::i16:
506 RetVals.push_back(MVT::i32);
507 break;
508 case MVT::f32:
509 if (X86ScalarSSE)
510 RetVals.push_back(MVT::f32);
511 else
512 RetVals.push_back(MVT::f64);
513 break;
514 case MVT::i64:
515 RetVals.push_back(MVT::i32);
516 RetVals.push_back(MVT::i32);
517 break;
518 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000519
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000520 std::vector<MVT::ValueType> NodeTys;
521 NodeTys.push_back(MVT::Other); // Returns a chain
522 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
523 std::vector<SDOperand> Ops;
524 Ops.push_back(Chain);
525 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000526
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000527 // FIXME: Do not generate X86ISD::TAILCALL for now.
528 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
529 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000530
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000531 NodeTys.clear();
532 NodeTys.push_back(MVT::Other); // Returns a chain
533 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
534 Ops.clear();
535 Ops.push_back(Chain);
536 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
537 Ops.push_back(DAG.getConstant(0, getPointerTy()));
538 Ops.push_back(InFlag);
539 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
540 InFlag = Chain.getValue(1);
541
542 SDOperand RetVal;
543 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000544 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000545 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000546 case MVT::i1:
547 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000548 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
549 Chain = RetVal.getValue(1);
550 if (RetTyVT == MVT::i1)
551 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
552 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000553 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000554 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
555 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000556 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000557 case MVT::i32:
558 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, 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::i64: {
562 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
563 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
564 Lo.getValue(2));
565 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
566 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000567 break;
568 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000569 case MVT::f32:
570 case MVT::f64: {
571 std::vector<MVT::ValueType> Tys;
572 Tys.push_back(MVT::f64);
573 Tys.push_back(MVT::Other);
574 Tys.push_back(MVT::Flag);
575 std::vector<SDOperand> Ops;
576 Ops.push_back(Chain);
577 Ops.push_back(InFlag);
578 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
579 Chain = RetVal.getValue(1);
580 InFlag = RetVal.getValue(2);
581 if (X86ScalarSSE) {
582 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
583 // shouldn't be necessary except that RFP cannot be live across
584 // multiple blocks. When stackifier is fixed, they can be uncoupled.
585 MachineFunction &MF = DAG.getMachineFunction();
586 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
587 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
588 Tys.clear();
589 Tys.push_back(MVT::Other);
590 Ops.clear();
591 Ops.push_back(Chain);
592 Ops.push_back(RetVal);
593 Ops.push_back(StackSlot);
594 Ops.push_back(DAG.getValueType(RetTyVT));
595 Ops.push_back(InFlag);
596 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
597 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
598 DAG.getSrcValue(NULL));
599 Chain = RetVal.getValue(1);
600 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000601
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000602 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
603 // FIXME: we would really like to remember that this FP_ROUND
604 // operation is okay to eliminate if we allow excess FP precision.
605 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
606 break;
607 }
608 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000609 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000610
611 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000612}
613
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000614//===----------------------------------------------------------------------===//
615// Fast Calling Convention implementation
616//===----------------------------------------------------------------------===//
617//
618// The X86 'fast' calling convention passes up to two integer arguments in
619// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
620// and requires that the callee pop its arguments off the stack (allowing proper
621// tail calls), and has the same return value conventions as C calling convs.
622//
623// This calling convention always arranges for the callee pop value to be 8n+4
624// bytes, which is needed for tail recursion elimination and stack alignment
625// reasons.
626//
627// Note that this can be enhanced in the future to pass fp vals in registers
628// (when we have a global fp allocator) and do other tricks.
629//
630
631/// AddLiveIn - This helper function adds the specified physical register to the
632/// MachineFunction as a live in value. It also creates a corresponding virtual
633/// register for it.
634static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
635 TargetRegisterClass *RC) {
636 assert(RC->contains(PReg) && "Not the correct regclass!");
637 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
638 MF.addLiveIn(PReg, VReg);
639 return VReg;
640}
641
Chris Lattner1c636e92006-03-17 05:10:20 +0000642enum {
643 // FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
644 // to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
645 // EDX". Anything more is illegal.
646 FASTCC_NUM_INT_ARGS_INREGS = 2
647};
648
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000649
650std::vector<SDOperand>
651X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
652 std::vector<SDOperand> ArgValues;
653
654 MachineFunction &MF = DAG.getMachineFunction();
655 MachineFrameInfo *MFI = MF.getFrameInfo();
656
657 // Add DAG nodes to load the arguments... On entry to a function the stack
658 // frame looks like this:
659 //
660 // [ESP] -- return address
661 // [ESP + 4] -- first nonreg argument (leftmost lexically)
662 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
663 // ...
664 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
665
666 // Keep track of the number of integer regs passed so far. This can be either
667 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
668 // used).
669 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000670
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000671 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
672 MVT::ValueType ObjectVT = getValueType(I->getType());
673 unsigned ArgIncrement = 4;
674 unsigned ObjSize = 0;
675 SDOperand ArgValue;
676
677 switch (ObjectVT) {
678 default: assert(0 && "Unhandled argument type!");
679 case MVT::i1:
680 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000681 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000682 if (!I->use_empty()) {
683 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
684 X86::R8RegisterClass);
685 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
686 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000687 if (ObjectVT == MVT::i1)
688 // FIXME: Should insert a assertzext here.
689 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000690 }
691 ++NumIntRegs;
692 break;
693 }
694
695 ObjSize = 1;
696 break;
697 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000698 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000699 if (!I->use_empty()) {
700 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
701 X86::R16RegisterClass);
702 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
703 DAG.setRoot(ArgValue.getValue(1));
704 }
705 ++NumIntRegs;
706 break;
707 }
708 ObjSize = 2;
709 break;
710 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000711 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000712 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000713 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000714 X86::R32RegisterClass);
715 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
716 DAG.setRoot(ArgValue.getValue(1));
717 }
718 ++NumIntRegs;
719 break;
720 }
721 ObjSize = 4;
722 break;
723 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000724 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000725 if (!I->use_empty()) {
726 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
727 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
728
729 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
730 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
731 DAG.setRoot(Hi.getValue(1));
732
733 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
734 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000735 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000736 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000737 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000738 if (!I->use_empty()) {
739 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
740 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
741 DAG.setRoot(Low.getValue(1));
742
743 // Load the high part from memory.
744 // Create the frame index object for this incoming parameter...
745 int FI = MFI->CreateFixedObject(4, ArgOffset);
746 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
747 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
748 DAG.getSrcValue(NULL));
749 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
750 }
751 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000752 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000753 break;
754 }
755 ObjSize = ArgIncrement = 8;
756 break;
757 case MVT::f32: ObjSize = 4; break;
758 case MVT::f64: ObjSize = ArgIncrement = 8; break;
759 }
760
761 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
762 // dead loads.
763 if (ObjSize && !I->use_empty()) {
764 // Create the frame index object for this incoming parameter...
765 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
766
767 // Create the SelectionDAG nodes corresponding to a load from this
768 // parameter.
769 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
770
771 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
772 DAG.getSrcValue(NULL));
773 } else if (ArgValue.Val == 0) {
774 if (MVT::isInteger(ObjectVT))
775 ArgValue = DAG.getConstant(0, ObjectVT);
776 else
777 ArgValue = DAG.getConstantFP(0, ObjectVT);
778 }
779 ArgValues.push_back(ArgValue);
780
781 if (ObjSize)
782 ArgOffset += ArgIncrement; // Move on to the next argument.
783 }
784
785 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
786 // arguments and the arguments after the retaddr has been pushed are aligned.
787 if ((ArgOffset & 7) == 0)
788 ArgOffset += 4;
789
790 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
791 ReturnAddrIndex = 0; // No return address slot generated yet.
792 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
793 BytesCallerReserves = 0;
794
795 // Finally, inform the code generator which regs we return values in.
796 switch (getValueType(F.getReturnType())) {
797 default: assert(0 && "Unknown type!");
798 case MVT::isVoid: break;
799 case MVT::i1:
800 case MVT::i8:
801 case MVT::i16:
802 case MVT::i32:
803 MF.addLiveOut(X86::EAX);
804 break;
805 case MVT::i64:
806 MF.addLiveOut(X86::EAX);
807 MF.addLiveOut(X86::EDX);
808 break;
809 case MVT::f32:
810 case MVT::f64:
811 MF.addLiveOut(X86::ST0);
812 break;
813 }
814 return ArgValues;
815}
816
817std::pair<SDOperand, SDOperand>
818X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
819 bool isTailCall, SDOperand Callee,
820 ArgListTy &Args, SelectionDAG &DAG) {
821 // Count how many bytes are to be pushed on the stack.
822 unsigned NumBytes = 0;
823
824 // Keep track of the number of integer regs passed so far. This can be either
825 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
826 // used).
827 unsigned NumIntRegs = 0;
828
829 for (unsigned i = 0, e = Args.size(); i != e; ++i)
830 switch (getValueType(Args[i].second)) {
831 default: assert(0 && "Unknown value type!");
832 case MVT::i1:
833 case MVT::i8:
834 case MVT::i16:
835 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000836 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000837 ++NumIntRegs;
838 break;
839 }
840 // fall through
841 case MVT::f32:
842 NumBytes += 4;
843 break;
844 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000845 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
846 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000847 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000848 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
849 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000850 NumBytes += 4;
851 break;
852 }
853
854 // fall through
855 case MVT::f64:
856 NumBytes += 8;
857 break;
858 }
859
860 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
861 // arguments and the arguments after the retaddr has been pushed are aligned.
862 if ((NumBytes & 7) == 0)
863 NumBytes += 4;
864
Chris Lattner94dd2922006-02-13 09:00:43 +0000865 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000866
867 // Arguments go on the stack in reverse order, as specified by the ABI.
868 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000869 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000870 NumIntRegs = 0;
871 std::vector<SDOperand> Stores;
872 std::vector<SDOperand> RegValuesToPass;
873 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
874 switch (getValueType(Args[i].second)) {
875 default: assert(0 && "Unexpected ValueType for argument!");
876 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000877 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
878 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000879 case MVT::i8:
880 case MVT::i16:
881 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000882 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000883 RegValuesToPass.push_back(Args[i].first);
884 ++NumIntRegs;
885 break;
886 }
887 // Fall through
888 case MVT::f32: {
889 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
890 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
891 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
892 Args[i].first, PtrOff,
893 DAG.getSrcValue(NULL)));
894 ArgOffset += 4;
895 break;
896 }
897 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000898 // Can pass (at least) part of it in regs?
899 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000900 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
901 Args[i].first, DAG.getConstant(1, MVT::i32));
902 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
903 Args[i].first, DAG.getConstant(0, MVT::i32));
904 RegValuesToPass.push_back(Lo);
905 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000906
907 // Pass both parts in regs?
908 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000909 RegValuesToPass.push_back(Hi);
910 ++NumIntRegs;
911 } else {
912 // Pass the high part in memory.
913 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
914 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
915 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
916 Hi, PtrOff, DAG.getSrcValue(NULL)));
917 ArgOffset += 4;
918 }
919 break;
920 }
921 // Fall through
922 case MVT::f64:
923 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
924 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
925 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
926 Args[i].first, PtrOff,
927 DAG.getSrcValue(NULL)));
928 ArgOffset += 8;
929 break;
930 }
931 }
932 if (!Stores.empty())
933 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
934
935 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
936 // arguments and the arguments after the retaddr has been pushed are aligned.
937 if ((ArgOffset & 7) == 0)
938 ArgOffset += 4;
939
940 std::vector<MVT::ValueType> RetVals;
941 MVT::ValueType RetTyVT = getValueType(RetTy);
942
943 RetVals.push_back(MVT::Other);
944
945 // The result values produced have to be legal. Promote the result.
946 switch (RetTyVT) {
947 case MVT::isVoid: break;
948 default:
949 RetVals.push_back(RetTyVT);
950 break;
951 case MVT::i1:
952 case MVT::i8:
953 case MVT::i16:
954 RetVals.push_back(MVT::i32);
955 break;
956 case MVT::f32:
957 if (X86ScalarSSE)
958 RetVals.push_back(MVT::f32);
959 else
960 RetVals.push_back(MVT::f64);
961 break;
962 case MVT::i64:
963 RetVals.push_back(MVT::i32);
964 RetVals.push_back(MVT::i32);
965 break;
966 }
967
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000968 // Build a sequence of copy-to-reg nodes chained together with token chain
969 // and flag operands which copy the outgoing args into registers.
970 SDOperand InFlag;
971 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
972 unsigned CCReg;
973 SDOperand RegToPass = RegValuesToPass[i];
974 switch (RegToPass.getValueType()) {
975 default: assert(0 && "Bad thing to pass in regs");
976 case MVT::i8:
977 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000978 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000979 case MVT::i16:
980 CCReg = (i == 0) ? X86::AX : X86::DX;
981 break;
982 case MVT::i32:
983 CCReg = (i == 0) ? X86::EAX : X86::EDX;
984 break;
985 }
986
987 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
988 InFlag = Chain.getValue(1);
989 }
990
991 std::vector<MVT::ValueType> NodeTys;
992 NodeTys.push_back(MVT::Other); // Returns a chain
993 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
994 std::vector<SDOperand> Ops;
995 Ops.push_back(Chain);
996 Ops.push_back(Callee);
997 if (InFlag.Val)
998 Ops.push_back(InFlag);
999
1000 // FIXME: Do not generate X86ISD::TAILCALL for now.
1001 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1002 InFlag = Chain.getValue(1);
1003
1004 NodeTys.clear();
1005 NodeTys.push_back(MVT::Other); // Returns a chain
1006 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1007 Ops.clear();
1008 Ops.push_back(Chain);
1009 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1010 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1011 Ops.push_back(InFlag);
1012 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1013 InFlag = Chain.getValue(1);
1014
1015 SDOperand RetVal;
1016 if (RetTyVT != MVT::isVoid) {
1017 switch (RetTyVT) {
1018 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001019 case MVT::i1:
1020 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001021 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1022 Chain = RetVal.getValue(1);
1023 if (RetTyVT == MVT::i1)
1024 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1025 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001026 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001027 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1028 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001029 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001030 case MVT::i32:
1031 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1032 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001033 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001034 case MVT::i64: {
1035 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1036 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1037 Lo.getValue(2));
1038 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1039 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001040 break;
1041 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001042 case MVT::f32:
1043 case MVT::f64: {
1044 std::vector<MVT::ValueType> Tys;
1045 Tys.push_back(MVT::f64);
1046 Tys.push_back(MVT::Other);
1047 Tys.push_back(MVT::Flag);
1048 std::vector<SDOperand> Ops;
1049 Ops.push_back(Chain);
1050 Ops.push_back(InFlag);
1051 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1052 Chain = RetVal.getValue(1);
1053 InFlag = RetVal.getValue(2);
1054 if (X86ScalarSSE) {
1055 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1056 // shouldn't be necessary except that RFP cannot be live across
1057 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1058 MachineFunction &MF = DAG.getMachineFunction();
1059 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1060 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1061 Tys.clear();
1062 Tys.push_back(MVT::Other);
1063 Ops.clear();
1064 Ops.push_back(Chain);
1065 Ops.push_back(RetVal);
1066 Ops.push_back(StackSlot);
1067 Ops.push_back(DAG.getValueType(RetTyVT));
1068 Ops.push_back(InFlag);
1069 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1070 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1071 DAG.getSrcValue(NULL));
1072 Chain = RetVal.getValue(1);
1073 }
Evan Chengd9558e02006-01-06 00:43:03 +00001074
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001075 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1076 // FIXME: we would really like to remember that this FP_ROUND
1077 // operation is okay to eliminate if we allow excess FP precision.
1078 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1079 break;
1080 }
1081 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001082 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001083
1084 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001085}
1086
1087SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1088 if (ReturnAddrIndex == 0) {
1089 // Set up a frame object for the return address.
1090 MachineFunction &MF = DAG.getMachineFunction();
1091 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1092 }
1093
1094 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1095}
1096
1097
1098
1099std::pair<SDOperand, SDOperand> X86TargetLowering::
1100LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1101 SelectionDAG &DAG) {
1102 SDOperand Result;
1103 if (Depth) // Depths > 0 not supported yet!
1104 Result = DAG.getConstant(0, getPointerTy());
1105 else {
1106 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1107 if (!isFrameAddress)
1108 // Just load the return address
1109 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1110 DAG.getSrcValue(NULL));
1111 else
1112 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1113 DAG.getConstant(4, MVT::i32));
1114 }
1115 return std::make_pair(Result, Chain);
1116}
1117
Evan Cheng4a460802006-01-11 00:33:36 +00001118/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1119/// which corresponds to the condition code.
1120static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1121 switch (X86CC) {
1122 default: assert(0 && "Unknown X86 conditional code!");
1123 case X86ISD::COND_A: return X86::JA;
1124 case X86ISD::COND_AE: return X86::JAE;
1125 case X86ISD::COND_B: return X86::JB;
1126 case X86ISD::COND_BE: return X86::JBE;
1127 case X86ISD::COND_E: return X86::JE;
1128 case X86ISD::COND_G: return X86::JG;
1129 case X86ISD::COND_GE: return X86::JGE;
1130 case X86ISD::COND_L: return X86::JL;
1131 case X86ISD::COND_LE: return X86::JLE;
1132 case X86ISD::COND_NE: return X86::JNE;
1133 case X86ISD::COND_NO: return X86::JNO;
1134 case X86ISD::COND_NP: return X86::JNP;
1135 case X86ISD::COND_NS: return X86::JNS;
1136 case X86ISD::COND_O: return X86::JO;
1137 case X86ISD::COND_P: return X86::JP;
1138 case X86ISD::COND_S: return X86::JS;
1139 }
1140}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001141
Evan Cheng6dfa9992006-01-30 23:41:35 +00001142/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1143/// specific condition code. It returns a false if it cannot do a direct
1144/// translation. X86CC is the translated CondCode. Flip is set to true if the
1145/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001146static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1147 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001148 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001149 Flip = false;
1150 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001151 if (!isFP) {
1152 switch (SetCCOpcode) {
1153 default: break;
1154 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1155 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1156 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1157 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1158 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1159 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1160 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1161 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1162 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1163 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1164 }
1165 } else {
1166 // On a floating point condition, the flags are set as follows:
1167 // ZF PF CF op
1168 // 0 | 0 | 0 | X > Y
1169 // 0 | 0 | 1 | X < Y
1170 // 1 | 0 | 0 | X == Y
1171 // 1 | 1 | 1 | unordered
1172 switch (SetCCOpcode) {
1173 default: break;
1174 case ISD::SETUEQ:
1175 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001176 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001177 case ISD::SETOGT:
1178 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001179 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001180 case ISD::SETOGE:
1181 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001182 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001183 case ISD::SETULT:
1184 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001185 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001186 case ISD::SETULE:
1187 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1188 case ISD::SETONE:
1189 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1190 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1191 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1192 }
1193 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001194
1195 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001196}
1197
Evan Cheng4a460802006-01-11 00:33:36 +00001198/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1199/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001200/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001201static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001202 switch (X86CC) {
1203 default:
1204 return false;
1205 case X86ISD::COND_B:
1206 case X86ISD::COND_BE:
1207 case X86ISD::COND_E:
1208 case X86ISD::COND_P:
1209 case X86ISD::COND_A:
1210 case X86ISD::COND_AE:
1211 case X86ISD::COND_NE:
1212 case X86ISD::COND_NP:
1213 return true;
1214 }
1215}
1216
Evan Cheng4a460802006-01-11 00:33:36 +00001217MachineBasicBlock *
1218X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1219 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001220 switch (MI->getOpcode()) {
1221 default: assert(false && "Unexpected instr type to insert");
1222 case X86::CMOV_FR32:
1223 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001224 // To "insert" a SELECT_CC instruction, we actually have to insert the
1225 // diamond control-flow pattern. The incoming instruction knows the
1226 // destination vreg to set, the condition code register to branch on, the
1227 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001228 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1229 ilist<MachineBasicBlock>::iterator It = BB;
1230 ++It;
1231
1232 // thisMBB:
1233 // ...
1234 // TrueVal = ...
1235 // cmpTY ccX, r1, r2
1236 // bCC copy1MBB
1237 // fallthrough --> copy0MBB
1238 MachineBasicBlock *thisMBB = BB;
1239 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1240 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1241 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1242 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1243 MachineFunction *F = BB->getParent();
1244 F->getBasicBlockList().insert(It, copy0MBB);
1245 F->getBasicBlockList().insert(It, sinkMBB);
1246 // Update machine-CFG edges
1247 BB->addSuccessor(copy0MBB);
1248 BB->addSuccessor(sinkMBB);
1249
1250 // copy0MBB:
1251 // %FalseValue = ...
1252 // # fallthrough to sinkMBB
1253 BB = copy0MBB;
1254
1255 // Update machine-CFG edges
1256 BB->addSuccessor(sinkMBB);
1257
1258 // sinkMBB:
1259 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1260 // ...
1261 BB = sinkMBB;
1262 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1263 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1264 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001265
Evan Cheng0cc39452006-01-16 21:21:29 +00001266 delete MI; // The pseudo instruction is gone now.
1267 return BB;
1268 }
Evan Cheng4a460802006-01-11 00:33:36 +00001269
Evan Cheng0cc39452006-01-16 21:21:29 +00001270 case X86::FP_TO_INT16_IN_MEM:
1271 case X86::FP_TO_INT32_IN_MEM:
1272 case X86::FP_TO_INT64_IN_MEM: {
1273 // Change the floating point control register to use "round towards zero"
1274 // mode when truncating to an integer value.
1275 MachineFunction *F = BB->getParent();
1276 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1277 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1278
1279 // Load the old value of the high byte of the control word...
1280 unsigned OldCW =
1281 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1282 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1283
1284 // Set the high part to be round to zero...
1285 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1286
1287 // Reload the modified control word now...
1288 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1289
1290 // Restore the memory image of control word to original value
1291 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1292
1293 // Get the X86 opcode to use.
1294 unsigned Opc;
1295 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001296 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001297 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1298 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1299 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1300 }
1301
1302 X86AddressMode AM;
1303 MachineOperand &Op = MI->getOperand(0);
1304 if (Op.isRegister()) {
1305 AM.BaseType = X86AddressMode::RegBase;
1306 AM.Base.Reg = Op.getReg();
1307 } else {
1308 AM.BaseType = X86AddressMode::FrameIndexBase;
1309 AM.Base.FrameIndex = Op.getFrameIndex();
1310 }
1311 Op = MI->getOperand(1);
1312 if (Op.isImmediate())
1313 AM.Scale = Op.getImmedValue();
1314 Op = MI->getOperand(2);
1315 if (Op.isImmediate())
1316 AM.IndexReg = Op.getImmedValue();
1317 Op = MI->getOperand(3);
1318 if (Op.isGlobalAddress()) {
1319 AM.GV = Op.getGlobal();
1320 } else {
1321 AM.Disp = Op.getImmedValue();
1322 }
1323 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1324
1325 // Reload the original control word now.
1326 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1327
1328 delete MI; // The pseudo instruction is gone now.
1329 return BB;
1330 }
1331 }
Evan Cheng4a460802006-01-11 00:33:36 +00001332}
1333
1334
1335//===----------------------------------------------------------------------===//
1336// X86 Custom Lowering Hooks
1337//===----------------------------------------------------------------------===//
1338
Evan Cheng30b37b52006-03-13 23:18:16 +00001339/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1340/// load. For Darwin, external and weak symbols are indirect, loading the value
1341/// at address GV rather then the value of GV itself. This means that the
1342/// GlobalAddress must be in the base or index register of the address, not the
1343/// GV offset field.
1344static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1345 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1346 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1347}
1348
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001349/// LowerOperation - Provide custom lowering hooks for some operations.
1350///
1351SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1352 switch (Op.getOpcode()) {
1353 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001354 case ISD::SHL_PARTS:
1355 case ISD::SRA_PARTS:
1356 case ISD::SRL_PARTS: {
1357 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1358 "Not an i64 shift!");
1359 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1360 SDOperand ShOpLo = Op.getOperand(0);
1361 SDOperand ShOpHi = Op.getOperand(1);
1362 SDOperand ShAmt = Op.getOperand(2);
1363 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001364 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001365 : DAG.getConstant(0, MVT::i32);
1366
1367 SDOperand Tmp2, Tmp3;
1368 if (Op.getOpcode() == ISD::SHL_PARTS) {
1369 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1370 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1371 } else {
1372 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001373 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001374 }
1375
1376 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1377 ShAmt, DAG.getConstant(32, MVT::i8));
1378
1379 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001380 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001381
1382 std::vector<MVT::ValueType> Tys;
1383 Tys.push_back(MVT::i32);
1384 Tys.push_back(MVT::Flag);
1385 std::vector<SDOperand> Ops;
1386 if (Op.getOpcode() == ISD::SHL_PARTS) {
1387 Ops.push_back(Tmp2);
1388 Ops.push_back(Tmp3);
1389 Ops.push_back(CC);
1390 Ops.push_back(InFlag);
1391 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1392 InFlag = Hi.getValue(1);
1393
1394 Ops.clear();
1395 Ops.push_back(Tmp3);
1396 Ops.push_back(Tmp1);
1397 Ops.push_back(CC);
1398 Ops.push_back(InFlag);
1399 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1400 } else {
1401 Ops.push_back(Tmp2);
1402 Ops.push_back(Tmp3);
1403 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001404 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001405 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1406 InFlag = Lo.getValue(1);
1407
1408 Ops.clear();
1409 Ops.push_back(Tmp3);
1410 Ops.push_back(Tmp1);
1411 Ops.push_back(CC);
1412 Ops.push_back(InFlag);
1413 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1414 }
1415
1416 Tys.clear();
1417 Tys.push_back(MVT::i32);
1418 Tys.push_back(MVT::i32);
1419 Ops.clear();
1420 Ops.push_back(Lo);
1421 Ops.push_back(Hi);
1422 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1423 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001424 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001425 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001426 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001427 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001428
1429 SDOperand Result;
1430 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1431 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001432 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001433 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001434 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001435 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1436 DAG.getEntryNode(), Op.getOperand(0),
1437 StackSlot, DAG.getSrcValue(NULL));
1438
1439 // Build the FILD
1440 std::vector<MVT::ValueType> Tys;
1441 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001442 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001443 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001444 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001445 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001446 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001447 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001448 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1449 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001450
1451 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001452 Chain = Result.getValue(1);
1453 SDOperand InFlag = Result.getValue(2);
1454
Evan Chenge3de85b2006-02-04 02:20:30 +00001455 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001456 // shouldn't be necessary except that RFP cannot be live across
1457 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1458 MachineFunction &MF = DAG.getMachineFunction();
1459 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1460 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1461 std::vector<MVT::ValueType> Tys;
1462 Tys.push_back(MVT::Other);
1463 std::vector<SDOperand> Ops;
1464 Ops.push_back(Chain);
1465 Ops.push_back(Result);
1466 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001467 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001468 Ops.push_back(InFlag);
1469 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1470 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1471 DAG.getSrcValue(NULL));
1472 }
1473
Evan Chenga3195e82006-01-12 22:54:21 +00001474 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001475 }
1476 case ISD::FP_TO_SINT: {
1477 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001478 "Unknown FP_TO_SINT to lower!");
1479 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1480 // stack slot.
1481 MachineFunction &MF = DAG.getMachineFunction();
1482 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1483 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1484 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1485
1486 unsigned Opc;
1487 switch (Op.getValueType()) {
1488 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1489 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1490 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1491 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1492 }
1493
Evan Cheng6dab0532006-01-30 08:02:57 +00001494 SDOperand Chain = DAG.getEntryNode();
1495 SDOperand Value = Op.getOperand(0);
1496 if (X86ScalarSSE) {
1497 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1498 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1499 DAG.getSrcValue(0));
1500 std::vector<MVT::ValueType> Tys;
1501 Tys.push_back(MVT::f64);
1502 Tys.push_back(MVT::Other);
1503 std::vector<SDOperand> Ops;
1504 Ops.push_back(Chain);
1505 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001506 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001507 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1508 Chain = Value.getValue(1);
1509 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1510 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1511 }
1512
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001513 // Build the FP_TO_INT*_IN_MEM
1514 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001515 Ops.push_back(Chain);
1516 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001517 Ops.push_back(StackSlot);
1518 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1519
1520 // Load the result.
1521 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1522 DAG.getSrcValue(NULL));
1523 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001524 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001525 std::vector<MVT::ValueType> Tys;
1526 Tys.push_back(MVT::Other);
1527 Tys.push_back(MVT::Flag);
1528 std::vector<SDOperand> Ops;
1529 Ops.push_back(Op.getOperand(0));
1530 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001531 Ops.clear();
1532 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1533 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1534 MVT::i32, Ops[0].getValue(2)));
1535 Ops.push_back(Ops[1].getValue(1));
1536 Tys[0] = Tys[1] = MVT::i32;
1537 Tys.push_back(MVT::Other);
1538 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001539 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001540 case ISD::FABS: {
1541 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001542 const Type *OpNTy = MVT::getTypeForValueType(VT);
1543 std::vector<Constant*> CV;
1544 if (VT == MVT::f64) {
1545 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1546 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1547 } else {
1548 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1549 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1550 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1551 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1552 }
1553 Constant *CS = ConstantStruct::get(CV);
1554 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1555 SDOperand Mask
1556 = DAG.getNode(X86ISD::LOAD_PACK,
1557 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001558 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1559 }
Evan Cheng223547a2006-01-31 22:28:30 +00001560 case ISD::FNEG: {
1561 MVT::ValueType VT = Op.getValueType();
1562 const Type *OpNTy = MVT::getTypeForValueType(VT);
1563 std::vector<Constant*> CV;
1564 if (VT == MVT::f64) {
1565 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1566 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1567 } else {
1568 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1569 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1570 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1571 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1572 }
1573 Constant *CS = ConstantStruct::get(CV);
1574 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1575 SDOperand Mask
1576 = DAG.getNode(X86ISD::LOAD_PACK,
1577 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1578 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1579 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001580 case ISD::SETCC: {
1581 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001582 SDOperand Cond;
1583 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001584 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1585 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001586 bool Flip;
1587 unsigned X86CC;
1588 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1589 if (Flip)
1590 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1591 Op.getOperand(1), Op.getOperand(0));
1592 else
1593 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1594 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001595 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1596 DAG.getConstant(X86CC, MVT::i8), Cond);
1597 } else {
1598 assert(isFP && "Illegal integer SetCC!");
1599
Evan Cheng6dfa9992006-01-30 23:41:35 +00001600 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1601 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001602 std::vector<MVT::ValueType> Tys;
1603 std::vector<SDOperand> Ops;
1604 switch (SetCCOpcode) {
1605 default: assert(false && "Illegal floating point SetCC!");
1606 case ISD::SETOEQ: { // !PF & ZF
1607 Tys.push_back(MVT::i8);
1608 Tys.push_back(MVT::Flag);
1609 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1610 Ops.push_back(Cond);
1611 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1612 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1613 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1614 Tmp1.getValue(1));
1615 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1616 }
Evan Chengd9558e02006-01-06 00:43:03 +00001617 case ISD::SETUNE: { // PF | !ZF
1618 Tys.push_back(MVT::i8);
1619 Tys.push_back(MVT::Flag);
1620 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1621 Ops.push_back(Cond);
1622 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1623 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1624 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1625 Tmp1.getValue(1));
1626 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1627 }
1628 }
1629 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001630 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001631 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001632 MVT::ValueType VT = Op.getValueType();
1633 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001634 bool isFPStack = isFP && !X86ScalarSSE;
1635 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001636 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001637 SDOperand Op0 = Op.getOperand(0);
1638 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001639 if (Op0.getOpcode() == ISD::SETCC)
1640 Op0 = LowerOperation(Op0, DAG);
1641
Evan Chengaaca22c2006-01-10 20:26:56 +00001642 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001643 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1644 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1645 // have another use it will be eliminated.
1646 // If the X86ISD::SETCC has more than one use, then it's probably better
1647 // to use a test instead of duplicating the X86ISD::CMP (for register
1648 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001649 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1650 if (!Op0.hasOneUse()) {
1651 std::vector<MVT::ValueType> Tys;
1652 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1653 Tys.push_back(Op0.Val->getValueType(i));
1654 std::vector<SDOperand> Ops;
1655 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1656 Ops.push_back(Op0.getOperand(i));
1657 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1658 }
1659
Evan Cheng1bcee362006-01-13 01:03:02 +00001660 CC = Op0.getOperand(0);
1661 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001662 // Make a copy as flag result cannot be used by more than one.
1663 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1664 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001665 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001666 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001667 } else
1668 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001669 } else
1670 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001671
Evan Cheng189d01e2006-01-13 01:06:49 +00001672 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001673 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001674 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001675 }
Evan Chenge3413162006-01-09 18:33:28 +00001676
1677 std::vector<MVT::ValueType> Tys;
1678 Tys.push_back(Op.getValueType());
1679 Tys.push_back(MVT::Flag);
1680 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001681 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1682 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001683 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001684 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001685 Ops.push_back(CC);
1686 Ops.push_back(Cond);
1687 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001688 }
Evan Cheng898101c2005-12-19 23:12:38 +00001689 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001690 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001691 SDOperand Cond = Op.getOperand(1);
1692 SDOperand Dest = Op.getOperand(2);
1693 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001694 if (Cond.getOpcode() == ISD::SETCC)
1695 Cond = LowerOperation(Cond, DAG);
1696
Evan Chengd5781fc2005-12-21 20:21:51 +00001697 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001698 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1699 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1700 // have another use it will be eliminated.
1701 // If the X86ISD::SETCC has more than one use, then it's probably better
1702 // to use a test instead of duplicating the X86ISD::CMP (for register
1703 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001704 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1705 if (!Cond.hasOneUse()) {
1706 std::vector<MVT::ValueType> Tys;
1707 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1708 Tys.push_back(Cond.Val->getValueType(i));
1709 std::vector<SDOperand> Ops;
1710 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1711 Ops.push_back(Cond.getOperand(i));
1712 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1713 }
1714
Evan Cheng1bcee362006-01-13 01:03:02 +00001715 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001716 Cond = Cond.getOperand(1);
1717 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001718 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001719 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001720 } else
1721 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001722 } else
1723 addTest = true;
1724
1725 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001726 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001727 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1728 }
1729 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1730 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1731 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001732 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001733 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001734 SDOperand Chain = Op.getOperand(0);
1735 unsigned Align =
1736 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1737 if (Align == 0) Align = 1;
1738
Evan Cheng18a84522006-02-16 00:21:07 +00001739 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1740 // If not DWORD aligned, call memset if size is less than the threshold.
1741 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001742 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001743 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001744 MVT::ValueType IntPtr = getPointerTy();
1745 const Type *IntPtrTy = getTargetData().getIntPtrType();
1746 std::vector<std::pair<SDOperand, const Type*> > Args;
1747 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1748 // Extend the ubyte argument to be an int value for the call.
1749 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1750 Args.push_back(std::make_pair(Val, IntPtrTy));
1751 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1752 std::pair<SDOperand,SDOperand> CallResult =
1753 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1754 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1755 return CallResult.second;
1756 }
1757
Evan Cheng67f92a72006-01-11 22:15:48 +00001758 MVT::ValueType AVT;
1759 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001760 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1761 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001762 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001763 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001764 unsigned ValReg;
1765 unsigned Val = ValC->getValue() & 255;
1766
1767 // If the value is a constant, then we can potentially use larger sets.
1768 switch (Align & 3) {
1769 case 2: // WORD aligned
1770 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001771 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1772 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001773 Val = (Val << 8) | Val;
1774 ValReg = X86::AX;
1775 break;
1776 case 0: // DWORD aligned
1777 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001778 if (I) {
1779 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1780 BytesLeft = I->getValue() % 4;
1781 } else {
1782 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1783 DAG.getConstant(2, MVT::i8));
1784 TwoRepStos = true;
1785 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001786 Val = (Val << 8) | Val;
1787 Val = (Val << 16) | Val;
1788 ValReg = X86::EAX;
1789 break;
1790 default: // Byte aligned
1791 AVT = MVT::i8;
1792 Count = Op.getOperand(3);
1793 ValReg = X86::AL;
1794 break;
1795 }
1796
1797 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1798 InFlag);
1799 InFlag = Chain.getValue(1);
1800 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001801 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001802 Count = Op.getOperand(3);
1803 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1804 InFlag = Chain.getValue(1);
1805 }
1806
1807 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1808 InFlag = Chain.getValue(1);
1809 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1810 InFlag = Chain.getValue(1);
1811
Evan Chengff909922006-03-07 23:29:39 +00001812 std::vector<MVT::ValueType> Tys;
1813 Tys.push_back(MVT::Other);
1814 Tys.push_back(MVT::Flag);
1815 std::vector<SDOperand> Ops;
1816 Ops.push_back(Chain);
1817 Ops.push_back(DAG.getValueType(AVT));
1818 Ops.push_back(InFlag);
1819 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1820
1821 if (TwoRepStos) {
1822 InFlag = Chain.getValue(1);
1823 Count = Op.getOperand(3);
1824 MVT::ValueType CVT = Count.getValueType();
1825 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1826 DAG.getConstant(3, CVT));
1827 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1828 InFlag = Chain.getValue(1);
1829 Tys.clear();
1830 Tys.push_back(MVT::Other);
1831 Tys.push_back(MVT::Flag);
1832 Ops.clear();
1833 Ops.push_back(Chain);
1834 Ops.push_back(DAG.getValueType(MVT::i8));
1835 Ops.push_back(InFlag);
1836 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1837 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001838 // Issue stores for the last 1 - 3 bytes.
1839 SDOperand Value;
1840 unsigned Val = ValC->getValue() & 255;
1841 unsigned Offset = I->getValue() - BytesLeft;
1842 SDOperand DstAddr = Op.getOperand(1);
1843 MVT::ValueType AddrVT = DstAddr.getValueType();
1844 if (BytesLeft >= 2) {
1845 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1846 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1847 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1848 DAG.getConstant(Offset, AddrVT)),
1849 DAG.getSrcValue(NULL));
1850 BytesLeft -= 2;
1851 Offset += 2;
1852 }
1853
1854 if (BytesLeft == 1) {
1855 Value = DAG.getConstant(Val, MVT::i8);
1856 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1857 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1858 DAG.getConstant(Offset, AddrVT)),
1859 DAG.getSrcValue(NULL));
1860 }
1861 }
1862
1863 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001864 }
1865 case ISD::MEMCPY: {
1866 SDOperand Chain = Op.getOperand(0);
1867 unsigned Align =
1868 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1869 if (Align == 0) Align = 1;
1870
Evan Cheng18a84522006-02-16 00:21:07 +00001871 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1872 // If not DWORD aligned, call memcpy if size is less than the threshold.
1873 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001874 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001875 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001876 MVT::ValueType IntPtr = getPointerTy();
1877 const Type *IntPtrTy = getTargetData().getIntPtrType();
1878 std::vector<std::pair<SDOperand, const Type*> > Args;
1879 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1880 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1881 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1882 std::pair<SDOperand,SDOperand> CallResult =
1883 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1884 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1885 return CallResult.second;
1886 }
1887
Evan Cheng67f92a72006-01-11 22:15:48 +00001888 MVT::ValueType AVT;
1889 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001890 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001891 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001892 switch (Align & 3) {
1893 case 2: // WORD aligned
1894 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001895 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1896 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001897 break;
1898 case 0: // DWORD aligned
1899 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001900 if (I) {
1901 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1902 BytesLeft = I->getValue() % 4;
1903 } else {
1904 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1905 DAG.getConstant(2, MVT::i8));
1906 TwoRepMovs = true;
1907 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001908 break;
1909 default: // Byte aligned
1910 AVT = MVT::i8;
1911 Count = Op.getOperand(3);
1912 break;
1913 }
1914
Evan Cheng62bec2c2006-03-04 02:48:56 +00001915 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001916 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1917 InFlag = Chain.getValue(1);
1918 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1919 InFlag = Chain.getValue(1);
1920 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1921 InFlag = Chain.getValue(1);
1922
Evan Chengff909922006-03-07 23:29:39 +00001923 std::vector<MVT::ValueType> Tys;
1924 Tys.push_back(MVT::Other);
1925 Tys.push_back(MVT::Flag);
1926 std::vector<SDOperand> Ops;
1927 Ops.push_back(Chain);
1928 Ops.push_back(DAG.getValueType(AVT));
1929 Ops.push_back(InFlag);
1930 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1931
1932 if (TwoRepMovs) {
1933 InFlag = Chain.getValue(1);
1934 Count = Op.getOperand(3);
1935 MVT::ValueType CVT = Count.getValueType();
1936 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1937 DAG.getConstant(3, CVT));
1938 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1939 InFlag = Chain.getValue(1);
1940 Tys.clear();
1941 Tys.push_back(MVT::Other);
1942 Tys.push_back(MVT::Flag);
1943 Ops.clear();
1944 Ops.push_back(Chain);
1945 Ops.push_back(DAG.getValueType(MVT::i8));
1946 Ops.push_back(InFlag);
1947 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1948 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001949 // Issue loads and stores for the last 1 - 3 bytes.
1950 unsigned Offset = I->getValue() - BytesLeft;
1951 SDOperand DstAddr = Op.getOperand(1);
1952 MVT::ValueType DstVT = DstAddr.getValueType();
1953 SDOperand SrcAddr = Op.getOperand(2);
1954 MVT::ValueType SrcVT = SrcAddr.getValueType();
1955 SDOperand Value;
1956 if (BytesLeft >= 2) {
1957 Value = DAG.getLoad(MVT::i16, Chain,
1958 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1959 DAG.getConstant(Offset, SrcVT)),
1960 DAG.getSrcValue(NULL));
1961 Chain = Value.getValue(1);
1962 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1963 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1964 DAG.getConstant(Offset, DstVT)),
1965 DAG.getSrcValue(NULL));
1966 BytesLeft -= 2;
1967 Offset += 2;
1968 }
1969
1970 if (BytesLeft == 1) {
1971 Value = DAG.getLoad(MVT::i8, Chain,
1972 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1973 DAG.getConstant(Offset, SrcVT)),
1974 DAG.getSrcValue(NULL));
1975 Chain = Value.getValue(1);
1976 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1977 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1978 DAG.getConstant(Offset, DstVT)),
1979 DAG.getSrcValue(NULL));
1980 }
1981 }
1982
1983 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001984 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00001985
1986 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1987 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1988 // one of the above mentioned nodes. It has to be wrapped because otherwise
1989 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1990 // be used to form addressing mode. These wrapped nodes will be selected
1991 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00001992 case ISD::ConstantPool: {
1993 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00001994 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1995 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1996 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00001997 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00001998 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001999 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002000 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2001 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2002 }
2003
2004 return Result;
2005 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002006 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002007 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2008 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2009 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00002010 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00002011 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002012 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002013 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002014 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2015 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002016
2017 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002018 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002019 // the GlobalAddress must be in the base or index register of the address,
2020 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002021 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002022 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002023 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002024 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002025 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002026
Evan Cheng002fe9b2006-01-12 07:56:47 +00002027 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002028 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002029 case ISD::ExternalSymbol: {
2030 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2031 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2032 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2033 if (getTargetMachine().
2034 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2035 // With PIC, the address is actually $g + Offset.
2036 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2037 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2038 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2039 }
2040
2041 return Result;
2042 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002043 case ISD::VASTART: {
2044 // vastart just stores the address of the VarArgsFrameIndex slot into the
2045 // memory location argument.
2046 // FIXME: Replace MVT::i32 with PointerTy
2047 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2048 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2049 Op.getOperand(1), Op.getOperand(2));
2050 }
Nate Begemanee625572006-01-27 21:09:22 +00002051 case ISD::RET: {
2052 SDOperand Copy;
2053
2054 switch(Op.getNumOperands()) {
2055 default:
2056 assert(0 && "Do not know how to return this many arguments!");
2057 abort();
2058 case 1:
2059 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2060 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2061 case 2: {
2062 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2063 if (MVT::isInteger(ArgVT))
2064 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2065 SDOperand());
2066 else if (!X86ScalarSSE) {
2067 std::vector<MVT::ValueType> Tys;
2068 Tys.push_back(MVT::Other);
2069 Tys.push_back(MVT::Flag);
2070 std::vector<SDOperand> Ops;
2071 Ops.push_back(Op.getOperand(0));
2072 Ops.push_back(Op.getOperand(1));
2073 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2074 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002075 SDOperand MemLoc;
2076 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002077 SDOperand Value = Op.getOperand(1);
2078
Evan Cheng760df292006-02-01 01:19:32 +00002079 if (Value.getOpcode() == ISD::LOAD &&
2080 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002081 Chain = Value.getOperand(0);
2082 MemLoc = Value.getOperand(1);
2083 } else {
2084 // Spill the value to memory and reload it into top of stack.
2085 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2086 MachineFunction &MF = DAG.getMachineFunction();
2087 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2088 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2089 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2090 Value, MemLoc, DAG.getSrcValue(0));
2091 }
Nate Begemanee625572006-01-27 21:09:22 +00002092 std::vector<MVT::ValueType> Tys;
2093 Tys.push_back(MVT::f64);
2094 Tys.push_back(MVT::Other);
2095 std::vector<SDOperand> Ops;
2096 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002097 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002098 Ops.push_back(DAG.getValueType(ArgVT));
2099 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2100 Tys.clear();
2101 Tys.push_back(MVT::Other);
2102 Tys.push_back(MVT::Flag);
2103 Ops.clear();
2104 Ops.push_back(Copy.getValue(1));
2105 Ops.push_back(Copy);
2106 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2107 }
2108 break;
2109 }
2110 case 3:
2111 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2112 SDOperand());
2113 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2114 break;
2115 }
2116 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2117 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2118 Copy.getValue(1));
2119 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002120 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002121}
Evan Cheng72261582005-12-20 06:22:03 +00002122
2123const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2124 switch (Opcode) {
2125 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002126 case X86ISD::SHLD: return "X86ISD::SHLD";
2127 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002128 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002129 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002130 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002131 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002132 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2133 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2134 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002135 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002136 case X86ISD::FST: return "X86ISD::FST";
2137 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002138 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002139 case X86ISD::CALL: return "X86ISD::CALL";
2140 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2141 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2142 case X86ISD::CMP: return "X86ISD::CMP";
2143 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002144 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002145 case X86ISD::CMOV: return "X86ISD::CMOV";
2146 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002147 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002148 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2149 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002150 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002151 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002152 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng72261582005-12-20 06:22:03 +00002153 }
2154}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002155
Nate Begeman368e18d2006-02-16 21:11:51 +00002156void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2157 uint64_t Mask,
2158 uint64_t &KnownZero,
2159 uint64_t &KnownOne,
2160 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002161
2162 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002163 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002164
2165 switch (Opc) {
2166 default:
2167 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2168 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002169 case X86ISD::SETCC:
2170 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2171 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002172 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002173}
Chris Lattner259e97c2006-01-31 19:43:35 +00002174
2175std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002176getRegClassForInlineAsmConstraint(const std::string &Constraint,
2177 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002178 if (Constraint.size() == 1) {
2179 // FIXME: not handling fp-stack yet!
2180 // FIXME: not handling MMX registers yet ('y' constraint).
2181 switch (Constraint[0]) { // GCC X86 Constraint Letters
2182 default: break; // Unknown constriant letter
2183 case 'r': // GENERAL_REGS
2184 case 'R': // LEGACY_REGS
2185 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2186 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2187 case 'l': // INDEX_REGS
2188 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2189 X86::ESI, X86::EDI, X86::EBP, 0);
2190 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2191 case 'Q': // Q_REGS
2192 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2193 case 'x': // SSE_REGS if SSE1 allowed
2194 if (Subtarget->hasSSE1())
2195 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2196 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2197 0);
2198 return std::vector<unsigned>();
2199 case 'Y': // SSE_REGS if SSE2 allowed
2200 if (Subtarget->hasSSE2())
2201 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2202 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2203 0);
2204 return std::vector<unsigned>();
2205 }
2206 }
2207
Chris Lattner1efa40f2006-02-22 00:56:39 +00002208 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002209}
Evan Cheng30b37b52006-03-13 23:18:16 +00002210
2211/// isLegalAddressImmediate - Return true if the integer value or
2212/// GlobalValue can be used as the offset of the target addressing mode.
2213bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2214 // X86 allows a sign-extended 32-bit immediate field.
2215 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2216}
2217
2218bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2219 if (getTargetMachine().
2220 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2221 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2222 if (RModel == Reloc::Static)
2223 return true;
2224 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002225 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002226 else
2227 return false;
2228 } else
2229 return true;
2230}