blob: a67e77f2ce692a28dcdacf6fb54e3d5e93927fc5 [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 Cheng0488db92007-09-25 01:57:46 +000034#include "llvm/Support/CommandLine.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000035#include "llvm/Support/MathExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000036#include "llvm/Target/TargetOptions.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000037#include "llvm/ADT/StringExtras.h"
Duncan Sandsb116fac2007-07-27 20:02:49 +000038#include "llvm/ParameterAttributes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039using namespace llvm;
40
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041X86TargetLowering::X86TargetLowering(TargetMachine &TM)
42 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000043 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000044 X86ScalarSSEf64 = Subtarget->hasSSE2();
45 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000046 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Evan Cheng559806f2006-01-27 08:10:46 +000047
Anton Korobeynikov2365f512007-07-14 14:06:15 +000048 RegInfo = TM.getRegisterInfo();
49
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000050 // Set up the TargetLowering object.
51
52 // X86 is weird, it always uses i8 for shift amounts and setcc results.
53 setShiftAmountType(MVT::i8);
54 setSetCCResultType(MVT::i8);
55 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000056 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000058 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000059
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000060 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000061 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000062 setUseUnderscoreSetJmp(false);
63 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000064 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000065 // MS runtime is weird: it exports _setjmp, but longjmp!
66 setUseUnderscoreSetJmp(true);
67 setUseUnderscoreLongJmp(false);
68 } else {
69 setUseUnderscoreSetJmp(true);
70 setUseUnderscoreLongJmp(true);
71 }
72
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000073 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000074 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
75 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
76 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000077 if (Subtarget->is64Bit())
78 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000079
Evan Chengc5484282006-10-04 00:56:09 +000080 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Expand);
81
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000082 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
83 // operation.
84 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
85 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
86 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000087
Evan Cheng25ab6902006-09-08 06:48:29 +000088 if (Subtarget->is64Bit()) {
89 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +000090 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +000091 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000092 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +000093 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
94 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
95 else
96 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
97 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000098
99 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
100 // this operation.
101 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
102 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000103 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000104 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000105 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000106 // f32 and f64 cases are Legal, f80 case is not
107 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
108 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000109 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
110 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
111 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112
Dale Johannesen73328d12007-09-19 23:55:34 +0000113 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
114 // are Legal, f80 is custom lowered.
115 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000117
Evan Cheng02568ff2006-01-30 22:13:22 +0000118 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
119 // this operation.
120 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
121 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
122
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000123 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000124 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000125 // f32 and f64 cases are Legal, f80 case is not
126 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000127 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000128 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000129 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000130 }
131
132 // Handle FP_TO_UINT by promoting the destination to a larger signed
133 // conversion.
134 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
136 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
137
Evan Cheng25ab6902006-09-08 06:48:29 +0000138 if (Subtarget->is64Bit()) {
139 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000140 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000141 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000142 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000143 // Expand FP_TO_UINT into a select.
144 // FIXME: We would like to use a Custom expander here eventually to do
145 // the optimal thing for SSE vs. the default expansion in the legalizer.
146 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
147 else
148 // With SSE3 we can use fisttpll to convert to a signed i64.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
150 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000151
Chris Lattner399610a2006-12-05 18:22:22 +0000152 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000153 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000154 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
155 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
156 }
Chris Lattner21f66852005-12-23 05:15:23 +0000157
Evan Chengc35497f2006-10-30 08:02:39 +0000158 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000159 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000160 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
161 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000162 setOperationAction(ISD::MEMMOVE , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000163 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000164 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
165 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
166 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000167 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
168 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000169 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000170
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000171 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
172 setOperationAction(ISD::CTTZ , MVT::i8 , Expand);
173 setOperationAction(ISD::CTLZ , MVT::i8 , Expand);
174 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
175 setOperationAction(ISD::CTTZ , MVT::i16 , Expand);
176 setOperationAction(ISD::CTLZ , MVT::i16 , Expand);
177 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
178 setOperationAction(ISD::CTTZ , MVT::i32 , Expand);
179 setOperationAction(ISD::CTLZ , MVT::i32 , Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000180 if (Subtarget->is64Bit()) {
181 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
182 setOperationAction(ISD::CTTZ , MVT::i64 , Expand);
183 setOperationAction(ISD::CTLZ , MVT::i64 , Expand);
184 }
185
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000186 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000187 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000188
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000189 // These should be promoted to a larger select which is supported.
190 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
191 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000192 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000193 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
194 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
195 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
196 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000197 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000198 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
199 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
200 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
201 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
202 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000203 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000204 if (Subtarget->is64Bit()) {
205 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
206 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
207 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000208 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000209 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000210 if (!Subtarget->is64Bit())
211 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
212
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000213 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000214 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000215 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000216 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000217 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000218 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000219 if (Subtarget->is64Bit()) {
220 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
221 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
222 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
223 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
224 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000225 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000226 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
227 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
228 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000229 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000230 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
231 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000232
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000233 // Use the default ISD::LOCATION expansion.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000234 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000235 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000236 if (!Subtarget->isTargetDarwin() &&
237 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000238 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000239 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000240
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000241 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
242 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
243 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
244 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
245 if (Subtarget->is64Bit()) {
246 // FIXME: Verify
247 setExceptionPointerRegister(X86::RAX);
248 setExceptionSelectorRegister(X86::RDX);
249 } else {
250 setExceptionPointerRegister(X86::EAX);
251 setExceptionSelectorRegister(X86::EDX);
252 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000253 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000254
Duncan Sandsf7331b32007-09-11 14:10:23 +0000255 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000256
Nate Begemanacc398c2006-01-25 18:21:52 +0000257 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
258 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000259 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000260 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000261 if (Subtarget->is64Bit())
262 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
263 else
264 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
265
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000266 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000267 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000268 if (Subtarget->is64Bit())
269 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000270 if (Subtarget->isTargetCygMing())
271 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
272 else
273 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000274
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000275 if (X86ScalarSSEf64) {
276 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000277 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000278 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
279 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000280
Evan Cheng223547a2006-01-31 22:28:30 +0000281 // Use ANDPD to simulate FABS.
282 setOperationAction(ISD::FABS , MVT::f64, Custom);
283 setOperationAction(ISD::FABS , MVT::f32, Custom);
284
285 // Use XORP to simulate FNEG.
286 setOperationAction(ISD::FNEG , MVT::f64, Custom);
287 setOperationAction(ISD::FNEG , MVT::f32, Custom);
288
Evan Cheng68c47cb2007-01-05 07:55:56 +0000289 // Use ANDPD and ORPD to simulate FCOPYSIGN.
290 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
291 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
292
Evan Chengd25e9e82006-02-02 00:28:23 +0000293 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000294 setOperationAction(ISD::FSIN , MVT::f64, Expand);
295 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000296 setOperationAction(ISD::FREM , MVT::f64, Expand);
297 setOperationAction(ISD::FSIN , MVT::f32, Expand);
298 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000299 setOperationAction(ISD::FREM , MVT::f32, Expand);
300
Chris Lattnera54aa942006-01-29 06:26:08 +0000301 // Expand FP immediates into loads from the stack, except for the special
302 // cases we handle.
303 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
304 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000305 addLegalFPImmediate(APFloat(+0.0)); // xorpd
306 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000307
308 // Conversions to long double (in X87) go through memory.
309 setConvertAction(MVT::f32, MVT::f80, Expand);
310 setConvertAction(MVT::f64, MVT::f80, Expand);
311
312 // Conversions from long double (in X87) go through memory.
313 setConvertAction(MVT::f80, MVT::f32, Expand);
314 setConvertAction(MVT::f80, MVT::f64, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000315 } else if (X86ScalarSSEf32) {
316 // Use SSE for f32, x87 for f64.
317 // Set up the FP register classes.
318 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
319 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
320
321 // Use ANDPS to simulate FABS.
322 setOperationAction(ISD::FABS , MVT::f32, Custom);
323
324 // Use XORP to simulate FNEG.
325 setOperationAction(ISD::FNEG , MVT::f32, Custom);
326
327 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
328
329 // Use ANDPS and ORPS to simulate FCOPYSIGN.
330 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
331 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
332
333 // We don't support sin/cos/fmod
334 setOperationAction(ISD::FSIN , MVT::f32, Expand);
335 setOperationAction(ISD::FCOS , MVT::f32, Expand);
336 setOperationAction(ISD::FREM , MVT::f32, Expand);
337
338 // Expand FP immediates into loads from the stack, except for the special
339 // cases we handle.
340 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
341 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
342 addLegalFPImmediate(APFloat(+0.0f)); // xorps
343 addLegalFPImmediate(APFloat(+0.0)); // FLD0
344 addLegalFPImmediate(APFloat(+1.0)); // FLD1
345 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
346 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
347
348 // SSE->x87 conversions go through memory.
349 setConvertAction(MVT::f32, MVT::f64, Expand);
350 setConvertAction(MVT::f32, MVT::f80, Expand);
351
352 // x87->SSE truncations need to go through memory.
353 setConvertAction(MVT::f80, MVT::f32, Expand);
354 setConvertAction(MVT::f64, MVT::f32, Expand);
355 // And x87->x87 truncations also.
356 setConvertAction(MVT::f80, MVT::f64, Expand);
357
358 if (!UnsafeFPMath) {
359 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
360 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
361 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000362 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000363 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000364 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000365 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
366 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000367
Evan Cheng68c47cb2007-01-05 07:55:56 +0000368 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000369 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000370 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
371 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000372
373 // Floating truncations need to go through memory.
374 setConvertAction(MVT::f80, MVT::f32, Expand);
375 setConvertAction(MVT::f64, MVT::f32, Expand);
376 setConvertAction(MVT::f80, MVT::f64, Expand);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000377
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000378 if (!UnsafeFPMath) {
379 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
380 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
381 }
382
Chris Lattnera54aa942006-01-29 06:26:08 +0000383 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000384 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000385 addLegalFPImmediate(APFloat(+0.0)); // FLD0
386 addLegalFPImmediate(APFloat(+1.0)); // FLD1
387 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
388 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000389 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
390 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
391 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
392 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000393 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000394
Dale Johannesen59a58732007-08-05 18:49:15 +0000395 // Long double always uses X87.
396 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000397 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
398 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
399 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
Dale Johannesen59a58732007-08-05 18:49:15 +0000400
Evan Chengd30bf012006-03-01 01:11:20 +0000401 // First set operation action for all vector types to expand. Then we
402 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000403 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
404 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000405 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
406 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000407 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000408 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000409 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000410 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000411 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
412 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
413 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
414 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
415 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
416 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000417 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000418 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000419 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000420 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000421 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
422 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
423 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
424 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
425 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
426 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
427 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000428 }
429
Evan Chenga88973f2006-03-22 19:22:18 +0000430 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000431 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
432 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
433 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000434 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000435
Evan Chengd30bf012006-03-01 01:11:20 +0000436 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000437
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000438 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
439 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
440 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000441 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000442
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000443 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
444 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
445 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
446
Bill Wendling74027e92007-03-15 21:24:36 +0000447 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
448 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
449
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000450 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000451 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000452 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000453 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
454 setOperationAction(ISD::AND, MVT::v2i32, Promote);
455 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
456 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000457
458 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000459 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000460 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000461 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
462 setOperationAction(ISD::OR, MVT::v2i32, Promote);
463 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
464 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000465
466 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000467 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000468 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000469 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
470 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
471 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
472 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000473
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000474 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000475 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000476 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000477 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
478 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
479 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
480 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000481
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000482 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
483 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
484 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
485 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000486
487 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
488 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
489 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000490 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000491
492 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
493 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000494 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
495 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000496 }
497
Evan Chenga88973f2006-03-22 19:22:18 +0000498 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000499 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
500
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000501 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
502 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
503 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
504 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000505 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
506 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000507 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
508 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
509 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000510 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000511 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000512 }
513
Evan Chenga88973f2006-03-22 19:22:18 +0000514 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000515 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
516 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
517 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
518 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
519 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
520
Evan Chengf7c378e2006-04-10 07:23:14 +0000521 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
522 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
523 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000524 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000525 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
526 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
527 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000528 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000529 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000530 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
531 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
532 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
533 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000534 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
535 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000536
Evan Chengf7c378e2006-04-10 07:23:14 +0000537 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
538 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000539 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000540 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
541 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
542 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000543
Evan Cheng2c3ae372006-04-12 21:21:57 +0000544 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
545 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
546 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
547 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
548 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
549 }
550 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
551 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
552 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
553 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
554 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
555 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
556
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000557 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000558 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
559 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
560 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
561 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
562 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
563 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
564 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000565 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
566 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000567 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
568 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000569 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000570
571 // Custom lower v2i64 and v2f64 selects.
572 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000573 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000574 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000575 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000576 }
577
Evan Cheng6be2c582006-04-05 23:38:46 +0000578 // We want to custom lower some of our intrinsics.
579 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
580
Evan Cheng206ee9d2006-07-07 08:33:52 +0000581 // We have target-specific dag combine patterns for the following nodes:
582 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000583 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000584
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000585 computeRegisterProperties();
586
Evan Cheng87ed7162006-02-14 08:25:08 +0000587 // FIXME: These should be based on subtarget info. Plus, the values should
588 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000589 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
590 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
591 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000592 allowUnalignedMemoryAccesses = true; // x86 supports it!
593}
594
Chris Lattner2b02a442007-02-25 08:29:00 +0000595
596//===----------------------------------------------------------------------===//
597// Return Value Calling Convention Implementation
598//===----------------------------------------------------------------------===//
599
Chris Lattner59ed56b2007-02-28 04:55:35 +0000600#include "X86GenCallingConv.inc"
Chris Lattner9774c912007-02-27 05:28:59 +0000601
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000602/// LowerRET - Lower an ISD::RET node.
603SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
604 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
605
Chris Lattner9774c912007-02-27 05:28:59 +0000606 SmallVector<CCValAssign, 16> RVLocs;
607 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000608 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
609 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000610 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000611
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000612
613 // If this is the first return lowered for this function, add the regs to the
614 // liveout set for the function.
615 if (DAG.getMachineFunction().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000616 for (unsigned i = 0; i != RVLocs.size(); ++i)
617 if (RVLocs[i].isRegLoc())
618 DAG.getMachineFunction().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000619 }
620
621 SDOperand Chain = Op.getOperand(0);
622 SDOperand Flag;
623
624 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000625 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
626 RVLocs[0].getLocReg() != X86::ST0) {
627 for (unsigned i = 0; i != RVLocs.size(); ++i) {
628 CCValAssign &VA = RVLocs[i];
629 assert(VA.isRegLoc() && "Can only return in registers!");
630 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
631 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000632 Flag = Chain.getValue(1);
633 }
634 } else {
635 // We need to handle a destination of ST0 specially, because it isn't really
636 // a register.
637 SDOperand Value = Op.getOperand(1);
638
639 // If this is an FP return with ScalarSSE, we need to move the value from
640 // an XMM register onto the fp-stack.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000641 if ((X86ScalarSSEf32 && RVLocs[0].getValVT()==MVT::f32) ||
642 (X86ScalarSSEf64 && RVLocs[0].getValVT()==MVT::f64)) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000643 SDOperand MemLoc;
644
645 // If this is a load into a scalarsse value, don't store the loaded value
646 // back to the stack, only to reload it: just replace the scalar-sse load.
647 if (ISD::isNON_EXTLoad(Value.Val) &&
648 (Chain == Value.getValue(1) || Chain == Value.getOperand(0))) {
649 Chain = Value.getOperand(0);
650 MemLoc = Value.getOperand(1);
651 } else {
652 // Spill the value to memory and reload it into top of stack.
Chris Lattner9774c912007-02-27 05:28:59 +0000653 unsigned Size = MVT::getSizeInBits(RVLocs[0].getValVT())/8;
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000654 MachineFunction &MF = DAG.getMachineFunction();
655 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
656 MemLoc = DAG.getFrameIndex(SSFI, getPointerTy());
657 Chain = DAG.getStore(Op.getOperand(0), Value, MemLoc, NULL, 0);
658 }
Dale Johannesen849f2142007-07-03 00:53:03 +0000659 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other);
Chris Lattner9774c912007-02-27 05:28:59 +0000660 SDOperand Ops[] = {Chain, MemLoc, DAG.getValueType(RVLocs[0].getValVT())};
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000661 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
662 Chain = Value.getValue(1);
663 }
664
665 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
666 SDOperand Ops[] = { Chain, Value };
667 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
668 Flag = Chain.getValue(1);
669 }
670
671 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
672 if (Flag.Val)
673 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
674 else
675 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
676}
677
678
Chris Lattner3085e152007-02-25 08:59:22 +0000679/// LowerCallResult - Lower the result values of an ISD::CALL into the
680/// appropriate copies out of appropriate physical registers. This assumes that
681/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
682/// being lowered. The returns a SDNode with the same number of values as the
683/// ISD::CALL.
684SDNode *X86TargetLowering::
685LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
686 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000687
688 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000689 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000690 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
691 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000692 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
693
Chris Lattner3085e152007-02-25 08:59:22 +0000694
Chris Lattnere32bbf62007-02-28 07:09:55 +0000695 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000696
697 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000698 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
699 for (unsigned i = 0; i != RVLocs.size(); ++i) {
700 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
701 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000702 InFlag = Chain.getValue(2);
703 ResultVals.push_back(Chain.getValue(0));
704 }
705 } else {
706 // Copies from the FP stack are special, as ST0 isn't a valid register
707 // before the fp stackifier runs.
708
709 // Copy ST0 into an RFP register with FP_GET_RESULT.
Dale Johannesen849f2142007-07-03 00:53:03 +0000710 SDVTList Tys = DAG.getVTList(RVLocs[0].getValVT(), MVT::Other, MVT::Flag);
Chris Lattner3085e152007-02-25 08:59:22 +0000711 SDOperand GROps[] = { Chain, InFlag };
712 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
713 Chain = RetVal.getValue(1);
714 InFlag = RetVal.getValue(2);
715
716 // If we are using ScalarSSE, store ST(0) to the stack and reload it into
717 // an XMM register.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000718 if ((X86ScalarSSEf32 && RVLocs[0].getValVT() == MVT::f32) ||
719 (X86ScalarSSEf64 && RVLocs[0].getValVT() == MVT::f64)) {
Chris Lattner3085e152007-02-25 08:59:22 +0000720 // FIXME: Currently the FST is flagged to the FP_GET_RESULT. This
721 // shouldn't be necessary except that RFP cannot be live across
722 // multiple blocks. When stackifier is fixed, they can be uncoupled.
723 MachineFunction &MF = DAG.getMachineFunction();
724 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
725 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
726 SDOperand Ops[] = {
Chris Lattner9774c912007-02-27 05:28:59 +0000727 Chain, RetVal, StackSlot, DAG.getValueType(RVLocs[0].getValVT()), InFlag
Chris Lattner3085e152007-02-25 08:59:22 +0000728 };
729 Chain = DAG.getNode(X86ISD::FST, MVT::Other, Ops, 5);
Chris Lattner9774c912007-02-27 05:28:59 +0000730 RetVal = DAG.getLoad(RVLocs[0].getValVT(), Chain, StackSlot, NULL, 0);
Chris Lattner3085e152007-02-25 08:59:22 +0000731 Chain = RetVal.getValue(1);
732 }
Chris Lattner3085e152007-02-25 08:59:22 +0000733 ResultVals.push_back(RetVal);
734 }
735
736 // Merge everything together with a MERGE_VALUES node.
737 ResultVals.push_back(Chain);
738 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
739 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000740}
741
742
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000743//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000744// C & StdCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000745//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000746// StdCall calling convention seems to be standard for many Windows' API
747// routines and around. It differs from C calling convention just a little:
748// callee should clean up the stack, not caller. Symbols should be also
749// decorated in some fancy way :) It doesn't support any vector arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000750
Evan Cheng85e38002006-04-27 05:35:28 +0000751/// AddLiveIn - This helper function adds the specified physical register to the
752/// MachineFunction as a live in value. It also creates a corresponding virtual
753/// register for it.
754static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000755 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000756 assert(RC->contains(PReg) && "Not the correct regclass!");
757 unsigned VReg = MF.getSSARegMap()->createVirtualRegister(RC);
758 MF.addLiveIn(PReg, VReg);
759 return VReg;
760}
761
Rafael Espindola7effac52007-09-14 15:48:13 +0000762SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
763 const CCValAssign &VA,
764 MachineFrameInfo *MFI,
765 SDOperand Root, unsigned i) {
766 // Create the nodes corresponding to a load from this parameter slot.
767 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
768 VA.getLocMemOffset());
769 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
770
771 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
772
773 if (Flags & ISD::ParamFlags::ByVal)
774 return FIN;
775 else
776 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
777}
778
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000779SDOperand X86TargetLowering::LowerCCCArguments(SDOperand Op, SelectionDAG &DAG,
780 bool isStdCall) {
Evan Cheng25caf632006-05-23 21:06:34 +0000781 unsigned NumArgs = Op.Val->getNumValues() - 1;
Evan Cheng1bc78042006-04-26 01:20:17 +0000782 MachineFunction &MF = DAG.getMachineFunction();
783 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +0000784 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000785 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000786
Chris Lattner638402b2007-02-28 07:00:42 +0000787 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000788 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000789 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
790 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000791 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_C);
792
Chris Lattnerf39f7712007-02-28 05:46:49 +0000793 SmallVector<SDOperand, 8> ArgValues;
794 unsigned LastVal = ~0U;
795 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
796 CCValAssign &VA = ArgLocs[i];
797 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
798 // places.
799 assert(VA.getValNo() != LastVal &&
800 "Don't support value assigned to multiple locs yet");
801 LastVal = VA.getValNo();
802
803 if (VA.isRegLoc()) {
804 MVT::ValueType RegVT = VA.getLocVT();
805 TargetRegisterClass *RC;
806 if (RegVT == MVT::i32)
807 RC = X86::GR32RegisterClass;
808 else {
809 assert(MVT::isVector(RegVT));
810 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000811 }
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000812
Chris Lattner82932a52007-03-02 05:12:29 +0000813 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
814 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +0000815
816 // If this is an 8 or 16-bit value, it is really passed promoted to 32
817 // bits. Insert an assert[sz]ext to capture this, then truncate to the
818 // right size.
819 if (VA.getLocInfo() == CCValAssign::SExt)
820 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
821 DAG.getValueType(VA.getValVT()));
822 else if (VA.getLocInfo() == CCValAssign::ZExt)
823 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
824 DAG.getValueType(VA.getValVT()));
825
826 if (VA.getLocInfo() != CCValAssign::Full)
827 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
828
829 ArgValues.push_back(ArgValue);
830 } else {
831 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +0000832 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +0000833 }
Evan Cheng1bc78042006-04-26 01:20:17 +0000834 }
Chris Lattnerf39f7712007-02-28 05:46:49 +0000835
836 unsigned StackSize = CCInfo.getNextStackOffset();
Evan Cheng1bc78042006-04-26 01:20:17 +0000837
Evan Cheng25caf632006-05-23 21:06:34 +0000838 ArgValues.push_back(Root);
839
Evan Cheng1bc78042006-04-26 01:20:17 +0000840 // If the function takes variable number of arguments, make a frame index for
841 // the start of the first vararg value... for expansion of llvm.va_start.
Evan Cheng4db3af32006-05-23 21:08:24 +0000842 if (isVarArg)
Chris Lattnerf39f7712007-02-28 05:46:49 +0000843 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000844
845 if (isStdCall && !isVarArg) {
Chris Lattnerf39f7712007-02-28 05:46:49 +0000846 BytesToPopOnReturn = StackSize; // Callee pops everything..
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000847 BytesCallerReserves = 0;
848 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000849 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +0000850
851 // If this is an sret function, the return should pop the hidden pointer.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000852 if (NumArgs &&
853 (cast<ConstantSDNode>(Op.getOperand(3))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000854 ISD::ParamFlags::StructReturn))
Chris Lattnerf39f7712007-02-28 05:46:49 +0000855 BytesToPopOnReturn = 4;
856
857 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000858 }
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000859
Evan Cheng25ab6902006-09-08 06:48:29 +0000860 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Evan Cheng25caf632006-05-23 21:06:34 +0000861
Anton Korobeynikova2780e12007-08-15 17:12:32 +0000862 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
863 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +0000864
Evan Cheng25caf632006-05-23 21:06:34 +0000865 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +0000866 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +0000867 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000868}
869
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000870SDOperand X86TargetLowering::LowerCCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +0000871 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +0000872 SDOperand Chain = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000873 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +0000874 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
875 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +0000876 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000877
Chris Lattner638402b2007-02-28 07:00:42 +0000878 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +0000879 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000880 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +0000881 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_C);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000882
Chris Lattner423c5f42007-02-28 05:31:48 +0000883 // Get a count of how many bytes are to be pushed on the stack.
884 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000885
Evan Cheng32fe1032006-05-25 00:59:30 +0000886 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000887
Chris Lattner5a88b832007-02-25 07:10:00 +0000888 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
889 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +0000890
Chris Lattner423c5f42007-02-28 05:31:48 +0000891 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +0000892
893 // Walk the register/memloc assignments, inserting copies/loads.
894 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
895 CCValAssign &VA = ArgLocs[i];
896 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000897
Chris Lattner423c5f42007-02-28 05:31:48 +0000898 // Promote the value if needed.
899 switch (VA.getLocInfo()) {
900 default: assert(0 && "Unknown loc info!");
901 case CCValAssign::Full: break;
902 case CCValAssign::SExt:
903 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
904 break;
905 case CCValAssign::ZExt:
906 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
907 break;
908 case CCValAssign::AExt:
909 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
910 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +0000911 }
Chris Lattner423c5f42007-02-28 05:31:48 +0000912
913 if (VA.isRegLoc()) {
914 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
915 } else {
916 assert(VA.isMemLoc());
917 if (StackPtr.Val == 0)
918 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +0000919
920 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
921 Arg));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000922 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000923 }
924
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000925 // If the first argument is an sret pointer, remember it.
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +0000926 bool isSRet = NumOps &&
927 (cast<ConstantSDNode>(Op.getOperand(6))->getValue() &
Anton Korobeynikovd0b82b32007-03-07 16:25:09 +0000928 ISD::ParamFlags::StructReturn);
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000929
Evan Cheng32fe1032006-05-25 00:59:30 +0000930 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000931 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
932 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000933
Evan Cheng347d5f72006-04-28 21:29:37 +0000934 // Build a sequence of copy-to-reg nodes chained together with token chain
935 // and flag operands which copy the outgoing args into registers.
936 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +0000937 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
938 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
939 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +0000940 InFlag = Chain.getValue(1);
941 }
942
Evan Chengf4684712007-02-21 21:18:14 +0000943 // ELF / PIC requires GOT in the EBX register before function calls via PLT
944 // GOT pointer.
Evan Cheng706535d2007-01-22 21:34:25 +0000945 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
946 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000947 Chain = DAG.getCopyToReg(Chain, X86::EBX,
948 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
949 InFlag);
950 InFlag = Chain.getValue(1);
951 }
952
Evan Cheng32fe1032006-05-25 00:59:30 +0000953 // If the callee is a GlobalAddress node (quite common, every direct call is)
954 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +0000955 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +0000956 // We should use extra load for direct calls to dllimported functions in
957 // non-JIT mode.
958 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
959 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +0000960 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
961 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +0000962 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
963
Chris Lattnerd96d0722007-02-25 06:40:16 +0000964 // Returns a chain & a flag for retval copy to use.
965 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +0000966 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000967 Ops.push_back(Chain);
968 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +0000969
970 // Add argument registers to the end of the list so that they are known live
971 // into the call.
972 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000973 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +0000974 RegsToPass[i].second.getValueType()));
Evan Chengf4684712007-02-21 21:18:14 +0000975
976 // Add an implicit use GOT pointer in EBX.
977 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
978 Subtarget->isPICStyleGOT())
979 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Anton Korobeynikov7f705592007-01-12 19:20:47 +0000980
Evan Cheng347d5f72006-04-28 21:29:37 +0000981 if (InFlag.Val)
982 Ops.push_back(InFlag);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000983
Evan Cheng32fe1032006-05-25 00:59:30 +0000984 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +0000985 NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +0000986 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +0000987
Chris Lattner2d297092006-05-23 18:50:38 +0000988 // Create the CALLSEQ_END node.
989 unsigned NumBytesForCalleeToPush = 0;
990
Chris Lattner09c75a42007-02-25 09:06:15 +0000991 if (CC == CallingConv::X86_StdCall) {
992 if (isVarArg)
Chris Lattnerc0bdf342007-02-28 05:39:26 +0000993 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Chris Lattner09c75a42007-02-25 09:06:15 +0000994 else
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000995 NumBytesForCalleeToPush = NumBytes;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000996 } else {
997 // If this is is a call to a struct-return function, the callee
998 // pops the hidden struct pointer, so we have to push it back.
999 // This is common for Darwin/X86, Linux & Mingw32 targets.
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001000 NumBytesForCalleeToPush = isSRet ? 4 : 0;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001001 }
1002
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001003 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001004 Ops.clear();
1005 Ops.push_back(Chain);
1006 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattner2d297092006-05-23 18:50:38 +00001007 Ops.push_back(DAG.getConstant(NumBytesForCalleeToPush, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001008 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001009 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner3085e152007-02-25 08:59:22 +00001010 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001011
Chris Lattner3085e152007-02-25 08:59:22 +00001012 // Handle result values, copying them out of physregs into vregs that we
1013 // return.
Chris Lattner09c75a42007-02-25 09:06:15 +00001014 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001015}
1016
Evan Cheng25ab6902006-09-08 06:48:29 +00001017
1018//===----------------------------------------------------------------------===//
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001019// FastCall Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001020//===----------------------------------------------------------------------===//
1021//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001022// The X86 'fastcall' calling convention passes up to two integer arguments in
1023// registers (an appropriate portion of ECX/EDX), passes arguments in C order,
1024// and requires that the callee pop its arguments off the stack (allowing proper
1025// tail calls), and has the same return value conventions as C calling convs.
1026//
1027// This calling convention always arranges for the callee pop value to be 8n+4
1028// bytes, which is needed for tail recursion elimination and stack alignment
1029// reasons.
Evan Cheng25caf632006-05-23 21:06:34 +00001030SDOperand
Chris Lattner2db39b82007-02-28 06:05:16 +00001031X86TargetLowering::LowerFastCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001032 MachineFunction &MF = DAG.getMachineFunction();
1033 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001034 SDOperand Root = Op.getOperand(0);
Chris Lattner52387be2007-06-19 00:13:10 +00001035 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001036
Chris Lattner638402b2007-02-28 07:00:42 +00001037 // Assign locations to all of the incoming arguments.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001038 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001039 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1040 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001041 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_32_FastCall);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001042
1043 SmallVector<SDOperand, 8> ArgValues;
1044 unsigned LastVal = ~0U;
1045 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1046 CCValAssign &VA = ArgLocs[i];
1047 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1048 // places.
1049 assert(VA.getValNo() != LastVal &&
1050 "Don't support value assigned to multiple locs yet");
1051 LastVal = VA.getValNo();
1052
1053 if (VA.isRegLoc()) {
1054 MVT::ValueType RegVT = VA.getLocVT();
1055 TargetRegisterClass *RC;
1056 if (RegVT == MVT::i32)
1057 RC = X86::GR32RegisterClass;
1058 else {
1059 assert(MVT::isVector(RegVT));
1060 RC = X86::VR128RegisterClass;
1061 }
1062
Chris Lattner82932a52007-03-02 05:12:29 +00001063 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1064 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfc664c12007-02-28 06:21:19 +00001065
1066 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1067 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1068 // right size.
1069 if (VA.getLocInfo() == CCValAssign::SExt)
1070 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1071 DAG.getValueType(VA.getValVT()));
1072 else if (VA.getLocInfo() == CCValAssign::ZExt)
1073 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1074 DAG.getValueType(VA.getValVT()));
1075
1076 if (VA.getLocInfo() != CCValAssign::Full)
1077 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1078
1079 ArgValues.push_back(ArgValue);
1080 } else {
1081 assert(VA.isMemLoc());
Rafael Espindola1242d282007-09-21 14:55:38 +00001082 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfc664c12007-02-28 06:21:19 +00001083 }
1084 }
1085
Evan Cheng25caf632006-05-23 21:06:34 +00001086 ArgValues.push_back(Root);
1087
Chris Lattnerfc664c12007-02-28 06:21:19 +00001088 unsigned StackSize = CCInfo.getNextStackOffset();
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001089
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001090 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001091 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1092 // arguments and the arguments after the retaddr has been pushed are aligned.
1093 if ((StackSize & 7) == 0)
1094 StackSize += 4;
1095 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001096
1097 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
Evan Cheng25ab6902006-09-08 06:48:29 +00001098 RegSaveFrameIndex = 0xAAAAAAA; // X86-64 only.
Chris Lattnerfc664c12007-02-28 06:21:19 +00001099 BytesToPopOnReturn = StackSize; // Callee pops all stack arguments.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001100 BytesCallerReserves = 0;
1101
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001102 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1103 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001104
Evan Cheng25caf632006-05-23 21:06:34 +00001105 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001106 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001107 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001108}
1109
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001110SDOperand
1111X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1112 const SDOperand &StackPtr,
1113 const CCValAssign &VA,
1114 SDOperand Chain,
1115 SDOperand Arg) {
1116 SDOperand PtrOff = DAG.getConstant(VA.getLocMemOffset(), getPointerTy());
1117 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1118 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1119 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1120 if (Flags & ISD::ParamFlags::ByVal) {
1121 unsigned Align = 1 << ((Flags & ISD::ParamFlags::ByValAlign) >>
1122 ISD::ParamFlags::ByValAlignOffs);
1123
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001124 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
1125 ISD::ParamFlags::ByValSizeOffs;
1126
1127 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1128 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
1129
1130 return DAG.getNode(ISD::MEMCPY, MVT::Other, Chain, PtrOff, Arg, SizeNode,
1131 AlignNode);
1132 } else {
1133 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
1134 }
1135}
1136
Chris Lattnere87e1152006-09-26 03:57:53 +00001137SDOperand X86TargetLowering::LowerFastCCCallTo(SDOperand Op, SelectionDAG &DAG,
Chris Lattner09c75a42007-02-25 09:06:15 +00001138 unsigned CC) {
Evan Cheng32fe1032006-05-25 00:59:30 +00001139 SDOperand Chain = Op.getOperand(0);
Evan Cheng32fe1032006-05-25 00:59:30 +00001140 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
Chris Lattner52387be2007-06-19 00:13:10 +00001141 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Evan Cheng32fe1032006-05-25 00:59:30 +00001142 SDOperand Callee = Op.getOperand(4);
Evan Cheng32fe1032006-05-25 00:59:30 +00001143
Chris Lattner638402b2007-02-28 07:00:42 +00001144 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001145 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001146 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001147 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_32_FastCall);
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001148
1149 // Get a count of how many bytes are to be pushed on the stack.
1150 unsigned NumBytes = CCInfo.getNextStackOffset();
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001151
Anton Korobeynikovf7dcfa82007-03-02 21:50:27 +00001152 if (!Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows()) {
Anton Korobeynikov9dd9abd2007-03-01 16:29:22 +00001153 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1154 // arguments and the arguments after the retaddr has been pushed are aligned.
1155 if ((NumBytes & 7) == 0)
1156 NumBytes += 4;
1157 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001158
Chris Lattner94dd2922006-02-13 09:00:43 +00001159 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001160
Chris Lattner5a88b832007-02-25 07:10:00 +00001161 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1162 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001163
1164 SDOperand StackPtr;
1165
1166 // Walk the register/memloc assignments, inserting copies/loads.
1167 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1168 CCValAssign &VA = ArgLocs[i];
1169 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1170
1171 // Promote the value if needed.
1172 switch (VA.getLocInfo()) {
1173 default: assert(0 && "Unknown loc info!");
1174 case CCValAssign::Full: break;
1175 case CCValAssign::SExt:
1176 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
Chris Lattner2db39b82007-02-28 06:05:16 +00001177 break;
Chris Lattnerf5d280a2007-02-28 06:26:33 +00001178 case CCValAssign::ZExt:
1179 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1180 break;
1181 case CCValAssign::AExt:
1182 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1183 break;
1184 }
1185
1186 if (VA.isRegLoc()) {
1187 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1188 } else {
1189 assert(VA.isMemLoc());
1190 if (StackPtr.Val == 0)
1191 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindolaa37ac9f2007-09-21 15:50:22 +00001192
1193 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1194 Arg));
Evan Cheng32fe1032006-05-25 00:59:30 +00001195 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001196 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001197
Evan Cheng32fe1032006-05-25 00:59:30 +00001198 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001199 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1200 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001201
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001202 // Build a sequence of copy-to-reg nodes chained together with token chain
1203 // and flag operands which copy the outgoing args into registers.
1204 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001205 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1206 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1207 InFlag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001208 InFlag = Chain.getValue(1);
1209 }
1210
Evan Cheng32fe1032006-05-25 00:59:30 +00001211 // If the callee is a GlobalAddress node (quite common, every direct call is)
1212 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001213 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001214 // We should use extra load for direct calls to dllimported functions in
1215 // non-JIT mode.
1216 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1217 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001218 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1219 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Cheng32fe1032006-05-25 00:59:30 +00001220 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1221
Evan Chengf4684712007-02-21 21:18:14 +00001222 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1223 // GOT pointer.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001224 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1225 Subtarget->isPICStyleGOT()) {
1226 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1227 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1228 InFlag);
1229 InFlag = Chain.getValue(1);
1230 }
1231
Chris Lattnerd96d0722007-02-25 06:40:16 +00001232 // Returns a chain & a flag for retval copy to use.
1233 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001234 SmallVector<SDOperand, 8> Ops;
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001235 Ops.push_back(Chain);
1236 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001237
1238 // Add argument registers to the end of the list so that they are known live
1239 // into the call.
1240 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001241 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
Evan Chengb69d1132006-06-14 18:17:40 +00001242 RegsToPass[i].second.getValueType()));
1243
Evan Chengf4684712007-02-21 21:18:14 +00001244 // Add an implicit use GOT pointer in EBX.
1245 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1246 Subtarget->isPICStyleGOT())
1247 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1248
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001249 if (InFlag.Val)
1250 Ops.push_back(InFlag);
1251
1252 // FIXME: Do not generate X86ISD::TAILCALL for now.
Chris Lattner8c0c10c2006-05-16 06:45:34 +00001253 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001254 NodeTys, &Ops[0], Ops.size());
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001255 InFlag = Chain.getValue(1);
1256
Chris Lattner7d53a1c2007-02-25 07:18:38 +00001257 // Returns a flag for retval copy to use.
1258 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001259 Ops.clear();
1260 Ops.push_back(Chain);
Evan Cheng32fe1032006-05-25 00:59:30 +00001261 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1262 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001263 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001264 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
Chris Lattner339b4392007-02-25 09:10:05 +00001265 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001266
Chris Lattner339b4392007-02-25 09:10:05 +00001267 // Handle result values, copying them out of physregs into vregs that we
1268 // return.
1269 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001270}
1271
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001272
1273//===----------------------------------------------------------------------===//
1274// X86-64 C Calling Convention implementation
1275//===----------------------------------------------------------------------===//
1276
1277SDOperand
1278X86TargetLowering::LowerX86_64CCCArguments(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001279 MachineFunction &MF = DAG.getMachineFunction();
1280 MachineFrameInfo *MFI = MF.getFrameInfo();
1281 SDOperand Root = Op.getOperand(0);
1282 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1283
1284 static const unsigned GPR64ArgRegs[] = {
1285 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1286 };
1287 static const unsigned XMMArgRegs[] = {
1288 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1289 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1290 };
1291
Chris Lattner638402b2007-02-28 07:00:42 +00001292
1293 // Assign locations to all of the incoming arguments.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001294 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001295 CCState CCInfo(MF.getFunction()->getCallingConv(), isVarArg,
1296 getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001297 CCInfo.AnalyzeFormalArguments(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001298
1299 SmallVector<SDOperand, 8> ArgValues;
1300 unsigned LastVal = ~0U;
1301 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1302 CCValAssign &VA = ArgLocs[i];
1303 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1304 // places.
1305 assert(VA.getValNo() != LastVal &&
1306 "Don't support value assigned to multiple locs yet");
1307 LastVal = VA.getValNo();
1308
1309 if (VA.isRegLoc()) {
1310 MVT::ValueType RegVT = VA.getLocVT();
1311 TargetRegisterClass *RC;
1312 if (RegVT == MVT::i32)
1313 RC = X86::GR32RegisterClass;
1314 else if (RegVT == MVT::i64)
1315 RC = X86::GR64RegisterClass;
1316 else if (RegVT == MVT::f32)
1317 RC = X86::FR32RegisterClass;
1318 else if (RegVT == MVT::f64)
1319 RC = X86::FR64RegisterClass;
1320 else {
1321 assert(MVT::isVector(RegVT));
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001322 if (MVT::getSizeInBits(RegVT) == 64) {
1323 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1324 RegVT = MVT::i64;
1325 } else
Chris Lattner6b7c21c2007-06-09 05:01:50 +00001326 RC = X86::VR128RegisterClass;
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001327 }
Chris Lattner82932a52007-03-02 05:12:29 +00001328
1329 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1330 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001331
1332 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1333 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1334 // right size.
1335 if (VA.getLocInfo() == CCValAssign::SExt)
1336 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1337 DAG.getValueType(VA.getValVT()));
1338 else if (VA.getLocInfo() == CCValAssign::ZExt)
1339 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1340 DAG.getValueType(VA.getValVT()));
1341
1342 if (VA.getLocInfo() != CCValAssign::Full)
1343 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1344
Chris Lattnerfdbe7202007-06-09 05:08:10 +00001345 // Handle MMX values passed in GPRs.
1346 if (RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1347 MVT::getSizeInBits(RegVT) == 64)
1348 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1349
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001350 ArgValues.push_back(ArgValue);
1351 } else {
1352 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001353 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001354 }
1355 }
1356
1357 unsigned StackSize = CCInfo.getNextStackOffset();
1358
1359 // If the function takes variable number of arguments, make a frame index for
1360 // the start of the first vararg value... for expansion of llvm.va_start.
1361 if (isVarArg) {
1362 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1363 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1364
1365 // For X86-64, if there are vararg parameters that are passed via
1366 // registers, then we must store them to their spots on the stack so they
1367 // may be loaded by deferencing the result of va_next.
1368 VarArgsGPOffset = NumIntRegs * 8;
1369 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1370 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1371 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1372
1373 // Store the integer parameter registers.
1374 SmallVector<SDOperand, 8> MemOps;
1375 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1376 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1377 DAG.getConstant(VarArgsGPOffset, getPointerTy()));
1378 for (; NumIntRegs != 6; ++NumIntRegs) {
1379 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1380 X86::GR64RegisterClass);
1381 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1382 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1383 MemOps.push_back(Store);
1384 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1385 DAG.getConstant(8, getPointerTy()));
1386 }
1387
1388 // Now store the XMM (fp + vector) parameter registers.
1389 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
1390 DAG.getConstant(VarArgsFPOffset, getPointerTy()));
1391 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1392 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1393 X86::VR128RegisterClass);
1394 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1395 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1396 MemOps.push_back(Store);
1397 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
1398 DAG.getConstant(16, getPointerTy()));
1399 }
1400 if (!MemOps.empty())
1401 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1402 &MemOps[0], MemOps.size());
1403 }
1404
1405 ArgValues.push_back(Root);
1406
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001407 BytesToPopOnReturn = 0; // Callee pops nothing.
1408 BytesCallerReserves = StackSize;
1409
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001410 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1411 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
1412
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001413 // Return the new list of results.
1414 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
1415 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
1416}
1417
1418SDOperand
1419X86TargetLowering::LowerX86_64CCCCallTo(SDOperand Op, SelectionDAG &DAG,
1420 unsigned CC) {
1421 SDOperand Chain = Op.getOperand(0);
1422 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1423 bool isTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0;
1424 SDOperand Callee = Op.getOperand(4);
Chris Lattner638402b2007-02-28 07:00:42 +00001425
1426 // Analyze operands of the call, assigning locations to each operand.
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001427 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001428 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner638402b2007-02-28 07:00:42 +00001429 CCInfo.AnalyzeCallOperands(Op.Val, CC_X86_64_C);
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001430
1431 // Get a count of how many bytes are to be pushed on the stack.
1432 unsigned NumBytes = CCInfo.getNextStackOffset();
1433 Chain = DAG.getCALLSEQ_START(Chain,DAG.getConstant(NumBytes, getPointerTy()));
1434
1435 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1436 SmallVector<SDOperand, 8> MemOpChains;
1437
1438 SDOperand StackPtr;
1439
1440 // Walk the register/memloc assignments, inserting copies/loads.
1441 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1442 CCValAssign &VA = ArgLocs[i];
1443 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
1444
1445 // Promote the value if needed.
1446 switch (VA.getLocInfo()) {
1447 default: assert(0 && "Unknown loc info!");
1448 case CCValAssign::Full: break;
1449 case CCValAssign::SExt:
1450 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1451 break;
1452 case CCValAssign::ZExt:
1453 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1454 break;
1455 case CCValAssign::AExt:
1456 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1457 break;
1458 }
1459
1460 if (VA.isRegLoc()) {
1461 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1462 } else {
1463 assert(VA.isMemLoc());
1464 if (StackPtr.Val == 0)
1465 StackPtr = DAG.getRegister(getStackPtrReg(), getPointerTy());
Rafael Espindola21485be2007-08-20 15:18:24 +00001466
Rafael Espindola1b5dcc32007-08-31 15:06:30 +00001467 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1468 Arg));
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001469 }
1470 }
1471
1472 if (!MemOpChains.empty())
1473 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1474 &MemOpChains[0], MemOpChains.size());
1475
1476 // Build a sequence of copy-to-reg nodes chained together with token chain
1477 // and flag operands which copy the outgoing args into registers.
1478 SDOperand InFlag;
1479 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1480 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1481 InFlag);
1482 InFlag = Chain.getValue(1);
1483 }
1484
1485 if (isVarArg) {
1486 // From AMD64 ABI document:
1487 // For calls that may call functions that use varargs or stdargs
1488 // (prototype-less calls or calls to functions containing ellipsis (...) in
1489 // the declaration) %al is used as hidden argument to specify the number
1490 // of SSE registers used. The contents of %al do not need to match exactly
1491 // the number of registers, but must be an ubound on the number of SSE
1492 // registers used and is in the range 0 - 8 inclusive.
1493
1494 // Count the number of XMM registers allocated.
1495 static const unsigned XMMArgRegs[] = {
1496 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1497 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1498 };
1499 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1500
1501 Chain = DAG.getCopyToReg(Chain, X86::AL,
1502 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1503 InFlag = Chain.getValue(1);
1504 }
1505
1506 // If the callee is a GlobalAddress node (quite common, every direct call is)
1507 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
1508 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
1509 // We should use extra load for direct calls to dllimported functions in
1510 // non-JIT mode.
Evan Chengba693002007-03-14 22:11:11 +00001511 if (getTargetMachine().getCodeModel() != CodeModel::Large
Anton Korobeynikovbed29462007-04-16 18:10:23 +00001512 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1513 getTargetMachine(), true))
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001514 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
1515 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee))
Evan Chengba693002007-03-14 22:11:11 +00001516 if (getTargetMachine().getCodeModel() != CodeModel::Large)
1517 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001518
1519 // Returns a chain & a flag for retval copy to use.
1520 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1521 SmallVector<SDOperand, 8> Ops;
1522 Ops.push_back(Chain);
1523 Ops.push_back(Callee);
1524
1525 // Add argument registers to the end of the list so that they are known live
1526 // into the call.
1527 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1528 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1529 RegsToPass[i].second.getValueType()));
1530
1531 if (InFlag.Val)
1532 Ops.push_back(InFlag);
1533
1534 // FIXME: Do not generate X86ISD::TAILCALL for now.
1535 Chain = DAG.getNode(isTailCall ? X86ISD::TAILCALL : X86ISD::CALL,
1536 NodeTys, &Ops[0], Ops.size());
1537 InFlag = Chain.getValue(1);
1538
1539 // Returns a flag for retval copy to use.
1540 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1541 Ops.clear();
1542 Ops.push_back(Chain);
1543 Ops.push_back(DAG.getConstant(NumBytes, getPointerTy()));
1544 Ops.push_back(DAG.getConstant(0, getPointerTy()));
1545 Ops.push_back(InFlag);
1546 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1547 InFlag = Chain.getValue(1);
1548
1549 // Handle result values, copying them out of physregs into vregs that we
1550 // return.
1551 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1552}
1553
1554
1555//===----------------------------------------------------------------------===//
1556// Other Lowering Hooks
1557//===----------------------------------------------------------------------===//
1558
1559
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001560SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001561 MachineFunction &MF = DAG.getMachineFunction();
1562 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1563 int ReturnAddrIndex = FuncInfo->getRAIndex();
1564
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001565 if (ReturnAddrIndex == 0) {
1566 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00001567 if (Subtarget->is64Bit())
1568 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1569 else
1570 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001571
1572 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001573 }
1574
Evan Cheng25ab6902006-09-08 06:48:29 +00001575 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001576}
1577
1578
1579
Evan Cheng6dfa9992006-01-30 23:41:35 +00001580/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1581/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001582/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1583/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001584static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001585 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1586 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001587 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001588 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001589 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1590 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1591 // X > -1 -> X == 0, jump !sign.
1592 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001593 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001594 return true;
1595 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1596 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001597 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001598 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00001599 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1600 // X < 1 -> X <= 0
1601 RHS = DAG.getConstant(0, RHS.getValueType());
1602 X86CC = X86::COND_LE;
1603 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001604 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001605 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001606
Evan Chengd9558e02006-01-06 00:43:03 +00001607 switch (SetCCOpcode) {
1608 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001609 case ISD::SETEQ: X86CC = X86::COND_E; break;
1610 case ISD::SETGT: X86CC = X86::COND_G; break;
1611 case ISD::SETGE: X86CC = X86::COND_GE; break;
1612 case ISD::SETLT: X86CC = X86::COND_L; break;
1613 case ISD::SETLE: X86CC = X86::COND_LE; break;
1614 case ISD::SETNE: X86CC = X86::COND_NE; break;
1615 case ISD::SETULT: X86CC = X86::COND_B; break;
1616 case ISD::SETUGT: X86CC = X86::COND_A; break;
1617 case ISD::SETULE: X86CC = X86::COND_BE; break;
1618 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001619 }
1620 } else {
1621 // On a floating point condition, the flags are set as follows:
1622 // ZF PF CF op
1623 // 0 | 0 | 0 | X > Y
1624 // 0 | 0 | 1 | X < Y
1625 // 1 | 0 | 0 | X == Y
1626 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001627 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001628 switch (SetCCOpcode) {
1629 default: break;
1630 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001631 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001632 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001633 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001634 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001635 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001636 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001637 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001638 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001639 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001640 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001641 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001642 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001643 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001644 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001645 case ISD::SETNE: X86CC = X86::COND_NE; break;
1646 case ISD::SETUO: X86CC = X86::COND_P; break;
1647 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001648 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001649 if (Flip)
1650 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001651 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001652
Chris Lattner7fbe9722006-10-20 17:42:20 +00001653 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001654}
1655
Evan Cheng4a460802006-01-11 00:33:36 +00001656/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1657/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001658/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001659static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001660 switch (X86CC) {
1661 default:
1662 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001663 case X86::COND_B:
1664 case X86::COND_BE:
1665 case X86::COND_E:
1666 case X86::COND_P:
1667 case X86::COND_A:
1668 case X86::COND_AE:
1669 case X86::COND_NE:
1670 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001671 return true;
1672 }
1673}
1674
Evan Cheng5ced1d82006-04-06 23:23:56 +00001675/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001676/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001677static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1678 if (Op.getOpcode() == ISD::UNDEF)
1679 return true;
1680
1681 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001682 return (Val >= Low && Val < Hi);
1683}
1684
1685/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1686/// true if Op is undef or if its value equal to the specified value.
1687static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1688 if (Op.getOpcode() == ISD::UNDEF)
1689 return true;
1690 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001691}
1692
Evan Cheng0188ecb2006-03-22 18:59:22 +00001693/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1694/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1695bool X86::isPSHUFDMask(SDNode *N) {
1696 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1697
Dan Gohman7f55fcb2007-08-02 21:17:01 +00001698 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00001699 return false;
1700
1701 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001702 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001703 SDOperand Arg = N->getOperand(i);
1704 if (Arg.getOpcode() == ISD::UNDEF) continue;
1705 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00001706 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00001707 return false;
1708 }
1709
1710 return true;
1711}
1712
1713/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001714/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001715bool X86::isPSHUFHWMask(SDNode *N) {
1716 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1717
1718 if (N->getNumOperands() != 8)
1719 return false;
1720
1721 // Lower quadword copied in order.
1722 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001723 SDOperand Arg = N->getOperand(i);
1724 if (Arg.getOpcode() == ISD::UNDEF) continue;
1725 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1726 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001727 return false;
1728 }
1729
1730 // Upper quadword shuffled.
1731 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001732 SDOperand Arg = N->getOperand(i);
1733 if (Arg.getOpcode() == ISD::UNDEF) continue;
1734 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1735 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001736 if (Val < 4 || Val > 7)
1737 return false;
1738 }
1739
1740 return true;
1741}
1742
1743/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001744/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001745bool X86::isPSHUFLWMask(SDNode *N) {
1746 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1747
1748 if (N->getNumOperands() != 8)
1749 return false;
1750
1751 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001752 for (unsigned i = 4; i != 8; ++i)
1753 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001754 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001755
1756 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001757 for (unsigned i = 0; i != 4; ++i)
1758 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001759 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001760
1761 return true;
1762}
1763
Evan Cheng14aed5e2006-03-24 01:18:28 +00001764/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1765/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00001766static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00001767 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001768
Evan Cheng39623da2006-04-20 08:58:49 +00001769 unsigned Half = NumElems / 2;
1770 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001771 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00001772 return false;
1773 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001774 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00001775 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00001776
1777 return true;
1778}
1779
Evan Cheng39623da2006-04-20 08:58:49 +00001780bool X86::isSHUFPMask(SDNode *N) {
1781 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001782 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001783}
1784
Evan Cheng213d2cf2007-05-17 18:45:50 +00001785/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00001786/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
1787/// half elements to come from vector 1 (which would equal the dest.) and
1788/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00001789static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
1790 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001791
Chris Lattner5a88b832007-02-25 07:10:00 +00001792 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00001793 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00001794 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00001795 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00001796 for (unsigned i = Half; i < NumOps; ++i)
1797 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00001798 return false;
1799 return true;
1800}
1801
1802static bool isCommutedSHUFP(SDNode *N) {
1803 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001804 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00001805}
1806
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001807/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
1808/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
1809bool X86::isMOVHLPSMask(SDNode *N) {
1810 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1811
Evan Cheng2064a2b2006-03-28 06:50:32 +00001812 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00001813 return false;
1814
Evan Cheng2064a2b2006-03-28 06:50:32 +00001815 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00001816 return isUndefOrEqual(N->getOperand(0), 6) &&
1817 isUndefOrEqual(N->getOperand(1), 7) &&
1818 isUndefOrEqual(N->getOperand(2), 2) &&
1819 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00001820}
1821
Evan Cheng6e56e2c2006-11-07 22:14:24 +00001822/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
1823/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
1824/// <2, 3, 2, 3>
1825bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
1826 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1827
1828 if (N->getNumOperands() != 4)
1829 return false;
1830
1831 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
1832 return isUndefOrEqual(N->getOperand(0), 2) &&
1833 isUndefOrEqual(N->getOperand(1), 3) &&
1834 isUndefOrEqual(N->getOperand(2), 2) &&
1835 isUndefOrEqual(N->getOperand(3), 3);
1836}
1837
Evan Cheng5ced1d82006-04-06 23:23:56 +00001838/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
1839/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
1840bool X86::isMOVLPMask(SDNode *N) {
1841 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1842
1843 unsigned NumElems = N->getNumOperands();
1844 if (NumElems != 2 && NumElems != 4)
1845 return false;
1846
Evan Chengc5cdff22006-04-07 21:53:05 +00001847 for (unsigned i = 0; i < NumElems/2; ++i)
1848 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
1849 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001850
Evan Chengc5cdff22006-04-07 21:53:05 +00001851 for (unsigned i = NumElems/2; i < NumElems; ++i)
1852 if (!isUndefOrEqual(N->getOperand(i), i))
1853 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001854
1855 return true;
1856}
1857
1858/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00001859/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
1860/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00001861bool X86::isMOVHPMask(SDNode *N) {
1862 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1863
1864 unsigned NumElems = N->getNumOperands();
1865 if (NumElems != 2 && NumElems != 4)
1866 return false;
1867
Evan Chengc5cdff22006-04-07 21:53:05 +00001868 for (unsigned i = 0; i < NumElems/2; ++i)
1869 if (!isUndefOrEqual(N->getOperand(i), i))
1870 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001871
1872 for (unsigned i = 0; i < NumElems/2; ++i) {
1873 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00001874 if (!isUndefOrEqual(Arg, i + NumElems))
1875 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001876 }
1877
1878 return true;
1879}
1880
Evan Cheng0038e592006-03-28 00:39:58 +00001881/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
1882/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00001883bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
1884 bool V2IsSplat = false) {
1885 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00001886 return false;
1887
Chris Lattner5a88b832007-02-25 07:10:00 +00001888 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1889 SDOperand BitI = Elts[i];
1890 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00001891 if (!isUndefOrEqual(BitI, j))
1892 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001893 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001894 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001895 return false;
1896 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00001897 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001898 return false;
1899 }
Evan Cheng0038e592006-03-28 00:39:58 +00001900 }
1901
1902 return true;
1903}
1904
Evan Cheng39623da2006-04-20 08:58:49 +00001905bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
1906 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001907 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001908}
1909
Evan Cheng4fcb9222006-03-28 02:43:26 +00001910/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
1911/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00001912bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
1913 bool V2IsSplat = false) {
1914 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00001915 return false;
1916
Chris Lattner5a88b832007-02-25 07:10:00 +00001917 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
1918 SDOperand BitI = Elts[i];
1919 SDOperand BitI1 = Elts[i+1];
1920 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00001921 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00001922 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00001923 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001924 return false;
1925 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00001926 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00001927 return false;
1928 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00001929 }
1930
1931 return true;
1932}
1933
Evan Cheng39623da2006-04-20 08:58:49 +00001934bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
1935 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00001936 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00001937}
1938
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001939/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
1940/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
1941/// <0, 0, 1, 1>
1942bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
1943 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1944
1945 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00001946 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001947 return false;
1948
1949 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
1950 SDOperand BitI = N->getOperand(i);
1951 SDOperand BitI1 = N->getOperand(i+1);
1952
Evan Chengc5cdff22006-04-07 21:53:05 +00001953 if (!isUndefOrEqual(BitI, j))
1954 return false;
1955 if (!isUndefOrEqual(BitI1, j))
1956 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00001957 }
1958
1959 return true;
1960}
1961
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00001962/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
1963/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
1964/// <2, 2, 3, 3>
1965bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
1966 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1967
1968 unsigned NumElems = N->getNumOperands();
1969 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
1970 return false;
1971
1972 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
1973 SDOperand BitI = N->getOperand(i);
1974 SDOperand BitI1 = N->getOperand(i + 1);
1975
1976 if (!isUndefOrEqual(BitI, j))
1977 return false;
1978 if (!isUndefOrEqual(BitI1, j))
1979 return false;
1980 }
1981
1982 return true;
1983}
1984
Evan Cheng017dcc62006-04-21 01:05:10 +00001985/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
1986/// specifies a shuffle of elements that is suitable for input to MOVSS,
1987/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00001988static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
1989 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001990 return false;
1991
Chris Lattner5a88b832007-02-25 07:10:00 +00001992 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001993 return false;
1994
Chris Lattner5a88b832007-02-25 07:10:00 +00001995 for (unsigned i = 1; i < NumElts; ++i) {
1996 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00001997 return false;
1998 }
1999
2000 return true;
2001}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002002
Evan Cheng017dcc62006-04-21 01:05:10 +00002003bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002004 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002005 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002006}
2007
Evan Cheng017dcc62006-04-21 01:05:10 +00002008/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2009/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002010/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002011static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2012 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002013 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002014 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002015 return false;
2016
2017 if (!isUndefOrEqual(Ops[0], 0))
2018 return false;
2019
Chris Lattner5a88b832007-02-25 07:10:00 +00002020 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002021 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002022 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2023 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2024 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002025 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002026 }
2027
2028 return true;
2029}
2030
Evan Cheng8cf723d2006-09-08 01:50:06 +00002031static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2032 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002033 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002034 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2035 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002036}
2037
Evan Chengd9539472006-04-14 21:59:03 +00002038/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2039/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2040bool X86::isMOVSHDUPMask(SDNode *N) {
2041 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2042
2043 if (N->getNumOperands() != 4)
2044 return false;
2045
2046 // Expect 1, 1, 3, 3
2047 for (unsigned i = 0; i < 2; ++i) {
2048 SDOperand Arg = N->getOperand(i);
2049 if (Arg.getOpcode() == ISD::UNDEF) continue;
2050 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2051 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2052 if (Val != 1) return false;
2053 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002054
2055 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002056 for (unsigned i = 2; i < 4; ++i) {
2057 SDOperand Arg = N->getOperand(i);
2058 if (Arg.getOpcode() == ISD::UNDEF) continue;
2059 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2060 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2061 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002062 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002063 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002064
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002065 // Don't use movshdup if it can be done with a shufps.
2066 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002067}
2068
2069/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2070/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2071bool X86::isMOVSLDUPMask(SDNode *N) {
2072 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2073
2074 if (N->getNumOperands() != 4)
2075 return false;
2076
2077 // Expect 0, 0, 2, 2
2078 for (unsigned i = 0; i < 2; ++i) {
2079 SDOperand Arg = N->getOperand(i);
2080 if (Arg.getOpcode() == ISD::UNDEF) continue;
2081 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2082 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2083 if (Val != 0) return false;
2084 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002085
2086 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002087 for (unsigned i = 2; i < 4; ++i) {
2088 SDOperand Arg = N->getOperand(i);
2089 if (Arg.getOpcode() == ISD::UNDEF) continue;
2090 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2091 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2092 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002093 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002094 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002095
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002096 // Don't use movshdup if it can be done with a shufps.
2097 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002098}
2099
Evan Cheng49892af2007-06-19 00:02:56 +00002100/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2101/// specifies a identity operation on the LHS or RHS.
2102static bool isIdentityMask(SDNode *N, bool RHS = false) {
2103 unsigned NumElems = N->getNumOperands();
2104 for (unsigned i = 0; i < NumElems; ++i)
2105 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2106 return false;
2107 return true;
2108}
2109
Evan Chengb9df0ca2006-03-22 02:53:00 +00002110/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2111/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002112static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002113 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2114
Evan Chengb9df0ca2006-03-22 02:53:00 +00002115 // This is a splat operation if each element of the permute is the same, and
2116 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002117 unsigned NumElems = N->getNumOperands();
2118 SDOperand ElementBase;
2119 unsigned i = 0;
2120 for (; i != NumElems; ++i) {
2121 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002122 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002123 ElementBase = Elt;
2124 break;
2125 }
2126 }
2127
2128 if (!ElementBase.Val)
2129 return false;
2130
2131 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002132 SDOperand Arg = N->getOperand(i);
2133 if (Arg.getOpcode() == ISD::UNDEF) continue;
2134 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002135 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002136 }
2137
2138 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002139 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002140}
2141
Evan Chengc575ca22006-04-17 20:43:08 +00002142/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2143/// a splat of a single element and it's a 2 or 4 element mask.
2144bool X86::isSplatMask(SDNode *N) {
2145 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2146
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002147 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002148 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2149 return false;
2150 return ::isSplatMask(N);
2151}
2152
Evan Chengf686d9b2006-10-27 21:08:32 +00002153/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2154/// specifies a splat of zero element.
2155bool X86::isSplatLoMask(SDNode *N) {
2156 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2157
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002158 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002159 if (!isUndefOrEqual(N->getOperand(i), 0))
2160 return false;
2161 return true;
2162}
2163
Evan Cheng63d33002006-03-22 08:01:21 +00002164/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2165/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2166/// instructions.
2167unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002168 unsigned NumOperands = N->getNumOperands();
2169 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2170 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002171 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002172 unsigned Val = 0;
2173 SDOperand Arg = N->getOperand(NumOperands-i-1);
2174 if (Arg.getOpcode() != ISD::UNDEF)
2175 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002176 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002177 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002178 if (i != NumOperands - 1)
2179 Mask <<= Shift;
2180 }
Evan Cheng63d33002006-03-22 08:01:21 +00002181
2182 return Mask;
2183}
2184
Evan Cheng506d3df2006-03-29 23:07:14 +00002185/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2186/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2187/// instructions.
2188unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2189 unsigned Mask = 0;
2190 // 8 nodes, but we only care about the last 4.
2191 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002192 unsigned Val = 0;
2193 SDOperand Arg = N->getOperand(i);
2194 if (Arg.getOpcode() != ISD::UNDEF)
2195 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002196 Mask |= (Val - 4);
2197 if (i != 4)
2198 Mask <<= 2;
2199 }
2200
2201 return Mask;
2202}
2203
2204/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2205/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2206/// instructions.
2207unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2208 unsigned Mask = 0;
2209 // 8 nodes, but we only care about the first 4.
2210 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002211 unsigned Val = 0;
2212 SDOperand Arg = N->getOperand(i);
2213 if (Arg.getOpcode() != ISD::UNDEF)
2214 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002215 Mask |= Val;
2216 if (i != 0)
2217 Mask <<= 2;
2218 }
2219
2220 return Mask;
2221}
2222
Evan Chengc21a0532006-04-05 01:47:37 +00002223/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2224/// specifies a 8 element shuffle that can be broken into a pair of
2225/// PSHUFHW and PSHUFLW.
2226static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2227 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2228
2229 if (N->getNumOperands() != 8)
2230 return false;
2231
2232 // Lower quadword shuffled.
2233 for (unsigned i = 0; i != 4; ++i) {
2234 SDOperand Arg = N->getOperand(i);
2235 if (Arg.getOpcode() == ISD::UNDEF) continue;
2236 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2237 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2238 if (Val > 4)
2239 return false;
2240 }
2241
2242 // Upper quadword shuffled.
2243 for (unsigned i = 4; i != 8; ++i) {
2244 SDOperand Arg = N->getOperand(i);
2245 if (Arg.getOpcode() == ISD::UNDEF) continue;
2246 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2247 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2248 if (Val < 4 || Val > 7)
2249 return false;
2250 }
2251
2252 return true;
2253}
2254
Evan Cheng5ced1d82006-04-06 23:23:56 +00002255/// CommuteVectorShuffle - Swap vector_shuffle operandsas well as
2256/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002257static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2258 SDOperand &V2, SDOperand &Mask,
2259 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002260 MVT::ValueType VT = Op.getValueType();
2261 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002262 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002263 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002264 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002265
2266 for (unsigned i = 0; i != NumElems; ++i) {
2267 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002268 if (Arg.getOpcode() == ISD::UNDEF) {
2269 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2270 continue;
2271 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002272 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2273 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2274 if (Val < NumElems)
2275 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2276 else
2277 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2278 }
2279
Evan Cheng9eca5e82006-10-25 21:49:50 +00002280 std::swap(V1, V2);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002281 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng9eca5e82006-10-25 21:49:50 +00002282 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002283}
2284
Evan Cheng533a0aa2006-04-19 20:35:22 +00002285/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2286/// match movhlps. The lower half elements should come from upper half of
2287/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002288/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002289static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2290 unsigned NumElems = Mask->getNumOperands();
2291 if (NumElems != 4)
2292 return false;
2293 for (unsigned i = 0, e = 2; i != e; ++i)
2294 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2295 return false;
2296 for (unsigned i = 2; i != 4; ++i)
2297 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2298 return false;
2299 return true;
2300}
2301
Evan Cheng5ced1d82006-04-06 23:23:56 +00002302/// isScalarLoadToVector - Returns true if the node is a scalar load that
2303/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002304static inline bool isScalarLoadToVector(SDNode *N) {
2305 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2306 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002307 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002308 }
2309 return false;
2310}
2311
Evan Cheng533a0aa2006-04-19 20:35:22 +00002312/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2313/// match movlp{s|d}. The lower half elements should come from lower half of
2314/// V1 (and in order), and the upper half elements should come from the upper
2315/// half of V2 (and in order). And since V1 will become the source of the
2316/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002317static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002318 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002319 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002320 // Is V2 is a vector load, don't do this transformation. We will try to use
2321 // load folding shufps op.
2322 if (ISD::isNON_EXTLoad(V2))
2323 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002324
Evan Cheng533a0aa2006-04-19 20:35:22 +00002325 unsigned NumElems = Mask->getNumOperands();
2326 if (NumElems != 2 && NumElems != 4)
2327 return false;
2328 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2329 if (!isUndefOrEqual(Mask->getOperand(i), i))
2330 return false;
2331 for (unsigned i = NumElems/2; i != NumElems; ++i)
2332 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2333 return false;
2334 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002335}
2336
Evan Cheng39623da2006-04-20 08:58:49 +00002337/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2338/// all the same.
2339static bool isSplatVector(SDNode *N) {
2340 if (N->getOpcode() != ISD::BUILD_VECTOR)
2341 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002342
Evan Cheng39623da2006-04-20 08:58:49 +00002343 SDOperand SplatValue = N->getOperand(0);
2344 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2345 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002346 return false;
2347 return true;
2348}
2349
Evan Cheng8cf723d2006-09-08 01:50:06 +00002350/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2351/// to an undef.
2352static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002353 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002354 return false;
2355
2356 SDOperand V1 = N->getOperand(0);
2357 SDOperand V2 = N->getOperand(1);
2358 SDOperand Mask = N->getOperand(2);
2359 unsigned NumElems = Mask.getNumOperands();
2360 for (unsigned i = 0; i != NumElems; ++i) {
2361 SDOperand Arg = Mask.getOperand(i);
2362 if (Arg.getOpcode() != ISD::UNDEF) {
2363 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2364 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2365 return false;
2366 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2367 return false;
2368 }
2369 }
2370 return true;
2371}
2372
Evan Cheng213d2cf2007-05-17 18:45:50 +00002373/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2374/// constant +0.0.
2375static inline bool isZeroNode(SDOperand Elt) {
2376 return ((isa<ConstantSDNode>(Elt) &&
2377 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2378 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002379 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002380}
2381
2382/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2383/// to an zero vector.
2384static bool isZeroShuffle(SDNode *N) {
2385 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2386 return false;
2387
2388 SDOperand V1 = N->getOperand(0);
2389 SDOperand V2 = N->getOperand(1);
2390 SDOperand Mask = N->getOperand(2);
2391 unsigned NumElems = Mask.getNumOperands();
2392 for (unsigned i = 0; i != NumElems; ++i) {
2393 SDOperand Arg = Mask.getOperand(i);
2394 if (Arg.getOpcode() != ISD::UNDEF) {
2395 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2396 if (Idx < NumElems) {
2397 unsigned Opc = V1.Val->getOpcode();
2398 if (Opc == ISD::UNDEF)
2399 continue;
2400 if (Opc != ISD::BUILD_VECTOR ||
2401 !isZeroNode(V1.Val->getOperand(Idx)))
2402 return false;
2403 } else if (Idx >= NumElems) {
2404 unsigned Opc = V2.Val->getOpcode();
2405 if (Opc == ISD::UNDEF)
2406 continue;
2407 if (Opc != ISD::BUILD_VECTOR ||
2408 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2409 return false;
2410 }
2411 }
2412 }
2413 return true;
2414}
2415
2416/// getZeroVector - Returns a vector of specified type with all zero elements.
2417///
2418static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2419 assert(MVT::isVector(VT) && "Expected a vector type");
Dan Gohman237898a2007-05-24 14:33:05 +00002420 unsigned NumElems = MVT::getVectorNumElements(VT);
Dan Gohman51eaa862007-06-14 22:58:02 +00002421 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002422 bool isFP = MVT::isFloatingPoint(EVT);
2423 SDOperand Zero = isFP ? DAG.getConstantFP(0.0, EVT) : DAG.getConstant(0, EVT);
2424 SmallVector<SDOperand, 8> ZeroVec(NumElems, Zero);
2425 return DAG.getNode(ISD::BUILD_VECTOR, VT, &ZeroVec[0], ZeroVec.size());
2426}
2427
Evan Cheng39623da2006-04-20 08:58:49 +00002428/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2429/// that point to V2 points to its first element.
2430static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2431 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2432
2433 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002434 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002435 unsigned NumElems = Mask.getNumOperands();
2436 for (unsigned i = 0; i != NumElems; ++i) {
2437 SDOperand Arg = Mask.getOperand(i);
2438 if (Arg.getOpcode() != ISD::UNDEF) {
2439 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2440 if (Val > NumElems) {
2441 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2442 Changed = true;
2443 }
2444 }
2445 MaskVec.push_back(Arg);
2446 }
2447
2448 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002449 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2450 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002451 return Mask;
2452}
2453
Evan Cheng017dcc62006-04-21 01:05:10 +00002454/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2455/// operation of specified width.
2456static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002457 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002458 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002459
Chris Lattner5a88b832007-02-25 07:10:00 +00002460 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002461 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2462 for (unsigned i = 1; i != NumElems; ++i)
2463 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002464 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002465}
2466
Evan Chengc575ca22006-04-17 20:43:08 +00002467/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2468/// of specified width.
2469static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2470 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002471 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002472 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002473 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2474 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2475 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2476 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002477 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002478}
2479
Evan Cheng39623da2006-04-20 08:58:49 +00002480/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2481/// of specified width.
2482static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2483 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002484 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002485 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002486 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002487 for (unsigned i = 0; i != Half; ++i) {
2488 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2489 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2490 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002491 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002492}
2493
Evan Chengc575ca22006-04-17 20:43:08 +00002494/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2495///
2496static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2497 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002498 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002499 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002500 unsigned NumElems = Mask.getNumOperands();
2501 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002502 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002503 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002504 NumElems >>= 1;
2505 }
2506 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2507
2508 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Evan Cheng017dcc62006-04-21 01:05:10 +00002509 Mask = getZeroVector(MaskVT, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002510 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002511 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002512 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2513}
2514
Evan Chengba05f722006-04-21 23:03:30 +00002515/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Evan Cheng213d2cf2007-05-17 18:45:50 +00002516/// vector of zero or undef vector.
Evan Chengba05f722006-04-21 23:03:30 +00002517static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002518 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002519 bool isZero, SelectionDAG &DAG) {
2520 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002521 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002522 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002523 SDOperand Zero = DAG.getConstant(0, EVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002524 SmallVector<SDOperand, 8> MaskVec(NumElems, Zero);
Evan Cheng017dcc62006-04-21 01:05:10 +00002525 MaskVec[Idx] = DAG.getConstant(NumElems, EVT);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002526 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2527 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002528 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002529}
2530
Evan Chengc78d3b42006-04-24 18:01:45 +00002531/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2532///
2533static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2534 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002535 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002536 if (NumNonZero > 8)
2537 return SDOperand();
2538
2539 SDOperand V(0, 0);
2540 bool First = true;
2541 for (unsigned i = 0; i < 16; ++i) {
2542 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2543 if (ThisIsNonZero && First) {
2544 if (NumZero)
2545 V = getZeroVector(MVT::v8i16, DAG);
2546 else
2547 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2548 First = false;
2549 }
2550
2551 if ((i & 1) != 0) {
2552 SDOperand ThisElt(0, 0), LastElt(0, 0);
2553 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2554 if (LastIsNonZero) {
2555 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2556 }
2557 if (ThisIsNonZero) {
2558 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2559 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2560 ThisElt, DAG.getConstant(8, MVT::i8));
2561 if (LastIsNonZero)
2562 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2563 } else
2564 ThisElt = LastElt;
2565
2566 if (ThisElt.Val)
2567 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Evan Cheng25ab6902006-09-08 06:48:29 +00002568 DAG.getConstant(i/2, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002569 }
2570 }
2571
2572 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2573}
2574
Bill Wendlinga348c562007-03-22 18:42:45 +00002575/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002576///
2577static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2578 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002579 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002580 if (NumNonZero > 4)
2581 return SDOperand();
2582
2583 SDOperand V(0, 0);
2584 bool First = true;
2585 for (unsigned i = 0; i < 8; ++i) {
2586 bool isNonZero = (NonZeros & (1 << i)) != 0;
2587 if (isNonZero) {
2588 if (First) {
2589 if (NumZero)
2590 V = getZeroVector(MVT::v8i16, DAG);
2591 else
2592 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2593 First = false;
2594 }
2595 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Evan Cheng25ab6902006-09-08 06:48:29 +00002596 DAG.getConstant(i, TLI.getPointerTy()));
Evan Chengc78d3b42006-04-24 18:01:45 +00002597 }
2598 }
2599
2600 return V;
2601}
2602
Evan Cheng0db9fe62006-04-25 20:13:52 +00002603SDOperand
2604X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
2605 // All zero's are handled with pxor.
2606 if (ISD::isBuildVectorAllZeros(Op.Val))
2607 return Op;
2608
2609 // All one's are handled with pcmpeqd.
2610 if (ISD::isBuildVectorAllOnes(Op.Val))
2611 return Op;
2612
2613 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002614 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002615 unsigned EVTBits = MVT::getSizeInBits(EVT);
2616
2617 unsigned NumElems = Op.getNumOperands();
2618 unsigned NumZero = 0;
2619 unsigned NumNonZero = 0;
2620 unsigned NonZeros = 0;
Dan Gohmana3941172007-07-24 22:55:08 +00002621 unsigned NumNonZeroImms = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002622 std::set<SDOperand> Values;
2623 for (unsigned i = 0; i < NumElems; ++i) {
2624 SDOperand Elt = Op.getOperand(i);
2625 if (Elt.getOpcode() != ISD::UNDEF) {
2626 Values.insert(Elt);
2627 if (isZeroNode(Elt))
2628 NumZero++;
2629 else {
2630 NonZeros |= (1 << i);
2631 NumNonZero++;
Dan Gohmana3941172007-07-24 22:55:08 +00002632 if (Elt.getOpcode() == ISD::Constant ||
2633 Elt.getOpcode() == ISD::ConstantFP)
2634 NumNonZeroImms++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002635 }
2636 }
2637 }
2638
Dan Gohman7f321562007-06-25 16:23:39 +00002639 if (NumNonZero == 0) {
2640 if (NumZero == 0)
2641 // All undef vector. Return an UNDEF.
2642 return DAG.getNode(ISD::UNDEF, VT);
2643 else
2644 // A mix of zero and undef. Return a zero vector.
2645 return getZeroVector(VT, DAG);
2646 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002647
2648 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2649 if (Values.size() == 1)
2650 return SDOperand();
2651
2652 // Special case for single non-zero element.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002653 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002654 unsigned Idx = CountTrailingZeros_32(NonZeros);
2655 SDOperand Item = Op.getOperand(Idx);
2656 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2657 if (Idx == 0)
2658 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2659 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2660 NumZero > 0, DAG);
2661
2662 if (EVTBits == 32) {
2663 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2664 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2665 DAG);
2666 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002667 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002668 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002669 for (unsigned i = 0; i < NumElems; i++)
2670 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002671 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2672 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002673 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2674 DAG.getNode(ISD::UNDEF, VT), Mask);
2675 }
2676 }
2677
Dan Gohmana3941172007-07-24 22:55:08 +00002678 // A vector full of immediates; various special cases are already
2679 // handled, so this is best done with a single constant-pool load.
2680 if (NumNonZero == NumNonZeroImms)
2681 return SDOperand();
2682
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002683 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002684 if (EVTBits == 64)
2685 return SDOperand();
2686
2687 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00002688 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002689 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2690 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002691 if (V.Val) return V;
2692 }
2693
Bill Wendling826f36f2007-03-28 00:57:11 +00002694 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002695 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2696 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002697 if (V.Val) return V;
2698 }
2699
2700 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00002701 SmallVector<SDOperand, 8> V;
2702 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002703 if (NumElems == 4 && NumZero > 0) {
2704 for (unsigned i = 0; i < 4; ++i) {
2705 bool isZero = !(NonZeros & (1 << i));
2706 if (isZero)
2707 V[i] = getZeroVector(VT, DAG);
2708 else
2709 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2710 }
2711
2712 for (unsigned i = 0; i < 2; ++i) {
2713 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
2714 default: break;
2715 case 0:
2716 V[i] = V[i*2]; // Must be a zero vector.
2717 break;
2718 case 1:
2719 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
2720 getMOVLMask(NumElems, DAG));
2721 break;
2722 case 2:
2723 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2724 getMOVLMask(NumElems, DAG));
2725 break;
2726 case 3:
2727 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
2728 getUnpacklMask(NumElems, DAG));
2729 break;
2730 }
2731 }
2732
Evan Cheng069287d2006-05-16 07:21:53 +00002733 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002734 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002735 // FIXME: we can do the same for v4f32 case when we know both parts of
2736 // the lower half come from scalar_to_vector (loadf32). We should do
2737 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002738 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00002739 return V[0];
2740 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002741 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002742 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002743 bool Reverse = (NonZeros & 0x3) == 2;
2744 for (unsigned i = 0; i < 2; ++i)
2745 if (Reverse)
2746 MaskVec.push_back(DAG.getConstant(1-i, EVT));
2747 else
2748 MaskVec.push_back(DAG.getConstant(i, EVT));
2749 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
2750 for (unsigned i = 0; i < 2; ++i)
2751 if (Reverse)
2752 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
2753 else
2754 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002755 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2756 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002757 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
2758 }
2759
2760 if (Values.size() > 2) {
2761 // Expand into a number of unpckl*.
2762 // e.g. for v4f32
2763 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
2764 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
2765 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
2766 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
2767 for (unsigned i = 0; i < NumElems; ++i)
2768 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2769 NumElems >>= 1;
2770 while (NumElems != 0) {
2771 for (unsigned i = 0; i < NumElems; ++i)
2772 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
2773 UnpckMask);
2774 NumElems >>= 1;
2775 }
2776 return V[0];
2777 }
2778
2779 return SDOperand();
2780}
2781
2782SDOperand
2783X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
2784 SDOperand V1 = Op.getOperand(0);
2785 SDOperand V2 = Op.getOperand(1);
2786 SDOperand PermMask = Op.getOperand(2);
2787 MVT::ValueType VT = Op.getValueType();
2788 unsigned NumElems = PermMask.getNumOperands();
2789 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
2790 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00002791 bool V1IsSplat = false;
2792 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002793
Evan Cheng8cf723d2006-09-08 01:50:06 +00002794 if (isUndefShuffle(Op.Val))
2795 return DAG.getNode(ISD::UNDEF, VT);
2796
Evan Cheng213d2cf2007-05-17 18:45:50 +00002797 if (isZeroShuffle(Op.Val))
2798 return getZeroVector(VT, DAG);
2799
Evan Cheng49892af2007-06-19 00:02:56 +00002800 if (isIdentityMask(PermMask.Val))
2801 return V1;
2802 else if (isIdentityMask(PermMask.Val, true))
2803 return V2;
2804
Evan Cheng0db9fe62006-04-25 20:13:52 +00002805 if (isSplatMask(PermMask.Val)) {
2806 if (NumElems <= 4) return Op;
2807 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00002808 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002809 }
2810
Evan Cheng9bbbb982006-10-25 20:48:19 +00002811 if (X86::isMOVLMask(PermMask.Val))
2812 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002813
Evan Cheng9bbbb982006-10-25 20:48:19 +00002814 if (X86::isMOVSHDUPMask(PermMask.Val) ||
2815 X86::isMOVSLDUPMask(PermMask.Val) ||
2816 X86::isMOVHLPSMask(PermMask.Val) ||
2817 X86::isMOVHPMask(PermMask.Val) ||
2818 X86::isMOVLPMask(PermMask.Val))
2819 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002820
Evan Cheng9bbbb982006-10-25 20:48:19 +00002821 if (ShouldXformToMOVHLPS(PermMask.Val) ||
2822 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00002823 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002824
Evan Cheng9eca5e82006-10-25 21:49:50 +00002825 bool Commuted = false;
Evan Cheng9bbbb982006-10-25 20:48:19 +00002826 V1IsSplat = isSplatVector(V1.Val);
2827 V2IsSplat = isSplatVector(V2.Val);
2828 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00002829 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00002830 std::swap(V1IsSplat, V2IsSplat);
2831 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002832 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00002833 }
2834
2835 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
2836 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00002837 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00002838 if (V2IsSplat) {
2839 // V2 is a splat, so the mask may be malformed. That is, it may point
2840 // to any V2 element. The instruction selectior won't like this. Get
2841 // a corrected mask and commute to form a proper MOVS{S|D}.
2842 SDOperand NewMask = getMOVLMask(NumElems, DAG);
2843 if (NewMask.Val != PermMask.Val)
2844 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002845 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00002846 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00002847 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002848
Evan Chengd9b8e402006-10-16 06:36:00 +00002849 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002850 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00002851 X86::isUNPCKLMask(PermMask.Val) ||
2852 X86::isUNPCKHMask(PermMask.Val))
2853 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00002854
Evan Cheng9bbbb982006-10-25 20:48:19 +00002855 if (V2IsSplat) {
2856 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002857 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00002858 // new vector_shuffle with the corrected mask.
2859 SDOperand NewMask = NormalizeMask(PermMask, DAG);
2860 if (NewMask.Val != PermMask.Val) {
2861 if (X86::isUNPCKLMask(PermMask.Val, true)) {
2862 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
2863 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
2864 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
2865 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
2866 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002867 }
2868 }
2869 }
2870
2871 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00002872 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
2873 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2874
2875 if (Commuted) {
2876 // Commute is back and try unpck* again.
2877 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
2878 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002879 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00002880 X86::isUNPCKLMask(PermMask.Val) ||
2881 X86::isUNPCKHMask(PermMask.Val))
2882 return Op;
2883 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002884
2885 // If VT is integer, try PSHUF* first, then SHUFP*.
2886 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002887 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
2888 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
2889 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
2890 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00002891 X86::isPSHUFHWMask(PermMask.Val) ||
2892 X86::isPSHUFLWMask(PermMask.Val)) {
2893 if (V2.getOpcode() != ISD::UNDEF)
2894 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2895 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2896 return Op;
2897 }
2898
Chris Lattner07c70cd2007-05-17 17:13:13 +00002899 if (X86::isSHUFPMask(PermMask.Val) &&
2900 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002901 return Op;
2902
2903 // Handle v8i16 shuffle high / low shuffle node pair.
2904 if (VT == MVT::v8i16 && isPSHUFHW_PSHUFLWMask(PermMask.Val)) {
2905 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002906 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002907 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002908 for (unsigned i = 0; i != 4; ++i)
2909 MaskVec.push_back(PermMask.getOperand(i));
2910 for (unsigned i = 4; i != 8; ++i)
2911 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnere2199452006-08-11 17:38:39 +00002912 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2913 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002914 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2915 MaskVec.clear();
2916 for (unsigned i = 0; i != 4; ++i)
2917 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2918 for (unsigned i = 4; i != 8; ++i)
2919 MaskVec.push_back(PermMask.getOperand(i));
Chris Lattnere2199452006-08-11 17:38:39 +00002920 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0],MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002921 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
2922 }
2923 } else {
2924 // Floating point cases in the other order.
2925 if (X86::isSHUFPMask(PermMask.Val))
2926 return Op;
2927 if (X86::isPSHUFDMask(PermMask.Val) ||
2928 X86::isPSHUFHWMask(PermMask.Val) ||
2929 X86::isPSHUFLWMask(PermMask.Val)) {
2930 if (V2.getOpcode() != ISD::UNDEF)
2931 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
2932 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
2933 return Op;
2934 }
2935 }
2936
Chris Lattner07c70cd2007-05-17 17:13:13 +00002937 if (NumElems == 4 &&
2938 // Don't do this for MMX.
2939 MVT::getSizeInBits(VT) != 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002940 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002941 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002942 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00002943 Locs.reserve(NumElems);
Chris Lattner5a88b832007-02-25 07:10:00 +00002944 SmallVector<SDOperand, 8> Mask1(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2945 SmallVector<SDOperand, 8> Mask2(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002946 unsigned NumHi = 0;
2947 unsigned NumLo = 0;
2948 // If no more than two elements come from either vector. This can be
2949 // implemented with two shuffles. First shuffle gather the elements.
2950 // The second shuffle, which takes the first shuffle as both of its
2951 // vector operands, put the elements into the right order.
2952 for (unsigned i = 0; i != NumElems; ++i) {
2953 SDOperand Elt = PermMask.getOperand(i);
2954 if (Elt.getOpcode() == ISD::UNDEF) {
2955 Locs[i] = std::make_pair(-1, -1);
2956 } else {
2957 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
2958 if (Val < NumElems) {
2959 Locs[i] = std::make_pair(0, NumLo);
2960 Mask1[NumLo] = Elt;
2961 NumLo++;
2962 } else {
2963 Locs[i] = std::make_pair(1, NumHi);
2964 if (2+NumHi < NumElems)
2965 Mask1[2+NumHi] = Elt;
2966 NumHi++;
2967 }
2968 }
2969 }
2970 if (NumLo <= 2 && NumHi <= 2) {
2971 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00002972 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2973 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002974 for (unsigned i = 0; i != NumElems; ++i) {
2975 if (Locs[i].first == -1)
2976 continue;
2977 else {
2978 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
2979 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
2980 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
2981 }
2982 }
2983
2984 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00002985 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2986 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00002987 }
2988
2989 // Break it into (shuffle shuffle_hi, shuffle_lo).
2990 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00002991 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2992 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
2993 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002994 unsigned MaskIdx = 0;
2995 unsigned LoIdx = 0;
2996 unsigned HiIdx = NumElems/2;
2997 for (unsigned i = 0; i != NumElems; ++i) {
2998 if (i == NumElems/2) {
2999 MaskPtr = &HiMask;
3000 MaskIdx = 1;
3001 LoIdx = 0;
3002 HiIdx = NumElems/2;
3003 }
3004 SDOperand Elt = PermMask.getOperand(i);
3005 if (Elt.getOpcode() == ISD::UNDEF) {
3006 Locs[i] = std::make_pair(-1, -1);
3007 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3008 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3009 (*MaskPtr)[LoIdx] = Elt;
3010 LoIdx++;
3011 } else {
3012 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3013 (*MaskPtr)[HiIdx] = Elt;
3014 HiIdx++;
3015 }
3016 }
3017
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003018 SDOperand LoShuffle =
3019 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003020 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3021 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003022 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003023 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003024 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3025 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003026 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003027 for (unsigned i = 0; i != NumElems; ++i) {
3028 if (Locs[i].first == -1) {
3029 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3030 } else {
3031 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3032 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3033 }
3034 }
3035 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003036 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3037 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003038 }
3039
3040 return SDOperand();
3041}
3042
3043SDOperand
3044X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3045 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3046 return SDOperand();
3047
3048 MVT::ValueType VT = Op.getValueType();
3049 // TODO: handle v16i8.
3050 if (MVT::getSizeInBits(VT) == 16) {
3051 // Transform it so it match pextrw which produces a 32-bit result.
3052 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3053 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3054 Op.getOperand(0), Op.getOperand(1));
3055 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3056 DAG.getValueType(VT));
3057 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3058 } else if (MVT::getSizeInBits(VT) == 32) {
3059 SDOperand Vec = Op.getOperand(0);
3060 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3061 if (Idx == 0)
3062 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003063 // SHUFPS the element to the lowest double word, then movss.
3064 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003065 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003066 IdxVec.push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3067 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3068 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3069 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003070 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3071 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003072 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003073 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003074 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003075 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003076 } else if (MVT::getSizeInBits(VT) == 64) {
3077 SDOperand Vec = Op.getOperand(0);
3078 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3079 if (Idx == 0)
3080 return Op;
3081
3082 // UNPCKHPD the element to the lowest double word, then movsd.
3083 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3084 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3085 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003086 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003087 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
3088 IdxVec.push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003089 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3090 &IdxVec[0], IdxVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003091 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3092 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3093 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Evan Cheng015188f2006-06-15 08:14:54 +00003094 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003095 }
3096
3097 return SDOperand();
3098}
3099
3100SDOperand
3101X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng069287d2006-05-16 07:21:53 +00003102 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
Evan Cheng0db9fe62006-04-25 20:13:52 +00003103 // as its second argument.
3104 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003105 MVT::ValueType BaseVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003106 SDOperand N0 = Op.getOperand(0);
3107 SDOperand N1 = Op.getOperand(1);
3108 SDOperand N2 = Op.getOperand(2);
3109 if (MVT::getSizeInBits(BaseVT) == 16) {
3110 if (N1.getValueType() != MVT::i32)
3111 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3112 if (N2.getValueType() != MVT::i32)
Evan Cheng0db58622007-06-29 00:01:20 +00003113 N2 = DAG.getConstant(cast<ConstantSDNode>(N2)->getValue(),getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003114 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
3115 } else if (MVT::getSizeInBits(BaseVT) == 32) {
3116 unsigned Idx = cast<ConstantSDNode>(N2)->getValue();
3117 if (Idx == 0) {
3118 // Use a movss.
3119 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, N1);
3120 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman51eaa862007-06-14 22:58:02 +00003121 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003122 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003123 MaskVec.push_back(DAG.getConstant(4, BaseVT));
3124 for (unsigned i = 1; i <= 3; ++i)
3125 MaskVec.push_back(DAG.getConstant(i, BaseVT));
3126 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, N0, N1,
Chris Lattnere2199452006-08-11 17:38:39 +00003127 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3128 &MaskVec[0], MaskVec.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003129 } else {
3130 // Use two pinsrw instructions to insert a 32 bit value.
3131 Idx <<= 1;
3132 if (MVT::isFloatingPoint(N1.getValueType())) {
Evan Cheng4ebcc8c2007-07-31 06:21:44 +00003133 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4f32, N1);
3134 N1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, N1);
3135 N1 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32, N1,
3136 DAG.getConstant(0, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003137 }
3138 N0 = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, N0);
3139 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003140 DAG.getConstant(Idx, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003141 N1 = DAG.getNode(ISD::SRL, MVT::i32, N1, DAG.getConstant(16, MVT::i8));
3142 N0 = DAG.getNode(X86ISD::PINSRW, MVT::v8i16, N0, N1,
Evan Cheng015188f2006-06-15 08:14:54 +00003143 DAG.getConstant(Idx+1, getPointerTy()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003144 return DAG.getNode(ISD::BIT_CONVERT, VT, N0);
3145 }
3146 }
3147
3148 return SDOperand();
3149}
3150
3151SDOperand
3152X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3153 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3154 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3155}
3156
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003157// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003158// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3159// one of the above mentioned nodes. It has to be wrapped because otherwise
3160// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3161// be used to form addressing mode. These wrapped nodes will be selected
3162// into MOV32ri.
3163SDOperand
3164X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3165 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003166 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3167 getPointerTy(),
3168 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003169 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003170 // With PIC, the address is actually $g + Offset.
3171 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3172 !Subtarget->isPICStyleRIPRel()) {
3173 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3174 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3175 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003176 }
3177
3178 return Result;
3179}
3180
3181SDOperand
3182X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3183 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003184 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003185 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003186 // With PIC, the address is actually $g + Offset.
3187 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3188 !Subtarget->isPICStyleRIPRel()) {
3189 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3190 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3191 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003192 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003193
3194 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3195 // load the value at address GV, not the value of GV itself. This means that
3196 // the GlobalAddress must be in the base or index register of the address, not
3197 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003198 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003199 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3200 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003201
3202 return Result;
3203}
3204
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003205// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3206static SDOperand
3207LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3208 const MVT::ValueType PtrVT) {
3209 SDOperand InFlag;
3210 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3211 DAG.getNode(X86ISD::GlobalBaseReg,
3212 PtrVT), InFlag);
3213 InFlag = Chain.getValue(1);
3214
3215 // emit leal symbol@TLSGD(,%ebx,1), %eax
3216 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3217 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3218 GA->getValueType(0),
3219 GA->getOffset());
3220 SDOperand Ops[] = { Chain, TGA, InFlag };
3221 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3222 InFlag = Result.getValue(2);
3223 Chain = Result.getValue(1);
3224
3225 // call ___tls_get_addr. This function receives its argument in
3226 // the register EAX.
3227 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3228 InFlag = Chain.getValue(1);
3229
3230 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3231 SDOperand Ops1[] = { Chain,
3232 DAG.getTargetExternalSymbol("___tls_get_addr",
3233 PtrVT),
3234 DAG.getRegister(X86::EAX, PtrVT),
3235 DAG.getRegister(X86::EBX, PtrVT),
3236 InFlag };
3237 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3238 InFlag = Chain.getValue(1);
3239
3240 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3241}
3242
3243// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3244// "local exec" model.
3245static SDOperand
3246LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3247 const MVT::ValueType PtrVT) {
3248 // Get the Thread Pointer
3249 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3250 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3251 // exec)
3252 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3253 GA->getValueType(0),
3254 GA->getOffset());
3255 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003256
3257 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3258 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3259
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003260 // The address of the thread local variable is the add of the thread
3261 // pointer with the offset of the variable.
3262 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3263}
3264
3265SDOperand
3266X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3267 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003268 // TODO: implement the "initial exec"model for pic executables
3269 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3270 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003271 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3272 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3273 // otherwise use the "Local Exec"TLS Model
3274 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3275 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3276 else
3277 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3278}
3279
Evan Cheng0db9fe62006-04-25 20:13:52 +00003280SDOperand
3281X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3282 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003283 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003284 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003285 // With PIC, the address is actually $g + Offset.
3286 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3287 !Subtarget->isPICStyleRIPRel()) {
3288 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3289 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3290 Result);
3291 }
3292
3293 return Result;
3294}
3295
3296SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3297 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3298 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3299 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3300 // With PIC, the address is actually $g + Offset.
3301 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3302 !Subtarget->isPICStyleRIPRel()) {
3303 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3304 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3305 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003306 }
3307
3308 return Result;
3309}
3310
3311SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge3413162006-01-09 18:33:28 +00003312 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3313 "Not an i64 shift!");
3314 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3315 SDOperand ShOpLo = Op.getOperand(0);
3316 SDOperand ShOpHi = Op.getOperand(1);
3317 SDOperand ShAmt = Op.getOperand(2);
Evan Cheng734503b2006-09-11 02:19:56 +00003318 SDOperand Tmp1 = isSRA ?
3319 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3320 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003321
3322 SDOperand Tmp2, Tmp3;
3323 if (Op.getOpcode() == ISD::SHL_PARTS) {
3324 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3325 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3326 } else {
3327 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
Evan Chengb7b57062006-01-19 01:46:14 +00003328 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
Evan Chenge3413162006-01-09 18:33:28 +00003329 }
3330
Evan Cheng734503b2006-09-11 02:19:56 +00003331 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3332 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3333 DAG.getConstant(32, MVT::i8));
3334 SDOperand COps[]={DAG.getEntryNode(), AndNode, DAG.getConstant(0, MVT::i8)};
Evan Cheng0488db92007-09-25 01:57:46 +00003335 SDOperand Cond = NewCCModeling
3336 ? DAG.getNode(X86ISD::CMP_NEW, MVT::i32,
3337 AndNode, DAG.getConstant(0, MVT::i8))
3338 : DAG.getNode(X86ISD::CMP, VTs, 2, COps, 3).getValue(1);
Evan Chenge3413162006-01-09 18:33:28 +00003339
3340 SDOperand Hi, Lo;
Chris Lattner7fbe9722006-10-20 17:42:20 +00003341 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng0488db92007-09-25 01:57:46 +00003342 unsigned Opc = NewCCModeling ? X86ISD::CMOV_NEW : X86ISD::CMOV;
Evan Cheng734503b2006-09-11 02:19:56 +00003343 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3344 SmallVector<SDOperand, 4> Ops;
Evan Chenge3413162006-01-09 18:33:28 +00003345 if (Op.getOpcode() == ISD::SHL_PARTS) {
3346 Ops.push_back(Tmp2);
3347 Ops.push_back(Tmp3);
3348 Ops.push_back(CC);
Evan Cheng0488db92007-09-25 01:57:46 +00003349 Ops.push_back(Cond);
3350 if (NewCCModeling)
3351 Hi = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3352 else {
3353 Hi = DAG.getNode(Opc, VTs, 2, &Ops[0], Ops.size());
3354 Cond = Hi.getValue(1);
3355 }
Evan Chenge3413162006-01-09 18:33:28 +00003356
3357 Ops.clear();
3358 Ops.push_back(Tmp3);
3359 Ops.push_back(Tmp1);
3360 Ops.push_back(CC);
Evan Cheng0488db92007-09-25 01:57:46 +00003361 Ops.push_back(Cond);
3362 if (NewCCModeling)
3363 Lo = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3364 else
3365 Lo = DAG.getNode(Opc, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003366 } else {
3367 Ops.push_back(Tmp2);
3368 Ops.push_back(Tmp3);
3369 Ops.push_back(CC);
Evan Cheng0488db92007-09-25 01:57:46 +00003370 Ops.push_back(Cond);
3371 if (NewCCModeling)
3372 Lo = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3373 else {
3374 Lo = DAG.getNode(Opc, VTs, 2, &Ops[0], Ops.size());
3375 Cond = Lo.getValue(1);
3376 }
Evan Chenge3413162006-01-09 18:33:28 +00003377
3378 Ops.clear();
3379 Ops.push_back(Tmp3);
3380 Ops.push_back(Tmp1);
3381 Ops.push_back(CC);
Evan Cheng0488db92007-09-25 01:57:46 +00003382 Ops.push_back(Cond);
3383 if (NewCCModeling)
3384 Hi = DAG.getNode(Opc, MVT::i32, &Ops[0], Ops.size());
3385 else
3386 Hi = DAG.getNode(Opc, VTs, 2, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003387 }
3388
Evan Cheng734503b2006-09-11 02:19:56 +00003389 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003390 Ops.clear();
3391 Ops.push_back(Lo);
3392 Ops.push_back(Hi);
Evan Cheng734503b2006-09-11 02:19:56 +00003393 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003394}
Evan Chenga3195e82006-01-12 22:54:21 +00003395
Evan Cheng0db9fe62006-04-25 20:13:52 +00003396SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3397 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3398 Op.getOperand(0).getValueType() >= MVT::i16 &&
3399 "Unknown SINT_TO_FP to lower!");
3400
3401 SDOperand Result;
3402 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3403 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3404 MachineFunction &MF = DAG.getMachineFunction();
3405 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3406 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003407 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003408 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003409
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003410 // These are really Legal; caller falls through into that case.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003411 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f32 && X86ScalarSSEf32)
3412 return Result;
3413 if (SrcVT==MVT::i32 && Op.getValueType() == MVT::f64 && X86ScalarSSEf64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003414 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00003415 if (SrcVT==MVT::i64 && Op.getValueType() != MVT::f80 &&
3416 Subtarget->is64Bit())
3417 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003418
Evan Cheng0db9fe62006-04-25 20:13:52 +00003419 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00003420 SDVTList Tys;
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003421 bool useSSE = (X86ScalarSSEf32 && Op.getValueType() == MVT::f32) ||
3422 (X86ScalarSSEf64 && Op.getValueType() == MVT::f64);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003423 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00003424 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3425 else
Dale Johannesen849f2142007-07-03 00:53:03 +00003426 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003427 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003428 Ops.push_back(Chain);
3429 Ops.push_back(StackSlot);
3430 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003431 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003432 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003433
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003434 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003435 Chain = Result.getValue(1);
3436 SDOperand InFlag = Result.getValue(2);
3437
3438 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
3439 // shouldn't be necessary except that RFP cannot be live across
3440 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003441 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003442 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003443 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00003444 Tys = DAG.getVTList(MVT::Other);
3445 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00003446 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003447 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003448 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003449 Ops.push_back(DAG.getValueType(Op.getValueType()));
3450 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003451 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00003452 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003453 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003454
Evan Cheng0db9fe62006-04-25 20:13:52 +00003455 return Result;
3456}
3457
3458SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
3459 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
3460 "Unknown FP_TO_SINT to lower!");
3461 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
3462 // stack slot.
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003463 SDOperand Result;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003464 MachineFunction &MF = DAG.getMachineFunction();
3465 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
3466 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3467 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3468
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003469 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003470 if (Op.getValueType() == MVT::i32 &&
3471 X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32)
3472 return Result;
3473 if (Op.getValueType() == MVT::i32 &&
3474 X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003475 return Result;
Dale Johannesen73328d12007-09-19 23:55:34 +00003476 if (Subtarget->is64Bit() &&
3477 Op.getValueType() == MVT::i64 &&
3478 Op.getOperand(0).getValueType() != MVT::f80)
3479 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003480
Evan Cheng0db9fe62006-04-25 20:13:52 +00003481 unsigned Opc;
3482 switch (Op.getValueType()) {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003483 default: assert(0 && "Invalid FP_TO_SINT to lower!");
3484 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
3485 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
3486 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003487 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003488
Evan Cheng0db9fe62006-04-25 20:13:52 +00003489 SDOperand Chain = DAG.getEntryNode();
3490 SDOperand Value = Op.getOperand(0);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003491 if ((X86ScalarSSEf32 && Op.getOperand(0).getValueType() == MVT::f32) ||
3492 (X86ScalarSSEf64 && Op.getOperand(0).getValueType() == MVT::f64)) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003493 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00003494 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00003495 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003496 SDOperand Ops[] = {
3497 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
3498 };
3499 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003500 Chain = Value.getValue(1);
3501 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
3502 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
3503 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00003504
Evan Cheng0db9fe62006-04-25 20:13:52 +00003505 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00003506 SDOperand Ops[] = { Chain, Value, StackSlot };
3507 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00003508
Evan Cheng0db9fe62006-04-25 20:13:52 +00003509 // Load the result.
Evan Cheng466685d2006-10-09 20:57:25 +00003510 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003511}
3512
3513SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
3514 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00003515 MVT::ValueType EltVT = VT;
3516 if (MVT::isVector(VT))
3517 EltVT = MVT::getVectorElementType(VT);
3518 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003519 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00003520 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003521 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00003522 CV.push_back(C);
3523 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003524 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003525 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00003526 CV.push_back(C);
3527 CV.push_back(C);
3528 CV.push_back(C);
3529 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003530 }
Dan Gohmand3006222007-07-27 17:16:43 +00003531 Constant *C = ConstantVector::get(CV);
3532 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3533 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3534 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003535 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
3536}
3537
3538SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
3539 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00003540 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00003541 unsigned EltNum = 1;
3542 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00003543 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00003544 EltNum = MVT::getVectorNumElements(VT);
3545 }
Dan Gohman20382522007-07-10 00:05:58 +00003546 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003547 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00003548 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003549 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00003550 CV.push_back(C);
3551 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003552 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003553 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00003554 CV.push_back(C);
3555 CV.push_back(C);
3556 CV.push_back(C);
3557 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003558 }
Dan Gohmand3006222007-07-27 17:16:43 +00003559 Constant *C = ConstantVector::get(CV);
3560 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3561 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3562 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00003563 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00003564 return DAG.getNode(ISD::BIT_CONVERT, VT,
3565 DAG.getNode(ISD::XOR, MVT::v2i64,
3566 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
3567 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
3568 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00003569 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
3570 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003571}
3572
Evan Cheng68c47cb2007-01-05 07:55:56 +00003573SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00003574 SDOperand Op0 = Op.getOperand(0);
3575 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003576 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00003577 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00003578 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003579
3580 // If second operand is smaller, extend it first.
3581 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
3582 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
3583 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00003584 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003585 }
3586
Evan Cheng68c47cb2007-01-05 07:55:56 +00003587 // First get the sign bit of second operand.
3588 std::vector<Constant*> CV;
3589 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003590 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
3591 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00003592 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003593 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
3594 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3595 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3596 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00003597 }
Dan Gohmand3006222007-07-27 17:16:43 +00003598 Constant *C = ConstantVector::get(CV);
3599 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3600 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
3601 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003602 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003603
3604 // Shift sign bit right or left if the two operands have different types.
3605 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
3606 // Op0 is MVT::f32, Op1 is MVT::f64.
3607 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
3608 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
3609 DAG.getConstant(32, MVT::i32));
3610 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
3611 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
3612 DAG.getConstant(0, getPointerTy()));
Evan Cheng68c47cb2007-01-05 07:55:56 +00003613 }
3614
Evan Cheng73d6cf12007-01-05 21:37:56 +00003615 // Clear first operand sign bit.
3616 CV.clear();
3617 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003618 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
3619 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00003620 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00003621 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
3622 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3623 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
3624 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00003625 }
Dan Gohmand3006222007-07-27 17:16:43 +00003626 C = ConstantVector::get(CV);
3627 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
3628 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
3629 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00003630 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
3631
3632 // Or the value with the sign bit.
3633 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00003634}
3635
Evan Cheng734503b2006-09-11 02:19:56 +00003636SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG,
3637 SDOperand Chain) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003638 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3639 SDOperand Cond;
Evan Cheng734503b2006-09-11 02:19:56 +00003640 SDOperand Op0 = Op.getOperand(0);
3641 SDOperand Op1 = Op.getOperand(1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003642 SDOperand CC = Op.getOperand(2);
3643 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Evan Chengcf12ec42006-10-12 19:12:56 +00003644 const MVT::ValueType *VTs1 = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3645 const MVT::ValueType *VTs2 = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003646 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003647 unsigned X86CC;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003648
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003649 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Chris Lattnerf9570512006-09-13 03:22:10 +00003650 Op0, Op1, DAG)) {
Evan Cheng734503b2006-09-11 02:19:56 +00003651 SDOperand Ops1[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003652 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, Ops1, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003653 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003654 return DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003655 }
3656
3657 assert(isFP && "Illegal integer SetCC!");
3658
3659 SDOperand COps[] = { Chain, Op0, Op1 };
Evan Chengcf12ec42006-10-12 19:12:56 +00003660 Cond = DAG.getNode(X86ISD::CMP, VTs1, 2, COps, 3).getValue(1);
Evan Cheng734503b2006-09-11 02:19:56 +00003661
3662 switch (SetCCOpcode) {
3663 default: assert(false && "Illegal floating point SetCC!");
3664 case ISD::SETOEQ: { // !PF & ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003665 SDOperand Ops1[] = { DAG.getConstant(X86::COND_NP, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003666 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003667 SDOperand Ops2[] = { DAG.getConstant(X86::COND_E, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003668 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003669 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003670 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3671 }
3672 case ISD::SETUNE: { // PF | !ZF
Chris Lattner7fbe9722006-10-20 17:42:20 +00003673 SDOperand Ops1[] = { DAG.getConstant(X86::COND_P, MVT::i8), Cond };
Evan Chengcf12ec42006-10-12 19:12:56 +00003674 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops1, 2);
Chris Lattner7fbe9722006-10-20 17:42:20 +00003675 SDOperand Ops2[] = { DAG.getConstant(X86::COND_NE, MVT::i8),
Evan Cheng734503b2006-09-11 02:19:56 +00003676 Tmp1.getValue(1) };
Evan Chengcf12ec42006-10-12 19:12:56 +00003677 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, VTs2, 2, Ops2, 2);
Evan Cheng734503b2006-09-11 02:19:56 +00003678 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3679 }
Evan Chengd5781fc2005-12-21 20:21:51 +00003680 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003681}
Evan Cheng6dfa9992006-01-30 23:41:35 +00003682
Evan Cheng0488db92007-09-25 01:57:46 +00003683SDOperand X86TargetLowering::LowerSETCC_New(SDOperand Op, SelectionDAG &DAG) {
3684 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
3685 SDOperand Op0 = Op.getOperand(0);
3686 SDOperand Op1 = Op.getOperand(1);
3687 SDOperand CC = Op.getOperand(2);
3688 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
3689 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
3690 unsigned X86CC;
3691
3692 SDOperand Cond = DAG.getNode(X86ISD::CMP_NEW, MVT::i32, Op0, Op1);
3693 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
3694 Op0, Op1, DAG))
3695 return DAG.getNode(X86ISD::SETCC_NEW, MVT::i8,
3696 DAG.getConstant(X86CC, MVT::i8), Cond);
3697
3698 assert(isFP && "Illegal integer SetCC!");
3699
3700 switch (SetCCOpcode) {
3701 default: assert(false && "Illegal floating point SetCC!");
3702 case ISD::SETOEQ: { // !PF & ZF
3703 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC_NEW, MVT::i8,
3704 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
3705 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC_NEW, MVT::i8,
3706 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
3707 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
3708 }
3709 case ISD::SETUNE: { // PF | !ZF
3710 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC_NEW, MVT::i8,
3711 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
3712 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC_NEW, MVT::i8,
3713 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
3714 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
3715 }
3716 }
3717}
3718
3719
Evan Cheng0db9fe62006-04-25 20:13:52 +00003720SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003721 bool addTest = true;
3722 SDOperand Chain = DAG.getEntryNode();
3723 SDOperand Cond = Op.getOperand(0);
3724 SDOperand CC;
3725 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
Evan Cheng9bba8942006-01-26 02:13:10 +00003726
Evan Cheng734503b2006-09-11 02:19:56 +00003727 if (Cond.getOpcode() == ISD::SETCC)
3728 Cond = LowerSETCC(Cond, DAG, Chain);
3729
3730 if (Cond.getOpcode() == X86ISD::SETCC) {
3731 CC = Cond.getOperand(0);
3732
Evan Cheng0db9fe62006-04-25 20:13:52 +00003733 // If condition flag is set by a X86ISD::CMP, then make a copy of it
Evan Cheng734503b2006-09-11 02:19:56 +00003734 // (since flag operand cannot be shared). Use it as the condition setting
3735 // operand in place of the X86ISD::SETCC.
3736 // If the X86ISD::SETCC has more than one use, then perhaps it's better
Evan Cheng0db9fe62006-04-25 20:13:52 +00003737 // to use a test instead of duplicating the X86ISD::CMP (for register
Evan Cheng734503b2006-09-11 02:19:56 +00003738 // pressure reason)?
3739 SDOperand Cmp = Cond.getOperand(1);
3740 unsigned Opc = Cmp.getOpcode();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003741 bool IllegalFPCMov =
3742 ! ((X86ScalarSSEf32 && Op.getValueType()==MVT::f32) ||
3743 (X86ScalarSSEf64 && Op.getValueType()==MVT::f64)) &&
Evan Cheng734503b2006-09-11 02:19:56 +00003744 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3745 if ((Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) &&
3746 !IllegalFPCMov) {
3747 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3748 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3749 addTest = false;
3750 }
3751 }
Evan Chengaaca22c2006-01-10 20:26:56 +00003752
Evan Cheng0db9fe62006-04-25 20:13:52 +00003753 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003754 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003755 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3756 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng7df96d62005-12-17 01:21:05 +00003757 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00003758
Evan Cheng734503b2006-09-11 02:19:56 +00003759 VTs = DAG.getNodeValueTypes(Op.getValueType(), MVT::Flag);
3760 SmallVector<SDOperand, 4> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003761 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3762 // condition is true.
3763 Ops.push_back(Op.getOperand(2));
3764 Ops.push_back(Op.getOperand(1));
3765 Ops.push_back(CC);
Evan Cheng734503b2006-09-11 02:19:56 +00003766 Ops.push_back(Cond.getValue(1));
3767 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003768}
Evan Cheng9bba8942006-01-26 02:13:10 +00003769
Evan Cheng0488db92007-09-25 01:57:46 +00003770SDOperand X86TargetLowering::LowerSELECT_New(SDOperand Op, SelectionDAG &DAG) {
3771 bool addTest = true;
3772 SDOperand Cond = Op.getOperand(0);
3773 SDOperand CC;
3774
3775 if (Cond.getOpcode() == ISD::SETCC)
3776 Cond = LowerSETCC_New(Cond, DAG);
3777
3778 if (Cond.getOpcode() == X86ISD::SETCC_NEW) {
3779 CC = Cond.getOperand(0);
3780
3781 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3782 // (since flag operand cannot be shared). Use it as the condition setting
3783 // operand in place of the X86ISD::SETCC.
3784 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3785 // to use a test instead of duplicating the X86ISD::CMP (for register
3786 // pressure reason)?
3787 SDOperand Cmp = Cond.getOperand(1);
3788 unsigned Opc = Cmp.getOpcode();
3789 bool IllegalFPCMov =
3790 ! ((X86ScalarSSEf32 && Op.getValueType()==MVT::f32) ||
3791 (X86ScalarSSEf64 && Op.getValueType()==MVT::f64)) &&
3792 !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
3793 if ((Opc == X86ISD::CMP_NEW ||
3794 Opc == X86ISD::COMI_NEW ||
3795 Opc == X86ISD::UCOMI_NEW) &&
3796 !IllegalFPCMov) {
3797 Cond = DAG.getNode(Opc, MVT::i32, Cmp.getOperand(0), Cmp.getOperand(1));
3798 addTest = false;
3799 }
3800 }
3801
3802 if (addTest) {
3803 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3804 Cond = DAG.getNode(X86ISD::CMP_NEW, MVT::i32, Cond,
3805 DAG.getConstant(0, MVT::i8));
3806 }
3807
3808 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
3809 MVT::Flag);
3810 SmallVector<SDOperand, 4> Ops;
3811 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
3812 // condition is true.
3813 Ops.push_back(Op.getOperand(2));
3814 Ops.push_back(Op.getOperand(1));
3815 Ops.push_back(CC);
3816 Ops.push_back(Cond);
3817 return DAG.getNode(X86ISD::CMOV_NEW, VTs, 2, &Ops[0], Ops.size());
3818}
3819
Evan Cheng0db9fe62006-04-25 20:13:52 +00003820SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00003821 bool addTest = true;
3822 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003823 SDOperand Cond = Op.getOperand(1);
3824 SDOperand Dest = Op.getOperand(2);
3825 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00003826 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3827
Evan Cheng0db9fe62006-04-25 20:13:52 +00003828 if (Cond.getOpcode() == ISD::SETCC)
Evan Cheng734503b2006-09-11 02:19:56 +00003829 Cond = LowerSETCC(Cond, DAG, Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003830
3831 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00003832 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003833
Evan Cheng734503b2006-09-11 02:19:56 +00003834 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3835 // (since flag operand cannot be shared). Use it as the condition setting
3836 // operand in place of the X86ISD::SETCC.
3837 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3838 // to use a test instead of duplicating the X86ISD::CMP (for register
3839 // pressure reason)?
3840 SDOperand Cmp = Cond.getOperand(1);
3841 unsigned Opc = Cmp.getOpcode();
3842 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI) {
3843 SDOperand Ops[] = { Chain, Cmp.getOperand(1), Cmp.getOperand(2) };
3844 Cond = DAG.getNode(Opc, VTs, 2, Ops, 3);
3845 addTest = false;
3846 }
3847 }
Evan Cheng1bcee362006-01-13 01:03:02 +00003848
Evan Cheng0db9fe62006-04-25 20:13:52 +00003849 if (addTest) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00003850 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng734503b2006-09-11 02:19:56 +00003851 SDOperand Ops[] = { Chain, Cond, DAG.getConstant(0, MVT::i8) };
3852 Cond = DAG.getNode(X86ISD::CMP, VTs, 2, Ops, 3);
Evan Cheng898101c2005-12-19 23:12:38 +00003853 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003854 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng734503b2006-09-11 02:19:56 +00003855 Cond, Op.getOperand(2), CC, Cond.getValue(1));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003856}
Evan Cheng67f92a72006-01-11 22:15:48 +00003857
Evan Cheng0488db92007-09-25 01:57:46 +00003858SDOperand X86TargetLowering::LowerBRCOND_New(SDOperand Op, SelectionDAG &DAG) {
3859 bool addTest = true;
3860 SDOperand Chain = Op.getOperand(0);
3861 SDOperand Cond = Op.getOperand(1);
3862 SDOperand Dest = Op.getOperand(2);
3863 SDOperand CC;
3864
3865 if (Cond.getOpcode() == ISD::SETCC)
3866 Cond = LowerSETCC_New(Cond, DAG);
3867
3868 if (Cond.getOpcode() == X86ISD::SETCC_NEW) {
3869 CC = Cond.getOperand(0);
3870
3871 // If condition flag is set by a X86ISD::CMP, then make a copy of it
3872 // (since flag operand cannot be shared). Use it as the condition setting
3873 // operand in place of the X86ISD::SETCC.
3874 // If the X86ISD::SETCC has more than one use, then perhaps it's better
3875 // to use a test instead of duplicating the X86ISD::CMP (for register
3876 // pressure reason)?
3877 SDOperand Cmp = Cond.getOperand(1);
3878 unsigned Opc = Cmp.getOpcode();
3879 if (Opc == X86ISD::CMP_NEW ||
3880 Opc == X86ISD::COMI_NEW ||
3881 Opc == X86ISD::UCOMI_NEW) {
3882 Cond = DAG.getNode(Opc, MVT::i32, Cmp.getOperand(0), Cmp.getOperand(1));
3883 addTest = false;
3884 }
3885 }
3886
3887 if (addTest) {
3888 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3889 Cond= DAG.getNode(X86ISD::CMP_NEW, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
3890 }
3891 return DAG.getNode(X86ISD::BRCOND_NEW, Op.getValueType(),
3892 Chain, Op.getOperand(2), CC, Cond);
3893}
3894
Evan Cheng32fe1032006-05-25 00:59:30 +00003895SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
3896 unsigned CallingConv= cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003897
Evan Cheng25ab6902006-09-08 06:48:29 +00003898 if (Subtarget->is64Bit())
Chris Lattner09c75a42007-02-25 09:06:15 +00003899 return LowerX86_64CCCCallTo(Op, DAG, CallingConv);
Evan Cheng32fe1032006-05-25 00:59:30 +00003900 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003901 switch (CallingConv) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00003902 default:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003903 assert(0 && "Unsupported calling convention");
Chris Lattnerf38f5432006-09-27 18:29:38 +00003904 case CallingConv::Fast:
Chris Lattner2db39b82007-02-28 06:05:16 +00003905 // TODO: Implement fastcc
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003906 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00003907 case CallingConv::C:
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003908 case CallingConv::X86_StdCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00003909 return LowerCCCCallTo(Op, DAG, CallingConv);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003910 case CallingConv::X86_FastCall:
Chris Lattner09c75a42007-02-25 09:06:15 +00003911 return LowerFastCCCallTo(Op, DAG, CallingConv);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003912 }
Evan Cheng32fe1032006-05-25 00:59:30 +00003913}
3914
Anton Korobeynikove060b532007-04-17 19:34:00 +00003915
3916// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
3917// Calls to _alloca is needed to probe the stack when allocating more than 4k
3918// bytes in one go. Touching the stack at 4K increments is necessary to ensure
3919// that the guard pages used by the OS virtual memory manager are allocated in
3920// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003921SDOperand
3922X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
3923 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00003924 assert(Subtarget->isTargetCygMing() &&
3925 "This should be used only on Cygwin/Mingw targets");
3926
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003927 // Get the inputs.
3928 SDOperand Chain = Op.getOperand(0);
3929 SDOperand Size = Op.getOperand(1);
3930 // FIXME: Ensure alignment here
3931
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003932 SDOperand Flag;
3933
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003934 MVT::ValueType IntPtr = getPointerTy();
3935 MVT::ValueType SPTy = (Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003936
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003937 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
3938 Flag = Chain.getValue(1);
3939
3940 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3941 SDOperand Ops[] = { Chain,
3942 DAG.getTargetExternalSymbol("_alloca", IntPtr),
3943 DAG.getRegister(X86::EAX, IntPtr),
3944 Flag };
3945 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
3946 Flag = Chain.getValue(1);
3947
3948 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003949
3950 std::vector<MVT::ValueType> Tys;
3951 Tys.push_back(SPTy);
3952 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00003953 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
3954 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00003955}
3956
Evan Cheng1bc78042006-04-26 01:20:17 +00003957SDOperand
3958X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Chenge8bd0a32006-06-06 23:30:24 +00003959 MachineFunction &MF = DAG.getMachineFunction();
3960 const Function* Fn = MF.getFunction();
3961 if (Fn->hasExternalLinkage() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +00003962 Subtarget->isTargetCygMing() &&
Evan Chengb12223e2006-06-09 06:24:42 +00003963 Fn->getName() == "main")
Chris Lattnerd15dff22007-04-17 17:21:52 +00003964 MF.getInfo<X86MachineFunctionInfo>()->setForceFramePointer(true);
Evan Chenge8bd0a32006-06-06 23:30:24 +00003965
Evan Cheng25caf632006-05-23 21:06:34 +00003966 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Evan Cheng25ab6902006-09-08 06:48:29 +00003967 if (Subtarget->is64Bit())
3968 return LowerX86_64CCCArguments(Op, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00003969 else
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003970 switch(CC) {
Chris Lattnerf38f5432006-09-27 18:29:38 +00003971 default:
3972 assert(0 && "Unsupported calling convention");
3973 case CallingConv::Fast:
Chris Lattner2db39b82007-02-28 06:05:16 +00003974 // TODO: implement fastcc.
3975
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003976 // Falls through
Chris Lattnerf38f5432006-09-27 18:29:38 +00003977 case CallingConv::C:
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003978 return LowerCCCArguments(Op, DAG);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003979 case CallingConv::X86_StdCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00003980 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(StdCall);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00003981 return LowerCCCArguments(Op, DAG, true);
Chris Lattnerf38f5432006-09-27 18:29:38 +00003982 case CallingConv::X86_FastCall:
Chris Lattnerd15dff22007-04-17 17:21:52 +00003983 MF.getInfo<X86MachineFunctionInfo>()->setDecorationStyle(FastCall);
Chris Lattner2db39b82007-02-28 06:05:16 +00003984 return LowerFastCCArguments(Op, DAG);
Anton Korobeynikovf8248682006-09-20 22:03:51 +00003985 }
Evan Cheng1bc78042006-04-26 01:20:17 +00003986}
3987
Evan Cheng0db9fe62006-04-25 20:13:52 +00003988SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
3989 SDOperand InFlag(0, 0);
3990 SDOperand Chain = Op.getOperand(0);
3991 unsigned Align =
3992 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
3993 if (Align == 0) Align = 1;
3994
3995 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00003996 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00003997 // The libc version is likely to be faster for these cases. It can use the
3998 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003999 if ((Align & 3) != 0 ||
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004000 (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004001 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004002 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004003 TargetLowering::ArgListTy Args;
4004 TargetLowering::ArgListEntry Entry;
4005 Entry.Node = Op.getOperand(1);
4006 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004007 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004008 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004009 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4010 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004011 Args.push_back(Entry);
4012 Entry.Node = Op.getOperand(3);
4013 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004014 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004015 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004016 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4017 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004018 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004019
Evan Cheng0db9fe62006-04-25 20:13:52 +00004020 MVT::ValueType AVT;
4021 SDOperand Count;
4022 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4023 unsigned BytesLeft = 0;
4024 bool TwoRepStos = false;
4025 if (ValC) {
4026 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004027 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004028
Evan Cheng0db9fe62006-04-25 20:13:52 +00004029 // If the value is a constant, then we can potentially use larger sets.
4030 switch (Align & 3) {
4031 case 2: // WORD aligned
4032 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004033 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004034 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004035 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004036 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004037 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004038 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004039 Val = (Val << 8) | Val;
4040 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004041 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4042 AVT = MVT::i64;
4043 ValReg = X86::RAX;
4044 Val = (Val << 32) | Val;
4045 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004046 break;
4047 default: // Byte aligned
4048 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004049 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004050 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004051 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004052 }
4053
Evan Cheng25ab6902006-09-08 06:48:29 +00004054 if (AVT > MVT::i8) {
4055 if (I) {
4056 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4057 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4058 BytesLeft = I->getValue() % UBytes;
4059 } else {
4060 assert(AVT >= MVT::i32 &&
4061 "Do not use rep;stos if not at least DWORD aligned");
4062 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4063 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4064 TwoRepStos = true;
4065 }
4066 }
4067
Evan Cheng0db9fe62006-04-25 20:13:52 +00004068 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4069 InFlag);
4070 InFlag = Chain.getValue(1);
4071 } else {
4072 AVT = MVT::i8;
4073 Count = Op.getOperand(3);
4074 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4075 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004076 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004077
Evan Cheng25ab6902006-09-08 06:48:29 +00004078 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4079 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004080 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004081 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4082 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004083 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004084
Chris Lattnerd96d0722007-02-25 06:40:16 +00004085 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004086 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004087 Ops.push_back(Chain);
4088 Ops.push_back(DAG.getValueType(AVT));
4089 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004090 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004091
Evan Cheng0db9fe62006-04-25 20:13:52 +00004092 if (TwoRepStos) {
4093 InFlag = Chain.getValue(1);
4094 Count = Op.getOperand(3);
4095 MVT::ValueType CVT = Count.getValueType();
4096 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004097 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4098 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4099 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004100 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004101 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004102 Ops.clear();
4103 Ops.push_back(Chain);
4104 Ops.push_back(DAG.getValueType(MVT::i8));
4105 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004106 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004107 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004108 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004109 SDOperand Value;
4110 unsigned Val = ValC->getValue() & 255;
4111 unsigned Offset = I->getValue() - BytesLeft;
4112 SDOperand DstAddr = Op.getOperand(1);
4113 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004114 if (BytesLeft >= 4) {
4115 Val = (Val << 8) | Val;
4116 Val = (Val << 16) | Val;
4117 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004118 Chain = DAG.getStore(Chain, Value,
4119 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4120 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004121 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004122 BytesLeft -= 4;
4123 Offset += 4;
4124 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004125 if (BytesLeft >= 2) {
4126 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004127 Chain = DAG.getStore(Chain, Value,
4128 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4129 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004130 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004131 BytesLeft -= 2;
4132 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004133 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004134 if (BytesLeft == 1) {
4135 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004136 Chain = DAG.getStore(Chain, Value,
4137 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4138 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004139 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004140 }
Evan Cheng386031a2006-03-24 07:29:27 +00004141 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004142
Evan Cheng0db9fe62006-04-25 20:13:52 +00004143 return Chain;
4144}
Evan Cheng11e15b32006-04-03 20:53:28 +00004145
Evan Cheng0db9fe62006-04-25 20:13:52 +00004146SDOperand X86TargetLowering::LowerMEMCPY(SDOperand Op, SelectionDAG &DAG) {
4147 SDOperand Chain = Op.getOperand(0);
4148 unsigned Align =
4149 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4150 if (Align == 0) Align = 1;
Evan Cheng11e15b32006-04-03 20:53:28 +00004151
Evan Cheng0db9fe62006-04-25 20:13:52 +00004152 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004153 // If not DWORD aligned or size is more than the threshold, call memcpy.
Rafael Espindola44c82652007-08-27 17:48:26 +00004154 // The libc version is likely to be faster for these cases. It can use the
4155 // address value and run time information about the CPU.
4156 // With glibc 2.6.1 on a core 2, coping an array of 100M longs was 30% faster
Evan Cheng0db9fe62006-04-25 20:13:52 +00004157 if ((Align & 3) != 0 ||
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004158 (I && I->getValue() > Subtarget->getMinRepStrSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004159 MVT::ValueType IntPtr = getPointerTy();
Reid Spencer47857812006-12-31 05:55:36 +00004160 TargetLowering::ArgListTy Args;
4161 TargetLowering::ArgListEntry Entry;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00004162 Entry.Ty = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004163 Entry.Node = Op.getOperand(1); Args.push_back(Entry);
4164 Entry.Node = Op.getOperand(2); Args.push_back(Entry);
4165 Entry.Node = Op.getOperand(3); Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004166 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004167 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004168 DAG.getExternalSymbol("memcpy", IntPtr), Args, DAG);
4169 return CallResult.second;
Evan Chengb067a1e2006-03-31 19:22:53 +00004170 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004171
4172 MVT::ValueType AVT;
4173 SDOperand Count;
4174 unsigned BytesLeft = 0;
4175 bool TwoRepMovs = false;
4176 switch (Align & 3) {
4177 case 2: // WORD aligned
4178 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004179 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004180 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004181 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004182 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4183 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004184 break;
4185 default: // Byte aligned
4186 AVT = MVT::i8;
4187 Count = Op.getOperand(3);
4188 break;
4189 }
4190
Evan Cheng25ab6902006-09-08 06:48:29 +00004191 if (AVT > MVT::i8) {
4192 if (I) {
4193 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
4194 Count = DAG.getConstant(I->getValue() / UBytes, getPointerTy());
4195 BytesLeft = I->getValue() % UBytes;
4196 } else {
4197 assert(AVT >= MVT::i32 &&
4198 "Do not use rep;movs if not at least DWORD aligned");
4199 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4200 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4201 TwoRepMovs = true;
4202 }
4203 }
4204
Evan Cheng0db9fe62006-04-25 20:13:52 +00004205 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004206 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4207 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004208 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004209 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4210 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004211 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004212 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
4213 Op.getOperand(2), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004214 InFlag = Chain.getValue(1);
4215
Chris Lattnerd96d0722007-02-25 06:40:16 +00004216 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004217 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004218 Ops.push_back(Chain);
4219 Ops.push_back(DAG.getValueType(AVT));
4220 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004221 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004222
4223 if (TwoRepMovs) {
4224 InFlag = Chain.getValue(1);
4225 Count = Op.getOperand(3);
4226 MVT::ValueType CVT = Count.getValueType();
4227 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004228 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4229 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4230 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004231 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004232 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004233 Ops.clear();
4234 Ops.push_back(Chain);
4235 Ops.push_back(DAG.getValueType(MVT::i8));
4236 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004237 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004238 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004239 // Issue loads and stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004240 unsigned Offset = I->getValue() - BytesLeft;
4241 SDOperand DstAddr = Op.getOperand(1);
4242 MVT::ValueType DstVT = DstAddr.getValueType();
4243 SDOperand SrcAddr = Op.getOperand(2);
4244 MVT::ValueType SrcVT = SrcAddr.getValueType();
4245 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004246 if (BytesLeft >= 4) {
4247 Value = DAG.getLoad(MVT::i32, Chain,
4248 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4249 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004250 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004251 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004252 Chain = DAG.getStore(Chain, Value,
4253 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4254 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004255 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004256 BytesLeft -= 4;
4257 Offset += 4;
4258 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004259 if (BytesLeft >= 2) {
4260 Value = DAG.getLoad(MVT::i16, Chain,
4261 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4262 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004263 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004264 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004265 Chain = DAG.getStore(Chain, Value,
4266 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4267 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004268 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004269 BytesLeft -= 2;
4270 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004271 }
4272
Evan Cheng0db9fe62006-04-25 20:13:52 +00004273 if (BytesLeft == 1) {
4274 Value = DAG.getLoad(MVT::i8, Chain,
4275 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4276 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004277 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004278 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004279 Chain = DAG.getStore(Chain, Value,
4280 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4281 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004282 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004283 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004284 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004285
4286 return Chain;
4287}
4288
4289SDOperand
4290X86TargetLowering::LowerREADCYCLCECOUNTER(SDOperand Op, SelectionDAG &DAG) {
Chris Lattnerd96d0722007-02-25 06:40:16 +00004291 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004292 SDOperand TheOp = Op.getOperand(0);
4293 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheOp, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004294 if (Subtarget->is64Bit()) {
4295 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4296 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::RDX,
4297 MVT::i64, Copy1.getValue(2));
4298 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, Copy2,
4299 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004300 SDOperand Ops[] = {
4301 DAG.getNode(ISD::OR, MVT::i64, Copy1, Tmp), Copy2.getValue(1)
4302 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004303
4304 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004305 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004306 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004307
4308 SDOperand Copy1 = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4309 SDOperand Copy2 = DAG.getCopyFromReg(Copy1.getValue(1), X86::EDX,
4310 MVT::i32, Copy1.getValue(2));
4311 SDOperand Ops[] = { Copy1, Copy2, Copy2.getValue(1) };
4312 Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
4313 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004314}
4315
4316SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004317 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4318
Evan Cheng25ab6902006-09-08 06:48:29 +00004319 if (!Subtarget->is64Bit()) {
4320 // vastart just stores the address of the VarArgsFrameIndex slot into the
4321 // memory location argument.
4322 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004323 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4324 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004325 }
4326
4327 // __va_list_tag:
4328 // gp_offset (0 - 6 * 8)
4329 // fp_offset (48 - 48 + 8 * 16)
4330 // overflow_arg_area (point to parameters coming in memory).
4331 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00004332 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00004333 SDOperand FIN = Op.getOperand(1);
4334 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004335 SDOperand Store = DAG.getStore(Op.getOperand(0),
4336 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004337 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004338 MemOps.push_back(Store);
4339
4340 // Store fp_offset
4341 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4342 DAG.getConstant(4, getPointerTy()));
Evan Cheng786225a2006-10-05 23:01:46 +00004343 Store = DAG.getStore(Op.getOperand(0),
4344 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004345 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004346 MemOps.push_back(Store);
4347
4348 // Store ptr to overflow_arg_area
4349 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4350 DAG.getConstant(4, getPointerTy()));
4351 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004352 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4353 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004354 MemOps.push_back(Store);
4355
4356 // Store ptr to reg_save_area.
4357 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
4358 DAG.getConstant(8, getPointerTy()));
4359 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004360 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4361 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004362 MemOps.push_back(Store);
4363 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004364}
4365
Evan Chengae642192007-03-02 23:16:35 +00004366SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4367 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4368 SDOperand Chain = Op.getOperand(0);
4369 SDOperand DstPtr = Op.getOperand(1);
4370 SDOperand SrcPtr = Op.getOperand(2);
4371 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4372 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4373
4374 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4375 SrcSV->getValue(), SrcSV->getOffset());
4376 Chain = SrcPtr.getValue(1);
4377 for (unsigned i = 0; i < 3; ++i) {
4378 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4379 SrcSV->getValue(), SrcSV->getOffset());
4380 Chain = Val.getValue(1);
4381 Chain = DAG.getStore(Chain, Val, DstPtr,
4382 DstSV->getValue(), DstSV->getOffset());
4383 if (i == 2)
4384 break;
4385 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
4386 DAG.getConstant(8, getPointerTy()));
4387 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
4388 DAG.getConstant(8, getPointerTy()));
4389 }
4390 return Chain;
4391}
4392
Evan Cheng0db9fe62006-04-25 20:13:52 +00004393SDOperand
4394X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4395 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4396 switch (IntNo) {
4397 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004398 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004399 case Intrinsic::x86_sse_comieq_ss:
4400 case Intrinsic::x86_sse_comilt_ss:
4401 case Intrinsic::x86_sse_comile_ss:
4402 case Intrinsic::x86_sse_comigt_ss:
4403 case Intrinsic::x86_sse_comige_ss:
4404 case Intrinsic::x86_sse_comineq_ss:
4405 case Intrinsic::x86_sse_ucomieq_ss:
4406 case Intrinsic::x86_sse_ucomilt_ss:
4407 case Intrinsic::x86_sse_ucomile_ss:
4408 case Intrinsic::x86_sse_ucomigt_ss:
4409 case Intrinsic::x86_sse_ucomige_ss:
4410 case Intrinsic::x86_sse_ucomineq_ss:
4411 case Intrinsic::x86_sse2_comieq_sd:
4412 case Intrinsic::x86_sse2_comilt_sd:
4413 case Intrinsic::x86_sse2_comile_sd:
4414 case Intrinsic::x86_sse2_comigt_sd:
4415 case Intrinsic::x86_sse2_comige_sd:
4416 case Intrinsic::x86_sse2_comineq_sd:
4417 case Intrinsic::x86_sse2_ucomieq_sd:
4418 case Intrinsic::x86_sse2_ucomilt_sd:
4419 case Intrinsic::x86_sse2_ucomile_sd:
4420 case Intrinsic::x86_sse2_ucomigt_sd:
4421 case Intrinsic::x86_sse2_ucomige_sd:
4422 case Intrinsic::x86_sse2_ucomineq_sd: {
4423 unsigned Opc = 0;
4424 ISD::CondCode CC = ISD::SETCC_INVALID;
4425 switch (IntNo) {
4426 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004427 case Intrinsic::x86_sse_comieq_ss:
4428 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004429 Opc = X86ISD::COMI;
4430 CC = ISD::SETEQ;
4431 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004432 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004433 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004434 Opc = X86ISD::COMI;
4435 CC = ISD::SETLT;
4436 break;
4437 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004438 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004439 Opc = X86ISD::COMI;
4440 CC = ISD::SETLE;
4441 break;
4442 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004443 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004444 Opc = X86ISD::COMI;
4445 CC = ISD::SETGT;
4446 break;
4447 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004448 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004449 Opc = X86ISD::COMI;
4450 CC = ISD::SETGE;
4451 break;
4452 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004453 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004454 Opc = X86ISD::COMI;
4455 CC = ISD::SETNE;
4456 break;
4457 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004458 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004459 Opc = X86ISD::UCOMI;
4460 CC = ISD::SETEQ;
4461 break;
4462 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004463 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004464 Opc = X86ISD::UCOMI;
4465 CC = ISD::SETLT;
4466 break;
4467 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004468 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004469 Opc = X86ISD::UCOMI;
4470 CC = ISD::SETLE;
4471 break;
4472 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004473 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004474 Opc = X86ISD::UCOMI;
4475 CC = ISD::SETGT;
4476 break;
4477 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004478 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004479 Opc = X86ISD::UCOMI;
4480 CC = ISD::SETGE;
4481 break;
4482 case Intrinsic::x86_sse_ucomineq_ss:
4483 case Intrinsic::x86_sse2_ucomineq_sd:
4484 Opc = X86ISD::UCOMI;
4485 CC = ISD::SETNE;
4486 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004487 }
Evan Cheng734503b2006-09-11 02:19:56 +00004488
Evan Cheng0db9fe62006-04-25 20:13:52 +00004489 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004490 SDOperand LHS = Op.getOperand(1);
4491 SDOperand RHS = Op.getOperand(2);
4492 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004493
Evan Cheng0488db92007-09-25 01:57:46 +00004494 if (NewCCModeling) {
4495 Opc = (Opc == X86ISD::UCOMI) ? X86ISD::UCOMI_NEW : X86ISD::COMI_NEW;
4496 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4497 SDOperand SetCC = DAG.getNode(X86ISD::SETCC_NEW, MVT::i8,
4498 DAG.getConstant(X86CC, MVT::i8), Cond);
4499 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4500 } else {
4501 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
4502 SDOperand Ops1[] = { DAG.getEntryNode(), LHS, RHS };
4503 SDOperand Cond = DAG.getNode(Opc, VTs, 2, Ops1, 3);
4504 VTs = DAG.getNodeValueTypes(MVT::i8, MVT::Flag);
4505 SDOperand Ops2[] = { DAG.getConstant(X86CC, MVT::i8), Cond };
4506 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, VTs, 2, Ops2, 2);
4507 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
4508 }
Evan Cheng6be2c582006-04-05 23:38:46 +00004509 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004510 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004511}
Evan Cheng72261582005-12-20 06:22:03 +00004512
Nate Begemanbcc5f362007-01-29 22:58:52 +00004513SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4514 // Depths > 0 not supported yet!
4515 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4516 return SDOperand();
4517
4518 // Just load the return address
4519 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4520 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4521}
4522
4523SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4524 // Depths > 0 not supported yet!
4525 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4526 return SDOperand();
4527
4528 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4529 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
4530 DAG.getConstant(4, getPointerTy()));
4531}
4532
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004533SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4534 SelectionDAG &DAG) {
4535 // Is not yet supported on x86-64
4536 if (Subtarget->is64Bit())
4537 return SDOperand();
4538
4539 return DAG.getConstant(8, getPointerTy());
4540}
4541
4542SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4543{
4544 assert(!Subtarget->is64Bit() &&
4545 "Lowering of eh_return builtin is not supported yet on x86-64");
4546
4547 MachineFunction &MF = DAG.getMachineFunction();
4548 SDOperand Chain = Op.getOperand(0);
4549 SDOperand Offset = Op.getOperand(1);
4550 SDOperand Handler = Op.getOperand(2);
4551
4552 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4553 getPointerTy());
4554
4555 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
4556 DAG.getConstant(-4UL, getPointerTy()));
4557 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4558 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4559 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
4560 MF.addLiveOut(X86::ECX);
4561
4562 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4563 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4564}
4565
Duncan Sandsb116fac2007-07-27 20:02:49 +00004566SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4567 SelectionDAG &DAG) {
4568 SDOperand Root = Op.getOperand(0);
4569 SDOperand Trmp = Op.getOperand(1); // trampoline
4570 SDOperand FPtr = Op.getOperand(2); // nested function
4571 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4572
4573 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4574
4575 if (Subtarget->is64Bit()) {
4576 return SDOperand(); // not yet supported
4577 } else {
4578 Function *Func = (Function *)
4579 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4580 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00004581 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004582
4583 switch (CC) {
4584 default:
4585 assert(0 && "Unsupported calling convention");
4586 case CallingConv::C:
4587 case CallingConv::Fast:
4588 case CallingConv::X86_StdCall: {
4589 // Pass 'nest' parameter in ECX.
4590 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004591 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004592
4593 // Check that ECX wasn't needed by an 'inreg' parameter.
4594 const FunctionType *FTy = Func->getFunctionType();
4595 const ParamAttrsList *Attrs = FTy->getParamAttrs();
4596
4597 if (Attrs && !Func->isVarArg()) {
4598 unsigned InRegCount = 0;
4599 unsigned Idx = 1;
4600
4601 for (FunctionType::param_iterator I = FTy->param_begin(),
4602 E = FTy->param_end(); I != E; ++I, ++Idx)
4603 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4604 // FIXME: should only count parameters that are lowered to integers.
4605 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
4606
4607 if (InRegCount > 2) {
4608 cerr << "Nest register in use - reduce number of inreg parameters!\n";
4609 abort();
4610 }
4611 }
4612 break;
4613 }
4614 case CallingConv::X86_FastCall:
4615 // Pass 'nest' parameter in EAX.
4616 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004617 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004618 break;
4619 }
4620
Duncan Sandsee465742007-08-29 19:01:20 +00004621 const X86InstrInfo *TII =
4622 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4623
Duncan Sandsb116fac2007-07-27 20:02:49 +00004624 SDOperand OutChains[4];
4625 SDOperand Addr, Disp;
4626
4627 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
4628 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
4629
Duncan Sandsee465742007-08-29 19:01:20 +00004630 unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
4631 unsigned char N86Reg = ((X86RegisterInfo&)RegInfo).getX86RegNum(NestReg);
4632 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsb116fac2007-07-27 20:02:49 +00004633 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
4634
4635 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
4636 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4637 TrmpSV->getOffset() + 1, false, 1);
4638
Duncan Sandsee465742007-08-29 19:01:20 +00004639 unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004640 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
4641 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
4642 TrmpSV->getValue() + 5, TrmpSV->getOffset());
4643
4644 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
4645 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
4646 TrmpSV->getOffset() + 6, false, 1);
4647
Duncan Sandsf7331b32007-09-11 14:10:23 +00004648 SDOperand Ops[] =
4649 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
4650 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004651 }
4652}
4653
Evan Cheng0db9fe62006-04-25 20:13:52 +00004654/// LowerOperation - Provide custom lowering hooks for some operations.
4655///
4656SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
4657 switch (Op.getOpcode()) {
4658 default: assert(0 && "Should not custom lower this!");
4659 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
4660 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
4661 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
4662 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
4663 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
4664 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
4665 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004666 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004667 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
4668 case ISD::SHL_PARTS:
4669 case ISD::SRA_PARTS:
4670 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
4671 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
4672 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
4673 case ISD::FABS: return LowerFABS(Op, DAG);
4674 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004675 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00004676 case ISD::SETCC: return NewCCModeling
4677 ? LowerSETCC_New(Op, DAG)
4678 : LowerSETCC(Op, DAG, DAG.getEntryNode());
4679 case ISD::SELECT: return NewCCModeling
4680 ? LowerSELECT_New(Op, DAG)
4681 : LowerSELECT(Op, DAG);
4682 case ISD::BRCOND: return NewCCModeling
4683 ? LowerBRCOND_New(Op, DAG)
4684 : LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004685 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00004686 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004687 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00004688 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004689 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
4690 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
4691 case ISD::READCYCLECOUNTER: return LowerREADCYCLCECOUNTER(Op, DAG);
4692 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00004693 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004694 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00004695 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
4696 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004697 case ISD::FRAME_TO_ARGS_OFFSET:
4698 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004699 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004700 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004701 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004702 }
Jim Laskey62819f32007-02-21 22:54:50 +00004703 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004704}
4705
Evan Cheng72261582005-12-20 06:22:03 +00004706const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
4707 switch (Opcode) {
4708 default: return NULL;
Evan Chenge3413162006-01-09 18:33:28 +00004709 case X86ISD::SHLD: return "X86ISD::SHLD";
4710 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00004711 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004712 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00004713 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00004714 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00004715 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00004716 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00004717 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
4718 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
4719 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00004720 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00004721 case X86ISD::FST: return "X86ISD::FST";
4722 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Chengb077b842005-12-21 02:39:21 +00004723 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00004724 case X86ISD::CALL: return "X86ISD::CALL";
4725 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
4726 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
4727 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng0488db92007-09-25 01:57:46 +00004728 case X86ISD::CMP_NEW: return "X86ISD::CMP_NEW";
Evan Cheng6be2c582006-04-05 23:38:46 +00004729 case X86ISD::COMI: return "X86ISD::COMI";
Evan Cheng0488db92007-09-25 01:57:46 +00004730 case X86ISD::COMI_NEW: return "X86ISD::COMI_NEW";
Evan Cheng6be2c582006-04-05 23:38:46 +00004731 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Cheng0488db92007-09-25 01:57:46 +00004732 case X86ISD::UCOMI_NEW: return "X86ISD::UCOMI_NEW";
Evan Chengd5781fc2005-12-21 20:21:51 +00004733 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng0488db92007-09-25 01:57:46 +00004734 case X86ISD::SETCC_NEW: return "X86ISD::SETCC_NEW";
Evan Cheng72261582005-12-20 06:22:03 +00004735 case X86ISD::CMOV: return "X86ISD::CMOV";
Evan Cheng0488db92007-09-25 01:57:46 +00004736 case X86ISD::CMOV_NEW: return "X86ISD::CMOV_NEW";
Evan Cheng72261582005-12-20 06:22:03 +00004737 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Cheng0488db92007-09-25 01:57:46 +00004738 case X86ISD::BRCOND_NEW: return "X86ISD::BRCOND_NEW";
Evan Chengb077b842005-12-21 02:39:21 +00004739 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00004740 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
4741 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00004742 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00004743 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00004744 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00004745 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00004746 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00004747 case X86ISD::FMAX: return "X86ISD::FMAX";
4748 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00004749 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
4750 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004751 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
4752 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004753 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Evan Cheng72261582005-12-20 06:22:03 +00004754 }
4755}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00004756
Chris Lattnerc9addb72007-03-30 23:15:24 +00004757// isLegalAddressingMode - Return true if the addressing mode represented
4758// by AM is legal for this target, for a load/store of the specified type.
4759bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
4760 const Type *Ty) const {
4761 // X86 supports extremely general addressing modes.
4762
4763 // X86 allows a sign-extended 32-bit immediate field as a displacement.
4764 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
4765 return false;
4766
4767 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00004768 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00004769 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
4770 return false;
Evan Cheng52787842007-08-01 23:46:47 +00004771
4772 // X86-64 only supports addr of globals in small code model.
4773 if (Subtarget->is64Bit()) {
4774 if (getTargetMachine().getCodeModel() != CodeModel::Small)
4775 return false;
4776 // If lower 4G is not available, then we must use rip-relative addressing.
4777 if (AM.BaseOffs || AM.Scale > 1)
4778 return false;
4779 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00004780 }
4781
4782 switch (AM.Scale) {
4783 case 0:
4784 case 1:
4785 case 2:
4786 case 4:
4787 case 8:
4788 // These scales always work.
4789 break;
4790 case 3:
4791 case 5:
4792 case 9:
4793 // These scales are formed with basereg+scalereg. Only accept if there is
4794 // no basereg yet.
4795 if (AM.HasBaseReg)
4796 return false;
4797 break;
4798 default: // Other stuff never works.
4799 return false;
4800 }
4801
4802 return true;
4803}
4804
4805
Evan Cheng60c07e12006-07-05 22:17:51 +00004806/// isShuffleMaskLegal - Targets can use this to indicate that they only
4807/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
4808/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
4809/// are assumed to be legal.
4810bool
4811X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
4812 // Only do shuffles on 128-bit vector types for now.
4813 if (MVT::getSizeInBits(VT) == 64) return false;
4814 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00004815 isIdentityMask(Mask.Val) ||
4816 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00004817 isSplatMask(Mask.Val) ||
4818 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
4819 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00004820 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00004821 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00004822 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00004823}
4824
4825bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
4826 MVT::ValueType EVT,
4827 SelectionDAG &DAG) const {
4828 unsigned NumElts = BVOps.size();
4829 // Only do shuffles on 128-bit vector types for now.
4830 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
4831 if (NumElts == 2) return true;
4832 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00004833 return (isMOVLMask(&BVOps[0], 4) ||
4834 isCommutedMOVL(&BVOps[0], 4, true) ||
4835 isSHUFPMask(&BVOps[0], 4) ||
4836 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00004837 }
4838 return false;
4839}
4840
4841//===----------------------------------------------------------------------===//
4842// X86 Scheduler Hooks
4843//===----------------------------------------------------------------------===//
4844
4845MachineBasicBlock *
4846X86TargetLowering::InsertAtEndOfBasicBlock(MachineInstr *MI,
4847 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00004848 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00004849 switch (MI->getOpcode()) {
4850 default: assert(false && "Unexpected instr type to insert");
4851 case X86::CMOV_FR32:
4852 case X86::CMOV_FR64:
4853 case X86::CMOV_V4F32:
4854 case X86::CMOV_V2F64:
Evan Cheng0488db92007-09-25 01:57:46 +00004855 case X86::CMOV_V2I64:
4856
4857 case X86::NEW_CMOV_FR32:
4858 case X86::NEW_CMOV_FR64:
4859 case X86::NEW_CMOV_V4F32:
4860 case X86::NEW_CMOV_V2F64:
4861 case X86::NEW_CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00004862 // To "insert" a SELECT_CC instruction, we actually have to insert the
4863 // diamond control-flow pattern. The incoming instruction knows the
4864 // destination vreg to set, the condition code register to branch on, the
4865 // true/false values to select between, and a branch opcode to use.
4866 const BasicBlock *LLVM_BB = BB->getBasicBlock();
4867 ilist<MachineBasicBlock>::iterator It = BB;
4868 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004869
Evan Cheng60c07e12006-07-05 22:17:51 +00004870 // thisMBB:
4871 // ...
4872 // TrueVal = ...
4873 // cmpTY ccX, r1, r2
4874 // bCC copy1MBB
4875 // fallthrough --> copy0MBB
4876 MachineBasicBlock *thisMBB = BB;
4877 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
4878 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004879 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00004880 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00004881 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00004882 MachineFunction *F = BB->getParent();
4883 F->getBasicBlockList().insert(It, copy0MBB);
4884 F->getBasicBlockList().insert(It, sinkMBB);
4885 // Update machine-CFG edges by first adding all successors of the current
4886 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004887 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00004888 e = BB->succ_end(); i != e; ++i)
4889 sinkMBB->addSuccessor(*i);
4890 // Next, remove all successors of the current block, and add the true
4891 // and fallthrough blocks as its successors.
4892 while(!BB->succ_empty())
4893 BB->removeSuccessor(BB->succ_begin());
4894 BB->addSuccessor(copy0MBB);
4895 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004896
Evan Cheng60c07e12006-07-05 22:17:51 +00004897 // copy0MBB:
4898 // %FalseValue = ...
4899 // # fallthrough to sinkMBB
4900 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004901
Evan Cheng60c07e12006-07-05 22:17:51 +00004902 // Update machine-CFG edges
4903 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004904
Evan Cheng60c07e12006-07-05 22:17:51 +00004905 // sinkMBB:
4906 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
4907 // ...
4908 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00004909 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00004910 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
4911 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
4912
4913 delete MI; // The pseudo instruction is gone now.
4914 return BB;
4915 }
4916
Dale Johannesen849f2142007-07-03 00:53:03 +00004917 case X86::FP32_TO_INT16_IN_MEM:
4918 case X86::FP32_TO_INT32_IN_MEM:
4919 case X86::FP32_TO_INT64_IN_MEM:
4920 case X86::FP64_TO_INT16_IN_MEM:
4921 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00004922 case X86::FP64_TO_INT64_IN_MEM:
4923 case X86::FP80_TO_INT16_IN_MEM:
4924 case X86::FP80_TO_INT32_IN_MEM:
4925 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00004926 // Change the floating point control register to use "round towards zero"
4927 // mode when truncating to an integer value.
4928 MachineFunction *F = BB->getParent();
4929 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004930 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004931
4932 // Load the old value of the high byte of the control word...
4933 unsigned OldCW =
4934 F->getSSARegMap()->createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00004935 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004936
4937 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004938 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
4939 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00004940
4941 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00004942 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004943
4944 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00004945 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
4946 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00004947
4948 // Get the X86 opcode to use.
4949 unsigned Opc;
4950 switch (MI->getOpcode()) {
4951 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00004952 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
4953 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
4954 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
4955 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
4956 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
4957 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00004958 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
4959 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
4960 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00004961 }
4962
4963 X86AddressMode AM;
4964 MachineOperand &Op = MI->getOperand(0);
4965 if (Op.isRegister()) {
4966 AM.BaseType = X86AddressMode::RegBase;
4967 AM.Base.Reg = Op.getReg();
4968 } else {
4969 AM.BaseType = X86AddressMode::FrameIndexBase;
4970 AM.Base.FrameIndex = Op.getFrameIndex();
4971 }
4972 Op = MI->getOperand(1);
4973 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004974 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004975 Op = MI->getOperand(2);
4976 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00004977 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004978 Op = MI->getOperand(3);
4979 if (Op.isGlobalAddress()) {
4980 AM.GV = Op.getGlobal();
4981 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00004982 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00004983 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00004984 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
4985 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00004986
4987 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00004988 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00004989
4990 delete MI; // The pseudo instruction is gone now.
4991 return BB;
4992 }
4993 }
4994}
4995
4996//===----------------------------------------------------------------------===//
4997// X86 Optimization Hooks
4998//===----------------------------------------------------------------------===//
4999
Nate Begeman368e18d2006-02-16 21:11:51 +00005000void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5001 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005002 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00005003 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005004 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005005 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005006 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005007 assert((Opc >= ISD::BUILTIN_OP_END ||
5008 Opc == ISD::INTRINSIC_WO_CHAIN ||
5009 Opc == ISD::INTRINSIC_W_CHAIN ||
5010 Opc == ISD::INTRINSIC_VOID) &&
5011 "Should use MaskedValueIsZero if you don't know whether Op"
5012 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005013
Evan Cheng865f0602006-04-05 06:11:20 +00005014 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005015 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005016 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005017 case X86ISD::SETCC:
Evan Cheng0488db92007-09-25 01:57:46 +00005018 case X86ISD::SETCC_NEW:
Nate Begeman368e18d2006-02-16 21:11:51 +00005019 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5020 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005021 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005022}
Chris Lattner259e97c2006-01-31 19:43:35 +00005023
Evan Cheng206ee9d2006-07-07 08:33:52 +00005024/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5025/// element of the result of the vector shuffle.
5026static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5027 MVT::ValueType VT = N->getValueType(0);
5028 SDOperand PermMask = N->getOperand(2);
5029 unsigned NumElems = PermMask.getNumOperands();
5030 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5031 i %= NumElems;
5032 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5033 return (i == 0)
Dan Gohman51eaa862007-06-14 22:58:02 +00005034 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005035 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5036 SDOperand Idx = PermMask.getOperand(i);
5037 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005038 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005039 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5040 }
5041 return SDOperand();
5042}
5043
5044/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5045/// node is a GlobalAddress + an offset.
5046static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005047 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005048 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005049 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5050 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5051 return true;
5052 }
Evan Cheng0085a282006-11-30 21:55:46 +00005053 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005054 SDOperand N1 = N->getOperand(0);
5055 SDOperand N2 = N->getOperand(1);
5056 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5057 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5058 if (V) {
5059 Offset += V->getSignExtended();
5060 return true;
5061 }
5062 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5063 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5064 if (V) {
5065 Offset += V->getSignExtended();
5066 return true;
5067 }
5068 }
5069 }
5070 return false;
5071}
5072
5073/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5074/// + Dist * Size.
5075static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5076 MachineFrameInfo *MFI) {
5077 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5078 return false;
5079
5080 SDOperand Loc = N->getOperand(1);
5081 SDOperand BaseLoc = Base->getOperand(1);
5082 if (Loc.getOpcode() == ISD::FrameIndex) {
5083 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5084 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005085 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5086 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005087 int FS = MFI->getObjectSize(FI);
5088 int BFS = MFI->getObjectSize(BFI);
5089 if (FS != BFS || FS != Size) return false;
5090 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5091 } else {
5092 GlobalValue *GV1 = NULL;
5093 GlobalValue *GV2 = NULL;
5094 int64_t Offset1 = 0;
5095 int64_t Offset2 = 0;
5096 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5097 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5098 if (isGA1 && isGA2 && GV1 == GV2)
5099 return Offset1 == (Offset2 + Dist*Size);
5100 }
5101
5102 return false;
5103}
5104
Evan Cheng1e60c092006-07-10 21:37:44 +00005105static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5106 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005107 GlobalValue *GV;
5108 int64_t Offset;
5109 if (isGAPlusOffset(Base, GV, Offset))
5110 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
5111 else {
5112 assert(Base->getOpcode() == ISD::FrameIndex && "Unexpected base node!");
Dan Gohman275769a2007-07-23 20:24:29 +00005113 int BFI = cast<FrameIndexSDNode>(Base)->getIndex();
Evan Cheng1e60c092006-07-10 21:37:44 +00005114 if (BFI < 0)
5115 // Fixed objects do not specify alignment, however the offsets are known.
5116 return ((Subtarget->getStackAlignment() % 16) == 0 &&
5117 (MFI->getObjectOffset(BFI) % 16) == 0);
5118 else
5119 return MFI->getObjectAlignment(BFI) >= 16;
Evan Cheng206ee9d2006-07-07 08:33:52 +00005120 }
5121 return false;
5122}
5123
5124
5125/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5126/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5127/// if the load addresses are consecutive, non-overlapping, and in the right
5128/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005129static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5130 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005131 MachineFunction &MF = DAG.getMachineFunction();
5132 MachineFrameInfo *MFI = MF.getFrameInfo();
5133 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00005134 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005135 SDOperand PermMask = N->getOperand(2);
5136 int NumElems = (int)PermMask.getNumOperands();
5137 SDNode *Base = NULL;
5138 for (int i = 0; i < NumElems; ++i) {
5139 SDOperand Idx = PermMask.getOperand(i);
5140 if (Idx.getOpcode() == ISD::UNDEF) {
5141 if (!Base) return SDOperand();
5142 } else {
5143 SDOperand Arg =
5144 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005145 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005146 return SDOperand();
5147 if (!Base)
5148 Base = Arg.Val;
5149 else if (!isConsecutiveLoad(Arg.Val, Base,
5150 i, MVT::getSizeInBits(EVT)/8,MFI))
5151 return SDOperand();
5152 }
5153 }
5154
Evan Cheng1e60c092006-07-10 21:37:44 +00005155 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00005156 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00005157 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00005158 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00005159 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00005160 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00005161 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5162 LD->getSrcValueOffset(), LD->isVolatile(),
5163 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00005164 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005165}
5166
Chris Lattner83e6c992006-10-04 06:57:07 +00005167/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5168static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5169 const X86Subtarget *Subtarget) {
5170 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005171
Chris Lattner83e6c992006-10-04 06:57:07 +00005172 // If we have SSE[12] support, try to form min/max nodes.
5173 if (Subtarget->hasSSE2() &&
5174 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5175 if (Cond.getOpcode() == ISD::SETCC) {
5176 // Get the LHS/RHS of the select.
5177 SDOperand LHS = N->getOperand(1);
5178 SDOperand RHS = N->getOperand(2);
5179 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005180
Evan Cheng8ca29322006-11-10 21:43:37 +00005181 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005182 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005183 switch (CC) {
5184 default: break;
5185 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5186 case ISD::SETULE:
5187 case ISD::SETLE:
5188 if (!UnsafeFPMath) break;
5189 // FALL THROUGH.
5190 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5191 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005192 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005193 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005194
Chris Lattner1907a7b2006-10-05 04:11:26 +00005195 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5196 case ISD::SETUGT:
5197 case ISD::SETGT:
5198 if (!UnsafeFPMath) break;
5199 // FALL THROUGH.
5200 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5201 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005202 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005203 break;
5204 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005205 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005206 switch (CC) {
5207 default: break;
5208 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5209 case ISD::SETUGT:
5210 case ISD::SETGT:
5211 if (!UnsafeFPMath) break;
5212 // FALL THROUGH.
5213 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5214 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005215 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005216 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005217
Chris Lattner1907a7b2006-10-05 04:11:26 +00005218 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5219 case ISD::SETULE:
5220 case ISD::SETLE:
5221 if (!UnsafeFPMath) break;
5222 // FALL THROUGH.
5223 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5224 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005225 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005226 break;
5227 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005228 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005229
Evan Cheng8ca29322006-11-10 21:43:37 +00005230 if (Opcode)
5231 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00005232 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005233
Chris Lattner83e6c992006-10-04 06:57:07 +00005234 }
5235
5236 return SDOperand();
5237}
5238
5239
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005240SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00005241 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005242 SelectionDAG &DAG = DCI.DAG;
5243 switch (N->getOpcode()) {
5244 default: break;
5245 case ISD::VECTOR_SHUFFLE:
Evan Cheng1e60c092006-07-10 21:37:44 +00005246 return PerformShuffleCombine(N, DAG, Subtarget);
Chris Lattner83e6c992006-10-04 06:57:07 +00005247 case ISD::SELECT:
5248 return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005249 }
5250
5251 return SDOperand();
5252}
5253
Evan Cheng60c07e12006-07-05 22:17:51 +00005254//===----------------------------------------------------------------------===//
5255// X86 Inline Assembly Support
5256//===----------------------------------------------------------------------===//
5257
Chris Lattnerf4dff842006-07-11 02:54:03 +00005258/// getConstraintType - Given a constraint letter, return the type of
5259/// constraint it is for this target.
5260X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005261X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5262 if (Constraint.size() == 1) {
5263 switch (Constraint[0]) {
5264 case 'A':
5265 case 'r':
5266 case 'R':
5267 case 'l':
5268 case 'q':
5269 case 'Q':
5270 case 'x':
5271 case 'Y':
5272 return C_RegisterClass;
5273 default:
5274 break;
5275 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00005276 }
Chris Lattner4234f572007-03-25 02:14:49 +00005277 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00005278}
5279
Chris Lattner48884cd2007-08-25 00:47:38 +00005280/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5281/// vector. If it is invalid, don't add anything to Ops.
5282void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5283 char Constraint,
5284 std::vector<SDOperand>&Ops,
5285 SelectionDAG &DAG) {
5286 SDOperand Result(0, 0);
5287
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005288 switch (Constraint) {
5289 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00005290 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00005291 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005292 if (C->getValue() <= 31) {
5293 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5294 break;
5295 }
Devang Patel84f7fd22007-03-17 00:13:28 +00005296 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005297 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00005298 case 'N':
5299 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005300 if (C->getValue() <= 255) {
5301 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5302 break;
5303 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00005304 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005305 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00005306 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005307 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00005308 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5309 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5310 break;
5311 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005312
Chris Lattnerdc43a882007-05-03 16:52:29 +00005313 // If we are in non-pic codegen mode, we allow the address of a global (with
5314 // an optional displacement) to be used with 'i'.
5315 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5316 int64_t Offset = 0;
5317
5318 // Match either (GA) or (GA+C)
5319 if (GA) {
5320 Offset = GA->getOffset();
5321 } else if (Op.getOpcode() == ISD::ADD) {
5322 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5323 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5324 if (C && GA) {
5325 Offset = GA->getOffset()+C->getValue();
5326 } else {
5327 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5328 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5329 if (C && GA)
5330 Offset = GA->getOffset()+C->getValue();
5331 else
5332 C = 0, GA = 0;
5333 }
5334 }
5335
5336 if (GA) {
5337 // If addressing this global requires a load (e.g. in PIC mode), we can't
5338 // match.
5339 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5340 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00005341 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005342
Chris Lattnerdc43a882007-05-03 16:52:29 +00005343 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5344 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00005345 Result = Op;
5346 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005347 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005348
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005349 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00005350 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005351 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00005352 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005353
5354 if (Result.Val) {
5355 Ops.push_back(Result);
5356 return;
5357 }
5358 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005359}
5360
Chris Lattner259e97c2006-01-31 19:43:35 +00005361std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00005362getRegClassForInlineAsmConstraint(const std::string &Constraint,
5363 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00005364 if (Constraint.size() == 1) {
5365 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00005366 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00005367 default: break; // Unknown constraint letter
5368 case 'A': // EAX/EDX
5369 if (VT == MVT::i32 || VT == MVT::i64)
5370 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5371 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005372 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5373 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005374 if (VT == MVT::i32)
5375 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5376 else if (VT == MVT::i16)
5377 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5378 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00005379 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005380 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005381 }
5382 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005383
Chris Lattner1efa40f2006-02-22 00:56:39 +00005384 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00005385}
Chris Lattnerf76d1802006-07-31 23:26:50 +00005386
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005387std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00005388X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5389 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00005390 // First, see if this is a constraint that directly corresponds to an LLVM
5391 // register class.
5392 if (Constraint.size() == 1) {
5393 // GCC Constraint Letters
5394 switch (Constraint[0]) {
5395 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005396 case 'r': // GENERAL_REGS
5397 case 'R': // LEGACY_REGS
5398 case 'l': // INDEX_REGS
5399 if (VT == MVT::i64 && Subtarget->is64Bit())
5400 return std::make_pair(0U, X86::GR64RegisterClass);
5401 if (VT == MVT::i32)
5402 return std::make_pair(0U, X86::GR32RegisterClass);
5403 else if (VT == MVT::i16)
5404 return std::make_pair(0U, X86::GR16RegisterClass);
5405 else if (VT == MVT::i8)
5406 return std::make_pair(0U, X86::GR8RegisterClass);
5407 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00005408 case 'y': // MMX_REGS if MMX allowed.
5409 if (!Subtarget->hasMMX()) break;
5410 return std::make_pair(0U, X86::VR64RegisterClass);
5411 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005412 case 'Y': // SSE_REGS if SSE2 allowed
5413 if (!Subtarget->hasSSE2()) break;
5414 // FALL THROUGH.
5415 case 'x': // SSE_REGS if SSE1 allowed
5416 if (!Subtarget->hasSSE1()) break;
5417
5418 switch (VT) {
5419 default: break;
5420 // Scalar SSE types.
5421 case MVT::f32:
5422 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00005423 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005424 case MVT::f64:
5425 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00005426 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005427 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00005428 case MVT::v16i8:
5429 case MVT::v8i16:
5430 case MVT::v4i32:
5431 case MVT::v2i64:
5432 case MVT::v4f32:
5433 case MVT::v2f64:
5434 return std::make_pair(0U, X86::VR128RegisterClass);
5435 }
Chris Lattnerad043e82007-04-09 05:11:28 +00005436 break;
5437 }
5438 }
5439
Chris Lattnerf76d1802006-07-31 23:26:50 +00005440 // Use the default implementation in TargetLowering to convert the register
5441 // constraint into a member of a register class.
5442 std::pair<unsigned, const TargetRegisterClass*> Res;
5443 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00005444
5445 // Not found as a standard register?
5446 if (Res.second == 0) {
5447 // GCC calls "st(0)" just plain "st".
5448 if (StringsEqualNoCase("{st}", Constraint)) {
5449 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00005450 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00005451 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005452
Chris Lattner1a60aa72006-10-31 19:42:44 +00005453 return Res;
5454 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005455
Chris Lattnerf76d1802006-07-31 23:26:50 +00005456 // Otherwise, check to see if this is a register class of the wrong value
5457 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
5458 // turn into {ax},{dx}.
5459 if (Res.second->hasType(VT))
5460 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005461
Chris Lattnerf76d1802006-07-31 23:26:50 +00005462 // All of the single-register GCC register classes map their values onto
5463 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
5464 // really want an 8-bit or 32-bit register, map to the appropriate register
5465 // class and return the appropriate register.
5466 if (Res.second != X86::GR16RegisterClass)
5467 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005468
Chris Lattnerf76d1802006-07-31 23:26:50 +00005469 if (VT == MVT::i8) {
5470 unsigned DestReg = 0;
5471 switch (Res.first) {
5472 default: break;
5473 case X86::AX: DestReg = X86::AL; break;
5474 case X86::DX: DestReg = X86::DL; break;
5475 case X86::CX: DestReg = X86::CL; break;
5476 case X86::BX: DestReg = X86::BL; break;
5477 }
5478 if (DestReg) {
5479 Res.first = DestReg;
5480 Res.second = Res.second = X86::GR8RegisterClass;
5481 }
5482 } else if (VT == MVT::i32) {
5483 unsigned DestReg = 0;
5484 switch (Res.first) {
5485 default: break;
5486 case X86::AX: DestReg = X86::EAX; break;
5487 case X86::DX: DestReg = X86::EDX; break;
5488 case X86::CX: DestReg = X86::ECX; break;
5489 case X86::BX: DestReg = X86::EBX; break;
5490 case X86::SI: DestReg = X86::ESI; break;
5491 case X86::DI: DestReg = X86::EDI; break;
5492 case X86::BP: DestReg = X86::EBP; break;
5493 case X86::SP: DestReg = X86::ESP; break;
5494 }
5495 if (DestReg) {
5496 Res.first = DestReg;
5497 Res.second = Res.second = X86::GR32RegisterClass;
5498 }
Evan Cheng25ab6902006-09-08 06:48:29 +00005499 } else if (VT == MVT::i64) {
5500 unsigned DestReg = 0;
5501 switch (Res.first) {
5502 default: break;
5503 case X86::AX: DestReg = X86::RAX; break;
5504 case X86::DX: DestReg = X86::RDX; break;
5505 case X86::CX: DestReg = X86::RCX; break;
5506 case X86::BX: DestReg = X86::RBX; break;
5507 case X86::SI: DestReg = X86::RSI; break;
5508 case X86::DI: DestReg = X86::RDI; break;
5509 case X86::BP: DestReg = X86::RBP; break;
5510 case X86::SP: DestReg = X86::RSP; break;
5511 }
5512 if (DestReg) {
5513 Res.first = DestReg;
5514 Res.second = Res.second = X86::GR64RegisterClass;
5515 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00005516 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005517
Chris Lattnerf76d1802006-07-31 23:26:50 +00005518 return Res;
5519}