blob: 63011ba53ff11226da8b7433d2898449f70b0b77 [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"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000022#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000023#include "llvm/CodeGen/MachineFunction.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/CodeGen/SelectionDAG.h"
26#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000027#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000028#include "llvm/Target/TargetOptions.h"
Chris Lattner259e97c2006-01-31 19:43:35 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030using namespace llvm;
31
32// FIXME: temporary.
33#include "llvm/Support/CommandLine.h"
34static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
35 cl::desc("Enable fastcc on X86"));
36
37X86TargetLowering::X86TargetLowering(TargetMachine &TM)
38 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000039 Subtarget = &TM.getSubtarget<X86Subtarget>();
40 X86ScalarSSE = Subtarget->hasSSE2();
41
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042 // Set up the TargetLowering object.
43
44 // X86 is weird, it always uses i8 for shift amounts and setcc results.
45 setShiftAmountType(MVT::i8);
46 setSetCCResultType(MVT::i8);
47 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000048 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000049 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner9edba762006-01-13 18:00:54 +000050 setStackPointerRegisterToSaveRestore(X86::ESP);
Chris Lattnera54aa942006-01-29 06:26:08 +000051
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 // Set up the register classes.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000053 addRegisterClass(MVT::i8, X86::R8RegisterClass);
54 addRegisterClass(MVT::i16, X86::R16RegisterClass);
55 addRegisterClass(MVT::i32, X86::R32RegisterClass);
56
57 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
58 // operation.
59 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
60 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
61 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000062
63 if (X86ScalarSSE)
64 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
65 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
66 else
67 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068
69 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
70 // this operation.
71 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
72 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000073 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000074 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +000075 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +000076 else {
77 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
78 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
79 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080
Evan Cheng6dab0532006-01-30 08:02:57 +000081 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
82 // isn't legal.
83 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
84 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
85
Evan Cheng02568ff2006-01-30 22:13:22 +000086 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
87 // this operation.
88 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
89 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
90
91 if (X86ScalarSSE) {
92 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
93 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +000095 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000096 }
97
98 // Handle FP_TO_UINT by promoting the destination to a larger signed
99 // conversion.
100 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
101 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
102 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
103
Evan Cheng45af8fd2006-02-18 07:26:17 +0000104 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng02568ff2006-01-30 22:13:22 +0000105 // Expand FP_TO_UINT into a select.
106 // FIXME: We would like to use a Custom expander here eventually to do
107 // the optimal thing for SSE vs. the default expansion in the legalizer.
108 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
109 else
Evan Cheng45af8fd2006-02-18 07:26:17 +0000110 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000111 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
112
Evan Cheng02568ff2006-01-30 22:13:22 +0000113 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
114 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner21f66852005-12-23 05:15:23 +0000115
Evan Cheng5298bcc2006-02-17 07:01:52 +0000116 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000117 setOperationAction(ISD::BRCONDTWOWAY , MVT::Other, Expand);
118 setOperationAction(ISD::BRTWOWAY_CC , MVT::Other, Expand);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000119 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
120 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000121 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
122 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000123 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000124 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
125 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
126 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
127 setOperationAction(ISD::FREM , MVT::f64 , Expand);
128 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
129 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
130 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
131 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
132 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
133 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
134 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
135 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
136 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000137 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000138 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000139
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000140 // These should be promoted to a larger select which is supported.
141 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
142 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000143
144 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000145 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
146 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
147 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
148 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
149 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
150 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
151 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
152 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
153 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000154 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000155 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000156 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000157 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000158 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000159 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000160 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000161 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
162 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
163 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000164 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000165 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
166 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167
Chris Lattnerf73bae12005-11-29 06:16:21 +0000168 // We don't have line number support yet.
169 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000170 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
171 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000172
Nate Begemanacc398c2006-01-25 18:21:52 +0000173 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
174 setOperationAction(ISD::VASTART , MVT::Other, Custom);
175
176 // Use the default implementation.
177 setOperationAction(ISD::VAARG , MVT::Other, Expand);
178 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
179 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000180 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
181 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
182 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000183
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000184 if (X86ScalarSSE) {
185 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000186 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
187 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000188
189 // SSE has no load+extend ops
190 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
191 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
192
Evan Cheng223547a2006-01-31 22:28:30 +0000193 // Use ANDPD to simulate FABS.
194 setOperationAction(ISD::FABS , MVT::f64, Custom);
195 setOperationAction(ISD::FABS , MVT::f32, Custom);
196
197 // Use XORP to simulate FNEG.
198 setOperationAction(ISD::FNEG , MVT::f64, Custom);
199 setOperationAction(ISD::FNEG , MVT::f32, Custom);
200
Evan Chengd25e9e82006-02-02 00:28:23 +0000201 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000202 setOperationAction(ISD::FSIN , MVT::f64, Expand);
203 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000204 setOperationAction(ISD::FREM , MVT::f64, Expand);
205 setOperationAction(ISD::FSIN , MVT::f32, Expand);
206 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000207 setOperationAction(ISD::FREM , MVT::f32, Expand);
208
Chris Lattnera54aa942006-01-29 06:26:08 +0000209 // Expand FP immediates into loads from the stack, except for the special
210 // cases we handle.
211 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
212 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000213 addLegalFPImmediate(+0.0); // xorps / xorpd
214 } else {
215 // Set up the FP register classes.
216 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner44d9b9b2006-01-29 06:44:22 +0000217
218 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
219
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 if (!UnsafeFPMath) {
221 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
222 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
223 }
224
Chris Lattnera54aa942006-01-29 06:26:08 +0000225 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 addLegalFPImmediate(+0.0); // FLD0
227 addLegalFPImmediate(+1.0); // FLD1
228 addLegalFPImmediate(-0.0); // FLD0/FCHS
229 addLegalFPImmediate(-1.0); // FLD1/FCHS
230 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000231
Evan Chengd30bf012006-03-01 01:11:20 +0000232 // First set operation action for all vector types to expand. Then we
233 // will selectively turn on ones that can be effectively codegen'd.
234 for (unsigned VT = (unsigned)MVT::Vector + 1;
235 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
236 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
237 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
238 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
239 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
240 }
241
Evan Cheng470a6ad2006-02-22 02:26:30 +0000242 if (TM.getSubtarget<X86Subtarget>().hasMMX()) {
243 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
244 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
245 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
246
Evan Chengd30bf012006-03-01 01:11:20 +0000247 // FIXME: add MMX packed arithmetics
Evan Cheng470a6ad2006-02-22 02:26:30 +0000248 setOperationAction(ISD::ConstantVec, MVT::v8i8, Expand);
249 setOperationAction(ISD::ConstantVec, MVT::v4i16, Expand);
250 setOperationAction(ISD::ConstantVec, MVT::v2i32, Expand);
251 }
252
253 if (TM.getSubtarget<X86Subtarget>().hasSSE1()) {
254 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
255
Evan Chengd30bf012006-03-01 01:11:20 +0000256 setOperationAction(ISD::ADD , MVT::v4f32, Legal);
257 setOperationAction(ISD::SUB , MVT::v4f32, Legal);
258 setOperationAction(ISD::MUL , MVT::v4f32, Legal);
259 setOperationAction(ISD::LOAD , MVT::v4f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000260 setOperationAction(ISD::ConstantVec, MVT::v4f32, Expand);
261 }
262
263 if (TM.getSubtarget<X86Subtarget>().hasSSE2()) {
264 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
265 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
266 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
267 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
268 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
269
270
Evan Chengd30bf012006-03-01 01:11:20 +0000271 setOperationAction(ISD::ADD , MVT::v2f64, Legal);
272 setOperationAction(ISD::SUB , MVT::v2f64, Legal);
273 setOperationAction(ISD::MUL , MVT::v2f64, Legal);
274 setOperationAction(ISD::LOAD , MVT::v2f64, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000275 setOperationAction(ISD::ConstantVec, MVT::v2f64, Expand);
276 setOperationAction(ISD::ConstantVec, MVT::v16i8, Expand);
277 setOperationAction(ISD::ConstantVec, MVT::v8i16, Expand);
278 setOperationAction(ISD::ConstantVec, MVT::v4i32, Expand);
279 setOperationAction(ISD::ConstantVec, MVT::v2i64, Expand);
280 }
281
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000282 computeRegisterProperties();
283
Evan Cheng87ed7162006-02-14 08:25:08 +0000284 // FIXME: These should be based on subtarget info. Plus, the values should
285 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000286 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
287 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
288 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000289 allowUnalignedMemoryAccesses = true; // x86 supports it!
290}
291
292std::vector<SDOperand>
293X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
294 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
295 return LowerFastCCArguments(F, DAG);
296 return LowerCCCArguments(F, DAG);
297}
298
299std::pair<SDOperand, SDOperand>
300X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
301 bool isVarArg, unsigned CallingConv,
302 bool isTailCall,
303 SDOperand Callee, ArgListTy &Args,
304 SelectionDAG &DAG) {
305 assert((!isVarArg || CallingConv == CallingConv::C) &&
306 "Only C takes varargs!");
Evan Chengd9558e02006-01-06 00:43:03 +0000307
308 // If the callee is a GlobalAddress node (quite common, every direct call is)
309 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
310 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
311 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Cheng8700e142006-01-11 06:09:51 +0000312 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
313 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Chengd9558e02006-01-06 00:43:03 +0000314
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000315 if (CallingConv == CallingConv::Fast && EnableFastCC)
316 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
317 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
318}
319
320//===----------------------------------------------------------------------===//
321// C Calling Convention implementation
322//===----------------------------------------------------------------------===//
323
324std::vector<SDOperand>
325X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
326 std::vector<SDOperand> ArgValues;
327
328 MachineFunction &MF = DAG.getMachineFunction();
329 MachineFrameInfo *MFI = MF.getFrameInfo();
330
331 // Add DAG nodes to load the arguments... On entry to a function on the X86,
332 // the stack frame looks like this:
333 //
334 // [ESP] -- return address
335 // [ESP + 4] -- first argument (leftmost lexically)
336 // [ESP + 8] -- second argument, if first argument is four bytes in size
337 // ...
338 //
339 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
340 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
341 MVT::ValueType ObjectVT = getValueType(I->getType());
342 unsigned ArgIncrement = 4;
343 unsigned ObjSize;
344 switch (ObjectVT) {
345 default: assert(0 && "Unhandled argument type!");
346 case MVT::i1:
347 case MVT::i8: ObjSize = 1; break;
348 case MVT::i16: ObjSize = 2; break;
349 case MVT::i32: ObjSize = 4; break;
350 case MVT::i64: ObjSize = ArgIncrement = 8; break;
351 case MVT::f32: ObjSize = 4; break;
352 case MVT::f64: ObjSize = ArgIncrement = 8; break;
353 }
354 // Create the frame index object for this incoming parameter...
355 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
356
357 // Create the SelectionDAG nodes corresponding to a load from this parameter
358 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
359
360 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
361 // dead loads.
362 SDOperand ArgValue;
363 if (!I->use_empty())
364 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
365 DAG.getSrcValue(NULL));
366 else {
367 if (MVT::isInteger(ObjectVT))
368 ArgValue = DAG.getConstant(0, ObjectVT);
369 else
370 ArgValue = DAG.getConstantFP(0, ObjectVT);
371 }
372 ArgValues.push_back(ArgValue);
373
374 ArgOffset += ArgIncrement; // Move on to the next argument...
375 }
376
377 // If the function takes variable number of arguments, make a frame index for
378 // the start of the first vararg value... for expansion of llvm.va_start.
379 if (F.isVarArg())
380 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
381 ReturnAddrIndex = 0; // No return address slot generated yet.
382 BytesToPopOnReturn = 0; // Callee pops nothing.
383 BytesCallerReserves = ArgOffset;
384
385 // Finally, inform the code generator which regs we return values in.
386 switch (getValueType(F.getReturnType())) {
387 default: assert(0 && "Unknown type!");
388 case MVT::isVoid: break;
389 case MVT::i1:
390 case MVT::i8:
391 case MVT::i16:
392 case MVT::i32:
393 MF.addLiveOut(X86::EAX);
394 break;
395 case MVT::i64:
396 MF.addLiveOut(X86::EAX);
397 MF.addLiveOut(X86::EDX);
398 break;
399 case MVT::f32:
400 case MVT::f64:
401 MF.addLiveOut(X86::ST0);
402 break;
403 }
404 return ArgValues;
405}
406
407std::pair<SDOperand, SDOperand>
408X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
409 bool isVarArg, bool isTailCall,
410 SDOperand Callee, ArgListTy &Args,
411 SelectionDAG &DAG) {
412 // Count how many bytes are to be pushed on the stack.
413 unsigned NumBytes = 0;
414
415 if (Args.empty()) {
416 // Save zero bytes.
Chris Lattner94dd2922006-02-13 09:00:43 +0000417 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000418 } else {
419 for (unsigned i = 0, e = Args.size(); i != e; ++i)
420 switch (getValueType(Args[i].second)) {
421 default: assert(0 && "Unknown value type!");
422 case MVT::i1:
423 case MVT::i8:
424 case MVT::i16:
425 case MVT::i32:
426 case MVT::f32:
427 NumBytes += 4;
428 break;
429 case MVT::i64:
430 case MVT::f64:
431 NumBytes += 8;
432 break;
433 }
434
Chris Lattner94dd2922006-02-13 09:00:43 +0000435 Chain = DAG.getCALLSEQ_START(Chain,
436 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437
438 // Arguments go on the stack in reverse order, as specified by the ABI.
439 unsigned ArgOffset = 0;
Evan Cheng8700e142006-01-11 06:09:51 +0000440 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000441 std::vector<SDOperand> Stores;
442
443 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
444 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
445 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
446
447 switch (getValueType(Args[i].second)) {
448 default: assert(0 && "Unexpected ValueType for argument!");
449 case MVT::i1:
450 case MVT::i8:
451 case MVT::i16:
452 // Promote the integer to 32 bits. If the input type is signed use a
453 // sign extend, otherwise use a zero extend.
454 if (Args[i].second->isSigned())
455 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
456 else
457 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
458
459 // FALL THROUGH
460 case MVT::i32:
461 case MVT::f32:
462 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
463 Args[i].first, PtrOff,
464 DAG.getSrcValue(NULL)));
465 ArgOffset += 4;
466 break;
467 case MVT::i64:
468 case MVT::f64:
469 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
470 Args[i].first, PtrOff,
471 DAG.getSrcValue(NULL)));
472 ArgOffset += 8;
473 break;
474 }
475 }
476 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
477 }
478
479 std::vector<MVT::ValueType> RetVals;
480 MVT::ValueType RetTyVT = getValueType(RetTy);
481 RetVals.push_back(MVT::Other);
482
483 // The result values produced have to be legal. Promote the result.
484 switch (RetTyVT) {
485 case MVT::isVoid: break;
486 default:
487 RetVals.push_back(RetTyVT);
488 break;
489 case MVT::i1:
490 case MVT::i8:
491 case MVT::i16:
492 RetVals.push_back(MVT::i32);
493 break;
494 case MVT::f32:
495 if (X86ScalarSSE)
496 RetVals.push_back(MVT::f32);
497 else
498 RetVals.push_back(MVT::f64);
499 break;
500 case MVT::i64:
501 RetVals.push_back(MVT::i32);
502 RetVals.push_back(MVT::i32);
503 break;
504 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000505
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000506 std::vector<MVT::ValueType> NodeTys;
507 NodeTys.push_back(MVT::Other); // Returns a chain
508 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
509 std::vector<SDOperand> Ops;
510 Ops.push_back(Chain);
511 Ops.push_back(Callee);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000512
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000513 // FIXME: Do not generate X86ISD::TAILCALL for now.
514 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
515 SDOperand InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000516
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000517 NodeTys.clear();
518 NodeTys.push_back(MVT::Other); // Returns a chain
519 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
520 Ops.clear();
521 Ops.push_back(Chain);
522 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
523 Ops.push_back(DAG.getConstant(0, getPointerTy()));
524 Ops.push_back(InFlag);
525 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
526 InFlag = Chain.getValue(1);
527
528 SDOperand RetVal;
529 if (RetTyVT != MVT::isVoid) {
Evan Chengd90eb7f2006-01-05 00:27:02 +0000530 switch (RetTyVT) {
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000531 default: assert(0 && "Unknown value type to return!");
Evan Chengd90eb7f2006-01-05 00:27:02 +0000532 case MVT::i1:
533 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000534 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
535 Chain = RetVal.getValue(1);
536 if (RetTyVT == MVT::i1)
537 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
538 break;
Evan Chengd90eb7f2006-01-05 00:27:02 +0000539 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000540 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
541 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000542 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000543 case MVT::i32:
544 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
545 Chain = RetVal.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000546 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000547 case MVT::i64: {
548 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
549 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
550 Lo.getValue(2));
551 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
552 Chain = Hi.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000553 break;
554 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000555 case MVT::f32:
556 case MVT::f64: {
557 std::vector<MVT::ValueType> Tys;
558 Tys.push_back(MVT::f64);
559 Tys.push_back(MVT::Other);
560 Tys.push_back(MVT::Flag);
561 std::vector<SDOperand> Ops;
562 Ops.push_back(Chain);
563 Ops.push_back(InFlag);
564 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
565 Chain = RetVal.getValue(1);
566 InFlag = RetVal.getValue(2);
567 if (X86ScalarSSE) {
568 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
569 // shouldn't be necessary except that RFP cannot be live across
570 // multiple blocks. When stackifier is fixed, they can be uncoupled.
571 MachineFunction &MF = DAG.getMachineFunction();
572 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
573 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
574 Tys.clear();
575 Tys.push_back(MVT::Other);
576 Ops.clear();
577 Ops.push_back(Chain);
578 Ops.push_back(RetVal);
579 Ops.push_back(StackSlot);
580 Ops.push_back(DAG.getValueType(RetTyVT));
581 Ops.push_back(InFlag);
582 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
583 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
584 DAG.getSrcValue(NULL));
585 Chain = RetVal.getValue(1);
586 }
Evan Chengd90eb7f2006-01-05 00:27:02 +0000587
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000588 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
589 // FIXME: we would really like to remember that this FP_ROUND
590 // operation is okay to eliminate if we allow excess FP precision.
591 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
592 break;
593 }
594 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000595 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000596
597 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000598}
599
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000600//===----------------------------------------------------------------------===//
601// Fast Calling Convention implementation
602//===----------------------------------------------------------------------===//
603//
604// The X86 'fast' calling convention passes up to two integer arguments in
605// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
606// and requires that the callee pop its arguments off the stack (allowing proper
607// tail calls), and has the same return value conventions as C calling convs.
608//
609// This calling convention always arranges for the callee pop value to be 8n+4
610// bytes, which is needed for tail recursion elimination and stack alignment
611// reasons.
612//
613// Note that this can be enhanced in the future to pass fp vals in registers
614// (when we have a global fp allocator) and do other tricks.
615//
616
617/// AddLiveIn - This helper function adds the specified physical register to the
618/// MachineFunction as a live in value. It also creates a corresponding virtual
619/// register for it.
620static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
621 TargetRegisterClass *RC) {
622 assert(RC->contains(PReg) && "Not the correct regclass!");
623 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
624 MF.addLiveIn(PReg, VReg);
625 return VReg;
626}
627
628
629std::vector<SDOperand>
630X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
631 std::vector<SDOperand> ArgValues;
632
633 MachineFunction &MF = DAG.getMachineFunction();
634 MachineFrameInfo *MFI = MF.getFrameInfo();
635
636 // Add DAG nodes to load the arguments... On entry to a function the stack
637 // frame looks like this:
638 //
639 // [ESP] -- return address
640 // [ESP + 4] -- first nonreg argument (leftmost lexically)
641 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
642 // ...
643 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
644
645 // Keep track of the number of integer regs passed so far. This can be either
646 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
647 // used).
648 unsigned NumIntRegs = 0;
649
650 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
651 MVT::ValueType ObjectVT = getValueType(I->getType());
652 unsigned ArgIncrement = 4;
653 unsigned ObjSize = 0;
654 SDOperand ArgValue;
655
656 switch (ObjectVT) {
657 default: assert(0 && "Unhandled argument type!");
658 case MVT::i1:
659 case MVT::i8:
660 if (NumIntRegs < 2) {
661 if (!I->use_empty()) {
662 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
663 X86::R8RegisterClass);
664 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
665 DAG.setRoot(ArgValue.getValue(1));
Chris Lattnerf31d1932005-12-27 03:02:18 +0000666 if (ObjectVT == MVT::i1)
667 // FIXME: Should insert a assertzext here.
668 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000669 }
670 ++NumIntRegs;
671 break;
672 }
673
674 ObjSize = 1;
675 break;
676 case MVT::i16:
677 if (NumIntRegs < 2) {
678 if (!I->use_empty()) {
679 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
680 X86::R16RegisterClass);
681 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
682 DAG.setRoot(ArgValue.getValue(1));
683 }
684 ++NumIntRegs;
685 break;
686 }
687 ObjSize = 2;
688 break;
689 case MVT::i32:
690 if (NumIntRegs < 2) {
691 if (!I->use_empty()) {
692 unsigned VReg = AddLiveIn(MF,NumIntRegs ? X86::EDX : X86::EAX,
693 X86::R32RegisterClass);
694 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
695 DAG.setRoot(ArgValue.getValue(1));
696 }
697 ++NumIntRegs;
698 break;
699 }
700 ObjSize = 4;
701 break;
702 case MVT::i64:
703 if (NumIntRegs == 0) {
704 if (!I->use_empty()) {
705 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
706 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
707
708 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
709 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
710 DAG.setRoot(Hi.getValue(1));
711
712 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
713 }
714 NumIntRegs = 2;
715 break;
716 } else if (NumIntRegs == 1) {
717 if (!I->use_empty()) {
718 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
719 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
720 DAG.setRoot(Low.getValue(1));
721
722 // Load the high part from memory.
723 // Create the frame index object for this incoming parameter...
724 int FI = MFI->CreateFixedObject(4, ArgOffset);
725 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
726 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
727 DAG.getSrcValue(NULL));
728 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
729 }
730 ArgOffset += 4;
731 NumIntRegs = 2;
732 break;
733 }
734 ObjSize = ArgIncrement = 8;
735 break;
736 case MVT::f32: ObjSize = 4; break;
737 case MVT::f64: ObjSize = ArgIncrement = 8; break;
738 }
739
740 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
741 // dead loads.
742 if (ObjSize && !I->use_empty()) {
743 // Create the frame index object for this incoming parameter...
744 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
745
746 // Create the SelectionDAG nodes corresponding to a load from this
747 // parameter.
748 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
749
750 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
751 DAG.getSrcValue(NULL));
752 } else if (ArgValue.Val == 0) {
753 if (MVT::isInteger(ObjectVT))
754 ArgValue = DAG.getConstant(0, ObjectVT);
755 else
756 ArgValue = DAG.getConstantFP(0, ObjectVT);
757 }
758 ArgValues.push_back(ArgValue);
759
760 if (ObjSize)
761 ArgOffset += ArgIncrement; // Move on to the next argument.
762 }
763
764 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
765 // arguments and the arguments after the retaddr has been pushed are aligned.
766 if ((ArgOffset & 7) == 0)
767 ArgOffset += 4;
768
769 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
770 ReturnAddrIndex = 0; // No return address slot generated yet.
771 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
772 BytesCallerReserves = 0;
773
774 // Finally, inform the code generator which regs we return values in.
775 switch (getValueType(F.getReturnType())) {
776 default: assert(0 && "Unknown type!");
777 case MVT::isVoid: break;
778 case MVT::i1:
779 case MVT::i8:
780 case MVT::i16:
781 case MVT::i32:
782 MF.addLiveOut(X86::EAX);
783 break;
784 case MVT::i64:
785 MF.addLiveOut(X86::EAX);
786 MF.addLiveOut(X86::EDX);
787 break;
788 case MVT::f32:
789 case MVT::f64:
790 MF.addLiveOut(X86::ST0);
791 break;
792 }
793 return ArgValues;
794}
795
796std::pair<SDOperand, SDOperand>
797X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
798 bool isTailCall, SDOperand Callee,
799 ArgListTy &Args, SelectionDAG &DAG) {
800 // Count how many bytes are to be pushed on the stack.
801 unsigned NumBytes = 0;
802
803 // Keep track of the number of integer regs passed so far. This can be either
804 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
805 // used).
806 unsigned NumIntRegs = 0;
807
808 for (unsigned i = 0, e = Args.size(); i != e; ++i)
809 switch (getValueType(Args[i].second)) {
810 default: assert(0 && "Unknown value type!");
811 case MVT::i1:
812 case MVT::i8:
813 case MVT::i16:
814 case MVT::i32:
815 if (NumIntRegs < 2) {
816 ++NumIntRegs;
817 break;
818 }
819 // fall through
820 case MVT::f32:
821 NumBytes += 4;
822 break;
823 case MVT::i64:
824 if (NumIntRegs == 0) {
825 NumIntRegs = 2;
826 break;
827 } else if (NumIntRegs == 1) {
828 NumIntRegs = 2;
829 NumBytes += 4;
830 break;
831 }
832
833 // fall through
834 case MVT::f64:
835 NumBytes += 8;
836 break;
837 }
838
839 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
840 // arguments and the arguments after the retaddr has been pushed are aligned.
841 if ((NumBytes & 7) == 0)
842 NumBytes += 4;
843
Chris Lattner94dd2922006-02-13 09:00:43 +0000844 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000845
846 // Arguments go on the stack in reverse order, as specified by the ABI.
847 unsigned ArgOffset = 0;
Chris Lattner91cacc82006-01-24 06:14:44 +0000848 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000849 NumIntRegs = 0;
850 std::vector<SDOperand> Stores;
851 std::vector<SDOperand> RegValuesToPass;
852 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
853 switch (getValueType(Args[i].second)) {
854 default: assert(0 && "Unexpected ValueType for argument!");
855 case MVT::i1:
Chris Lattnerf31d1932005-12-27 03:02:18 +0000856 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
857 // Fall through.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000858 case MVT::i8:
859 case MVT::i16:
860 case MVT::i32:
861 if (NumIntRegs < 2) {
862 RegValuesToPass.push_back(Args[i].first);
863 ++NumIntRegs;
864 break;
865 }
866 // Fall through
867 case MVT::f32: {
868 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
869 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
870 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
871 Args[i].first, PtrOff,
872 DAG.getSrcValue(NULL)));
873 ArgOffset += 4;
874 break;
875 }
876 case MVT::i64:
877 if (NumIntRegs < 2) { // Can pass part of it in regs?
878 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
879 Args[i].first, DAG.getConstant(1, MVT::i32));
880 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
881 Args[i].first, DAG.getConstant(0, MVT::i32));
882 RegValuesToPass.push_back(Lo);
883 ++NumIntRegs;
884 if (NumIntRegs < 2) { // Pass both parts in regs?
885 RegValuesToPass.push_back(Hi);
886 ++NumIntRegs;
887 } else {
888 // Pass the high part in memory.
889 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
890 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
891 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
892 Hi, PtrOff, DAG.getSrcValue(NULL)));
893 ArgOffset += 4;
894 }
895 break;
896 }
897 // Fall through
898 case MVT::f64:
899 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
900 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
901 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
902 Args[i].first, PtrOff,
903 DAG.getSrcValue(NULL)));
904 ArgOffset += 8;
905 break;
906 }
907 }
908 if (!Stores.empty())
909 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
910
911 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
912 // arguments and the arguments after the retaddr has been pushed are aligned.
913 if ((ArgOffset & 7) == 0)
914 ArgOffset += 4;
915
916 std::vector<MVT::ValueType> RetVals;
917 MVT::ValueType RetTyVT = getValueType(RetTy);
918
919 RetVals.push_back(MVT::Other);
920
921 // The result values produced have to be legal. Promote the result.
922 switch (RetTyVT) {
923 case MVT::isVoid: break;
924 default:
925 RetVals.push_back(RetTyVT);
926 break;
927 case MVT::i1:
928 case MVT::i8:
929 case MVT::i16:
930 RetVals.push_back(MVT::i32);
931 break;
932 case MVT::f32:
933 if (X86ScalarSSE)
934 RetVals.push_back(MVT::f32);
935 else
936 RetVals.push_back(MVT::f64);
937 break;
938 case MVT::i64:
939 RetVals.push_back(MVT::i32);
940 RetVals.push_back(MVT::i32);
941 break;
942 }
943
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000944 // Build a sequence of copy-to-reg nodes chained together with token chain
945 // and flag operands which copy the outgoing args into registers.
946 SDOperand InFlag;
947 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
948 unsigned CCReg;
949 SDOperand RegToPass = RegValuesToPass[i];
950 switch (RegToPass.getValueType()) {
951 default: assert(0 && "Bad thing to pass in regs");
952 case MVT::i8:
953 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Chengd9558e02006-01-06 00:43:03 +0000954 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000955 case MVT::i16:
956 CCReg = (i == 0) ? X86::AX : X86::DX;
957 break;
958 case MVT::i32:
959 CCReg = (i == 0) ? X86::EAX : X86::EDX;
960 break;
961 }
962
963 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
964 InFlag = Chain.getValue(1);
965 }
966
967 std::vector<MVT::ValueType> NodeTys;
968 NodeTys.push_back(MVT::Other); // Returns a chain
969 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
970 std::vector<SDOperand> Ops;
971 Ops.push_back(Chain);
972 Ops.push_back(Callee);
973 if (InFlag.Val)
974 Ops.push_back(InFlag);
975
976 // FIXME: Do not generate X86ISD::TAILCALL for now.
977 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
978 InFlag = Chain.getValue(1);
979
980 NodeTys.clear();
981 NodeTys.push_back(MVT::Other); // Returns a chain
982 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
983 Ops.clear();
984 Ops.push_back(Chain);
985 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
986 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
987 Ops.push_back(InFlag);
988 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
989 InFlag = Chain.getValue(1);
990
991 SDOperand RetVal;
992 if (RetTyVT != MVT::isVoid) {
993 switch (RetTyVT) {
994 default: assert(0 && "Unknown value type to return!");
Evan Chengd9558e02006-01-06 00:43:03 +0000995 case MVT::i1:
996 case MVT::i8:
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000997 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
998 Chain = RetVal.getValue(1);
999 if (RetTyVT == MVT::i1)
1000 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1001 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001002 case MVT::i16:
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001003 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1004 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001005 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001006 case MVT::i32:
1007 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1008 Chain = RetVal.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001009 break;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001010 case MVT::i64: {
1011 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1012 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1013 Lo.getValue(2));
1014 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1015 Chain = Hi.getValue(1);
Evan Chengd9558e02006-01-06 00:43:03 +00001016 break;
1017 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001018 case MVT::f32:
1019 case MVT::f64: {
1020 std::vector<MVT::ValueType> Tys;
1021 Tys.push_back(MVT::f64);
1022 Tys.push_back(MVT::Other);
1023 Tys.push_back(MVT::Flag);
1024 std::vector<SDOperand> Ops;
1025 Ops.push_back(Chain);
1026 Ops.push_back(InFlag);
1027 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1028 Chain = RetVal.getValue(1);
1029 InFlag = RetVal.getValue(2);
1030 if (X86ScalarSSE) {
1031 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1032 // shouldn't be necessary except that RFP cannot be live across
1033 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1034 MachineFunction &MF = DAG.getMachineFunction();
1035 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1036 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1037 Tys.clear();
1038 Tys.push_back(MVT::Other);
1039 Ops.clear();
1040 Ops.push_back(Chain);
1041 Ops.push_back(RetVal);
1042 Ops.push_back(StackSlot);
1043 Ops.push_back(DAG.getValueType(RetTyVT));
1044 Ops.push_back(InFlag);
1045 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1046 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1047 DAG.getSrcValue(NULL));
1048 Chain = RetVal.getValue(1);
1049 }
Evan Chengd9558e02006-01-06 00:43:03 +00001050
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001051 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1052 // FIXME: we would really like to remember that this FP_ROUND
1053 // operation is okay to eliminate if we allow excess FP precision.
1054 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1055 break;
1056 }
1057 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001058 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001059
1060 return std::make_pair(RetVal, Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001061}
1062
1063SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1064 if (ReturnAddrIndex == 0) {
1065 // Set up a frame object for the return address.
1066 MachineFunction &MF = DAG.getMachineFunction();
1067 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1068 }
1069
1070 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1071}
1072
1073
1074
1075std::pair<SDOperand, SDOperand> X86TargetLowering::
1076LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1077 SelectionDAG &DAG) {
1078 SDOperand Result;
1079 if (Depth) // Depths > 0 not supported yet!
1080 Result = DAG.getConstant(0, getPointerTy());
1081 else {
1082 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1083 if (!isFrameAddress)
1084 // Just load the return address
1085 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1086 DAG.getSrcValue(NULL));
1087 else
1088 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1089 DAG.getConstant(4, MVT::i32));
1090 }
1091 return std::make_pair(Result, Chain);
1092}
1093
Evan Cheng4a460802006-01-11 00:33:36 +00001094/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1095/// which corresponds to the condition code.
1096static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1097 switch (X86CC) {
1098 default: assert(0 && "Unknown X86 conditional code!");
1099 case X86ISD::COND_A: return X86::JA;
1100 case X86ISD::COND_AE: return X86::JAE;
1101 case X86ISD::COND_B: return X86::JB;
1102 case X86ISD::COND_BE: return X86::JBE;
1103 case X86ISD::COND_E: return X86::JE;
1104 case X86ISD::COND_G: return X86::JG;
1105 case X86ISD::COND_GE: return X86::JGE;
1106 case X86ISD::COND_L: return X86::JL;
1107 case X86ISD::COND_LE: return X86::JLE;
1108 case X86ISD::COND_NE: return X86::JNE;
1109 case X86ISD::COND_NO: return X86::JNO;
1110 case X86ISD::COND_NP: return X86::JNP;
1111 case X86ISD::COND_NS: return X86::JNS;
1112 case X86ISD::COND_O: return X86::JO;
1113 case X86ISD::COND_P: return X86::JP;
1114 case X86ISD::COND_S: return X86::JS;
1115 }
1116}
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001117
Evan Cheng6dfa9992006-01-30 23:41:35 +00001118/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1119/// specific condition code. It returns a false if it cannot do a direct
1120/// translation. X86CC is the translated CondCode. Flip is set to true if the
1121/// the order of comparison operands should be flipped.
Chris Lattner259e97c2006-01-31 19:43:35 +00001122static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1123 bool &Flip) {
Evan Chengd9558e02006-01-06 00:43:03 +00001124 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Cheng6dfa9992006-01-30 23:41:35 +00001125 Flip = false;
1126 X86CC = X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001127 if (!isFP) {
1128 switch (SetCCOpcode) {
1129 default: break;
1130 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1131 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1132 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1133 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1134 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1135 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1136 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1137 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1138 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1139 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1140 }
1141 } else {
1142 // On a floating point condition, the flags are set as follows:
1143 // ZF PF CF op
1144 // 0 | 0 | 0 | X > Y
1145 // 0 | 0 | 1 | X < Y
1146 // 1 | 0 | 0 | X == Y
1147 // 1 | 1 | 1 | unordered
1148 switch (SetCCOpcode) {
1149 default: break;
1150 case ISD::SETUEQ:
1151 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001152 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001153 case ISD::SETOGT:
1154 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001155 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001156 case ISD::SETOGE:
1157 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001158 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001159 case ISD::SETULT:
1160 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001161 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001162 case ISD::SETULE:
1163 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1164 case ISD::SETONE:
1165 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1166 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1167 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1168 }
1169 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001170
1171 return X86CC != X86ISD::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001172}
1173
Evan Cheng4a460802006-01-11 00:33:36 +00001174/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1175/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001176/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001177static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001178 switch (X86CC) {
1179 default:
1180 return false;
1181 case X86ISD::COND_B:
1182 case X86ISD::COND_BE:
1183 case X86ISD::COND_E:
1184 case X86ISD::COND_P:
1185 case X86ISD::COND_A:
1186 case X86ISD::COND_AE:
1187 case X86ISD::COND_NE:
1188 case X86ISD::COND_NP:
1189 return true;
1190 }
1191}
1192
Evan Cheng4a460802006-01-11 00:33:36 +00001193MachineBasicBlock *
1194X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1195 MachineBasicBlock *BB) {
Evan Cheng0cc39452006-01-16 21:21:29 +00001196 switch (MI->getOpcode()) {
1197 default: assert(false && "Unexpected instr type to insert");
1198 case X86::CMOV_FR32:
1199 case X86::CMOV_FR64: {
Chris Lattner259e97c2006-01-31 19:43:35 +00001200 // To "insert" a SELECT_CC instruction, we actually have to insert the
1201 // diamond control-flow pattern. The incoming instruction knows the
1202 // destination vreg to set, the condition code register to branch on, the
1203 // true/false values to select between, and a branch opcode to use.
Evan Cheng0cc39452006-01-16 21:21:29 +00001204 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1205 ilist<MachineBasicBlock>::iterator It = BB;
1206 ++It;
1207
1208 // thisMBB:
1209 // ...
1210 // TrueVal = ...
1211 // cmpTY ccX, r1, r2
1212 // bCC copy1MBB
1213 // fallthrough --> copy0MBB
1214 MachineBasicBlock *thisMBB = BB;
1215 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1216 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1217 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1218 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1219 MachineFunction *F = BB->getParent();
1220 F->getBasicBlockList().insert(It, copy0MBB);
1221 F->getBasicBlockList().insert(It, sinkMBB);
1222 // Update machine-CFG edges
1223 BB->addSuccessor(copy0MBB);
1224 BB->addSuccessor(sinkMBB);
1225
1226 // copy0MBB:
1227 // %FalseValue = ...
1228 // # fallthrough to sinkMBB
1229 BB = copy0MBB;
1230
1231 // Update machine-CFG edges
1232 BB->addSuccessor(sinkMBB);
1233
1234 // sinkMBB:
1235 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1236 // ...
1237 BB = sinkMBB;
1238 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1239 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1240 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng4a460802006-01-11 00:33:36 +00001241
Evan Cheng0cc39452006-01-16 21:21:29 +00001242 delete MI; // The pseudo instruction is gone now.
1243 return BB;
1244 }
Evan Cheng4a460802006-01-11 00:33:36 +00001245
Evan Cheng0cc39452006-01-16 21:21:29 +00001246 case X86::FP_TO_INT16_IN_MEM:
1247 case X86::FP_TO_INT32_IN_MEM:
1248 case X86::FP_TO_INT64_IN_MEM: {
1249 // Change the floating point control register to use "round towards zero"
1250 // mode when truncating to an integer value.
1251 MachineFunction *F = BB->getParent();
1252 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1253 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1254
1255 // Load the old value of the high byte of the control word...
1256 unsigned OldCW =
1257 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1258 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1259
1260 // Set the high part to be round to zero...
1261 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1262
1263 // Reload the modified control word now...
1264 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1265
1266 // Restore the memory image of control word to original value
1267 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1268
1269 // Get the X86 opcode to use.
1270 unsigned Opc;
1271 switch (MI->getOpcode()) {
Chris Lattner6b2469c2006-01-28 10:34:47 +00001272 default: assert(0 && "illegal opcode!");
Evan Cheng0cc39452006-01-16 21:21:29 +00001273 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1274 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1275 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1276 }
1277
1278 X86AddressMode AM;
1279 MachineOperand &Op = MI->getOperand(0);
1280 if (Op.isRegister()) {
1281 AM.BaseType = X86AddressMode::RegBase;
1282 AM.Base.Reg = Op.getReg();
1283 } else {
1284 AM.BaseType = X86AddressMode::FrameIndexBase;
1285 AM.Base.FrameIndex = Op.getFrameIndex();
1286 }
1287 Op = MI->getOperand(1);
1288 if (Op.isImmediate())
1289 AM.Scale = Op.getImmedValue();
1290 Op = MI->getOperand(2);
1291 if (Op.isImmediate())
1292 AM.IndexReg = Op.getImmedValue();
1293 Op = MI->getOperand(3);
1294 if (Op.isGlobalAddress()) {
1295 AM.GV = Op.getGlobal();
1296 } else {
1297 AM.Disp = Op.getImmedValue();
1298 }
1299 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1300
1301 // Reload the original control word now.
1302 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1303
1304 delete MI; // The pseudo instruction is gone now.
1305 return BB;
1306 }
1307 }
Evan Cheng4a460802006-01-11 00:33:36 +00001308}
1309
1310
1311//===----------------------------------------------------------------------===//
1312// X86 Custom Lowering Hooks
1313//===----------------------------------------------------------------------===//
1314
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001315/// LowerOperation - Provide custom lowering hooks for some operations.
1316///
1317SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1318 switch (Op.getOpcode()) {
1319 default: assert(0 && "Should not custom lower this!");
Evan Chenge3413162006-01-09 18:33:28 +00001320 case ISD::SHL_PARTS:
1321 case ISD::SRA_PARTS:
1322 case ISD::SRL_PARTS: {
1323 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1324 "Not an i64 shift!");
1325 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1326 SDOperand ShOpLo = Op.getOperand(0);
1327 SDOperand ShOpHi = Op.getOperand(1);
1328 SDOperand ShAmt = Op.getOperand(2);
1329 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng99fa0a12006-01-18 09:26:46 +00001330 DAG.getConstant(31, MVT::i8))
Evan Chenge3413162006-01-09 18:33:28 +00001331 : DAG.getConstant(0, MVT::i32);
1332
1333 SDOperand Tmp2, Tmp3;
1334 if (Op.getOpcode() == ISD::SHL_PARTS) {
1335 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1336 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1337 } else {
1338 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00001339 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00001340 }
1341
1342 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1343 ShAmt, DAG.getConstant(32, MVT::i8));
1344
1345 SDOperand Hi, Lo;
Evan Cheng82a24b92006-01-09 20:49:21 +00001346 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00001347
1348 std::vector<MVT::ValueType> Tys;
1349 Tys.push_back(MVT::i32);
1350 Tys.push_back(MVT::Flag);
1351 std::vector<SDOperand> Ops;
1352 if (Op.getOpcode() == ISD::SHL_PARTS) {
1353 Ops.push_back(Tmp2);
1354 Ops.push_back(Tmp3);
1355 Ops.push_back(CC);
1356 Ops.push_back(InFlag);
1357 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1358 InFlag = Hi.getValue(1);
1359
1360 Ops.clear();
1361 Ops.push_back(Tmp3);
1362 Ops.push_back(Tmp1);
1363 Ops.push_back(CC);
1364 Ops.push_back(InFlag);
1365 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1366 } else {
1367 Ops.push_back(Tmp2);
1368 Ops.push_back(Tmp3);
1369 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00001370 Ops.push_back(InFlag);
Evan Chenge3413162006-01-09 18:33:28 +00001371 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1372 InFlag = Lo.getValue(1);
1373
1374 Ops.clear();
1375 Ops.push_back(Tmp3);
1376 Ops.push_back(Tmp1);
1377 Ops.push_back(CC);
1378 Ops.push_back(InFlag);
1379 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1380 }
1381
1382 Tys.clear();
1383 Tys.push_back(MVT::i32);
1384 Tys.push_back(MVT::i32);
1385 Ops.clear();
1386 Ops.push_back(Lo);
1387 Ops.push_back(Hi);
1388 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1389 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001390 case ISD::SINT_TO_FP: {
Evan Cheng02568ff2006-01-30 22:13:22 +00001391 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Chenga3195e82006-01-12 22:54:21 +00001392 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001393 "Unknown SINT_TO_FP to lower!");
Evan Chenga3195e82006-01-12 22:54:21 +00001394
1395 SDOperand Result;
1396 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1397 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001398 MachineFunction &MF = DAG.getMachineFunction();
Evan Chenga3195e82006-01-12 22:54:21 +00001399 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001400 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Chenga3195e82006-01-12 22:54:21 +00001401 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1402 DAG.getEntryNode(), Op.getOperand(0),
1403 StackSlot, DAG.getSrcValue(NULL));
1404
1405 // Build the FILD
1406 std::vector<MVT::ValueType> Tys;
1407 Tys.push_back(MVT::f64);
Evan Cheng6dab0532006-01-30 08:02:57 +00001408 Tys.push_back(MVT::Other);
Evan Chenge3de85b2006-02-04 02:20:30 +00001409 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001410 std::vector<SDOperand> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00001411 Ops.push_back(Chain);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001412 Ops.push_back(StackSlot);
Evan Chenga3195e82006-01-12 22:54:21 +00001413 Ops.push_back(DAG.getValueType(SrcVT));
Evan Chenge3de85b2006-02-04 02:20:30 +00001414 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1415 Tys, Ops);
Evan Cheng6dab0532006-01-30 08:02:57 +00001416
1417 if (X86ScalarSSE) {
Evan Cheng6dab0532006-01-30 08:02:57 +00001418 Chain = Result.getValue(1);
1419 SDOperand InFlag = Result.getValue(2);
1420
Evan Chenge3de85b2006-02-04 02:20:30 +00001421 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng6dab0532006-01-30 08:02:57 +00001422 // shouldn't be necessary except that RFP cannot be live across
1423 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1424 MachineFunction &MF = DAG.getMachineFunction();
1425 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1426 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1427 std::vector<MVT::ValueType> Tys;
1428 Tys.push_back(MVT::Other);
1429 std::vector<SDOperand> Ops;
1430 Ops.push_back(Chain);
1431 Ops.push_back(Result);
1432 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001433 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001434 Ops.push_back(InFlag);
1435 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1436 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1437 DAG.getSrcValue(NULL));
1438 }
1439
Evan Chenga3195e82006-01-12 22:54:21 +00001440 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001441 }
1442 case ISD::FP_TO_SINT: {
1443 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001444 "Unknown FP_TO_SINT to lower!");
1445 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1446 // stack slot.
1447 MachineFunction &MF = DAG.getMachineFunction();
1448 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1449 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1450 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1451
1452 unsigned Opc;
1453 switch (Op.getValueType()) {
1454 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1455 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1456 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1457 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
1458 }
1459
Evan Cheng6dab0532006-01-30 08:02:57 +00001460 SDOperand Chain = DAG.getEntryNode();
1461 SDOperand Value = Op.getOperand(0);
1462 if (X86ScalarSSE) {
1463 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
1464 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
1465 DAG.getSrcValue(0));
1466 std::vector<MVT::ValueType> Tys;
1467 Tys.push_back(MVT::f64);
1468 Tys.push_back(MVT::Other);
1469 std::vector<SDOperand> Ops;
1470 Ops.push_back(Chain);
1471 Ops.push_back(StackSlot);
Evan Cheng02568ff2006-01-30 22:13:22 +00001472 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng6dab0532006-01-30 08:02:57 +00001473 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
1474 Chain = Value.getValue(1);
1475 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1476 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1477 }
1478
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001479 // Build the FP_TO_INT*_IN_MEM
1480 std::vector<SDOperand> Ops;
Evan Cheng6dab0532006-01-30 08:02:57 +00001481 Ops.push_back(Chain);
1482 Ops.push_back(Value);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001483 Ops.push_back(StackSlot);
1484 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
1485
1486 // Load the result.
1487 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
1488 DAG.getSrcValue(NULL));
1489 }
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001490 case ISD::READCYCLECOUNTER: {
Chris Lattner81363c32005-11-20 22:01:40 +00001491 std::vector<MVT::ValueType> Tys;
1492 Tys.push_back(MVT::Other);
1493 Tys.push_back(MVT::Flag);
1494 std::vector<SDOperand> Ops;
1495 Ops.push_back(Op.getOperand(0));
1496 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner81f803d2005-11-20 22:57:19 +00001497 Ops.clear();
1498 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
1499 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
1500 MVT::i32, Ops[0].getValue(2)));
1501 Ops.push_back(Ops[1].getValue(1));
1502 Tys[0] = Tys[1] = MVT::i32;
1503 Tys.push_back(MVT::Other);
1504 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharthb873ff32005-11-20 21:41:10 +00001505 }
Evan Chengef6ffb12006-01-31 03:14:29 +00001506 case ISD::FABS: {
1507 MVT::ValueType VT = Op.getValueType();
Evan Cheng223547a2006-01-31 22:28:30 +00001508 const Type *OpNTy = MVT::getTypeForValueType(VT);
1509 std::vector<Constant*> CV;
1510 if (VT == MVT::f64) {
1511 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
1512 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1513 } else {
1514 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
1515 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1516 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1517 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1518 }
1519 Constant *CS = ConstantStruct::get(CV);
1520 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1521 SDOperand Mask
1522 = DAG.getNode(X86ISD::LOAD_PACK,
1523 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Chengef6ffb12006-01-31 03:14:29 +00001524 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
1525 }
Evan Cheng223547a2006-01-31 22:28:30 +00001526 case ISD::FNEG: {
1527 MVT::ValueType VT = Op.getValueType();
1528 const Type *OpNTy = MVT::getTypeForValueType(VT);
1529 std::vector<Constant*> CV;
1530 if (VT == MVT::f64) {
1531 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
1532 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1533 } else {
1534 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
1535 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1536 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1537 CV.push_back(ConstantFP::get(OpNTy, 0.0));
1538 }
1539 Constant *CS = ConstantStruct::get(CV);
1540 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
1541 SDOperand Mask
1542 = DAG.getNode(X86ISD::LOAD_PACK,
1543 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
1544 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
1545 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001546 case ISD::SETCC: {
1547 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng6dfa9992006-01-30 23:41:35 +00001548 SDOperand Cond;
1549 SDOperand CC = Op.getOperand(2);
Evan Chengd9558e02006-01-06 00:43:03 +00001550 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
1551 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng6dfa9992006-01-30 23:41:35 +00001552 bool Flip;
1553 unsigned X86CC;
1554 if (translateX86CC(CC, isFP, X86CC, Flip)) {
1555 if (Flip)
1556 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1557 Op.getOperand(1), Op.getOperand(0));
1558 else
1559 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1560 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001561 return DAG.getNode(X86ISD::SETCC, MVT::i8,
1562 DAG.getConstant(X86CC, MVT::i8), Cond);
1563 } else {
1564 assert(isFP && "Illegal integer SetCC!");
1565
Evan Cheng6dfa9992006-01-30 23:41:35 +00001566 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1567 Op.getOperand(0), Op.getOperand(1));
Evan Chengd9558e02006-01-06 00:43:03 +00001568 std::vector<MVT::ValueType> Tys;
1569 std::vector<SDOperand> Ops;
1570 switch (SetCCOpcode) {
1571 default: assert(false && "Illegal floating point SetCC!");
1572 case ISD::SETOEQ: { // !PF & ZF
1573 Tys.push_back(MVT::i8);
1574 Tys.push_back(MVT::Flag);
1575 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
1576 Ops.push_back(Cond);
1577 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1578 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1579 DAG.getConstant(X86ISD::COND_E, MVT::i8),
1580 Tmp1.getValue(1));
1581 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
1582 }
Evan Chengd9558e02006-01-06 00:43:03 +00001583 case ISD::SETUNE: { // PF | !ZF
1584 Tys.push_back(MVT::i8);
1585 Tys.push_back(MVT::Flag);
1586 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
1587 Ops.push_back(Cond);
1588 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1589 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
1590 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
1591 Tmp1.getValue(1));
1592 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
1593 }
1594 }
1595 }
Evan Chengd5781fc2005-12-21 20:21:51 +00001596 }
Evan Cheng7df96d62005-12-17 01:21:05 +00001597 case ISD::SELECT: {
Evan Chengaaca22c2006-01-10 20:26:56 +00001598 MVT::ValueType VT = Op.getValueType();
1599 bool isFP = MVT::isFloatingPoint(VT);
Evan Cheng559806f2006-01-27 08:10:46 +00001600 bool isFPStack = isFP && !X86ScalarSSE;
1601 bool isFPSSE = isFP && X86ScalarSSE;
Evan Cheng1bcee362006-01-13 01:03:02 +00001602 bool addTest = false;
Evan Chengaaca22c2006-01-10 20:26:56 +00001603 SDOperand Op0 = Op.getOperand(0);
1604 SDOperand Cond, CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001605 if (Op0.getOpcode() == ISD::SETCC)
1606 Op0 = LowerOperation(Op0, DAG);
1607
Evan Chengaaca22c2006-01-10 20:26:56 +00001608 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001609 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1610 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1611 // have another use it will be eliminated.
1612 // If the X86ISD::SETCC has more than one use, then it's probably better
1613 // to use a test instead of duplicating the X86ISD::CMP (for register
1614 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001615 if (Op0.getOperand(1).getOpcode() == X86ISD::CMP) {
1616 if (!Op0.hasOneUse()) {
1617 std::vector<MVT::ValueType> Tys;
1618 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
1619 Tys.push_back(Op0.Val->getValueType(i));
1620 std::vector<SDOperand> Ops;
1621 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
1622 Ops.push_back(Op0.getOperand(i));
1623 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1624 }
1625
Evan Cheng1bcee362006-01-13 01:03:02 +00001626 CC = Op0.getOperand(0);
1627 Cond = Op0.getOperand(1);
Evan Cheng0d718e92006-01-25 09:05:09 +00001628 // Make a copy as flag result cannot be used by more than one.
1629 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
1630 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001631 addTest =
Evan Cheng80ebe382006-01-13 01:17:24 +00001632 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Cheng1bcee362006-01-13 01:03:02 +00001633 } else
1634 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001635 } else
1636 addTest = true;
Evan Chengaaca22c2006-01-10 20:26:56 +00001637
Evan Cheng189d01e2006-01-13 01:06:49 +00001638 if (addTest) {
Evan Chenge90da972006-01-13 19:51:46 +00001639 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Chengaaca22c2006-01-10 20:26:56 +00001640 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng7df96d62005-12-17 01:21:05 +00001641 }
Evan Chenge3413162006-01-09 18:33:28 +00001642
1643 std::vector<MVT::ValueType> Tys;
1644 Tys.push_back(Op.getValueType());
1645 Tys.push_back(MVT::Flag);
1646 std::vector<SDOperand> Ops;
Evan Chenge90da972006-01-13 19:51:46 +00001647 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
1648 // condition is true.
Evan Chenge3413162006-01-09 18:33:28 +00001649 Ops.push_back(Op.getOperand(2));
Evan Chenge90da972006-01-13 19:51:46 +00001650 Ops.push_back(Op.getOperand(1));
Evan Chenge3413162006-01-09 18:33:28 +00001651 Ops.push_back(CC);
1652 Ops.push_back(Cond);
1653 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng7df96d62005-12-17 01:21:05 +00001654 }
Evan Cheng898101c2005-12-19 23:12:38 +00001655 case ISD::BRCOND: {
Evan Cheng1bcee362006-01-13 01:03:02 +00001656 bool addTest = false;
Evan Cheng898101c2005-12-19 23:12:38 +00001657 SDOperand Cond = Op.getOperand(1);
1658 SDOperand Dest = Op.getOperand(2);
1659 SDOperand CC;
Evan Cheng6dfa9992006-01-30 23:41:35 +00001660 if (Cond.getOpcode() == ISD::SETCC)
1661 Cond = LowerOperation(Cond, DAG);
1662
Evan Chengd5781fc2005-12-21 20:21:51 +00001663 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng1bcee362006-01-13 01:03:02 +00001664 // If condition flag is set by a X86ISD::CMP, then make a copy of it
1665 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
1666 // have another use it will be eliminated.
1667 // If the X86ISD::SETCC has more than one use, then it's probably better
1668 // to use a test instead of duplicating the X86ISD::CMP (for register
1669 // pressure reason).
Evan Cheng9bba8942006-01-26 02:13:10 +00001670 if (Cond.getOperand(1).getOpcode() == X86ISD::CMP) {
1671 if (!Cond.hasOneUse()) {
1672 std::vector<MVT::ValueType> Tys;
1673 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
1674 Tys.push_back(Cond.Val->getValueType(i));
1675 std::vector<SDOperand> Ops;
1676 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
1677 Ops.push_back(Cond.getOperand(i));
1678 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
1679 }
1680
Evan Cheng1bcee362006-01-13 01:03:02 +00001681 CC = Cond.getOperand(0);
Evan Cheng0d718e92006-01-25 09:05:09 +00001682 Cond = Cond.getOperand(1);
1683 // Make a copy as flag result cannot be used by more than one.
Evan Cheng1bcee362006-01-13 01:03:02 +00001684 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
Evan Cheng0d718e92006-01-25 09:05:09 +00001685 Cond.getOperand(0), Cond.getOperand(1));
Evan Cheng1bcee362006-01-13 01:03:02 +00001686 } else
1687 addTest = true;
Evan Cheng1bcee362006-01-13 01:03:02 +00001688 } else
1689 addTest = true;
1690
1691 if (addTest) {
Evan Chengd9558e02006-01-06 00:43:03 +00001692 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng898101c2005-12-19 23:12:38 +00001693 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
1694 }
1695 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
1696 Op.getOperand(0), Op.getOperand(2), CC, Cond);
1697 }
Evan Cheng67f92a72006-01-11 22:15:48 +00001698 case ISD::MEMSET: {
1699 SDOperand InFlag;
1700 SDOperand Chain = Op.getOperand(0);
1701 unsigned Align =
1702 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1703 if (Align == 0) Align = 1;
1704
Evan Cheng18a84522006-02-16 00:21:07 +00001705 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1706 // If not DWORD aligned, call memset if size is less than the threshold.
1707 // It knows how to align to the right boundary first.
1708 if ((Align & 3) != 0 &&
1709 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1710 MVT::ValueType IntPtr = getPointerTy();
1711 const Type *IntPtrTy = getTargetData().getIntPtrType();
1712 std::vector<std::pair<SDOperand, const Type*> > Args;
1713 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1714 // Extend the ubyte argument to be an int value for the call.
1715 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
1716 Args.push_back(std::make_pair(Val, IntPtrTy));
1717 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1718 std::pair<SDOperand,SDOperand> CallResult =
1719 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1720 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
1721 return CallResult.second;
1722 }
1723
Evan Cheng67f92a72006-01-11 22:15:48 +00001724 MVT::ValueType AVT;
1725 SDOperand Count;
1726 if (ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2))) {
1727 unsigned ValReg;
1728 unsigned Val = ValC->getValue() & 255;
1729
1730 // If the value is a constant, then we can potentially use larger sets.
1731 switch (Align & 3) {
1732 case 2: // WORD aligned
1733 AVT = MVT::i16;
Evan Cheng18a84522006-02-16 00:21:07 +00001734 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001735 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1736 else
1737 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1738 DAG.getConstant(1, MVT::i8));
1739 Val = (Val << 8) | Val;
1740 ValReg = X86::AX;
1741 break;
1742 case 0: // DWORD aligned
1743 AVT = MVT::i32;
Evan Cheng18a84522006-02-16 00:21:07 +00001744 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001745 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1746 else
1747 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1748 DAG.getConstant(2, MVT::i8));
1749 Val = (Val << 8) | Val;
1750 Val = (Val << 16) | Val;
1751 ValReg = X86::EAX;
1752 break;
1753 default: // Byte aligned
1754 AVT = MVT::i8;
1755 Count = Op.getOperand(3);
1756 ValReg = X86::AL;
1757 break;
1758 }
1759
1760 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
1761 InFlag);
1762 InFlag = Chain.getValue(1);
1763 } else {
Evan Cheng18a84522006-02-16 00:21:07 +00001764 AVT = MVT::i8;
Evan Cheng67f92a72006-01-11 22:15:48 +00001765 Count = Op.getOperand(3);
1766 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
1767 InFlag = Chain.getValue(1);
1768 }
1769
1770 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1771 InFlag = Chain.getValue(1);
1772 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1773 InFlag = Chain.getValue(1);
1774
1775 return DAG.getNode(X86ISD::REP_STOS, MVT::Other, Chain,
1776 DAG.getValueType(AVT), InFlag);
1777 }
1778 case ISD::MEMCPY: {
1779 SDOperand Chain = Op.getOperand(0);
1780 unsigned Align =
1781 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
1782 if (Align == 0) Align = 1;
1783
Evan Cheng18a84522006-02-16 00:21:07 +00001784 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
1785 // If not DWORD aligned, call memcpy if size is less than the threshold.
1786 // It knows how to align to the right boundary first.
1787 if ((Align & 3) != 0 &&
1788 !(I && I->getValue() >= Subtarget->getMinRepStrSizeThreshold())) {
1789 MVT::ValueType IntPtr = getPointerTy();
1790 const Type *IntPtrTy = getTargetData().getIntPtrType();
1791 std::vector<std::pair<SDOperand, const Type*> > Args;
1792 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
1793 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
1794 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
1795 std::pair<SDOperand,SDOperand> CallResult =
1796 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
1797 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
1798 return CallResult.second;
1799 }
1800
Evan Cheng67f92a72006-01-11 22:15:48 +00001801 MVT::ValueType AVT;
1802 SDOperand Count;
1803 switch (Align & 3) {
1804 case 2: // WORD aligned
1805 AVT = MVT::i16;
Evan Cheng18a84522006-02-16 00:21:07 +00001806 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001807 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
1808 else
Evan Cheng18a84522006-02-16 00:21:07 +00001809 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
Evan Cheng67f92a72006-01-11 22:15:48 +00001810 break;
1811 case 0: // DWORD aligned
1812 AVT = MVT::i32;
Evan Cheng18a84522006-02-16 00:21:07 +00001813 if (I)
Evan Cheng67f92a72006-01-11 22:15:48 +00001814 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
1815 else
1816 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
1817 DAG.getConstant(2, MVT::i8));
1818 break;
1819 default: // Byte aligned
1820 AVT = MVT::i8;
1821 Count = Op.getOperand(3);
1822 break;
1823 }
1824
1825 SDOperand InFlag;
1826 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
1827 InFlag = Chain.getValue(1);
1828 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
1829 InFlag = Chain.getValue(1);
1830 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
1831 InFlag = Chain.getValue(1);
1832
1833 return DAG.getNode(X86ISD::REP_MOVS, MVT::Other, Chain,
1834 DAG.getValueType(AVT), InFlag);
1835 }
Evan Chengbbbb2fb2006-02-25 09:55:19 +00001836
1837 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
1838 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
1839 // one of the above mentioned nodes. It has to be wrapped because otherwise
1840 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
1841 // be used to form addressing mode. These wrapped nodes will be selected
1842 // into MOV32ri.
Evan Cheng7ccced62006-02-18 00:15:05 +00001843 case ISD::ConstantPool: {
1844 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Cheng020d2e82006-02-23 20:41:18 +00001845 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1846 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
1847 CP->getAlignment()));
Evan Chenga0ea0532006-02-23 02:43:52 +00001848 if (getTargetMachine().getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00001849 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001850 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng7ccced62006-02-18 00:15:05 +00001851 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1852 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1853 }
1854
1855 return Result;
1856 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001857 case ISD::GlobalAddress: {
Evan Cheng020d2e82006-02-23 20:41:18 +00001858 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
1859 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1860 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengb077b842005-12-21 02:39:21 +00001861 if (getTargetMachine().
Evan Cheng7ccced62006-02-18 00:15:05 +00001862 getSubtarget<X86Subtarget>().isTargetDarwin()) {
Evan Cheng7ccced62006-02-18 00:15:05 +00001863 // With PIC, the address is actually $g + Offset.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001864 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Chenga0ea0532006-02-23 02:43:52 +00001865 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1866 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng7ccced62006-02-18 00:15:05 +00001867
1868 // For Darwin, external and weak symbols are indirect, so we want to load
1869 // the value at address GV, not the value of GV itself. This means that
1870 // the GlobalAddress must be in the base or index register of the address,
1871 // not the GV offset field.
Evan Cheng4c1aa862006-02-22 20:19:42 +00001872 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
1873 (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1874 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode())))
Evan Cheng2338c5c2006-02-07 08:38:37 +00001875 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Chenga0ea0532006-02-23 02:43:52 +00001876 Result, DAG.getSrcValue(NULL));
Evan Cheng2338c5c2006-02-07 08:38:37 +00001877 }
Evan Cheng7ccced62006-02-18 00:15:05 +00001878
Evan Cheng002fe9b2006-01-12 07:56:47 +00001879 return Result;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001880 }
Evan Cheng020d2e82006-02-23 20:41:18 +00001881 case ISD::ExternalSymbol: {
1882 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
1883 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
1884 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
1885 if (getTargetMachine().
1886 getSubtarget<X86Subtarget>().isTargetDarwin()) {
1887 // With PIC, the address is actually $g + Offset.
1888 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
1889 Result = DAG.getNode(ISD::ADD, getPointerTy(),
1890 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
1891 }
1892
1893 return Result;
1894 }
Nate Begemanacc398c2006-01-25 18:21:52 +00001895 case ISD::VASTART: {
1896 // vastart just stores the address of the VarArgsFrameIndex slot into the
1897 // memory location argument.
1898 // FIXME: Replace MVT::i32 with PointerTy
1899 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
1900 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
1901 Op.getOperand(1), Op.getOperand(2));
1902 }
Nate Begemanee625572006-01-27 21:09:22 +00001903 case ISD::RET: {
1904 SDOperand Copy;
1905
1906 switch(Op.getNumOperands()) {
1907 default:
1908 assert(0 && "Do not know how to return this many arguments!");
1909 abort();
1910 case 1:
1911 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
1912 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
1913 case 2: {
1914 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
1915 if (MVT::isInteger(ArgVT))
1916 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
1917 SDOperand());
1918 else if (!X86ScalarSSE) {
1919 std::vector<MVT::ValueType> Tys;
1920 Tys.push_back(MVT::Other);
1921 Tys.push_back(MVT::Flag);
1922 std::vector<SDOperand> Ops;
1923 Ops.push_back(Op.getOperand(0));
1924 Ops.push_back(Op.getOperand(1));
1925 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1926 } else {
Evan Cheng0d084c92006-02-01 00:20:21 +00001927 SDOperand MemLoc;
1928 SDOperand Chain = Op.getOperand(0);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001929 SDOperand Value = Op.getOperand(1);
1930
Evan Cheng760df292006-02-01 01:19:32 +00001931 if (Value.getOpcode() == ISD::LOAD &&
1932 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng0e8671b2006-01-31 23:19:54 +00001933 Chain = Value.getOperand(0);
1934 MemLoc = Value.getOperand(1);
1935 } else {
1936 // Spill the value to memory and reload it into top of stack.
1937 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
1938 MachineFunction &MF = DAG.getMachineFunction();
1939 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
1940 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
1941 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
1942 Value, MemLoc, DAG.getSrcValue(0));
1943 }
Nate Begemanee625572006-01-27 21:09:22 +00001944 std::vector<MVT::ValueType> Tys;
1945 Tys.push_back(MVT::f64);
1946 Tys.push_back(MVT::Other);
1947 std::vector<SDOperand> Ops;
1948 Ops.push_back(Chain);
Evan Cheng0e8671b2006-01-31 23:19:54 +00001949 Ops.push_back(MemLoc);
Nate Begemanee625572006-01-27 21:09:22 +00001950 Ops.push_back(DAG.getValueType(ArgVT));
1951 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
1952 Tys.clear();
1953 Tys.push_back(MVT::Other);
1954 Tys.push_back(MVT::Flag);
1955 Ops.clear();
1956 Ops.push_back(Copy.getValue(1));
1957 Ops.push_back(Copy);
1958 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
1959 }
1960 break;
1961 }
1962 case 3:
1963 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
1964 SDOperand());
1965 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
1966 break;
1967 }
1968 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
1969 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
1970 Copy.getValue(1));
1971 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00001972 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001973}
Evan Cheng72261582005-12-20 06:22:03 +00001974
1975const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
1976 switch (Opcode) {
1977 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00001978 case X86ISD::SHLD: return "X86ISD::SHLD";
1979 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00001980 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng223547a2006-01-31 22:28:30 +00001981 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Chenga3195e82006-01-12 22:54:21 +00001982 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00001983 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00001984 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
1985 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
1986 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00001987 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00001988 case X86ISD::FST: return "X86ISD::FST";
1989 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00001990 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00001991 case X86ISD::CALL: return "X86ISD::CALL";
1992 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
1993 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
1994 case X86ISD::CMP: return "X86ISD::CMP";
1995 case X86ISD::TEST: return "X86ISD::TEST";
Evan Chengd5781fc2005-12-21 20:21:51 +00001996 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00001997 case X86ISD::CMOV: return "X86ISD::CMOV";
1998 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00001999 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00002000 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2001 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00002002 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng7ccced62006-02-18 00:15:05 +00002003 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00002004 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Cheng72261582005-12-20 06:22:03 +00002005 }
2006}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002007
Nate Begeman368e18d2006-02-16 21:11:51 +00002008void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2009 uint64_t Mask,
2010 uint64_t &KnownZero,
2011 uint64_t &KnownOne,
2012 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002013
2014 unsigned Opc = Op.getOpcode();
Nate Begeman368e18d2006-02-16 21:11:51 +00002015 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002016
2017 switch (Opc) {
2018 default:
2019 assert(Opc >= ISD::BUILTIN_OP_END && "Expected a target specific node");
2020 break;
Nate Begeman368e18d2006-02-16 21:11:51 +00002021 case X86ISD::SETCC:
2022 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
2023 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002024 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00002025}
Chris Lattner259e97c2006-01-31 19:43:35 +00002026
2027std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00002028getRegClassForInlineAsmConstraint(const std::string &Constraint,
2029 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00002030 if (Constraint.size() == 1) {
2031 // FIXME: not handling fp-stack yet!
2032 // FIXME: not handling MMX registers yet ('y' constraint).
2033 switch (Constraint[0]) { // GCC X86 Constraint Letters
2034 default: break; // Unknown constriant letter
2035 case 'r': // GENERAL_REGS
2036 case 'R': // LEGACY_REGS
2037 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2038 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
2039 case 'l': // INDEX_REGS
2040 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
2041 X86::ESI, X86::EDI, X86::EBP, 0);
2042 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
2043 case 'Q': // Q_REGS
2044 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
2045 case 'x': // SSE_REGS if SSE1 allowed
2046 if (Subtarget->hasSSE1())
2047 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2048 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2049 0);
2050 return std::vector<unsigned>();
2051 case 'Y': // SSE_REGS if SSE2 allowed
2052 if (Subtarget->hasSSE2())
2053 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
2054 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
2055 0);
2056 return std::vector<unsigned>();
2057 }
2058 }
2059
Chris Lattner1efa40f2006-02-22 00:56:39 +00002060 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00002061}