blob: c13d498d5e67adf0ba39f22c95d37e5586e29611 [file] [log] [blame]
Chris Lattner76ac0682005-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 Cheng911c68d2006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng72d5c252006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000021#include "llvm/Function.h"
Evan Cheng78038292006-04-05 23:38:46 +000022#include "llvm/Intrinsics.h"
Evan Chengaf598d22006-03-13 23:18:16 +000023#include "llvm/ADT/VectorExtras.h"
24#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000025#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng339edad2006-01-11 00:33:36 +000026#include "llvm/CodeGen/MachineFunction.h"
27#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000028#include "llvm/CodeGen/SelectionDAG.h"
29#include "llvm/CodeGen/SSARegMap.h"
Evan Cheng2dd217b2006-01-31 03:14:29 +000030#include "llvm/Support/MathExtras.h"
Chris Lattner76ac0682005-11-15 00:40:23 +000031#include "llvm/Target/TargetOptions.h"
32using namespace llvm;
33
34// FIXME: temporary.
35#include "llvm/Support/CommandLine.h"
36static cl::opt<bool> EnableFastCC("enable-x86-fastcc", cl::Hidden,
37 cl::desc("Enable fastcc on X86"));
38
39X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
Evan Chengcde9e302006-01-27 08:10:46 +000041 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
43
Chris Lattner76ac0682005-11-15 00:40:23 +000044 // Set up the TargetLowering object.
45
46 // X86 is weird, it always uses i8 for shift amounts and setcc results.
47 setShiftAmountType(MVT::i8);
48 setSetCCResultType(MVT::i8);
49 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng83eeefb2006-01-25 09:15:17 +000050 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattner76ac0682005-11-15 00:40:23 +000051 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Chris Lattner1a8d9182006-01-13 18:00:54 +000052 setStackPointerRegisterToSaveRestore(X86::ESP);
Evan Cheng20931a72006-03-16 21:47:42 +000053
Evan Chengbc047222006-03-22 19:22:18 +000054 if (!Subtarget->isTargetDarwin())
Evan Chengb09a56f2006-03-17 20:31:41 +000055 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
56 setUseUnderscoreSetJmpLongJmp(true);
57
Evan Cheng20931a72006-03-16 21:47:42 +000058 // Add legal addressing mode scale values.
59 addLegalAddressScale(8);
60 addLegalAddressScale(4);
61 addLegalAddressScale(2);
62 // Enter the ones which require both scale + index last. These are more
63 // expensive.
64 addLegalAddressScale(9);
65 addLegalAddressScale(5);
66 addLegalAddressScale(3);
Chris Lattner61c9a8e2006-01-29 06:26:08 +000067
Chris Lattner76ac0682005-11-15 00:40:23 +000068 // Set up the register classes.
Chris Lattner76ac0682005-11-15 00:40:23 +000069 addRegisterClass(MVT::i8, X86::R8RegisterClass);
70 addRegisterClass(MVT::i16, X86::R16RegisterClass);
71 addRegisterClass(MVT::i32, X86::R32RegisterClass);
72
73 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
74 // operation.
75 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
76 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
77 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng0d5b69f2006-01-17 02:32:49 +000078
79 if (X86ScalarSSE)
80 // No SSE i64 SINT_TO_FP, so expand i32 UINT_TO_FP instead.
81 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
82 else
83 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Chris Lattner76ac0682005-11-15 00:40:23 +000084
85 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
86 // this operation.
87 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
88 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +000089 // SSE has no i16 to fp conversion, only i32
Evan Cheng08390f62006-01-30 22:13:22 +000090 if (X86ScalarSSE)
Evan Cheng08390f62006-01-30 22:13:22 +000091 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng593bea72006-02-17 07:01:52 +000092 else {
93 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
94 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
95 }
Chris Lattner76ac0682005-11-15 00:40:23 +000096
Evan Cheng5b97fcf2006-01-30 08:02:57 +000097 // We can handle SINT_TO_FP and FP_TO_SINT from/to i64 even though i64
98 // isn't legal.
99 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
100 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
101
Evan Cheng08390f62006-01-30 22:13:22 +0000102 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
103 // this operation.
104 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
105 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
106
107 if (X86ScalarSSE) {
108 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
109 } else {
Chris Lattner76ac0682005-11-15 00:40:23 +0000110 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng08390f62006-01-30 22:13:22 +0000111 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000112 }
113
114 // Handle FP_TO_UINT by promoting the destination to a larger signed
115 // conversion.
116 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
117 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
118 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
119
Evan Chengd13778e2006-02-18 07:26:17 +0000120 if (X86ScalarSSE && !Subtarget->hasSSE3())
Evan Cheng08390f62006-01-30 22:13:22 +0000121 // Expand FP_TO_UINT into a select.
122 // FIXME: We would like to use a Custom expander here eventually to do
123 // the optimal thing for SSE vs. the default expansion in the legalizer.
124 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
125 else
Evan Chengd13778e2006-02-18 07:26:17 +0000126 // With SSE3 we can use fisttpll to convert to a signed i64.
Chris Lattner76ac0682005-11-15 00:40:23 +0000127 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
128
Evan Cheng08390f62006-01-30 22:13:22 +0000129 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
130 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
Chris Lattner30107e62005-12-23 05:15:23 +0000131
Evan Cheng593bea72006-02-17 07:01:52 +0000132 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman7e7f4392006-02-01 07:19:44 +0000133 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
134 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000135 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
136 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattner32257332005-12-07 17:59:14 +0000137 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000138 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
139 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
140 setOperationAction(ISD::SEXTLOAD , MVT::i1 , Expand);
141 setOperationAction(ISD::FREM , MVT::f64 , Expand);
142 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
143 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
144 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
145 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
146 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
147 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
148 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
149 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
150 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +0000151 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begeman2fba8a32006-01-14 03:14:10 +0000152 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman1b8121b2006-01-11 21:21:00 +0000153
Chris Lattner76ac0682005-11-15 00:40:23 +0000154 // These should be promoted to a larger select which is supported.
155 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
156 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000157
158 // X86 wants to expand cmov itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000159 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
160 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
161 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
162 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
163 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
164 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
165 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
166 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
167 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000168 // X86 ret instruction may pop stack.
Evan Cheng593bea72006-02-17 07:01:52 +0000169 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000170 // Darwin ABI issue.
Evan Cheng5588de92006-02-18 00:15:05 +0000171 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Evan Cheng593bea72006-02-17 07:01:52 +0000172 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Evan Chenge0ed6ec2006-02-23 20:41:18 +0000173 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000174 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng593bea72006-02-17 07:01:52 +0000175 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
176 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
177 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman7e5496d2006-02-17 00:03:04 +0000178 // X86 wants to expand memset / memcpy itself.
Evan Cheng593bea72006-02-17 07:01:52 +0000179 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
180 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattner76ac0682005-11-15 00:40:23 +0000181
Chris Lattner9c415362005-11-29 06:16:21 +0000182 // We don't have line number support yet.
183 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeydeeafa02006-01-05 01:47:43 +0000184 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng30d7b702006-03-07 02:02:57 +0000185 // FIXME - use subtarget debug flags
Evan Chengbc047222006-03-22 19:22:18 +0000186 if (!Subtarget->isTargetDarwin())
Evan Cheng30d7b702006-03-07 02:02:57 +0000187 setOperationAction(ISD::DEBUG_LABEL, MVT::Other, Expand);
Chris Lattner9c415362005-11-29 06:16:21 +0000188
Nate Begemane74795c2006-01-25 18:21:52 +0000189 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
190 setOperationAction(ISD::VASTART , MVT::Other, Custom);
191
192 // Use the default implementation.
193 setOperationAction(ISD::VAARG , MVT::Other, Expand);
194 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
195 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Chris Lattner78c358d2006-01-15 09:00:21 +0000196 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
197 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
198 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32 , Expand);
Chris Lattner8e2f52e2006-01-13 02:42:53 +0000199
Chris Lattner9c7f5032006-03-05 05:08:37 +0000200 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
201 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
202
Chris Lattner76ac0682005-11-15 00:40:23 +0000203 if (X86ScalarSSE) {
204 // Set up the FP register classes.
Evan Cheng84dc9b52006-01-12 08:27:59 +0000205 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
206 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattner76ac0682005-11-15 00:40:23 +0000207
208 // SSE has no load+extend ops
209 setOperationAction(ISD::EXTLOAD, MVT::f32, Expand);
210 setOperationAction(ISD::ZEXTLOAD, MVT::f32, Expand);
211
Evan Cheng72d5c252006-01-31 22:28:30 +0000212 // Use ANDPD to simulate FABS.
213 setOperationAction(ISD::FABS , MVT::f64, Custom);
214 setOperationAction(ISD::FABS , MVT::f32, Custom);
215
216 // Use XORP to simulate FNEG.
217 setOperationAction(ISD::FNEG , MVT::f64, Custom);
218 setOperationAction(ISD::FNEG , MVT::f32, Custom);
219
Evan Chengd8fba3a2006-02-02 00:28:23 +0000220 // We don't support sin/cos/fmod
Chris Lattner76ac0682005-11-15 00:40:23 +0000221 setOperationAction(ISD::FSIN , MVT::f64, Expand);
222 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000223 setOperationAction(ISD::FREM , MVT::f64, Expand);
224 setOperationAction(ISD::FSIN , MVT::f32, Expand);
225 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000226 setOperationAction(ISD::FREM , MVT::f32, Expand);
227
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000228 // Expand FP immediates into loads from the stack, except for the special
229 // cases we handle.
230 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
231 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000232 addLegalFPImmediate(+0.0); // xorps / xorpd
233 } else {
234 // Set up the FP register classes.
235 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Chris Lattner132177e2006-01-29 06:44:22 +0000236
237 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
238
Chris Lattner76ac0682005-11-15 00:40:23 +0000239 if (!UnsafeFPMath) {
240 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
241 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
242 }
243
Chris Lattner61c9a8e2006-01-29 06:26:08 +0000244 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattner76ac0682005-11-15 00:40:23 +0000245 addLegalFPImmediate(+0.0); // FLD0
246 addLegalFPImmediate(+1.0); // FLD1
247 addLegalFPImmediate(-0.0); // FLD0/FCHS
248 addLegalFPImmediate(-1.0); // FLD1/FCHS
249 }
Evan Cheng9e252e32006-02-22 02:26:30 +0000250
Evan Cheng19264272006-03-01 01:11:20 +0000251 // First set operation action for all vector types to expand. Then we
252 // will selectively turn on ones that can be effectively codegen'd.
253 for (unsigned VT = (unsigned)MVT::Vector + 1;
254 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
255 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
256 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
257 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
258 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000259 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner00f46832006-03-21 20:51:05 +0000260 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengcbffa462006-03-31 19:22:53 +0000261 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Cheng19264272006-03-01 01:11:20 +0000262 }
263
Evan Chengbc047222006-03-22 19:22:18 +0000264 if (Subtarget->hasMMX()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000265 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
266 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
267 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
268
Evan Cheng19264272006-03-01 01:11:20 +0000269 // FIXME: add MMX packed arithmetics
Evan Chengd5e905d2006-03-21 23:01:21 +0000270 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Expand);
271 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Expand);
272 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Expand);
Evan Cheng9e252e32006-02-22 02:26:30 +0000273 }
274
Evan Chengbc047222006-03-22 19:22:18 +0000275 if (Subtarget->hasSSE1()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000276 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
277
Evan Chengd5e905d2006-03-21 23:01:21 +0000278 setOperationAction(ISD::ADD, MVT::v4f32, Legal);
279 setOperationAction(ISD::SUB, MVT::v4f32, Legal);
280 setOperationAction(ISD::MUL, MVT::v4f32, Legal);
281 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
Evan Cheng082c8782006-03-24 07:29:27 +0000282 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
Evan Chengd097e672006-03-22 02:53:00 +0000283 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000284 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000285 }
286
Evan Chengbc047222006-03-22 19:22:18 +0000287 if (Subtarget->hasSSE2()) {
Evan Cheng9e252e32006-02-22 02:26:30 +0000288 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
289 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
290 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
291 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
292 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
293
294
Evan Chengd5e905d2006-03-21 23:01:21 +0000295 setOperationAction(ISD::ADD, MVT::v2f64, Legal);
Evan Chengb9b05502006-03-23 01:57:24 +0000296 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
297 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
298 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Chengd5e905d2006-03-21 23:01:21 +0000299 setOperationAction(ISD::SUB, MVT::v2f64, Legal);
Evan Cheng6f7d31e2006-03-25 01:33:37 +0000300 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
301 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
302 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Chengd5e905d2006-03-21 23:01:21 +0000303 setOperationAction(ISD::MUL, MVT::v2f64, Legal);
304 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Chengb9b05502006-03-23 01:57:24 +0000305 setOperationAction(ISD::LOAD, MVT::v16i8, Legal);
306 setOperationAction(ISD::LOAD, MVT::v8i16, Legal);
307 setOperationAction(ISD::LOAD, MVT::v4i32, Legal);
308 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Cheng5df75882006-03-28 00:39:58 +0000309 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
310 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Cheng082c8782006-03-24 07:29:27 +0000311 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
312 setOperationAction(ISD::BUILD_VECTOR, MVT::v16i8, Custom);
313 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i16, Custom);
314 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i32, Custom);
315 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
Evan Chengd097e672006-03-22 02:53:00 +0000316 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
Evan Cheng5df75882006-03-28 00:39:58 +0000317 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v16i8, Custom);
318 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i16, Custom);
319 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i32, Custom);
320 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000321 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000322 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Chengebf10062006-04-03 20:53:28 +0000323 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Chengcbffa462006-03-31 19:22:53 +0000324 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng9e252e32006-02-22 02:26:30 +0000325 }
326
Evan Cheng78038292006-04-05 23:38:46 +0000327 // We want to custom lower some of our intrinsics.
328 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
329
Chris Lattner76ac0682005-11-15 00:40:23 +0000330 computeRegisterProperties();
331
Evan Cheng6a374562006-02-14 08:25:08 +0000332 // FIXME: These should be based on subtarget info. Plus, the values should
333 // be smaller when we are in optimizing for size mode.
Evan Cheng4b40a422006-02-14 08:38:30 +0000334 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
335 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
336 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattner76ac0682005-11-15 00:40:23 +0000337 allowUnalignedMemoryAccesses = true; // x86 supports it!
338}
339
340std::vector<SDOperand>
341X86TargetLowering::LowerArguments(Function &F, SelectionDAG &DAG) {
342 if (F.getCallingConv() == CallingConv::Fast && EnableFastCC)
343 return LowerFastCCArguments(F, DAG);
344 return LowerCCCArguments(F, DAG);
345}
346
347std::pair<SDOperand, SDOperand>
348X86TargetLowering::LowerCallTo(SDOperand Chain, const Type *RetTy,
349 bool isVarArg, unsigned CallingConv,
350 bool isTailCall,
351 SDOperand Callee, ArgListTy &Args,
352 SelectionDAG &DAG) {
353 assert((!isVarArg || CallingConv == CallingConv::C) &&
354 "Only C takes varargs!");
Evan Cheng172fce72006-01-06 00:43:03 +0000355
356 // If the callee is a GlobalAddress node (quite common, every direct call is)
357 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
358 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
359 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Evan Chengbc7a0f442006-01-11 06:09:51 +0000360 else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
361 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Evan Cheng172fce72006-01-06 00:43:03 +0000362
Chris Lattner76ac0682005-11-15 00:40:23 +0000363 if (CallingConv == CallingConv::Fast && EnableFastCC)
364 return LowerFastCCCallTo(Chain, RetTy, isTailCall, Callee, Args, DAG);
365 return LowerCCCCallTo(Chain, RetTy, isVarArg, isTailCall, Callee, Args, DAG);
366}
367
368//===----------------------------------------------------------------------===//
369// C Calling Convention implementation
370//===----------------------------------------------------------------------===//
371
372std::vector<SDOperand>
373X86TargetLowering::LowerCCCArguments(Function &F, SelectionDAG &DAG) {
374 std::vector<SDOperand> ArgValues;
375
376 MachineFunction &MF = DAG.getMachineFunction();
377 MachineFrameInfo *MFI = MF.getFrameInfo();
378
379 // Add DAG nodes to load the arguments... On entry to a function on the X86,
380 // the stack frame looks like this:
381 //
382 // [ESP] -- return address
383 // [ESP + 4] -- first argument (leftmost lexically)
384 // [ESP + 8] -- second argument, if first argument is four bytes in size
385 // ...
386 //
387 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
388 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
389 MVT::ValueType ObjectVT = getValueType(I->getType());
390 unsigned ArgIncrement = 4;
391 unsigned ObjSize;
392 switch (ObjectVT) {
393 default: assert(0 && "Unhandled argument type!");
394 case MVT::i1:
395 case MVT::i8: ObjSize = 1; break;
396 case MVT::i16: ObjSize = 2; break;
397 case MVT::i32: ObjSize = 4; break;
398 case MVT::i64: ObjSize = ArgIncrement = 8; break;
399 case MVT::f32: ObjSize = 4; break;
400 case MVT::f64: ObjSize = ArgIncrement = 8; break;
401 }
402 // Create the frame index object for this incoming parameter...
403 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
404
405 // Create the SelectionDAG nodes corresponding to a load from this parameter
406 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
407
408 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
409 // dead loads.
410 SDOperand ArgValue;
411 if (!I->use_empty())
412 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
413 DAG.getSrcValue(NULL));
414 else {
415 if (MVT::isInteger(ObjectVT))
416 ArgValue = DAG.getConstant(0, ObjectVT);
417 else
418 ArgValue = DAG.getConstantFP(0, ObjectVT);
419 }
420 ArgValues.push_back(ArgValue);
421
422 ArgOffset += ArgIncrement; // Move on to the next argument...
423 }
424
425 // If the function takes variable number of arguments, make a frame index for
426 // the start of the first vararg value... for expansion of llvm.va_start.
427 if (F.isVarArg())
428 VarArgsFrameIndex = MFI->CreateFixedObject(1, ArgOffset);
429 ReturnAddrIndex = 0; // No return address slot generated yet.
430 BytesToPopOnReturn = 0; // Callee pops nothing.
431 BytesCallerReserves = ArgOffset;
432
433 // Finally, inform the code generator which regs we return values in.
434 switch (getValueType(F.getReturnType())) {
435 default: assert(0 && "Unknown type!");
436 case MVT::isVoid: break;
437 case MVT::i1:
438 case MVT::i8:
439 case MVT::i16:
440 case MVT::i32:
441 MF.addLiveOut(X86::EAX);
442 break;
443 case MVT::i64:
444 MF.addLiveOut(X86::EAX);
445 MF.addLiveOut(X86::EDX);
446 break;
447 case MVT::f32:
448 case MVT::f64:
449 MF.addLiveOut(X86::ST0);
450 break;
451 }
452 return ArgValues;
453}
454
455std::pair<SDOperand, SDOperand>
456X86TargetLowering::LowerCCCCallTo(SDOperand Chain, const Type *RetTy,
457 bool isVarArg, bool isTailCall,
458 SDOperand Callee, ArgListTy &Args,
459 SelectionDAG &DAG) {
460 // Count how many bytes are to be pushed on the stack.
461 unsigned NumBytes = 0;
462
463 if (Args.empty()) {
464 // Save zero bytes.
Chris Lattner62c34842006-02-13 09:00:43 +0000465 Chain = DAG.getCALLSEQ_START(Chain, DAG.getConstant(0, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000466 } else {
467 for (unsigned i = 0, e = Args.size(); i != e; ++i)
468 switch (getValueType(Args[i].second)) {
469 default: assert(0 && "Unknown value type!");
470 case MVT::i1:
471 case MVT::i8:
472 case MVT::i16:
473 case MVT::i32:
474 case MVT::f32:
475 NumBytes += 4;
476 break;
477 case MVT::i64:
478 case MVT::f64:
479 NumBytes += 8;
480 break;
481 }
482
Chris Lattner62c34842006-02-13 09:00:43 +0000483 Chain = DAG.getCALLSEQ_START(Chain,
484 DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000485
486 // Arguments go on the stack in reverse order, as specified by the ABI.
487 unsigned ArgOffset = 0;
Evan Chengbc7a0f442006-01-11 06:09:51 +0000488 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000489 std::vector<SDOperand> Stores;
490
491 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
492 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
493 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
494
495 switch (getValueType(Args[i].second)) {
496 default: assert(0 && "Unexpected ValueType for argument!");
497 case MVT::i1:
498 case MVT::i8:
499 case MVT::i16:
500 // Promote the integer to 32 bits. If the input type is signed use a
501 // sign extend, otherwise use a zero extend.
502 if (Args[i].second->isSigned())
503 Args[i].first =DAG.getNode(ISD::SIGN_EXTEND, MVT::i32, Args[i].first);
504 else
505 Args[i].first =DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Args[i].first);
506
507 // FALL THROUGH
508 case MVT::i32:
509 case MVT::f32:
510 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
511 Args[i].first, PtrOff,
512 DAG.getSrcValue(NULL)));
513 ArgOffset += 4;
514 break;
515 case MVT::i64:
516 case MVT::f64:
517 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
518 Args[i].first, PtrOff,
519 DAG.getSrcValue(NULL)));
520 ArgOffset += 8;
521 break;
522 }
523 }
524 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
525 }
526
527 std::vector<MVT::ValueType> RetVals;
528 MVT::ValueType RetTyVT = getValueType(RetTy);
529 RetVals.push_back(MVT::Other);
530
531 // The result values produced have to be legal. Promote the result.
532 switch (RetTyVT) {
533 case MVT::isVoid: break;
534 default:
535 RetVals.push_back(RetTyVT);
536 break;
537 case MVT::i1:
538 case MVT::i8:
539 case MVT::i16:
540 RetVals.push_back(MVT::i32);
541 break;
542 case MVT::f32:
543 if (X86ScalarSSE)
544 RetVals.push_back(MVT::f32);
545 else
546 RetVals.push_back(MVT::f64);
547 break;
548 case MVT::i64:
549 RetVals.push_back(MVT::i32);
550 RetVals.push_back(MVT::i32);
551 break;
552 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000553
Nate Begeman7e5496d2006-02-17 00:03:04 +0000554 std::vector<MVT::ValueType> NodeTys;
555 NodeTys.push_back(MVT::Other); // Returns a chain
556 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
557 std::vector<SDOperand> Ops;
558 Ops.push_back(Chain);
559 Ops.push_back(Callee);
Evan Cheng45e190982006-01-05 00:27:02 +0000560
Nate Begeman7e5496d2006-02-17 00:03:04 +0000561 // FIXME: Do not generate X86ISD::TAILCALL for now.
562 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
563 SDOperand InFlag = Chain.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000564
Nate Begeman7e5496d2006-02-17 00:03:04 +0000565 NodeTys.clear();
566 NodeTys.push_back(MVT::Other); // Returns a chain
567 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
568 Ops.clear();
569 Ops.push_back(Chain);
570 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
571 Ops.push_back(DAG.getConstant(0, getPointerTy()));
572 Ops.push_back(InFlag);
573 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
574 InFlag = Chain.getValue(1);
575
576 SDOperand RetVal;
577 if (RetTyVT != MVT::isVoid) {
Evan Cheng45e190982006-01-05 00:27:02 +0000578 switch (RetTyVT) {
Nate Begeman7e5496d2006-02-17 00:03:04 +0000579 default: assert(0 && "Unknown value type to return!");
Evan Cheng45e190982006-01-05 00:27:02 +0000580 case MVT::i1:
581 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000582 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
583 Chain = RetVal.getValue(1);
584 if (RetTyVT == MVT::i1)
585 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
586 break;
Evan Cheng45e190982006-01-05 00:27:02 +0000587 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +0000588 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
589 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000590 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000591 case MVT::i32:
592 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
593 Chain = RetVal.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000594 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +0000595 case MVT::i64: {
596 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
597 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
598 Lo.getValue(2));
599 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
600 Chain = Hi.getValue(1);
Evan Cheng45e190982006-01-05 00:27:02 +0000601 break;
602 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000603 case MVT::f32:
604 case MVT::f64: {
605 std::vector<MVT::ValueType> Tys;
606 Tys.push_back(MVT::f64);
607 Tys.push_back(MVT::Other);
608 Tys.push_back(MVT::Flag);
609 std::vector<SDOperand> Ops;
610 Ops.push_back(Chain);
611 Ops.push_back(InFlag);
612 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
613 Chain = RetVal.getValue(1);
614 InFlag = RetVal.getValue(2);
615 if (X86ScalarSSE) {
616 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
617 // shouldn't be necessary except that RFP cannot be live across
618 // multiple blocks. When stackifier is fixed, they can be uncoupled.
619 MachineFunction &MF = DAG.getMachineFunction();
620 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
621 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
622 Tys.clear();
623 Tys.push_back(MVT::Other);
624 Ops.clear();
625 Ops.push_back(Chain);
626 Ops.push_back(RetVal);
627 Ops.push_back(StackSlot);
628 Ops.push_back(DAG.getValueType(RetTyVT));
629 Ops.push_back(InFlag);
630 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
631 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
632 DAG.getSrcValue(NULL));
633 Chain = RetVal.getValue(1);
634 }
Evan Cheng45e190982006-01-05 00:27:02 +0000635
Nate Begeman7e5496d2006-02-17 00:03:04 +0000636 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
637 // FIXME: we would really like to remember that this FP_ROUND
638 // operation is okay to eliminate if we allow excess FP precision.
639 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
640 break;
641 }
642 }
Chris Lattner76ac0682005-11-15 00:40:23 +0000643 }
Nate Begeman7e5496d2006-02-17 00:03:04 +0000644
645 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +0000646}
647
Chris Lattner76ac0682005-11-15 00:40:23 +0000648//===----------------------------------------------------------------------===//
649// Fast Calling Convention implementation
650//===----------------------------------------------------------------------===//
651//
652// The X86 'fast' calling convention passes up to two integer arguments in
653// registers (an appropriate portion of EAX/EDX), passes arguments in C order,
654// and requires that the callee pop its arguments off the stack (allowing proper
655// tail calls), and has the same return value conventions as C calling convs.
656//
657// This calling convention always arranges for the callee pop value to be 8n+4
658// bytes, which is needed for tail recursion elimination and stack alignment
659// reasons.
660//
661// Note that this can be enhanced in the future to pass fp vals in registers
662// (when we have a global fp allocator) and do other tricks.
663//
664
665/// AddLiveIn - This helper function adds the specified physical register to the
666/// MachineFunction as a live in value. It also creates a corresponding virtual
667/// register for it.
668static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
669 TargetRegisterClass *RC) {
670 assert(RC->contains(PReg) && "Not the correct regclass!");
671 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
672 MF.addLiveIn(PReg, VReg);
673 return VReg;
674}
675
Chris Lattner388fc4d2006-03-17 17:27:47 +0000676// FASTCC_NUM_INT_ARGS_INREGS - This is the max number of integer arguments
677// to pass in registers. 0 is none, 1 is is "use EAX", 2 is "use EAX and
678// EDX". Anything more is illegal.
679//
680// FIXME: The linscan register allocator currently has problem with
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000681// coalescing. At the time of this writing, whenever it decides to coalesce
Chris Lattner388fc4d2006-03-17 17:27:47 +0000682// a physreg with a virtreg, this increases the size of the physreg's live
683// range, and the live range cannot ever be reduced. This causes problems if
Chris Lattnerf5efddf2006-03-24 07:12:19 +0000684// too many physregs are coaleced with virtregs, which can cause the register
Chris Lattner388fc4d2006-03-17 17:27:47 +0000685// allocator to wedge itself.
686//
687// This code triggers this problem more often if we pass args in registers,
688// so disable it until this is fixed.
689//
690// NOTE: this isn't marked const, so that GCC doesn't emit annoying warnings
691// about code being dead.
692//
693static unsigned FASTCC_NUM_INT_ARGS_INREGS = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000694
Chris Lattner76ac0682005-11-15 00:40:23 +0000695
696std::vector<SDOperand>
697X86TargetLowering::LowerFastCCArguments(Function &F, SelectionDAG &DAG) {
698 std::vector<SDOperand> ArgValues;
699
700 MachineFunction &MF = DAG.getMachineFunction();
701 MachineFrameInfo *MFI = MF.getFrameInfo();
702
703 // Add DAG nodes to load the arguments... On entry to a function the stack
704 // frame looks like this:
705 //
706 // [ESP] -- return address
707 // [ESP + 4] -- first nonreg argument (leftmost lexically)
708 // [ESP + 8] -- second nonreg argument, if first argument is 4 bytes in size
709 // ...
710 unsigned ArgOffset = 0; // Frame mechanisms handle retaddr slot
711
712 // Keep track of the number of integer regs passed so far. This can be either
713 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
714 // used).
715 unsigned NumIntRegs = 0;
Chris Lattner43798852006-03-17 05:10:20 +0000716
Chris Lattner76ac0682005-11-15 00:40:23 +0000717 for (Function::arg_iterator I = F.arg_begin(), E = F.arg_end(); I != E; ++I) {
718 MVT::ValueType ObjectVT = getValueType(I->getType());
719 unsigned ArgIncrement = 4;
720 unsigned ObjSize = 0;
721 SDOperand ArgValue;
722
723 switch (ObjectVT) {
724 default: assert(0 && "Unhandled argument type!");
725 case MVT::i1:
726 case MVT::i8:
Chris Lattner43798852006-03-17 05:10:20 +0000727 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000728 if (!I->use_empty()) {
729 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DL : X86::AL,
730 X86::R8RegisterClass);
731 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i8);
732 DAG.setRoot(ArgValue.getValue(1));
Chris Lattner82584892005-12-27 03:02:18 +0000733 if (ObjectVT == MVT::i1)
734 // FIXME: Should insert a assertzext here.
735 ArgValue = DAG.getNode(ISD::TRUNCATE, MVT::i1, ArgValue);
Chris Lattner76ac0682005-11-15 00:40:23 +0000736 }
737 ++NumIntRegs;
738 break;
739 }
740
741 ObjSize = 1;
742 break;
743 case MVT::i16:
Chris Lattner43798852006-03-17 05:10:20 +0000744 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000745 if (!I->use_empty()) {
746 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::DX : X86::AX,
747 X86::R16RegisterClass);
748 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i16);
749 DAG.setRoot(ArgValue.getValue(1));
750 }
751 ++NumIntRegs;
752 break;
753 }
754 ObjSize = 2;
755 break;
756 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000757 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000758 if (!I->use_empty()) {
Chris Lattner43798852006-03-17 05:10:20 +0000759 unsigned VReg = AddLiveIn(MF, NumIntRegs ? X86::EDX : X86::EAX,
Chris Lattner76ac0682005-11-15 00:40:23 +0000760 X86::R32RegisterClass);
761 ArgValue = DAG.getCopyFromReg(DAG.getRoot(), VReg, MVT::i32);
762 DAG.setRoot(ArgValue.getValue(1));
763 }
764 ++NumIntRegs;
765 break;
766 }
767 ObjSize = 4;
768 break;
769 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000770 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000771 if (!I->use_empty()) {
772 unsigned BotReg = AddLiveIn(MF, X86::EAX, X86::R32RegisterClass);
773 unsigned TopReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
774
775 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
776 SDOperand Hi = DAG.getCopyFromReg(Low.getValue(1), TopReg, MVT::i32);
777 DAG.setRoot(Hi.getValue(1));
778
779 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
780 }
Chris Lattner43798852006-03-17 05:10:20 +0000781 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000782 break;
Chris Lattner43798852006-03-17 05:10:20 +0000783 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000784 if (!I->use_empty()) {
785 unsigned BotReg = AddLiveIn(MF, X86::EDX, X86::R32RegisterClass);
786 SDOperand Low = DAG.getCopyFromReg(DAG.getRoot(), BotReg, MVT::i32);
787 DAG.setRoot(Low.getValue(1));
788
789 // Load the high part from memory.
790 // Create the frame index object for this incoming parameter...
791 int FI = MFI->CreateFixedObject(4, ArgOffset);
792 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
793 SDOperand Hi = DAG.getLoad(MVT::i32, DAG.getEntryNode(), FIN,
794 DAG.getSrcValue(NULL));
795 ArgValue = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Low, Hi);
796 }
797 ArgOffset += 4;
Chris Lattner43798852006-03-17 05:10:20 +0000798 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000799 break;
800 }
801 ObjSize = ArgIncrement = 8;
802 break;
803 case MVT::f32: ObjSize = 4; break;
804 case MVT::f64: ObjSize = ArgIncrement = 8; break;
805 }
806
807 // Don't codegen dead arguments. FIXME: remove this check when we can nuke
808 // dead loads.
809 if (ObjSize && !I->use_empty()) {
810 // Create the frame index object for this incoming parameter...
811 int FI = MFI->CreateFixedObject(ObjSize, ArgOffset);
812
813 // Create the SelectionDAG nodes corresponding to a load from this
814 // parameter.
815 SDOperand FIN = DAG.getFrameIndex(FI, MVT::i32);
816
817 ArgValue = DAG.getLoad(ObjectVT, DAG.getEntryNode(), FIN,
818 DAG.getSrcValue(NULL));
819 } else if (ArgValue.Val == 0) {
820 if (MVT::isInteger(ObjectVT))
821 ArgValue = DAG.getConstant(0, ObjectVT);
822 else
823 ArgValue = DAG.getConstantFP(0, ObjectVT);
824 }
825 ArgValues.push_back(ArgValue);
826
827 if (ObjSize)
828 ArgOffset += ArgIncrement; // Move on to the next argument.
829 }
830
831 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
832 // arguments and the arguments after the retaddr has been pushed are aligned.
833 if ((ArgOffset & 7) == 0)
834 ArgOffset += 4;
835
836 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
837 ReturnAddrIndex = 0; // No return address slot generated yet.
838 BytesToPopOnReturn = ArgOffset; // Callee pops all stack arguments.
839 BytesCallerReserves = 0;
840
841 // Finally, inform the code generator which regs we return values in.
842 switch (getValueType(F.getReturnType())) {
843 default: assert(0 && "Unknown type!");
844 case MVT::isVoid: break;
845 case MVT::i1:
846 case MVT::i8:
847 case MVT::i16:
848 case MVT::i32:
849 MF.addLiveOut(X86::EAX);
850 break;
851 case MVT::i64:
852 MF.addLiveOut(X86::EAX);
853 MF.addLiveOut(X86::EDX);
854 break;
855 case MVT::f32:
856 case MVT::f64:
857 MF.addLiveOut(X86::ST0);
858 break;
859 }
860 return ArgValues;
861}
862
863std::pair<SDOperand, SDOperand>
864X86TargetLowering::LowerFastCCCallTo(SDOperand Chain, const Type *RetTy,
865 bool isTailCall, SDOperand Callee,
866 ArgListTy &Args, SelectionDAG &DAG) {
867 // Count how many bytes are to be pushed on the stack.
868 unsigned NumBytes = 0;
869
870 // Keep track of the number of integer regs passed so far. This can be either
871 // 0 (neither EAX or EDX used), 1 (EAX is used) or 2 (EAX and EDX are both
872 // used).
873 unsigned NumIntRegs = 0;
874
875 for (unsigned i = 0, e = Args.size(); i != e; ++i)
876 switch (getValueType(Args[i].second)) {
877 default: assert(0 && "Unknown value type!");
878 case MVT::i1:
879 case MVT::i8:
880 case MVT::i16:
881 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000882 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000883 ++NumIntRegs;
884 break;
885 }
886 // fall through
887 case MVT::f32:
888 NumBytes += 4;
889 break;
890 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000891 if (NumIntRegs+2 <= FASTCC_NUM_INT_ARGS_INREGS) {
892 NumIntRegs += 2;
Chris Lattner76ac0682005-11-15 00:40:23 +0000893 break;
Chris Lattner43798852006-03-17 05:10:20 +0000894 } else if (NumIntRegs+1 <= FASTCC_NUM_INT_ARGS_INREGS) {
895 NumIntRegs = FASTCC_NUM_INT_ARGS_INREGS;
Chris Lattner76ac0682005-11-15 00:40:23 +0000896 NumBytes += 4;
897 break;
898 }
899
900 // fall through
901 case MVT::f64:
902 NumBytes += 8;
903 break;
904 }
905
906 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
907 // arguments and the arguments after the retaddr has been pushed are aligned.
908 if ((NumBytes & 7) == 0)
909 NumBytes += 4;
910
Chris Lattner62c34842006-02-13 09:00:43 +0000911 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner76ac0682005-11-15 00:40:23 +0000912
913 // Arguments go on the stack in reverse order, as specified by the ABI.
914 unsigned ArgOffset = 0;
Chris Lattner27d30a52006-01-24 06:14:44 +0000915 SDOperand StackPtr = DAG.getRegister(X86::ESP, MVT::i32);
Chris Lattner76ac0682005-11-15 00:40:23 +0000916 NumIntRegs = 0;
917 std::vector<SDOperand> Stores;
918 std::vector<SDOperand> RegValuesToPass;
919 for (unsigned i = 0, e = Args.size(); i != e; ++i) {
920 switch (getValueType(Args[i].second)) {
921 default: assert(0 && "Unexpected ValueType for argument!");
922 case MVT::i1:
Chris Lattner82584892005-12-27 03:02:18 +0000923 Args[i].first = DAG.getNode(ISD::ANY_EXTEND, MVT::i8, Args[i].first);
924 // Fall through.
Chris Lattner76ac0682005-11-15 00:40:23 +0000925 case MVT::i8:
926 case MVT::i16:
927 case MVT::i32:
Chris Lattner43798852006-03-17 05:10:20 +0000928 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000929 RegValuesToPass.push_back(Args[i].first);
930 ++NumIntRegs;
931 break;
932 }
933 // Fall through
934 case MVT::f32: {
935 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
936 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
937 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
938 Args[i].first, PtrOff,
939 DAG.getSrcValue(NULL)));
940 ArgOffset += 4;
941 break;
942 }
943 case MVT::i64:
Chris Lattner43798852006-03-17 05:10:20 +0000944 // Can pass (at least) part of it in regs?
945 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000946 SDOperand Hi = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
947 Args[i].first, DAG.getConstant(1, MVT::i32));
948 SDOperand Lo = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
949 Args[i].first, DAG.getConstant(0, MVT::i32));
950 RegValuesToPass.push_back(Lo);
951 ++NumIntRegs;
Chris Lattner43798852006-03-17 05:10:20 +0000952
953 // Pass both parts in regs?
954 if (NumIntRegs < FASTCC_NUM_INT_ARGS_INREGS) {
Chris Lattner76ac0682005-11-15 00:40:23 +0000955 RegValuesToPass.push_back(Hi);
956 ++NumIntRegs;
957 } else {
958 // Pass the high part in memory.
959 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
960 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
961 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
962 Hi, PtrOff, DAG.getSrcValue(NULL)));
963 ArgOffset += 4;
964 }
965 break;
966 }
967 // Fall through
968 case MVT::f64:
969 SDOperand PtrOff = DAG.getConstant(ArgOffset, getPointerTy());
970 PtrOff = DAG.getNode(ISD::ADD, MVT::i32, StackPtr, PtrOff);
971 Stores.push_back(DAG.getNode(ISD::STORE, MVT::Other, Chain,
972 Args[i].first, PtrOff,
973 DAG.getSrcValue(NULL)));
974 ArgOffset += 8;
975 break;
976 }
977 }
978 if (!Stores.empty())
979 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other, Stores);
980
981 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
982 // arguments and the arguments after the retaddr has been pushed are aligned.
983 if ((ArgOffset & 7) == 0)
984 ArgOffset += 4;
985
986 std::vector<MVT::ValueType> RetVals;
987 MVT::ValueType RetTyVT = getValueType(RetTy);
988
989 RetVals.push_back(MVT::Other);
990
991 // The result values produced have to be legal. Promote the result.
992 switch (RetTyVT) {
993 case MVT::isVoid: break;
994 default:
995 RetVals.push_back(RetTyVT);
996 break;
997 case MVT::i1:
998 case MVT::i8:
999 case MVT::i16:
1000 RetVals.push_back(MVT::i32);
1001 break;
1002 case MVT::f32:
1003 if (X86ScalarSSE)
1004 RetVals.push_back(MVT::f32);
1005 else
1006 RetVals.push_back(MVT::f64);
1007 break;
1008 case MVT::i64:
1009 RetVals.push_back(MVT::i32);
1010 RetVals.push_back(MVT::i32);
1011 break;
1012 }
1013
Nate Begeman7e5496d2006-02-17 00:03:04 +00001014 // Build a sequence of copy-to-reg nodes chained together with token chain
1015 // and flag operands which copy the outgoing args into registers.
1016 SDOperand InFlag;
1017 for (unsigned i = 0, e = RegValuesToPass.size(); i != e; ++i) {
1018 unsigned CCReg;
1019 SDOperand RegToPass = RegValuesToPass[i];
1020 switch (RegToPass.getValueType()) {
1021 default: assert(0 && "Bad thing to pass in regs");
1022 case MVT::i8:
1023 CCReg = (i == 0) ? X86::AL : X86::DL;
Evan Cheng172fce72006-01-06 00:43:03 +00001024 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001025 case MVT::i16:
1026 CCReg = (i == 0) ? X86::AX : X86::DX;
1027 break;
1028 case MVT::i32:
1029 CCReg = (i == 0) ? X86::EAX : X86::EDX;
1030 break;
1031 }
1032
1033 Chain = DAG.getCopyToReg(Chain, CCReg, RegToPass, InFlag);
1034 InFlag = Chain.getValue(1);
1035 }
1036
1037 std::vector<MVT::ValueType> NodeTys;
1038 NodeTys.push_back(MVT::Other); // Returns a chain
1039 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1040 std::vector<SDOperand> Ops;
1041 Ops.push_back(Chain);
1042 Ops.push_back(Callee);
1043 if (InFlag.Val)
1044 Ops.push_back(InFlag);
1045
1046 // FIXME: Do not generate X86ISD::TAILCALL for now.
1047 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops);
1048 InFlag = Chain.getValue(1);
1049
1050 NodeTys.clear();
1051 NodeTys.push_back(MVT::Other); // Returns a chain
1052 NodeTys.push_back(MVT::Flag); // Returns a flag for retval copy to use.
1053 Ops.clear();
1054 Ops.push_back(Chain);
1055 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1056 Ops.push_back(DAG.getConstant(ArgOffset, getPointerTy()));
1057 Ops.push_back(InFlag);
1058 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, Ops);
1059 InFlag = Chain.getValue(1);
1060
1061 SDOperand RetVal;
1062 if (RetTyVT != MVT::isVoid) {
1063 switch (RetTyVT) {
1064 default: assert(0 && "Unknown value type to return!");
Evan Cheng172fce72006-01-06 00:43:03 +00001065 case MVT::i1:
1066 case MVT::i8:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001067 RetVal = DAG.getCopyFromReg(Chain, X86::AL, MVT::i8, InFlag);
1068 Chain = RetVal.getValue(1);
1069 if (RetTyVT == MVT::i1)
1070 RetVal = DAG.getNode(ISD::TRUNCATE, MVT::i1, RetVal);
1071 break;
Evan Cheng172fce72006-01-06 00:43:03 +00001072 case MVT::i16:
Nate Begeman7e5496d2006-02-17 00:03:04 +00001073 RetVal = DAG.getCopyFromReg(Chain, X86::AX, MVT::i16, InFlag);
1074 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001075 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001076 case MVT::i32:
1077 RetVal = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1078 Chain = RetVal.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001079 break;
Nate Begeman7e5496d2006-02-17 00:03:04 +00001080 case MVT::i64: {
1081 SDOperand Lo = DAG.getCopyFromReg(Chain, X86::EAX, MVT::i32, InFlag);
1082 SDOperand Hi = DAG.getCopyFromReg(Lo.getValue(1), X86::EDX, MVT::i32,
1083 Lo.getValue(2));
1084 RetVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Lo, Hi);
1085 Chain = Hi.getValue(1);
Evan Cheng172fce72006-01-06 00:43:03 +00001086 break;
1087 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001088 case MVT::f32:
1089 case MVT::f64: {
1090 std::vector<MVT::ValueType> Tys;
1091 Tys.push_back(MVT::f64);
1092 Tys.push_back(MVT::Other);
1093 Tys.push_back(MVT::Flag);
1094 std::vector<SDOperand> Ops;
1095 Ops.push_back(Chain);
1096 Ops.push_back(InFlag);
1097 RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, Ops);
1098 Chain = RetVal.getValue(1);
1099 InFlag = RetVal.getValue(2);
1100 if (X86ScalarSSE) {
1101 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
1102 // shouldn't be necessary except that RFP cannot be live across
1103 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1104 MachineFunction &MF = DAG.getMachineFunction();
1105 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1106 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1107 Tys.clear();
1108 Tys.push_back(MVT::Other);
1109 Ops.clear();
1110 Ops.push_back(Chain);
1111 Ops.push_back(RetVal);
1112 Ops.push_back(StackSlot);
1113 Ops.push_back(DAG.getValueType(RetTyVT));
1114 Ops.push_back(InFlag);
1115 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1116 RetVal = DAG.getLoad(RetTyVT, Chain, StackSlot,
1117 DAG.getSrcValue(NULL));
1118 Chain = RetVal.getValue(1);
1119 }
Evan Cheng172fce72006-01-06 00:43:03 +00001120
Nate Begeman7e5496d2006-02-17 00:03:04 +00001121 if (RetTyVT == MVT::f32 && !X86ScalarSSE)
1122 // FIXME: we would really like to remember that this FP_ROUND
1123 // operation is okay to eliminate if we allow excess FP precision.
1124 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
1125 break;
1126 }
1127 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001128 }
Nate Begeman7e5496d2006-02-17 00:03:04 +00001129
1130 return std::make_pair(RetVal, Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001131}
1132
1133SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1134 if (ReturnAddrIndex == 0) {
1135 // Set up a frame object for the return address.
1136 MachineFunction &MF = DAG.getMachineFunction();
1137 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
1138 }
1139
1140 return DAG.getFrameIndex(ReturnAddrIndex, MVT::i32);
1141}
1142
1143
1144
1145std::pair<SDOperand, SDOperand> X86TargetLowering::
1146LowerFrameReturnAddress(bool isFrameAddress, SDOperand Chain, unsigned Depth,
1147 SelectionDAG &DAG) {
1148 SDOperand Result;
1149 if (Depth) // Depths > 0 not supported yet!
1150 Result = DAG.getConstant(0, getPointerTy());
1151 else {
1152 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
1153 if (!isFrameAddress)
1154 // Just load the return address
1155 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(), RetAddrFI,
1156 DAG.getSrcValue(NULL));
1157 else
1158 Result = DAG.getNode(ISD::SUB, MVT::i32, RetAddrFI,
1159 DAG.getConstant(4, MVT::i32));
1160 }
1161 return std::make_pair(Result, Chain);
1162}
1163
Evan Cheng339edad2006-01-11 00:33:36 +00001164/// getCondBrOpcodeForX86CC - Returns the X86 conditional branch opcode
1165/// which corresponds to the condition code.
1166static unsigned getCondBrOpcodeForX86CC(unsigned X86CC) {
1167 switch (X86CC) {
1168 default: assert(0 && "Unknown X86 conditional code!");
1169 case X86ISD::COND_A: return X86::JA;
1170 case X86ISD::COND_AE: return X86::JAE;
1171 case X86ISD::COND_B: return X86::JB;
1172 case X86ISD::COND_BE: return X86::JBE;
1173 case X86ISD::COND_E: return X86::JE;
1174 case X86ISD::COND_G: return X86::JG;
1175 case X86ISD::COND_GE: return X86::JGE;
1176 case X86ISD::COND_L: return X86::JL;
1177 case X86ISD::COND_LE: return X86::JLE;
1178 case X86ISD::COND_NE: return X86::JNE;
1179 case X86ISD::COND_NO: return X86::JNO;
1180 case X86ISD::COND_NP: return X86::JNP;
1181 case X86ISD::COND_NS: return X86::JNS;
1182 case X86ISD::COND_O: return X86::JO;
1183 case X86ISD::COND_P: return X86::JP;
1184 case X86ISD::COND_S: return X86::JS;
1185 }
1186}
Chris Lattner76ac0682005-11-15 00:40:23 +00001187
Evan Cheng45df7f82006-01-30 23:41:35 +00001188/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1189/// specific condition code. It returns a false if it cannot do a direct
1190/// translation. X86CC is the translated CondCode. Flip is set to true if the
1191/// the order of comparison operands should be flipped.
Evan Cheng78038292006-04-05 23:38:46 +00001192static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
1193 unsigned &X86CC, bool &Flip) {
Evan Cheng45df7f82006-01-30 23:41:35 +00001194 Flip = false;
1195 X86CC = X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001196 if (!isFP) {
1197 switch (SetCCOpcode) {
1198 default: break;
1199 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
1200 case ISD::SETGT: X86CC = X86ISD::COND_G; break;
1201 case ISD::SETGE: X86CC = X86ISD::COND_GE; break;
1202 case ISD::SETLT: X86CC = X86ISD::COND_L; break;
1203 case ISD::SETLE: X86CC = X86ISD::COND_LE; break;
1204 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1205 case ISD::SETULT: X86CC = X86ISD::COND_B; break;
1206 case ISD::SETUGT: X86CC = X86ISD::COND_A; break;
1207 case ISD::SETULE: X86CC = X86ISD::COND_BE; break;
1208 case ISD::SETUGE: X86CC = X86ISD::COND_AE; break;
1209 }
1210 } else {
1211 // On a floating point condition, the flags are set as follows:
1212 // ZF PF CF op
1213 // 0 | 0 | 0 | X > Y
1214 // 0 | 0 | 1 | X < Y
1215 // 1 | 0 | 0 | X == Y
1216 // 1 | 1 | 1 | unordered
1217 switch (SetCCOpcode) {
1218 default: break;
1219 case ISD::SETUEQ:
1220 case ISD::SETEQ: X86CC = X86ISD::COND_E; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001221 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001222 case ISD::SETOGT:
1223 case ISD::SETGT: X86CC = X86ISD::COND_A; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001224 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001225 case ISD::SETOGE:
1226 case ISD::SETGE: X86CC = X86ISD::COND_AE; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001227 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001228 case ISD::SETULT:
1229 case ISD::SETLT: X86CC = X86ISD::COND_B; break;
Evan Cheng45df7f82006-01-30 23:41:35 +00001230 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Cheng172fce72006-01-06 00:43:03 +00001231 case ISD::SETULE:
1232 case ISD::SETLE: X86CC = X86ISD::COND_BE; break;
1233 case ISD::SETONE:
1234 case ISD::SETNE: X86CC = X86ISD::COND_NE; break;
1235 case ISD::SETUO: X86CC = X86ISD::COND_P; break;
1236 case ISD::SETO: X86CC = X86ISD::COND_NP; break;
1237 }
1238 }
Evan Cheng45df7f82006-01-30 23:41:35 +00001239
1240 return X86CC != X86ISD::COND_INVALID;
Evan Cheng172fce72006-01-06 00:43:03 +00001241}
1242
Evan Cheng78038292006-04-05 23:38:46 +00001243static bool translateX86CC(SDOperand CC, bool isFP, unsigned &X86CC,
1244 bool &Flip) {
1245 return translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC, Flip);
1246}
1247
Evan Cheng339edad2006-01-11 00:33:36 +00001248/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1249/// code. Current x86 isa includes the following FP cmov instructions:
Evan Cheng73a1ad92006-01-10 20:26:56 +00001250/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng339edad2006-01-11 00:33:36 +00001251static bool hasFPCMov(unsigned X86CC) {
Evan Cheng73a1ad92006-01-10 20:26:56 +00001252 switch (X86CC) {
1253 default:
1254 return false;
1255 case X86ISD::COND_B:
1256 case X86ISD::COND_BE:
1257 case X86ISD::COND_E:
1258 case X86ISD::COND_P:
1259 case X86ISD::COND_A:
1260 case X86ISD::COND_AE:
1261 case X86ISD::COND_NE:
1262 case X86ISD::COND_NP:
1263 return true;
1264 }
1265}
1266
Evan Cheng339edad2006-01-11 00:33:36 +00001267MachineBasicBlock *
1268X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
1269 MachineBasicBlock *BB) {
Evan Cheng911c68d2006-01-16 21:21:29 +00001270 switch (MI->getOpcode()) {
1271 default: assert(false && "Unexpected instr type to insert");
1272 case X86::CMOV_FR32:
1273 case X86::CMOV_FR64: {
Chris Lattnerc642aa52006-01-31 19:43:35 +00001274 // To "insert" a SELECT_CC instruction, we actually have to insert the
1275 // diamond control-flow pattern. The incoming instruction knows the
1276 // destination vreg to set, the condition code register to branch on, the
1277 // true/false values to select between, and a branch opcode to use.
Evan Cheng911c68d2006-01-16 21:21:29 +00001278 const BasicBlock *LLVM_BB = BB->getBasicBlock();
1279 ilist<MachineBasicBlock>::iterator It = BB;
1280 ++It;
1281
1282 // thisMBB:
1283 // ...
1284 // TrueVal = ...
1285 // cmpTY ccX, r1, r2
1286 // bCC copy1MBB
1287 // fallthrough --> copy0MBB
1288 MachineBasicBlock *thisMBB = BB;
1289 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
1290 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
1291 unsigned Opc = getCondBrOpcodeForX86CC(MI->getOperand(3).getImmedValue());
1292 BuildMI(BB, Opc, 1).addMBB(sinkMBB);
1293 MachineFunction *F = BB->getParent();
1294 F->getBasicBlockList().insert(It, copy0MBB);
1295 F->getBasicBlockList().insert(It, sinkMBB);
Nate Begemaned728c12006-03-27 01:32:24 +00001296 // Update machine-CFG edges by first adding all successors of the current
1297 // block to the new block which will contain the Phi node for the select.
1298 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
1299 e = BB->succ_end(); i != e; ++i)
1300 sinkMBB->addSuccessor(*i);
1301 // Next, remove all successors of the current block, and add the true
1302 // and fallthrough blocks as its successors.
1303 while(!BB->succ_empty())
1304 BB->removeSuccessor(BB->succ_begin());
Evan Cheng911c68d2006-01-16 21:21:29 +00001305 BB->addSuccessor(copy0MBB);
1306 BB->addSuccessor(sinkMBB);
1307
1308 // copy0MBB:
1309 // %FalseValue = ...
1310 // # fallthrough to sinkMBB
1311 BB = copy0MBB;
1312
1313 // Update machine-CFG edges
1314 BB->addSuccessor(sinkMBB);
1315
1316 // sinkMBB:
1317 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
1318 // ...
1319 BB = sinkMBB;
1320 BuildMI(BB, X86::PHI, 4, MI->getOperand(0).getReg())
1321 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
1322 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
Evan Cheng339edad2006-01-11 00:33:36 +00001323
Evan Cheng911c68d2006-01-16 21:21:29 +00001324 delete MI; // The pseudo instruction is gone now.
1325 return BB;
1326 }
Evan Cheng339edad2006-01-11 00:33:36 +00001327
Evan Cheng911c68d2006-01-16 21:21:29 +00001328 case X86::FP_TO_INT16_IN_MEM:
1329 case X86::FP_TO_INT32_IN_MEM:
1330 case X86::FP_TO_INT64_IN_MEM: {
1331 // Change the floating point control register to use "round towards zero"
1332 // mode when truncating to an integer value.
1333 MachineFunction *F = BB->getParent();
1334 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
1335 addFrameReference(BuildMI(BB, X86::FNSTCW16m, 4), CWFrameIdx);
1336
1337 // Load the old value of the high byte of the control word...
1338 unsigned OldCW =
1339 F->getSSARegMap()->createVirtualRegister(X86::R16RegisterClass);
1340 addFrameReference(BuildMI(BB, X86::MOV16rm, 4, OldCW), CWFrameIdx);
1341
1342 // Set the high part to be round to zero...
1343 addFrameReference(BuildMI(BB, X86::MOV16mi, 5), CWFrameIdx).addImm(0xC7F);
1344
1345 // Reload the modified control word now...
1346 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1347
1348 // Restore the memory image of control word to original value
1349 addFrameReference(BuildMI(BB, X86::MOV16mr, 5), CWFrameIdx).addReg(OldCW);
1350
1351 // Get the X86 opcode to use.
1352 unsigned Opc;
1353 switch (MI->getOpcode()) {
Chris Lattnerccd2a202006-01-28 10:34:47 +00001354 default: assert(0 && "illegal opcode!");
Evan Cheng911c68d2006-01-16 21:21:29 +00001355 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
1356 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
1357 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
1358 }
1359
1360 X86AddressMode AM;
1361 MachineOperand &Op = MI->getOperand(0);
1362 if (Op.isRegister()) {
1363 AM.BaseType = X86AddressMode::RegBase;
1364 AM.Base.Reg = Op.getReg();
1365 } else {
1366 AM.BaseType = X86AddressMode::FrameIndexBase;
1367 AM.Base.FrameIndex = Op.getFrameIndex();
1368 }
1369 Op = MI->getOperand(1);
1370 if (Op.isImmediate())
1371 AM.Scale = Op.getImmedValue();
1372 Op = MI->getOperand(2);
1373 if (Op.isImmediate())
1374 AM.IndexReg = Op.getImmedValue();
1375 Op = MI->getOperand(3);
1376 if (Op.isGlobalAddress()) {
1377 AM.GV = Op.getGlobal();
1378 } else {
1379 AM.Disp = Op.getImmedValue();
1380 }
1381 addFullAddress(BuildMI(BB, Opc, 5), AM).addReg(MI->getOperand(4).getReg());
1382
1383 // Reload the original control word now.
1384 addFrameReference(BuildMI(BB, X86::FLDCW16m, 4), CWFrameIdx);
1385
1386 delete MI; // The pseudo instruction is gone now.
1387 return BB;
1388 }
1389 }
Evan Cheng339edad2006-01-11 00:33:36 +00001390}
1391
1392
1393//===----------------------------------------------------------------------===//
1394// X86 Custom Lowering Hooks
1395//===----------------------------------------------------------------------===//
1396
Evan Chengaf598d22006-03-13 23:18:16 +00001397/// DarwinGVRequiresExtraLoad - true if accessing the GV requires an extra
1398/// load. For Darwin, external and weak symbols are indirect, loading the value
1399/// at address GV rather then the value of GV itself. This means that the
1400/// GlobalAddress must be in the base or index register of the address, not the
1401/// GV offset field.
1402static bool DarwinGVRequiresExtraLoad(GlobalValue *GV) {
1403 return (GV->hasWeakLinkage() || GV->hasLinkOnceLinkage() ||
1404 (GV->isExternal() && !GV->hasNotBeenReadFromBytecode()));
1405}
1406
Evan Cheng68ad48b2006-03-22 18:59:22 +00001407/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1408/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1409bool X86::isPSHUFDMask(SDNode *N) {
1410 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1411
1412 if (N->getNumOperands() != 4)
1413 return false;
1414
1415 // Check if the value doesn't reference the second vector.
Evan Chengb7fedff2006-03-29 23:07:14 +00001416 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001417 SDOperand Arg = N->getOperand(i);
1418 if (Arg.getOpcode() == ISD::UNDEF) continue;
1419 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1420 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Chengb7fedff2006-03-29 23:07:14 +00001421 return false;
1422 }
1423
1424 return true;
1425}
1426
1427/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001428/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001429bool X86::isPSHUFHWMask(SDNode *N) {
1430 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1431
1432 if (N->getNumOperands() != 8)
1433 return false;
1434
1435 // Lower quadword copied in order.
1436 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001437 SDOperand Arg = N->getOperand(i);
1438 if (Arg.getOpcode() == ISD::UNDEF) continue;
1439 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1440 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001441 return false;
1442 }
1443
1444 // Upper quadword shuffled.
1445 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001446 SDOperand Arg = N->getOperand(i);
1447 if (Arg.getOpcode() == ISD::UNDEF) continue;
1448 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1449 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001450 if (Val < 4 || Val > 7)
1451 return false;
1452 }
1453
1454 return true;
1455}
1456
1457/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng59a63552006-04-05 01:47:37 +00001458/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Chengb7fedff2006-03-29 23:07:14 +00001459bool X86::isPSHUFLWMask(SDNode *N) {
1460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1461
1462 if (N->getNumOperands() != 8)
1463 return false;
1464
1465 // Upper quadword copied in order.
1466 for (unsigned i = 4; i != 8; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001467 SDOperand Arg = N->getOperand(i);
1468 if (Arg.getOpcode() == ISD::UNDEF) continue;
1469 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1470 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Chengb7fedff2006-03-29 23:07:14 +00001471 return false;
1472 }
1473
1474 // Lower quadword shuffled.
1475 for (unsigned i = 0; i != 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001476 SDOperand Arg = N->getOperand(i);
1477 if (Arg.getOpcode() == ISD::UNDEF) continue;
1478 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1479 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001480 if (Val > 4)
1481 return false;
Evan Cheng68ad48b2006-03-22 18:59:22 +00001482 }
1483
1484 return true;
1485}
1486
Evan Chengd27fb3e2006-03-24 01:18:28 +00001487/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1488/// specifies a shuffle of elements that is suitable for input to SHUFP*.
1489bool X86::isSHUFPMask(SDNode *N) {
1490 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1491
Evan Chenge7ee6a52006-03-24 23:15:12 +00001492 unsigned NumElems = N->getNumOperands();
1493 if (NumElems == 2) {
Evan Cheng2595a682006-03-24 02:58:06 +00001494 // The only case that ought be handled by SHUFPD is
1495 // Dest { 2, 1 } <= shuffle( Dest { 1, 0 }, Src { 3, 2 }
1496 // Expect bit 0 == 1, bit1 == 2
1497 SDOperand Bit0 = N->getOperand(0);
Evan Chengebf10062006-04-03 20:53:28 +00001498 if (Bit0.getOpcode() != ISD::UNDEF) {
1499 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1500 if (cast<ConstantSDNode>(Bit0)->getValue() != 1)
1501 return false;
1502 }
1503
Evan Cheng2595a682006-03-24 02:58:06 +00001504 SDOperand Bit1 = N->getOperand(1);
Evan Chengebf10062006-04-03 20:53:28 +00001505 if (Bit1.getOpcode() != ISD::UNDEF) {
1506 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1507 if (cast<ConstantSDNode>(Bit1)->getValue() != 2)
1508 return false;
1509 }
1510
1511 return true;
Evan Cheng2595a682006-03-24 02:58:06 +00001512 }
1513
Evan Chenge7ee6a52006-03-24 23:15:12 +00001514 if (NumElems != 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001515
1516 // Each half must refer to only one of the vector.
Evan Cheng7e2ff112006-03-30 19:54:57 +00001517 for (unsigned i = 0; i < 2; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001518 SDOperand Arg = N->getOperand(i);
1519 if (Arg.getOpcode() == ISD::UNDEF) continue;
1520 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1521 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7e2ff112006-03-30 19:54:57 +00001522 if (Val >= 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001523 }
Evan Cheng7e2ff112006-03-30 19:54:57 +00001524 for (unsigned i = 2; i < 4; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001525 SDOperand Arg = N->getOperand(i);
1526 if (Arg.getOpcode() == ISD::UNDEF) continue;
1527 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1528 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng7e2ff112006-03-30 19:54:57 +00001529 if (Val < 4) return false;
Evan Chengd27fb3e2006-03-24 01:18:28 +00001530 }
1531
1532 return true;
1533}
1534
Evan Cheng2595a682006-03-24 02:58:06 +00001535/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1536/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1537bool X86::isMOVHLPSMask(SDNode *N) {
1538 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1539
Evan Cheng1a194a52006-03-28 06:50:32 +00001540 if (N->getNumOperands() != 4)
Evan Cheng2595a682006-03-24 02:58:06 +00001541 return false;
1542
Evan Cheng1a194a52006-03-28 06:50:32 +00001543 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Cheng2595a682006-03-24 02:58:06 +00001544 SDOperand Bit0 = N->getOperand(0);
1545 SDOperand Bit1 = N->getOperand(1);
Evan Cheng1a194a52006-03-28 06:50:32 +00001546 SDOperand Bit2 = N->getOperand(2);
1547 SDOperand Bit3 = N->getOperand(3);
Evan Cheng99d72052006-03-31 00:30:29 +00001548
1549 if (Bit0.getOpcode() != ISD::UNDEF) {
1550 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1551 if (cast<ConstantSDNode>(Bit0)->getValue() != 6)
1552 return false;
1553 }
1554
1555 if (Bit1.getOpcode() != ISD::UNDEF) {
1556 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1557 if (cast<ConstantSDNode>(Bit1)->getValue() != 7)
1558 return false;
1559 }
1560
1561 if (Bit2.getOpcode() != ISD::UNDEF) {
1562 assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1563 if (cast<ConstantSDNode>(Bit2)->getValue() != 2)
1564 return false;
1565 }
1566
1567 if (Bit3.getOpcode() != ISD::UNDEF) {
1568 assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1569 if (cast<ConstantSDNode>(Bit3)->getValue() != 3)
1570 return false;
1571 }
1572
1573 return true;
Evan Cheng1a194a52006-03-28 06:50:32 +00001574}
1575
1576/// isMOVLHPSMask - Return true if the specified VECTOR_SHUFFLE operand
1577/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1578bool X86::isMOVLHPSMask(SDNode *N) {
1579 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1580
1581 if (N->getNumOperands() != 4)
1582 return false;
1583
1584 // Expect bit0 == 0, bit1 == 1, bit2 == 4, bit3 == 5
1585 SDOperand Bit0 = N->getOperand(0);
1586 SDOperand Bit1 = N->getOperand(1);
1587 SDOperand Bit2 = N->getOperand(2);
1588 SDOperand Bit3 = N->getOperand(3);
Evan Cheng99d72052006-03-31 00:30:29 +00001589
1590 if (Bit0.getOpcode() != ISD::UNDEF) {
1591 assert(isa<ConstantSDNode>(Bit0) && "Invalid VECTOR_SHUFFLE mask!");
1592 if (cast<ConstantSDNode>(Bit0)->getValue() != 0)
1593 return false;
1594 }
1595
1596 if (Bit1.getOpcode() != ISD::UNDEF) {
1597 assert(isa<ConstantSDNode>(Bit1) && "Invalid VECTOR_SHUFFLE mask!");
1598 if (cast<ConstantSDNode>(Bit1)->getValue() != 1)
1599 return false;
1600 }
1601
1602 if (Bit2.getOpcode() != ISD::UNDEF) {
1603 assert(isa<ConstantSDNode>(Bit2) && "Invalid VECTOR_SHUFFLE mask!");
1604 if (cast<ConstantSDNode>(Bit2)->getValue() != 4)
1605 return false;
1606 }
1607
1608 if (Bit3.getOpcode() != ISD::UNDEF) {
1609 assert(isa<ConstantSDNode>(Bit3) && "Invalid VECTOR_SHUFFLE mask!");
1610 if (cast<ConstantSDNode>(Bit3)->getValue() != 5)
1611 return false;
1612 }
1613
1614 return true;
Evan Cheng2595a682006-03-24 02:58:06 +00001615}
1616
Evan Cheng5df75882006-03-28 00:39:58 +00001617/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1618/// specifies a shuffle of elements that is suitable for input to UNPCKL.
1619bool X86::isUNPCKLMask(SDNode *N) {
1620 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1621
1622 unsigned NumElems = N->getNumOperands();
1623 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1624 return false;
1625
1626 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1627 SDOperand BitI = N->getOperand(i);
1628 SDOperand BitI1 = N->getOperand(i+1);
Evan Cheng99d72052006-03-31 00:30:29 +00001629
1630 if (BitI.getOpcode() != ISD::UNDEF) {
1631 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1632 if (cast<ConstantSDNode>(BitI)->getValue() != j)
1633 return false;
1634 }
1635
1636 if (BitI1.getOpcode() != ISD::UNDEF) {
1637 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengd9d0bbb2006-03-31 00:33:57 +00001638 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems)
Evan Cheng99d72052006-03-31 00:30:29 +00001639 return false;
1640 }
Evan Cheng5df75882006-03-28 00:39:58 +00001641 }
1642
1643 return true;
1644}
1645
Evan Cheng2bc32802006-03-28 02:43:26 +00001646/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1647/// specifies a shuffle of elements that is suitable for input to UNPCKH.
1648bool X86::isUNPCKHMask(SDNode *N) {
1649 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1650
1651 unsigned NumElems = N->getNumOperands();
1652 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1653 return false;
1654
1655 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1656 SDOperand BitI = N->getOperand(i);
1657 SDOperand BitI1 = N->getOperand(i+1);
Evan Cheng99d72052006-03-31 00:30:29 +00001658
1659 if (BitI.getOpcode() != ISD::UNDEF) {
1660 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1661 if (cast<ConstantSDNode>(BitI)->getValue() != j + NumElems/2)
1662 return false;
1663 }
1664
1665 if (BitI1.getOpcode() != ISD::UNDEF) {
1666 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
Evan Chengd9d0bbb2006-03-31 00:33:57 +00001667 if (cast<ConstantSDNode>(BitI1)->getValue() != j + NumElems/2 + NumElems)
Evan Cheng99d72052006-03-31 00:30:29 +00001668 return false;
1669 }
Evan Cheng2bc32802006-03-28 02:43:26 +00001670 }
1671
1672 return true;
1673}
1674
Evan Chengf3b52c82006-04-05 07:20:06 +00001675/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1676/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1677/// <0, 0, 1, 1>
1678bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1679 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1680
1681 unsigned NumElems = N->getNumOperands();
1682 if (NumElems != 4 && NumElems != 8 && NumElems != 16)
1683 return false;
1684
1685 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1686 SDOperand BitI = N->getOperand(i);
1687 SDOperand BitI1 = N->getOperand(i+1);
1688
1689 if (BitI.getOpcode() != ISD::UNDEF) {
1690 assert(isa<ConstantSDNode>(BitI) && "Invalid VECTOR_SHUFFLE mask!");
1691 if (cast<ConstantSDNode>(BitI)->getValue() != j)
1692 return false;
1693 }
1694
1695 if (BitI1.getOpcode() != ISD::UNDEF) {
1696 assert(isa<ConstantSDNode>(BitI1) && "Invalid VECTOR_SHUFFLE mask!");
1697 if (cast<ConstantSDNode>(BitI1)->getValue() != j)
1698 return false;
1699 }
1700 }
1701
1702 return true;
1703}
1704
1705
Evan Chengd097e672006-03-22 02:53:00 +00001706/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1707/// a splat of a single element.
1708bool X86::isSplatMask(SDNode *N) {
1709 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1710
1711 // We can only splat 64-bit, and 32-bit quantities.
1712 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1713 return false;
1714
1715 // This is a splat operation if each element of the permute is the same, and
1716 // if the value doesn't reference the second vector.
1717 SDOperand Elt = N->getOperand(0);
1718 assert(isa<ConstantSDNode>(Elt) && "Invalid VECTOR_SHUFFLE mask!");
1719 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001720 SDOperand Arg = N->getOperand(i);
1721 if (Arg.getOpcode() == ISD::UNDEF) continue;
1722 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1723 if (Arg != Elt) return false;
Evan Chengd097e672006-03-22 02:53:00 +00001724 }
1725
1726 // Make sure it is a splat of the first vector operand.
1727 return cast<ConstantSDNode>(Elt)->getValue() < N->getNumOperands();
1728}
1729
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001730/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1731/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1732/// instructions.
1733unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengd097e672006-03-22 02:53:00 +00001734 unsigned NumOperands = N->getNumOperands();
1735 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1736 unsigned Mask = 0;
Evan Cheng8160fd32006-03-28 23:41:33 +00001737 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001738 unsigned Val = 0;
1739 SDOperand Arg = N->getOperand(NumOperands-i-1);
1740 if (Arg.getOpcode() != ISD::UNDEF)
1741 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengd27fb3e2006-03-24 01:18:28 +00001742 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001743 Mask |= Val;
Evan Cheng8160fd32006-03-28 23:41:33 +00001744 if (i != NumOperands - 1)
1745 Mask <<= Shift;
1746 }
Evan Cheng8fdbdf22006-03-22 08:01:21 +00001747
1748 return Mask;
1749}
1750
Evan Chengb7fedff2006-03-29 23:07:14 +00001751/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
1752/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
1753/// instructions.
1754unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
1755 unsigned Mask = 0;
1756 // 8 nodes, but we only care about the last 4.
1757 for (unsigned i = 7; i >= 4; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001758 unsigned Val = 0;
1759 SDOperand Arg = N->getOperand(i);
1760 if (Arg.getOpcode() != ISD::UNDEF)
1761 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001762 Mask |= (Val - 4);
1763 if (i != 4)
1764 Mask <<= 2;
1765 }
1766
1767 return Mask;
1768}
1769
1770/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
1771/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
1772/// instructions.
1773unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
1774 unsigned Mask = 0;
1775 // 8 nodes, but we only care about the first 4.
1776 for (int i = 3; i >= 0; --i) {
Evan Cheng99d72052006-03-31 00:30:29 +00001777 unsigned Val = 0;
1778 SDOperand Arg = N->getOperand(i);
1779 if (Arg.getOpcode() != ISD::UNDEF)
1780 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Chengb7fedff2006-03-29 23:07:14 +00001781 Mask |= Val;
1782 if (i != 0)
1783 Mask <<= 2;
1784 }
1785
1786 return Mask;
1787}
1788
Evan Chengda59b0d2006-03-29 01:30:51 +00001789/// NormalizeVectorShuffle - Swap vector_shuffle operands (as well as
1790/// values in ther permute mask if needed. Use V1 as second vector if it is
1791/// undef. Return an empty SDOperand is it is already well formed.
Evan Cheng2cf42322006-04-05 06:09:26 +00001792static SDOperand NormalizeVectorShuffle(SDOperand Op, SelectionDAG &DAG) {
1793 SDOperand V1 = Op.getOperand(0);
1794 SDOperand V2 = Op.getOperand(1);
1795 SDOperand Mask = Op.getOperand(2);
1796 MVT::ValueType VT = Op.getValueType();
Evan Cheng1a194a52006-03-28 06:50:32 +00001797 unsigned NumElems = Mask.getNumOperands();
1798 SDOperand Half1 = Mask.getOperand(0);
1799 SDOperand Half2 = Mask.getOperand(NumElems/2);
Evan Chengda59b0d2006-03-29 01:30:51 +00001800 bool V2Undef = false;
1801 if (V2.getOpcode() == ISD::UNDEF) {
1802 V2Undef = true;
1803 V2 = V1;
1804 }
1805
Evan Cheng1a194a52006-03-28 06:50:32 +00001806 if (cast<ConstantSDNode>(Half1)->getValue() >= NumElems &&
1807 cast<ConstantSDNode>(Half2)->getValue() < NumElems) {
1808 // Swap the operands and change mask.
1809 std::vector<SDOperand> MaskVec;
1810 for (unsigned i = NumElems / 2; i != NumElems; ++i)
1811 MaskVec.push_back(Mask.getOperand(i));
1812 for (unsigned i = 0; i != NumElems / 2; ++i)
1813 MaskVec.push_back(Mask.getOperand(i));
1814 Mask =
1815 DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(), MaskVec);
1816 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1, Mask);
1817 }
Evan Chengda59b0d2006-03-29 01:30:51 +00001818
1819 if (V2Undef)
1820 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
1821
Evan Cheng2cf42322006-04-05 06:09:26 +00001822 return Op;
Evan Cheng1a194a52006-03-28 06:50:32 +00001823}
1824
Evan Cheng59a63552006-04-05 01:47:37 +00001825/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
1826/// specifies a 8 element shuffle that can be broken into a pair of
1827/// PSHUFHW and PSHUFLW.
1828static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
1829 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1830
1831 if (N->getNumOperands() != 8)
1832 return false;
1833
1834 // Lower quadword shuffled.
1835 for (unsigned i = 0; i != 4; ++i) {
1836 SDOperand Arg = N->getOperand(i);
1837 if (Arg.getOpcode() == ISD::UNDEF) continue;
1838 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1839 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1840 if (Val > 4)
1841 return false;
1842 }
1843
1844 // Upper quadword shuffled.
1845 for (unsigned i = 4; i != 8; ++i) {
1846 SDOperand Arg = N->getOperand(i);
1847 if (Arg.getOpcode() == ISD::UNDEF) continue;
1848 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1849 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1850 if (Val < 4 || Val > 7)
1851 return false;
1852 }
1853
1854 return true;
1855}
1856
Chris Lattner76ac0682005-11-15 00:40:23 +00001857/// LowerOperation - Provide custom lowering hooks for some operations.
1858///
1859SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
1860 switch (Op.getOpcode()) {
1861 default: assert(0 && "Should not custom lower this!");
Evan Cheng9c249c32006-01-09 18:33:28 +00001862 case ISD::SHL_PARTS:
1863 case ISD::SRA_PARTS:
1864 case ISD::SRL_PARTS: {
1865 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
1866 "Not an i64 shift!");
1867 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
1868 SDOperand ShOpLo = Op.getOperand(0);
1869 SDOperand ShOpHi = Op.getOperand(1);
1870 SDOperand ShAmt = Op.getOperand(2);
1871 SDOperand Tmp1 = isSRA ? DAG.getNode(ISD::SRA, MVT::i32, ShOpHi,
Evan Cheng621674a2006-01-18 09:26:46 +00001872 DAG.getConstant(31, MVT::i8))
Evan Cheng9c249c32006-01-09 18:33:28 +00001873 : DAG.getConstant(0, MVT::i32);
1874
1875 SDOperand Tmp2, Tmp3;
1876 if (Op.getOpcode() == ISD::SHL_PARTS) {
1877 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
1878 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
1879 } else {
1880 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Cheng267ba592006-01-19 01:46:14 +00001881 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Cheng9c249c32006-01-09 18:33:28 +00001882 }
1883
1884 SDOperand InFlag = DAG.getNode(X86ISD::TEST, MVT::Flag,
1885 ShAmt, DAG.getConstant(32, MVT::i8));
1886
1887 SDOperand Hi, Lo;
Evan Cheng77fa9192006-01-09 20:49:21 +00001888 SDOperand CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng9c249c32006-01-09 18:33:28 +00001889
1890 std::vector<MVT::ValueType> Tys;
1891 Tys.push_back(MVT::i32);
1892 Tys.push_back(MVT::Flag);
1893 std::vector<SDOperand> Ops;
1894 if (Op.getOpcode() == ISD::SHL_PARTS) {
1895 Ops.push_back(Tmp2);
1896 Ops.push_back(Tmp3);
1897 Ops.push_back(CC);
1898 Ops.push_back(InFlag);
1899 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1900 InFlag = Hi.getValue(1);
1901
1902 Ops.clear();
1903 Ops.push_back(Tmp3);
1904 Ops.push_back(Tmp1);
1905 Ops.push_back(CC);
1906 Ops.push_back(InFlag);
1907 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1908 } else {
1909 Ops.push_back(Tmp2);
1910 Ops.push_back(Tmp3);
1911 Ops.push_back(CC);
Evan Cheng12181af2006-01-09 22:29:54 +00001912 Ops.push_back(InFlag);
Evan Cheng9c249c32006-01-09 18:33:28 +00001913 Lo = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1914 InFlag = Lo.getValue(1);
1915
1916 Ops.clear();
1917 Ops.push_back(Tmp3);
1918 Ops.push_back(Tmp1);
1919 Ops.push_back(CC);
1920 Ops.push_back(InFlag);
1921 Hi = DAG.getNode(X86ISD::CMOV, Tys, Ops);
1922 }
1923
1924 Tys.clear();
1925 Tys.push_back(MVT::i32);
1926 Tys.push_back(MVT::i32);
1927 Ops.clear();
1928 Ops.push_back(Lo);
1929 Ops.push_back(Hi);
1930 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
1931 }
Chris Lattner76ac0682005-11-15 00:40:23 +00001932 case ISD::SINT_TO_FP: {
Evan Cheng08390f62006-01-30 22:13:22 +00001933 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
Evan Cheng6305e502006-01-12 22:54:21 +00001934 Op.getOperand(0).getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001935 "Unknown SINT_TO_FP to lower!");
Evan Cheng6305e502006-01-12 22:54:21 +00001936
1937 SDOperand Result;
1938 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
1939 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
Chris Lattner76ac0682005-11-15 00:40:23 +00001940 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng6305e502006-01-12 22:54:21 +00001941 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Chris Lattner76ac0682005-11-15 00:40:23 +00001942 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng6305e502006-01-12 22:54:21 +00001943 SDOperand Chain = DAG.getNode(ISD::STORE, MVT::Other,
1944 DAG.getEntryNode(), Op.getOperand(0),
1945 StackSlot, DAG.getSrcValue(NULL));
1946
1947 // Build the FILD
1948 std::vector<MVT::ValueType> Tys;
1949 Tys.push_back(MVT::f64);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001950 Tys.push_back(MVT::Other);
Evan Cheng11613a52006-02-04 02:20:30 +00001951 if (X86ScalarSSE) Tys.push_back(MVT::Flag);
Chris Lattner76ac0682005-11-15 00:40:23 +00001952 std::vector<SDOperand> Ops;
Evan Cheng6305e502006-01-12 22:54:21 +00001953 Ops.push_back(Chain);
Chris Lattner76ac0682005-11-15 00:40:23 +00001954 Ops.push_back(StackSlot);
Evan Cheng6305e502006-01-12 22:54:21 +00001955 Ops.push_back(DAG.getValueType(SrcVT));
Evan Cheng11613a52006-02-04 02:20:30 +00001956 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
1957 Tys, Ops);
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001958
1959 if (X86ScalarSSE) {
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001960 Chain = Result.getValue(1);
1961 SDOperand InFlag = Result.getValue(2);
1962
Evan Cheng11613a52006-02-04 02:20:30 +00001963 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001964 // shouldn't be necessary except that RFP cannot be live across
1965 // multiple blocks. When stackifier is fixed, they can be uncoupled.
1966 MachineFunction &MF = DAG.getMachineFunction();
1967 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
1968 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1969 std::vector<MVT::ValueType> Tys;
1970 Tys.push_back(MVT::Other);
1971 std::vector<SDOperand> Ops;
1972 Ops.push_back(Chain);
1973 Ops.push_back(Result);
1974 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00001975 Ops.push_back(DAG.getValueType(Op.getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00001976 Ops.push_back(InFlag);
1977 Chain = DAG.getNode(X86ISD::FST, Tys, Ops);
1978 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
1979 DAG.getSrcValue(NULL));
1980 }
1981
Evan Cheng6305e502006-01-12 22:54:21 +00001982 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00001983 }
1984 case ISD::FP_TO_SINT: {
1985 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
Chris Lattner76ac0682005-11-15 00:40:23 +00001986 "Unknown FP_TO_SINT to lower!");
1987 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
1988 // stack slot.
1989 MachineFunction &MF = DAG.getMachineFunction();
1990 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
1991 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
1992 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
1993
1994 unsigned Opc;
1995 switch (Op.getValueType()) {
1996 default: assert(0 && "Invalid FP_TO_SINT to lower!");
1997 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
1998 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
1999 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
2000 }
2001
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002002 SDOperand Chain = DAG.getEntryNode();
2003 SDOperand Value = Op.getOperand(0);
2004 if (X86ScalarSSE) {
2005 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
2006 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value, StackSlot,
2007 DAG.getSrcValue(0));
2008 std::vector<MVT::ValueType> Tys;
2009 Tys.push_back(MVT::f64);
2010 Tys.push_back(MVT::Other);
2011 std::vector<SDOperand> Ops;
2012 Ops.push_back(Chain);
2013 Ops.push_back(StackSlot);
Evan Cheng08390f62006-01-30 22:13:22 +00002014 Ops.push_back(DAG.getValueType(Op.getOperand(0).getValueType()));
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002015 Value = DAG.getNode(X86ISD::FLD, Tys, Ops);
2016 Chain = Value.getValue(1);
2017 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
2018 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
2019 }
2020
Chris Lattner76ac0682005-11-15 00:40:23 +00002021 // Build the FP_TO_INT*_IN_MEM
2022 std::vector<SDOperand> Ops;
Evan Cheng5b97fcf2006-01-30 08:02:57 +00002023 Ops.push_back(Chain);
2024 Ops.push_back(Value);
Chris Lattner76ac0682005-11-15 00:40:23 +00002025 Ops.push_back(StackSlot);
2026 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops);
2027
2028 // Load the result.
2029 return DAG.getLoad(Op.getValueType(), FIST, StackSlot,
2030 DAG.getSrcValue(NULL));
2031 }
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00002032 case ISD::READCYCLECOUNTER: {
Chris Lattner6df9e112005-11-20 22:01:40 +00002033 std::vector<MVT::ValueType> Tys;
2034 Tys.push_back(MVT::Other);
2035 Tys.push_back(MVT::Flag);
2036 std::vector<SDOperand> Ops;
2037 Ops.push_back(Op.getOperand(0));
2038 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, Ops);
Chris Lattner6c1ca882005-11-20 22:57:19 +00002039 Ops.clear();
2040 Ops.push_back(DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1)));
2041 Ops.push_back(DAG.getCopyFromReg(Ops[0].getValue(1), X86::EDX,
2042 MVT::i32, Ops[0].getValue(2)));
2043 Ops.push_back(Ops[1].getValue(1));
2044 Tys[0] = Tys[1] = MVT::i32;
2045 Tys.push_back(MVT::Other);
2046 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops);
Andrew Lenharth0bf68ae2005-11-20 21:41:10 +00002047 }
Evan Cheng2dd217b2006-01-31 03:14:29 +00002048 case ISD::FABS: {
2049 MVT::ValueType VT = Op.getValueType();
Evan Cheng72d5c252006-01-31 22:28:30 +00002050 const Type *OpNTy = MVT::getTypeForValueType(VT);
2051 std::vector<Constant*> CV;
2052 if (VT == MVT::f64) {
2053 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
2054 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2055 } else {
2056 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
2057 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2058 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2059 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2060 }
2061 Constant *CS = ConstantStruct::get(CV);
2062 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2063 SDOperand Mask
2064 = DAG.getNode(X86ISD::LOAD_PACK,
2065 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
Evan Cheng2dd217b2006-01-31 03:14:29 +00002066 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
2067 }
Evan Cheng72d5c252006-01-31 22:28:30 +00002068 case ISD::FNEG: {
2069 MVT::ValueType VT = Op.getValueType();
2070 const Type *OpNTy = MVT::getTypeForValueType(VT);
2071 std::vector<Constant*> CV;
2072 if (VT == MVT::f64) {
2073 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
2074 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2075 } else {
2076 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
2077 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2078 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2079 CV.push_back(ConstantFP::get(OpNTy, 0.0));
2080 }
2081 Constant *CS = ConstantStruct::get(CV);
2082 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
2083 SDOperand Mask
2084 = DAG.getNode(X86ISD::LOAD_PACK,
2085 VT, DAG.getEntryNode(), CPIdx, DAG.getSrcValue(NULL));
2086 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
2087 }
Evan Chengc1583db2005-12-21 20:21:51 +00002088 case ISD::SETCC: {
2089 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng45df7f82006-01-30 23:41:35 +00002090 SDOperand Cond;
2091 SDOperand CC = Op.getOperand(2);
Evan Cheng172fce72006-01-06 00:43:03 +00002092 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
2093 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng45df7f82006-01-30 23:41:35 +00002094 bool Flip;
2095 unsigned X86CC;
2096 if (translateX86CC(CC, isFP, X86CC, Flip)) {
2097 if (Flip)
2098 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2099 Op.getOperand(1), Op.getOperand(0));
2100 else
2101 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2102 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00002103 return DAG.getNode(X86ISD::SETCC, MVT::i8,
2104 DAG.getConstant(X86CC, MVT::i8), Cond);
2105 } else {
2106 assert(isFP && "Illegal integer SetCC!");
2107
Evan Cheng45df7f82006-01-30 23:41:35 +00002108 Cond = DAG.getNode(X86ISD::CMP, MVT::Flag,
2109 Op.getOperand(0), Op.getOperand(1));
Evan Cheng172fce72006-01-06 00:43:03 +00002110 std::vector<MVT::ValueType> Tys;
2111 std::vector<SDOperand> Ops;
2112 switch (SetCCOpcode) {
2113 default: assert(false && "Illegal floating point SetCC!");
2114 case ISD::SETOEQ: { // !PF & ZF
2115 Tys.push_back(MVT::i8);
2116 Tys.push_back(MVT::Flag);
2117 Ops.push_back(DAG.getConstant(X86ISD::COND_NP, MVT::i8));
2118 Ops.push_back(Cond);
2119 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2120 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2121 DAG.getConstant(X86ISD::COND_E, MVT::i8),
2122 Tmp1.getValue(1));
2123 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
2124 }
Evan Cheng172fce72006-01-06 00:43:03 +00002125 case ISD::SETUNE: { // PF | !ZF
2126 Tys.push_back(MVT::i8);
2127 Tys.push_back(MVT::Flag);
2128 Ops.push_back(DAG.getConstant(X86ISD::COND_P, MVT::i8));
2129 Ops.push_back(Cond);
2130 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2131 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
2132 DAG.getConstant(X86ISD::COND_NE, MVT::i8),
2133 Tmp1.getValue(1));
2134 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
2135 }
2136 }
2137 }
Evan Chengc1583db2005-12-21 20:21:51 +00002138 }
Evan Cheng225a4d02005-12-17 01:21:05 +00002139 case ISD::SELECT: {
Evan Cheng73a1ad92006-01-10 20:26:56 +00002140 MVT::ValueType VT = Op.getValueType();
2141 bool isFP = MVT::isFloatingPoint(VT);
Evan Chengcde9e302006-01-27 08:10:46 +00002142 bool isFPStack = isFP && !X86ScalarSSE;
2143 bool isFPSSE = isFP && X86ScalarSSE;
Evan Chengfb22e862006-01-13 01:03:02 +00002144 bool addTest = false;
Evan Cheng73a1ad92006-01-10 20:26:56 +00002145 SDOperand Op0 = Op.getOperand(0);
2146 SDOperand Cond, CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00002147 if (Op0.getOpcode() == ISD::SETCC)
2148 Op0 = LowerOperation(Op0, DAG);
2149
Evan Cheng73a1ad92006-01-10 20:26:56 +00002150 if (Op0.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00002151 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2152 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2153 // have another use it will be eliminated.
2154 // If the X86ISD::SETCC has more than one use, then it's probably better
2155 // to use a test instead of duplicating the X86ISD::CMP (for register
2156 // pressure reason).
Evan Cheng78038292006-04-05 23:38:46 +00002157 unsigned CmpOpc = Op0.getOperand(1).getOpcode();
2158 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
2159 CmpOpc == X86ISD::UCOMI) {
Evan Cheng944d1e92006-01-26 02:13:10 +00002160 if (!Op0.hasOneUse()) {
2161 std::vector<MVT::ValueType> Tys;
2162 for (unsigned i = 0; i < Op0.Val->getNumValues(); ++i)
2163 Tys.push_back(Op0.Val->getValueType(i));
2164 std::vector<SDOperand> Ops;
2165 for (unsigned i = 0; i < Op0.getNumOperands(); ++i)
2166 Ops.push_back(Op0.getOperand(i));
2167 Op0 = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2168 }
2169
Evan Chengfb22e862006-01-13 01:03:02 +00002170 CC = Op0.getOperand(0);
2171 Cond = Op0.getOperand(1);
Evan Chengaff08002006-01-25 09:05:09 +00002172 // Make a copy as flag result cannot be used by more than one.
Evan Cheng78038292006-04-05 23:38:46 +00002173 Cond = DAG.getNode(CmpOpc, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00002174 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00002175 addTest =
Evan Chengd7faa4b2006-01-13 01:17:24 +00002176 isFPStack && !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Evan Chengfb22e862006-01-13 01:03:02 +00002177 } else
2178 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00002179 } else
2180 addTest = true;
Evan Cheng73a1ad92006-01-10 20:26:56 +00002181
Evan Cheng731423f2006-01-13 01:06:49 +00002182 if (addTest) {
Evan Chengdba84bb2006-01-13 19:51:46 +00002183 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng73a1ad92006-01-10 20:26:56 +00002184 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Op0, Op0);
Evan Cheng225a4d02005-12-17 01:21:05 +00002185 }
Evan Cheng9c249c32006-01-09 18:33:28 +00002186
2187 std::vector<MVT::ValueType> Tys;
2188 Tys.push_back(Op.getValueType());
2189 Tys.push_back(MVT::Flag);
2190 std::vector<SDOperand> Ops;
Evan Chengdba84bb2006-01-13 19:51:46 +00002191 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
2192 // condition is true.
Evan Cheng9c249c32006-01-09 18:33:28 +00002193 Ops.push_back(Op.getOperand(2));
Evan Chengdba84bb2006-01-13 19:51:46 +00002194 Ops.push_back(Op.getOperand(1));
Evan Cheng9c249c32006-01-09 18:33:28 +00002195 Ops.push_back(CC);
2196 Ops.push_back(Cond);
2197 return DAG.getNode(X86ISD::CMOV, Tys, Ops);
Evan Cheng225a4d02005-12-17 01:21:05 +00002198 }
Evan Cheng6fc31042005-12-19 23:12:38 +00002199 case ISD::BRCOND: {
Evan Chengfb22e862006-01-13 01:03:02 +00002200 bool addTest = false;
Evan Cheng6fc31042005-12-19 23:12:38 +00002201 SDOperand Cond = Op.getOperand(1);
2202 SDOperand Dest = Op.getOperand(2);
2203 SDOperand CC;
Evan Cheng45df7f82006-01-30 23:41:35 +00002204 if (Cond.getOpcode() == ISD::SETCC)
2205 Cond = LowerOperation(Cond, DAG);
2206
Evan Chengc1583db2005-12-21 20:21:51 +00002207 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Chengfb22e862006-01-13 01:03:02 +00002208 // If condition flag is set by a X86ISD::CMP, then make a copy of it
2209 // (since flag operand cannot be shared). If the X86ISD::SETCC does not
2210 // have another use it will be eliminated.
2211 // If the X86ISD::SETCC has more than one use, then it's probably better
2212 // to use a test instead of duplicating the X86ISD::CMP (for register
2213 // pressure reason).
Evan Cheng78038292006-04-05 23:38:46 +00002214 unsigned CmpOpc = Cond.getOperand(1).getOpcode();
2215 if (CmpOpc == X86ISD::CMP || CmpOpc == X86ISD::COMI ||
2216 CmpOpc == X86ISD::UCOMI) {
Evan Cheng944d1e92006-01-26 02:13:10 +00002217 if (!Cond.hasOneUse()) {
2218 std::vector<MVT::ValueType> Tys;
2219 for (unsigned i = 0; i < Cond.Val->getNumValues(); ++i)
2220 Tys.push_back(Cond.Val->getValueType(i));
2221 std::vector<SDOperand> Ops;
2222 for (unsigned i = 0; i < Cond.getNumOperands(); ++i)
2223 Ops.push_back(Cond.getOperand(i));
2224 Cond = DAG.getNode(X86ISD::SETCC, Tys, Ops);
2225 }
2226
Evan Chengfb22e862006-01-13 01:03:02 +00002227 CC = Cond.getOperand(0);
Evan Chengaff08002006-01-25 09:05:09 +00002228 Cond = Cond.getOperand(1);
2229 // Make a copy as flag result cannot be used by more than one.
Evan Cheng78038292006-04-05 23:38:46 +00002230 Cond = DAG.getNode(CmpOpc, MVT::Flag,
Evan Chengaff08002006-01-25 09:05:09 +00002231 Cond.getOperand(0), Cond.getOperand(1));
Evan Chengfb22e862006-01-13 01:03:02 +00002232 } else
2233 addTest = true;
Evan Chengfb22e862006-01-13 01:03:02 +00002234 } else
2235 addTest = true;
2236
2237 if (addTest) {
Evan Cheng172fce72006-01-06 00:43:03 +00002238 CC = DAG.getConstant(X86ISD::COND_NE, MVT::i8);
Evan Cheng6fc31042005-12-19 23:12:38 +00002239 Cond = DAG.getNode(X86ISD::TEST, MVT::Flag, Cond, Cond);
2240 }
2241 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
2242 Op.getOperand(0), Op.getOperand(2), CC, Cond);
2243 }
Evan Chengae986f12006-01-11 22:15:48 +00002244 case ISD::MEMSET: {
Evan Cheng6dc73292006-03-04 02:48:56 +00002245 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00002246 SDOperand Chain = Op.getOperand(0);
2247 unsigned Align =
2248 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2249 if (Align == 0) Align = 1;
2250
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002251 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2252 // If not DWORD aligned, call memset if size is less than the threshold.
2253 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00002254 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00002255 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002256 MVT::ValueType IntPtr = getPointerTy();
2257 const Type *IntPtrTy = getTargetData().getIntPtrType();
2258 std::vector<std::pair<SDOperand, const Type*> > Args;
2259 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2260 // Extend the ubyte argument to be an int value for the call.
2261 SDOperand Val = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
2262 Args.push_back(std::make_pair(Val, IntPtrTy));
2263 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2264 std::pair<SDOperand,SDOperand> CallResult =
2265 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2266 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
2267 return CallResult.second;
2268 }
2269
Evan Chengae986f12006-01-11 22:15:48 +00002270 MVT::ValueType AVT;
2271 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00002272 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
2273 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00002274 bool TwoRepStos = false;
Evan Cheng6dc73292006-03-04 02:48:56 +00002275 if (ValC) {
Evan Chengae986f12006-01-11 22:15:48 +00002276 unsigned ValReg;
2277 unsigned Val = ValC->getValue() & 255;
2278
2279 // If the value is a constant, then we can potentially use larger sets.
2280 switch (Align & 3) {
2281 case 2: // WORD aligned
2282 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00002283 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2284 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00002285 Val = (Val << 8) | Val;
2286 ValReg = X86::AX;
2287 break;
2288 case 0: // DWORD aligned
2289 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00002290 if (I) {
2291 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2292 BytesLeft = I->getValue() % 4;
2293 } else {
2294 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2295 DAG.getConstant(2, MVT::i8));
2296 TwoRepStos = true;
2297 }
Evan Chengae986f12006-01-11 22:15:48 +00002298 Val = (Val << 8) | Val;
2299 Val = (Val << 16) | Val;
2300 ValReg = X86::EAX;
2301 break;
2302 default: // Byte aligned
2303 AVT = MVT::i8;
2304 Count = Op.getOperand(3);
2305 ValReg = X86::AL;
2306 break;
2307 }
2308
2309 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
2310 InFlag);
2311 InFlag = Chain.getValue(1);
2312 } else {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002313 AVT = MVT::i8;
Evan Chengae986f12006-01-11 22:15:48 +00002314 Count = Op.getOperand(3);
2315 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
2316 InFlag = Chain.getValue(1);
2317 }
2318
2319 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2320 InFlag = Chain.getValue(1);
2321 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2322 InFlag = Chain.getValue(1);
2323
Evan Chengadc70932006-03-07 23:29:39 +00002324 std::vector<MVT::ValueType> Tys;
2325 Tys.push_back(MVT::Other);
2326 Tys.push_back(MVT::Flag);
2327 std::vector<SDOperand> Ops;
2328 Ops.push_back(Chain);
2329 Ops.push_back(DAG.getValueType(AVT));
2330 Ops.push_back(InFlag);
2331 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2332
2333 if (TwoRepStos) {
2334 InFlag = Chain.getValue(1);
2335 Count = Op.getOperand(3);
2336 MVT::ValueType CVT = Count.getValueType();
2337 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2338 DAG.getConstant(3, CVT));
2339 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2340 InFlag = Chain.getValue(1);
2341 Tys.clear();
2342 Tys.push_back(MVT::Other);
2343 Tys.push_back(MVT::Flag);
2344 Ops.clear();
2345 Ops.push_back(Chain);
2346 Ops.push_back(DAG.getValueType(MVT::i8));
2347 Ops.push_back(InFlag);
2348 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, Ops);
2349 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00002350 // Issue stores for the last 1 - 3 bytes.
2351 SDOperand Value;
2352 unsigned Val = ValC->getValue() & 255;
2353 unsigned Offset = I->getValue() - BytesLeft;
2354 SDOperand DstAddr = Op.getOperand(1);
2355 MVT::ValueType AddrVT = DstAddr.getValueType();
2356 if (BytesLeft >= 2) {
2357 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
2358 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2359 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2360 DAG.getConstant(Offset, AddrVT)),
2361 DAG.getSrcValue(NULL));
2362 BytesLeft -= 2;
2363 Offset += 2;
2364 }
2365
2366 if (BytesLeft == 1) {
2367 Value = DAG.getConstant(Val, MVT::i8);
2368 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2369 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
2370 DAG.getConstant(Offset, AddrVT)),
2371 DAG.getSrcValue(NULL));
2372 }
2373 }
2374
2375 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002376 }
2377 case ISD::MEMCPY: {
2378 SDOperand Chain = Op.getOperand(0);
2379 unsigned Align =
2380 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
2381 if (Align == 0) Align = 1;
2382
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002383 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
2384 // If not DWORD aligned, call memcpy if size is less than the threshold.
2385 // It knows how to align to the right boundary first.
Evan Cheng6dc73292006-03-04 02:48:56 +00002386 if ((Align & 3) != 0 ||
Evan Chengadc70932006-03-07 23:29:39 +00002387 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng03c1e6f2006-02-16 00:21:07 +00002388 MVT::ValueType IntPtr = getPointerTy();
2389 const Type *IntPtrTy = getTargetData().getIntPtrType();
2390 std::vector<std::pair<SDOperand, const Type*> > Args;
2391 Args.push_back(std::make_pair(Op.getOperand(1), IntPtrTy));
2392 Args.push_back(std::make_pair(Op.getOperand(2), IntPtrTy));
2393 Args.push_back(std::make_pair(Op.getOperand(3), IntPtrTy));
2394 std::pair<SDOperand,SDOperand> CallResult =
2395 LowerCallTo(Chain, Type::VoidTy, false, CallingConv::C, false,
2396 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
2397 return CallResult.second;
2398 }
2399
Evan Chengae986f12006-01-11 22:15:48 +00002400 MVT::ValueType AVT;
2401 SDOperand Count;
Evan Cheng6dc73292006-03-04 02:48:56 +00002402 unsigned BytesLeft = 0;
Evan Chengadc70932006-03-07 23:29:39 +00002403 bool TwoRepMovs = false;
Evan Chengae986f12006-01-11 22:15:48 +00002404 switch (Align & 3) {
2405 case 2: // WORD aligned
2406 AVT = MVT::i16;
Evan Cheng6dc73292006-03-04 02:48:56 +00002407 Count = DAG.getConstant(I->getValue() / 2, MVT::i32);
2408 BytesLeft = I->getValue() % 2;
Evan Chengae986f12006-01-11 22:15:48 +00002409 break;
2410 case 0: // DWORD aligned
2411 AVT = MVT::i32;
Evan Chengadc70932006-03-07 23:29:39 +00002412 if (I) {
2413 Count = DAG.getConstant(I->getValue() / 4, MVT::i32);
2414 BytesLeft = I->getValue() % 4;
2415 } else {
2416 Count = DAG.getNode(ISD::SRL, MVT::i32, Op.getOperand(3),
2417 DAG.getConstant(2, MVT::i8));
2418 TwoRepMovs = true;
2419 }
Evan Chengae986f12006-01-11 22:15:48 +00002420 break;
2421 default: // Byte aligned
2422 AVT = MVT::i8;
2423 Count = Op.getOperand(3);
2424 break;
2425 }
2426
Evan Cheng6dc73292006-03-04 02:48:56 +00002427 SDOperand InFlag(0, 0);
Evan Chengae986f12006-01-11 22:15:48 +00002428 Chain = DAG.getCopyToReg(Chain, X86::ECX, Count, InFlag);
2429 InFlag = Chain.getValue(1);
2430 Chain = DAG.getCopyToReg(Chain, X86::EDI, Op.getOperand(1), InFlag);
2431 InFlag = Chain.getValue(1);
2432 Chain = DAG.getCopyToReg(Chain, X86::ESI, Op.getOperand(2), InFlag);
2433 InFlag = Chain.getValue(1);
2434
Evan Chengadc70932006-03-07 23:29:39 +00002435 std::vector<MVT::ValueType> Tys;
2436 Tys.push_back(MVT::Other);
2437 Tys.push_back(MVT::Flag);
2438 std::vector<SDOperand> Ops;
2439 Ops.push_back(Chain);
2440 Ops.push_back(DAG.getValueType(AVT));
2441 Ops.push_back(InFlag);
2442 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2443
2444 if (TwoRepMovs) {
2445 InFlag = Chain.getValue(1);
2446 Count = Op.getOperand(3);
2447 MVT::ValueType CVT = Count.getValueType();
2448 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
2449 DAG.getConstant(3, CVT));
2450 Chain = DAG.getCopyToReg(Chain, X86::ECX, Left, InFlag);
2451 InFlag = Chain.getValue(1);
2452 Tys.clear();
2453 Tys.push_back(MVT::Other);
2454 Tys.push_back(MVT::Flag);
2455 Ops.clear();
2456 Ops.push_back(Chain);
2457 Ops.push_back(DAG.getValueType(MVT::i8));
2458 Ops.push_back(InFlag);
2459 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, Ops);
2460 } else if (BytesLeft) {
Evan Cheng6dc73292006-03-04 02:48:56 +00002461 // Issue loads and stores for the last 1 - 3 bytes.
2462 unsigned Offset = I->getValue() - BytesLeft;
2463 SDOperand DstAddr = Op.getOperand(1);
2464 MVT::ValueType DstVT = DstAddr.getValueType();
2465 SDOperand SrcAddr = Op.getOperand(2);
2466 MVT::ValueType SrcVT = SrcAddr.getValueType();
2467 SDOperand Value;
2468 if (BytesLeft >= 2) {
2469 Value = DAG.getLoad(MVT::i16, Chain,
2470 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2471 DAG.getConstant(Offset, SrcVT)),
2472 DAG.getSrcValue(NULL));
2473 Chain = Value.getValue(1);
2474 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2475 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2476 DAG.getConstant(Offset, DstVT)),
2477 DAG.getSrcValue(NULL));
2478 BytesLeft -= 2;
2479 Offset += 2;
2480 }
2481
2482 if (BytesLeft == 1) {
2483 Value = DAG.getLoad(MVT::i8, Chain,
2484 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
2485 DAG.getConstant(Offset, SrcVT)),
2486 DAG.getSrcValue(NULL));
2487 Chain = Value.getValue(1);
2488 Chain = DAG.getNode(ISD::STORE, MVT::Other, Chain, Value,
2489 DAG.getNode(ISD::ADD, DstVT, DstAddr,
2490 DAG.getConstant(Offset, DstVT)),
2491 DAG.getSrcValue(NULL));
2492 }
2493 }
2494
2495 return Chain;
Evan Chengae986f12006-01-11 22:15:48 +00002496 }
Evan Cheng99470012006-02-25 09:55:19 +00002497
2498 // ConstantPool, GlobalAddress, and ExternalSymbol are lowered as their
2499 // target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2500 // one of the above mentioned nodes. It has to be wrapped because otherwise
2501 // Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2502 // be used to form addressing mode. These wrapped nodes will be selected
2503 // into MOV32ri.
Evan Cheng5588de92006-02-18 00:15:05 +00002504 case ISD::ConstantPool: {
2505 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002506 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2507 DAG.getTargetConstantPool(CP->get(), getPointerTy(),
2508 CP->getAlignment()));
Evan Chengbc047222006-03-22 19:22:18 +00002509 if (Subtarget->isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002510 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002511 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng5588de92006-02-18 00:15:05 +00002512 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2513 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2514 }
2515
2516 return Result;
2517 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002518 case ISD::GlobalAddress: {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002519 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
2520 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2521 DAG.getTargetGlobalAddress(GV, getPointerTy()));
Evan Chengbc047222006-03-22 19:22:18 +00002522 if (Subtarget->isTargetDarwin()) {
Evan Cheng5588de92006-02-18 00:15:05 +00002523 // With PIC, the address is actually $g + Offset.
Evan Cheng73136df2006-02-22 20:19:42 +00002524 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
Evan Cheng1f342c22006-02-23 02:43:52 +00002525 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2526 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
Evan Cheng5588de92006-02-18 00:15:05 +00002527
2528 // For Darwin, external and weak symbols are indirect, so we want to load
Evan Chengaf598d22006-03-13 23:18:16 +00002529 // the value at address GV, not the value of GV itself. This means that
Evan Cheng5588de92006-02-18 00:15:05 +00002530 // the GlobalAddress must be in the base or index register of the address,
2531 // not the GV offset field.
Evan Cheng73136df2006-02-22 20:19:42 +00002532 if (getTargetMachine().getRelocationModel() != Reloc::Static &&
Evan Chengaf598d22006-03-13 23:18:16 +00002533 DarwinGVRequiresExtraLoad(GV))
Evan Cheng5a766802006-02-07 08:38:37 +00002534 Result = DAG.getLoad(MVT::i32, DAG.getEntryNode(),
Evan Cheng1f342c22006-02-23 02:43:52 +00002535 Result, DAG.getSrcValue(NULL));
Evan Cheng5a766802006-02-07 08:38:37 +00002536 }
Evan Cheng5588de92006-02-18 00:15:05 +00002537
Evan Chengb94db9e2006-01-12 07:56:47 +00002538 return Result;
Chris Lattner76ac0682005-11-15 00:40:23 +00002539 }
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002540 case ISD::ExternalSymbol: {
2541 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
2542 SDOperand Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(),
2543 DAG.getTargetExternalSymbol(Sym, getPointerTy()));
Evan Chengbc047222006-03-22 19:22:18 +00002544 if (Subtarget->isTargetDarwin()) {
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002545 // With PIC, the address is actually $g + Offset.
2546 if (getTargetMachine().getRelocationModel() == Reloc::PIC)
2547 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2548 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()), Result);
2549 }
2550
2551 return Result;
2552 }
Nate Begemane74795c2006-01-25 18:21:52 +00002553 case ISD::VASTART: {
2554 // vastart just stores the address of the VarArgsFrameIndex slot into the
2555 // memory location argument.
2556 // FIXME: Replace MVT::i32 with PointerTy
2557 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, MVT::i32);
2558 return DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0), FR,
2559 Op.getOperand(1), Op.getOperand(2));
2560 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002561 case ISD::RET: {
2562 SDOperand Copy;
2563
2564 switch(Op.getNumOperands()) {
2565 default:
2566 assert(0 && "Do not know how to return this many arguments!");
2567 abort();
2568 case 1:
2569 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Op.getOperand(0),
2570 DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
2571 case 2: {
2572 MVT::ValueType ArgVT = Op.getOperand(1).getValueType();
2573 if (MVT::isInteger(ArgVT))
2574 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EAX, Op.getOperand(1),
2575 SDOperand());
2576 else if (!X86ScalarSSE) {
2577 std::vector<MVT::ValueType> Tys;
2578 Tys.push_back(MVT::Other);
2579 Tys.push_back(MVT::Flag);
2580 std::vector<SDOperand> Ops;
2581 Ops.push_back(Op.getOperand(0));
2582 Ops.push_back(Op.getOperand(1));
2583 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2584 } else {
Evan Chenge1ce4d72006-02-01 00:20:21 +00002585 SDOperand MemLoc;
2586 SDOperand Chain = Op.getOperand(0);
Evan Cheng5659ca82006-01-31 23:19:54 +00002587 SDOperand Value = Op.getOperand(1);
2588
Evan Chenga24617f2006-02-01 01:19:32 +00002589 if (Value.getOpcode() == ISD::LOAD &&
2590 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
Evan Cheng5659ca82006-01-31 23:19:54 +00002591 Chain = Value.getOperand(0);
2592 MemLoc = Value.getOperand(1);
2593 } else {
2594 // Spill the value to memory and reload it into top of stack.
2595 unsigned Size = MVT::getSizeInBits(ArgVT)/8;
2596 MachineFunction &MF = DAG.getMachineFunction();
2597 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
2598 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
2599 Chain = DAG.getNode(ISD::STORE, MVT::Other, Op.getOperand(0),
2600 Value, MemLoc, DAG.getSrcValue(0));
2601 }
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002602 std::vector<MVT::ValueType> Tys;
2603 Tys.push_back(MVT::f64);
2604 Tys.push_back(MVT::Other);
2605 std::vector<SDOperand> Ops;
2606 Ops.push_back(Chain);
Evan Cheng5659ca82006-01-31 23:19:54 +00002607 Ops.push_back(MemLoc);
Nate Begeman8c47c3a2006-01-27 21:09:22 +00002608 Ops.push_back(DAG.getValueType(ArgVT));
2609 Copy = DAG.getNode(X86ISD::FLD, Tys, Ops);
2610 Tys.clear();
2611 Tys.push_back(MVT::Other);
2612 Tys.push_back(MVT::Flag);
2613 Ops.clear();
2614 Ops.push_back(Copy.getValue(1));
2615 Ops.push_back(Copy);
2616 Copy = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops);
2617 }
2618 break;
2619 }
2620 case 3:
2621 Copy = DAG.getCopyToReg(Op.getOperand(0), X86::EDX, Op.getOperand(2),
2622 SDOperand());
2623 Copy = DAG.getCopyToReg(Copy, X86::EAX,Op.getOperand(1),Copy.getValue(1));
2624 break;
2625 }
2626 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other,
2627 Copy, DAG.getConstant(getBytesToPopOnReturn(), MVT::i16),
2628 Copy.getValue(1));
2629 }
Evan Chengd5e905d2006-03-21 23:01:21 +00002630 case ISD::SCALAR_TO_VECTOR: {
2631 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Evan Chenge7ee6a52006-03-24 23:15:12 +00002632 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
Evan Chengd5e905d2006-03-21 23:01:21 +00002633 }
Evan Chengd097e672006-03-22 02:53:00 +00002634 case ISD::VECTOR_SHUFFLE: {
2635 SDOperand V1 = Op.getOperand(0);
2636 SDOperand V2 = Op.getOperand(1);
2637 SDOperand PermMask = Op.getOperand(2);
2638 MVT::ValueType VT = Op.getValueType();
Evan Cheng2595a682006-03-24 02:58:06 +00002639 unsigned NumElems = PermMask.getNumOperands();
Evan Chengd097e672006-03-22 02:53:00 +00002640
Evan Chengacc33642006-03-29 19:02:40 +00002641 // Splat && PSHUFD's 2nd vector must be undef.
Evan Cheng7e2ff112006-03-30 19:54:57 +00002642 if (X86::isSplatMask(PermMask.Val)) {
Evan Cheng500ec162006-03-29 03:04:49 +00002643 if (V2.getOpcode() != ISD::UNDEF)
Evan Chengda59b0d2006-03-29 01:30:51 +00002644 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
Evan Cheng500ec162006-03-29 03:04:49 +00002645 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002646 return Op;
Evan Cheng500ec162006-03-29 03:04:49 +00002647 }
Evan Chengda59b0d2006-03-29 01:30:51 +00002648
Evan Chengacc33642006-03-29 19:02:40 +00002649 if (X86::isUNPCKLMask(PermMask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00002650 X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Evan Chengacc33642006-03-29 19:02:40 +00002651 X86::isUNPCKHMask(PermMask.Val))
2652 // Leave the VECTOR_SHUFFLE alone. It matches {P}UNPCKL*.
Evan Cheng2cf42322006-04-05 06:09:26 +00002653 return Op;
Evan Chengacc33642006-03-29 19:02:40 +00002654
Evan Cheng7e2ff112006-03-30 19:54:57 +00002655 if (NumElems == 2)
Evan Cheng2cf42322006-04-05 06:09:26 +00002656 return NormalizeVectorShuffle(Op, DAG);
Evan Cheng7e2ff112006-03-30 19:54:57 +00002657
2658 // If VT is integer, try PSHUF* first, then SHUFP*.
2659 if (MVT::isInteger(VT)) {
2660 if (X86::isPSHUFDMask(PermMask.Val) ||
2661 X86::isPSHUFHWMask(PermMask.Val) ||
2662 X86::isPSHUFLWMask(PermMask.Val)) {
2663 if (V2.getOpcode() != ISD::UNDEF)
2664 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2665 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002666 return Op;
Evan Cheng7e2ff112006-03-30 19:54:57 +00002667 }
2668
2669 if (X86::isSHUFPMask(PermMask.Val))
Evan Cheng2cf42322006-04-05 06:09:26 +00002670 return NormalizeVectorShuffle(Op, DAG);
Evan Cheng59a63552006-04-05 01:47:37 +00002671
2672 // Handle v8i16 shuffle high / low shuffle node pair.
2673 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2674 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2675 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2676 std::vector<SDOperand> MaskVec;
2677 for (unsigned i = 0; i != 4; ++i)
2678 MaskVec.push_back(PermMask.getOperand(i));
2679 for (unsigned i = 4; i != 8; ++i)
2680 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2681 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2682 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2683 MaskVec.clear();
2684 for (unsigned i = 0; i != 4; ++i)
2685 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2686 for (unsigned i = 4; i != 8; ++i)
2687 MaskVec.push_back(PermMask.getOperand(i));
2688 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
2689 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2690 }
Evan Cheng7e2ff112006-03-30 19:54:57 +00002691 } else {
2692 // Floating point cases in the other order.
2693 if (X86::isSHUFPMask(PermMask.Val))
Evan Cheng2cf42322006-04-05 06:09:26 +00002694 return NormalizeVectorShuffle(Op, DAG);
Evan Cheng7e2ff112006-03-30 19:54:57 +00002695 if (X86::isPSHUFDMask(PermMask.Val) ||
2696 X86::isPSHUFHWMask(PermMask.Val) ||
2697 X86::isPSHUFLWMask(PermMask.Val)) {
2698 if (V2.getOpcode() != ISD::UNDEF)
2699 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2700 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
Evan Cheng2cf42322006-04-05 06:09:26 +00002701 return Op;
Evan Cheng7e2ff112006-03-30 19:54:57 +00002702 }
Evan Chengda59b0d2006-03-29 01:30:51 +00002703 }
Evan Chengd097e672006-03-22 02:53:00 +00002704
Evan Cheng2cf42322006-04-05 06:09:26 +00002705 return SDOperand();
Evan Chengd097e672006-03-22 02:53:00 +00002706 }
Evan Cheng082c8782006-03-24 07:29:27 +00002707 case ISD::BUILD_VECTOR: {
Evan Cheng9b9cc4f2006-03-27 07:00:16 +00002708 // All one's are handled with pcmpeqd.
2709 if (ISD::isBuildVectorAllOnes(Op.Val))
2710 return Op;
2711
Evan Cheng2bc09412006-03-25 09:37:23 +00002712 std::set<SDOperand> Values;
Evan Chenge7ee6a52006-03-24 23:15:12 +00002713 SDOperand Elt0 = Op.getOperand(0);
Evan Cheng2bc09412006-03-25 09:37:23 +00002714 Values.insert(Elt0);
Evan Chenge7ee6a52006-03-24 23:15:12 +00002715 bool Elt0IsZero = (isa<ConstantSDNode>(Elt0) &&
2716 cast<ConstantSDNode>(Elt0)->getValue() == 0) ||
2717 (isa<ConstantFPSDNode>(Elt0) &&
2718 cast<ConstantFPSDNode>(Elt0)->isExactlyValue(0.0));
2719 bool RestAreZero = true;
Evan Cheng082c8782006-03-24 07:29:27 +00002720 unsigned NumElems = Op.getNumOperands();
Evan Chenge7ee6a52006-03-24 23:15:12 +00002721 for (unsigned i = 1; i < NumElems; ++i) {
Evan Cheng2bc09412006-03-25 09:37:23 +00002722 SDOperand Elt = Op.getOperand(i);
2723 if (ConstantFPSDNode *FPC = dyn_cast<ConstantFPSDNode>(Elt)) {
Evan Cheng082c8782006-03-24 07:29:27 +00002724 if (!FPC->isExactlyValue(+0.0))
Evan Chenge7ee6a52006-03-24 23:15:12 +00002725 RestAreZero = false;
Evan Cheng2bc09412006-03-25 09:37:23 +00002726 } else if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Elt)) {
Evan Cheng082c8782006-03-24 07:29:27 +00002727 if (!C->isNullValue())
Evan Chenge7ee6a52006-03-24 23:15:12 +00002728 RestAreZero = false;
Evan Cheng082c8782006-03-24 07:29:27 +00002729 } else
Evan Chenge7ee6a52006-03-24 23:15:12 +00002730 RestAreZero = false;
Evan Cheng2bc09412006-03-25 09:37:23 +00002731 Values.insert(Elt);
Evan Cheng082c8782006-03-24 07:29:27 +00002732 }
2733
Evan Chenge7ee6a52006-03-24 23:15:12 +00002734 if (RestAreZero) {
2735 if (Elt0IsZero) return Op;
2736
2737 // Zero extend a scalar to a vector.
2738 return DAG.getNode(X86ISD::ZEXT_S2VEC, Op.getValueType(), Elt0);
2739 }
2740
Evan Cheng2bc09412006-03-25 09:37:23 +00002741 if (Values.size() > 2) {
2742 // Expand into a number of unpckl*.
2743 // e.g. for v4f32
2744 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2745 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2746 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2747 MVT::ValueType VT = Op.getValueType();
Evan Cheng5df75882006-03-28 00:39:58 +00002748 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2749 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2750 std::vector<SDOperand> MaskVec;
2751 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2752 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2753 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2754 }
2755 SDOperand PermMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, MaskVec);
Evan Cheng2bc09412006-03-25 09:37:23 +00002756 std::vector<SDOperand> V(NumElems);
2757 for (unsigned i = 0; i < NumElems; ++i)
2758 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2759 NumElems >>= 1;
2760 while (NumElems != 0) {
2761 for (unsigned i = 0; i < NumElems; ++i)
Evan Cheng5df75882006-03-28 00:39:58 +00002762 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2763 PermMask);
Evan Cheng2bc09412006-03-25 09:37:23 +00002764 NumElems >>= 1;
2765 }
2766 return V[0];
2767 }
2768
Evan Cheng082c8782006-03-24 07:29:27 +00002769 return SDOperand();
2770 }
Evan Chengcbffa462006-03-31 19:22:53 +00002771 case ISD::EXTRACT_VECTOR_ELT: {
Evan Chengebf10062006-04-03 20:53:28 +00002772 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2773 return SDOperand();
2774
Evan Chengcbffa462006-03-31 19:22:53 +00002775 MVT::ValueType VT = Op.getValueType();
2776 if (MVT::getSizeInBits(VT) == 16) {
Evan Chengebf10062006-04-03 20:53:28 +00002777 // Transform it so it match pextrw which produces a 32-bit result.
Evan Chengcbffa462006-03-31 19:22:53 +00002778 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2779 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2780 Op.getOperand(0), Op.getOperand(1));
2781 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2782 DAG.getValueType(VT));
2783 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Chengebf10062006-04-03 20:53:28 +00002784 } else if (MVT::getSizeInBits(VT) == 32) {
2785 SDOperand Vec = Op.getOperand(0);
2786 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2787 if (Idx == 0)
2788 return Op;
2789
2790 // TODO: if Idex == 2, we can use unpckhps
2791 // SHUFPS the element to the lowest double word, then movss.
2792 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2793 SDOperand IdxNode = DAG.getConstant((Idx < 2) ? Idx : Idx+4,
2794 MVT::getVectorBaseType(MaskVT));
2795 std::vector<SDOperand> IdxVec;
2796 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2797 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2798 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2799 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2800 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2801 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2802 Vec, Vec, Mask);
2803 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2804 DAG.getConstant(0, MVT::i32));
2805 } else if (MVT::getSizeInBits(VT) == 64) {
2806 SDOperand Vec = Op.getOperand(0);
2807 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2808 if (Idx == 0)
2809 return Op;
2810
2811 // UNPCKHPD the element to the lowest double word, then movsd.
Evan Chengb64827e2006-04-03 22:30:54 +00002812 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2813 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Evan Chengebf10062006-04-03 20:53:28 +00002814 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2815 std::vector<SDOperand> IdxVec;
2816 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2817 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2818 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, IdxVec);
2819 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2820 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2821 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
2822 DAG.getConstant(0, MVT::i32));
Evan Chengcbffa462006-03-31 19:22:53 +00002823 }
2824
2825 return SDOperand();
2826 }
2827 case ISD::INSERT_VECTOR_ELT: {
2828 // Transform it so it match pinsrw which expects a 16-bit value in a R32
2829 // as its second argument.
2830 MVT::ValueType VT = Op.getValueType();
2831 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2832 if (MVT::getSizeInBits(BaseVT) == 16) {
2833 SDOperand N1 = Op.getOperand(1);
2834 SDOperand N2 = Op.getOperand(2);
2835 if (N1.getValueType() != MVT::i32)
2836 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2837 if (N2.getValueType() != MVT::i32)
2838 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
Evan Cheng5fd7c692006-03-31 21:55:24 +00002839 return DAG.getNode(X86ISD::PINSRW, VT, Op.getOperand(0), N1, N2);
Evan Chengcbffa462006-03-31 19:22:53 +00002840 }
2841
2842 return SDOperand();
2843 }
Evan Cheng78038292006-04-05 23:38:46 +00002844 case ISD::INTRINSIC_WO_CHAIN: {
2845 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
2846 switch (IntNo) {
2847 default: return SDOperand(); // Don't custom lower most intrinsics.
2848 // Comparison intrinsics.
2849 case Intrinsic::x86_sse_comieq_ss:
2850 case Intrinsic::x86_sse_comilt_ss:
2851 case Intrinsic::x86_sse_comile_ss:
2852 case Intrinsic::x86_sse_comigt_ss:
2853 case Intrinsic::x86_sse_comige_ss:
2854 case Intrinsic::x86_sse_comineq_ss:
2855 case Intrinsic::x86_sse_ucomieq_ss:
2856 case Intrinsic::x86_sse_ucomilt_ss:
2857 case Intrinsic::x86_sse_ucomile_ss:
2858 case Intrinsic::x86_sse_ucomigt_ss:
2859 case Intrinsic::x86_sse_ucomige_ss:
2860 case Intrinsic::x86_sse_ucomineq_ss:
2861 case Intrinsic::x86_sse2_comieq_sd:
2862 case Intrinsic::x86_sse2_comilt_sd:
2863 case Intrinsic::x86_sse2_comile_sd:
2864 case Intrinsic::x86_sse2_comigt_sd:
2865 case Intrinsic::x86_sse2_comige_sd:
2866 case Intrinsic::x86_sse2_comineq_sd:
2867 case Intrinsic::x86_sse2_ucomieq_sd:
2868 case Intrinsic::x86_sse2_ucomilt_sd:
2869 case Intrinsic::x86_sse2_ucomile_sd:
2870 case Intrinsic::x86_sse2_ucomigt_sd:
2871 case Intrinsic::x86_sse2_ucomige_sd:
2872 case Intrinsic::x86_sse2_ucomineq_sd: {
2873 unsigned Opc;
2874 ISD::CondCode CC;
2875 switch (IntNo) {
2876 default: break;
2877 case Intrinsic::x86_sse_comieq_ss:
2878 case Intrinsic::x86_sse2_comieq_sd:
2879 Opc = X86ISD::COMI;
2880 CC = ISD::SETEQ;
2881 break;
2882 case Intrinsic::x86_sse_comilt_ss:
2883 case Intrinsic::x86_sse2_comilt_sd:
2884 Opc = X86ISD::COMI;
2885 CC = ISD::SETLT;
2886 break;
2887 case Intrinsic::x86_sse_comile_ss:
2888 case Intrinsic::x86_sse2_comile_sd:
2889 Opc = X86ISD::COMI;
2890 CC = ISD::SETLE;
2891 break;
2892 case Intrinsic::x86_sse_comigt_ss:
2893 case Intrinsic::x86_sse2_comigt_sd:
2894 Opc = X86ISD::COMI;
2895 CC = ISD::SETGT;
2896 break;
2897 case Intrinsic::x86_sse_comige_ss:
2898 case Intrinsic::x86_sse2_comige_sd:
2899 Opc = X86ISD::COMI;
2900 CC = ISD::SETGE;
2901 break;
2902 case Intrinsic::x86_sse_comineq_ss:
2903 case Intrinsic::x86_sse2_comineq_sd:
2904 Opc = X86ISD::COMI;
2905 CC = ISD::SETNE;
2906 break;
2907 case Intrinsic::x86_sse_ucomieq_ss:
2908 case Intrinsic::x86_sse2_ucomieq_sd:
2909 Opc = X86ISD::UCOMI;
2910 CC = ISD::SETEQ;
2911 break;
2912 case Intrinsic::x86_sse_ucomilt_ss:
2913 case Intrinsic::x86_sse2_ucomilt_sd:
2914 Opc = X86ISD::UCOMI;
2915 CC = ISD::SETLT;
2916 break;
2917 case Intrinsic::x86_sse_ucomile_ss:
2918 case Intrinsic::x86_sse2_ucomile_sd:
2919 Opc = X86ISD::UCOMI;
2920 CC = ISD::SETLE;
2921 break;
2922 case Intrinsic::x86_sse_ucomigt_ss:
2923 case Intrinsic::x86_sse2_ucomigt_sd:
2924 Opc = X86ISD::UCOMI;
2925 CC = ISD::SETGT;
2926 break;
2927 case Intrinsic::x86_sse_ucomige_ss:
2928 case Intrinsic::x86_sse2_ucomige_sd:
2929 Opc = X86ISD::UCOMI;
2930 CC = ISD::SETGE;
2931 break;
2932 case Intrinsic::x86_sse_ucomineq_ss:
2933 case Intrinsic::x86_sse2_ucomineq_sd:
2934 Opc = X86ISD::UCOMI;
2935 CC = ISD::SETNE;
2936 break;
2937 }
2938 bool Flip;
2939 unsigned X86CC;
2940 translateX86CC(CC, true, X86CC, Flip);
2941 SDOperand Cond = DAG.getNode(Opc, MVT::Flag, Op.getOperand(Flip?2:1),
2942 Op.getOperand(Flip?1:2));
2943 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
2944 DAG.getConstant(X86CC, MVT::i8), Cond);
2945 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
2946 }
2947 }
2948 }
Evan Cheng5c59d492005-12-23 07:31:11 +00002949 }
Chris Lattner76ac0682005-11-15 00:40:23 +00002950}
Evan Cheng6af02632005-12-20 06:22:03 +00002951
2952const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
2953 switch (Opcode) {
2954 default: return NULL;
Evan Cheng9c249c32006-01-09 18:33:28 +00002955 case X86ISD::SHLD: return "X86ISD::SHLD";
2956 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Cheng2dd217b2006-01-31 03:14:29 +00002957 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng72d5c252006-01-31 22:28:30 +00002958 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng6305e502006-01-12 22:54:21 +00002959 case X86ISD::FILD: return "X86ISD::FILD";
Evan Cheng11613a52006-02-04 02:20:30 +00002960 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng6af02632005-12-20 06:22:03 +00002961 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
2962 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
2963 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chenga74ce622005-12-21 02:39:21 +00002964 case X86ISD::FLD: return "X86ISD::FLD";
Evan Cheng45e190982006-01-05 00:27:02 +00002965 case X86ISD::FST: return "X86ISD::FST";
2966 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chenga74ce622005-12-21 02:39:21 +00002967 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng6af02632005-12-20 06:22:03 +00002968 case X86ISD::CALL: return "X86ISD::CALL";
2969 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
2970 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
2971 case X86ISD::CMP: return "X86ISD::CMP";
2972 case X86ISD::TEST: return "X86ISD::TEST";
Evan Cheng78038292006-04-05 23:38:46 +00002973 case X86ISD::COMI: return "X86ISD::COMI";
2974 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengc1583db2005-12-21 20:21:51 +00002975 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng6af02632005-12-20 06:22:03 +00002976 case X86ISD::CMOV: return "X86ISD::CMOV";
2977 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chenga74ce622005-12-21 02:39:21 +00002978 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng084a1022006-03-04 01:12:00 +00002979 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
2980 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng72d5c252006-01-31 22:28:30 +00002981 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng5588de92006-02-18 00:15:05 +00002982 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Chenge0ed6ec2006-02-23 20:41:18 +00002983 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chenge7ee6a52006-03-24 23:15:12 +00002984 case X86ISD::S2VEC: return "X86ISD::S2VEC";
2985 case X86ISD::ZEXT_S2VEC: return "X86ISD::ZEXT_S2VEC";
Evan Chengcbffa462006-03-31 19:22:53 +00002986 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng5fd7c692006-03-31 21:55:24 +00002987 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng6af02632005-12-20 06:22:03 +00002988 }
2989}
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002990
Nate Begeman8a77efe2006-02-16 21:11:51 +00002991void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
2992 uint64_t Mask,
2993 uint64_t &KnownZero,
2994 uint64_t &KnownOne,
2995 unsigned Depth) const {
Evan Cheng9cdc16c2005-12-21 23:05:39 +00002996 unsigned Opc = Op.getOpcode();
Evan Cheng6d196db2006-04-05 06:11:20 +00002997 assert((Opc >= ISD::BUILTIN_OP_END ||
2998 Opc == ISD::INTRINSIC_WO_CHAIN ||
2999 Opc == ISD::INTRINSIC_W_CHAIN ||
3000 Opc == ISD::INTRINSIC_VOID) &&
3001 "Should use MaskedValueIsZero if you don't know whether Op"
3002 " is a target node!");
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003003
Evan Cheng6d196db2006-04-05 06:11:20 +00003004 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003005 switch (Opc) {
Evan Cheng6d196db2006-04-05 06:11:20 +00003006 default: break;
Nate Begeman8a77efe2006-02-16 21:11:51 +00003007 case X86ISD::SETCC:
3008 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
3009 break;
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003010 }
Evan Cheng9cdc16c2005-12-21 23:05:39 +00003011}
Chris Lattnerc642aa52006-01-31 19:43:35 +00003012
3013std::vector<unsigned> X86TargetLowering::
Chris Lattner7ad77df2006-02-22 00:56:39 +00003014getRegClassForInlineAsmConstraint(const std::string &Constraint,
3015 MVT::ValueType VT) const {
Chris Lattnerc642aa52006-01-31 19:43:35 +00003016 if (Constraint.size() == 1) {
3017 // FIXME: not handling fp-stack yet!
3018 // FIXME: not handling MMX registers yet ('y' constraint).
3019 switch (Constraint[0]) { // GCC X86 Constraint Letters
3020 default: break; // Unknown constriant letter
3021 case 'r': // GENERAL_REGS
3022 case 'R': // LEGACY_REGS
3023 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3024 X86::ESI, X86::EDI, X86::EBP, X86::ESP, 0);
3025 case 'l': // INDEX_REGS
3026 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX,
3027 X86::ESI, X86::EDI, X86::EBP, 0);
3028 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
3029 case 'Q': // Q_REGS
3030 return make_vector<unsigned>(X86::EAX, X86::EBX, X86::ECX, X86::EDX, 0);
3031 case 'x': // SSE_REGS if SSE1 allowed
3032 if (Subtarget->hasSSE1())
3033 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3034 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3035 0);
3036 return std::vector<unsigned>();
3037 case 'Y': // SSE_REGS if SSE2 allowed
3038 if (Subtarget->hasSSE2())
3039 return make_vector<unsigned>(X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
3040 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7,
3041 0);
3042 return std::vector<unsigned>();
3043 }
3044 }
3045
Chris Lattner7ad77df2006-02-22 00:56:39 +00003046 return std::vector<unsigned>();
Chris Lattnerc642aa52006-01-31 19:43:35 +00003047}
Evan Chengaf598d22006-03-13 23:18:16 +00003048
3049/// isLegalAddressImmediate - Return true if the integer value or
3050/// GlobalValue can be used as the offset of the target addressing mode.
3051bool X86TargetLowering::isLegalAddressImmediate(int64_t V) const {
3052 // X86 allows a sign-extended 32-bit immediate field.
3053 return (V > -(1LL << 32) && V < (1LL << 32)-1);
3054}
3055
3056bool X86TargetLowering::isLegalAddressImmediate(GlobalValue *GV) const {
Evan Chengbc047222006-03-22 19:22:18 +00003057 if (Subtarget->isTargetDarwin()) {
Evan Chengaf598d22006-03-13 23:18:16 +00003058 Reloc::Model RModel = getTargetMachine().getRelocationModel();
3059 if (RModel == Reloc::Static)
3060 return true;
3061 else if (RModel == Reloc::DynamicNoPIC)
Evan Chengf75555f2006-03-16 22:02:48 +00003062 return !DarwinGVRequiresExtraLoad(GV);
Evan Chengaf598d22006-03-13 23:18:16 +00003063 else
3064 return false;
3065 } else
3066 return true;
3067}
Evan Cheng68ad48b2006-03-22 18:59:22 +00003068
3069/// isShuffleMaskLegal - Targets can use this to indicate that they only
3070/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
3071/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
3072/// are assumed to be legal.
Evan Cheng021bb7c2006-03-22 22:07:06 +00003073bool
3074X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
3075 // Only do shuffles on 128-bit vector types for now.
3076 if (MVT::getSizeInBits(VT) == 64) return false;
Evan Cheng2595a682006-03-24 02:58:06 +00003077 return (Mask.Val->getNumOperands() == 2 ||
3078 X86::isSplatMask(Mask.Val) ||
Evan Chengd27fb3e2006-03-24 01:18:28 +00003079 X86::isPSHUFDMask(Mask.Val) ||
Evan Cheng59a63552006-04-05 01:47:37 +00003080 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
Evan Cheng5df75882006-03-28 00:39:58 +00003081 X86::isSHUFPMask(Mask.Val) ||
Evan Cheng21e54762006-03-28 08:27:15 +00003082 X86::isUNPCKLMask(Mask.Val) ||
Evan Chengf3b52c82006-04-05 07:20:06 +00003083 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Jim Laskey457e54e2006-03-28 10:17:11 +00003084 X86::isUNPCKHMask(Mask.Val));
Evan Cheng68ad48b2006-03-22 18:59:22 +00003085}