blob: c17795f39439e42816fba0928fb9d801c272584d [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 Lattner89fad2c2006-03-17 17:27:47 +0000642// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
643// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
644// EDX". Anything more is illegal.
645//
646// FIXME: The linscan register allocator currently has problem with
647// coallescing. At the time of this writing, whenever it decides to coallesce
648// a physreg with a virtreg, this increases the size of the physreg's live
649// range, and the live range cannot ever be reduced. This causes problems if
650// too many physregs are coalleced with virtregs, which can cause the register
651// allocator to wedge itself.
652//
653// This code triggers this problem more often if we pass args in registers,
654// so disable it until this is fixed.
655//
656// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
657// about code being dead.
658//
659static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000660
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000661
662std::vector<SDOperand>
663X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
664 std::vector<SDOperand> ArgValues;
665
666 MachineFunction &MF = DAG.getMachineFunction();
667 MachineFrameInfo *MFI = MF.getFrameInfo();
668
669 // Add DAG nodes to load the arguments... On entry to a function the stack
670 // frame looks like this:
671 //
672 // [ESP] -- return address
673 // [ESP + 4] -- first nonreg argument (leftmost lexically)
674 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
675 // ...
676 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
677
678 // Keep track of the number of integer regs passed so far. This can be either
679 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
680 // used).
681 unsigned NumIntRegs = 0;
Chris Lattner1c636e92006-03-17 05:10:20 +0000682
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000683 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
684 MVT::ValueType ObjectVT = getValueType(I->getType());
685 unsigned ArgIncrement = 4;
686 unsigned ObjSize = 0;
687 SDOperand ArgValue;
688
689 switch (ObjectVT) {
690 default: assert(0 && "Unhandled argument type!");
691 case MVT::i1:
692 case MVT::i8:
Chris Lattner1c636e92006-03-17 05:10:20 +0000693 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000694 if (!I->use_empty()) {
695 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
696 X86::R8RegisterClass);
697 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
698 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000699 if (ObjectVT == MVT::i1)
700 // FIXME: Should insert a assertzext here.
701 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000702 }
703 ++NumIntRegs;
704 break;
705 }
706
707 ObjSize = 1;
708 break;
709 case MVT::i16:
Chris Lattner1c636e92006-03-17 05:10:20 +0000710 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000711 if (!I->use_empty()) {
712 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
713 X86::R16RegisterClass);
714 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
715 DAG.setRoot(ArgValue.getValue(1));
716 }
717 ++NumIntRegs;
718 break;
719 }
720 ObjSize = 2;
721 break;
722 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000723 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000724 if (!I->use_empty()) {
Chris Lattner1c636e92006-03-17 05:10:20 +0000725 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000726 X86::R32RegisterClass);
727 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
728 DAG.setRoot(ArgValue.getValue(1));
729 }
730 ++NumIntRegs;
731 break;
732 }
733 ObjSize = 4;
734 break;
735 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000736 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000737 if (!I->use_empty()) {
738 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
739 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
740
741 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
742 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
743 DAG.setRoot(Hi.getValue(1));
744
745 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
746 }
Chris Lattner1c636e92006-03-17 05:10:20 +0000747 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000748 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000749 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000750 if (!I->use_empty()) {
751 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
752 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
753 DAG.setRoot(Low.getValue(1));
754
755 // Load the high part from memory.
756 // Create the frame index object for this incoming parameter...
757 int FI = MFI->CreateFixedObject(4, ArgOffset);
758 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
759 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
760 DAG.getSrcValue(NULL));
761 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
762 }
763 ArgOffset += 4;
Chris Lattner1c636e92006-03-17 05:10:20 +0000764 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000765 break;
766 }
767 ObjSize = ArgIncrement = 8;
768 break;
769 case MVT::f32: ObjSize = 4; break;
770 case MVT::f64: ObjSize = ArgIncrement = 8; break;
771 }
772
773 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
774 // dead loads.
775 if (ObjSize && !I->use_empty()) {
776 // Create the frame index object for this incoming parameter...
777 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
778
779 // Create the SelectionDAG nodes corresponding to a load from this
780 // parameter.
781 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
782
783 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
784 DAG.getSrcValue(NULL));
785 } else if (ArgValue.Val == 0) {
786 if (MVT::isInteger(ObjectVT))
787 ArgValue = DAG.getConstant(0, ObjectVT);
788 else
789 ArgValue = DAG.getConstantFP(0, ObjectVT);
790 }
791 ArgValues.push_back(ArgValue);
792
793 if (ObjSize)
794 ArgOffset += ArgIncrement; // Move on to the next argument.
795 }
796
797 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
798 // arguments and the arguments after the retaddr has been pushed are aligned.
799 if ((ArgOffset & 7) == 0)
800 ArgOffset += 4;
801
802 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
803 ReturnAddrIndex = 0; // No return address slot generated yet.
804 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
805 BytesCallerReserves = 0;
806
807 // Finally, inform the code generator which regs we return values in.
808 switch (getValueType(F.getReturnType())) {
809 default: assert(0 && "Unknown type!");
810 case MVT::isVoid: break;
811 case MVT::i1:
812 case MVT::i8:
813 case MVT::i16:
814 case MVT::i32:
815 MF.addLiveOut(X86::EAX);
816 break;
817 case MVT::i64:
818 MF.addLiveOut(X86::EAX);
819 MF.addLiveOut(X86::EDX);
820 break;
821 case MVT::f32:
822 case MVT::f64:
823 MF.addLiveOut(X86::ST0);
824 break;
825 }
826 return ArgValues;
827}
828
829std::pair<SDOperand, SDOperand>
830X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
831 bool isTailCall, SDOperand Callee,
832 ArgListTy &Args, SelectionDAG &DAG) {
833 // Count how many bytes are to be pushed on the stack.
834 unsigned NumBytes = 0;
835
836 // Keep track of the number of integer regs passed so far. This can be either
837 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
838 // used).
839 unsigned NumIntRegs = 0;
840
841 for (unsigned i = 0, e = Args.size(); i != e; ++i)
842 switch (getValueType(Args[i].second)) {
843 default: assert(0 && "Unknown value type!");
844 case MVT::i1:
845 case MVT::i8:
846 case MVT::i16:
847 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000848 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000849 ++NumIntRegs;
850 break;
851 }
852 // fall through
853 case MVT::f32:
854 NumBytes += 4;
855 break;
856 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000857 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
858 NumIntRegs += 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000859 break;
Chris Lattner1c636e92006-03-17 05:10:20 +0000860 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
861 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000862 NumBytes += 4;
863 break;
864 }
865
866 // fall through
867 case MVT::f64:
868 NumBytes += 8;
869 break;
870 }
871
872 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
873 // arguments and the arguments after the retaddr has been pushed are aligned.
874 if ((NumBytes & 7) == 0)
875 NumBytes += 4;
876
Chris Lattner94dd2922006-02-13 09:00:43 +0000877 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000878
879 // Arguments go on the stack in reverse order, as specified by the ABI.
880 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000881 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000882 NumIntRegs = 0;
883 std::vector<SDOperand> Stores;
884 std::vector<SDOperand> RegValuesToPass;
885 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
886 switch (getValueType(Args[i].second)) {
887 default: assert(0 && "Unexpected ValueType for argument!");
888 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000889 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
890 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000891 case MVT::i8:
892 case MVT::i16:
893 case MVT::i32:
Chris Lattner1c636e92006-03-17 05:10:20 +0000894 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000895 RegValuesToPass.push_back(Args[i].first);
896 ++NumIntRegs;
897 break;
898 }
899 // Fall through
900 case MVT::f32: {
901 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
902 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
903 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
904 Args[i].first, PtrOff,
905 DAG.getSrcValue(NULL)));
906 ArgOffset += 4;
907 break;
908 }
909 case MVT::i64:
Chris Lattner1c636e92006-03-17 05:10:20 +0000910 // Can pass (at least) part of it in regs?
911 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000912 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
913 Args[i].first, DAG.getConstant(1, MVT::i32));
914 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
915 Args[i].first, DAG.getConstant(0, MVT::i32));
916 RegValuesToPass.push_back(Lo);
917 ++NumIntRegs;
Chris Lattner1c636e92006-03-17 05:10:20 +0000918
919 // Pass both parts in regs?
920 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000921 RegValuesToPass.push_back(Hi);
922 ++NumIntRegs;
923 } else {
924 // Pass the high part in memory.
925 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
926 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
927 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
928 Hi, PtrOff, DAG.getSrcValue(NULL)));
929 ArgOffset += 4;
930 }
931 break;
932 }
933 // Fall through
934 case MVT::f64:
935 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
936 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
937 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
938 Args[i].first, PtrOff,
939 DAG.getSrcValue(NULL)));
940 ArgOffset += 8;
941 break;
942 }
943 }
944 if (!Stores.empty())
945 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
946
947 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
948 // arguments and the arguments after the retaddr has been pushed are aligned.
949 if ((ArgOffset & 7) == 0)
950 ArgOffset += 4;
951
952 std::vector<MVT::ValueType> RetVals;
953 MVT::ValueType RetTyVT = getValueType(RetTy);
954
955 RetVals.push_back(MVT::Other);
956
957 // The result values produced have to be legal. Promote the result.
958 switch (RetTyVT) {
959 case MVT::isVoid: break;
960 default:
961 RetVals.push_back(RetTyVT);
962 break;
963 case MVT::i1:
964 case MVT::i8:
965 case MVT::i16:
966 RetVals.push_back(MVT::i32);
967 break;
968 case MVT::f32:
969 if (X86ScalarSSE)
970 RetVals.push_back(MVT::f32);
971 else
972 RetVals.push_back(MVT::f64);
973 break;
974 case MVT::i64:
975 RetVals.push_back(MVT::i32);
976 RetVals.push_back(MVT::i32);
977 break;
978 }
979
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000980 // Build a sequence of copy-to-reg nodes chained together with token chain
981 // and flag operands which copy the outgoing args into registers.
982 SDOperand InFlag;
983 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
984 unsigned CCReg;
985 SDOperand RegToPass = RegValuesToPass[i];
986 switch (RegToPass.getValueType()) {
987 default: assert(0 && "Bad thing to pass in regs");
988 case MVT::i8:
989 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000990 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000991 case MVT::i16:
992 CCReg = (i == 0) ? X86::AX : X86::DX;
993 break;
994 case MVT::i32:
995 CCReg = (i == 0) ? X86::EAX : X86::EDX;
996 break;
997 }
998
999 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1000 InFlag = Chain.getValue(1);
1001 }
1002
1003 std::vector<MVT::ValueType> NodeTys;
1004 NodeTys.push_back(MVT::Other); // Returns a chain
1005 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1006 std::vector<SDOperand> Ops;
1007 Ops.push_back(Chain);
1008 Ops.push_back(Callee);
1009 if (InFlag.Val)
1010 Ops.push_back(InFlag);
1011
1012 // FIXME: Do not generate X86ISD::TAILCALL for now.
1013 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1014 InFlag = Chain.getValue(1);
1015
1016 NodeTys.clear();
1017 NodeTys.push_back(MVT::Other); // Returns a chain
1018 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1019 Ops.clear();
1020 Ops.push_back(Chain);
1021 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1022 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1023 Ops.push_back(InFlag);
1024 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1025 InFlag = Chain.getValue(1);
1026
1027 SDOperand RetVal;
1028 if (RetTyVT != MVT::isVoid) {
1029 switch (RetTyVT) {
1030 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +00001031 case MVT::i1:
1032 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001033 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1034 Chain = RetVal.getValue(1);
1035 if (RetTyVT == MVT::i1)
1036 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1037 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001038 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001039 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1040 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001041 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001042 case MVT::i32:
1043 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1044 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001045 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001046 case MVT::i64: {
1047 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1048 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1049 Lo.getValue(2));
1050 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1051 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001052 break;
1053 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001054 case MVT::f32:
1055 case MVT::f64: {
1056 std::vector<MVT::ValueType> Tys;
1057 Tys.push_back(MVT::f64);
1058 Tys.push_back(MVT::Other);
1059 Tys.push_back(MVT::Flag);
1060 std::vector<SDOperand> Ops;
1061 Ops.push_back(Chain);
1062 Ops.push_back(InFlag);
1063 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1064 Chain = RetVal.getValue(1);
1065 InFlag = RetVal.getValue(2);
1066 if (X86ScalarSSE) {
1067 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1068 // shouldn't be necessary except that RFP cannot be live across
1069 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1070 MachineFunction &MF = DAG.getMachineFunction();
1071 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1072 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1073 Tys.clear();
1074 Tys.push_back(MVT::Other);
1075 Ops.clear();
1076 Ops.push_back(Chain);
1077 Ops.push_back(RetVal);
1078 Ops.push_back(StackSlot);
1079 Ops.push_back(DAG.getValueType(RetTyVT));
1080 Ops.push_back(InFlag);
1081 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1082 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1083 DAG.getSrcValue(NULL));
1084 Chain = RetVal.getValue(1);
1085 }
Evan Chengd9558e02006-01-06 00:43:03 +00001086
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001087 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1088 // FIXME: we would really like to remember that this FP_ROUND
1089 // operation is okay to eliminate if we allow excess FP precision.
1090 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1091 break;
1092 }
1093 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001094 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001095
1096 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001097}
1098
1099SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1100 if (ReturnAddrIndex == 0) {
1101 // Set up a frame object for the return address.
1102 MachineFunction &MF = DAG.getMachineFunction();
1103 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1104 }
1105
1106 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1107}
1108
1109
1110
1111std::pair<SDOperand, SDOperand> X86TargetLowering::
1112LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1113 SelectionDAG &DAG) {
1114 SDOperand Result;
1115 if (Depth) // Depths > 0 not supported yet!
1116 Result = DAG.getConstant(0, getPointerTy());
1117 else {
1118 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1119 if (!isFrameAddress)
1120 // Just load the return address
1121 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1122 DAG.getSrcValue(NULL));
1123 else
1124 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1125 DAG.getConstant(4, MVT::i32));
1126 }
1127 return std::make_pair(Result, Chain);
1128}
1129
Evan Cheng4a460802006-01-11 00:33:36 +00001130/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1131/// which corresponds to the condition code.
1132static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1133 switch (X86CC) {
1134 default: assert(0 && "Unknown X86 conditional code!");
1135 case X86ISD::COND_A: return X86::JA;
1136 case X86ISD::COND_AE: return X86::JAE;
1137 case X86ISD::COND_B: return X86::JB;
1138 case X86ISD::COND_BE: return X86::JBE;
1139 case X86ISD::COND_E: return X86::JE;
1140 case X86ISD::COND_G: return X86::JG;
1141 case X86ISD::COND_GE: return X86::JGE;
1142 case X86ISD::COND_L: return X86::JL;
1143 case X86ISD::COND_LE: return X86::JLE;
1144 case X86ISD::COND_NE: return X86::JNE;
1145 case X86ISD::COND_NO: return X86::JNO;
1146 case X86ISD::COND_NP: return X86::JNP;
1147 case X86ISD::COND_NS: return X86::JNS;
1148 case X86ISD::COND_O: return X86::JO;
1149 case X86ISD::COND_P: return X86::JP;
1150 case X86ISD::COND_S: return X86::JS;
1151 }
1152}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001153
Evan Cheng6dfa9992006-01-30 23:41:35 +00001154/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1155/// specific condition code. It returns a false if it cannot do a direct
1156/// translation. X86CC is the translated CondCode. Flip is set to true if the
1157/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001158static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1159 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001160 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001161 Flip = false;
1162 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001163 if (!isFP) {
1164 switch (SetCCOpcode) {
1165 default: break;
1166 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1167 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1168 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1169 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1170 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1171 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1172 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1173 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1174 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1175 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1176 }
1177 } else {
1178 // On a floating point condition, the flags are set as follows:
1179 // ZF PF CF op
1180 // 0 | 0 | 0 | X > Y
1181 // 0 | 0 | 1 | X < Y
1182 // 1 | 0 | 0 | X == Y
1183 // 1 | 1 | 1 | unordered
1184 switch (SetCCOpcode) {
1185 default: break;
1186 case ISD::SETUEQ:
1187 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001188 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001189 case ISD::SETOGT:
1190 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001191 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001192 case ISD::SETOGE:
1193 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001194 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001195 case ISD::SETULT:
1196 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001197 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001198 case ISD::SETULE:
1199 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1200 case ISD::SETONE:
1201 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1202 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1203 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1204 }
1205 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001206
1207 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001208}
1209
Evan Cheng4a460802006-01-11 00:33:36 +00001210/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1211/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001212/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001213static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001214 switch (X86CC) {
1215 default:
1216 return false;
1217 case X86ISD::COND_B:
1218 case X86ISD::COND_BE:
1219 case X86ISD::COND_E:
1220 case X86ISD::COND_P:
1221 case X86ISD::COND_A:
1222 case X86ISD::COND_AE:
1223 case X86ISD::COND_NE:
1224 case X86ISD::COND_NP:
1225 return true;
1226 }
1227}
1228
Evan Cheng4a460802006-01-11 00:33:36 +00001229MachineBasicBlock *
1230X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1231 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001232 switch (MI->getOpcode()) {
1233 default: assert(false && "Unexpected instr type to insert");
1234 case X86::CMOV_FR32:
1235 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001236 // To "insert" a SELECT_CC instruction, we actually have to insert the
1237 // diamond control-flow pattern. The incoming instruction knows the
1238 // destination vreg to set, the condition code register to branch on, the
1239 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001240 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1241 ilist<MachineBasicBlock>::iterator It = BB;
1242 ++It;
1243
1244 // thisMBB:
1245 // ...
1246 // TrueVal = ...
1247 // cmpTY ccX, r1, r2
1248 // bCC copy1MBB
1249 // fallthrough --> copy0MBB
1250 MachineBasicBlock *thisMBB = BB;
1251 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1252 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1253 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1254 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1255 MachineFunction *F = BB->getParent();
1256 F->getBasicBlockList().insert(It, copy0MBB);
1257 F->getBasicBlockList().insert(It, sinkMBB);
1258 // Update machine-CFG edges
1259 BB->addSuccessor(copy0MBB);
1260 BB->addSuccessor(sinkMBB);
1261
1262 // copy0MBB:
1263 // %FalseValue = ...
1264 // # fallthrough to sinkMBB
1265 BB = copy0MBB;
1266
1267 // Update machine-CFG edges
1268 BB->addSuccessor(sinkMBB);
1269
1270 // sinkMBB:
1271 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1272 // ...
1273 BB = sinkMBB;
1274 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1275 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1276 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001277
Evan Cheng0cc39452006-01-16 21:21:29 +00001278 delete MI; // The pseudo instruction is gone now.
1279 return BB;
1280 }
Evan Cheng4a460802006-01-11 00:33:36 +00001281
Evan Cheng0cc39452006-01-16 21:21:29 +00001282 case X86::FP_TO_INT16_IN_MEM:
1283 case X86::FP_TO_INT32_IN_MEM:
1284 case X86::FP_TO_INT64_IN_MEM: {
1285 // Change the floating point control register to use "round towards zero"
1286 // mode when truncating to an integer value.
1287 MachineFunction *F = BB->getParent();
1288 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1289 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1290
1291 // Load the old value of the high byte of the control word...
1292 unsigned OldCW =
1293 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1294 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1295
1296 // Set the high part to be round to zero...
1297 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1298
1299 // Reload the modified control word now...
1300 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1301
1302 // Restore the memory image of control word to original value
1303 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1304
1305 // Get the X86 opcode to use.
1306 unsigned Opc;
1307 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001308 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001309 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1310 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1311 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1312 }
1313
1314 X86AddressMode AM;
1315 MachineOperand &Op = MI->getOperand(0);
1316 if (Op.isRegister()) {
1317 AM.BaseType = X86AddressMode::RegBase;
1318 AM.Base.Reg = Op.getReg();
1319 } else {
1320 AM.BaseType = X86AddressMode::FrameIndexBase;
1321 AM.Base.FrameIndex = Op.getFrameIndex();
1322 }
1323 Op = MI->getOperand(1);
1324 if (Op.isImmediate())
1325 AM.Scale = Op.getImmedValue();
1326 Op = MI->getOperand(2);
1327 if (Op.isImmediate())
1328 AM.IndexReg = Op.getImmedValue();
1329 Op = MI->getOperand(3);
1330 if (Op.isGlobalAddress()) {
1331 AM.GV = Op.getGlobal();
1332 } else {
1333 AM.Disp = Op.getImmedValue();
1334 }
1335 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1336
1337 // Reload the original control word now.
1338 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1339
1340 delete MI; // The pseudo instruction is gone now.
1341 return BB;
1342 }
1343 }
Evan Cheng4a460802006-01-11 00:33:36 +00001344}
1345
1346
1347//===----------------------------------------------------------------------===//
1348// X86 Custom Lowering Hooks
1349//===----------------------------------------------------------------------===//
1350
Evan Cheng30b37b52006-03-13 23:18:16 +00001351/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1352/// load. For Darwin, external and weak symbols are indirect, loading the value
1353/// at address GV rather then the value of GV itself. This means that the
1354/// GlobalAddress must be in the base or index register of the address, not the
1355/// GV offset field.
1356static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1357 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1358 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1359}
1360
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001361/// LowerOperation - Provide custom lowering hooks for some operations.
1362///
1363SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1364 switch (Op.getOpcode()) {
1365 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001366 case ISD::SHL_PARTS:
1367 case ISD::SRA_PARTS:
1368 case ISD::SRL_PARTS: {
1369 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1370 "Not an i64 shift!");
1371 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1372 SDOperand ShOpLo = Op.getOperand(0);
1373 SDOperand ShOpHi = Op.getOperand(1);
1374 SDOperand ShAmt = Op.getOperand(2);
1375 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001376 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001377 : DAG.getConstant(0, MVT::i32);
1378
1379 SDOperand Tmp2, Tmp3;
1380 if (Op.getOpcode() == ISD::SHL_PARTS) {
1381 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1382 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1383 } else {
1384 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001385 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001386 }
1387
1388 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1389 ShAmt, DAG.getConstant(32, MVT::i8));
1390
1391 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001392 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001393
1394 std::vector<MVT::ValueType> Tys;
1395 Tys.push_back(MVT::i32);
1396 Tys.push_back(MVT::Flag);
1397 std::vector<SDOperand> Ops;
1398 if (Op.getOpcode() == ISD::SHL_PARTS) {
1399 Ops.push_back(Tmp2);
1400 Ops.push_back(Tmp3);
1401 Ops.push_back(CC);
1402 Ops.push_back(InFlag);
1403 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1404 InFlag = Hi.getValue(1);
1405
1406 Ops.clear();
1407 Ops.push_back(Tmp3);
1408 Ops.push_back(Tmp1);
1409 Ops.push_back(CC);
1410 Ops.push_back(InFlag);
1411 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1412 } else {
1413 Ops.push_back(Tmp2);
1414 Ops.push_back(Tmp3);
1415 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001416 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001417 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1418 InFlag = Lo.getValue(1);
1419
1420 Ops.clear();
1421 Ops.push_back(Tmp3);
1422 Ops.push_back(Tmp1);
1423 Ops.push_back(CC);
1424 Ops.push_back(InFlag);
1425 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1426 }
1427
1428 Tys.clear();
1429 Tys.push_back(MVT::i32);
1430 Tys.push_back(MVT::i32);
1431 Ops.clear();
1432 Ops.push_back(Lo);
1433 Ops.push_back(Hi);
1434 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1435 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001436 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001437 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001438 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001439 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001440
1441 SDOperand Result;
1442 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1443 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001444 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001445 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001446 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001447 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1448 DAG.getEntryNode(), Op.getOperand(0),
1449 StackSlot, DAG.getSrcValue(NULL));
1450
1451 // Build the FILD
1452 std::vector<MVT::ValueType> Tys;
1453 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001454 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001455 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001456 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001457 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001458 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001459 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001460 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1461 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001462
1463 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001464 Chain = Result.getValue(1);
1465 SDOperand InFlag = Result.getValue(2);
1466
Evan Chenge3de85b2006-02-04 02:20:30 +00001467 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001468 // shouldn't be necessary except that RFP cannot be live across
1469 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1470 MachineFunction &MF = DAG.getMachineFunction();
1471 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1472 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1473 std::vector<MVT::ValueType> Tys;
1474 Tys.push_back(MVT::Other);
1475 std::vector<SDOperand> Ops;
1476 Ops.push_back(Chain);
1477 Ops.push_back(Result);
1478 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001479 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001480 Ops.push_back(InFlag);
1481 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1482 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1483 DAG.getSrcValue(NULL));
1484 }
1485
Evan Chenga3195e82006-01-12 22:54:21 +00001486 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001487 }
1488 case ISD::FP_TO_SINT: {
1489 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001490 "Unknown FP_TO_SINT to lower!");
1491 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1492 // stack slot.
1493 MachineFunction &MF = DAG.getMachineFunction();
1494 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1495 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1496 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1497
1498 unsigned Opc;
1499 switch (Op.getValueType()) {
1500 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1501 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1502 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1503 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1504 }
1505
Evan Cheng6dab0532006-01-30 08:02:57 +00001506 SDOperand Chain = DAG.getEntryNode();
1507 SDOperand Value = Op.getOperand(0);
1508 if (X86ScalarSSE) {
1509 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1510 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1511 DAG.getSrcValue(0));
1512 std::vector<MVT::ValueType> Tys;
1513 Tys.push_back(MVT::f64);
1514 Tys.push_back(MVT::Other);
1515 std::vector<SDOperand> Ops;
1516 Ops.push_back(Chain);
1517 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001518 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001519 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1520 Chain = Value.getValue(1);
1521 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1522 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1523 }
1524
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001525 // Build the FP_TO_INT*_IN_MEM
1526 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001527 Ops.push_back(Chain);
1528 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001529 Ops.push_back(StackSlot);
1530 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1531
1532 // Load the result.
1533 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1534 DAG.getSrcValue(NULL));
1535 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001536 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001537 std::vector<MVT::ValueType> Tys;
1538 Tys.push_back(MVT::Other);
1539 Tys.push_back(MVT::Flag);
1540 std::vector<SDOperand> Ops;
1541 Ops.push_back(Op.getOperand(0));
1542 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001543 Ops.clear();
1544 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1545 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1546 MVT::i32, Ops[0].getValue(2)));
1547 Ops.push_back(Ops[1].getValue(1));
1548 Tys[0] = Tys[1] = MVT::i32;
1549 Tys.push_back(MVT::Other);
1550 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001551 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001552 case ISD::FABS: {
1553 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001554 const Type *OpNTy = MVT::getTypeForValueType(VT);
1555 std::vector<Constant*> CV;
1556 if (VT == MVT::f64) {
1557 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1558 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1559 } else {
1560 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1561 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1562 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1563 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1564 }
1565 Constant *CS = ConstantStruct::get(CV);
1566 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1567 SDOperand Mask
1568 = DAG.getNode(X86ISD::LOAD_PACK,
1569 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001570 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1571 }
Evan Cheng223547a2006-01-31 22:28:30 +00001572 case ISD::FNEG: {
1573 MVT::ValueType VT = Op.getValueType();
1574 const Type *OpNTy = MVT::getTypeForValueType(VT);
1575 std::vector<Constant*> CV;
1576 if (VT == MVT::f64) {
1577 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1578 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1579 } else {
1580 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1581 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1582 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1583 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1584 }
1585 Constant *CS = ConstantStruct::get(CV);
1586 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1587 SDOperand Mask
1588 = DAG.getNode(X86ISD::LOAD_PACK,
1589 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1590 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1591 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001592 case ISD::SETCC: {
1593 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001594 SDOperand Cond;
1595 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001596 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1597 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001598 bool Flip;
1599 unsigned X86CC;
1600 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1601 if (Flip)
1602 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1603 Op.getOperand(1), Op.getOperand(0));
1604 else
1605 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1606 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001607 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1608 DAG.getConstant(X86CC, MVT::i8), Cond);
1609 } else {
1610 assert(isFP && "Illegal integer SetCC!");
1611
Evan Cheng6dfa9992006-01-30 23:41:35 +00001612 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1613 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001614 std::vector<MVT::ValueType> Tys;
1615 std::vector<SDOperand> Ops;
1616 switch (SetCCOpcode) {
1617 default: assert(false && "Illegal floating point SetCC!");
1618 case ISD::SETOEQ: { // !PF & ZF
1619 Tys.push_back(MVT::i8);
1620 Tys.push_back(MVT::Flag);
1621 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1622 Ops.push_back(Cond);
1623 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1624 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1625 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1626 Tmp1.getValue(1));
1627 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1628 }
Evan Chengd9558e02006-01-06 00:43:03 +00001629 case ISD::SETUNE: { // PF | !ZF
1630 Tys.push_back(MVT::i8);
1631 Tys.push_back(MVT::Flag);
1632 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1633 Ops.push_back(Cond);
1634 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1635 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1636 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1637 Tmp1.getValue(1));
1638 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1639 }
1640 }
1641 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001642 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001643 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001644 MVT::ValueType VT = Op.getValueType();
1645 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001646 bool isFPStack = isFP && !X86ScalarSSE;
1647 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001648 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001649 SDOperand Op0 = Op.getOperand(0);
1650 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001651 if (Op0.getOpcode() == ISD::SETCC)
1652 Op0 = LowerOperation(Op0, DAG);
1653
Evan Chengaaca22c2006-01-10 20:26:56 +00001654 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001655 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1656 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1657 // have another use it will be eliminated.
1658 // If the X86ISD::SETCC has more than one use, then it's probably better
1659 // to use a test instead of duplicating the X86ISD::CMP (for register
1660 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001661 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1662 if (!Op0.hasOneUse()) {
1663 std::vector<MVT::ValueType> Tys;
1664 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1665 Tys.push_back(Op0.Val->getValueType(i));
1666 std::vector<SDOperand> Ops;
1667 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1668 Ops.push_back(Op0.getOperand(i));
1669 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1670 }
1671
Evan Cheng1bcee362006-01-13 01:03:02 +00001672 CC = Op0.getOperand(0);
1673 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001674 // Make a copy as flag result cannot be used by more than one.
1675 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1676 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001677 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001678 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001679 } else
1680 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001681 } else
1682 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001683
Evan Cheng189d01e2006-01-13 01:06:49 +00001684 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001685 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001686 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001687 }
Evan Chenge3413162006-01-09 18:33:28 +00001688
1689 std::vector<MVT::ValueType> Tys;
1690 Tys.push_back(Op.getValueType());
1691 Tys.push_back(MVT::Flag);
1692 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001693 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1694 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001695 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001696 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001697 Ops.push_back(CC);
1698 Ops.push_back(Cond);
1699 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001700 }
Evan Cheng898101c2005-12-19 23:12:38 +00001701 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001702 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001703 SDOperand Cond = Op.getOperand(1);
1704 SDOperand Dest = Op.getOperand(2);
1705 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001706 if (Cond.getOpcode() == ISD::SETCC)
1707 Cond = LowerOperation(Cond, DAG);
1708
Evan Chengd5781fc2005-12-21 20:21:51 +00001709 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001710 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1711 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1712 // have another use it will be eliminated.
1713 // If the X86ISD::SETCC has more than one use, then it's probably better
1714 // to use a test instead of duplicating the X86ISD::CMP (for register
1715 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001716 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1717 if (!Cond.hasOneUse()) {
1718 std::vector<MVT::ValueType> Tys;
1719 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1720 Tys.push_back(Cond.Val->getValueType(i));
1721 std::vector<SDOperand> Ops;
1722 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1723 Ops.push_back(Cond.getOperand(i));
1724 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1725 }
1726
Evan Cheng1bcee362006-01-13 01:03:02 +00001727 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001728 Cond = Cond.getOperand(1);
1729 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001730 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001731 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001732 } else
1733 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001734 } else
1735 addTest = true;
1736
1737 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001738 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001739 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1740 }
1741 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1742 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1743 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001744 case ISD::MEMSET: {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001745 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001746 SDOperand Chain = Op.getOperand(0);
1747 unsigned Align =
1748 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1749 if (Align == 0) Align = 1;
1750
Evan Cheng18a84522006-02-16 00:21:07 +00001751 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1752 // If not DWORD aligned, call memset if size is less than the threshold.
1753 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001754 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001755 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001756 MVT::ValueType IntPtr = getPointerTy();
1757 const Type *IntPtrTy = getTargetData().getIntPtrType();
1758 std::vector<std::pair<SDOperand, const Type*> > Args;
1759 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1760 // Extend the ubyte argument to be an int value for the call.
1761 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1762 Args.push_back(std::make_pair(Val, IntPtrTy));
1763 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1764 std::pair<SDOperand,SDOperand> CallResult =
1765 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1766 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1767 return CallResult.second;
1768 }
1769
Evan Cheng67f92a72006-01-11 22:15:48 +00001770 MVT::ValueType AVT;
1771 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001772 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
1773 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001774 bool TwoRepStos = false;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001775 if (ValC) {
Evan Cheng67f92a72006-01-11 22:15:48 +00001776 unsigned ValReg;
1777 unsigned Val = ValC->getValue() & 255;
1778
1779 // If the value is a constant, then we can potentially use larger sets.
1780 switch (Align & 3) {
1781 case 2: // WORD aligned
1782 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001783 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1784 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001785 Val = (Val << 8) | Val;
1786 ValReg = X86::AX;
1787 break;
1788 case 0: // DWORD aligned
1789 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001790 if (I) {
1791 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1792 BytesLeft = I->getValue() % 4;
1793 } else {
1794 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1795 DAG.getConstant(2, MVT::i8));
1796 TwoRepStos = true;
1797 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001798 Val = (Val << 8) | Val;
1799 Val = (Val << 16) | Val;
1800 ValReg = X86::EAX;
1801 break;
1802 default: // Byte aligned
1803 AVT = MVT::i8;
1804 Count = Op.getOperand(3);
1805 ValReg = X86::AL;
1806 break;
1807 }
1808
1809 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1810 InFlag);
1811 InFlag = Chain.getValue(1);
1812 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001813 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001814 Count = Op.getOperand(3);
1815 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1816 InFlag = Chain.getValue(1);
1817 }
1818
1819 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1820 InFlag = Chain.getValue(1);
1821 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1822 InFlag = Chain.getValue(1);
1823
Evan Chengff909922006-03-07 23:29:39 +00001824 std::vector<MVT::ValueType> Tys;
1825 Tys.push_back(MVT::Other);
1826 Tys.push_back(MVT::Flag);
1827 std::vector<SDOperand> Ops;
1828 Ops.push_back(Chain);
1829 Ops.push_back(DAG.getValueType(AVT));
1830 Ops.push_back(InFlag);
1831 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1832
1833 if (TwoRepStos) {
1834 InFlag = Chain.getValue(1);
1835 Count = Op.getOperand(3);
1836 MVT::ValueType CVT = Count.getValueType();
1837 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1838 DAG.getConstant(3, CVT));
1839 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1840 InFlag = Chain.getValue(1);
1841 Tys.clear();
1842 Tys.push_back(MVT::Other);
1843 Tys.push_back(MVT::Flag);
1844 Ops.clear();
1845 Ops.push_back(Chain);
1846 Ops.push_back(DAG.getValueType(MVT::i8));
1847 Ops.push_back(InFlag);
1848 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
1849 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001850 // Issue stores for the last 1 - 3 bytes.
1851 SDOperand Value;
1852 unsigned Val = ValC->getValue() & 255;
1853 unsigned Offset = I->getValue() - BytesLeft;
1854 SDOperand DstAddr = Op.getOperand(1);
1855 MVT::ValueType AddrVT = DstAddr.getValueType();
1856 if (BytesLeft >= 2) {
1857 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
1858 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1859 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1860 DAG.getConstant(Offset, AddrVT)),
1861 DAG.getSrcValue(NULL));
1862 BytesLeft -= 2;
1863 Offset += 2;
1864 }
1865
1866 if (BytesLeft == 1) {
1867 Value = DAG.getConstant(Val, MVT::i8);
1868 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1869 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
1870 DAG.getConstant(Offset, AddrVT)),
1871 DAG.getSrcValue(NULL));
1872 }
1873 }
1874
1875 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001876 }
1877 case ISD::MEMCPY: {
1878 SDOperand Chain = Op.getOperand(0);
1879 unsigned Align =
1880 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1881 if (Align == 0) Align = 1;
1882
Evan Cheng18a84522006-02-16 00:21:07 +00001883 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1884 // If not DWORD aligned, call memcpy if size is less than the threshold.
1885 // It knows how to align to the right boundary first.
Evan Cheng62bec2c2006-03-04 02:48:56 +00001886 if ((Align & 3) != 0 ||
Evan Chengff909922006-03-07 23:29:39 +00001887 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng18a84522006-02-16 00:21:07 +00001888 MVT::ValueType IntPtr = getPointerTy();
1889 const Type *IntPtrTy = getTargetData().getIntPtrType();
1890 std::vector<std::pair<SDOperand, const Type*> > Args;
1891 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1892 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1893 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1894 std::pair<SDOperand,SDOperand> CallResult =
1895 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1896 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1897 return CallResult.second;
1898 }
1899
Evan Cheng67f92a72006-01-11 22:15:48 +00001900 MVT::ValueType AVT;
1901 SDOperand Count;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001902 unsigned BytesLeft = 0;
Evan Chengff909922006-03-07 23:29:39 +00001903 bool TwoRepMovs = false;
Evan Cheng67f92a72006-01-11 22:15:48 +00001904 switch (Align & 3) {
1905 case 2: // WORD aligned
1906 AVT = MVT::i16;
Evan Cheng62bec2c2006-03-04 02:48:56 +00001907 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1908 BytesLeft = I->getValue() % 2;
Evan Cheng67f92a72006-01-11 22:15:48 +00001909 break;
1910 case 0: // DWORD aligned
1911 AVT = MVT::i32;
Evan Chengff909922006-03-07 23:29:39 +00001912 if (I) {
1913 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1914 BytesLeft = I->getValue() % 4;
1915 } else {
1916 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1917 DAG.getConstant(2, MVT::i8));
1918 TwoRepMovs = true;
1919 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001920 break;
1921 default: // Byte aligned
1922 AVT = MVT::i8;
1923 Count = Op.getOperand(3);
1924 break;
1925 }
1926
Evan Cheng62bec2c2006-03-04 02:48:56 +00001927 SDOperand InFlag(0, 0);
Evan Cheng67f92a72006-01-11 22:15:48 +00001928 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1929 InFlag = Chain.getValue(1);
1930 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1931 InFlag = Chain.getValue(1);
1932 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1933 InFlag = Chain.getValue(1);
1934
Evan Chengff909922006-03-07 23:29:39 +00001935 std::vector<MVT::ValueType> Tys;
1936 Tys.push_back(MVT::Other);
1937 Tys.push_back(MVT::Flag);
1938 std::vector<SDOperand> Ops;
1939 Ops.push_back(Chain);
1940 Ops.push_back(DAG.getValueType(AVT));
1941 Ops.push_back(InFlag);
1942 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1943
1944 if (TwoRepMovs) {
1945 InFlag = Chain.getValue(1);
1946 Count = Op.getOperand(3);
1947 MVT::ValueType CVT = Count.getValueType();
1948 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
1949 DAG.getConstant(3, CVT));
1950 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
1951 InFlag = Chain.getValue(1);
1952 Tys.clear();
1953 Tys.push_back(MVT::Other);
1954 Tys.push_back(MVT::Flag);
1955 Ops.clear();
1956 Ops.push_back(Chain);
1957 Ops.push_back(DAG.getValueType(MVT::i8));
1958 Ops.push_back(InFlag);
1959 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
1960 } else if (BytesLeft) {
Evan Cheng62bec2c2006-03-04 02:48:56 +00001961 // Issue loads and stores for the last 1 - 3 bytes.
1962 unsigned Offset = I->getValue() - BytesLeft;
1963 SDOperand DstAddr = Op.getOperand(1);
1964 MVT::ValueType DstVT = DstAddr.getValueType();
1965 SDOperand SrcAddr = Op.getOperand(2);
1966 MVT::ValueType SrcVT = SrcAddr.getValueType();
1967 SDOperand Value;
1968 if (BytesLeft >= 2) {
1969 Value = DAG.getLoad(MVT::i16, Chain,
1970 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1971 DAG.getConstant(Offset, SrcVT)),
1972 DAG.getSrcValue(NULL));
1973 Chain = Value.getValue(1);
1974 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1975 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1976 DAG.getConstant(Offset, DstVT)),
1977 DAG.getSrcValue(NULL));
1978 BytesLeft -= 2;
1979 Offset += 2;
1980 }
1981
1982 if (BytesLeft == 1) {
1983 Value = DAG.getLoad(MVT::i8, Chain,
1984 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
1985 DAG.getConstant(Offset, SrcVT)),
1986 DAG.getSrcValue(NULL));
1987 Chain = Value.getValue(1);
1988 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
1989 DAG.getNode(ISD::ADD, DstVT, DstAddr,
1990 DAG.getConstant(Offset, DstVT)),
1991 DAG.getSrcValue(NULL));
1992 }
1993 }
1994
1995 return Chain;
Evan Cheng67f92a72006-01-11 22:15:48 +00001996 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00001997
1998 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1999 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2000 // one of the above mentioned nodes. It has to be wrapped because otherwise
2001 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2002 // be used to form addressing mode. These wrapped nodes will be selected
2003 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00002004 case ISD::ConstantPool: {
2005 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00002006 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2007 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2008 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00002009 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002010 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002011 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00002012 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2013 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2014 }
2015
2016 return Result;
2017 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002018 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00002019 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2020 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2021 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00002022 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00002023 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00002024 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002025 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00002026 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2027 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00002028
2029 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Cheng30b37b52006-03-13 23:18:16 +00002030 // the value at address GV, not the value of GV itself. This means that
Evan Cheng7ccced62006-02-18 00:15:05 +00002031 // the GlobalAddress must be in the base or index register of the address,
2032 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00002033 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Cheng30b37b52006-03-13 23:18:16 +00002034 DarwinGVRequiresExtraLoad(GV))
Evan Cheng2338c5c2006-02-07 08:38:37 +00002035 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00002036 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00002037 }
Evan Cheng7ccced62006-02-18 00:15:05 +00002038
Evan Cheng002fe9b2006-01-12 07:56:47 +00002039 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002040 }
Evan Cheng020d2e82006-02-23 20:41:18 +00002041 case ISD::ExternalSymbol: {
2042 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2043 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2044 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
2045 if (getTargetMachine().
2046 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2047 // With PIC, the address is actually $g + Offset.
2048 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2049 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2050 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2051 }
2052
2053 return Result;
2054 }
Nate Begemanacc398c2006-01-25 18:21:52 +00002055 case ISD::VASTART: {
2056 // vastart just stores the address of the VarArgsFrameIndex slot into the
2057 // memory location argument.
2058 // FIXME: Replace MVT::i32 with PointerTy
2059 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2060 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2061 Op.getOperand(1), Op.getOperand(2));
2062 }
Nate Begemanee625572006-01-27 21:09:22 +00002063 case ISD::RET: {
2064 SDOperand Copy;
2065
2066 switch(Op.getNumOperands()) {
2067 default:
2068 assert(0 && "Do not know how to return this many arguments!");
2069 abort();
2070 case 1:
2071 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2072 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2073 case 2: {
2074 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2075 if (MVT::isInteger(ArgVT))
2076 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2077 SDOperand());
2078 else if (!X86ScalarSSE) {
2079 std::vector<MVT::ValueType> Tys;
2080 Tys.push_back(MVT::Other);
2081 Tys.push_back(MVT::Flag);
2082 std::vector<SDOperand> Ops;
2083 Ops.push_back(Op.getOperand(0));
2084 Ops.push_back(Op.getOperand(1));
2085 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2086 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00002087 SDOperand MemLoc;
2088 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002089 SDOperand Value = Op.getOperand(1);
2090
Evan Cheng760df292006-02-01 01:19:32 +00002091 if (Value.getOpcode() == ISD::LOAD &&
2092 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00002093 Chain = Value.getOperand(0);
2094 MemLoc = Value.getOperand(1);
2095 } else {
2096 // Spill the value to memory and reload it into top of stack.
2097 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2098 MachineFunction &MF = DAG.getMachineFunction();
2099 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2100 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2101 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2102 Value, MemLoc, DAG.getSrcValue(0));
2103 }
Nate Begemanee625572006-01-27 21:09:22 +00002104 std::vector<MVT::ValueType> Tys;
2105 Tys.push_back(MVT::f64);
2106 Tys.push_back(MVT::Other);
2107 std::vector<SDOperand> Ops;
2108 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00002109 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00002110 Ops.push_back(DAG.getValueType(ArgVT));
2111 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2112 Tys.clear();
2113 Tys.push_back(MVT::Other);
2114 Tys.push_back(MVT::Flag);
2115 Ops.clear();
2116 Ops.push_back(Copy.getValue(1));
2117 Ops.push_back(Copy);
2118 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2119 }
2120 break;
2121 }
2122 case 3:
2123 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2124 SDOperand());
2125 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2126 break;
2127 }
2128 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2129 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2130 Copy.getValue(1));
2131 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00002132 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002133}
Evan Cheng72261582005-12-20 06:22:03 +00002134
2135const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2136 switch (Opcode) {
2137 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00002138 case X86ISD::SHLD: return "X86ISD::SHLD";
2139 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00002140 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00002141 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00002142 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00002143 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00002144 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2145 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2146 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00002147 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00002148 case X86ISD::FST: return "X86ISD::FST";
2149 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00002150 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00002151 case X86ISD::CALL: return "X86ISD::CALL";
2152 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2153 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2154 case X86ISD::CMP: return "X86ISD::CMP";
2155 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00002156 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00002157 case X86ISD::CMOV: return "X86ISD::CMOV";
2158 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00002159 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002160 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2161 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002162 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002163 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002164 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng72261582005-12-20 06:22:03 +00002165 }
2166}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002167
Nate Begeman368e18d2006-02-16 21:11:51 +00002168void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2169 uint64_t Mask,
2170 uint64_t &KnownZero,
2171 uint64_t &KnownOne,
2172 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002173
2174 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002175 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002176
2177 switch (Opc) {
2178 default:
2179 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2180 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002181 case X86ISD::SETCC:
2182 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2183 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002184 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002185}
Chris Lattner259e97c2006-01-31 19:43:35 +00002186
2187std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002188getRegClassForInlineAsmConstraint(const std::string &Constraint,
2189 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002190 if (Constraint.size() == 1) {
2191 // FIXME: not handling fp-stack yet!
2192 // FIXME: not handling MMX registers yet ('y' constraint).
2193 switch (Constraint[0]) { // GCC X86 Constraint Letters
2194 default: break; // Unknown constriant letter
2195 case 'r': // GENERAL_REGS
2196 case 'R': // LEGACY_REGS
2197 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2198 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2199 case 'l': // INDEX_REGS
2200 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2201 X86::ESI, X86::EDI, X86::EBP, 0);
2202 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2203 case 'Q': // Q_REGS
2204 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2205 case 'x': // SSE_REGS if SSE1 allowed
2206 if (Subtarget->hasSSE1())
2207 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2208 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2209 0);
2210 return std::vector<unsigned>();
2211 case 'Y': // SSE_REGS if SSE2 allowed
2212 if (Subtarget->hasSSE2())
2213 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2214 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2215 0);
2216 return std::vector<unsigned>();
2217 }
2218 }
2219
Chris Lattner1efa40f2006-02-22 00:56:39 +00002220 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002221}
Evan Cheng30b37b52006-03-13 23:18:16 +00002222
2223/// isLegalAddressImmediate - Return true if the integer value or
2224/// GlobalValue can be used as the offset of the target addressing mode.
2225bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
2226 // X86 allows a sign-extended 32-bit immediate field.
2227 return (V > -(1LL << 32) && V < (1LL << 32)-1);
2228}
2229
2230bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
2231 if (getTargetMachine().
2232 getSubtarget<X86Subtarget>().isTargetDarwin()) {
2233 Reloc::Model RModel = getTargetMachine().getRelocationModel();
2234 if (RModel == Reloc::Static)
2235 return true;
2236 else if (RModel == Reloc::DynamicNoPIC)
Evan Cheng2221de92006-03-16 22:02:48 +00002237 return !DarwinGVRequiresExtraLoad(GV);
Evan Cheng30b37b52006-03-13 23:18:16 +00002238 else
2239 return false;
2240 } else
2241 return true;
2242}