blob: 996ffe94ecc040deb9c22bfe016e64757d84da75 [file] [log] [blame]
Chris Lattnerc6d05672006-05-23 23:20:42 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Chris Lattner and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000026#include "llvm/ADT/VectorExtras.h"
27#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000032#include "llvm/CodeGen/SelectionDAG.h"
33#include "llvm/CodeGen/SSARegMap.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000034#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/Target/TargetOptions.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000036#include "llvm/ADT/StringExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000037using namespace llvm;
38
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039X86TargetLowering::X86TargetLowering(TargetMachine &TM)
40 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000041 Subtarget = &TM.getSubtarget<X86Subtarget>();
42 X86ScalarSSE = Subtarget->hasSSE2();
Evan Cheng25ab6902006-09-08 06:48:29 +000043 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Evan Cheng559806f2006-01-27 08:10:46 +000044
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000045 // Set up the TargetLowering object.
46
47 // X86 is weird, it always uses i8 for shift amounts and setcc results.
48 setShiftAmountType(MVT::i8);
49 setSetCCResultType(MVT::i8);
50 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000051 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000053 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000054
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000055 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000056 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000057 setUseUnderscoreSetJmp(false);
58 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000059 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000060 // MS runtime is weird: it exports _setjmp, but longjmp!
61 setUseUnderscoreSetJmp(true);
62 setUseUnderscoreLongJmp(false);
63 } else {
64 setUseUnderscoreSetJmp(true);
65 setUseUnderscoreLongJmp(true);
66 }
67
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000068 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000069 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
70 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
71 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000072 if (Subtarget->is64Bit())
73 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000074
Evan Chengc5484282006-10-04 00:56:09 +000075 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
76
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000077 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
78 // operation.
79 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
80 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
81 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000082
Evan Cheng25ab6902006-09-08 06:48:29 +000083 if (Subtarget->is64Bit()) {
84 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000085 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000086 } else {
87 if (X86ScalarSSE)
88 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
89 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
90 else
91 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
92 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000093
94 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
95 // this operation.
96 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
97 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +000098 // SSE has no i16 to fp conversion, only i32
Evan Cheng02568ff2006-01-30 22:13:22 +000099 if (X86ScalarSSE)
Evan Cheng02568ff2006-01-30 22:13:22 +0000100 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000101 else {
102 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
103 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
104 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105
Evan Cheng25ab6902006-09-08 06:48:29 +0000106 if (!Subtarget->is64Bit()) {
107 // Custom lower SINT_TO_FP and FP_TO_SINT from/to i64 in 32-bit mode.
108 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
109 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
110 }
Evan Cheng6dab0532006-01-30 08:02:57 +0000111
Evan Cheng02568ff2006-01-30 22:13:22 +0000112 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
113 // this operation.
114 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
115 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
116
117 if (X86ScalarSSE) {
118 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
119 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000120 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000121 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000122 }
123
124 // Handle FP_TO_UINT by promoting the destination to a larger signed
125 // conversion.
126 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
127 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
128 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
129
Evan Cheng25ab6902006-09-08 06:48:29 +0000130 if (Subtarget->is64Bit()) {
131 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000132 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000133 } else {
134 if (X86ScalarSSE && !Subtarget->hasSSE3())
135 // Expand FP_TO_UINT into a select.
136 // FIXME: We would like to use a Custom expander here eventually to do
137 // the optimal thing for SSE vs. the default expansion in the legalizer.
138 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
139 else
140 // With SSE3 we can use fisttpll to convert to a signed i64.
141 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
142 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143
Chris Lattner399610a2006-12-05 18:22:22 +0000144 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Chris Lattnerf3597a12006-12-05 18:45:06 +0000145 if (!X86ScalarSSE) {
146 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
147 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
148 }
Chris Lattner21f66852005-12-23 05:15:23 +0000149
Evan Chengc35497f2006-10-30 08:02:39 +0000150 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000151 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000152 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
153 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 if (Subtarget->is64Bit())
156 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000157 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Expand);
Chris Lattnere80242a2005-12-07 17:59:14 +0000158 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000159 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
160 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000161 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000162
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000163 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
164 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
165 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
166 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
167 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
168 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
169 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
170 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
171 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000172 if (Subtarget->is64Bit()) {
173 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
174 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
175 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
176 }
177
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000178 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000179 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000180
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181 // These should be promoted to a larger select which is supported.
182 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
183 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000184 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000185 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
186 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
187 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
188 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
189 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
190 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
191 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
192 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
193 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000194 if (Subtarget->is64Bit()) {
195 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
196 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
197 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000198 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000199 setOperationAction(ISD::RET , MVT::Other, Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000200 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000201 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000202 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000203 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000204 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000205 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000206 if (Subtarget->is64Bit()) {
207 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
208 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
209 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
210 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
211 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000212 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000213 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
214 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
215 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000216 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000217 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
218 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219
Chris Lattnerf73bae12005-11-29 06:16:21 +0000220 // We don't have line number support yet.
221 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Jim Laskeye0bce712006-01-05 01:47:43 +0000222 setOperationAction(ISD::DEBUG_LOC, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000223 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000224 if (!Subtarget->isTargetDarwin() &&
225 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000226 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000227 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000228
Nate Begemanacc398c2006-01-25 18:21:52 +0000229 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
230 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000231 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000232 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000233 if (Subtarget->is64Bit())
234 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
235 else
236 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
237
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000238 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000239 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000240 if (Subtarget->is64Bit())
241 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000242 if (Subtarget->isTargetCygMing())
243 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
244 else
245 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000246
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000247 if (X86ScalarSSE) {
248 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000249 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
250 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251
Evan Cheng223547a2006-01-31 22:28:30 +0000252 // Use ANDPD to simulate FABS.
253 setOperationAction(ISD::FABS , MVT::f64, Custom);
254 setOperationAction(ISD::FABS , MVT::f32, Custom);
255
256 // Use XORP to simulate FNEG.
257 setOperationAction(ISD::FNEG , MVT::f64, Custom);
258 setOperationAction(ISD::FNEG , MVT::f32, Custom);
259
Evan Cheng68c47cb2007-01-05 07:55:56 +0000260 // Use ANDPD and ORPD to simulate FCOPYSIGN.
261 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
262 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
263
Evan Chengd25e9e82006-02-02 00:28:23 +0000264 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000265 setOperationAction(ISD::FSIN , MVT::f64, Expand);
266 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000267 setOperationAction(ISD::FREM , MVT::f64, Expand);
268 setOperationAction(ISD::FSIN , MVT::f32, Expand);
269 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000270 setOperationAction(ISD::FREM , MVT::f32, Expand);
271
Chris Lattnera54aa942006-01-29 06:26:08 +0000272 // Expand FP immediates into loads from the stack, except for the special
273 // cases we handle.
274 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
275 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000276 addLegalFPImmediate(+0.0); // xorps / xorpd
277 } else {
278 // Set up the FP register classes.
279 addRegisterClass(MVT::f64, X86::RFPRegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000280
Evan Cheng68c47cb2007-01-05 07:55:56 +0000281 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
282 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
283 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000284
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000285 if (!UnsafeFPMath) {
286 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
287 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
288 }
289
Chris Lattnera54aa942006-01-29 06:26:08 +0000290 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000291 addLegalFPImmediate(+0.0); // FLD0
292 addLegalFPImmediate(+1.0); // FLD1
293 addLegalFPImmediate(-0.0); // FLD0/FCHS
294 addLegalFPImmediate(-1.0); // FLD1/FCHS
295 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000296
Evan Chengd30bf012006-03-01 01:11:20 +0000297 // First set operation action for all vector types to expand. Then we
298 // will selectively turn on ones that can be effectively codegen'd.
299 for (unsigned VT = (unsigned)MVT::Vector + 1;
300 VT != (unsigned)MVT::LAST_VALUETYPE; VT++) {
301 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
302 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000303 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
304 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000305 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000306 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
307 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
308 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
309 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
310 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
311 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000312 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000313 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000314 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000315 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000316 }
317
Evan Chenga88973f2006-03-22 19:22:18 +0000318 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000319 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
320 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
321 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000322 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000323
Evan Chengd30bf012006-03-01 01:11:20 +0000324 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000325
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000326 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
327 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
328 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000329 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000330
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000331 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
332 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
333 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
334
Bill Wendling74027e92007-03-15 21:24:36 +0000335 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
336 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
337
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000338 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000339 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000340 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000341 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
342 setOperationAction(ISD::AND, MVT::v2i32, Promote);
343 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
344 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000345
346 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000347 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000348 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000349 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
350 setOperationAction(ISD::OR, MVT::v2i32, Promote);
351 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
352 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000353
354 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000355 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000356 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000357 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
358 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
359 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
360 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000361
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000362 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000363 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000364 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000365 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
366 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
367 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
368 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000369
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000370 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
371 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
372 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
373 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000374
375 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
376 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
377 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000378 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000379
380 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
381 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000382 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
383 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000384 }
385
Evan Chenga88973f2006-03-22 19:22:18 +0000386 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000387 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
388
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000389 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
390 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
391 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
392 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000393 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
394 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
395 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000396 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000397 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000398 }
399
Evan Chenga88973f2006-03-22 19:22:18 +0000400 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000401 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
402 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
403 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
404 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
405 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
406
Evan Chengf7c378e2006-04-10 07:23:14 +0000407 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
408 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
409 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000410 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000411 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
412 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
413 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000414 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000415 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000416 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
417 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
418 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
419 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000420
Evan Chengf7c378e2006-04-10 07:23:14 +0000421 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
422 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000423 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000424 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
425 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
426 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000427
Evan Cheng2c3ae372006-04-12 21:21:57 +0000428 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
429 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
430 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
431 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
432 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
433 }
434 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
435 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
436 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
437 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
438 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
439 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
440
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000441 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000442 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
443 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
444 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
445 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
446 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
447 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
448 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000449 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
450 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000451 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
452 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000453 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000454
455 // Custom lower v2i64 and v2f64 selects.
456 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000457 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000458 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000459 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000460 }
461
Evan Cheng6be2c582006-04-05 23:38:46 +0000462 // We want to custom lower some of our intrinsics.
463 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
464
Evan Cheng206ee9d2006-07-07 08:33:52 +0000465 // We have target-specific dag combine patterns for the following nodes:
466 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000467 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000468
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000469 computeRegisterProperties();
470
Evan Cheng87ed7162006-02-14 08:25:08 +0000471 // FIXME: These should be based on subtarget info. Plus, the values should
472 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000473 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
474 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
475 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000476 allowUnalignedMemoryAccesses = true; // x86 supports it!
477}
478
Chris Lattner2b02a442007-02-25 08:29:00 +0000479
480//===----------------------------------------------------------------------===//
481// Return Value Calling Convention Implementation
482//===----------------------------------------------------------------------===//
483
Chris Lattner59ed56b2007-02-28 04:55:35 +0000484#include "X86GenCallingConv.inc"
Chris Lattner9774c912007-02-27 05:28:59 +0000485
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000486/// LowerRET - Lower an ISD::RET node.
487SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
488 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
489
Chris Lattner9774c912007-02-27 05:28:59 +0000490 SmallVector<CCValAssign, 16> RVLocs;
491 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
492 CCState CCInfo(CC, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000493 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000494
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000495
496 // If this is the first return lowered for this function, add the regs to the
497 // liveout set for the function.
498 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000499 for (unsigned i = 0; i != RVLocs.size(); ++i)
500 if (RVLocs[i].isRegLoc())
501 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000502 }
503
504 SDOperand Chain = Op.getOperand(0);
505 SDOperand Flag;
506
507 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000508 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
509 RVLocs[0].getLocReg() != X86::ST0) {
510 for (unsigned i = 0; i != RVLocs.size(); ++i) {
511 CCValAssign &VA = RVLocs[i];
512 assert(VA.isRegLoc() && "Can only return in registers!");
513 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
514 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000515 Flag = Chain.getValue(1);
516 }
517 } else {
518 // We need to handle a destination of ST0 specially, because it isn't really
519 // a register.
520 SDOperand Value = Op.getOperand(1);
521
522 // If this is an FP return with ScalarSSE, we need to move the value from
523 // an XMM register onto the fp-stack.
524 if (X86ScalarSSE) {
525 SDOperand MemLoc;
526
527 // If this is a load into a scalarsse value, don't store the loaded value
528 // back to the stack, only to reload it: just replace the scalar-sse load.
529 if (ISD::isNON_EXTLoad(Value.Val) &&
530 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
531 Chain = Value.getOperand(0);
532 MemLoc = Value.getOperand(1);
533 } else {
534 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000535 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000536 MachineFunction &MF = DAG.getMachineFunction();
537 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
538 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
539 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
540 }
541 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000542 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000543 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
544 Chain = Value.getValue(1);
545 }
546
547 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
548 SDOperand Ops[] = { Chain, Value };
549 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
550 Flag = Chain.getValue(1);
551 }
552
553 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
554 if (Flag.Val)
555 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
556 else
557 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
558}
559
560
Chris Lattner3085e152007-02-25 08:59:22 +0000561/// LowerCallResult - Lower the result values of an ISD::CALL into the
562/// appropriate copies out of appropriate physical registers. This assumes that
563/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
564/// being lowered. The returns a SDNode with the same number of values as the
565/// ISD::CALL.
566SDNode *X86TargetLowering::
567LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
568 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000569
570 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000571 SmallVector<CCValAssign, 16> RVLocs;
572 CCState CCInfo(CallingConv, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000573 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
574
Chris Lattner3085e152007-02-25 08:59:22 +0000575
Chris Lattnere32bbf62007-02-28 07:09:55 +0000576 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000577
578 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000579 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
580 for (unsigned i = 0; i != RVLocs.size(); ++i) {
581 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
582 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000583 InFlag = Chain.getValue(2);
584 ResultVals.push_back(Chain.getValue(0));
585 }
586 } else {
587 // Copies from the FP stack are special, as ST0 isn't a valid register
588 // before the fp stackifier runs.
589
590 // Copy ST0 into an RFP register with FP_GET_RESULT.
591 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
592 SDOperand GROps[] = { Chain, InFlag };
593 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
594 Chain = RetVal.getValue(1);
595 InFlag = RetVal.getValue(2);
596
597 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
598 // an XMM register.
599 if (X86ScalarSSE) {
600 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
601 // shouldn't be necessary except that RFP cannot be live across
602 // multiple blocks. When stackifier is fixed, they can be uncoupled.
603 MachineFunction &MF = DAG.getMachineFunction();
604 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
605 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
606 SDOperand Ops[] = {
Chris Lattner9774c912007-02-27 05:28:59 +0000607 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000608 };
609 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner9774c912007-02-27 05:28:59 +0000610 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner3085e152007-02-25 08:59:22 +0000611 Chain = RetVal.getValue(1);
612 }
613
Chris Lattner9774c912007-02-27 05:28:59 +0000614 if (RVLocs[0].getValVT() == MVT::f32 && !X86ScalarSSE)
Chris Lattner3085e152007-02-25 08:59:22 +0000615 // FIXME: we would really like to remember that this FP_ROUND
616 // operation is okay to eliminate if we allow excess FP precision.
617 RetVal = DAG.getNode(ISD::FP_ROUND, MVT::f32, RetVal);
618 ResultVals.push_back(RetVal);
619 }
620
621 // Merge everything together with a MERGE_VALUES node.
622 ResultVals.push_back(Chain);
623 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
624 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000625}
626
627
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000628//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000629// C & StdCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000630//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000631// StdCall calling convention seems to be standard for many Windows' API
632// routines and around. It differs from C calling convention just a little:
633// callee should clean up the stack, not caller. Symbols should be also
634// decorated in some fancy way :) It doesn't support any vector arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000635
Evan Cheng85e38002006-04-27 05:35:28 +0000636/// AddLiveIn - This helper function adds the specified physical register to the
637/// MachineFunction as a live in value. It also creates a corresponding virtual
638/// register for it.
639static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000640 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000641 assert(RC->contains(PReg) && "Not the correct regclass!");
642 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
643 MF.addLiveIn(PReg, VReg);
644 return VReg;
645}
646
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000647SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
648 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000649 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000650 MachineFunction &MF = DAG.getMachineFunction();
651 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000652 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000653 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000654
Chris Lattner638402b2007-02-28 07:00:42 +0000655 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000656 SmallVector<CCValAssign, 16> ArgLocs;
657 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
658 ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000659 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
660
Chris Lattnerf39f7712007-02-28 05:46:49 +0000661 SmallVector<SDOperand, 8> ArgValues;
662 unsigned LastVal = ~0U;
663 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
664 CCValAssign &VA = ArgLocs[i];
665 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
666 // places.
667 assert(VA.getValNo() != LastVal &&
668 "Don't support value assigned to multiple locs yet");
669 LastVal = VA.getValNo();
670
671 if (VA.isRegLoc()) {
672 MVT::ValueType RegVT = VA.getLocVT();
673 TargetRegisterClass *RC;
674 if (RegVT == MVT::i32)
675 RC = X86::GR32RegisterClass;
676 else {
677 assert(MVT::isVector(RegVT));
678 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000679 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000680
Chris Lattner82932a52007-03-02 05:12:29 +0000681 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
682 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +0000683
684 // If this is an 8 or 16-bit value, it is really passed promoted to 32
685 // bits. Insert an assert[sz]ext to capture this, then truncate to the
686 // right size.
687 if (VA.getLocInfo() == CCValAssign::SExt)
688 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
689 DAG.getValueType(VA.getValVT()));
690 else if (VA.getLocInfo() == CCValAssign::ZExt)
691 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
692 DAG.getValueType(VA.getValVT()));
693
694 if (VA.getLocInfo() != CCValAssign::Full)
695 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
696
697 ArgValues.push_back(ArgValue);
698 } else {
699 assert(VA.isMemLoc());
700
701 // Create the nodes corresponding to a load from this parameter slot.
702 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
703 VA.getLocMemOffset());
704 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
705 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
Evan Cheng1bc78042006-04-26 01:20:17 +0000706 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000707 }
Chris Lattnerf39f7712007-02-28 05:46:49 +0000708
709 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng1bc78042006-04-26 01:20:17 +0000710
Evan Cheng25caf632006-05-23 21:06:34 +0000711 ArgValues.push_back(Root);
712
Evan Cheng1bc78042006-04-26 01:20:17 +0000713 // If the function takes variable number of arguments, make a frame index for
714 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000715 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +0000716 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000717
718 if (isStdCall && !isVarArg) {
Chris Lattnerf39f7712007-02-28 05:46:49 +0000719 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000720 BytesCallerReserves = 0;
721 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000722 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000723
724 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000725 if (NumArgs &&
726 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000727 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +0000728 BytesToPopOnReturn = 4;
729
730 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000731 }
732
Evan Cheng25ab6902006-09-08 06:48:29 +0000733 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
734 ReturnAddrIndex = 0; // No return address slot generated yet.
Evan Cheng25caf632006-05-23 21:06:34 +0000735
Chris Lattnerd15dff22007-04-17 17:21:52 +0000736 MF.getInfo<X86MachineFunctionInfo>()
737 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +0000738
Evan Cheng25caf632006-05-23 21:06:34 +0000739 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +0000740 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +0000741 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000742}
743
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000744SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +0000745 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000746 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000747 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000748 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
749 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +0000750 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000751
Chris Lattner638402b2007-02-28 07:00:42 +0000752 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +0000753 SmallVector<CCValAssign, 16> ArgLocs;
754 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000755 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000756
Chris Lattner423c5f42007-02-28 05:31:48 +0000757 // Get a count of how many bytes are to be pushed on the stack.
758 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000759
Evan Cheng32fe1032006-05-25 00:59:30 +0000760 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000761
Chris Lattner5a88b832007-02-25 07:10:00 +0000762 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
763 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +0000764
Chris Lattner423c5f42007-02-28 05:31:48 +0000765 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +0000766
767 // Walk the register/memloc assignments, inserting copies/loads.
768 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
769 CCValAssign &VA = ArgLocs[i];
770 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000771
Chris Lattner423c5f42007-02-28 05:31:48 +0000772 // Promote the value if needed.
773 switch (VA.getLocInfo()) {
774 default: assert(0 && "Unknown loc info!");
775 case CCValAssign::Full: break;
776 case CCValAssign::SExt:
777 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
778 break;
779 case CCValAssign::ZExt:
780 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
781 break;
782 case CCValAssign::AExt:
783 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
784 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +0000785 }
Chris Lattner423c5f42007-02-28 05:31:48 +0000786
787 if (VA.isRegLoc()) {
788 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
789 } else {
790 assert(VA.isMemLoc());
791 if (StackPtr.Val == 0)
792 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
793 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000794 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
795 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000796 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000797 }
798
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000799 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000800 bool isSRet = NumOps &&
801 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000802 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000803
Evan Cheng32fe1032006-05-25 00:59:30 +0000804 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000805 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
806 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000807
Evan Cheng347d5f72006-04-28 21:29:37 +0000808 // Build a sequence of copy-to-reg nodes chained together with token chain
809 // and flag operands which copy the outgoing args into registers.
810 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000811 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
812 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
813 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000814 InFlag = Chain.getValue(1);
815 }
816
Evan Chengf4684712007-02-21 21:18:14 +0000817 // ELF / PIC requires GOT in the EBX register before function calls via PLT
818 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +0000819 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
820 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000821 Chain = DAG.getCopyToReg(Chain, X86::EBX,
822 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
823 InFlag);
824 InFlag = Chain.getValue(1);
825 }
826
Evan Cheng32fe1032006-05-25 00:59:30 +0000827 // If the callee is a GlobalAddress node (quite common, every direct call is)
828 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +0000829 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +0000830 // We should use extra load for direct calls to dllimported functions in
831 // non-JIT mode.
832 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
833 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +0000834 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
835 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +0000836 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
837
Chris Lattnerd96d0722007-02-25 06:40:16 +0000838 // Returns a chain & a flag for retval copy to use.
839 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +0000840 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000841 Ops.push_back(Chain);
842 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +0000843
844 // Add argument registers to the end of the list so that they are known live
845 // into the call.
846 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000847 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +0000848 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +0000849
850 // Add an implicit use GOT pointer in EBX.
851 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
852 Subtarget->isPICStyleGOT())
853 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000854
Evan Cheng347d5f72006-04-28 21:29:37 +0000855 if (InFlag.Val)
856 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000857
Evan Cheng32fe1032006-05-25 00:59:30 +0000858 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000859 NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +0000860 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000861
Chris Lattner2d297092006-05-23 18:50:38 +0000862 // Create the CALLSEQ_END node.
863 unsigned NumBytesForCalleeToPush = 0;
864
Chris Lattner09c75a42007-02-25 09:06:15 +0000865 if (CC == CallingConv::X86_StdCall) {
866 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000867 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +0000868 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000869 NumBytesForCalleeToPush = NumBytes;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000870 } else {
871 // If this is is a call to a struct-return function, the callee
872 // pops the hidden struct pointer, so we have to push it back.
873 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000874 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000875 }
876
Chris Lattner7d53a1c2007-02-25 07:18:38 +0000877 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000878 Ops.clear();
879 Ops.push_back(Chain);
880 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +0000881 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000882 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000883 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner3085e152007-02-25 08:59:22 +0000884 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000885
Chris Lattner3085e152007-02-25 08:59:22 +0000886 // Handle result values, copying them out of physregs into vregs that we
887 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +0000888 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000889}
890
Evan Cheng25ab6902006-09-08 06:48:29 +0000891
892//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +0000893// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000894//===----------------------------------------------------------------------===//
895//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000896// The X86 'fastcall' calling convention passes up to two integer arguments in
897// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
898// and requires that the callee pop its arguments off the stack (allowing proper
899// tail calls), and has the same return value conventions as C calling convs.
900//
901// This calling convention always arranges for the callee pop value to be 8n+4
902// bytes, which is needed for tail recursion elimination and stack alignment
903// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +0000904SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +0000905X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000906 MachineFunction &MF = DAG.getMachineFunction();
907 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000908 SDOperand Root = Op.getOperand(0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000909
Chris Lattner638402b2007-02-28 07:00:42 +0000910 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +0000911 SmallVector<CCValAssign, 16> ArgLocs;
912 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
913 ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000914 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +0000915
916 SmallVector<SDOperand, 8> ArgValues;
917 unsigned LastVal = ~0U;
918 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
919 CCValAssign &VA = ArgLocs[i];
920 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
921 // places.
922 assert(VA.getValNo() != LastVal &&
923 "Don't support value assigned to multiple locs yet");
924 LastVal = VA.getValNo();
925
926 if (VA.isRegLoc()) {
927 MVT::ValueType RegVT = VA.getLocVT();
928 TargetRegisterClass *RC;
929 if (RegVT == MVT::i32)
930 RC = X86::GR32RegisterClass;
931 else {
932 assert(MVT::isVector(RegVT));
933 RC = X86::VR128RegisterClass;
934 }
935
Chris Lattner82932a52007-03-02 05:12:29 +0000936 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
937 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +0000938
939 // If this is an 8 or 16-bit value, it is really passed promoted to 32
940 // bits. Insert an assert[sz]ext to capture this, then truncate to the
941 // right size.
942 if (VA.getLocInfo() == CCValAssign::SExt)
943 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
944 DAG.getValueType(VA.getValVT()));
945 else if (VA.getLocInfo() == CCValAssign::ZExt)
946 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
947 DAG.getValueType(VA.getValVT()));
948
949 if (VA.getLocInfo() != CCValAssign::Full)
950 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
951
952 ArgValues.push_back(ArgValue);
953 } else {
954 assert(VA.isMemLoc());
955
956 // Create the nodes corresponding to a load from this parameter slot.
957 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
958 VA.getLocMemOffset());
959 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
960 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
961 }
962 }
963
Evan Cheng25caf632006-05-23 21:06:34 +0000964 ArgValues.push_back(Root);
965
Chris Lattnerfc664c12007-02-28 06:21:19 +0000966 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +0000967
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +0000968 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +0000969 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
970 // arguments and the arguments after the retaddr has been pushed are aligned.
971 if ((StackSize & 7) == 0)
972 StackSize += 4;
973 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000974
975 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +0000976 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000977 ReturnAddrIndex = 0; // No return address slot generated yet.
Chris Lattnerfc664c12007-02-28 06:21:19 +0000978 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000979 BytesCallerReserves = 0;
980
Chris Lattnerd15dff22007-04-17 17:21:52 +0000981 MF.getInfo<X86MachineFunctionInfo>()
982 ->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000983
Evan Cheng25caf632006-05-23 21:06:34 +0000984 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +0000985 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +0000986 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000987}
988
Chris Lattnere87e1152006-09-26 03:57:53 +0000989SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +0000990 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000991 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +0000992 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
993 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +0000994
Chris Lattner638402b2007-02-28 07:00:42 +0000995 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +0000996 SmallVector<CCValAssign, 16> ArgLocs;
997 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000998 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +0000999
1000 // Get a count of how many bytes are to be pushed on the stack.
1001 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001002
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001003 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001004 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1005 // arguments and the arguments after the retaddr has been pushed are aligned.
1006 if ((NumBytes & 7) == 0)
1007 NumBytes += 4;
1008 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001009
Chris Lattner94dd2922006-02-13 09:00:43 +00001010 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001011
Chris Lattner5a88b832007-02-25 07:10:00 +00001012 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1013 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001014
1015 SDOperand StackPtr;
1016
1017 // Walk the register/memloc assignments, inserting copies/loads.
1018 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1019 CCValAssign &VA = ArgLocs[i];
1020 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1021
1022 // Promote the value if needed.
1023 switch (VA.getLocInfo()) {
1024 default: assert(0 && "Unknown loc info!");
1025 case CCValAssign::Full: break;
1026 case CCValAssign::SExt:
1027 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001028 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001029 case CCValAssign::ZExt:
1030 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1031 break;
1032 case CCValAssign::AExt:
1033 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1034 break;
1035 }
1036
1037 if (VA.isRegLoc()) {
1038 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1039 } else {
1040 assert(VA.isMemLoc());
1041 if (StackPtr.Val == 0)
1042 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1043 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
Evan Cheng32fe1032006-05-25 00:59:30 +00001044 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Evan Cheng8b2794a2006-10-13 21:14:26 +00001045 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
Evan Cheng32fe1032006-05-25 00:59:30 +00001046 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001047 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001048
Evan Cheng32fe1032006-05-25 00:59:30 +00001049 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001050 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1051 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001052
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001053 // Build a sequence of copy-to-reg nodes chained together with token chain
1054 // and flag operands which copy the outgoing args into registers.
1055 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001056 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1057 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1058 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001059 InFlag = Chain.getValue(1);
1060 }
1061
Evan Cheng32fe1032006-05-25 00:59:30 +00001062 // If the callee is a GlobalAddress node (quite common, every direct call is)
1063 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001064 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001065 // We should use extra load for direct calls to dllimported functions in
1066 // non-JIT mode.
1067 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1068 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001069 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1070 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001071 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1072
Evan Chengf4684712007-02-21 21:18:14 +00001073 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1074 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001075 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1076 Subtarget->isPICStyleGOT()) {
1077 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1078 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1079 InFlag);
1080 InFlag = Chain.getValue(1);
1081 }
1082
Chris Lattnerd96d0722007-02-25 06:40:16 +00001083 // Returns a chain & a flag for retval copy to use.
1084 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001085 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001086 Ops.push_back(Chain);
1087 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001088
1089 // Add argument registers to the end of the list so that they are known live
1090 // into the call.
1091 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001092 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001093 RegsToPass[i].second.getValueType()));
1094
Evan Chengf4684712007-02-21 21:18:14 +00001095 // Add an implicit use GOT pointer in EBX.
1096 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1097 Subtarget->isPICStyleGOT())
1098 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1099
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001100 if (InFlag.Val)
1101 Ops.push_back(InFlag);
1102
1103 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001104 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001105 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001106 InFlag = Chain.getValue(1);
1107
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001108 // Returns a flag for retval copy to use.
1109 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001110 Ops.clear();
1111 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001112 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1113 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001114 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001115 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001116 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001117
Chris Lattner339b4392007-02-25 09:10:05 +00001118 // Handle result values, copying them out of physregs into vregs that we
1119 // return.
1120 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001121}
1122
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001123
1124//===----------------------------------------------------------------------===//
1125// X86-64 C Calling Convention implementation
1126//===----------------------------------------------------------------------===//
1127
1128SDOperand
1129X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001130 MachineFunction &MF = DAG.getMachineFunction();
1131 MachineFrameInfo *MFI = MF.getFrameInfo();
1132 SDOperand Root = Op.getOperand(0);
1133 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1134
1135 static const unsigned GPR64ArgRegs[] = {
1136 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1137 };
1138 static const unsigned XMMArgRegs[] = {
1139 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1140 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1141 };
1142
Chris Lattner638402b2007-02-28 07:00:42 +00001143
1144 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001145 SmallVector<CCValAssign, 16> ArgLocs;
1146 CCState CCInfo(MF.getFunction()->getCallingConv(), getTargetMachine(),
1147 ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001148 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001149
1150 SmallVector<SDOperand, 8> ArgValues;
1151 unsigned LastVal = ~0U;
1152 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1153 CCValAssign &VA = ArgLocs[i];
1154 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1155 // places.
1156 assert(VA.getValNo() != LastVal &&
1157 "Don't support value assigned to multiple locs yet");
1158 LastVal = VA.getValNo();
1159
1160 if (VA.isRegLoc()) {
1161 MVT::ValueType RegVT = VA.getLocVT();
1162 TargetRegisterClass *RC;
1163 if (RegVT == MVT::i32)
1164 RC = X86::GR32RegisterClass;
1165 else if (RegVT == MVT::i64)
1166 RC = X86::GR64RegisterClass;
1167 else if (RegVT == MVT::f32)
1168 RC = X86::FR32RegisterClass;
1169 else if (RegVT == MVT::f64)
1170 RC = X86::FR64RegisterClass;
1171 else {
1172 assert(MVT::isVector(RegVT));
1173 RC = X86::VR128RegisterClass;
1174 }
Chris Lattner82932a52007-03-02 05:12:29 +00001175
1176 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1177 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001178
1179 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1180 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1181 // right size.
1182 if (VA.getLocInfo() == CCValAssign::SExt)
1183 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1184 DAG.getValueType(VA.getValVT()));
1185 else if (VA.getLocInfo() == CCValAssign::ZExt)
1186 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1187 DAG.getValueType(VA.getValVT()));
1188
1189 if (VA.getLocInfo() != CCValAssign::Full)
1190 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1191
1192 ArgValues.push_back(ArgValue);
1193 } else {
1194 assert(VA.isMemLoc());
1195
1196 // Create the nodes corresponding to a load from this parameter slot.
1197 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
1198 VA.getLocMemOffset());
1199 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
1200 ArgValues.push_back(DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0));
1201 }
1202 }
1203
1204 unsigned StackSize = CCInfo.getNextStackOffset();
1205
1206 // If the function takes variable number of arguments, make a frame index for
1207 // the start of the first vararg value... for expansion of llvm.va_start.
1208 if (isVarArg) {
1209 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1210 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1211
1212 // For X86-64, if there are vararg parameters that are passed via
1213 // registers, then we must store them to their spots on the stack so they
1214 // may be loaded by deferencing the result of va_next.
1215 VarArgsGPOffset = NumIntRegs * 8;
1216 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1217 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1218 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1219
1220 // Store the integer parameter registers.
1221 SmallVector<SDOperand, 8> MemOps;
1222 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1223 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1224 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1225 for (; NumIntRegs != 6; ++NumIntRegs) {
1226 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1227 X86::GR64RegisterClass);
1228 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1229 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1230 MemOps.push_back(Store);
1231 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1232 DAG.getConstant(8, getPointerTy()));
1233 }
1234
1235 // Now store the XMM (fp + vector) parameter registers.
1236 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1237 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1238 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1239 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1240 X86::VR128RegisterClass);
1241 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1242 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1243 MemOps.push_back(Store);
1244 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1245 DAG.getConstant(16, getPointerTy()));
1246 }
1247 if (!MemOps.empty())
1248 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1249 &MemOps[0], MemOps.size());
1250 }
1251
1252 ArgValues.push_back(Root);
1253
1254 ReturnAddrIndex = 0; // No return address slot generated yet.
1255 BytesToPopOnReturn = 0; // Callee pops nothing.
1256 BytesCallerReserves = StackSize;
1257
1258 // Return the new list of results.
1259 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1260 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1261}
1262
1263SDOperand
1264X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1265 unsigned CC) {
1266 SDOperand Chain = Op.getOperand(0);
1267 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1268 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1269 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001270
1271 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001272 SmallVector<CCValAssign, 16> ArgLocs;
1273 CCState CCInfo(CC, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001274 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001275
1276 // Get a count of how many bytes are to be pushed on the stack.
1277 unsigned NumBytes = CCInfo.getNextStackOffset();
1278 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1279
1280 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1281 SmallVector<SDOperand, 8> MemOpChains;
1282
1283 SDOperand StackPtr;
1284
1285 // Walk the register/memloc assignments, inserting copies/loads.
1286 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1287 CCValAssign &VA = ArgLocs[i];
1288 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1289
1290 // Promote the value if needed.
1291 switch (VA.getLocInfo()) {
1292 default: assert(0 && "Unknown loc info!");
1293 case CCValAssign::Full: break;
1294 case CCValAssign::SExt:
1295 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1296 break;
1297 case CCValAssign::ZExt:
1298 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1299 break;
1300 case CCValAssign::AExt:
1301 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1302 break;
1303 }
1304
1305 if (VA.isRegLoc()) {
1306 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1307 } else {
1308 assert(VA.isMemLoc());
1309 if (StackPtr.Val == 0)
1310 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
1311 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1312 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1313 MemOpChains.push_back(DAG.getStore(Chain, Arg, PtrOff, NULL, 0));
1314 }
1315 }
1316
1317 if (!MemOpChains.empty())
1318 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1319 &MemOpChains[0], MemOpChains.size());
1320
1321 // Build a sequence of copy-to-reg nodes chained together with token chain
1322 // and flag operands which copy the outgoing args into registers.
1323 SDOperand InFlag;
1324 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1325 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1326 InFlag);
1327 InFlag = Chain.getValue(1);
1328 }
1329
1330 if (isVarArg) {
1331 // From AMD64 ABI document:
1332 // For calls that may call functions that use varargs or stdargs
1333 // (prototype-less calls or calls to functions containing ellipsis (...) in
1334 // the declaration) %al is used as hidden argument to specify the number
1335 // of SSE registers used. The contents of %al do not need to match exactly
1336 // the number of registers, but must be an ubound on the number of SSE
1337 // registers used and is in the range 0 - 8 inclusive.
1338
1339 // Count the number of XMM registers allocated.
1340 static const unsigned XMMArgRegs[] = {
1341 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1342 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1343 };
1344 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1345
1346 Chain = DAG.getCopyToReg(Chain, X86::AL,
1347 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1348 InFlag = Chain.getValue(1);
1349 }
1350
1351 // If the callee is a GlobalAddress node (quite common, every direct call is)
1352 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1353 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1354 // We should use extra load for direct calls to dllimported functions in
1355 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00001356 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001357 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1358 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001359 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1360 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00001361 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1362 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001363
1364 // Returns a chain & a flag for retval copy to use.
1365 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1366 SmallVector<SDOperand, 8> Ops;
1367 Ops.push_back(Chain);
1368 Ops.push_back(Callee);
1369
1370 // Add argument registers to the end of the list so that they are known live
1371 // into the call.
1372 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1373 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1374 RegsToPass[i].second.getValueType()));
1375
1376 if (InFlag.Val)
1377 Ops.push_back(InFlag);
1378
1379 // FIXME: Do not generate X86ISD::TAILCALL for now.
1380 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1381 NodeTys, &Ops[0], Ops.size());
1382 InFlag = Chain.getValue(1);
1383
1384 // Returns a flag for retval copy to use.
1385 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1386 Ops.clear();
1387 Ops.push_back(Chain);
1388 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1389 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1390 Ops.push_back(InFlag);
1391 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1392 InFlag = Chain.getValue(1);
1393
1394 // Handle result values, copying them out of physregs into vregs that we
1395 // return.
1396 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1397}
1398
1399
1400//===----------------------------------------------------------------------===//
1401// Other Lowering Hooks
1402//===----------------------------------------------------------------------===//
1403
1404
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001405SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
1406 if (ReturnAddrIndex == 0) {
1407 // Set up a frame object for the return address.
1408 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng25ab6902006-09-08 06:48:29 +00001409 if (Subtarget->is64Bit())
1410 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1411 else
1412 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001413 }
1414
Evan Cheng25ab6902006-09-08 06:48:29 +00001415 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001416}
1417
1418
1419
Evan Cheng6dfa9992006-01-30 23:41:35 +00001420/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1421/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001422/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1423/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001424static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001425 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1426 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001427 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001428 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001429 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1430 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1431 // X > -1 -> X == 0, jump !sign.
1432 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001433 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001434 return true;
1435 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1436 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001437 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001438 return true;
1439 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001440 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001441
Evan Chengd9558e02006-01-06 00:43:03 +00001442 switch (SetCCOpcode) {
1443 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001444 case ISD::SETEQ: X86CC = X86::COND_E; break;
1445 case ISD::SETGT: X86CC = X86::COND_G; break;
1446 case ISD::SETGE: X86CC = X86::COND_GE; break;
1447 case ISD::SETLT: X86CC = X86::COND_L; break;
1448 case ISD::SETLE: X86CC = X86::COND_LE; break;
1449 case ISD::SETNE: X86CC = X86::COND_NE; break;
1450 case ISD::SETULT: X86CC = X86::COND_B; break;
1451 case ISD::SETUGT: X86CC = X86::COND_A; break;
1452 case ISD::SETULE: X86CC = X86::COND_BE; break;
1453 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001454 }
1455 } else {
1456 // On a floating point condition, the flags are set as follows:
1457 // ZF PF CF op
1458 // 0 | 0 | 0 | X > Y
1459 // 0 | 0 | 1 | X < Y
1460 // 1 | 0 | 0 | X == Y
1461 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001462 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001463 switch (SetCCOpcode) {
1464 default: break;
1465 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001466 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001467 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001468 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001469 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001470 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001471 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001472 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001473 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001474 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001475 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001476 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001477 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001478 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001479 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001480 case ISD::SETNE: X86CC = X86::COND_NE; break;
1481 case ISD::SETUO: X86CC = X86::COND_P; break;
1482 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001483 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001484 if (Flip)
1485 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001486 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001487
Chris Lattner7fbe9722006-10-20 17:42:20 +00001488 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001489}
1490
Evan Cheng4a460802006-01-11 00:33:36 +00001491/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1492/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001493/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001494static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001495 switch (X86CC) {
1496 default:
1497 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001498 case X86::COND_B:
1499 case X86::COND_BE:
1500 case X86::COND_E:
1501 case X86::COND_P:
1502 case X86::COND_A:
1503 case X86::COND_AE:
1504 case X86::COND_NE:
1505 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001506 return true;
1507 }
1508}
1509
Evan Cheng5ced1d82006-04-06 23:23:56 +00001510/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001511/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001512static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1513 if (Op.getOpcode() == ISD::UNDEF)
1514 return true;
1515
1516 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001517 return (Val >= Low && Val < Hi);
1518}
1519
1520/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1521/// true if Op is undef or if its value equal to the specified value.
1522static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1523 if (Op.getOpcode() == ISD::UNDEF)
1524 return true;
1525 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001526}
1527
Evan Cheng0188ecb2006-03-22 18:59:22 +00001528/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1529/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1530bool X86::isPSHUFDMask(SDNode *N) {
1531 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1532
1533 if (N->getNumOperands() != 4)
1534 return false;
1535
1536 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001537 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001538 SDOperand Arg = N->getOperand(i);
1539 if (Arg.getOpcode() == ISD::UNDEF) continue;
1540 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1541 if (cast<ConstantSDNode>(Arg)->getValue() >= 4)
Evan Cheng506d3df2006-03-29 23:07:14 +00001542 return false;
1543 }
1544
1545 return true;
1546}
1547
1548/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001549/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001550bool X86::isPSHUFHWMask(SDNode *N) {
1551 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1552
1553 if (N->getNumOperands() != 8)
1554 return false;
1555
1556 // Lower quadword copied in order.
1557 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001558 SDOperand Arg = N->getOperand(i);
1559 if (Arg.getOpcode() == ISD::UNDEF) continue;
1560 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1561 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001562 return false;
1563 }
1564
1565 // Upper quadword shuffled.
1566 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001567 SDOperand Arg = N->getOperand(i);
1568 if (Arg.getOpcode() == ISD::UNDEF) continue;
1569 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1570 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001571 if (Val < 4 || Val > 7)
1572 return false;
1573 }
1574
1575 return true;
1576}
1577
1578/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001579/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001580bool X86::isPSHUFLWMask(SDNode *N) {
1581 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1582
1583 if (N->getNumOperands() != 8)
1584 return false;
1585
1586 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001587 for (unsigned i = 4; i != 8; ++i)
1588 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001589 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001590
1591 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001592 for (unsigned i = 0; i != 4; ++i)
1593 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001594 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001595
1596 return true;
1597}
1598
Evan Cheng14aed5e2006-03-24 01:18:28 +00001599/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1600/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00001601static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00001602 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001603
Evan Cheng39623da2006-04-20 08:58:49 +00001604 unsigned Half = NumElems / 2;
1605 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001606 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00001607 return false;
1608 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001609 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00001610 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001611
1612 return true;
1613}
1614
Evan Cheng39623da2006-04-20 08:58:49 +00001615bool X86::isSHUFPMask(SDNode *N) {
1616 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001617 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001618}
1619
1620/// isCommutedSHUFP - Returns true if the shuffle mask is except
1621/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1622/// half elements to come from vector 1 (which would equal the dest.) and
1623/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00001624static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1625 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001626
Chris Lattner5a88b832007-02-25 07:10:00 +00001627 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00001628 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001629 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00001630 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00001631 for (unsigned i = Half; i < NumOps; ++i)
1632 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00001633 return false;
1634 return true;
1635}
1636
1637static bool isCommutedSHUFP(SDNode *N) {
1638 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001639 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001640}
1641
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001642/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1643/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1644bool X86::isMOVHLPSMask(SDNode *N) {
1645 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1646
Evan Cheng2064a2b2006-03-28 06:50:32 +00001647 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001648 return false;
1649
Evan Cheng2064a2b2006-03-28 06:50:32 +00001650 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001651 return isUndefOrEqual(N->getOperand(0), 6) &&
1652 isUndefOrEqual(N->getOperand(1), 7) &&
1653 isUndefOrEqual(N->getOperand(2), 2) &&
1654 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001655}
1656
Evan Cheng6e56e2c2006-11-07 22:14:24 +00001657/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1658/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1659/// <2, 3, 2, 3>
1660bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1661 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1662
1663 if (N->getNumOperands() != 4)
1664 return false;
1665
1666 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1667 return isUndefOrEqual(N->getOperand(0), 2) &&
1668 isUndefOrEqual(N->getOperand(1), 3) &&
1669 isUndefOrEqual(N->getOperand(2), 2) &&
1670 isUndefOrEqual(N->getOperand(3), 3);
1671}
1672
Evan Cheng5ced1d82006-04-06 23:23:56 +00001673/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1674/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1675bool X86::isMOVLPMask(SDNode *N) {
1676 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1677
1678 unsigned NumElems = N->getNumOperands();
1679 if (NumElems != 2 && NumElems != 4)
1680 return false;
1681
Evan Chengc5cdff22006-04-07 21:53:05 +00001682 for (unsigned i = 0; i < NumElems/2; ++i)
1683 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1684 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001685
Evan Chengc5cdff22006-04-07 21:53:05 +00001686 for (unsigned i = NumElems/2; i < NumElems; ++i)
1687 if (!isUndefOrEqual(N->getOperand(i), i))
1688 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001689
1690 return true;
1691}
1692
1693/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00001694/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1695/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00001696bool X86::isMOVHPMask(SDNode *N) {
1697 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1698
1699 unsigned NumElems = N->getNumOperands();
1700 if (NumElems != 2 && NumElems != 4)
1701 return false;
1702
Evan Chengc5cdff22006-04-07 21:53:05 +00001703 for (unsigned i = 0; i < NumElems/2; ++i)
1704 if (!isUndefOrEqual(N->getOperand(i), i))
1705 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001706
1707 for (unsigned i = 0; i < NumElems/2; ++i) {
1708 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001709 if (!isUndefOrEqual(Arg, i + NumElems))
1710 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001711 }
1712
1713 return true;
1714}
1715
Evan Cheng0038e592006-03-28 00:39:58 +00001716/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1717/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00001718bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1719 bool V2IsSplat = false) {
1720 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00001721 return false;
1722
Chris Lattner5a88b832007-02-25 07:10:00 +00001723 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1724 SDOperand BitI = Elts[i];
1725 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001726 if (!isUndefOrEqual(BitI, j))
1727 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001728 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001729 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001730 return false;
1731 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00001732 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001733 return false;
1734 }
Evan Cheng0038e592006-03-28 00:39:58 +00001735 }
1736
1737 return true;
1738}
1739
Evan Cheng39623da2006-04-20 08:58:49 +00001740bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1741 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001742 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001743}
1744
Evan Cheng4fcb9222006-03-28 02:43:26 +00001745/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1746/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00001747bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1748 bool V2IsSplat = false) {
1749 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00001750 return false;
1751
Chris Lattner5a88b832007-02-25 07:10:00 +00001752 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1753 SDOperand BitI = Elts[i];
1754 SDOperand BitI1 = Elts[i+1];
1755 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00001756 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001757 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001758 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001759 return false;
1760 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00001761 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001762 return false;
1763 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001764 }
1765
1766 return true;
1767}
1768
Evan Cheng39623da2006-04-20 08:58:49 +00001769bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1770 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001771 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001772}
1773
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001774/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1775/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1776/// <0, 0, 1, 1>
1777bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1778 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1779
1780 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00001781 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001782 return false;
1783
1784 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1785 SDOperand BitI = N->getOperand(i);
1786 SDOperand BitI1 = N->getOperand(i+1);
1787
Evan Chengc5cdff22006-04-07 21:53:05 +00001788 if (!isUndefOrEqual(BitI, j))
1789 return false;
1790 if (!isUndefOrEqual(BitI1, j))
1791 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001792 }
1793
1794 return true;
1795}
1796
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00001797/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1798/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1799/// <2, 2, 3, 3>
1800bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1801 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1802
1803 unsigned NumElems = N->getNumOperands();
1804 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1805 return false;
1806
1807 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1808 SDOperand BitI = N->getOperand(i);
1809 SDOperand BitI1 = N->getOperand(i + 1);
1810
1811 if (!isUndefOrEqual(BitI, j))
1812 return false;
1813 if (!isUndefOrEqual(BitI1, j))
1814 return false;
1815 }
1816
1817 return true;
1818}
1819
Evan Cheng017dcc62006-04-21 01:05:10 +00001820/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1821/// specifies a shuffle of elements that is suitable for input to MOVSS,
1822/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00001823static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1824 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001825 return false;
1826
Chris Lattner5a88b832007-02-25 07:10:00 +00001827 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001828 return false;
1829
Chris Lattner5a88b832007-02-25 07:10:00 +00001830 for (unsigned i = 1; i < NumElts; ++i) {
1831 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001832 return false;
1833 }
1834
1835 return true;
1836}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001837
Evan Cheng017dcc62006-04-21 01:05:10 +00001838bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00001839 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001840 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001841}
1842
Evan Cheng017dcc62006-04-21 01:05:10 +00001843/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
1844/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00001845/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00001846static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
1847 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00001848 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001849 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00001850 return false;
1851
1852 if (!isUndefOrEqual(Ops[0], 0))
1853 return false;
1854
Chris Lattner5a88b832007-02-25 07:10:00 +00001855 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00001856 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00001857 if (!(isUndefOrEqual(Arg, i+NumOps) ||
1858 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
1859 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00001860 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001861 }
1862
1863 return true;
1864}
1865
Evan Cheng8cf723d2006-09-08 01:50:06 +00001866static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
1867 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00001868 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001869 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
1870 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00001871}
1872
Evan Chengd9539472006-04-14 21:59:03 +00001873/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1874/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
1875bool X86::isMOVSHDUPMask(SDNode *N) {
1876 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1877
1878 if (N->getNumOperands() != 4)
1879 return false;
1880
1881 // Expect 1, 1, 3, 3
1882 for (unsigned i = 0; i < 2; ++i) {
1883 SDOperand Arg = N->getOperand(i);
1884 if (Arg.getOpcode() == ISD::UNDEF) continue;
1885 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1886 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1887 if (Val != 1) return false;
1888 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001889
1890 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001891 for (unsigned i = 2; i < 4; ++i) {
1892 SDOperand Arg = N->getOperand(i);
1893 if (Arg.getOpcode() == ISD::UNDEF) continue;
1894 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1895 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1896 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001897 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001898 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001899
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001900 // Don't use movshdup if it can be done with a shufps.
1901 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001902}
1903
1904/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
1905/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
1906bool X86::isMOVSLDUPMask(SDNode *N) {
1907 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1908
1909 if (N->getNumOperands() != 4)
1910 return false;
1911
1912 // Expect 0, 0, 2, 2
1913 for (unsigned i = 0; i < 2; ++i) {
1914 SDOperand Arg = N->getOperand(i);
1915 if (Arg.getOpcode() == ISD::UNDEF) continue;
1916 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1917 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1918 if (Val != 0) return false;
1919 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001920
1921 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00001922 for (unsigned i = 2; i < 4; ++i) {
1923 SDOperand Arg = N->getOperand(i);
1924 if (Arg.getOpcode() == ISD::UNDEF) continue;
1925 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1926 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
1927 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001928 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00001929 }
Evan Cheng39fc1452006-04-15 03:13:24 +00001930
Evan Cheng57ebe9f2006-04-15 05:37:34 +00001931 // Don't use movshdup if it can be done with a shufps.
1932 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00001933}
1934
Evan Chengb9df0ca2006-03-22 02:53:00 +00001935/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1936/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00001937static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001938 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1939
Evan Chengb9df0ca2006-03-22 02:53:00 +00001940 // This is a splat operation if each element of the permute is the same, and
1941 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001942 unsigned NumElems = N->getNumOperands();
1943 SDOperand ElementBase;
1944 unsigned i = 0;
1945 for (; i != NumElems; ++i) {
1946 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00001947 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001948 ElementBase = Elt;
1949 break;
1950 }
1951 }
1952
1953 if (!ElementBase.Val)
1954 return false;
1955
1956 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001957 SDOperand Arg = N->getOperand(i);
1958 if (Arg.getOpcode() == ISD::UNDEF) continue;
1959 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001960 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001961 }
1962
1963 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001964 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00001965}
1966
Evan Chengc575ca22006-04-17 20:43:08 +00001967/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
1968/// a splat of a single element and it's a 2 or 4 element mask.
1969bool X86::isSplatMask(SDNode *N) {
1970 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1971
Evan Cheng94fe5eb2006-04-19 23:28:59 +00001972 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00001973 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
1974 return false;
1975 return ::isSplatMask(N);
1976}
1977
Evan Chengf686d9b2006-10-27 21:08:32 +00001978/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
1979/// specifies a splat of zero element.
1980bool X86::isSplatLoMask(SDNode *N) {
1981 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1982
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001983 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00001984 if (!isUndefOrEqual(N->getOperand(i), 0))
1985 return false;
1986 return true;
1987}
1988
Evan Cheng63d33002006-03-22 08:01:21 +00001989/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
1990/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
1991/// instructions.
1992unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00001993 unsigned NumOperands = N->getNumOperands();
1994 unsigned Shift = (NumOperands == 4) ? 2 : 1;
1995 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00001996 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001997 unsigned Val = 0;
1998 SDOperand Arg = N->getOperand(NumOperands-i-1);
1999 if (Arg.getOpcode() != ISD::UNDEF)
2000 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002001 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002002 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002003 if (i != NumOperands - 1)
2004 Mask <<= Shift;
2005 }
Evan Cheng63d33002006-03-22 08:01:21 +00002006
2007 return Mask;
2008}
2009
Evan Cheng506d3df2006-03-29 23:07:14 +00002010/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2011/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2012/// instructions.
2013unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2014 unsigned Mask = 0;
2015 // 8 nodes, but we only care about the last 4.
2016 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002017 unsigned Val = 0;
2018 SDOperand Arg = N->getOperand(i);
2019 if (Arg.getOpcode() != ISD::UNDEF)
2020 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002021 Mask |= (Val - 4);
2022 if (i != 4)
2023 Mask <<= 2;
2024 }
2025
2026 return Mask;
2027}
2028
2029/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2030/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2031/// instructions.
2032unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2033 unsigned Mask = 0;
2034 // 8 nodes, but we only care about the first 4.
2035 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002036 unsigned Val = 0;
2037 SDOperand Arg = N->getOperand(i);
2038 if (Arg.getOpcode() != ISD::UNDEF)
2039 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002040 Mask |= Val;
2041 if (i != 0)
2042 Mask <<= 2;
2043 }
2044
2045 return Mask;
2046}
2047
Evan Chengc21a0532006-04-05 01:47:37 +00002048/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2049/// specifies a 8 element shuffle that can be broken into a pair of
2050/// PSHUFHW and PSHUFLW.
2051static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2052 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2053
2054 if (N->getNumOperands() != 8)
2055 return false;
2056
2057 // Lower quadword shuffled.
2058 for (unsigned i = 0; i != 4; ++i) {
2059 SDOperand Arg = N->getOperand(i);
2060 if (Arg.getOpcode() == ISD::UNDEF) continue;
2061 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2062 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2063 if (Val > 4)
2064 return false;
2065 }
2066
2067 // Upper quadword shuffled.
2068 for (unsigned i = 4; i != 8; ++i) {
2069 SDOperand Arg = N->getOperand(i);
2070 if (Arg.getOpcode() == ISD::UNDEF) continue;
2071 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2072 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2073 if (Val < 4 || Val > 7)
2074 return false;
2075 }
2076
2077 return true;
2078}
2079
Evan Cheng5ced1d82006-04-06 23:23:56 +00002080/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2081/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002082static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2083 SDOperand &V2, SDOperand &Mask,
2084 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002085 MVT::ValueType VT = Op.getValueType();
2086 MVT::ValueType MaskVT = Mask.getValueType();
2087 MVT::ValueType EltVT = MVT::getVectorBaseType(MaskVT);
2088 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002089 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002090
2091 for (unsigned i = 0; i != NumElems; ++i) {
2092 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002093 if (Arg.getOpcode() == ISD::UNDEF) {
2094 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2095 continue;
2096 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002097 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2098 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2099 if (Val < NumElems)
2100 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2101 else
2102 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2103 }
2104
Evan Cheng9eca5e82006-10-25 21:49:50 +00002105 std::swap(V1, V2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002106 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng9eca5e82006-10-25 21:49:50 +00002107 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002108}
2109
Evan Cheng533a0aa2006-04-19 20:35:22 +00002110/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2111/// match movhlps. The lower half elements should come from upper half of
2112/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002113/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002114static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2115 unsigned NumElems = Mask->getNumOperands();
2116 if (NumElems != 4)
2117 return false;
2118 for (unsigned i = 0, e = 2; i != e; ++i)
2119 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2120 return false;
2121 for (unsigned i = 2; i != 4; ++i)
2122 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2123 return false;
2124 return true;
2125}
2126
Evan Cheng5ced1d82006-04-06 23:23:56 +00002127/// isScalarLoadToVector - Returns true if the node is a scalar load that
2128/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002129static inline bool isScalarLoadToVector(SDNode *N) {
2130 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2131 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002132 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002133 }
2134 return false;
2135}
2136
Evan Cheng533a0aa2006-04-19 20:35:22 +00002137/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2138/// match movlp{s|d}. The lower half elements should come from lower half of
2139/// V1 (and in order), and the upper half elements should come from the upper
2140/// half of V2 (and in order). And since V1 will become the source of the
2141/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002142static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002143 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002144 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002145 // Is V2 is a vector load, don't do this transformation. We will try to use
2146 // load folding shufps op.
2147 if (ISD::isNON_EXTLoad(V2))
2148 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002149
Evan Cheng533a0aa2006-04-19 20:35:22 +00002150 unsigned NumElems = Mask->getNumOperands();
2151 if (NumElems != 2 && NumElems != 4)
2152 return false;
2153 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2154 if (!isUndefOrEqual(Mask->getOperand(i), i))
2155 return false;
2156 for (unsigned i = NumElems/2; i != NumElems; ++i)
2157 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2158 return false;
2159 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002160}
2161
Evan Cheng39623da2006-04-20 08:58:49 +00002162/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2163/// all the same.
2164static bool isSplatVector(SDNode *N) {
2165 if (N->getOpcode() != ISD::BUILD_VECTOR)
2166 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002167
Evan Cheng39623da2006-04-20 08:58:49 +00002168 SDOperand SplatValue = N->getOperand(0);
2169 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2170 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002171 return false;
2172 return true;
2173}
2174
Evan Cheng8cf723d2006-09-08 01:50:06 +00002175/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2176/// to an undef.
2177static bool isUndefShuffle(SDNode *N) {
2178 if (N->getOpcode() != ISD::BUILD_VECTOR)
2179 return false;
2180
2181 SDOperand V1 = N->getOperand(0);
2182 SDOperand V2 = N->getOperand(1);
2183 SDOperand Mask = N->getOperand(2);
2184 unsigned NumElems = Mask.getNumOperands();
2185 for (unsigned i = 0; i != NumElems; ++i) {
2186 SDOperand Arg = Mask.getOperand(i);
2187 if (Arg.getOpcode() != ISD::UNDEF) {
2188 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2189 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2190 return false;
2191 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2192 return false;
2193 }
2194 }
2195 return true;
2196}
2197
Evan Cheng39623da2006-04-20 08:58:49 +00002198/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2199/// that point to V2 points to its first element.
2200static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2201 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2202
2203 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002204 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002205 unsigned NumElems = Mask.getNumOperands();
2206 for (unsigned i = 0; i != NumElems; ++i) {
2207 SDOperand Arg = Mask.getOperand(i);
2208 if (Arg.getOpcode() != ISD::UNDEF) {
2209 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2210 if (Val > NumElems) {
2211 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2212 Changed = true;
2213 }
2214 }
2215 MaskVec.push_back(Arg);
2216 }
2217
2218 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002219 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2220 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002221 return Mask;
2222}
2223
Evan Cheng017dcc62006-04-21 01:05:10 +00002224/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2225/// operation of specified width.
2226static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002227 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2228 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2229
Chris Lattner5a88b832007-02-25 07:10:00 +00002230 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002231 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2232 for (unsigned i = 1; i != NumElems; ++i)
2233 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002234 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002235}
2236
Evan Chengc575ca22006-04-17 20:43:08 +00002237/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2238/// of specified width.
2239static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2240 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2241 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002242 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002243 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2244 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2245 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2246 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002247 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002248}
2249
Evan Cheng39623da2006-04-20 08:58:49 +00002250/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2251/// of specified width.
2252static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2253 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2254 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
2255 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002256 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002257 for (unsigned i = 0; i != Half; ++i) {
2258 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2259 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2260 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002261 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002262}
2263
Evan Cheng017dcc62006-04-21 01:05:10 +00002264/// getZeroVector - Returns a vector of specified type with all zero elements.
2265///
2266static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2267 assert(MVT::isVector(VT) && "Expected a vector type");
2268 unsigned NumElems = getVectorNumElements(VT);
2269 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2270 bool isFP = MVT::isFloatingPoint(EVT);
2271 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002272 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002273 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
Evan Cheng017dcc62006-04-21 01:05:10 +00002274}
2275
Evan Chengc575ca22006-04-17 20:43:08 +00002276/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2277///
2278static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2279 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002280 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002281 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002282 unsigned NumElems = Mask.getNumOperands();
2283 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002284 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002285 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002286 NumElems >>= 1;
2287 }
2288 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2289
2290 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002291 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002292 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002293 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002294 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2295}
2296
Evan Cheng017dcc62006-04-21 01:05:10 +00002297/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2298/// constant +0.0.
2299static inline bool isZeroNode(SDOperand Elt) {
2300 return ((isa<ConstantSDNode>(Elt) &&
2301 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2302 (isa<ConstantFPSDNode>(Elt) &&
2303 cast<ConstantFPSDNode>(Elt)->isExactlyValue(0.0)));
2304}
2305
Evan Chengba05f722006-04-21 23:03:30 +00002306/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
2307/// vector and zero or undef vector.
2308static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002309 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002310 bool isZero, SelectionDAG &DAG) {
2311 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002312 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2313 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
2314 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002315 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Cheng017dcc62006-04-21 01:05:10 +00002316 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002317 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2318 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002319 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002320}
2321
Evan Chengc78d3b42006-04-24 18:01:45 +00002322/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2323///
2324static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2325 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002326 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002327 if (NumNonZero > 8)
2328 return SDOperand();
2329
2330 SDOperand V(0, 0);
2331 bool First = true;
2332 for (unsigned i = 0; i < 16; ++i) {
2333 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2334 if (ThisIsNonZero && First) {
2335 if (NumZero)
2336 V = getZeroVector(MVT::v8i16, DAG);
2337 else
2338 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2339 First = false;
2340 }
2341
2342 if ((i & 1) != 0) {
2343 SDOperand ThisElt(0, 0), LastElt(0, 0);
2344 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2345 if (LastIsNonZero) {
2346 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2347 }
2348 if (ThisIsNonZero) {
2349 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2350 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2351 ThisElt, DAG.getConstant(8, MVT::i8));
2352 if (LastIsNonZero)
2353 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2354 } else
2355 ThisElt = LastElt;
2356
2357 if (ThisElt.Val)
2358 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00002359 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002360 }
2361 }
2362
2363 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2364}
2365
Bill Wendlinga348c562007-03-22 18:42:45 +00002366/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002367///
2368static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2369 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002370 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002371 if (NumNonZero > 4)
2372 return SDOperand();
2373
2374 SDOperand V(0, 0);
2375 bool First = true;
2376 for (unsigned i = 0; i < 8; ++i) {
2377 bool isNonZero = (NonZeros & (1 << i)) != 0;
2378 if (isNonZero) {
2379 if (First) {
2380 if (NumZero)
2381 V = getZeroVector(MVT::v8i16, DAG);
2382 else
2383 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2384 First = false;
2385 }
2386 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00002387 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002388 }
2389 }
2390
2391 return V;
2392}
2393
Evan Cheng0db9fe62006-04-25 20:13:52 +00002394SDOperand
2395X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2396 // All zero's are handled with pxor.
2397 if (ISD::isBuildVectorAllZeros(Op.Val))
2398 return Op;
2399
2400 // All one's are handled with pcmpeqd.
2401 if (ISD::isBuildVectorAllOnes(Op.Val))
2402 return Op;
2403
2404 MVT::ValueType VT = Op.getValueType();
2405 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
2406 unsigned EVTBits = MVT::getSizeInBits(EVT);
2407
2408 unsigned NumElems = Op.getNumOperands();
2409 unsigned NumZero = 0;
2410 unsigned NumNonZero = 0;
2411 unsigned NonZeros = 0;
2412 std::set<SDOperand> Values;
2413 for (unsigned i = 0; i < NumElems; ++i) {
2414 SDOperand Elt = Op.getOperand(i);
2415 if (Elt.getOpcode() != ISD::UNDEF) {
2416 Values.insert(Elt);
2417 if (isZeroNode(Elt))
2418 NumZero++;
2419 else {
2420 NonZeros |= (1 << i);
2421 NumNonZero++;
2422 }
2423 }
2424 }
2425
2426 if (NumNonZero == 0)
2427 // Must be a mix of zero and undef. Return a zero vector.
2428 return getZeroVector(VT, DAG);
2429
2430 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2431 if (Values.size() == 1)
2432 return SDOperand();
2433
2434 // Special case for single non-zero element.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002435 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002436 unsigned Idx = CountTrailingZeros_32(NonZeros);
2437 SDOperand Item = Op.getOperand(Idx);
2438 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2439 if (Idx == 0)
2440 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2441 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2442 NumZero > 0, DAG);
2443
2444 if (EVTBits == 32) {
2445 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2446 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2447 DAG);
2448 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2449 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002450 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002451 for (unsigned i = 0; i < NumElems; i++)
2452 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002453 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2454 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002455 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2456 DAG.getNode(ISD::UNDEF, VT), Mask);
2457 }
2458 }
2459
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002460 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002461 if (EVTBits == 64)
2462 return SDOperand();
2463
2464 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00002465 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002466 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2467 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002468 if (V.Val) return V;
2469 }
2470
Bill Wendling826f36f2007-03-28 00:57:11 +00002471 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002472 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2473 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002474 if (V.Val) return V;
2475 }
2476
2477 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00002478 SmallVector<SDOperand, 8> V;
2479 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002480 if (NumElems == 4 && NumZero > 0) {
2481 for (unsigned i = 0; i < 4; ++i) {
2482 bool isZero = !(NonZeros & (1 << i));
2483 if (isZero)
2484 V[i] = getZeroVector(VT, DAG);
2485 else
2486 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2487 }
2488
2489 for (unsigned i = 0; i < 2; ++i) {
2490 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2491 default: break;
2492 case 0:
2493 V[i] = V[i*2]; // Must be a zero vector.
2494 break;
2495 case 1:
2496 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2497 getMOVLMask(NumElems, DAG));
2498 break;
2499 case 2:
2500 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2501 getMOVLMask(NumElems, DAG));
2502 break;
2503 case 3:
2504 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2505 getUnpacklMask(NumElems, DAG));
2506 break;
2507 }
2508 }
2509
Evan Cheng069287d2006-05-16 07:21:53 +00002510 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002511 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002512 // FIXME: we can do the same for v4f32 case when we know both parts of
2513 // the lower half come from scalar_to_vector (loadf32). We should do
2514 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002515 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00002516 return V[0];
2517 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2518 MVT::ValueType EVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002519 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002520 bool Reverse = (NonZeros & 0x3) == 2;
2521 for (unsigned i = 0; i < 2; ++i)
2522 if (Reverse)
2523 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2524 else
2525 MaskVec.push_back(DAG.getConstant(i, EVT));
2526 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2527 for (unsigned i = 0; i < 2; ++i)
2528 if (Reverse)
2529 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2530 else
2531 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002532 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2533 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002534 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2535 }
2536
2537 if (Values.size() > 2) {
2538 // Expand into a number of unpckl*.
2539 // e.g. for v4f32
2540 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2541 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2542 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2543 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2544 for (unsigned i = 0; i < NumElems; ++i)
2545 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2546 NumElems >>= 1;
2547 while (NumElems != 0) {
2548 for (unsigned i = 0; i < NumElems; ++i)
2549 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2550 UnpckMask);
2551 NumElems >>= 1;
2552 }
2553 return V[0];
2554 }
2555
2556 return SDOperand();
2557}
2558
2559SDOperand
2560X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2561 SDOperand V1 = Op.getOperand(0);
2562 SDOperand V2 = Op.getOperand(1);
2563 SDOperand PermMask = Op.getOperand(2);
2564 MVT::ValueType VT = Op.getValueType();
2565 unsigned NumElems = PermMask.getNumOperands();
2566 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2567 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00002568 bool V1IsSplat = false;
2569 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002570
Evan Cheng8cf723d2006-09-08 01:50:06 +00002571 if (isUndefShuffle(Op.Val))
2572 return DAG.getNode(ISD::UNDEF, VT);
2573
Evan Cheng0db9fe62006-04-25 20:13:52 +00002574 if (isSplatMask(PermMask.Val)) {
2575 if (NumElems <= 4) return Op;
2576 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002577 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002578 }
2579
Evan Cheng9bbbb982006-10-25 20:48:19 +00002580 if (X86::isMOVLMask(PermMask.Val))
2581 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002582
Evan Cheng9bbbb982006-10-25 20:48:19 +00002583 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2584 X86::isMOVSLDUPMask(PermMask.Val) ||
2585 X86::isMOVHLPSMask(PermMask.Val) ||
2586 X86::isMOVHPMask(PermMask.Val) ||
2587 X86::isMOVLPMask(PermMask.Val))
2588 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002589
Evan Cheng9bbbb982006-10-25 20:48:19 +00002590 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2591 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00002592 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002593
Evan Cheng9eca5e82006-10-25 21:49:50 +00002594 bool Commuted = false;
Evan Cheng9bbbb982006-10-25 20:48:19 +00002595 V1IsSplat = isSplatVector(V1.Val);
2596 V2IsSplat = isSplatVector(V2.Val);
2597 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00002598 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00002599 std::swap(V1IsSplat, V2IsSplat);
2600 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002601 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00002602 }
2603
2604 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2605 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00002606 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00002607 if (V2IsSplat) {
2608 // V2 is a splat, so the mask may be malformed. That is, it may point
2609 // to any V2 element. The instruction selectior won't like this. Get
2610 // a corrected mask and commute to form a proper MOVS{S|D}.
2611 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2612 if (NewMask.Val != PermMask.Val)
2613 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002614 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00002615 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00002616 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002617
Evan Chengd9b8e402006-10-16 06:36:00 +00002618 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002619 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00002620 X86::isUNPCKLMask(PermMask.Val) ||
2621 X86::isUNPCKHMask(PermMask.Val))
2622 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00002623
Evan Cheng9bbbb982006-10-25 20:48:19 +00002624 if (V2IsSplat) {
2625 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002626 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00002627 // new vector_shuffle with the corrected mask.
2628 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2629 if (NewMask.Val != PermMask.Val) {
2630 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2631 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2632 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2633 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2634 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2635 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002636 }
2637 }
2638 }
2639
2640 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00002641 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2642 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2643
2644 if (Commuted) {
2645 // Commute is back and try unpck* again.
2646 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2647 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002648 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00002649 X86::isUNPCKLMask(PermMask.Val) ||
2650 X86::isUNPCKHMask(PermMask.Val))
2651 return Op;
2652 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002653
2654 // If VT is integer, try PSHUF* first, then SHUFP*.
2655 if (MVT::isInteger(VT)) {
2656 if (X86::isPSHUFDMask(PermMask.Val) ||
2657 X86::isPSHUFHWMask(PermMask.Val) ||
2658 X86::isPSHUFLWMask(PermMask.Val)) {
2659 if (V2.getOpcode() != ISD::UNDEF)
2660 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2661 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2662 return Op;
2663 }
2664
2665 if (X86::isSHUFPMask(PermMask.Val))
2666 return Op;
2667
2668 // Handle v8i16 shuffle high / low shuffle node pair.
2669 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2670 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2671 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002672 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002673 for (unsigned i = 0; i != 4; ++i)
2674 MaskVec.push_back(PermMask.getOperand(i));
2675 for (unsigned i = 4; i != 8; ++i)
2676 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002677 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2678 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002679 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2680 MaskVec.clear();
2681 for (unsigned i = 0; i != 4; ++i)
2682 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2683 for (unsigned i = 4; i != 8; ++i)
2684 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00002685 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002686 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2687 }
2688 } else {
2689 // Floating point cases in the other order.
2690 if (X86::isSHUFPMask(PermMask.Val))
2691 return Op;
2692 if (X86::isPSHUFDMask(PermMask.Val) ||
2693 X86::isPSHUFHWMask(PermMask.Val) ||
2694 X86::isPSHUFLWMask(PermMask.Val)) {
2695 if (V2.getOpcode() != ISD::UNDEF)
2696 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2697 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2698 return Op;
2699 }
2700 }
2701
2702 if (NumElems == 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002703 MVT::ValueType MaskVT = PermMask.getValueType();
2704 MVT::ValueType MaskEVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002705 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00002706 Locs.reserve(NumElems);
Chris Lattner5a88b832007-02-25 07:10:00 +00002707 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2708 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002709 unsigned NumHi = 0;
2710 unsigned NumLo = 0;
2711 // If no more than two elements come from either vector. This can be
2712 // implemented with two shuffles. First shuffle gather the elements.
2713 // The second shuffle, which takes the first shuffle as both of its
2714 // vector operands, put the elements into the right order.
2715 for (unsigned i = 0; i != NumElems; ++i) {
2716 SDOperand Elt = PermMask.getOperand(i);
2717 if (Elt.getOpcode() == ISD::UNDEF) {
2718 Locs[i] = std::make_pair(-1, -1);
2719 } else {
2720 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2721 if (Val < NumElems) {
2722 Locs[i] = std::make_pair(0, NumLo);
2723 Mask1[NumLo] = Elt;
2724 NumLo++;
2725 } else {
2726 Locs[i] = std::make_pair(1, NumHi);
2727 if (2+NumHi < NumElems)
2728 Mask1[2+NumHi] = Elt;
2729 NumHi++;
2730 }
2731 }
2732 }
2733 if (NumLo <= 2 && NumHi <= 2) {
2734 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00002735 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2736 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002737 for (unsigned i = 0; i != NumElems; ++i) {
2738 if (Locs[i].first == -1)
2739 continue;
2740 else {
2741 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2742 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2743 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2744 }
2745 }
2746
2747 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00002748 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2749 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002750 }
2751
2752 // Break it into (shuffle shuffle_hi, shuffle_lo).
2753 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00002754 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2755 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2756 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002757 unsigned MaskIdx = 0;
2758 unsigned LoIdx = 0;
2759 unsigned HiIdx = NumElems/2;
2760 for (unsigned i = 0; i != NumElems; ++i) {
2761 if (i == NumElems/2) {
2762 MaskPtr = &HiMask;
2763 MaskIdx = 1;
2764 LoIdx = 0;
2765 HiIdx = NumElems/2;
2766 }
2767 SDOperand Elt = PermMask.getOperand(i);
2768 if (Elt.getOpcode() == ISD::UNDEF) {
2769 Locs[i] = std::make_pair(-1, -1);
2770 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
2771 Locs[i] = std::make_pair(MaskIdx, LoIdx);
2772 (*MaskPtr)[LoIdx] = Elt;
2773 LoIdx++;
2774 } else {
2775 Locs[i] = std::make_pair(MaskIdx, HiIdx);
2776 (*MaskPtr)[HiIdx] = Elt;
2777 HiIdx++;
2778 }
2779 }
2780
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002781 SDOperand LoShuffle =
2782 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00002783 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2784 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002785 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00002786 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00002787 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2788 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00002789 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002790 for (unsigned i = 0; i != NumElems; ++i) {
2791 if (Locs[i].first == -1) {
2792 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
2793 } else {
2794 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
2795 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
2796 }
2797 }
2798 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00002799 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2800 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002801 }
2802
2803 return SDOperand();
2804}
2805
2806SDOperand
2807X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
2808 if (!isa<ConstantSDNode>(Op.getOperand(1)))
2809 return SDOperand();
2810
2811 MVT::ValueType VT = Op.getValueType();
2812 // TODO: handle v16i8.
2813 if (MVT::getSizeInBits(VT) == 16) {
2814 // Transform it so it match pextrw which produces a 32-bit result.
2815 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
2816 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
2817 Op.getOperand(0), Op.getOperand(1));
2818 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
2819 DAG.getValueType(VT));
2820 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
2821 } else if (MVT::getSizeInBits(VT) == 32) {
2822 SDOperand Vec = Op.getOperand(0);
2823 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2824 if (Idx == 0)
2825 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002826 // SHUFPS the element to the lowest double word, then movss.
2827 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00002828 SmallVector<SDOperand, 8> IdxVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002829 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorBaseType(MaskVT)));
2830 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2831 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
2832 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00002833 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2834 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002835 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002836 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002837 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00002838 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002839 } else if (MVT::getSizeInBits(VT) == 64) {
2840 SDOperand Vec = Op.getOperand(0);
2841 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
2842 if (Idx == 0)
2843 return Op;
2844
2845 // UNPCKHPD the element to the lowest double word, then movsd.
2846 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
2847 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
2848 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00002849 SmallVector<SDOperand, 8> IdxVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002850 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorBaseType(MaskVT)));
2851 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00002852 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2853 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002854 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
2855 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
2856 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00002857 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002858 }
2859
2860 return SDOperand();
2861}
2862
2863SDOperand
2864X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00002865 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00002866 // as its second argument.
2867 MVT::ValueType VT = Op.getValueType();
2868 MVT::ValueType BaseVT = MVT::getVectorBaseType(VT);
2869 SDOperand N0 = Op.getOperand(0);
2870 SDOperand N1 = Op.getOperand(1);
2871 SDOperand N2 = Op.getOperand(2);
2872 if (MVT::getSizeInBits(BaseVT) == 16) {
2873 if (N1.getValueType() != MVT::i32)
2874 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
2875 if (N2.getValueType() != MVT::i32)
2876 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(), MVT::i32);
2877 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
2878 } else if (MVT::getSizeInBits(BaseVT) == 32) {
2879 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
2880 if (Idx == 0) {
2881 // Use a movss.
2882 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
2883 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
2884 MVT::ValueType BaseVT = MVT::getVectorBaseType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002885 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002886 MaskVec.push_back(DAG.getConstant(4, BaseVT));
2887 for (unsigned i = 1; i <= 3; ++i)
2888 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2889 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00002890 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2891 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002892 } else {
2893 // Use two pinsrw instructions to insert a 32 bit value.
2894 Idx <<= 1;
2895 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng466685d2006-10-09 20:57:25 +00002896 if (ISD::isNON_EXTLoad(N1.Val)) {
Evan Cheng069287d2006-05-16 07:21:53 +00002897 // Just load directly from f32mem to GR32.
Evan Cheng466685d2006-10-09 20:57:25 +00002898 LoadSDNode *LD = cast<LoadSDNode>(N1);
2899 N1 = DAG.getLoad(MVT::i32, LD->getChain(), LD->getBasePtr(),
2900 LD->getSrcValue(), LD->getSrcValueOffset());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002901 } else {
2902 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
2903 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
2904 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00002905 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002906 }
2907 }
2908 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
2909 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00002910 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002911 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
2912 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00002913 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00002914 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
2915 }
2916 }
2917
2918 return SDOperand();
2919}
2920
2921SDOperand
2922X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2923 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
2924 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
2925}
2926
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002927// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00002928// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
2929// one of the above mentioned nodes. It has to be wrapped because otherwise
2930// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
2931// be used to form addressing mode. These wrapped nodes will be selected
2932// into MOV32ri.
2933SDOperand
2934X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
2935 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00002936 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
2937 getPointerTy(),
2938 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00002939 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002940 // With PIC, the address is actually $g + Offset.
2941 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2942 !Subtarget->isPICStyleRIPRel()) {
2943 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2944 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2945 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002946 }
2947
2948 return Result;
2949}
2950
2951SDOperand
2952X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
2953 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00002954 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00002955 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002956 // With PIC, the address is actually $g + Offset.
2957 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
2958 !Subtarget->isPICStyleRIPRel()) {
2959 Result = DAG.getNode(ISD::ADD, getPointerTy(),
2960 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
2961 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002962 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002963
2964 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
2965 // load the value at address GV, not the value of GV itself. This means that
2966 // the GlobalAddress must be in the base or index register of the address, not
2967 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00002968 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00002969 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
2970 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002971
2972 return Result;
2973}
2974
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00002975// Lower ISD::GlobalTLSAddress using the "general dynamic" model
2976static SDOperand
2977LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
2978 const MVT::ValueType PtrVT) {
2979 SDOperand InFlag;
2980 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
2981 DAG.getNode(X86ISD::GlobalBaseReg,
2982 PtrVT), InFlag);
2983 InFlag = Chain.getValue(1);
2984
2985 // emit leal symbol@TLSGD(,%ebx,1), %eax
2986 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
2987 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
2988 GA->getValueType(0),
2989 GA->getOffset());
2990 SDOperand Ops[] = { Chain, TGA, InFlag };
2991 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
2992 InFlag = Result.getValue(2);
2993 Chain = Result.getValue(1);
2994
2995 // call ___tls_get_addr. This function receives its argument in
2996 // the register EAX.
2997 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
2998 InFlag = Chain.getValue(1);
2999
3000 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3001 SDOperand Ops1[] = { Chain,
3002 DAG.getTargetExternalSymbol("___tls_get_addr",
3003 PtrVT),
3004 DAG.getRegister(X86::EAX, PtrVT),
3005 DAG.getRegister(X86::EBX, PtrVT),
3006 InFlag };
3007 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3008 InFlag = Chain.getValue(1);
3009
3010 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3011}
3012
3013// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3014// "local exec" model.
3015static SDOperand
3016LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3017 const MVT::ValueType PtrVT) {
3018 // Get the Thread Pointer
3019 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3020 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3021 // exec)
3022 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3023 GA->getValueType(0),
3024 GA->getOffset());
3025 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003026
3027 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3028 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3029
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003030 // The address of the thread local variable is the add of the thread
3031 // pointer with the offset of the variable.
3032 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3033}
3034
3035SDOperand
3036X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3037 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003038 // TODO: implement the "initial exec"model for pic executables
3039 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3040 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003041 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3042 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3043 // otherwise use the "Local Exec"TLS Model
3044 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3045 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3046 else
3047 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3048}
3049
Evan Cheng0db9fe62006-04-25 20:13:52 +00003050SDOperand
3051X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3052 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003053 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003054 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003055 // With PIC, the address is actually $g + Offset.
3056 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3057 !Subtarget->isPICStyleRIPRel()) {
3058 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3059 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3060 Result);
3061 }
3062
3063 return Result;
3064}
3065
3066SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3067 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3068 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3069 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3070 // With PIC, the address is actually $g + Offset.
3071 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3072 !Subtarget->isPICStyleRIPRel()) {
3073 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3074 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3075 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003076 }
3077
3078 return Result;
3079}
3080
3081SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00003082 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3083 "Not an i64 shift!");
3084 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3085 SDOperand ShOpLo = Op.getOperand(0);
3086 SDOperand ShOpHi = Op.getOperand(1);
3087 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng734503b2006-09-11 02:19:56 +00003088 SDOperand Tmp1 = isSRA ?
3089 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3090 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003091
3092 SDOperand Tmp2, Tmp3;
3093 if (Op.getOpcode() == ISD::SHL_PARTS) {
3094 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3095 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3096 } else {
3097 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00003098 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00003099 }
3100
Evan Cheng734503b2006-09-11 02:19:56 +00003101 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3102 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3103 DAG.getConstant(32, MVT::i8));
3104 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
3105 SDOperand InFlag = DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Chenge3413162006-01-09 18:33:28 +00003106
3107 SDOperand Hi, Lo;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003108 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge3413162006-01-09 18:33:28 +00003109
Evan Cheng734503b2006-09-11 02:19:56 +00003110 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3111 SmallVector<SDOperand, 4> Ops;
Evan Chenge3413162006-01-09 18:33:28 +00003112 if (Op.getOpcode() == ISD::SHL_PARTS) {
3113 Ops.push_back(Tmp2);
3114 Ops.push_back(Tmp3);
3115 Ops.push_back(CC);
3116 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003117 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003118 InFlag = Hi.getValue(1);
3119
3120 Ops.clear();
3121 Ops.push_back(Tmp3);
3122 Ops.push_back(Tmp1);
3123 Ops.push_back(CC);
3124 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003125 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003126 } else {
3127 Ops.push_back(Tmp2);
3128 Ops.push_back(Tmp3);
3129 Ops.push_back(CC);
Evan Cheng910cd3c2006-01-09 22:29:54 +00003130 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003131 Lo = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003132 InFlag = Lo.getValue(1);
3133
3134 Ops.clear();
3135 Ops.push_back(Tmp3);
3136 Ops.push_back(Tmp1);
3137 Ops.push_back(CC);
3138 Ops.push_back(InFlag);
Evan Cheng734503b2006-09-11 02:19:56 +00003139 Hi = DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003140 }
3141
Evan Cheng734503b2006-09-11 02:19:56 +00003142 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003143 Ops.clear();
3144 Ops.push_back(Lo);
3145 Ops.push_back(Hi);
Evan Cheng734503b2006-09-11 02:19:56 +00003146 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003147}
Evan Chenga3195e82006-01-12 22:54:21 +00003148
Evan Cheng0db9fe62006-04-25 20:13:52 +00003149SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3150 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3151 Op.getOperand(0).getValueType() >= MVT::i16 &&
3152 "Unknown SINT_TO_FP to lower!");
3153
3154 SDOperand Result;
3155 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3156 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3157 MachineFunction &MF = DAG.getMachineFunction();
3158 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3159 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003160 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003161 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003162
3163 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00003164 SDVTList Tys;
3165 if (X86ScalarSSE)
3166 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3167 else
3168 Tys = DAG.getVTList(MVT::f64, MVT::Other);
3169 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003170 Ops.push_back(Chain);
3171 Ops.push_back(StackSlot);
3172 Ops.push_back(DAG.getValueType(SrcVT));
3173 Result = DAG.getNode(X86ScalarSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003174 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003175
3176 if (X86ScalarSSE) {
3177 Chain = Result.getValue(1);
3178 SDOperand InFlag = Result.getValue(2);
3179
3180 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3181 // shouldn't be necessary except that RFP cannot be live across
3182 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003183 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003184 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003185 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00003186 Tys = DAG.getVTList(MVT::Other);
3187 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003188 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003189 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003190 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003191 Ops.push_back(DAG.getValueType(Op.getValueType()));
3192 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003193 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00003194 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003195 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003196
Evan Cheng0db9fe62006-04-25 20:13:52 +00003197 return Result;
3198}
3199
3200SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3201 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3202 "Unknown FP_TO_SINT to lower!");
3203 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3204 // stack slot.
3205 MachineFunction &MF = DAG.getMachineFunction();
3206 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3207 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3208 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3209
3210 unsigned Opc;
3211 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003212 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3213 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3214 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3215 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003216 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003217
Evan Cheng0db9fe62006-04-25 20:13:52 +00003218 SDOperand Chain = DAG.getEntryNode();
3219 SDOperand Value = Op.getOperand(0);
3220 if (X86ScalarSSE) {
3221 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003222 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Chris Lattner5a88b832007-02-25 07:10:00 +00003223 SDVTList Tys = DAG.getVTList(MVT::f64, MVT::Other);
3224 SDOperand Ops[] = {
3225 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3226 };
3227 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003228 Chain = Value.getValue(1);
3229 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3230 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3231 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003232
Evan Cheng0db9fe62006-04-25 20:13:52 +00003233 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00003234 SDOperand Ops[] = { Chain, Value, StackSlot };
3235 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00003236
Evan Cheng0db9fe62006-04-25 20:13:52 +00003237 // Load the result.
Evan Cheng466685d2006-10-09 20:57:25 +00003238 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003239}
3240
3241SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3242 MVT::ValueType VT = Op.getValueType();
3243 const Type *OpNTy = MVT::getTypeForValueType(VT);
3244 std::vector<Constant*> CV;
3245 if (VT == MVT::f64) {
3246 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(~(1ULL << 63))));
3247 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3248 } else {
3249 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(~(1U << 31))));
3250 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3251 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3252 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3253 }
3254 Constant *CS = ConstantStruct::get(CV);
3255 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003256 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Cheng64a752f2006-08-11 09:08:15 +00003257 SmallVector<SDOperand, 3> Ops;
3258 Ops.push_back(DAG.getEntryNode());
3259 Ops.push_back(CPIdx);
3260 Ops.push_back(DAG.getSrcValue(NULL));
3261 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003262 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3263}
3264
3265SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3266 MVT::ValueType VT = Op.getValueType();
3267 const Type *OpNTy = MVT::getTypeForValueType(VT);
3268 std::vector<Constant*> CV;
3269 if (VT == MVT::f64) {
3270 CV.push_back(ConstantFP::get(OpNTy, BitsToDouble(1ULL << 63)));
3271 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3272 } else {
3273 CV.push_back(ConstantFP::get(OpNTy, BitsToFloat(1U << 31)));
3274 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3275 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3276 CV.push_back(ConstantFP::get(OpNTy, 0.0));
3277 }
3278 Constant *CS = ConstantStruct::get(CV);
3279 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003280 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
Evan Cheng64a752f2006-08-11 09:08:15 +00003281 SmallVector<SDOperand, 3> Ops;
3282 Ops.push_back(DAG.getEntryNode());
3283 Ops.push_back(CPIdx);
3284 Ops.push_back(DAG.getSrcValue(NULL));
3285 SDOperand Mask = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003286 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3287}
3288
Evan Cheng68c47cb2007-01-05 07:55:56 +00003289SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00003290 SDOperand Op0 = Op.getOperand(0);
3291 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003292 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00003293 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00003294 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003295
3296 // If second operand is smaller, extend it first.
3297 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3298 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3299 SrcVT = VT;
3300 }
3301
Evan Cheng68c47cb2007-01-05 07:55:56 +00003302 // First get the sign bit of second operand.
3303 std::vector<Constant*> CV;
3304 if (SrcVT == MVT::f64) {
3305 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(1ULL << 63)));
3306 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3307 } else {
3308 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(1U << 31)));
3309 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3310 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3311 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3312 }
3313 Constant *CS = ConstantStruct::get(CV);
3314 SDOperand CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnerd96d0722007-02-25 06:40:16 +00003315 SDVTList Tys = DAG.getVTList(SrcVT, MVT::Other);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003316 SmallVector<SDOperand, 3> Ops;
3317 Ops.push_back(DAG.getEntryNode());
3318 Ops.push_back(CPIdx);
3319 Ops.push_back(DAG.getSrcValue(NULL));
Evan Cheng73d6cf12007-01-05 21:37:56 +00003320 SDOperand Mask1 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3321 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003322
3323 // Shift sign bit right or left if the two operands have different types.
3324 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3325 // Op0 is MVT::f32, Op1 is MVT::f64.
3326 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3327 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3328 DAG.getConstant(32, MVT::i32));
3329 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3330 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3331 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00003332 }
3333
Evan Cheng73d6cf12007-01-05 21:37:56 +00003334 // Clear first operand sign bit.
3335 CV.clear();
3336 if (VT == MVT::f64) {
3337 CV.push_back(ConstantFP::get(SrcTy, BitsToDouble(~(1ULL << 63))));
3338 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3339 } else {
3340 CV.push_back(ConstantFP::get(SrcTy, BitsToFloat(~(1U << 31))));
3341 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3342 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3343 CV.push_back(ConstantFP::get(SrcTy, 0.0));
3344 }
3345 CS = ConstantStruct::get(CV);
3346 CPIdx = DAG.getConstantPool(CS, getPointerTy(), 4);
Chris Lattnerd96d0722007-02-25 06:40:16 +00003347 Tys = DAG.getVTList(VT, MVT::Other);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003348 Ops.clear();
3349 Ops.push_back(DAG.getEntryNode());
3350 Ops.push_back(CPIdx);
3351 Ops.push_back(DAG.getSrcValue(NULL));
3352 SDOperand Mask2 = DAG.getNode(X86ISD::LOAD_PACK, Tys, &Ops[0], Ops.size());
3353 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3354
3355 // Or the value with the sign bit.
3356 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003357}
3358
Evan Cheng734503b2006-09-11 02:19:56 +00003359SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3360 SDOperand Chain) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003361 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3362 SDOperand Cond;
Evan Cheng734503b2006-09-11 02:19:56 +00003363 SDOperand Op0 = Op.getOperand(0);
3364 SDOperand Op1 = Op.getOperand(1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003365 SDOperand CC = Op.getOperand(2);
3366 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Chengcf12ec42006-10-12 19:12:56 +00003367 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3368 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003369 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003370 unsigned X86CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003371
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003372 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Chris Lattnerf9570512006-09-13 03:22:10 +00003373 Op0, Op1, DAG)) {
Evan Cheng734503b2006-09-11 02:19:56 +00003374 SDOperand Ops1[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003375 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003376 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003377 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003378 }
3379
3380 assert(isFP && "Illegal integer SetCC!");
3381
3382 SDOperand COps[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003383 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003384
3385 switch (SetCCOpcode) {
3386 default: assert(false && "Illegal floating point SetCC!");
3387 case ISD::SETOEQ: { // !PF & ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003388 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003389 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003390 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003391 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003392 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003393 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3394 }
3395 case ISD::SETUNE: { // PF | !ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003396 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003397 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003398 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003399 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003400 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003401 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3402 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003403 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003404}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003405
Evan Cheng0db9fe62006-04-25 20:13:52 +00003406SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003407 bool addTest = true;
3408 SDOperand Chain = DAG.getEntryNode();
3409 SDOperand Cond = Op.getOperand(0);
3410 SDOperand CC;
3411 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng9bba8942006-01-26 02:13:10 +00003412
Evan Cheng734503b2006-09-11 02:19:56 +00003413 if (Cond.getOpcode() == ISD::SETCC)
3414 Cond = LowerSETCC(Cond, DAG, Chain);
3415
3416 if (Cond.getOpcode() == X86ISD::SETCC) {
3417 CC = Cond.getOperand(0);
3418
Evan Cheng0db9fe62006-04-25 20:13:52 +00003419 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng734503b2006-09-11 02:19:56 +00003420 // (since flag operand cannot be shared). Use it as the condition setting
3421 // operand in place of the X86ISD::SETCC.
3422 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Cheng0db9fe62006-04-25 20:13:52 +00003423 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng734503b2006-09-11 02:19:56 +00003424 // pressure reason)?
3425 SDOperand Cmp = Cond.getOperand(1);
3426 unsigned Opc = Cmp.getOpcode();
3427 bool IllegalFPCMov = !X86ScalarSSE &&
3428 MVT::isFloatingPoint(Op.getValueType()) &&
3429 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3430 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3431 !IllegalFPCMov) {
3432 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3433 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3434 addTest = false;
3435 }
3436 }
Evan Chengaaca22c2006-01-10 20:26:56 +00003437
Evan Cheng0db9fe62006-04-25 20:13:52 +00003438 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003439 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003440 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3441 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng7df96d62005-12-17 01:21:05 +00003442 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003443
Evan Cheng734503b2006-09-11 02:19:56 +00003444 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3445 SmallVector<SDOperand, 4> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003446 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3447 // condition is true.
3448 Ops.push_back(Op.getOperand(2));
3449 Ops.push_back(Op.getOperand(1));
3450 Ops.push_back(CC);
Evan Cheng734503b2006-09-11 02:19:56 +00003451 Ops.push_back(Cond.getValue(1));
3452 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003453}
Evan Cheng9bba8942006-01-26 02:13:10 +00003454
Evan Cheng0db9fe62006-04-25 20:13:52 +00003455SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003456 bool addTest = true;
3457 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003458 SDOperand Cond = Op.getOperand(1);
3459 SDOperand Dest = Op.getOperand(2);
3460 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00003461 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3462
Evan Cheng0db9fe62006-04-25 20:13:52 +00003463 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng734503b2006-09-11 02:19:56 +00003464 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003465
3466 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00003467 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003468
Evan Cheng734503b2006-09-11 02:19:56 +00003469 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3470 // (since flag operand cannot be shared). Use it as the condition setting
3471 // operand in place of the X86ISD::SETCC.
3472 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3473 // to use a test instead of duplicating the X86ISD::CMP (for register
3474 // pressure reason)?
3475 SDOperand Cmp = Cond.getOperand(1);
3476 unsigned Opc = Cmp.getOpcode();
3477 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3478 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3479 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3480 addTest = false;
3481 }
3482 }
Evan Cheng1bcee362006-01-13 01:03:02 +00003483
Evan Cheng0db9fe62006-04-25 20:13:52 +00003484 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003485 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003486 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3487 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng898101c2005-12-19 23:12:38 +00003488 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003489 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng734503b2006-09-11 02:19:56 +00003490 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003491}
Evan Cheng67f92a72006-01-11 22:15:48 +00003492
Evan Cheng32fe1032006-05-25 00:59:30 +00003493SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3494 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003495
Evan Cheng25ab6902006-09-08 06:48:29 +00003496 if (Subtarget->is64Bit())
Chris Lattner09c75a42007-02-25 09:06:15 +00003497 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00003498 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003499 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00003500 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003501 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00003502 case CallingConv::Fast:
Chris Lattner2db39b82007-02-28 06:05:16 +00003503 // TODO: Implement fastcc
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003504 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00003505 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003506 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00003507 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003508 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00003509 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003510 }
Evan Cheng32fe1032006-05-25 00:59:30 +00003511}
3512
Anton Korobeynikove060b532007-04-17 19:34:00 +00003513
3514// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3515// Calls to _alloca is needed to probe the stack when allocating more than 4k
3516// bytes in one go. Touching the stack at 4K increments is necessary to ensure
3517// that the guard pages used by the OS virtual memory manager are allocated in
3518// correct sequence.
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003519SDOperand X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3520 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00003521 assert(Subtarget->isTargetCygMing() &&
3522 "This should be used only on Cygwin/Mingw targets");
3523
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003524 // Get the inputs.
3525 SDOperand Chain = Op.getOperand(0);
3526 SDOperand Size = Op.getOperand(1);
3527 // FIXME: Ensure alignment here
3528
3529 TargetLowering::ArgListTy Args;
3530 TargetLowering::ArgListEntry Entry;
3531 MVT::ValueType IntPtr = getPointerTy();
3532 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
3533 const Type *IntPtrTy = getTargetData()->getIntPtrType();
3534
3535 Entry.Node = Size;
3536 Entry.Ty = IntPtrTy;
3537 Entry.isInReg = true; // Should pass in EAX
3538 Args.push_back(Entry);
3539 std::pair<SDOperand, SDOperand> CallResult =
3540 LowerCallTo(Chain, IntPtrTy, false, false, CallingConv::C, false,
3541 DAG.getExternalSymbol("_alloca", IntPtr), Args, DAG);
3542
3543 SDOperand SP = DAG.getCopyFromReg(CallResult.second, X86StackPtr, SPTy);
3544
3545 std::vector<MVT::ValueType> Tys;
3546 Tys.push_back(SPTy);
3547 Tys.push_back(MVT::Other);
3548 SDOperand Ops[2] = { SP, CallResult.second };
3549 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
3550}
3551
Evan Cheng1bc78042006-04-26 01:20:17 +00003552SDOperand
3553X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00003554 MachineFunction &MF = DAG.getMachineFunction();
3555 const Function* Fn = MF.getFunction();
3556 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00003557 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00003558 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00003559 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00003560
Evan Cheng25caf632006-05-23 21:06:34 +00003561 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00003562 if (Subtarget->is64Bit())
3563 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00003564 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003565 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00003566 default:
3567 assert(0 && "Unsupported calling convention");
3568 case CallingConv::Fast:
Chris Lattner2db39b82007-02-28 06:05:16 +00003569 // TODO: implement fastcc.
3570
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003571 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00003572 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003573 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003574 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00003575 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00003576 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003577 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00003578 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00003579 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003580 }
Evan Cheng1bc78042006-04-26 01:20:17 +00003581}
3582
Evan Cheng0db9fe62006-04-25 20:13:52 +00003583SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3584 SDOperand InFlag(0, 0);
3585 SDOperand Chain = Op.getOperand(0);
3586 unsigned Align =
3587 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3588 if (Align == 0) Align = 1;
3589
3590 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3591 // If not DWORD aligned, call memset if size is less than the threshold.
3592 // It knows how to align to the right boundary first.
3593 if ((Align & 3) != 0 ||
3594 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3595 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00003596 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00003597 TargetLowering::ArgListTy Args;
3598 TargetLowering::ArgListEntry Entry;
3599 Entry.Node = Op.getOperand(1);
3600 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00003601 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00003602 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00003603 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
3604 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00003605 Args.push_back(Entry);
3606 Entry.Node = Op.getOperand(3);
3607 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003608 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00003609 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00003610 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
3611 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00003612 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00003613
Evan Cheng0db9fe62006-04-25 20:13:52 +00003614 MVT::ValueType AVT;
3615 SDOperand Count;
3616 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
3617 unsigned BytesLeft = 0;
3618 bool TwoRepStos = false;
3619 if (ValC) {
3620 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00003621 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00003622
Evan Cheng0db9fe62006-04-25 20:13:52 +00003623 // If the value is a constant, then we can potentially use larger sets.
3624 switch (Align & 3) {
3625 case 2: // WORD aligned
3626 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003627 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00003628 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003629 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00003630 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00003631 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00003632 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003633 Val = (Val << 8) | Val;
3634 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00003635 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
3636 AVT = MVT::i64;
3637 ValReg = X86::RAX;
3638 Val = (Val << 32) | Val;
3639 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003640 break;
3641 default: // Byte aligned
3642 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003643 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00003644 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003645 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00003646 }
3647
Evan Cheng25ab6902006-09-08 06:48:29 +00003648 if (AVT > MVT::i8) {
3649 if (I) {
3650 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3651 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3652 BytesLeft = I->getValue() % UBytes;
3653 } else {
3654 assert(AVT >= MVT::i32 &&
3655 "Do not use rep;stos if not at least DWORD aligned");
3656 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3657 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3658 TwoRepStos = true;
3659 }
3660 }
3661
Evan Cheng0db9fe62006-04-25 20:13:52 +00003662 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
3663 InFlag);
3664 InFlag = Chain.getValue(1);
3665 } else {
3666 AVT = MVT::i8;
3667 Count = Op.getOperand(3);
3668 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
3669 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00003670 }
Evan Chengc78d3b42006-04-24 18:01:45 +00003671
Evan Cheng25ab6902006-09-08 06:48:29 +00003672 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3673 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003674 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00003675 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3676 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003677 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00003678
Chris Lattnerd96d0722007-02-25 06:40:16 +00003679 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00003680 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003681 Ops.push_back(Chain);
3682 Ops.push_back(DAG.getValueType(AVT));
3683 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003684 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00003685
Evan Cheng0db9fe62006-04-25 20:13:52 +00003686 if (TwoRepStos) {
3687 InFlag = Chain.getValue(1);
3688 Count = Op.getOperand(3);
3689 MVT::ValueType CVT = Count.getValueType();
3690 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00003691 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3692 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3693 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003694 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00003695 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003696 Ops.clear();
3697 Ops.push_back(Chain);
3698 Ops.push_back(DAG.getValueType(MVT::i8));
3699 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003700 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003701 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003702 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003703 SDOperand Value;
3704 unsigned Val = ValC->getValue() & 255;
3705 unsigned Offset = I->getValue() - BytesLeft;
3706 SDOperand DstAddr = Op.getOperand(1);
3707 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00003708 if (BytesLeft >= 4) {
3709 Val = (Val << 8) | Val;
3710 Val = (Val << 16) | Val;
3711 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00003712 Chain = DAG.getStore(Chain, Value,
3713 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3714 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003715 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003716 BytesLeft -= 4;
3717 Offset += 4;
3718 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003719 if (BytesLeft >= 2) {
3720 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00003721 Chain = DAG.getStore(Chain, Value,
3722 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3723 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003724 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003725 BytesLeft -= 2;
3726 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00003727 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003728 if (BytesLeft == 1) {
3729 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00003730 Chain = DAG.getStore(Chain, Value,
3731 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
3732 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003733 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00003734 }
Evan Cheng386031a2006-03-24 07:29:27 +00003735 }
Evan Cheng11e15b32006-04-03 20:53:28 +00003736
Evan Cheng0db9fe62006-04-25 20:13:52 +00003737 return Chain;
3738}
Evan Cheng11e15b32006-04-03 20:53:28 +00003739
Evan Cheng0db9fe62006-04-25 20:13:52 +00003740SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
3741 SDOperand Chain = Op.getOperand(0);
3742 unsigned Align =
3743 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3744 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00003745
Evan Cheng0db9fe62006-04-25 20:13:52 +00003746 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
3747 // If not DWORD aligned, call memcpy if size is less than the threshold.
3748 // It knows how to align to the right boundary first.
3749 if ((Align & 3) != 0 ||
3750 (I && I->getValue() < Subtarget->getMinRepStrSizeThreshold())) {
3751 MVT::ValueType IntPtr = getPointerTy();
Reid Spencer47857812006-12-31 05:55:36 +00003752 TargetLowering::ArgListTy Args;
3753 TargetLowering::ArgListEntry Entry;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00003754 Entry.Ty = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00003755 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
3756 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
3757 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003758 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00003759 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00003760 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
3761 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00003762 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003763
3764 MVT::ValueType AVT;
3765 SDOperand Count;
3766 unsigned BytesLeft = 0;
3767 bool TwoRepMovs = false;
3768 switch (Align & 3) {
3769 case 2: // WORD aligned
3770 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003771 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00003772 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00003773 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00003774 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
3775 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003776 break;
3777 default: // Byte aligned
3778 AVT = MVT::i8;
3779 Count = Op.getOperand(3);
3780 break;
3781 }
3782
Evan Cheng25ab6902006-09-08 06:48:29 +00003783 if (AVT > MVT::i8) {
3784 if (I) {
3785 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
3786 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
3787 BytesLeft = I->getValue() % UBytes;
3788 } else {
3789 assert(AVT >= MVT::i32 &&
3790 "Do not use rep;movs if not at least DWORD aligned");
3791 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
3792 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
3793 TwoRepMovs = true;
3794 }
3795 }
3796
Evan Cheng0db9fe62006-04-25 20:13:52 +00003797 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003798 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
3799 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003800 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00003801 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
3802 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003803 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00003804 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
3805 Op.getOperand(2), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003806 InFlag = Chain.getValue(1);
3807
Chris Lattnerd96d0722007-02-25 06:40:16 +00003808 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00003809 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003810 Ops.push_back(Chain);
3811 Ops.push_back(DAG.getValueType(AVT));
3812 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003813 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003814
3815 if (TwoRepMovs) {
3816 InFlag = Chain.getValue(1);
3817 Count = Op.getOperand(3);
3818 MVT::ValueType CVT = Count.getValueType();
3819 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00003820 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
3821 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
3822 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003823 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00003824 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003825 Ops.clear();
3826 Ops.push_back(Chain);
3827 Ops.push_back(DAG.getValueType(MVT::i8));
3828 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00003829 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003830 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003831 // Issue loads and stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003832 unsigned Offset = I->getValue() - BytesLeft;
3833 SDOperand DstAddr = Op.getOperand(1);
3834 MVT::ValueType DstVT = DstAddr.getValueType();
3835 SDOperand SrcAddr = Op.getOperand(2);
3836 MVT::ValueType SrcVT = SrcAddr.getValueType();
3837 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00003838 if (BytesLeft >= 4) {
3839 Value = DAG.getLoad(MVT::i32, Chain,
3840 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3841 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00003842 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003843 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00003844 Chain = DAG.getStore(Chain, Value,
3845 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3846 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003847 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00003848 BytesLeft -= 4;
3849 Offset += 4;
3850 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003851 if (BytesLeft >= 2) {
3852 Value = DAG.getLoad(MVT::i16, Chain,
3853 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3854 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00003855 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003856 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00003857 Chain = DAG.getStore(Chain, Value,
3858 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3859 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003860 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003861 BytesLeft -= 2;
3862 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00003863 }
3864
Evan Cheng0db9fe62006-04-25 20:13:52 +00003865 if (BytesLeft == 1) {
3866 Value = DAG.getLoad(MVT::i8, Chain,
3867 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
3868 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00003869 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003870 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00003871 Chain = DAG.getStore(Chain, Value,
3872 DAG.getNode(ISD::ADD, DstVT, DstAddr,
3873 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003874 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003875 }
Evan Chengb067a1e2006-03-31 19:22:53 +00003876 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003877
3878 return Chain;
3879}
3880
3881SDOperand
3882X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerd96d0722007-02-25 06:40:16 +00003883 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00003884 SDOperand TheOp = Op.getOperand(0);
3885 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00003886 if (Subtarget->is64Bit()) {
3887 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
3888 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
3889 MVT::i64, Copy1.getValue(2));
3890 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
3891 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00003892 SDOperand Ops[] = {
3893 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
3894 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00003895
3896 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003897 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00003898 }
Chris Lattner5a88b832007-02-25 07:10:00 +00003899
3900 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
3901 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
3902 MVT::i32, Copy1.getValue(2));
3903 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
3904 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
3905 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003906}
3907
3908SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00003909 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
3910
Evan Cheng25ab6902006-09-08 06:48:29 +00003911 if (!Subtarget->is64Bit()) {
3912 // vastart just stores the address of the VarArgsFrameIndex slot into the
3913 // memory location argument.
3914 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003915 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
3916 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00003917 }
3918
3919 // __va_list_tag:
3920 // gp_offset (0 - 6 * 8)
3921 // fp_offset (48 - 48 + 8 * 16)
3922 // overflow_arg_area (point to parameters coming in memory).
3923 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00003924 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00003925 SDOperand FIN = Op.getOperand(1);
3926 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00003927 SDOperand Store = DAG.getStore(Op.getOperand(0),
3928 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003929 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00003930 MemOps.push_back(Store);
3931
3932 // Store fp_offset
3933 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3934 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00003935 Store = DAG.getStore(Op.getOperand(0),
3936 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003937 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00003938 MemOps.push_back(Store);
3939
3940 // Store ptr to overflow_arg_area
3941 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3942 DAG.getConstant(4, getPointerTy()));
3943 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003944 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
3945 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00003946 MemOps.push_back(Store);
3947
3948 // Store ptr to reg_save_area.
3949 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
3950 DAG.getConstant(8, getPointerTy()));
3951 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00003952 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
3953 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00003954 MemOps.push_back(Store);
3955 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003956}
3957
Evan Chengae642192007-03-02 23:16:35 +00003958SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
3959 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
3960 SDOperand Chain = Op.getOperand(0);
3961 SDOperand DstPtr = Op.getOperand(1);
3962 SDOperand SrcPtr = Op.getOperand(2);
3963 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
3964 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
3965
3966 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
3967 SrcSV->getValue(), SrcSV->getOffset());
3968 Chain = SrcPtr.getValue(1);
3969 for (unsigned i = 0; i < 3; ++i) {
3970 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
3971 SrcSV->getValue(), SrcSV->getOffset());
3972 Chain = Val.getValue(1);
3973 Chain = DAG.getStore(Chain, Val, DstPtr,
3974 DstSV->getValue(), DstSV->getOffset());
3975 if (i == 2)
3976 break;
3977 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
3978 DAG.getConstant(8, getPointerTy()));
3979 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
3980 DAG.getConstant(8, getPointerTy()));
3981 }
3982 return Chain;
3983}
3984
Evan Cheng0db9fe62006-04-25 20:13:52 +00003985SDOperand
3986X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
3987 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
3988 switch (IntNo) {
3989 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00003990 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003991 case Intrinsic::x86_sse_comieq_ss:
3992 case Intrinsic::x86_sse_comilt_ss:
3993 case Intrinsic::x86_sse_comile_ss:
3994 case Intrinsic::x86_sse_comigt_ss:
3995 case Intrinsic::x86_sse_comige_ss:
3996 case Intrinsic::x86_sse_comineq_ss:
3997 case Intrinsic::x86_sse_ucomieq_ss:
3998 case Intrinsic::x86_sse_ucomilt_ss:
3999 case Intrinsic::x86_sse_ucomile_ss:
4000 case Intrinsic::x86_sse_ucomigt_ss:
4001 case Intrinsic::x86_sse_ucomige_ss:
4002 case Intrinsic::x86_sse_ucomineq_ss:
4003 case Intrinsic::x86_sse2_comieq_sd:
4004 case Intrinsic::x86_sse2_comilt_sd:
4005 case Intrinsic::x86_sse2_comile_sd:
4006 case Intrinsic::x86_sse2_comigt_sd:
4007 case Intrinsic::x86_sse2_comige_sd:
4008 case Intrinsic::x86_sse2_comineq_sd:
4009 case Intrinsic::x86_sse2_ucomieq_sd:
4010 case Intrinsic::x86_sse2_ucomilt_sd:
4011 case Intrinsic::x86_sse2_ucomile_sd:
4012 case Intrinsic::x86_sse2_ucomigt_sd:
4013 case Intrinsic::x86_sse2_ucomige_sd:
4014 case Intrinsic::x86_sse2_ucomineq_sd: {
4015 unsigned Opc = 0;
4016 ISD::CondCode CC = ISD::SETCC_INVALID;
4017 switch (IntNo) {
4018 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004019 case Intrinsic::x86_sse_comieq_ss:
4020 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004021 Opc = X86ISD::COMI;
4022 CC = ISD::SETEQ;
4023 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004024 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004025 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004026 Opc = X86ISD::COMI;
4027 CC = ISD::SETLT;
4028 break;
4029 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004030 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004031 Opc = X86ISD::COMI;
4032 CC = ISD::SETLE;
4033 break;
4034 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004035 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004036 Opc = X86ISD::COMI;
4037 CC = ISD::SETGT;
4038 break;
4039 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004040 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004041 Opc = X86ISD::COMI;
4042 CC = ISD::SETGE;
4043 break;
4044 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004045 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004046 Opc = X86ISD::COMI;
4047 CC = ISD::SETNE;
4048 break;
4049 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004050 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004051 Opc = X86ISD::UCOMI;
4052 CC = ISD::SETEQ;
4053 break;
4054 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004055 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004056 Opc = X86ISD::UCOMI;
4057 CC = ISD::SETLT;
4058 break;
4059 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004060 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004061 Opc = X86ISD::UCOMI;
4062 CC = ISD::SETLE;
4063 break;
4064 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004065 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004066 Opc = X86ISD::UCOMI;
4067 CC = ISD::SETGT;
4068 break;
4069 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004070 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004071 Opc = X86ISD::UCOMI;
4072 CC = ISD::SETGE;
4073 break;
4074 case Intrinsic::x86_sse_ucomineq_ss:
4075 case Intrinsic::x86_sse2_ucomineq_sd:
4076 Opc = X86ISD::UCOMI;
4077 CC = ISD::SETNE;
4078 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004079 }
Evan Cheng734503b2006-09-11 02:19:56 +00004080
Evan Cheng0db9fe62006-04-25 20:13:52 +00004081 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004082 SDOperand LHS = Op.getOperand(1);
4083 SDOperand RHS = Op.getOperand(2);
4084 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004085
4086 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Chris Lattnerf9570512006-09-13 03:22:10 +00004087 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
Evan Cheng734503b2006-09-11 02:19:56 +00004088 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4089 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4090 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4091 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004092 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004093 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004094 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004095}
Evan Cheng72261582005-12-20 06:22:03 +00004096
Nate Begemanbcc5f362007-01-29 22:58:52 +00004097SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4098 // Depths > 0 not supported yet!
4099 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4100 return SDOperand();
4101
4102 // Just load the return address
4103 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4104 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4105}
4106
4107SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4108 // Depths > 0 not supported yet!
4109 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4110 return SDOperand();
4111
4112 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4113 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4114 DAG.getConstant(4, getPointerTy()));
4115}
4116
Evan Cheng0db9fe62006-04-25 20:13:52 +00004117/// LowerOperation - Provide custom lowering hooks for some operations.
4118///
4119SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4120 switch (Op.getOpcode()) {
4121 default: assert(0 && "Should not custom lower this!");
4122 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4123 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4124 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4125 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4126 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4127 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4128 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004129 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004130 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4131 case ISD::SHL_PARTS:
4132 case ISD::SRA_PARTS:
4133 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4134 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4135 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4136 case ISD::FABS: return LowerFABS(Op, DAG);
4137 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004138 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004139 case ISD::SETCC: return LowerSETCC(Op, DAG, DAG.getEntryNode());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004140 case ISD::SELECT: return LowerSELECT(Op, DAG);
4141 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
4142 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00004143 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004144 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00004145 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004146 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4147 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4148 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4149 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00004150 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004151 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00004152 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4153 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004154 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155 }
Jim Laskey62819f32007-02-21 22:54:50 +00004156 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004157}
4158
Evan Cheng72261582005-12-20 06:22:03 +00004159const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4160 switch (Opcode) {
4161 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00004162 case X86ISD::SHLD: return "X86ISD::SHLD";
4163 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00004164 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004165 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00004166 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004167 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00004168 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00004169 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00004170 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4171 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4172 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00004173 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00004174 case X86ISD::FST: return "X86ISD::FST";
4175 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00004176 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00004177 case X86ISD::CALL: return "X86ISD::CALL";
4178 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4179 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4180 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00004181 case X86ISD::COMI: return "X86ISD::COMI";
4182 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00004183 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00004184 case X86ISD::CMOV: return "X86ISD::CMOV";
4185 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00004186 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00004187 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4188 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng223547a2006-01-31 22:28:30 +00004189 case X86ISD::LOAD_PACK: return "X86ISD::LOAD_PACK";
Evan Cheng206ee9d2006-07-07 08:33:52 +00004190 case X86ISD::LOAD_UA: return "X86ISD::LOAD_UA";
Evan Cheng7ccced62006-02-18 00:15:05 +00004191 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00004192 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00004193 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00004194 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00004195 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00004196 case X86ISD::FMAX: return "X86ISD::FMAX";
4197 case X86ISD::FMIN: return "X86ISD::FMIN";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004198 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4199 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Evan Cheng72261582005-12-20 06:22:03 +00004200 }
4201}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004202
Chris Lattnerc9addb72007-03-30 23:15:24 +00004203// isLegalAddressingMode - Return true if the addressing mode represented
4204// by AM is legal for this target, for a load/store of the specified type.
4205bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4206 const Type *Ty) const {
4207 // X86 supports extremely general addressing modes.
4208
4209 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4210 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4211 return false;
4212
4213 if (AM.BaseGV) {
4214 // X86-64 only supports addr of globals in small code model.
4215 if (Subtarget->is64Bit() &&
4216 getTargetMachine().getCodeModel() != CodeModel::Small)
4217 return false;
4218
4219 // We can only fold this if we don't need a load either.
4220 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4221 return false;
4222 }
4223
4224 switch (AM.Scale) {
4225 case 0:
4226 case 1:
4227 case 2:
4228 case 4:
4229 case 8:
4230 // These scales always work.
4231 break;
4232 case 3:
4233 case 5:
4234 case 9:
4235 // These scales are formed with basereg+scalereg. Only accept if there is
4236 // no basereg yet.
4237 if (AM.HasBaseReg)
4238 return false;
4239 break;
4240 default: // Other stuff never works.
4241 return false;
4242 }
4243
4244 return true;
4245}
4246
4247
Evan Cheng60c07e12006-07-05 22:17:51 +00004248/// isShuffleMaskLegal - Targets can use this to indicate that they only
4249/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4250/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4251/// are assumed to be legal.
4252bool
4253X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4254 // Only do shuffles on 128-bit vector types for now.
4255 if (MVT::getSizeInBits(VT) == 64) return false;
4256 return (Mask.Val->getNumOperands() <= 4 ||
4257 isSplatMask(Mask.Val) ||
4258 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4259 X86::isUNPCKLMask(Mask.Val) ||
4260 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00004261 X86::isUNPCKH_v_undef_Mask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00004262 X86::isUNPCKHMask(Mask.Val));
4263}
4264
4265bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4266 MVT::ValueType EVT,
4267 SelectionDAG &DAG) const {
4268 unsigned NumElts = BVOps.size();
4269 // Only do shuffles on 128-bit vector types for now.
4270 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4271 if (NumElts == 2) return true;
4272 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00004273 return (isMOVLMask(&BVOps[0], 4) ||
4274 isCommutedMOVL(&BVOps[0], 4, true) ||
4275 isSHUFPMask(&BVOps[0], 4) ||
4276 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00004277 }
4278 return false;
4279}
4280
4281//===----------------------------------------------------------------------===//
4282// X86 Scheduler Hooks
4283//===----------------------------------------------------------------------===//
4284
4285MachineBasicBlock *
4286X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4287 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004288 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00004289 switch (MI->getOpcode()) {
4290 default: assert(false && "Unexpected instr type to insert");
4291 case X86::CMOV_FR32:
4292 case X86::CMOV_FR64:
4293 case X86::CMOV_V4F32:
4294 case X86::CMOV_V2F64:
4295 case X86::CMOV_V2I64: {
4296 // To "insert" a SELECT_CC instruction, we actually have to insert the
4297 // diamond control-flow pattern. The incoming instruction knows the
4298 // destination vreg to set, the condition code register to branch on, the
4299 // true/false values to select between, and a branch opcode to use.
4300 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4301 ilist<MachineBasicBlock>::iterator It = BB;
4302 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004303
Evan Cheng60c07e12006-07-05 22:17:51 +00004304 // thisMBB:
4305 // ...
4306 // TrueVal = ...
4307 // cmpTY ccX, r1, r2
4308 // bCC copy1MBB
4309 // fallthrough --> copy0MBB
4310 MachineBasicBlock *thisMBB = BB;
4311 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4312 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004313 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00004314 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00004315 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00004316 MachineFunction *F = BB->getParent();
4317 F->getBasicBlockList().insert(It, copy0MBB);
4318 F->getBasicBlockList().insert(It, sinkMBB);
4319 // Update machine-CFG edges by first adding all successors of the current
4320 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004321 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00004322 e = BB->succ_end(); i != e; ++i)
4323 sinkMBB->addSuccessor(*i);
4324 // Next, remove all successors of the current block, and add the true
4325 // and fallthrough blocks as its successors.
4326 while(!BB->succ_empty())
4327 BB->removeSuccessor(BB->succ_begin());
4328 BB->addSuccessor(copy0MBB);
4329 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004330
Evan Cheng60c07e12006-07-05 22:17:51 +00004331 // copy0MBB:
4332 // %FalseValue = ...
4333 // # fallthrough to sinkMBB
4334 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004335
Evan Cheng60c07e12006-07-05 22:17:51 +00004336 // Update machine-CFG edges
4337 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004338
Evan Cheng60c07e12006-07-05 22:17:51 +00004339 // sinkMBB:
4340 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4341 // ...
4342 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00004343 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00004344 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4345 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4346
4347 delete MI; // The pseudo instruction is gone now.
4348 return BB;
4349 }
4350
4351 case X86::FP_TO_INT16_IN_MEM:
4352 case X86::FP_TO_INT32_IN_MEM:
4353 case X86::FP_TO_INT64_IN_MEM: {
4354 // Change the floating point control register to use "round towards zero"
4355 // mode when truncating to an integer value.
4356 MachineFunction *F = BB->getParent();
4357 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004358 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004359
4360 // Load the old value of the high byte of the control word...
4361 unsigned OldCW =
4362 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004363 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004364
4365 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004366 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4367 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00004368
4369 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004370 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004371
4372 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00004373 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4374 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00004375
4376 // Get the X86 opcode to use.
4377 unsigned Opc;
4378 switch (MI->getOpcode()) {
4379 default: assert(0 && "illegal opcode!");
4380 case X86::FP_TO_INT16_IN_MEM: Opc = X86::FpIST16m; break;
4381 case X86::FP_TO_INT32_IN_MEM: Opc = X86::FpIST32m; break;
4382 case X86::FP_TO_INT64_IN_MEM: Opc = X86::FpIST64m; break;
4383 }
4384
4385 X86AddressMode AM;
4386 MachineOperand &Op = MI->getOperand(0);
4387 if (Op.isRegister()) {
4388 AM.BaseType = X86AddressMode::RegBase;
4389 AM.Base.Reg = Op.getReg();
4390 } else {
4391 AM.BaseType = X86AddressMode::FrameIndexBase;
4392 AM.Base.FrameIndex = Op.getFrameIndex();
4393 }
4394 Op = MI->getOperand(1);
4395 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004396 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004397 Op = MI->getOperand(2);
4398 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004399 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004400 Op = MI->getOperand(3);
4401 if (Op.isGlobalAddress()) {
4402 AM.GV = Op.getGlobal();
4403 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00004404 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004405 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00004406 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4407 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00004408
4409 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00004410 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004411
4412 delete MI; // The pseudo instruction is gone now.
4413 return BB;
4414 }
4415 }
4416}
4417
4418//===----------------------------------------------------------------------===//
4419// X86 Optimization Hooks
4420//===----------------------------------------------------------------------===//
4421
Nate Begeman368e18d2006-02-16 21:11:51 +00004422void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
4423 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004424 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00004425 uint64_t &KnownOne,
4426 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004427 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00004428 assert((Opc >= ISD::BUILTIN_OP_END ||
4429 Opc == ISD::INTRINSIC_WO_CHAIN ||
4430 Opc == ISD::INTRINSIC_W_CHAIN ||
4431 Opc == ISD::INTRINSIC_VOID) &&
4432 "Should use MaskedValueIsZero if you don't know whether Op"
4433 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004434
Evan Cheng865f0602006-04-05 06:11:20 +00004435 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004436 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00004437 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004438 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00004439 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
4440 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004441 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004442}
Chris Lattner259e97c2006-01-31 19:43:35 +00004443
Evan Cheng206ee9d2006-07-07 08:33:52 +00004444/// getShuffleScalarElt - Returns the scalar element that will make up the ith
4445/// element of the result of the vector shuffle.
4446static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
4447 MVT::ValueType VT = N->getValueType(0);
4448 SDOperand PermMask = N->getOperand(2);
4449 unsigned NumElems = PermMask.getNumOperands();
4450 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
4451 i %= NumElems;
4452 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
4453 return (i == 0)
4454 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4455 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
4456 SDOperand Idx = PermMask.getOperand(i);
4457 if (Idx.getOpcode() == ISD::UNDEF)
4458 return DAG.getNode(ISD::UNDEF, MVT::getVectorBaseType(VT));
4459 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
4460 }
4461 return SDOperand();
4462}
4463
4464/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
4465/// node is a GlobalAddress + an offset.
4466static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00004467 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004468 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004469 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
4470 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
4471 return true;
4472 }
Evan Cheng0085a282006-11-30 21:55:46 +00004473 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004474 SDOperand N1 = N->getOperand(0);
4475 SDOperand N2 = N->getOperand(1);
4476 if (isGAPlusOffset(N1.Val, GA, Offset)) {
4477 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
4478 if (V) {
4479 Offset += V->getSignExtended();
4480 return true;
4481 }
4482 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
4483 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
4484 if (V) {
4485 Offset += V->getSignExtended();
4486 return true;
4487 }
4488 }
4489 }
4490 return false;
4491}
4492
4493/// isConsecutiveLoad - Returns true if N is loading from an address of Base
4494/// + Dist * Size.
4495static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
4496 MachineFrameInfo *MFI) {
4497 if (N->getOperand(0).Val != Base->getOperand(0).Val)
4498 return false;
4499
4500 SDOperand Loc = N->getOperand(1);
4501 SDOperand BaseLoc = Base->getOperand(1);
4502 if (Loc.getOpcode() == ISD::FrameIndex) {
4503 if (BaseLoc.getOpcode() != ISD::FrameIndex)
4504 return false;
4505 int FI = dyn_cast<FrameIndexSDNode>(Loc)->getIndex();
4506 int BFI = dyn_cast<FrameIndexSDNode>(BaseLoc)->getIndex();
4507 int FS = MFI->getObjectSize(FI);
4508 int BFS = MFI->getObjectSize(BFI);
4509 if (FS != BFS || FS != Size) return false;
4510 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
4511 } else {
4512 GlobalValue *GV1 = NULL;
4513 GlobalValue *GV2 = NULL;
4514 int64_t Offset1 = 0;
4515 int64_t Offset2 = 0;
4516 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
4517 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
4518 if (isGA1 && isGA2 && GV1 == GV2)
4519 return Offset1 == (Offset2 + Dist*Size);
4520 }
4521
4522 return false;
4523}
4524
Evan Cheng1e60c092006-07-10 21:37:44 +00004525static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
4526 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004527 GlobalValue *GV;
4528 int64_t Offset;
4529 if (isGAPlusOffset(Base, GV, Offset))
4530 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
4531 else {
4532 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
4533 int BFI = dyn_cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00004534 if (BFI < 0)
4535 // Fixed objects do not specify alignment, however the offsets are known.
4536 return ((Subtarget->getStackAlignment() % 16) == 0 &&
4537 (MFI->getObjectOffset(BFI) % 16) == 0);
4538 else
4539 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00004540 }
4541 return false;
4542}
4543
4544
4545/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
4546/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
4547/// if the load addresses are consecutive, non-overlapping, and in the right
4548/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00004549static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
4550 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004551 MachineFunction &MF = DAG.getMachineFunction();
4552 MachineFrameInfo *MFI = MF.getFrameInfo();
4553 MVT::ValueType VT = N->getValueType(0);
4554 MVT::ValueType EVT = MVT::getVectorBaseType(VT);
4555 SDOperand PermMask = N->getOperand(2);
4556 int NumElems = (int)PermMask.getNumOperands();
4557 SDNode *Base = NULL;
4558 for (int i = 0; i < NumElems; ++i) {
4559 SDOperand Idx = PermMask.getOperand(i);
4560 if (Idx.getOpcode() == ISD::UNDEF) {
4561 if (!Base) return SDOperand();
4562 } else {
4563 SDOperand Arg =
4564 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00004565 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00004566 return SDOperand();
4567 if (!Base)
4568 Base = Arg.Val;
4569 else if (!isConsecutiveLoad(Arg.Val, Base,
4570 i, MVT::getSizeInBits(EVT)/8,MFI))
4571 return SDOperand();
4572 }
4573 }
4574
Evan Cheng1e60c092006-07-10 21:37:44 +00004575 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Evan Cheng466685d2006-10-09 20:57:25 +00004576 if (isAlign16) {
4577 LoadSDNode *LD = cast<LoadSDNode>(Base);
4578 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
4579 LD->getSrcValueOffset());
4580 } else {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004581 // Just use movups, it's shorter.
Chris Lattnerd96d0722007-02-25 06:40:16 +00004582 SDVTList Tys = DAG.getVTList(MVT::v4f32, MVT::Other);
Evan Cheng64a752f2006-08-11 09:08:15 +00004583 SmallVector<SDOperand, 3> Ops;
4584 Ops.push_back(Base->getOperand(0));
4585 Ops.push_back(Base->getOperand(1));
4586 Ops.push_back(Base->getOperand(2));
Evan Cheng206ee9d2006-07-07 08:33:52 +00004587 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Cheng64a752f2006-08-11 09:08:15 +00004588 DAG.getNode(X86ISD::LOAD_UA, Tys, &Ops[0], Ops.size()));
Evan Cheng311ace02006-08-11 07:35:45 +00004589 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00004590}
4591
Chris Lattner83e6c992006-10-04 06:57:07 +00004592/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
4593static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
4594 const X86Subtarget *Subtarget) {
4595 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004596
Chris Lattner83e6c992006-10-04 06:57:07 +00004597 // If we have SSE[12] support, try to form min/max nodes.
4598 if (Subtarget->hasSSE2() &&
4599 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
4600 if (Cond.getOpcode() == ISD::SETCC) {
4601 // Get the LHS/RHS of the select.
4602 SDOperand LHS = N->getOperand(1);
4603 SDOperand RHS = N->getOperand(2);
4604 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004605
Evan Cheng8ca29322006-11-10 21:43:37 +00004606 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00004607 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00004608 switch (CC) {
4609 default: break;
4610 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
4611 case ISD::SETULE:
4612 case ISD::SETLE:
4613 if (!UnsafeFPMath) break;
4614 // FALL THROUGH.
4615 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
4616 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00004617 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004618 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004619
Chris Lattner1907a7b2006-10-05 04:11:26 +00004620 case ISD::SETOGT: // (X > Y) ? X : Y -> max
4621 case ISD::SETUGT:
4622 case ISD::SETGT:
4623 if (!UnsafeFPMath) break;
4624 // FALL THROUGH.
4625 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
4626 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00004627 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004628 break;
4629 }
Chris Lattner83e6c992006-10-04 06:57:07 +00004630 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00004631 switch (CC) {
4632 default: break;
4633 case ISD::SETOGT: // (X > Y) ? Y : X -> min
4634 case ISD::SETUGT:
4635 case ISD::SETGT:
4636 if (!UnsafeFPMath) break;
4637 // FALL THROUGH.
4638 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
4639 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00004640 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004641 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004642
Chris Lattner1907a7b2006-10-05 04:11:26 +00004643 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
4644 case ISD::SETULE:
4645 case ISD::SETLE:
4646 if (!UnsafeFPMath) break;
4647 // FALL THROUGH.
4648 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
4649 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00004650 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00004651 break;
4652 }
Chris Lattner83e6c992006-10-04 06:57:07 +00004653 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004654
Evan Cheng8ca29322006-11-10 21:43:37 +00004655 if (Opcode)
4656 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00004657 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004658
Chris Lattner83e6c992006-10-04 06:57:07 +00004659 }
4660
4661 return SDOperand();
4662}
4663
4664
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004665SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00004666 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00004667 SelectionDAG &DAG = DCI.DAG;
4668 switch (N->getOpcode()) {
4669 default: break;
4670 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00004671 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00004672 case ISD::SELECT:
4673 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00004674 }
4675
4676 return SDOperand();
4677}
4678
Evan Cheng60c07e12006-07-05 22:17:51 +00004679//===----------------------------------------------------------------------===//
4680// X86 Inline Assembly Support
4681//===----------------------------------------------------------------------===//
4682
Chris Lattnerf4dff842006-07-11 02:54:03 +00004683/// getConstraintType - Given a constraint letter, return the type of
4684/// constraint it is for this target.
4685X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00004686X86TargetLowering::getConstraintType(const std::string &Constraint) const {
4687 if (Constraint.size() == 1) {
4688 switch (Constraint[0]) {
4689 case 'A':
4690 case 'r':
4691 case 'R':
4692 case 'l':
4693 case 'q':
4694 case 'Q':
4695 case 'x':
4696 case 'Y':
4697 return C_RegisterClass;
4698 default:
4699 break;
4700 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00004701 }
Chris Lattner4234f572007-03-25 02:14:49 +00004702 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00004703}
4704
Chris Lattner22aaf1d2006-10-31 20:13:11 +00004705/// isOperandValidForConstraint - Return the specified operand (possibly
4706/// modified) if the specified SDOperand is valid for the specified target
4707/// constraint letter, otherwise return null.
4708SDOperand X86TargetLowering::
4709isOperandValidForConstraint(SDOperand Op, char Constraint, SelectionDAG &DAG) {
4710 switch (Constraint) {
4711 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00004712 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00004713 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4714 if (C->getValue() <= 31)
Devang Patel84f7fd22007-03-17 00:13:28 +00004715 return Op;
Devang Patel84f7fd22007-03-17 00:13:28 +00004716 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00004717 return SDOperand(0,0);
4718 case 'N':
4719 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
4720 if (C->getValue() <= 255)
4721 return Op;
4722 }
4723 return SDOperand(0,0);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00004724 case 'i':
4725 // Literal immediates are always ok.
4726 if (isa<ConstantSDNode>(Op)) return Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004727
Chris Lattner22aaf1d2006-10-31 20:13:11 +00004728 // If we are in non-pic codegen mode, we allow the address of a global to
4729 // be used with 'i'.
4730 if (GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op)) {
4731 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4732 return SDOperand(0, 0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004733
Chris Lattner22aaf1d2006-10-31 20:13:11 +00004734 if (GA->getOpcode() != ISD::TargetGlobalAddress)
4735 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
4736 GA->getOffset());
4737 return Op;
4738 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004739
Chris Lattner22aaf1d2006-10-31 20:13:11 +00004740 // Otherwise, not valid for this mode.
4741 return SDOperand(0, 0);
4742 }
4743 return TargetLowering::isOperandValidForConstraint(Op, Constraint, DAG);
4744}
4745
Chris Lattner259e97c2006-01-31 19:43:35 +00004746std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00004747getRegClassForInlineAsmConstraint(const std::string &Constraint,
4748 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00004749 if (Constraint.size() == 1) {
4750 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00004751 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00004752 default: break; // Unknown constraint letter
4753 case 'A': // EAX/EDX
4754 if (VT == MVT::i32 || VT == MVT::i64)
4755 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
4756 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00004757 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
4758 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00004759 if (VT == MVT::i32)
4760 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
4761 else if (VT == MVT::i16)
4762 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
4763 else if (VT == MVT::i8)
4764 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::DL, 0);
4765 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00004766 }
4767 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004768
Chris Lattner1efa40f2006-02-22 00:56:39 +00004769 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00004770}
Chris Lattnerf76d1802006-07-31 23:26:50 +00004771
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004772std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00004773X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
4774 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00004775 // First, see if this is a constraint that directly corresponds to an LLVM
4776 // register class.
4777 if (Constraint.size() == 1) {
4778 // GCC Constraint Letters
4779 switch (Constraint[0]) {
4780 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00004781 case 'r': // GENERAL_REGS
4782 case 'R': // LEGACY_REGS
4783 case 'l': // INDEX_REGS
4784 if (VT == MVT::i64 && Subtarget->is64Bit())
4785 return std::make_pair(0U, X86::GR64RegisterClass);
4786 if (VT == MVT::i32)
4787 return std::make_pair(0U, X86::GR32RegisterClass);
4788 else if (VT == MVT::i16)
4789 return std::make_pair(0U, X86::GR16RegisterClass);
4790 else if (VT == MVT::i8)
4791 return std::make_pair(0U, X86::GR8RegisterClass);
4792 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00004793 case 'y': // MMX_REGS if MMX allowed.
4794 if (!Subtarget->hasMMX()) break;
4795 return std::make_pair(0U, X86::VR64RegisterClass);
4796 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00004797 case 'Y': // SSE_REGS if SSE2 allowed
4798 if (!Subtarget->hasSSE2()) break;
4799 // FALL THROUGH.
4800 case 'x': // SSE_REGS if SSE1 allowed
4801 if (!Subtarget->hasSSE1()) break;
4802
4803 switch (VT) {
4804 default: break;
4805 // Scalar SSE types.
4806 case MVT::f32:
4807 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00004808 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00004809 case MVT::f64:
4810 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00004811 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00004812 // Vector types.
4813 case MVT::Vector:
4814 case MVT::v16i8:
4815 case MVT::v8i16:
4816 case MVT::v4i32:
4817 case MVT::v2i64:
4818 case MVT::v4f32:
4819 case MVT::v2f64:
4820 return std::make_pair(0U, X86::VR128RegisterClass);
4821 }
Chris Lattnerad043e82007-04-09 05:11:28 +00004822 break;
4823 }
4824 }
4825
Chris Lattnerf76d1802006-07-31 23:26:50 +00004826 // Use the default implementation in TargetLowering to convert the register
4827 // constraint into a member of a register class.
4828 std::pair<unsigned, const TargetRegisterClass*> Res;
4829 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00004830
4831 // Not found as a standard register?
4832 if (Res.second == 0) {
4833 // GCC calls "st(0)" just plain "st".
4834 if (StringsEqualNoCase("{st}", Constraint)) {
4835 Res.first = X86::ST0;
4836 Res.second = X86::RSTRegisterClass;
4837 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004838
Chris Lattner1a60aa72006-10-31 19:42:44 +00004839 return Res;
4840 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004841
Chris Lattnerf76d1802006-07-31 23:26:50 +00004842 // Otherwise, check to see if this is a register class of the wrong value
4843 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
4844 // turn into {ax},{dx}.
4845 if (Res.second->hasType(VT))
4846 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004847
Chris Lattnerf76d1802006-07-31 23:26:50 +00004848 // All of the single-register GCC register classes map their values onto
4849 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
4850 // really want an 8-bit or 32-bit register, map to the appropriate register
4851 // class and return the appropriate register.
4852 if (Res.second != X86::GR16RegisterClass)
4853 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004854
Chris Lattnerf76d1802006-07-31 23:26:50 +00004855 if (VT == MVT::i8) {
4856 unsigned DestReg = 0;
4857 switch (Res.first) {
4858 default: break;
4859 case X86::AX: DestReg = X86::AL; break;
4860 case X86::DX: DestReg = X86::DL; break;
4861 case X86::CX: DestReg = X86::CL; break;
4862 case X86::BX: DestReg = X86::BL; break;
4863 }
4864 if (DestReg) {
4865 Res.first = DestReg;
4866 Res.second = Res.second = X86::GR8RegisterClass;
4867 }
4868 } else if (VT == MVT::i32) {
4869 unsigned DestReg = 0;
4870 switch (Res.first) {
4871 default: break;
4872 case X86::AX: DestReg = X86::EAX; break;
4873 case X86::DX: DestReg = X86::EDX; break;
4874 case X86::CX: DestReg = X86::ECX; break;
4875 case X86::BX: DestReg = X86::EBX; break;
4876 case X86::SI: DestReg = X86::ESI; break;
4877 case X86::DI: DestReg = X86::EDI; break;
4878 case X86::BP: DestReg = X86::EBP; break;
4879 case X86::SP: DestReg = X86::ESP; break;
4880 }
4881 if (DestReg) {
4882 Res.first = DestReg;
4883 Res.second = Res.second = X86::GR32RegisterClass;
4884 }
Evan Cheng25ab6902006-09-08 06:48:29 +00004885 } else if (VT == MVT::i64) {
4886 unsigned DestReg = 0;
4887 switch (Res.first) {
4888 default: break;
4889 case X86::AX: DestReg = X86::RAX; break;
4890 case X86::DX: DestReg = X86::RDX; break;
4891 case X86::CX: DestReg = X86::RCX; break;
4892 case X86::BX: DestReg = X86::RBX; break;
4893 case X86::SI: DestReg = X86::RSI; break;
4894 case X86::DI: DestReg = X86::RDI; break;
4895 case X86::BP: DestReg = X86::RBP; break;
4896 case X86::SP: DestReg = X86::RSP; break;
4897 }
4898 if (DestReg) {
4899 Res.first = DestReg;
4900 Res.second = Res.second = X86::GR64RegisterClass;
4901 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00004902 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004903
Chris Lattnerf76d1802006-07-31 23:26:50 +00004904 return Res;
4905}