blob: f99b14788cee10e5ae221652ee53c8817ba7a6f5 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
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 Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.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"
Evan Chenga844bde2008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000040#include "llvm/ADT/StringExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041using namespace llvm;
42
Evan Cheng10e86422008-04-25 19:11:04 +000043// Forward declarations.
Dan Gohman475871a2008-07-27 21:46:04 +000044static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng10e86422008-04-25 19:11:04 +000045
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000046X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000047 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000048 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000049 X86ScalarSSEf64 = Subtarget->hasSSE2();
50 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000051 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000052
Chris Lattnerd43d00c2008-01-24 08:07:48 +000053 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000054
Anton Korobeynikov2365f512007-07-14 14:06:15 +000055 RegInfo = TM.getRegisterInfo();
56
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000057 // Set up the TargetLowering object.
58
59 // X86 is weird, it always uses i8 for shift amounts and setcc results.
60 setShiftAmountType(MVT::i8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000061 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000062 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000063 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000064 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000065
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000066 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000067 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000068 setUseUnderscoreSetJmp(false);
69 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000070 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000071 // MS runtime is weird: it exports _setjmp, but longjmp!
72 setUseUnderscoreSetJmp(true);
73 setUseUnderscoreLongJmp(false);
74 } else {
75 setUseUnderscoreSetJmp(true);
76 setUseUnderscoreLongJmp(true);
77 }
78
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000079 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000080 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
81 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
82 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000083 if (Subtarget->is64Bit())
84 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000085
Duncan Sandsf9c98e62008-01-23 20:39:46 +000086 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000087
Chris Lattnerddf89562008-01-17 19:59:44 +000088 // We don't accept any truncstore of integer registers.
89 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
90 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
93 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
94 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
95
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000096 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
97 // operation.
98 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
99 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000101
Evan Cheng25ab6902006-09-08 06:48:29 +0000102 if (Subtarget->is64Bit()) {
103 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +0000104 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000105 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000106 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +0000107 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
109 else
110 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
111 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000112
113 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
114 // this operation.
115 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
116 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000117 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000118 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000119 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000120 // f32 and f64 cases are Legal, f80 case is not
121 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
122 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000123 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
124 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
125 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126
Dale Johannesen73328d12007-09-19 23:55:34 +0000127 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
128 // are Legal, f80 is custom lowered.
129 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000131
Evan Cheng02568ff2006-01-30 22:13:22 +0000132 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
133 // this operation.
134 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
135 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
136
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000137 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000138 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000139 // f32 and f64 cases are Legal, f80 case is not
140 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000141 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000142 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000143 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000144 }
145
146 // Handle FP_TO_UINT by promoting the destination to a larger signed
147 // conversion.
148 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
149 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
151
Evan Cheng25ab6902006-09-08 06:48:29 +0000152 if (Subtarget->is64Bit()) {
153 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000154 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000156 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000157 // Expand FP_TO_UINT into a select.
158 // FIXME: We would like to use a Custom expander here eventually to do
159 // the optimal thing for SSE vs. the default expansion in the legalizer.
160 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
161 else
162 // With SSE3 we can use fisttpll to convert to a signed i64.
163 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
164 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165
Chris Lattner399610a2006-12-05 18:22:22 +0000166 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000167 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000168 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
169 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
170 }
Chris Lattner21f66852005-12-23 05:15:23 +0000171
Dan Gohmanb00ee212008-02-18 19:34:53 +0000172 // Scalar integer divide and remainder are lowered to use operations that
173 // produce two results, to match the available instructions. This exposes
174 // the two-result form to trivial CSE, which is able to combine x/y and x%y
175 // into a single instruction.
176 //
177 // Scalar integer multiply-high is also lowered to use two-result
178 // operations, to match the available instructions. However, plain multiply
179 // (low) operations are left as Legal, as there are single-result
180 // instructions for this in x86. Using the two-result multiply instructions
181 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000182 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
183 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
184 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
185 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::SREM , MVT::i8 , Expand);
187 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000188 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
189 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
190 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
191 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::SREM , MVT::i16 , Expand);
193 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000194 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
195 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
196 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
197 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::SREM , MVT::i32 , Expand);
199 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000200 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
201 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
202 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::SREM , MVT::i64 , Expand);
205 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000206
Evan Chengc35497f2006-10-30 08:02:39 +0000207 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000208 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000209 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
210 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000211 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000212 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
216 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000217 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000218 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000219 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000220 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000221
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000222 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000223 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
224 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000225 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000226 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
227 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000228 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000229 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
230 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000231 if (Subtarget->is64Bit()) {
232 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000233 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
234 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000235 }
236
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000237 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000238 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000239
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000240 // These should be promoted to a larger select which is supported.
241 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
242 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000243 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000244 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
245 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
246 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000248 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000249 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
250 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
252 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000254 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000255 if (Subtarget->is64Bit()) {
256 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
257 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
258 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000259 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000260 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000261 if (!Subtarget->is64Bit())
262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
263
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000264 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000269 if (Subtarget->is64Bit())
270 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
277 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000278 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000279 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Evan Chengd2cde682008-03-10 19:38:10 +0000288 if (Subtarget->hasSSE1())
289 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000290
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000291 if (!Subtarget->hasSSE2())
292 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293
Mon P Wang63307c32008-05-05 19:05:59 +0000294 // Expand certain atomics
Mon P Wang28873102008-06-25 08:15:39 +0000295 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i64, Custom);
Andrew Lenharth08ca62b2008-08-03 20:17:34 +0000299 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i8, Expand);
300 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i16, Expand);
Mon P Wang28873102008-06-25 08:15:39 +0000301 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i32, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000302
Dan Gohman7f460202008-06-30 20:59:49 +0000303 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
304 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000305 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000306 if (!Subtarget->isTargetDarwin() &&
307 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000308 !Subtarget->isTargetCygMing()) {
309 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
310 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
311 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000312
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000313 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
314 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
315 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
316 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
317 if (Subtarget->is64Bit()) {
318 // FIXME: Verify
319 setExceptionPointerRegister(X86::RAX);
320 setExceptionSelectorRegister(X86::RDX);
321 } else {
322 setExceptionPointerRegister(X86::EAX);
323 setExceptionSelectorRegister(X86::EDX);
324 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000325 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000326
Duncan Sandsf7331b32007-09-11 14:10:23 +0000327 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000328
Chris Lattnerda68d302008-01-15 21:58:22 +0000329 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000330
Nate Begemanacc398c2006-01-25 18:21:52 +0000331 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
332 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000333 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000334 if (Subtarget->is64Bit()) {
335 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000336 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000337 } else {
338 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000339 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000340 }
Evan Chengae642192007-03-02 23:16:35 +0000341
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000342 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000343 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000344 if (Subtarget->is64Bit())
345 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000346 if (Subtarget->isTargetCygMing())
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
348 else
349 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000350
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000351 if (X86ScalarSSEf64) {
352 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000353 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000354 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
355 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000356
Evan Cheng223547a2006-01-31 22:28:30 +0000357 // Use ANDPD to simulate FABS.
358 setOperationAction(ISD::FABS , MVT::f64, Custom);
359 setOperationAction(ISD::FABS , MVT::f32, Custom);
360
361 // Use XORP to simulate FNEG.
362 setOperationAction(ISD::FNEG , MVT::f64, Custom);
363 setOperationAction(ISD::FNEG , MVT::f32, Custom);
364
Evan Cheng68c47cb2007-01-05 07:55:56 +0000365 // Use ANDPD and ORPD to simulate FCOPYSIGN.
366 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
367 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
368
Evan Chengd25e9e82006-02-02 00:28:23 +0000369 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000370 setOperationAction(ISD::FSIN , MVT::f64, Expand);
371 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000372 setOperationAction(ISD::FSIN , MVT::f32, Expand);
373 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000374
Chris Lattnera54aa942006-01-29 06:26:08 +0000375 // Expand FP immediates into loads from the stack, except for the special
376 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000377 addLegalFPImmediate(APFloat(+0.0)); // xorpd
378 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000379
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000380 // Floating truncations from f80 and extensions to f80 go through memory.
381 // If optimizing, we lie about this though and handle it in
382 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
383 if (Fast) {
384 setConvertAction(MVT::f32, MVT::f80, Expand);
385 setConvertAction(MVT::f64, MVT::f80, Expand);
386 setConvertAction(MVT::f80, MVT::f32, Expand);
387 setConvertAction(MVT::f80, MVT::f64, Expand);
388 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000389 } else if (X86ScalarSSEf32) {
390 // Use SSE for f32, x87 for f64.
391 // Set up the FP register classes.
392 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
393 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
394
395 // Use ANDPS to simulate FABS.
396 setOperationAction(ISD::FABS , MVT::f32, Custom);
397
398 // Use XORP to simulate FNEG.
399 setOperationAction(ISD::FNEG , MVT::f32, Custom);
400
401 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
402
403 // Use ANDPS and ORPS to simulate FCOPYSIGN.
404 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
405 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
406
407 // We don't support sin/cos/fmod
408 setOperationAction(ISD::FSIN , MVT::f32, Expand);
409 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000410
Nate Begemane1795842008-02-14 08:57:00 +0000411 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000412 addLegalFPImmediate(APFloat(+0.0f)); // xorps
413 addLegalFPImmediate(APFloat(+0.0)); // FLD0
414 addLegalFPImmediate(APFloat(+1.0)); // FLD1
415 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
416 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
417
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000418 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
419 // this though and handle it in InstructionSelectPreprocess so that
420 // dagcombine2 can hack on these.
421 if (Fast) {
422 setConvertAction(MVT::f32, MVT::f64, Expand);
423 setConvertAction(MVT::f32, MVT::f80, Expand);
424 setConvertAction(MVT::f80, MVT::f32, Expand);
425 setConvertAction(MVT::f64, MVT::f32, Expand);
426 // And x87->x87 truncations also.
427 setConvertAction(MVT::f80, MVT::f64, Expand);
428 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000429
430 if (!UnsafeFPMath) {
431 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
432 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
433 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000434 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000435 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000436 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000437 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
438 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000439
Evan Cheng68c47cb2007-01-05 07:55:56 +0000440 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000441 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000442 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
443 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000444
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000445 // Floating truncations go through memory. If optimizing, we lie about
446 // this though and handle it in InstructionSelectPreprocess so that
447 // dagcombine2 can hack on these.
448 if (Fast) {
449 setConvertAction(MVT::f80, MVT::f32, Expand);
450 setConvertAction(MVT::f64, MVT::f32, Expand);
451 setConvertAction(MVT::f80, MVT::f64, Expand);
452 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000453
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000454 if (!UnsafeFPMath) {
455 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
456 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
457 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000458 addLegalFPImmediate(APFloat(+0.0)); // FLD0
459 addLegalFPImmediate(APFloat(+1.0)); // FLD1
460 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
461 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000462 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
463 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
464 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
465 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000466 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000467
Dale Johannesen59a58732007-08-05 18:49:15 +0000468 // Long double always uses X87.
469 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000470 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
471 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000472 {
Chris Lattner71d07a02008-01-27 06:19:31 +0000473 APFloat TmpFlt(+0.0);
474 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
475 addLegalFPImmediate(TmpFlt); // FLD0
476 TmpFlt.changeSign();
477 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
478 APFloat TmpFlt2(+1.0);
479 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
480 addLegalFPImmediate(TmpFlt2); // FLD1
481 TmpFlt2.changeSign();
482 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
483 }
484
Dale Johannesen2f429012007-09-26 21:10:55 +0000485 if (!UnsafeFPMath) {
486 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
487 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
488 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000489
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000490 // Always use a library call for pow.
491 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
492 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
493 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
494
Evan Chengd30bf012006-03-01 01:11:20 +0000495 // First set operation action for all vector types to expand. Then we
496 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000497 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
498 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000499 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
500 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
501 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
502 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
503 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
504 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
505 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
506 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
507 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000537 }
538
Evan Chenga88973f2006-03-22 19:22:18 +0000539 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000540 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
541 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
542 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000543 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000544 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000545
Evan Chengd30bf012006-03-01 01:11:20 +0000546 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000547
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000548 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
549 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
550 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000551 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000552
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000553 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
554 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
555 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000556 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000557
Bill Wendling74027e92007-03-15 21:24:36 +0000558 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
559 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
560
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000561 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000562 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000563 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000564 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
565 setOperationAction(ISD::AND, MVT::v2i32, Promote);
566 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
567 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000568
569 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000570 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000571 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000572 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
573 setOperationAction(ISD::OR, MVT::v2i32, Promote);
574 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
575 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000576
577 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000578 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000579 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000580 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
581 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
582 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
583 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000584
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000585 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000586 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000587 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000588 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
589 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
590 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000591 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
592 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000593 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000594
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000595 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
596 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
597 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000598 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000599 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000600
601 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
602 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
603 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000604 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000605
Evan Cheng52672b82008-07-22 18:39:19 +0000606 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000607 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
608 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000609 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000610
611 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000612 }
613
Evan Chenga88973f2006-03-22 19:22:18 +0000614 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000615 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
616
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000617 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
618 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
619 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
620 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000621 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
622 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000623 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
624 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
625 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000626 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000627 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000628 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000629 }
630
Evan Chenga88973f2006-03-22 19:22:18 +0000631 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000632 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
633 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
634 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
635 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
636 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
637
Evan Chengf7c378e2006-04-10 07:23:14 +0000638 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
639 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
640 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000641 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000642 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
643 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
644 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000645 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000646 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000647 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
648 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
649 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
650 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000651 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
652 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000653
Nate Begeman30a0de92008-07-17 16:51:19 +0000654 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
655 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
656 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
657 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000658
Evan Chengf7c378e2006-04-10 07:23:14 +0000659 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
660 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000661 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000662 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000663 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000664
Evan Cheng2c3ae372006-04-12 21:21:57 +0000665 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000666 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
667 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000668 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000669 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000670 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000671 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
672 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
673 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000674 }
675 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
676 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
677 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
678 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000679 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000680 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000681 if (Subtarget->is64Bit()) {
682 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000683 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000684 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000685
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000686 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000687 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000688 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
689 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
690 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
691 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
692 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
693 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
694 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
695 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
696 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
697 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000698 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000699
Chris Lattnerddf89562008-01-17 19:59:44 +0000700 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000701
Evan Cheng2c3ae372006-04-12 21:21:57 +0000702 // Custom lower v2i64 and v2f64 selects.
703 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000704 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000705 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000706 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000707
Evan Cheng470a6ad2006-02-22 02:26:30 +0000708 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000709
710 if (Subtarget->hasSSE41()) {
711 // FIXME: Do we need to handle scalar-to-vector here?
712 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohman0b924dc2008-05-23 17:49:40 +0000713 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000714
715 // i8 and i16 vectors are custom , because the source register and source
716 // source memory operand types are not the same width. f32 vectors are
717 // custom since the immediate controlling the insert encodes additional
718 // information.
719 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
720 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
721 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
722 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
723
724 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
725 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
726 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng62a3f152008-03-24 21:52:23 +0000727 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000728
729 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000730 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
731 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000732 }
733 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000734
Nate Begeman30a0de92008-07-17 16:51:19 +0000735 if (Subtarget->hasSSE42()) {
736 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
737 }
738
Evan Cheng6be2c582006-04-05 23:38:46 +0000739 // We want to custom lower some of our intrinsics.
740 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
741
Evan Cheng206ee9d2006-07-07 08:33:52 +0000742 // We have target-specific dag combine patterns for the following nodes:
743 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000744 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000745 setTargetDAGCombine(ISD::SELECT);
Chris Lattner149a4e52008-02-22 02:09:43 +0000746 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000747
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000748 computeRegisterProperties();
749
Evan Cheng87ed7162006-02-14 08:25:08 +0000750 // FIXME: These should be based on subtarget info. Plus, the values should
751 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000752 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
753 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
754 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000755 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000756 setPrefLoopAlignment(16);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000757}
758
Scott Michel5b8f82e2008-03-10 15:42:14 +0000759
Dan Gohman475871a2008-07-27 21:46:04 +0000760MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000761 return MVT::i8;
762}
763
764
Evan Cheng29286502008-01-23 23:17:41 +0000765/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
766/// the desired ByVal argument alignment.
767static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
768 if (MaxAlign == 16)
769 return;
770 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
771 if (VTy->getBitWidth() == 128)
772 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000773 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
774 unsigned EltAlign = 0;
775 getMaxByValAlign(ATy->getElementType(), EltAlign);
776 if (EltAlign > MaxAlign)
777 MaxAlign = EltAlign;
778 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
779 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
780 unsigned EltAlign = 0;
781 getMaxByValAlign(STy->getElementType(i), EltAlign);
782 if (EltAlign > MaxAlign)
783 MaxAlign = EltAlign;
784 if (MaxAlign == 16)
785 break;
786 }
787 }
788 return;
789}
790
791/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
792/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000793/// that contain SSE vectors are placed at 16-byte boundaries while the rest
794/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000795unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
796 if (Subtarget->is64Bit())
797 return getTargetData()->getABITypeAlignment(Ty);
798 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000799 if (Subtarget->hasSSE1())
800 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000801 return Align;
802}
Chris Lattner2b02a442007-02-25 08:29:00 +0000803
Evan Chengf0df0312008-05-15 08:39:06 +0000804/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000805/// and store operations as a result of memset, memcpy, and memmove
806/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000807/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000808MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000809X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
810 bool isSrcConst, bool isSrcStr) const {
811 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
812 return MVT::v4i32;
813 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
814 return MVT::v4f32;
815 if (Subtarget->is64Bit() && Size >= 8)
816 return MVT::i64;
817 return MVT::i32;
818}
819
820
Evan Chengcc415862007-11-09 01:32:10 +0000821/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
822/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000823SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000824 SelectionDAG &DAG) const {
825 if (usesGlobalOffsetTable())
826 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
827 if (!Subtarget->isPICStyleRIPRel())
828 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
829 return Table;
830}
831
Chris Lattner2b02a442007-02-25 08:29:00 +0000832//===----------------------------------------------------------------------===//
833// Return Value Calling Convention Implementation
834//===----------------------------------------------------------------------===//
835
Chris Lattner59ed56b2007-02-28 04:55:35 +0000836#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000837
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000838/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000839SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000840 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
841
Chris Lattner9774c912007-02-27 05:28:59 +0000842 SmallVector<CCValAssign, 16> RVLocs;
843 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000844 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
845 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000846 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000847
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000848 // If this is the first return lowered for this function, add the regs to the
849 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000850 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000851 for (unsigned i = 0; i != RVLocs.size(); ++i)
852 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000853 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000854 }
Dan Gohman475871a2008-07-27 21:46:04 +0000855 SDValue Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000856
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000857 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000858 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000859 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000860 SDValue TailCall = Chain;
861 SDValue TargetAddress = TailCall.getOperand(1);
862 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000863 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000864 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
865 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
866 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
867 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
868 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000869 assert(StackAdjustment.getOpcode() == ISD::Constant &&
870 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000871
Dan Gohman475871a2008-07-27 21:46:04 +0000872 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000873 Operands.push_back(Chain.getOperand(0));
874 Operands.push_back(TargetAddress);
875 Operands.push_back(StackAdjustment);
876 // Copy registers used by the call. Last operand is a flag so it is not
877 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000878 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000879 Operands.push_back(Chain.getOperand(i));
880 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000881 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
882 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000883 }
884
885 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000886 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000887
Dan Gohman475871a2008-07-27 21:46:04 +0000888 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000889 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
890 // Operand #1 = Bytes To Pop
891 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
892
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000893 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000894 for (unsigned i = 0; i != RVLocs.size(); ++i) {
895 CCValAssign &VA = RVLocs[i];
896 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000897 SDValue ValToCopy = Op.getOperand(i*2+1);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000898
Chris Lattner447ff682008-03-11 03:23:40 +0000899 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
900 // the RET instruction and handled by the FP Stackifier.
901 if (RVLocs[i].getLocReg() == X86::ST0 ||
902 RVLocs[i].getLocReg() == X86::ST1) {
903 // If this is a copy from an xmm register to ST(0), use an FPExtend to
904 // change the value to the FP stack register class.
905 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
906 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
907 RetOps.push_back(ValToCopy);
908 // Don't emit a copytoreg.
909 continue;
910 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000911
Chris Lattner8e6da152008-03-10 21:08:41 +0000912 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000913 Flag = Chain.getValue(1);
914 }
Dan Gohman61a92132008-04-21 23:59:07 +0000915
916 // The x86-64 ABI for returning structs by value requires that we copy
917 // the sret argument into %rax for the return. We saved the argument into
918 // a virtual register in the entry block, so now we copy the value out
919 // and into %rax.
920 if (Subtarget->is64Bit() &&
921 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
922 MachineFunction &MF = DAG.getMachineFunction();
923 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
924 unsigned Reg = FuncInfo->getSRetReturnReg();
925 if (!Reg) {
926 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
927 FuncInfo->setSRetReturnReg(Reg);
928 }
Dan Gohman475871a2008-07-27 21:46:04 +0000929 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +0000930
931 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
932 Flag = Chain.getValue(1);
933 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000934
Chris Lattner447ff682008-03-11 03:23:40 +0000935 RetOps[0] = Chain; // Update chain.
936
937 // Add the flag if we have it.
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000938 if (Flag.Val)
Chris Lattner447ff682008-03-11 03:23:40 +0000939 RetOps.push_back(Flag);
940
941 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000942}
943
944
Chris Lattner3085e152007-02-25 08:59:22 +0000945/// LowerCallResult - Lower the result values of an ISD::CALL into the
946/// appropriate copies out of appropriate physical registers. This assumes that
947/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
948/// being lowered. The returns a SDNode with the same number of values as the
949/// ISD::CALL.
950SDNode *X86TargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000951LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +0000952 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000953
954 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000955 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000956 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
957 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000958 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
959
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SmallVector<SDValue, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000961
962 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +0000963 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000964 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattner8e6da152008-03-10 21:08:41 +0000965
966 // If this is a call to a function that returns an fp value on the floating
967 // point stack, but where we prefer to use the value in xmm registers, copy
968 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
969 if (RVLocs[i].getLocReg() == X86::ST0 &&
970 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
971 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +0000972 }
Chris Lattner3085e152007-02-25 08:59:22 +0000973
Chris Lattner8e6da152008-03-10 21:08:41 +0000974 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
975 CopyVT, InFlag).getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +0000976 SDValue Val = Chain.getValue(0);
Chris Lattner8e6da152008-03-10 21:08:41 +0000977 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +0000978
Chris Lattner8e6da152008-03-10 21:08:41 +0000979 if (CopyVT != RVLocs[i].getValVT()) {
980 // Round the F80 the right size, which also moves to the appropriate xmm
981 // register.
982 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
983 // This truncation won't change the value.
984 DAG.getIntPtrConstant(1));
985 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000986
Chris Lattner8e6da152008-03-10 21:08:41 +0000987 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +0000988 }
Duncan Sands4bdcb612008-07-02 17:40:58 +0000989
Chris Lattner3085e152007-02-25 08:59:22 +0000990 // Merge everything together with a MERGE_VALUES node.
991 ResultVals.push_back(Chain);
Duncan Sandsf9516202008-06-30 10:19:09 +0000992 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
993 ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000994}
995
996
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000997//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000998// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000999//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001000// StdCall calling convention seems to be standard for many Windows' API
1001// routines and around. It differs from C calling convention just a little:
1002// callee should clean up the stack, not caller. Symbols should be also
1003// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001004// For info on fast calling convention see Fast Calling Convention (tail call)
1005// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001006
Evan Cheng85e38002006-04-27 05:35:28 +00001007/// AddLiveIn - This helper function adds the specified physical register to the
1008/// MachineFunction as a live in value. It also creates a corresponding virtual
1009/// register for it.
1010static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001011 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +00001012 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +00001013 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1014 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +00001015 return VReg;
1016}
1017
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001018/// CallIsStructReturn - Determines whether a CALL node uses struct return
1019/// semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001020static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001021 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1022 if (!NumOps)
1023 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001024
1025 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001026}
1027
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001028/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1029/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001030static bool ArgsAreStructReturn(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001031 unsigned NumArgs = Op.Val->getNumValues() - 1;
1032 if (!NumArgs)
1033 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001034
1035 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001036}
1037
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001038/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1039/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001040/// calls.
Dan Gohman475871a2008-07-27 21:46:04 +00001041bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001042 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1043 if (IsVarArg)
1044 return false;
1045
1046 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1047 default:
1048 return false;
1049 case CallingConv::X86_StdCall:
1050 return !Subtarget->is64Bit();
1051 case CallingConv::X86_FastCall:
1052 return !Subtarget->is64Bit();
1053 case CallingConv::Fast:
1054 return PerformTailCallOpt;
1055 }
1056}
1057
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001058/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1059/// FORMAL_ARGUMENTS node.
Dan Gohman475871a2008-07-27 21:46:04 +00001060CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen86737662008-01-05 16:56:59 +00001061 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1062
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001063 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001064 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001065 return CC_X86_Win64_C;
1066 else {
1067 if (CC == CallingConv::Fast && PerformTailCallOpt)
1068 return CC_X86_64_TailCall;
1069 else
1070 return CC_X86_64_C;
1071 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001072 }
1073
Gordon Henriksen86737662008-01-05 16:56:59 +00001074 if (CC == CallingConv::X86_FastCall)
1075 return CC_X86_32_FastCall;
1076 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1077 return CC_X86_32_TailCall;
1078 else
1079 return CC_X86_32_C;
1080}
1081
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001082/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1083/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001084NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001085X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001086 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1087 if (CC == CallingConv::X86_FastCall)
1088 return FastCall;
1089 else if (CC == CallingConv::X86_StdCall)
1090 return StdCall;
1091 return None;
1092}
1093
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001094
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001095/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1096/// in a register before calling.
1097bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1098 return !IsTailCall && !Is64Bit &&
1099 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1100 Subtarget->isPICStyleGOT();
1101}
1102
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001103/// CallRequiresFnAddressInReg - Check whether the call requires the function
1104/// address to be loaded in a register.
1105bool
1106X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1107 return !Is64Bit && IsTailCall &&
1108 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1109 Subtarget->isPICStyleGOT();
1110}
1111
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001112/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1113/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001114/// the specific parameter attribute. The copy will be passed as a byval
1115/// function parameter.
Dan Gohman475871a2008-07-27 21:46:04 +00001116static SDValue
1117CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001118 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001119 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohman707e0182008-04-12 04:36:06 +00001120 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001121 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001122}
1123
Dan Gohman475871a2008-07-27 21:46:04 +00001124SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001125 const CCValAssign &VA,
1126 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001127 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001128 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001129 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001130 ISD::ArgFlagsTy Flags =
1131 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001132 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001133 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001134
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001135 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1136 // changed with more analysis.
1137 // In case of tail call optimization mark all arguments mutable. Since they
1138 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001139 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001140 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001141 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001142 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001143 return FIN;
Dan Gohman69de1932008-02-06 22:27:42 +00001144 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001145 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001146}
1147
Dan Gohman475871a2008-07-27 21:46:04 +00001148SDValue
1149X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001150 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001151 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1152
1153 const Function* Fn = MF.getFunction();
1154 if (Fn->hasExternalLinkage() &&
1155 Subtarget->isTargetCygMing() &&
1156 Fn->getName() == "main")
1157 FuncInfo->setForceFramePointer(true);
1158
1159 // Decorate the function name.
1160 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1161
Evan Cheng1bc78042006-04-26 01:20:17 +00001162 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001163 SDValue Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001164 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001165 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001166 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001167 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001168
1169 assert(!(isVarArg && CC == CallingConv::Fast) &&
1170 "Var args not supported with calling convention fastcc");
1171
Chris Lattner638402b2007-02-28 07:00:42 +00001172 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001173 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001174 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen86737662008-01-05 16:56:59 +00001175 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001176
Dan Gohman475871a2008-07-27 21:46:04 +00001177 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001178 unsigned LastVal = ~0U;
1179 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1180 CCValAssign &VA = ArgLocs[i];
1181 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1182 // places.
1183 assert(VA.getValNo() != LastVal &&
1184 "Don't support value assigned to multiple locs yet");
1185 LastVal = VA.getValNo();
1186
1187 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001188 MVT RegVT = VA.getLocVT();
Chris Lattnerf39f7712007-02-28 05:46:49 +00001189 TargetRegisterClass *RC;
1190 if (RegVT == MVT::i32)
1191 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001192 else if (Is64Bit && RegVT == MVT::i64)
1193 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001194 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001195 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001196 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001197 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001198 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001199 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001200 else if (RegVT.isVector()) {
1201 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001202 if (!Is64Bit)
1203 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1204 else {
1205 // Darwin calling convention passes MMX values in either GPRs or
1206 // XMMs in x86-64. Other targets pass them in memory.
1207 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1208 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1209 RegVT = MVT::v2i64;
1210 } else {
1211 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1212 RegVT = MVT::i64;
1213 }
1214 }
1215 } else {
1216 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001217 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001218
Chris Lattner82932a52007-03-02 05:12:29 +00001219 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman475871a2008-07-27 21:46:04 +00001220 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001221
1222 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1223 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1224 // right size.
1225 if (VA.getLocInfo() == CCValAssign::SExt)
1226 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1227 DAG.getValueType(VA.getValVT()));
1228 else if (VA.getLocInfo() == CCValAssign::ZExt)
1229 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1230 DAG.getValueType(VA.getValVT()));
1231
1232 if (VA.getLocInfo() != CCValAssign::Full)
1233 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1234
Gordon Henriksen86737662008-01-05 16:56:59 +00001235 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001236 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001237 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Cheng44c0fd12008-04-25 20:13:28 +00001238 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1239 else if (RC == X86::VR128RegisterClass) {
1240 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1241 DAG.getConstant(0, MVT::i64));
1242 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1243 }
1244 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001245
Chris Lattnerf39f7712007-02-28 05:46:49 +00001246 ArgValues.push_back(ArgValue);
1247 } else {
1248 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001249 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001250 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001251 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001252
Dan Gohman61a92132008-04-21 23:59:07 +00001253 // The x86-64 ABI for returning structs by value requires that we copy
1254 // the sret argument into %rax for the return. Save the argument into
1255 // a virtual register so that we can access it from the return points.
1256 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1257 MachineFunction &MF = DAG.getMachineFunction();
1258 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1259 unsigned Reg = FuncInfo->getSRetReturnReg();
1260 if (!Reg) {
1261 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1262 FuncInfo->setSRetReturnReg(Reg);
1263 }
Dan Gohman475871a2008-07-27 21:46:04 +00001264 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohman61a92132008-04-21 23:59:07 +00001265 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1266 }
1267
Chris Lattnerf39f7712007-02-28 05:46:49 +00001268 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001269 // align stack specially for tail calls
Gordon Henriksenae636f82008-01-03 16:47:34 +00001270 if (CC == CallingConv::Fast)
1271 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001272
Evan Cheng1bc78042006-04-26 01:20:17 +00001273 // If the function takes variable number of arguments, make a frame index for
1274 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001275 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001276 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1277 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1278 }
1279 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001280 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1281
1282 // FIXME: We should really autogenerate these arrays
1283 static const unsigned GPR64ArgRegsWin64[] = {
1284 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001285 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001286 static const unsigned XMMArgRegsWin64[] = {
1287 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1288 };
1289 static const unsigned GPR64ArgRegs64Bit[] = {
1290 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1291 };
1292 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001293 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1294 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1295 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001296 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1297
1298 if (IsWin64) {
1299 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1300 GPR64ArgRegs = GPR64ArgRegsWin64;
1301 XMMArgRegs = XMMArgRegsWin64;
1302 } else {
1303 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1304 GPR64ArgRegs = GPR64ArgRegs64Bit;
1305 XMMArgRegs = XMMArgRegs64Bit;
1306 }
1307 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1308 TotalNumIntRegs);
1309 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1310 TotalNumXMMRegs);
1311
Gordon Henriksen86737662008-01-05 16:56:59 +00001312 // For X86-64, if there are vararg parameters that are passed via
1313 // registers, then we must store them to their spots on the stack so they
1314 // may be loaded by deferencing the result of va_next.
1315 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001316 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1317 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1318 TotalNumXMMRegs * 16, 16);
1319
Gordon Henriksen86737662008-01-05 16:56:59 +00001320 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001321 SmallVector<SDValue, 8> MemOps;
1322 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1323 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001324 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001325 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001326 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1327 X86::GR64RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001328 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1329 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001330 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001331 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001332 MemOps.push_back(Store);
1333 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001334 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001335 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001336
Gordon Henriksen86737662008-01-05 16:56:59 +00001337 // Now store the XMM (fp + vector) parameter registers.
1338 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001339 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001340 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001341 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1342 X86::VR128RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001343 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1344 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001345 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001346 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001347 MemOps.push_back(Store);
1348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001350 }
1351 if (!MemOps.empty())
1352 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1353 &MemOps[0], MemOps.size());
1354 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001355 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001356
1357 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1358 // arguments and the arguments after the retaddr has been pushed are
1359 // aligned.
1360 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1361 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1362 (StackSize & 7) == 0)
1363 StackSize += 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001364
Gordon Henriksenae636f82008-01-03 16:47:34 +00001365 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001366
Gordon Henriksen86737662008-01-05 16:56:59 +00001367 // Some CCs need callee pop.
1368 if (IsCalleePop(Op)) {
1369 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001370 BytesCallerReserves = 0;
1371 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001372 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001373 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001374 if (!Is64Bit && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001375 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001376 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001377 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001378
Gordon Henriksen86737662008-01-05 16:56:59 +00001379 if (!Is64Bit) {
1380 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1381 if (CC == CallingConv::X86_FastCall)
1382 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1383 }
Evan Cheng25caf632006-05-23 21:06:34 +00001384
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001385 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001386
Evan Cheng25caf632006-05-23 21:06:34 +00001387 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +00001388 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1389 ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001390}
1391
Dan Gohman475871a2008-07-27 21:46:04 +00001392SDValue
1393X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1394 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001395 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001396 SDValue Chain,
1397 SDValue Arg) {
Dan Gohman4fdad172008-02-07 16:28:05 +00001398 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001399 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001400 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001401 ISD::ArgFlagsTy Flags =
1402 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1403 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001404 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001405 }
Dan Gohman4fdad172008-02-07 16:28:05 +00001406 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001407 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001408}
1409
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001410/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1411/// optimization is performed and it is required.
Dan Gohman475871a2008-07-27 21:46:04 +00001412SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001413X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001414 SDValue &OutRetAddr,
1415 SDValue Chain,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001416 bool IsTailCall,
1417 bool Is64Bit,
1418 int FPDiff) {
1419 if (!IsTailCall || FPDiff==0) return Chain;
1420
1421 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001422 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001423 OutRetAddr = getReturnAddressFrameIndex(DAG);
1424 // Load the "old" Return address.
1425 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001426 return SDValue(OutRetAddr.Val, 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001427}
1428
1429/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1430/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman475871a2008-07-27 21:46:04 +00001431static SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001432EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001433 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001434 bool Is64Bit, int FPDiff) {
1435 // Store the return address to the appropriate stack slot.
1436 if (!FPDiff) return Chain;
1437 // Calculate the new stack slot for the return address.
1438 int SlotSize = Is64Bit ? 8 : 4;
1439 int NewReturnAddrFI =
1440 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001441 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001442 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001443 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001444 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001445 return Chain;
1446}
1447
Dan Gohman475871a2008-07-27 21:46:04 +00001448SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001449 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00001450 SDValue Chain = Op.getOperand(0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001451 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001452 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen86737662008-01-05 16:56:59 +00001453 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1454 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohman475871a2008-07-27 21:46:04 +00001455 SDValue Callee = Op.getOperand(4);
Gordon Henriksen86737662008-01-05 16:56:59 +00001456 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng0d9e9762008-01-29 19:34:22 +00001457 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001458
1459 assert(!(isVarArg && CC == CallingConv::Fast) &&
1460 "Var args not supported with calling convention fastcc");
1461
Chris Lattner638402b2007-02-28 07:00:42 +00001462 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001463 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001464 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner920c37a2008-03-21 06:50:21 +00001465 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001466
Chris Lattner423c5f42007-02-28 05:31:48 +00001467 // Get a count of how many bytes are to be pushed on the stack.
1468 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001469 if (CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001470 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001471
Gordon Henriksen86737662008-01-05 16:56:59 +00001472 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1473 // arguments and the arguments after the retaddr has been pushed are aligned.
1474 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1475 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1476 (NumBytes & 7) == 0)
1477 NumBytes += 4;
1478
1479 int FPDiff = 0;
1480 if (IsTailCall) {
1481 // Lower arguments at fp - stackoffset + fpdiff.
1482 unsigned NumBytesCallerPushed =
1483 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1484 FPDiff = NumBytesCallerPushed - NumBytes;
1485
1486 // Set the delta of movement of the returnaddr stackslot.
1487 // But only set if delta is greater than previous delta.
1488 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1489 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1490 }
1491
Chris Lattner0bd48932008-01-17 07:00:52 +00001492 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001493
Dan Gohman475871a2008-07-27 21:46:04 +00001494 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001495 // Load return adress for tail calls.
1496 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1497 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001498
Dan Gohman475871a2008-07-27 21:46:04 +00001499 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1500 SmallVector<SDValue, 8> MemOpChains;
1501 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001502
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001503 // Walk the register/memloc assignments, inserting copies/loads. In the case
1504 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001505 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1506 CCValAssign &VA = ArgLocs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00001507 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001508 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1509 getArgFlags().isByVal();
1510
Chris Lattner423c5f42007-02-28 05:31:48 +00001511 // Promote the value if needed.
1512 switch (VA.getLocInfo()) {
1513 default: assert(0 && "Unknown loc info!");
1514 case CCValAssign::Full: break;
1515 case CCValAssign::SExt:
1516 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1517 break;
1518 case CCValAssign::ZExt:
1519 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1520 break;
1521 case CCValAssign::AExt:
1522 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1523 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001524 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001525
1526 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001527 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001528 MVT RegVT = VA.getLocVT();
1529 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001530 switch (VA.getLocReg()) {
1531 default:
1532 break;
1533 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1534 case X86::R8: {
1535 // Special case: passing MMX values in GPR registers.
1536 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1537 break;
1538 }
1539 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1540 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1541 // Special case: passing MMX values in XMM registers.
1542 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1543 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1544 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1545 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1546 getMOVLMask(2, DAG));
1547 break;
1548 }
1549 }
1550 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001551 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1552 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001553 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001554 assert(VA.isMemLoc());
1555 if (StackPtr.Val == 0)
1556 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1557
1558 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1559 Arg));
1560 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001561 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001562 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001563
Evan Cheng32fe1032006-05-25 00:59:30 +00001564 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001565 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1566 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001567
Evan Cheng347d5f72006-04-28 21:29:37 +00001568 // Build a sequence of copy-to-reg nodes chained together with token chain
1569 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001570 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001571 // Tail call byval lowering might overwrite argument registers so in case of
1572 // tail call optimization the copies to registers are lowered later.
1573 if (!IsTailCall)
1574 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1575 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1576 InFlag);
1577 InFlag = Chain.getValue(1);
1578 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001579
Evan Chengf4684712007-02-21 21:18:14 +00001580 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001581 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001582 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1583 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1584 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1585 InFlag);
1586 InFlag = Chain.getValue(1);
1587 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001588 // If we are tail calling and generating PIC/GOT style code load the address
1589 // of the callee into ecx. The value in ecx is used as target of the tail
1590 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1591 // calls on PIC/GOT architectures. Normally we would just put the address of
1592 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1593 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001594 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001595 // Note: The actual moving to ecx is done further down.
1596 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1597 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1598 !G->getGlobal()->hasProtectedVisibility())
1599 Callee = LowerGlobalAddress(Callee, DAG);
1600 else if (isa<ExternalSymbolSDNode>(Callee))
1601 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001602 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001603
Gordon Henriksen86737662008-01-05 16:56:59 +00001604 if (Is64Bit && isVarArg) {
1605 // From AMD64 ABI document:
1606 // For calls that may call functions that use varargs or stdargs
1607 // (prototype-less calls or calls to functions containing ellipsis (...) in
1608 // the declaration) %al is used as hidden argument to specify the number
1609 // of SSE registers used. The contents of %al do not need to match exactly
1610 // the number of registers, but must be an ubound on the number of SSE
1611 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001612
1613 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001614 // Count the number of XMM registers allocated.
1615 static const unsigned XMMArgRegs[] = {
1616 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1617 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1618 };
1619 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1620
1621 Chain = DAG.getCopyToReg(Chain, X86::AL,
1622 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1623 InFlag = Chain.getValue(1);
1624 }
1625
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001626
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001627 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001628 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001629 SmallVector<SDValue, 8> MemOpChains2;
1630 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001631 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001632 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001633 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001634 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1635 CCValAssign &VA = ArgLocs[i];
1636 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001637 assert(VA.isMemLoc());
Dan Gohman475871a2008-07-27 21:46:04 +00001638 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1639 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001640 ISD::ArgFlagsTy Flags =
1641 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen86737662008-01-05 16:56:59 +00001642 // Create frame index.
1643 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001644 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001645 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001646 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001647
Duncan Sands276dcbd2008-03-21 09:14:45 +00001648 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001649 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001650 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001651 if (StackPtr.Val == 0)
1652 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1653 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1654
1655 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng8e5712b2008-01-12 01:08:07 +00001656 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001658 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001659 MemOpChains2.push_back(
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001660 DAG.getStore(Chain, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001661 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001662 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 }
1664 }
1665
1666 if (!MemOpChains2.empty())
1667 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001668 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001669
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001670 // Copy arguments to their registers.
1671 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1672 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1673 InFlag);
1674 InFlag = Chain.getValue(1);
1675 }
Dan Gohman475871a2008-07-27 21:46:04 +00001676 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001677
Gordon Henriksen86737662008-01-05 16:56:59 +00001678 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001679 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1680 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001681 }
1682
Evan Cheng32fe1032006-05-25 00:59:30 +00001683 // If the callee is a GlobalAddress node (quite common, every direct call is)
1684 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001685 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001686 // We should use extra load for direct calls to dllimported functions in
1687 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001688 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1689 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001690 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksenae636f82008-01-03 16:47:34 +00001691 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng817a6a92008-07-16 01:34:02 +00001692 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001693 } else if (IsTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001694 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1695
1696 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001697 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001698 Callee,InFlag);
1699 Callee = DAG.getRegister(Opc, getPointerTy());
1700 // Add register as live out.
1701 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001702 }
1703
Chris Lattnerd96d0722007-02-25 06:40:16 +00001704 // Returns a chain & a flag for retval copy to use.
1705 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001706 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001707
1708 if (IsTailCall) {
1709 Ops.push_back(Chain);
Chris Lattner0bd48932008-01-17 07:00:52 +00001710 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1711 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen86737662008-01-05 16:56:59 +00001712 if (InFlag.Val)
1713 Ops.push_back(InFlag);
1714 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1715 InFlag = Chain.getValue(1);
1716
1717 // Returns a chain & a flag for retval copy to use.
1718 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1719 Ops.clear();
1720 }
1721
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001722 Ops.push_back(Chain);
1723 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001724
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 if (IsTailCall)
1726 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001727
Gordon Henriksen86737662008-01-05 16:56:59 +00001728 // Add argument registers to the end of the list so that they are known live
1729 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001730 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1731 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1732 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001733
Evan Cheng586ccac2008-03-18 23:36:35 +00001734 // Add an implicit use GOT pointer in EBX.
1735 if (!IsTailCall && !Is64Bit &&
1736 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1737 Subtarget->isPICStyleGOT())
1738 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1739
1740 // Add an implicit use of AL for x86 vararg functions.
1741 if (Is64Bit && isVarArg)
1742 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1743
Evan Cheng347d5f72006-04-28 21:29:37 +00001744 if (InFlag.Val)
1745 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001746
Gordon Henriksen86737662008-01-05 16:56:59 +00001747 if (IsTailCall) {
1748 assert(InFlag.Val &&
1749 "Flag must be set. Depend on flag being set in LowerRET");
1750 Chain = DAG.getNode(X86ISD::TAILCALL,
1751 Op.Val->getVTList(), &Ops[0], Ops.size());
1752
Dan Gohman475871a2008-07-27 21:46:04 +00001753 return SDValue(Chain.Val, Op.ResNo);
Gordon Henriksen86737662008-01-05 16:56:59 +00001754 }
1755
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001756 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001757 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001758
Chris Lattner2d297092006-05-23 18:50:38 +00001759 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001760 unsigned NumBytesForCalleeToPush;
1761 if (IsCalleePop(Op))
1762 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng0d9e9762008-01-29 19:34:22 +00001763 else if (!Is64Bit && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001764 // If this is is a call to a struct-return function, the callee
1765 // pops the hidden struct pointer, so we have to push it back.
1766 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001767 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001768 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001769 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001770
Gordon Henriksenae636f82008-01-03 16:47:34 +00001771 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001772 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner0bd48932008-01-17 07:00:52 +00001773 DAG.getIntPtrConstant(NumBytes),
1774 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001775 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001776 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001777
Chris Lattner3085e152007-02-25 08:59:22 +00001778 // Handle result values, copying them out of physregs into vregs that we
1779 // return.
Dan Gohman475871a2008-07-27 21:46:04 +00001780 return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001781}
1782
Evan Cheng25ab6902006-09-08 06:48:29 +00001783
1784//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001785// Fast Calling Convention (tail call) implementation
1786//===----------------------------------------------------------------------===//
1787
1788// Like std call, callee cleans arguments, convention except that ECX is
1789// reserved for storing the tail called function address. Only 2 registers are
1790// free for argument passing (inreg). Tail call optimization is performed
1791// provided:
1792// * tailcallopt is enabled
1793// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001794// On X86_64 architecture with GOT-style position independent code only local
1795// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001796// To keep the stack aligned according to platform abi the function
1797// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1798// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001799// If a tail called function callee has more arguments than the caller the
1800// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001801// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001802// original REtADDR, but before the saved framepointer or the spilled registers
1803// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1804// stack layout:
1805// arg1
1806// arg2
1807// RETADDR
1808// [ new RETADDR
1809// move area ]
1810// (possible EBP)
1811// ESI
1812// EDI
1813// local1 ..
1814
1815/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1816/// for a 16 byte align requirement.
1817unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1818 SelectionDAG& DAG) {
1819 if (PerformTailCallOpt) {
1820 MachineFunction &MF = DAG.getMachineFunction();
1821 const TargetMachine &TM = MF.getTarget();
1822 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1823 unsigned StackAlignment = TFI.getStackAlignment();
1824 uint64_t AlignMask = StackAlignment - 1;
1825 int64_t Offset = StackSize;
1826 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1827 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1828 // Number smaller than 12 so just add the difference.
1829 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1830 } else {
1831 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1832 Offset = ((~AlignMask) & Offset) + StackAlignment +
1833 (StackAlignment-SlotSize);
1834 }
1835 StackSize = Offset;
1836 }
1837 return StackSize;
1838}
1839
1840/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001841/// following the call is a return. A function is eligible if caller/callee
1842/// calling conventions match, currently only fastcc supports tail calls, and
1843/// the function CALL is immediatly followed by a RET.
Dan Gohman475871a2008-07-27 21:46:04 +00001844bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1845 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001846 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001847 if (!PerformTailCallOpt)
1848 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001849
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001850 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001851 MachineFunction &MF = DAG.getMachineFunction();
1852 unsigned CallerCC = MF.getFunction()->getCallingConv();
1853 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1854 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001855 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001856 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001857 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001858 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001859 return true;
1860
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001861 // Can only do local tail calls (in same module, hidden or protected) on
1862 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001863 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1864 return G->getGlobal()->hasHiddenVisibility()
1865 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001866 }
1867 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001868
1869 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001870}
1871
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001872//===----------------------------------------------------------------------===//
1873// Other Lowering Hooks
1874//===----------------------------------------------------------------------===//
1875
1876
Dan Gohman475871a2008-07-27 21:46:04 +00001877SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001878 MachineFunction &MF = DAG.getMachineFunction();
1879 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1880 int ReturnAddrIndex = FuncInfo->getRAIndex();
1881
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001882 if (ReturnAddrIndex == 0) {
1883 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00001884 if (Subtarget->is64Bit())
1885 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1886 else
1887 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001888
1889 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001890 }
1891
Evan Cheng25ab6902006-09-08 06:48:29 +00001892 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001893}
1894
1895
1896
Evan Cheng6dfa9992006-01-30 23:41:35 +00001897/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1898/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001899/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1900/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001901static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman475871a2008-07-27 21:46:04 +00001902 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Chris Lattnerf9570512006-09-13 03:22:10 +00001903 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001904 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001905 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001906 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1907 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1908 // X > -1 -> X == 0, jump !sign.
1909 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001910 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001911 return true;
1912 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1913 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001914 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001915 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00001916 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1917 // X < 1 -> X <= 0
1918 RHS = DAG.getConstant(0, RHS.getValueType());
1919 X86CC = X86::COND_LE;
1920 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001921 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001922 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001923
Evan Chengd9558e02006-01-06 00:43:03 +00001924 switch (SetCCOpcode) {
1925 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001926 case ISD::SETEQ: X86CC = X86::COND_E; break;
1927 case ISD::SETGT: X86CC = X86::COND_G; break;
1928 case ISD::SETGE: X86CC = X86::COND_GE; break;
1929 case ISD::SETLT: X86CC = X86::COND_L; break;
1930 case ISD::SETLE: X86CC = X86::COND_LE; break;
1931 case ISD::SETNE: X86CC = X86::COND_NE; break;
1932 case ISD::SETULT: X86CC = X86::COND_B; break;
1933 case ISD::SETUGT: X86CC = X86::COND_A; break;
1934 case ISD::SETULE: X86CC = X86::COND_BE; break;
1935 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001936 }
1937 } else {
1938 // On a floating point condition, the flags are set as follows:
1939 // ZF PF CF op
1940 // 0 | 0 | 0 | X > Y
1941 // 0 | 0 | 1 | X < Y
1942 // 1 | 0 | 0 | X == Y
1943 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001944 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001945 switch (SetCCOpcode) {
1946 default: break;
1947 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001948 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001949 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001950 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001951 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001952 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001953 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001954 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001955 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001956 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001957 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001958 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001959 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001960 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001961 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001962 case ISD::SETNE: X86CC = X86::COND_NE; break;
1963 case ISD::SETUO: X86CC = X86::COND_P; break;
1964 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001965 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001966 if (Flip)
1967 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001968 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001969
Chris Lattner7fbe9722006-10-20 17:42:20 +00001970 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001971}
1972
Evan Cheng4a460802006-01-11 00:33:36 +00001973/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1974/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001975/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001976static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001977 switch (X86CC) {
1978 default:
1979 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001980 case X86::COND_B:
1981 case X86::COND_BE:
1982 case X86::COND_E:
1983 case X86::COND_P:
1984 case X86::COND_A:
1985 case X86::COND_AE:
1986 case X86::COND_NE:
1987 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001988 return true;
1989 }
1990}
1991
Evan Cheng5ced1d82006-04-06 23:23:56 +00001992/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001993/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman475871a2008-07-27 21:46:04 +00001994static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00001995 if (Op.getOpcode() == ISD::UNDEF)
1996 return true;
1997
1998 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001999 return (Val >= Low && Val < Hi);
2000}
2001
2002/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2003/// true if Op is undef or if its value equal to the specified value.
Dan Gohman475871a2008-07-27 21:46:04 +00002004static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Evan Chengc5cdff22006-04-07 21:53:05 +00002005 if (Op.getOpcode() == ISD::UNDEF)
2006 return true;
2007 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002008}
2009
Evan Cheng0188ecb2006-03-22 18:59:22 +00002010/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2011/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2012bool X86::isPSHUFDMask(SDNode *N) {
2013 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2014
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002015 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002016 return false;
2017
2018 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002019 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002020 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002021 if (Arg.getOpcode() == ISD::UNDEF) continue;
2022 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002023 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002024 return false;
2025 }
2026
2027 return true;
2028}
2029
2030/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002031/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002032bool X86::isPSHUFHWMask(SDNode *N) {
2033 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2034
2035 if (N->getNumOperands() != 8)
2036 return false;
2037
2038 // Lower quadword copied in order.
2039 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002040 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002041 if (Arg.getOpcode() == ISD::UNDEF) continue;
2042 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2043 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002044 return false;
2045 }
2046
2047 // Upper quadword shuffled.
2048 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002049 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002050 if (Arg.getOpcode() == ISD::UNDEF) continue;
2051 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2052 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002053 if (Val < 4 || Val > 7)
2054 return false;
2055 }
2056
2057 return true;
2058}
2059
2060/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002061/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002062bool X86::isPSHUFLWMask(SDNode *N) {
2063 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2064
2065 if (N->getNumOperands() != 8)
2066 return false;
2067
2068 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002069 for (unsigned i = 4; i != 8; ++i)
2070 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002071 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002072
2073 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002074 for (unsigned i = 0; i != 4; ++i)
2075 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002076 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002077
2078 return true;
2079}
2080
Evan Cheng14aed5e2006-03-24 01:18:28 +00002081/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2082/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002083static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002084 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002085
Evan Cheng39623da2006-04-20 08:58:49 +00002086 unsigned Half = NumElems / 2;
2087 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002088 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002089 return false;
2090 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002091 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002092 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002093
2094 return true;
2095}
2096
Evan Cheng39623da2006-04-20 08:58:49 +00002097bool X86::isSHUFPMask(SDNode *N) {
2098 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002099 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002100}
2101
Evan Cheng213d2cf2007-05-17 18:45:50 +00002102/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002103/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2104/// half elements to come from vector 1 (which would equal the dest.) and
2105/// the upper half to come from vector 2.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002106static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002107 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002108
Chris Lattner5a88b832007-02-25 07:10:00 +00002109 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002110 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002111 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002112 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002113 for (unsigned i = Half; i < NumOps; ++i)
2114 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002115 return false;
2116 return true;
2117}
2118
2119static bool isCommutedSHUFP(SDNode *N) {
2120 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002121 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002122}
2123
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002124/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2125/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2126bool X86::isMOVHLPSMask(SDNode *N) {
2127 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2128
Evan Cheng2064a2b2006-03-28 06:50:32 +00002129 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002130 return false;
2131
Evan Cheng2064a2b2006-03-28 06:50:32 +00002132 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002133 return isUndefOrEqual(N->getOperand(0), 6) &&
2134 isUndefOrEqual(N->getOperand(1), 7) &&
2135 isUndefOrEqual(N->getOperand(2), 2) &&
2136 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002137}
2138
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002139/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2140/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2141/// <2, 3, 2, 3>
2142bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2143 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2144
2145 if (N->getNumOperands() != 4)
2146 return false;
2147
2148 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2149 return isUndefOrEqual(N->getOperand(0), 2) &&
2150 isUndefOrEqual(N->getOperand(1), 3) &&
2151 isUndefOrEqual(N->getOperand(2), 2) &&
2152 isUndefOrEqual(N->getOperand(3), 3);
2153}
2154
Evan Cheng5ced1d82006-04-06 23:23:56 +00002155/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2156/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2157bool X86::isMOVLPMask(SDNode *N) {
2158 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159
2160 unsigned NumElems = N->getNumOperands();
2161 if (NumElems != 2 && NumElems != 4)
2162 return false;
2163
Evan Chengc5cdff22006-04-07 21:53:05 +00002164 for (unsigned i = 0; i < NumElems/2; ++i)
2165 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2166 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002167
Evan Chengc5cdff22006-04-07 21:53:05 +00002168 for (unsigned i = NumElems/2; i < NumElems; ++i)
2169 if (!isUndefOrEqual(N->getOperand(i), i))
2170 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002171
2172 return true;
2173}
2174
2175/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002176/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2177/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002178bool X86::isMOVHPMask(SDNode *N) {
2179 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2180
2181 unsigned NumElems = N->getNumOperands();
2182 if (NumElems != 2 && NumElems != 4)
2183 return false;
2184
Evan Chengc5cdff22006-04-07 21:53:05 +00002185 for (unsigned i = 0; i < NumElems/2; ++i)
2186 if (!isUndefOrEqual(N->getOperand(i), i))
2187 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002188
2189 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002190 SDValue Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002191 if (!isUndefOrEqual(Arg, i + NumElems))
2192 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002193 }
2194
2195 return true;
2196}
2197
Evan Cheng0038e592006-03-28 00:39:58 +00002198/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2199/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002200bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002201 bool V2IsSplat = false) {
2202 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002203 return false;
2204
Chris Lattner5a88b832007-02-25 07:10:00 +00002205 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002206 SDValue BitI = Elts[i];
2207 SDValue BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002208 if (!isUndefOrEqual(BitI, j))
2209 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002210 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002211 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002212 return false;
2213 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002214 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002215 return false;
2216 }
Evan Cheng0038e592006-03-28 00:39:58 +00002217 }
2218
2219 return true;
2220}
2221
Evan Cheng39623da2006-04-20 08:58:49 +00002222bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2223 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002224 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002225}
2226
Evan Cheng4fcb9222006-03-28 02:43:26 +00002227/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2228/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002229bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002230 bool V2IsSplat = false) {
2231 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002232 return false;
2233
Chris Lattner5a88b832007-02-25 07:10:00 +00002234 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002235 SDValue BitI = Elts[i];
2236 SDValue BitI1 = Elts[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002237 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002238 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002239 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002240 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002241 return false;
2242 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002243 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002244 return false;
2245 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002246 }
2247
2248 return true;
2249}
2250
Evan Cheng39623da2006-04-20 08:58:49 +00002251bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2252 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002253 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002254}
2255
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002256/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2257/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2258/// <0, 0, 1, 1>
2259bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2260 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2261
2262 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002263 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002264 return false;
2265
2266 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002267 SDValue BitI = N->getOperand(i);
2268 SDValue BitI1 = N->getOperand(i+1);
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002269
Evan Chengc5cdff22006-04-07 21:53:05 +00002270 if (!isUndefOrEqual(BitI, j))
2271 return false;
2272 if (!isUndefOrEqual(BitI1, j))
2273 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002274 }
2275
2276 return true;
2277}
2278
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002279/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2280/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2281/// <2, 2, 3, 3>
2282bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2283 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2284
2285 unsigned NumElems = N->getNumOperands();
2286 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2287 return false;
2288
2289 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002290 SDValue BitI = N->getOperand(i);
2291 SDValue BitI1 = N->getOperand(i + 1);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002292
2293 if (!isUndefOrEqual(BitI, j))
2294 return false;
2295 if (!isUndefOrEqual(BitI1, j))
2296 return false;
2297 }
2298
2299 return true;
2300}
2301
Evan Cheng017dcc62006-04-21 01:05:10 +00002302/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to MOVSS,
2304/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002305static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002306 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002307 return false;
2308
Chris Lattner5a88b832007-02-25 07:10:00 +00002309 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002310 return false;
2311
Chris Lattner5a88b832007-02-25 07:10:00 +00002312 for (unsigned i = 1; i < NumElts; ++i) {
2313 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002314 return false;
2315 }
2316
2317 return true;
2318}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002319
Evan Cheng017dcc62006-04-21 01:05:10 +00002320bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002321 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002322 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002323}
2324
Evan Cheng017dcc62006-04-21 01:05:10 +00002325/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2326/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002327/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002328static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002329 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002330 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002331 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002332 return false;
2333
2334 if (!isUndefOrEqual(Ops[0], 0))
2335 return false;
2336
Chris Lattner5a88b832007-02-25 07:10:00 +00002337 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002338 SDValue Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002339 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2340 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2341 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002342 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002343 }
2344
2345 return true;
2346}
2347
Evan Cheng8cf723d2006-09-08 01:50:06 +00002348static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2349 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002350 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002351 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2352 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002353}
2354
Evan Chengd9539472006-04-14 21:59:03 +00002355/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2356/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2357bool X86::isMOVSHDUPMask(SDNode *N) {
2358 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2359
2360 if (N->getNumOperands() != 4)
2361 return false;
2362
2363 // Expect 1, 1, 3, 3
2364 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002365 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002366 if (Arg.getOpcode() == ISD::UNDEF) continue;
2367 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2368 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2369 if (Val != 1) return false;
2370 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002371
2372 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002373 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002374 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002375 if (Arg.getOpcode() == ISD::UNDEF) continue;
2376 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2377 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2378 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002379 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002380 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002381
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002382 // Don't use movshdup if it can be done with a shufps.
2383 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002384}
2385
2386/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2387/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2388bool X86::isMOVSLDUPMask(SDNode *N) {
2389 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2390
2391 if (N->getNumOperands() != 4)
2392 return false;
2393
2394 // Expect 0, 0, 2, 2
2395 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002396 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002397 if (Arg.getOpcode() == ISD::UNDEF) continue;
2398 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2399 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2400 if (Val != 0) return false;
2401 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002402
2403 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002404 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002405 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002406 if (Arg.getOpcode() == ISD::UNDEF) continue;
2407 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2408 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2409 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002410 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002411 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002412
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002413 // Don't use movshdup if it can be done with a shufps.
2414 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002415}
2416
Evan Cheng49892af2007-06-19 00:02:56 +00002417/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2418/// specifies a identity operation on the LHS or RHS.
2419static bool isIdentityMask(SDNode *N, bool RHS = false) {
2420 unsigned NumElems = N->getNumOperands();
2421 for (unsigned i = 0; i < NumElems; ++i)
2422 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2423 return false;
2424 return true;
2425}
2426
Evan Chengb9df0ca2006-03-22 02:53:00 +00002427/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2428/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002429static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002430 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2431
Evan Chengb9df0ca2006-03-22 02:53:00 +00002432 // This is a splat operation if each element of the permute is the same, and
2433 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002434 unsigned NumElems = N->getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002435 SDValue ElementBase;
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002436 unsigned i = 0;
2437 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002438 SDValue Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002439 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002440 ElementBase = Elt;
2441 break;
2442 }
2443 }
2444
2445 if (!ElementBase.Val)
2446 return false;
2447
2448 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002449 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002450 if (Arg.getOpcode() == ISD::UNDEF) continue;
2451 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002452 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002453 }
2454
2455 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002456 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002457}
2458
Evan Chengc575ca22006-04-17 20:43:08 +00002459/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2460/// a splat of a single element and it's a 2 or 4 element mask.
2461bool X86::isSplatMask(SDNode *N) {
2462 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2463
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002464 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002465 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2466 return false;
2467 return ::isSplatMask(N);
2468}
2469
Evan Chengf686d9b2006-10-27 21:08:32 +00002470/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2471/// specifies a splat of zero element.
2472bool X86::isSplatLoMask(SDNode *N) {
2473 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2474
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002475 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002476 if (!isUndefOrEqual(N->getOperand(i), 0))
2477 return false;
2478 return true;
2479}
2480
Evan Cheng63d33002006-03-22 08:01:21 +00002481/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2482/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2483/// instructions.
2484unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002485 unsigned NumOperands = N->getNumOperands();
2486 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2487 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002488 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002489 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002490 SDValue Arg = N->getOperand(NumOperands-i-1);
Evan Chengef698ca2006-03-31 00:30:29 +00002491 if (Arg.getOpcode() != ISD::UNDEF)
2492 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002493 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002494 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002495 if (i != NumOperands - 1)
2496 Mask <<= Shift;
2497 }
Evan Cheng63d33002006-03-22 08:01:21 +00002498
2499 return Mask;
2500}
2501
Evan Cheng506d3df2006-03-29 23:07:14 +00002502/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2503/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2504/// instructions.
2505unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2506 unsigned Mask = 0;
2507 // 8 nodes, but we only care about the last 4.
2508 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002509 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002510 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002511 if (Arg.getOpcode() != ISD::UNDEF)
2512 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002513 Mask |= (Val - 4);
2514 if (i != 4)
2515 Mask <<= 2;
2516 }
2517
2518 return Mask;
2519}
2520
2521/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2522/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2523/// instructions.
2524unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2525 unsigned Mask = 0;
2526 // 8 nodes, but we only care about the first 4.
2527 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002528 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002529 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002530 if (Arg.getOpcode() != ISD::UNDEF)
2531 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002532 Mask |= Val;
2533 if (i != 0)
2534 Mask <<= 2;
2535 }
2536
2537 return Mask;
2538}
2539
Evan Chengc21a0532006-04-05 01:47:37 +00002540/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2541/// specifies a 8 element shuffle that can be broken into a pair of
2542/// PSHUFHW and PSHUFLW.
2543static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2544 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2545
2546 if (N->getNumOperands() != 8)
2547 return false;
2548
2549 // Lower quadword shuffled.
2550 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002551 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002552 if (Arg.getOpcode() == ISD::UNDEF) continue;
2553 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2554 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002555 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002556 return false;
2557 }
2558
2559 // Upper quadword shuffled.
2560 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002561 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002562 if (Arg.getOpcode() == ISD::UNDEF) continue;
2563 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2564 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2565 if (Val < 4 || Val > 7)
2566 return false;
2567 }
2568
2569 return true;
2570}
2571
Chris Lattner8a594482007-11-25 00:24:49 +00002572/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002573/// values in ther permute mask.
Dan Gohman475871a2008-07-27 21:46:04 +00002574static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2575 SDValue &V2, SDValue &Mask,
Evan Cheng9eca5e82006-10-25 21:49:50 +00002576 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002577 MVT VT = Op.getValueType();
2578 MVT MaskVT = Mask.getValueType();
2579 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002580 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002581 SmallVector<SDValue, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002582
2583 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002584 SDValue Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002585 if (Arg.getOpcode() == ISD::UNDEF) {
2586 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2587 continue;
2588 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002589 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2590 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2591 if (Val < NumElems)
2592 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2593 else
2594 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2595 }
2596
Evan Cheng9eca5e82006-10-25 21:49:50 +00002597 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002598 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002599 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002600}
2601
Evan Cheng779ccea2007-12-07 21:30:01 +00002602/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2603/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002604static
Dan Gohman475871a2008-07-27 21:46:04 +00002605SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002606 MVT MaskVT = Mask.getValueType();
2607 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002608 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002609 SmallVector<SDValue, 8> MaskVec;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002610 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002611 SDValue Arg = Mask.getOperand(i);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002612 if (Arg.getOpcode() == ISD::UNDEF) {
2613 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2614 continue;
2615 }
2616 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2617 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2618 if (Val < NumElems)
2619 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2620 else
2621 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2622 }
2623 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2624}
2625
2626
Evan Cheng533a0aa2006-04-19 20:35:22 +00002627/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2628/// match movhlps. The lower half elements should come from upper half of
2629/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002630/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002631static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2632 unsigned NumElems = Mask->getNumOperands();
2633 if (NumElems != 4)
2634 return false;
2635 for (unsigned i = 0, e = 2; i != e; ++i)
2636 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2637 return false;
2638 for (unsigned i = 2; i != 4; ++i)
2639 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2640 return false;
2641 return true;
2642}
2643
Evan Cheng5ced1d82006-04-06 23:23:56 +00002644/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002645/// is promoted to a vector. It also returns the LoadSDNode by reference if
2646/// required.
2647static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng533a0aa2006-04-19 20:35:22 +00002648 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2649 N = N->getOperand(0).Val;
Evan Cheng7e2ff772008-05-08 00:57:18 +00002650 if (ISD::isNON_EXTLoad(N)) {
2651 if (LD)
2652 *LD = cast<LoadSDNode>(N);
2653 return true;
2654 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002655 }
2656 return false;
2657}
2658
Evan Cheng533a0aa2006-04-19 20:35:22 +00002659/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2660/// match movlp{s|d}. The lower half elements should come from lower half of
2661/// V1 (and in order), and the upper half elements should come from the upper
2662/// half of V2 (and in order). And since V1 will become the source of the
2663/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002664static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002665 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002666 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002667 // Is V2 is a vector load, don't do this transformation. We will try to use
2668 // load folding shufps op.
2669 if (ISD::isNON_EXTLoad(V2))
2670 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002671
Evan Cheng533a0aa2006-04-19 20:35:22 +00002672 unsigned NumElems = Mask->getNumOperands();
2673 if (NumElems != 2 && NumElems != 4)
2674 return false;
2675 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2676 if (!isUndefOrEqual(Mask->getOperand(i), i))
2677 return false;
2678 for (unsigned i = NumElems/2; i != NumElems; ++i)
2679 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2680 return false;
2681 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002682}
2683
Evan Cheng39623da2006-04-20 08:58:49 +00002684/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2685/// all the same.
2686static bool isSplatVector(SDNode *N) {
2687 if (N->getOpcode() != ISD::BUILD_VECTOR)
2688 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002689
Dan Gohman475871a2008-07-27 21:46:04 +00002690 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002691 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2692 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002693 return false;
2694 return true;
2695}
2696
Evan Cheng8cf723d2006-09-08 01:50:06 +00002697/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2698/// to an undef.
2699static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002700 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002701 return false;
2702
Dan Gohman475871a2008-07-27 21:46:04 +00002703 SDValue V1 = N->getOperand(0);
2704 SDValue V2 = N->getOperand(1);
2705 SDValue Mask = N->getOperand(2);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002706 unsigned NumElems = Mask.getNumOperands();
2707 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002708 SDValue Arg = Mask.getOperand(i);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002709 if (Arg.getOpcode() != ISD::UNDEF) {
2710 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2711 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2712 return false;
2713 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2714 return false;
2715 }
2716 }
2717 return true;
2718}
2719
Evan Cheng213d2cf2007-05-17 18:45:50 +00002720/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2721/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002722static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002723 return ((isa<ConstantSDNode>(Elt) &&
2724 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2725 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002726 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002727}
2728
2729/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2730/// to an zero vector.
2731static bool isZeroShuffle(SDNode *N) {
2732 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2733 return false;
2734
Dan Gohman475871a2008-07-27 21:46:04 +00002735 SDValue V1 = N->getOperand(0);
2736 SDValue V2 = N->getOperand(1);
2737 SDValue Mask = N->getOperand(2);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002738 unsigned NumElems = Mask.getNumOperands();
2739 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002740 SDValue Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002741 if (Arg.getOpcode() == ISD::UNDEF)
2742 continue;
2743
2744 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2745 if (Idx < NumElems) {
2746 unsigned Opc = V1.Val->getOpcode();
2747 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2748 continue;
2749 if (Opc != ISD::BUILD_VECTOR ||
2750 !isZeroNode(V1.Val->getOperand(Idx)))
2751 return false;
2752 } else if (Idx >= NumElems) {
2753 unsigned Opc = V2.Val->getOpcode();
2754 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2755 continue;
2756 if (Opc != ISD::BUILD_VECTOR ||
2757 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2758 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002759 }
2760 }
2761 return true;
2762}
2763
2764/// getZeroVector - Returns a vector of specified type with all zero elements.
2765///
Dan Gohman475871a2008-07-27 21:46:04 +00002766static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002767 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002768
2769 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2770 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002771 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002772 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002773 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002774 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002775 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002776 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002777 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002778 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002779 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chengf0df0312008-05-15 08:39:06 +00002780 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2781 }
Chris Lattner8a594482007-11-25 00:24:49 +00002782 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002783}
2784
Chris Lattner8a594482007-11-25 00:24:49 +00002785/// getOnesVector - Returns a vector of specified type with all bits set.
2786///
Dan Gohman475871a2008-07-27 21:46:04 +00002787static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002788 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002789
2790 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2791 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002792 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2793 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002794 if (VT.getSizeInBits() == 64) // MMX
Chris Lattner8a594482007-11-25 00:24:49 +00002795 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2796 else // SSE
2797 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2798 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2799}
2800
2801
Evan Cheng39623da2006-04-20 08:58:49 +00002802/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2803/// that point to V2 points to its first element.
Dan Gohman475871a2008-07-27 21:46:04 +00002804static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002805 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2806
2807 bool Changed = false;
Dan Gohman475871a2008-07-27 21:46:04 +00002808 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002809 unsigned NumElems = Mask.getNumOperands();
2810 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002811 SDValue Arg = Mask.getOperand(i);
Evan Cheng39623da2006-04-20 08:58:49 +00002812 if (Arg.getOpcode() != ISD::UNDEF) {
2813 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2814 if (Val > NumElems) {
2815 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2816 Changed = true;
2817 }
2818 }
2819 MaskVec.push_back(Arg);
2820 }
2821
2822 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002823 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2824 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002825 return Mask;
2826}
2827
Evan Cheng017dcc62006-04-21 01:05:10 +00002828/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2829/// operation of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002830static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002831 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2832 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002833
Dan Gohman475871a2008-07-27 21:46:04 +00002834 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002835 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2836 for (unsigned i = 1; i != NumElems; ++i)
2837 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002838 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002839}
2840
Evan Chengc575ca22006-04-17 20:43:08 +00002841/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2842/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002843static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002844 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2845 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002846 SmallVector<SDValue, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002847 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2848 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2849 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2850 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002851 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002852}
2853
Evan Cheng39623da2006-04-20 08:58:49 +00002854/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2855/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002856static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002857 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2858 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002859 unsigned Half = NumElems/2;
Dan Gohman475871a2008-07-27 21:46:04 +00002860 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002861 for (unsigned i = 0; i != Half; ++i) {
2862 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2863 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2864 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002865 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002866}
2867
Chris Lattner62098042008-03-09 01:05:04 +00002868/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2869/// element #0 of a vector with the specified index, leaving the rest of the
2870/// elements in place.
Dan Gohman475871a2008-07-27 21:46:04 +00002871static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner62098042008-03-09 01:05:04 +00002872 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002873 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2874 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002875 SmallVector<SDValue, 8> MaskVec;
Chris Lattner62098042008-03-09 01:05:04 +00002876 // Element #0 of the result gets the elt we are replacing.
2877 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2878 for (unsigned i = 1; i != NumElems; ++i)
2879 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2880 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2881}
2882
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002883/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman475871a2008-07-27 21:46:04 +00002884static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002885 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2886 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002887 if (PVT == VT)
2888 return Op;
Dan Gohman475871a2008-07-27 21:46:04 +00002889 SDValue V1 = Op.getOperand(0);
2890 SDValue Mask = Op.getOperand(2);
Evan Cheng017dcc62006-04-21 01:05:10 +00002891 unsigned NumElems = Mask.getNumOperands();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002892 // Special handling of v4f32 -> v4i32.
2893 if (VT != MVT::v4f32) {
2894 Mask = getUnpacklMask(NumElems, DAG);
2895 while (NumElems > 4) {
2896 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2897 NumElems >>= 1;
2898 }
Evan Chengf0df0312008-05-15 08:39:06 +00002899 Mask = getZeroVector(MVT::v4i32, true, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002900 }
Evan Chengc575ca22006-04-17 20:43:08 +00002901
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002902 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman475871a2008-07-27 21:46:04 +00002903 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002904 DAG.getNode(ISD::UNDEF, PVT), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002905 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2906}
2907
Evan Chengba05f722006-04-21 23:03:30 +00002908/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002909/// vector of zero or undef vector. This produces a shuffle where the low
2910/// element of V2 is swizzled into the zero/undef vector, landing at element
2911/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00002912static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002913 bool isZero, bool HasSSE2,
2914 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002915 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002916 SDValue V1 = isZero
Evan Chengf0df0312008-05-15 08:39:06 +00002917 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002918 unsigned NumElems = V2.getValueType().getVectorNumElements();
2919 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2920 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002921 SmallVector<SDValue, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002922 for (unsigned i = 0; i != NumElems; ++i)
2923 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2924 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2925 else
2926 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00002927 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002928 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002929 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002930}
2931
Evan Chengf26ffe92008-05-29 08:22:04 +00002932/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2933/// a shuffle that is zero.
2934static
Dan Gohman475871a2008-07-27 21:46:04 +00002935unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengf26ffe92008-05-29 08:22:04 +00002936 unsigned NumElems, bool Low,
2937 SelectionDAG &DAG) {
2938 unsigned NumZeros = 0;
2939 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002940 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman475871a2008-07-27 21:46:04 +00002941 SDValue Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00002942 if (Idx.getOpcode() == ISD::UNDEF) {
2943 ++NumZeros;
2944 continue;
2945 }
Dan Gohman475871a2008-07-27 21:46:04 +00002946 SDValue Elt = DAG.getShuffleScalarElt(Op.Val, Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00002947 if (Elt.Val && isZeroNode(Elt))
2948 ++NumZeros;
2949 else
2950 break;
2951 }
2952 return NumZeros;
2953}
2954
2955/// isVectorShift - Returns true if the shuffle can be implemented as a
2956/// logical left or right shift of a vector.
Dan Gohman475871a2008-07-27 21:46:04 +00002957static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2958 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002959 unsigned NumElems = Mask.getNumOperands();
2960
2961 isLeft = true;
2962 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2963 if (!NumZeros) {
2964 isLeft = false;
2965 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2966 if (!NumZeros)
2967 return false;
2968 }
2969
2970 bool SeenV1 = false;
2971 bool SeenV2 = false;
2972 for (unsigned i = NumZeros; i < NumElems; ++i) {
2973 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman475871a2008-07-27 21:46:04 +00002974 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengf26ffe92008-05-29 08:22:04 +00002975 if (Idx.getOpcode() == ISD::UNDEF)
2976 continue;
2977 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2978 if (Index < NumElems)
2979 SeenV1 = true;
2980 else {
2981 Index -= NumElems;
2982 SeenV2 = true;
2983 }
2984 if (Index != Val)
2985 return false;
2986 }
2987 if (SeenV1 && SeenV2)
2988 return false;
2989
2990 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2991 ShAmt = NumZeros;
2992 return true;
2993}
2994
2995
Evan Chengc78d3b42006-04-24 18:01:45 +00002996/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2997///
Dan Gohman475871a2008-07-27 21:46:04 +00002998static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002999 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003000 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003001 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003002 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003003
Dan Gohman475871a2008-07-27 21:46:04 +00003004 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003005 bool First = true;
3006 for (unsigned i = 0; i < 16; ++i) {
3007 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3008 if (ThisIsNonZero && First) {
3009 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003010 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003011 else
3012 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3013 First = false;
3014 }
3015
3016 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003017 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003018 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3019 if (LastIsNonZero) {
3020 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3021 }
3022 if (ThisIsNonZero) {
3023 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3024 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3025 ThisElt, DAG.getConstant(8, MVT::i8));
3026 if (LastIsNonZero)
3027 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3028 } else
3029 ThisElt = LastElt;
3030
3031 if (ThisElt.Val)
3032 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003033 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003034 }
3035 }
3036
3037 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3038}
3039
Bill Wendlinga348c562007-03-22 18:42:45 +00003040/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003041///
Dan Gohman475871a2008-07-27 21:46:04 +00003042static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003043 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003044 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003045 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003046 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003047
Dan Gohman475871a2008-07-27 21:46:04 +00003048 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003049 bool First = true;
3050 for (unsigned i = 0; i < 8; ++i) {
3051 bool isNonZero = (NonZeros & (1 << i)) != 0;
3052 if (isNonZero) {
3053 if (First) {
3054 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003055 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003056 else
3057 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3058 First = false;
3059 }
3060 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003061 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003062 }
3063 }
3064
3065 return V;
3066}
3067
Evan Chengf26ffe92008-05-29 08:22:04 +00003068/// getVShift - Return a vector logical shift node.
3069///
Dan Gohman475871a2008-07-27 21:46:04 +00003070static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003071 unsigned NumBits, SelectionDAG &DAG,
3072 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003073 bool isMMX = VT.getSizeInBits() == 64;
3074 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003075 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3076 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3077 return DAG.getNode(ISD::BIT_CONVERT, VT,
3078 DAG.getNode(Opc, ShVT, SrcOp,
3079 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3080}
3081
Dan Gohman475871a2008-07-27 21:46:04 +00003082SDValue
3083X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003084 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3085 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3086 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3087 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3088 // eliminated on x86-32 hosts.
3089 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3090 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003091
Chris Lattner8a594482007-11-25 00:24:49 +00003092 if (ISD::isBuildVectorAllOnes(Op.Val))
3093 return getOnesVector(Op.getValueType(), DAG);
Evan Chengf0df0312008-05-15 08:39:06 +00003094 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattner8a594482007-11-25 00:24:49 +00003095 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003096
Duncan Sands83ec4b62008-06-06 12:08:01 +00003097 MVT VT = Op.getValueType();
3098 MVT EVT = VT.getVectorElementType();
3099 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003100
3101 unsigned NumElems = Op.getNumOperands();
3102 unsigned NumZero = 0;
3103 unsigned NumNonZero = 0;
3104 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003105 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003106 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003107 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003108 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003109 if (Elt.getOpcode() == ISD::UNDEF)
3110 continue;
3111 Values.insert(Elt);
3112 if (Elt.getOpcode() != ISD::Constant &&
3113 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003114 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003115 if (isZeroNode(Elt))
3116 NumZero++;
3117 else {
3118 NonZeros |= (1 << i);
3119 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003120 }
3121 }
3122
Dan Gohman7f321562007-06-25 16:23:39 +00003123 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003124 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3125 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003126 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003127
Chris Lattner67f453a2008-03-09 05:42:06 +00003128 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003129 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003130 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003131 SDValue Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003132
Chris Lattner62098042008-03-09 01:05:04 +00003133 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3134 // the value are obviously zero, truncate the value to i32 and do the
3135 // insertion that way. Only do this if the value is non-constant or if the
3136 // value is a constant being inserted into element 0. It is cheaper to do
3137 // a constant pool load than it is to do a movd + shuffle.
3138 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3139 (!IsAllConstants || Idx == 0)) {
3140 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3141 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003142 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3143 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003144
3145 // Truncate the value (which may itself be a constant) to i32, and
3146 // convert it to a vector with movd (S2V+shuffle to zero extend).
3147 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3148 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003149 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3150 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003151
3152 // Now we have our 32-bit value zero extended in the low element of
3153 // a vector. If Idx != 0, swizzle it into place.
3154 if (Idx != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003155 SDValue Ops[] = {
Chris Lattner62098042008-03-09 01:05:04 +00003156 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3157 getSwapEltZeroMask(VecElts, Idx, DAG)
3158 };
3159 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3160 }
3161 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3162 }
3163 }
3164
Chris Lattner19f79692008-03-08 22:59:52 +00003165 // If we have a constant or non-constant insertion into the low element of
3166 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3167 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3168 // depending on what the source datatype is. Because we can only get here
3169 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3170 if (Idx == 0 &&
3171 // Don't do this for i64 values on x86-32.
3172 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003173 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003174 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003175 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3176 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003177 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003178
3179 // Is it a vector logical left shift?
3180 if (NumElems == 2 && Idx == 1 &&
3181 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003182 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003183 return getVShift(true, VT,
3184 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3185 NumBits/2, DAG, *this);
3186 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003187
3188 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003189 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003190
Chris Lattner19f79692008-03-08 22:59:52 +00003191 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3192 // is a non-constant being inserted into an element other than the low one,
3193 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3194 // movd/movss) to move this into the low element, then shuffle it into
3195 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003196 if (EVTBits == 32) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003197 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3198
Evan Cheng0db9fe62006-04-25 20:13:52 +00003199 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003200 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3201 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003202 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3203 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003204 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003205 for (unsigned i = 0; i < NumElems; i++)
3206 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003207 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003208 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003209 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3210 DAG.getNode(ISD::UNDEF, VT), Mask);
3211 }
3212 }
3213
Chris Lattner67f453a2008-03-09 05:42:06 +00003214 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3215 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003216 return SDValue();
Chris Lattner67f453a2008-03-09 05:42:06 +00003217
Dan Gohmana3941172007-07-24 22:55:08 +00003218 // A vector full of immediates; various special cases are already
3219 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003220 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003221 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003222
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003223 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003224 if (EVTBits == 64) {
3225 if (NumNonZero == 1) {
3226 // One half is zero or undef.
3227 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003228 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003229 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003230 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3231 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003232 }
Dan Gohman475871a2008-07-27 21:46:04 +00003233 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003234 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003235
3236 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003237 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003238 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003239 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003240 if (V.Val) return V;
3241 }
3242
Bill Wendling826f36f2007-03-28 00:57:11 +00003243 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003244 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003245 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003246 if (V.Val) return V;
3247 }
3248
3249 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003250 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003251 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003252 if (NumElems == 4 && NumZero > 0) {
3253 for (unsigned i = 0; i < 4; ++i) {
3254 bool isZero = !(NonZeros & (1 << i));
3255 if (isZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003256 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003257 else
3258 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3259 }
3260
3261 for (unsigned i = 0; i < 2; ++i) {
3262 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3263 default: break;
3264 case 0:
3265 V[i] = V[i*2]; // Must be a zero vector.
3266 break;
3267 case 1:
3268 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3269 getMOVLMask(NumElems, DAG));
3270 break;
3271 case 2:
3272 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3273 getMOVLMask(NumElems, DAG));
3274 break;
3275 case 3:
3276 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3277 getUnpacklMask(NumElems, DAG));
3278 break;
3279 }
3280 }
3281
Duncan Sands83ec4b62008-06-06 12:08:01 +00003282 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3283 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003284 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003285 bool Reverse = (NonZeros & 0x3) == 2;
3286 for (unsigned i = 0; i < 2; ++i)
3287 if (Reverse)
3288 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3289 else
3290 MaskVec.push_back(DAG.getConstant(i, EVT));
3291 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3292 for (unsigned i = 0; i < 2; ++i)
3293 if (Reverse)
3294 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3295 else
3296 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003297 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00003298 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003299 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3300 }
3301
3302 if (Values.size() > 2) {
3303 // Expand into a number of unpckl*.
3304 // e.g. for v4f32
3305 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3306 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3307 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman475871a2008-07-27 21:46:04 +00003308 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003309 for (unsigned i = 0; i < NumElems; ++i)
3310 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3311 NumElems >>= 1;
3312 while (NumElems != 0) {
3313 for (unsigned i = 0; i < NumElems; ++i)
3314 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3315 UnpckMask);
3316 NumElems >>= 1;
3317 }
3318 return V[0];
3319 }
3320
Dan Gohman475871a2008-07-27 21:46:04 +00003321 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003322}
3323
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003324static
Dan Gohman475871a2008-07-27 21:46:04 +00003325SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
3326 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003327 TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +00003328 SDValue NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003329 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3330 MVT MaskEVT = MaskVT.getVectorElementType();
3331 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003332 SmallVector<SDValue, 8> MaskElts(PermMask.Val->op_begin(),
Evan Cheng14b32e12007-12-11 01:46:18 +00003333 PermMask.Val->op_end());
3334
3335 // First record which half of which vector the low elements come from.
3336 SmallVector<unsigned, 4> LowQuad(4);
3337 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003338 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003339 if (Elt.getOpcode() == ISD::UNDEF)
3340 continue;
3341 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3342 int QuadIdx = EltIdx / 4;
3343 ++LowQuad[QuadIdx];
3344 }
3345 int BestLowQuad = -1;
3346 unsigned MaxQuad = 1;
3347 for (unsigned i = 0; i < 4; ++i) {
3348 if (LowQuad[i] > MaxQuad) {
3349 BestLowQuad = i;
3350 MaxQuad = LowQuad[i];
3351 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003352 }
3353
Evan Cheng14b32e12007-12-11 01:46:18 +00003354 // Record which half of which vector the high elements come from.
3355 SmallVector<unsigned, 4> HighQuad(4);
3356 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003357 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003358 if (Elt.getOpcode() == ISD::UNDEF)
3359 continue;
3360 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3361 int QuadIdx = EltIdx / 4;
3362 ++HighQuad[QuadIdx];
3363 }
3364 int BestHighQuad = -1;
3365 MaxQuad = 1;
3366 for (unsigned i = 0; i < 4; ++i) {
3367 if (HighQuad[i] > MaxQuad) {
3368 BestHighQuad = i;
3369 MaxQuad = HighQuad[i];
3370 }
3371 }
3372
3373 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3374 if (BestLowQuad != -1 || BestHighQuad != -1) {
3375 // First sort the 4 chunks in order using shufpd.
Dan Gohman475871a2008-07-27 21:46:04 +00003376 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003377 if (BestLowQuad != -1)
3378 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3379 else
3380 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3381 if (BestHighQuad != -1)
3382 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3383 else
3384 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00003385 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng14b32e12007-12-11 01:46:18 +00003386 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3387 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3388 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3389 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3390
3391 // Now sort high and low parts separately.
3392 BitVector InOrder(8);
3393 if (BestLowQuad != -1) {
3394 // Sort lower half in order using PSHUFLW.
3395 MaskVec.clear();
3396 bool AnyOutOrder = false;
3397 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003398 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003399 if (Elt.getOpcode() == ISD::UNDEF) {
3400 MaskVec.push_back(Elt);
3401 InOrder.set(i);
3402 } else {
3403 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3404 if (EltIdx != i)
3405 AnyOutOrder = true;
3406 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3407 // If this element is in the right place after this shuffle, then
3408 // remember it.
3409 if ((int)(EltIdx / 4) == BestLowQuad)
3410 InOrder.set(i);
3411 }
3412 }
3413 if (AnyOutOrder) {
3414 for (unsigned i = 4; i != 8; ++i)
3415 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003416 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003417 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3418 }
3419 }
3420
3421 if (BestHighQuad != -1) {
3422 // Sort high half in order using PSHUFHW if possible.
3423 MaskVec.clear();
3424 for (unsigned i = 0; i != 4; ++i)
3425 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3426 bool AnyOutOrder = false;
3427 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003428 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003429 if (Elt.getOpcode() == ISD::UNDEF) {
3430 MaskVec.push_back(Elt);
3431 InOrder.set(i);
3432 } else {
3433 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3434 if (EltIdx != i)
3435 AnyOutOrder = true;
3436 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3437 // If this element is in the right place after this shuffle, then
3438 // remember it.
3439 if ((int)(EltIdx / 4) == BestHighQuad)
3440 InOrder.set(i);
3441 }
3442 }
3443 if (AnyOutOrder) {
Dan Gohman475871a2008-07-27 21:46:04 +00003444 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003445 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3446 }
3447 }
3448
3449 // The other elements are put in the right place using pextrw and pinsrw.
3450 for (unsigned i = 0; i != 8; ++i) {
3451 if (InOrder[i])
3452 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003453 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003454 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003455 SDValue ExtOp = (EltIdx < 8)
Evan Cheng14b32e12007-12-11 01:46:18 +00003456 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3457 DAG.getConstant(EltIdx, PtrVT))
3458 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3459 DAG.getConstant(EltIdx - 8, PtrVT));
3460 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3461 DAG.getConstant(i, PtrVT));
3462 }
3463 return NewV;
3464 }
3465
3466 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3467 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003468 // First, let's find out how many elements are already in the right order.
3469 unsigned V1InOrder = 0;
3470 unsigned V1FromV1 = 0;
3471 unsigned V2InOrder = 0;
3472 unsigned V2FromV2 = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003473 SmallVector<SDValue, 8> V1Elts;
3474 SmallVector<SDValue, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003475 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003476 SDValue Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003477 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003478 V1Elts.push_back(Elt);
3479 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003480 ++V1InOrder;
3481 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003482 continue;
3483 }
3484 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3485 if (EltIdx == i) {
3486 V1Elts.push_back(Elt);
3487 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3488 ++V1InOrder;
3489 } else if (EltIdx == i+8) {
3490 V1Elts.push_back(Elt);
3491 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3492 ++V2InOrder;
3493 } else if (EltIdx < 8) {
3494 V1Elts.push_back(Elt);
3495 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003496 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003497 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3498 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003499 }
3500 }
3501
3502 if (V2InOrder > V1InOrder) {
3503 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3504 std::swap(V1, V2);
3505 std::swap(V1Elts, V2Elts);
3506 std::swap(V1FromV1, V2FromV2);
3507 }
3508
Evan Cheng14b32e12007-12-11 01:46:18 +00003509 if ((V1FromV1 + V1InOrder) != 8) {
3510 // Some elements are from V2.
3511 if (V1FromV1) {
3512 // If there are elements that are from V1 but out of place,
3513 // then first sort them in place
Dan Gohman475871a2008-07-27 21:46:04 +00003514 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003515 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003516 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003517 if (Elt.getOpcode() == ISD::UNDEF) {
3518 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3519 continue;
3520 }
3521 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3522 if (EltIdx >= 8)
3523 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3524 else
3525 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3526 }
Dan Gohman475871a2008-07-27 21:46:04 +00003527 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003528 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003529 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003530
3531 NewV = V1;
3532 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003533 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003534 if (Elt.getOpcode() == ISD::UNDEF)
3535 continue;
3536 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3537 if (EltIdx < 8)
3538 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003539 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003540 DAG.getConstant(EltIdx - 8, PtrVT));
3541 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3542 DAG.getConstant(i, PtrVT));
3543 }
3544 return NewV;
3545 } else {
3546 // All elements are from V1.
3547 NewV = V1;
3548 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003549 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003550 if (Elt.getOpcode() == ISD::UNDEF)
3551 continue;
3552 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003553 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003554 DAG.getConstant(EltIdx, PtrVT));
3555 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3556 DAG.getConstant(i, PtrVT));
3557 }
3558 return NewV;
3559 }
3560}
3561
Evan Cheng7a831ce2007-12-15 03:00:47 +00003562/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3563/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3564/// done when every pair / quad of shuffle mask elements point to elements in
3565/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003566/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3567static
Dan Gohman475871a2008-07-27 21:46:04 +00003568SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003569 MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng14b32e12007-12-11 01:46:18 +00003571 TargetLowering &TLI) {
3572 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003573 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003574 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003575 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003576 MVT NewVT = MaskVT;
3577 switch (VT.getSimpleVT()) {
3578 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003579 case MVT::v4f32: NewVT = MVT::v2f64; break;
3580 case MVT::v4i32: NewVT = MVT::v2i64; break;
3581 case MVT::v8i16: NewVT = MVT::v4i32; break;
3582 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003583 }
3584
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003585 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003586 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003587 NewVT = MVT::v2i64;
3588 else
3589 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003590 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003591 unsigned Scale = NumElems / NewWidth;
Dan Gohman475871a2008-07-27 21:46:04 +00003592 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003593 for (unsigned i = 0; i < NumElems; i += Scale) {
3594 unsigned StartIdx = ~0U;
3595 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003596 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng14b32e12007-12-11 01:46:18 +00003597 if (Elt.getOpcode() == ISD::UNDEF)
3598 continue;
3599 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3600 if (StartIdx == ~0U)
3601 StartIdx = EltIdx - (EltIdx % Scale);
3602 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003603 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003604 }
3605 if (StartIdx == ~0U)
Duncan Sandsd038e042008-07-21 10:20:31 +00003606 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003607 else
Duncan Sandsd038e042008-07-21 10:20:31 +00003608 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003609 }
3610
Evan Cheng7a831ce2007-12-15 03:00:47 +00003611 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3612 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3613 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3614 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3615 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003616}
3617
Evan Chengd880b972008-05-09 21:53:03 +00003618/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003619///
Dan Gohman475871a2008-07-27 21:46:04 +00003620static SDValue getVZextMovL(MVT VT, MVT OpVT,
3621 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003622 const X86Subtarget *Subtarget) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003623 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3624 LoadSDNode *LD = NULL;
3625 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3626 LD = dyn_cast<LoadSDNode>(SrcOp);
3627 if (!LD) {
3628 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3629 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003630 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003631 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3632 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3633 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3634 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3635 // PR2108
3636 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3637 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003638 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003639 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3640 SrcOp.getOperand(0).getOperand(0))));
3641 }
3642 }
3643 }
3644
3645 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003646 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003647 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3648}
3649
Evan Chengace3c172008-07-22 21:13:36 +00003650/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3651/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003652static SDValue
3653LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3654 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengace3c172008-07-22 21:13:36 +00003655 MVT MaskVT = PermMask.getValueType();
3656 MVT MaskEVT = MaskVT.getVectorElementType();
3657 SmallVector<std::pair<int, int>, 8> Locs;
3658 Locs.reserve(4);
Dan Gohman475871a2008-07-27 21:46:04 +00003659 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003660 unsigned NumHi = 0;
3661 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003662 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003663 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003664 if (Elt.getOpcode() == ISD::UNDEF) {
3665 Locs[i] = std::make_pair(-1, -1);
3666 } else {
3667 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmand0859942008-08-04 23:09:15 +00003668 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengace3c172008-07-22 21:13:36 +00003669 if (Val < 4) {
3670 Locs[i] = std::make_pair(0, NumLo);
3671 Mask1[NumLo] = Elt;
3672 NumLo++;
3673 } else {
3674 Locs[i] = std::make_pair(1, NumHi);
3675 if (2+NumHi < 4)
3676 Mask1[2+NumHi] = Elt;
3677 NumHi++;
3678 }
3679 }
3680 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003681
Evan Chengace3c172008-07-22 21:13:36 +00003682 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003683 // If no more than two elements come from either vector. This can be
3684 // implemented with two shuffles. First shuffle gather the elements.
3685 // The second shuffle, which takes the first shuffle as both of its
3686 // vector operands, put the elements into the right order.
Evan Chengace3c172008-07-22 21:13:36 +00003687 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3688 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3689 &Mask1[0], Mask1.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003690
Dan Gohman475871a2008-07-27 21:46:04 +00003691 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003692 for (unsigned i = 0; i != 4; ++i) {
3693 if (Locs[i].first == -1)
3694 continue;
3695 else {
3696 unsigned Idx = (i < 2) ? 0 : 4;
3697 Idx += Locs[i].first * 2 + Locs[i].second;
3698 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3699 }
3700 }
3701
3702 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3703 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3704 &Mask2[0], Mask2.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003705 } else if (NumLo == 3 || NumHi == 3) {
3706 // Otherwise, we must have three elements from one vector, call it X, and
3707 // one element from the other, call it Y. First, use a shufps to build an
3708 // intermediate vector with the one element from Y and the element from X
3709 // that will be in the same half in the final destination (the indexes don't
3710 // matter). Then, use a shufps to build the final vector, taking the half
3711 // containing the element from Y from the intermediate, and the other half
3712 // from X.
3713 if (NumHi == 3) {
3714 // Normalize it so the 3 elements come from V1.
3715 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3716 std::swap(V1, V2);
3717 }
3718
3719 // Find the element from V2.
3720 unsigned HiIndex;
3721 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman475871a2008-07-27 21:46:04 +00003722 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003723 if (Elt.getOpcode() == ISD::UNDEF)
3724 continue;
3725 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3726 if (Val >= 4)
3727 break;
3728 }
3729
3730 Mask1[0] = PermMask.getOperand(HiIndex);
3731 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3732 Mask1[2] = PermMask.getOperand(HiIndex^1);
3733 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3734 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3735 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3736
3737 if (HiIndex >= 2) {
3738 Mask1[0] = PermMask.getOperand(0);
3739 Mask1[1] = PermMask.getOperand(1);
3740 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3741 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3742 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3743 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3744 } else {
3745 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3746 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3747 Mask1[2] = PermMask.getOperand(2);
3748 Mask1[3] = PermMask.getOperand(3);
3749 if (Mask1[2].getOpcode() != ISD::UNDEF)
3750 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3751 MaskEVT);
3752 if (Mask1[3].getOpcode() != ISD::UNDEF)
3753 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3754 MaskEVT);
3755 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3756 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3757 }
Evan Chengace3c172008-07-22 21:13:36 +00003758 }
3759
3760 // Break it into (shuffle shuffle_hi, shuffle_lo).
3761 Locs.clear();
Dan Gohman475871a2008-07-27 21:46:04 +00003762 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3763 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3764 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003765 unsigned MaskIdx = 0;
3766 unsigned LoIdx = 0;
3767 unsigned HiIdx = 2;
3768 for (unsigned i = 0; i != 4; ++i) {
3769 if (i == 2) {
3770 MaskPtr = &HiMask;
3771 MaskIdx = 1;
3772 LoIdx = 0;
3773 HiIdx = 2;
3774 }
Dan Gohman475871a2008-07-27 21:46:04 +00003775 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003776 if (Elt.getOpcode() == ISD::UNDEF) {
3777 Locs[i] = std::make_pair(-1, -1);
3778 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3779 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3780 (*MaskPtr)[LoIdx] = Elt;
3781 LoIdx++;
3782 } else {
3783 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3784 (*MaskPtr)[HiIdx] = Elt;
3785 HiIdx++;
3786 }
3787 }
3788
Dan Gohman475871a2008-07-27 21:46:04 +00003789 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003790 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3791 &LoMask[0], LoMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003792 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003793 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3794 &HiMask[0], HiMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003795 SmallVector<SDValue, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003796 for (unsigned i = 0; i != 4; ++i) {
3797 if (Locs[i].first == -1) {
3798 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3799 } else {
3800 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3801 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3802 }
3803 }
3804 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3805 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3806 &MaskOps[0], MaskOps.size()));
3807}
3808
Dan Gohman475871a2008-07-27 21:46:04 +00003809SDValue
3810X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3811 SDValue V1 = Op.getOperand(0);
3812 SDValue V2 = Op.getOperand(1);
3813 SDValue PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003814 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003815 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003816 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003817 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3818 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003819 bool V1IsSplat = false;
3820 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003821
Evan Cheng8cf723d2006-09-08 01:50:06 +00003822 if (isUndefShuffle(Op.Val))
3823 return DAG.getNode(ISD::UNDEF, VT);
3824
Evan Cheng213d2cf2007-05-17 18:45:50 +00003825 if (isZeroShuffle(Op.Val))
Evan Chengf0df0312008-05-15 08:39:06 +00003826 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003827
Evan Cheng49892af2007-06-19 00:02:56 +00003828 if (isIdentityMask(PermMask.Val))
3829 return V1;
3830 else if (isIdentityMask(PermMask.Val, true))
3831 return V2;
3832
Evan Cheng0db9fe62006-04-25 20:13:52 +00003833 if (isSplatMask(PermMask.Val)) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003834 if (isMMX || NumElems < 4) return Op;
3835 // Promote it to a v4{if}32 splat.
3836 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003837 }
3838
Evan Cheng7a831ce2007-12-15 03:00:47 +00003839 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3840 // do it!
3841 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003842 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003843 if (NewOp.Val)
3844 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3845 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3846 // FIXME: Figure out a cleaner way to do this.
3847 // Try to make use of movq to zero out the top part.
3848 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003849 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003850 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003851 if (NewOp.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00003852 SDValue NewV1 = NewOp.getOperand(0);
3853 SDValue NewV2 = NewOp.getOperand(1);
3854 SDValue NewMask = NewOp.getOperand(2);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003855 if (isCommutedMOVL(NewMask.Val, true, false)) {
3856 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chengd880b972008-05-09 21:53:03 +00003857 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003858 }
3859 }
3860 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003861 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003862 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003863 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chengd880b972008-05-09 21:53:03 +00003864 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng7e2ff772008-05-08 00:57:18 +00003865 DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003866 }
3867 }
3868
Evan Chengf26ffe92008-05-29 08:22:04 +00003869 // Check if this can be converted into a logical shift.
3870 bool isLeft = false;
3871 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003872 SDValue ShVal;
Evan Chengf26ffe92008-05-29 08:22:04 +00003873 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3874 if (isShift && ShVal.hasOneUse()) {
3875 // If the shifted value has multiple uses, it may be cheaper to use
3876 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003877 MVT EVT = VT.getVectorElementType();
3878 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003879 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3880 }
3881
Evan Cheng7e2ff772008-05-08 00:57:18 +00003882 if (X86::isMOVLMask(PermMask.Val)) {
3883 if (V1IsUndef)
3884 return V2;
3885 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chengd880b972008-05-09 21:53:03 +00003886 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003887 if (!isMMX)
3888 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003889 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003890
Nate Begemanfb8ead02008-07-25 19:05:58 +00003891 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.Val) ||
3892 X86::isMOVSLDUPMask(PermMask.Val) ||
3893 X86::isMOVHLPSMask(PermMask.Val) ||
3894 X86::isMOVHPMask(PermMask.Val) ||
3895 X86::isMOVLPMask(PermMask.Val)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003896 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003897
Evan Cheng9bbbb982006-10-25 20:48:19 +00003898 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3899 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003900 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003901
Evan Chengf26ffe92008-05-29 08:22:04 +00003902 if (isShift) {
3903 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003904 MVT EVT = VT.getVectorElementType();
3905 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003906 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3907 }
3908
Evan Cheng9eca5e82006-10-25 21:49:50 +00003909 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003910 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3911 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003912 V1IsSplat = isSplatVector(V1.Val);
3913 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003914
3915 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003916 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003917 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003918 std::swap(V1IsSplat, V2IsSplat);
3919 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003920 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003921 }
3922
Evan Cheng7a831ce2007-12-15 03:00:47 +00003923 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003924 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3925 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003926 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003927 if (V2IsSplat) {
3928 // V2 is a splat, so the mask may be malformed. That is, it may point
3929 // to any V2 element. The instruction selectior won't like this. Get
3930 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman475871a2008-07-27 21:46:04 +00003931 SDValue NewMask = getMOVLMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003932 if (NewMask.Val != PermMask.Val)
3933 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003934 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003935 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003936 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003937
Evan Chengd9b8e402006-10-16 06:36:00 +00003938 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003939 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003940 X86::isUNPCKLMask(PermMask.Val) ||
3941 X86::isUNPCKHMask(PermMask.Val))
3942 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003943
Evan Cheng9bbbb982006-10-25 20:48:19 +00003944 if (V2IsSplat) {
3945 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003946 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003947 // new vector_shuffle with the corrected mask.
Dan Gohman475871a2008-07-27 21:46:04 +00003948 SDValue NewMask = NormalizeMask(PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003949 if (NewMask.Val != PermMask.Val) {
3950 if (X86::isUNPCKLMask(PermMask.Val, true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003951 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003952 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3953 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003954 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003955 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003956 }
3957 }
3958 }
3959
3960 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003961 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3962 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3963
3964 if (Commuted) {
3965 // Commute is back and try unpck* again.
3966 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3967 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003968 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003969 X86::isUNPCKLMask(PermMask.Val) ||
3970 X86::isUNPCKHMask(PermMask.Val))
3971 return Op;
3972 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003973
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003974 // Try PSHUF* first, then SHUFP*.
3975 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3976 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3977 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3978 if (V2.getOpcode() != ISD::UNDEF)
3979 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3980 DAG.getNode(ISD::UNDEF, VT), PermMask);
3981 return Op;
3982 }
3983
3984 if (!isMMX) {
3985 if (Subtarget->hasSSE2() &&
3986 (X86::isPSHUFDMask(PermMask.Val) ||
3987 X86::isPSHUFHWMask(PermMask.Val) ||
3988 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003989 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003990 if (VT == MVT::v4f32) {
3991 RVT = MVT::v4i32;
3992 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3993 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3994 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3995 } else if (V2.getOpcode() != ISD::UNDEF)
3996 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3997 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3998 if (RVT != VT)
3999 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004000 return Op;
4001 }
4002
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004003 // Binary or unary shufps.
4004 if (X86::isSHUFPMask(PermMask.Val) ||
4005 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Evan Cheng0db9fe62006-04-25 20:13:52 +00004006 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004007 }
4008
Evan Cheng14b32e12007-12-11 01:46:18 +00004009 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4010 if (VT == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004011 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Evan Cheng14b32e12007-12-11 01:46:18 +00004012 if (NewOp.Val)
4013 return NewOp;
4014 }
4015
Evan Chengace3c172008-07-22 21:13:36 +00004016 // Handle all 4 wide cases with a number of shuffles except for MMX.
4017 if (NumElems == 4 && !isMMX)
4018 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004019
Dan Gohman475871a2008-07-27 21:46:04 +00004020 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004021}
4022
Dan Gohman475871a2008-07-27 21:46:04 +00004023SDValue
4024X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004025 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004026 MVT VT = Op.getValueType();
4027 if (VT.getSizeInBits() == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004028 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004029 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004030 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004031 DAG.getValueType(VT));
4032 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004033 } else if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004034 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004035 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004036 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004037 DAG.getValueType(VT));
4038 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004039 } else if (VT == MVT::f32) {
4040 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4041 // the result back to FR32 register. It's only worth matching if the
Dan Gohman171c11e2008-04-16 02:32:24 +00004042 // result has a single use which is a store or a bitcast to i32.
Evan Cheng62a3f152008-03-24 21:52:23 +00004043 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004044 return SDValue();
Dan Gohman89684502008-07-27 20:43:25 +00004045 SDNode *User = *Op.Val->use_begin();
Dan Gohman171c11e2008-04-16 02:32:24 +00004046 if (User->getOpcode() != ISD::STORE &&
4047 (User->getOpcode() != ISD::BIT_CONVERT ||
4048 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004049 return SDValue();
4050 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng62a3f152008-03-24 21:52:23 +00004051 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4052 Op.getOperand(1));
4053 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004054 }
Dan Gohman475871a2008-07-27 21:46:04 +00004055 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004056}
4057
4058
Dan Gohman475871a2008-07-27 21:46:04 +00004059SDValue
4060X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004061 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004062 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063
Evan Cheng62a3f152008-03-24 21:52:23 +00004064 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004065 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Evan Cheng62a3f152008-03-24 21:52:23 +00004066 if (Res.Val)
4067 return Res;
4068 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004069
Duncan Sands83ec4b62008-06-06 12:08:01 +00004070 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004071 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004072 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004073 SDValue Vec = Op.getOperand(0);
Evan Cheng14b32e12007-12-11 01:46:18 +00004074 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4075 if (Idx == 0)
4076 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4077 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4078 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4079 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004080 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004081 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman475871a2008-07-27 21:46:04 +00004082 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004083 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004084 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004085 DAG.getValueType(VT));
4086 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004087 } else if (VT.getSizeInBits() == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004088 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4089 if (Idx == 0)
4090 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004091 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004092 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004094 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004095 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004096 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004097 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004098 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004099 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004100 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004101 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004102 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004103 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004104 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004105 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00004106 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004107 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004108 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004109 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004110 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4111 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4112 // to match extract_elt for f64.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004113 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4114 if (Idx == 0)
4115 return Op;
4116
4117 // UNPCKHPD the element to the lowest double word, then movsd.
4118 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4119 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd038e042008-07-21 10:20:31 +00004120 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman475871a2008-07-27 21:46:04 +00004121 SmallVector<SDValue, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004122 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004123 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004124 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004125 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004126 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004127 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004128 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4129 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4130 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004131 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004132 }
4133
Dan Gohman475871a2008-07-27 21:46:04 +00004134 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004135}
4136
Dan Gohman475871a2008-07-27 21:46:04 +00004137SDValue
4138X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004139 MVT VT = Op.getValueType();
4140 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004141
Dan Gohman475871a2008-07-27 21:46:04 +00004142 SDValue N0 = Op.getOperand(0);
4143 SDValue N1 = Op.getOperand(1);
4144 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004145
Dan Gohmanef521f12008-08-14 22:53:18 +00004146 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4147 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004148 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004149 : X86ISD::PINSRW;
4150 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4151 // argument.
4152 if (N1.getValueType() != MVT::i32)
4153 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4154 if (N2.getValueType() != MVT::i32)
4155 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4156 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004157 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004158 // Bits [7:6] of the constant are the source select. This will always be
4159 // zero here. The DAG Combiner may combine an extract_elt index into these
4160 // bits. For example (insert (extract, 3), 2) could be matched by putting
4161 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4162 // Bits [5:4] of the constant are the destination select. This is the
4163 // value of the incoming immediate.
4164 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4165 // combine either bitwise AND or insert of float 0.0 to set these bits.
4166 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4167 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4168 }
Dan Gohman475871a2008-07-27 21:46:04 +00004169 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004170}
4171
Dan Gohman475871a2008-07-27 21:46:04 +00004172SDValue
4173X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004174 MVT VT = Op.getValueType();
4175 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004176
4177 if (Subtarget->hasSSE41())
4178 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4179
Evan Cheng794405e2007-12-12 07:55:34 +00004180 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004181 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004182
Dan Gohman475871a2008-07-27 21:46:04 +00004183 SDValue N0 = Op.getOperand(0);
4184 SDValue N1 = Op.getOperand(1);
4185 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004186
Duncan Sands83ec4b62008-06-06 12:08:01 +00004187 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004188 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4189 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004190 if (N1.getValueType() != MVT::i32)
4191 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4192 if (N2.getValueType() != MVT::i32)
Chris Lattner0bd48932008-01-17 07:00:52 +00004193 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004194 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004195 }
Dan Gohman475871a2008-07-27 21:46:04 +00004196 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004197}
4198
Dan Gohman475871a2008-07-27 21:46:04 +00004199SDValue
4200X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng52672b82008-07-22 18:39:19 +00004201 if (Op.getValueType() == MVT::v2f32)
4202 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4203 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4204 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4205 Op.getOperand(0))));
4206
Dan Gohman475871a2008-07-27 21:46:04 +00004207 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004208 MVT VT = MVT::v2i32;
4209 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004210 default: break;
4211 case MVT::v16i8:
4212 case MVT::v8i16:
4213 VT = MVT::v4i32;
4214 break;
4215 }
4216 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4217 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004218}
4219
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004220// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00004221// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4222// one of the above mentioned nodes. It has to be wrapped because otherwise
4223// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4224// be used to form addressing mode. These wrapped nodes will be selected
4225// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004226SDValue
4227X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004228 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004229 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Evan Chengd0ff02c2006-11-29 23:19:46 +00004230 getPointerTy(),
4231 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004232 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004233 // With PIC, the address is actually $g + Offset.
4234 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4235 !Subtarget->isPICStyleRIPRel()) {
4236 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4237 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4238 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004239 }
4240
4241 return Result;
4242}
4243
Dan Gohman475871a2008-07-27 21:46:04 +00004244SDValue
4245X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004246 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00004247 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004248 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004249 // With PIC, the address is actually $g + Offset.
4250 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4251 !Subtarget->isPICStyleRIPRel()) {
4252 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4253 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4254 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004255 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004256
4257 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4258 // load the value at address GV, not the value of GV itself. This means that
4259 // the GlobalAddress must be in the base or index register of the address, not
4260 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004261 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004262 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman69de1932008-02-06 22:27:42 +00004263 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004264 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004265
4266 return Result;
4267}
4268
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004269// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004270static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004271LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004272 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004273 SDValue InFlag;
4274 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004275 DAG.getNode(X86ISD::GlobalBaseReg,
4276 PtrVT), InFlag);
4277 InFlag = Chain.getValue(1);
4278
4279 // emit leal symbol@TLSGD(,%ebx,1), %eax
4280 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004281 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004282 GA->getValueType(0),
4283 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004284 SDValue Ops[] = { Chain, TGA, InFlag };
4285 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004286 InFlag = Result.getValue(2);
4287 Chain = Result.getValue(1);
4288
4289 // call ___tls_get_addr. This function receives its argument in
4290 // the register EAX.
4291 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4292 InFlag = Chain.getValue(1);
4293
4294 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004295 SDValue Ops1[] = { Chain,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004296 DAG.getTargetExternalSymbol("___tls_get_addr",
4297 PtrVT),
4298 DAG.getRegister(X86::EAX, PtrVT),
4299 DAG.getRegister(X86::EBX, PtrVT),
4300 InFlag };
4301 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4302 InFlag = Chain.getValue(1);
4303
4304 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4305}
4306
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004307// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004308static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004309LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004310 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004311 SDValue InFlag, Chain;
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004312
4313 // emit leaq symbol@TLSGD(%rip), %rdi
4314 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004315 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004316 GA->getValueType(0),
4317 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004318 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4319 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004320 Chain = Result.getValue(1);
4321 InFlag = Result.getValue(2);
4322
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004323 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004324 // the register RDI.
4325 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4326 InFlag = Chain.getValue(1);
4327
4328 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004329 SDValue Ops1[] = { Chain,
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004330 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004331 PtrVT),
4332 DAG.getRegister(X86::RDI, PtrVT),
4333 InFlag };
4334 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4335 InFlag = Chain.getValue(1);
4336
4337 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4338}
4339
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004340// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4341// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004342static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004343 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004344 // Get the Thread Pointer
Dan Gohman475871a2008-07-27 21:46:04 +00004345 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004346 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4347 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004348 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004349 GA->getValueType(0),
4350 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004351 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004352
4353 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman69de1932008-02-06 22:27:42 +00004354 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004355 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004356
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004357 // The address of the thread local variable is the add of the thread
4358 // pointer with the offset of the variable.
4359 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4360}
4361
Dan Gohman475871a2008-07-27 21:46:04 +00004362SDValue
4363X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004364 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004365 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004366 assert(Subtarget->isTargetELF() &&
4367 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004368 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4369 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4370 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004371 if (Subtarget->is64Bit()) {
4372 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4373 } else {
4374 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4375 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4376 else
4377 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4378 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004379}
4380
Dan Gohman475871a2008-07-27 21:46:04 +00004381SDValue
4382X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004383 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman475871a2008-07-27 21:46:04 +00004384 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004385 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004386 // With PIC, the address is actually $g + Offset.
4387 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4388 !Subtarget->isPICStyleRIPRel()) {
4389 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4390 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4391 Result);
4392 }
4393
4394 return Result;
4395}
4396
Dan Gohman475871a2008-07-27 21:46:04 +00004397SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004398 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004399 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004400 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4401 // With PIC, the address is actually $g + Offset.
4402 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4403 !Subtarget->isPICStyleRIPRel()) {
4404 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4405 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4406 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004407 }
4408
4409 return Result;
4410}
4411
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004412/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4413/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004414SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004415 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004416 MVT VT = Op.getValueType();
4417 unsigned VTBits = VT.getSizeInBits();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004418 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004419 SDValue ShOpLo = Op.getOperand(0);
4420 SDValue ShOpHi = Op.getOperand(1);
4421 SDValue ShAmt = Op.getOperand(2);
4422 SDValue Tmp1 = isSRA ?
Dan Gohman4c1fa612008-03-03 22:22:09 +00004423 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4424 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004425
Dan Gohman475871a2008-07-27 21:46:04 +00004426 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004427 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004428 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4429 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004430 } else {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004431 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4432 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004433 }
Evan Chenge3413162006-01-09 18:33:28 +00004434
Dan Gohman475871a2008-07-27 21:46:04 +00004435 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004436 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00004437 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004438 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004439
Dan Gohman475871a2008-07-27 21:46:04 +00004440 SDValue Hi, Lo;
4441 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4442 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4443 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004444
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004445 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf9516202008-06-30 10:19:09 +00004446 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4447 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004448 } else {
Duncan Sandsf9516202008-06-30 10:19:09 +00004449 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4450 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004451 }
4452
Dan Gohman475871a2008-07-27 21:46:04 +00004453 SDValue Ops[2] = { Lo, Hi };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004454 return DAG.getMergeValues(Ops, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004455}
Evan Chenga3195e82006-01-12 22:54:21 +00004456
Dan Gohman475871a2008-07-27 21:46:04 +00004457SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004458 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004459 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004460 "Unknown SINT_TO_FP to lower!");
4461
4462 // These are really Legal; caller falls through into that case.
4463 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004464 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004465 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4466 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004467 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004468
Duncan Sands83ec4b62008-06-06 12:08:01 +00004469 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004470 MachineFunction &MF = DAG.getMachineFunction();
4471 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004472 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4473 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004474 StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004475 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004476
4477 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004478 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004479 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004480 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004481 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4482 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004483 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004484 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485 Ops.push_back(Chain);
4486 Ops.push_back(StackSlot);
4487 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman475871a2008-07-27 21:46:04 +00004488 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004489 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004490
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004491 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004492 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004493 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004494
4495 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4496 // shouldn't be necessary except that RFP cannot be live across
4497 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004498 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004499 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004500 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004501 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004502 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004503 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004504 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004505 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004506 Ops.push_back(DAG.getValueType(Op.getValueType()));
4507 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004508 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman69de1932008-02-06 22:27:42 +00004509 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004510 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004511 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004512
Evan Cheng0db9fe62006-04-25 20:13:52 +00004513 return Result;
4514}
4515
Dan Gohman475871a2008-07-27 21:46:04 +00004516std::pair<SDValue,SDValue> X86TargetLowering::
4517FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004518 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4519 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004520 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004521
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004522 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004523 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004524 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004525 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004526 if (Subtarget->is64Bit() &&
4527 Op.getValueType() == MVT::i64 &&
4528 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00004529 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004530
Evan Cheng87c89352007-10-15 20:11:21 +00004531 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4532 // stack slot.
4533 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004534 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004535 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004536 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004537 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004538 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004539 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4540 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4541 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4542 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004543 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004544
Dan Gohman475871a2008-07-27 21:46:04 +00004545 SDValue Chain = DAG.getEntryNode();
4546 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004547 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004548 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman69de1932008-02-06 22:27:42 +00004549 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004550 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004551 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004552 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004553 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4554 };
4555 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004556 Chain = Value.getValue(1);
4557 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4558 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4559 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004560
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004562 SDValue Ops[] = { Chain, Value, StackSlot };
4563 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004564
Chris Lattner27a6c732007-11-24 07:07:01 +00004565 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566}
4567
Dan Gohman475871a2008-07-27 21:46:04 +00004568SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4569 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4570 SDValue FIST = Vals.first, StackSlot = Vals.second;
4571 if (FIST.Val == 0) return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00004572
4573 // Load the result.
4574 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4575}
4576
4577SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00004578 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4579 SDValue FIST = Vals.first, StackSlot = Vals.second;
Chris Lattner27a6c732007-11-24 07:07:01 +00004580 if (FIST.Val == 0) return 0;
Duncan Sandsf9516202008-06-30 10:19:09 +00004581
4582 MVT VT = N->getValueType(0);
4583
4584 // Return a load from the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004585 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004586
Duncan Sands4bdcb612008-07-02 17:40:58 +00004587 // Use MERGE_VALUES to drop the chain result value and get a node with one
4588 // result. This requires turning off getMergeValues simplification, since
4589 // otherwise it will give us Res back.
4590 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf9516202008-06-30 10:19:09 +00004591}
Chris Lattner27a6c732007-11-24 07:07:01 +00004592
Dan Gohman475871a2008-07-27 21:46:04 +00004593SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004594 MVT VT = Op.getValueType();
4595 MVT EltVT = VT;
4596 if (VT.isVector())
4597 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004598 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004599 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004600 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004601 CV.push_back(C);
4602 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004603 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004604 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004605 CV.push_back(C);
4606 CV.push_back(C);
4607 CV.push_back(C);
4608 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609 }
Dan Gohmand3006222007-07-27 17:16:43 +00004610 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004611 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4612 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004613 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004614 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004615 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4616}
4617
Dan Gohman475871a2008-07-27 21:46:04 +00004618SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004619 MVT VT = Op.getValueType();
4620 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004621 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004622 if (VT.isVector()) {
4623 EltVT = VT.getVectorElementType();
4624 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004625 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004626 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004627 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004628 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004629 CV.push_back(C);
4630 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004631 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004632 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004633 CV.push_back(C);
4634 CV.push_back(C);
4635 CV.push_back(C);
4636 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004637 }
Dan Gohmand3006222007-07-27 17:16:43 +00004638 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004639 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4640 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004641 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004642 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004643 if (VT.isVector()) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004644 return DAG.getNode(ISD::BIT_CONVERT, VT,
4645 DAG.getNode(ISD::XOR, MVT::v2i64,
4646 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4647 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4648 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004649 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4650 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004651}
4652
Dan Gohman475871a2008-07-27 21:46:04 +00004653SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4654 SDValue Op0 = Op.getOperand(0);
4655 SDValue Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004656 MVT VT = Op.getValueType();
4657 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004658
4659 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004660 if (SrcVT.bitsLT(VT)) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004661 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4662 SrcVT = VT;
4663 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004664 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004665 if (SrcVT.bitsGT(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004666 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004667 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004668 }
4669
4670 // At this point the operands and the result should have the same
4671 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004672
Evan Cheng68c47cb2007-01-05 07:55:56 +00004673 // First get the sign bit of second operand.
4674 std::vector<Constant*> CV;
4675 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004676 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4677 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004678 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004679 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4680 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4681 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4682 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004683 }
Dan Gohmand3006222007-07-27 17:16:43 +00004684 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004685 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4686 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004687 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004688 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004689 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004690
4691 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004692 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004693 // Op0 is MVT::f32, Op1 is MVT::f64.
4694 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4695 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4696 DAG.getConstant(32, MVT::i32));
4697 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4698 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004699 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004700 }
4701
Evan Cheng73d6cf12007-01-05 21:37:56 +00004702 // Clear first operand sign bit.
4703 CV.clear();
4704 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004705 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4706 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004707 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004708 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4709 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4710 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4711 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004712 }
Dan Gohmand3006222007-07-27 17:16:43 +00004713 C = ConstantVector::get(CV);
4714 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman475871a2008-07-27 21:46:04 +00004715 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004716 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004717 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004718 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004719
4720 // Or the value with the sign bit.
4721 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004722}
4723
Dan Gohman475871a2008-07-27 21:46:04 +00004724SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004725 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00004726 SDValue Cond;
4727 SDValue Op0 = Op.getOperand(0);
4728 SDValue Op1 = Op.getOperand(1);
4729 SDValue CC = Op.getOperand(2);
Evan Cheng0488db92007-09-25 01:57:46 +00004730 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004731 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng0488db92007-09-25 01:57:46 +00004732 unsigned X86CC;
4733
Evan Cheng0488db92007-09-25 01:57:46 +00004734 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004735 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004736 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4737 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004738 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004739 }
Evan Cheng0488db92007-09-25 01:57:46 +00004740
4741 assert(isFP && "Illegal integer SetCC!");
4742
Evan Chenge5f62042007-09-29 00:00:36 +00004743 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004744 switch (SetCCOpcode) {
4745 default: assert(false && "Illegal floating point SetCC!");
4746 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman475871a2008-07-27 21:46:04 +00004747 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004748 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman475871a2008-07-27 21:46:04 +00004749 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004750 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4751 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4752 }
4753 case ISD::SETUNE: { // PF | !ZF
Dan Gohman475871a2008-07-27 21:46:04 +00004754 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004755 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman475871a2008-07-27 21:46:04 +00004756 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004757 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4758 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4759 }
4760 }
4761}
4762
Dan Gohman475871a2008-07-27 21:46:04 +00004763SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4764 SDValue Cond;
4765 SDValue Op0 = Op.getOperand(0);
4766 SDValue Op1 = Op.getOperand(1);
4767 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00004768 MVT VT = Op.getValueType();
4769 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4770 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4771
4772 if (isFP) {
4773 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00004774 MVT VT0 = Op0.getValueType();
4775 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4776 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00004777 bool Swap = false;
4778
4779 switch (SetCCOpcode) {
4780 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004781 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00004782 case ISD::SETEQ: SSECC = 0; break;
4783 case ISD::SETOGT:
4784 case ISD::SETGT: Swap = true; // Fallthrough
4785 case ISD::SETLT:
4786 case ISD::SETOLT: SSECC = 1; break;
4787 case ISD::SETOGE:
4788 case ISD::SETGE: Swap = true; // Fallthrough
4789 case ISD::SETLE:
4790 case ISD::SETOLE: SSECC = 2; break;
4791 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004792 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00004793 case ISD::SETNE: SSECC = 4; break;
4794 case ISD::SETULE: Swap = true;
4795 case ISD::SETUGE: SSECC = 5; break;
4796 case ISD::SETULT: Swap = true;
4797 case ISD::SETUGT: SSECC = 6; break;
4798 case ISD::SETO: SSECC = 7; break;
4799 }
4800 if (Swap)
4801 std::swap(Op0, Op1);
4802
Nate Begemanfb8ead02008-07-25 19:05:58 +00004803 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00004804 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00004805 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue UNORD, EQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004807 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4808 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4809 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4810 }
4811 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004812 SDValue ORD, NEQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004813 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4814 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4815 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4816 }
4817 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00004818 }
4819 // Handle all other FP comparisons here.
4820 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4821 }
4822
4823 // We are handling one of the integer comparisons here. Since SSE only has
4824 // GT and EQ comparisons for integer, swapping operands and multiple
4825 // operations may be required for some comparisons.
4826 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4827 bool Swap = false, Invert = false, FlipSigns = false;
4828
4829 switch (VT.getSimpleVT()) {
4830 default: break;
4831 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4832 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4833 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4834 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4835 }
4836
4837 switch (SetCCOpcode) {
4838 default: break;
4839 case ISD::SETNE: Invert = true;
4840 case ISD::SETEQ: Opc = EQOpc; break;
4841 case ISD::SETLT: Swap = true;
4842 case ISD::SETGT: Opc = GTOpc; break;
4843 case ISD::SETGE: Swap = true;
4844 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4845 case ISD::SETULT: Swap = true;
4846 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4847 case ISD::SETUGE: Swap = true;
4848 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4849 }
4850 if (Swap)
4851 std::swap(Op0, Op1);
4852
4853 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4854 // bits of the inputs before performing those operations.
4855 if (FlipSigns) {
4856 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00004857 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4858 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4859 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00004860 SignBits.size());
4861 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4862 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4863 }
4864
Dan Gohman475871a2008-07-27 21:46:04 +00004865 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00004866
4867 // If the logical-not of the result is required, perform that now.
4868 if (Invert) {
4869 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00004870 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4871 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4872 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00004873 NegOnes.size());
4874 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4875 }
4876 return Result;
4877}
Evan Cheng0488db92007-09-25 01:57:46 +00004878
Dan Gohman475871a2008-07-27 21:46:04 +00004879SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004880 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004881 SDValue Cond = Op.getOperand(0);
4882 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004883
Evan Cheng734503b2006-09-11 02:19:56 +00004884 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004885 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004886
Evan Cheng3f41d662007-10-08 22:16:29 +00004887 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4888 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004889 if (Cond.getOpcode() == X86ISD::SETCC) {
4890 CC = Cond.getOperand(0);
4891
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00004893 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004894 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00004895
Evan Cheng3f41d662007-10-08 22:16:29 +00004896 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004897 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00004898 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng3f41d662007-10-08 22:16:29 +00004899 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner1956d152008-01-16 06:19:45 +00004900
Evan Chenge5f62042007-09-29 00:00:36 +00004901 if ((Opc == X86ISD::CMP ||
4902 Opc == X86ISD::COMI ||
4903 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004904 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004905 addTest = false;
4906 }
4907 }
4908
4909 if (addTest) {
4910 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004911 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004912 }
4913
Duncan Sands83ec4b62008-06-06 12:08:01 +00004914 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004915 MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004916 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00004917 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4918 // condition is true.
4919 Ops.push_back(Op.getOperand(2));
4920 Ops.push_back(Op.getOperand(1));
4921 Ops.push_back(CC);
4922 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004923 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004924}
4925
Dan Gohman475871a2008-07-27 21:46:04 +00004926SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004927 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004928 SDValue Chain = Op.getOperand(0);
4929 SDValue Cond = Op.getOperand(1);
4930 SDValue Dest = Op.getOperand(2);
4931 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004932
Evan Cheng0db9fe62006-04-25 20:13:52 +00004933 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004934 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004935
Evan Cheng3f41d662007-10-08 22:16:29 +00004936 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4937 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004938 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004939 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004940
Dan Gohman475871a2008-07-27 21:46:04 +00004941 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00004942 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004943 if (Opc == X86ISD::CMP ||
4944 Opc == X86ISD::COMI ||
4945 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004946 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004947 addTest = false;
4948 }
4949 }
4950
4951 if (addTest) {
4952 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004953 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004954 }
Evan Chenge5f62042007-09-29 00:00:36 +00004955 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004956 Chain, Op.getOperand(2), CC, Cond);
4957}
4958
Anton Korobeynikove060b532007-04-17 19:34:00 +00004959
4960// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4961// Calls to _alloca is needed to probe the stack when allocating more than 4k
4962// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4963// that the guard pages used by the OS virtual memory manager are allocated in
4964// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00004965SDValue
4966X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004967 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004968 assert(Subtarget->isTargetCygMing() &&
4969 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004970
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004971 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004972 SDValue Chain = Op.getOperand(0);
4973 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004974 // FIXME: Ensure alignment here
4975
Dan Gohman475871a2008-07-27 21:46:04 +00004976 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004977
Duncan Sands83ec4b62008-06-06 12:08:01 +00004978 MVT IntPtr = getPointerTy();
4979 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004980
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004981 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4982
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004983 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4984 Flag = Chain.getValue(1);
4985
4986 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004987 SDValue Ops[] = { Chain,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004988 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4989 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004990 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004991 Flag };
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004992 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004993 Flag = Chain.getValue(1);
4994
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004995 Chain = DAG.getCALLSEQ_END(Chain,
4996 DAG.getIntPtrConstant(0),
4997 DAG.getIntPtrConstant(0),
4998 Flag);
4999
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005000 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005001
Dan Gohman475871a2008-07-27 21:46:04 +00005002 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005003 return DAG.getMergeValues(Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005004}
5005
Dan Gohman475871a2008-07-27 21:46:04 +00005006SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005007X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00005008 SDValue Chain,
5009 SDValue Dst, SDValue Src,
5010 SDValue Size, unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00005011 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005012 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005013
Dan Gohman707e0182008-04-12 04:36:06 +00005014 /// If not DWORD aligned or size is more than the threshold, call the library.
5015 /// The libc version is likely to be faster for these cases. It can use the
5016 /// address value and run time information about the CPU.
5017 if ((Align & 3) == 0 ||
5018 !ConstantSize ||
5019 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005020 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005021
5022 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005023 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5024 if (const char *bzeroEntry =
5025 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005026 MVT IntPtr = getPointerTy();
Dan Gohman707e0182008-04-12 04:36:06 +00005027 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5028 TargetLowering::ArgListTy Args;
5029 TargetLowering::ArgListEntry Entry;
5030 Entry.Node = Dst;
Dan Gohman68d599d2008-04-01 20:38:36 +00005031 Entry.Ty = IntPtrTy;
5032 Args.push_back(Entry);
Dan Gohman707e0182008-04-12 04:36:06 +00005033 Entry.Node = Size;
5034 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00005035 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00005036 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5037 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5038 Args, DAG);
5039 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005040 }
5041
Dan Gohman707e0182008-04-12 04:36:06 +00005042 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005043 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005044 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005045
Dan Gohman707e0182008-04-12 04:36:06 +00005046 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005047 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005048 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005049 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005050 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005051 unsigned BytesLeft = 0;
5052 bool TwoRepStos = false;
5053 if (ValC) {
5054 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00005055 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005056
Evan Cheng0db9fe62006-04-25 20:13:52 +00005057 // If the value is a constant, then we can potentially use larger sets.
5058 switch (Align & 3) {
5059 case 2: // WORD aligned
5060 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005061 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00005062 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005063 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00005064 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00005065 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00005066 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005067 Val = (Val << 8) | Val;
5068 Val = (Val << 16) | Val;
Dan Gohman6f836ad2008-04-12 02:35:39 +00005069 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Evan Cheng25ab6902006-09-08 06:48:29 +00005070 AVT = MVT::i64;
5071 ValReg = X86::RAX;
5072 Val = (Val << 32) | Val;
5073 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005074 break;
5075 default: // Byte aligned
5076 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005077 ValReg = X86::AL;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005078 Count = DAG.getIntPtrConstant(SizeVal);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005079 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005080 }
5081
Duncan Sands8e4eb092008-06-08 20:54:56 +00005082 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005083 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005084 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5085 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005086 }
5087
Evan Cheng0db9fe62006-04-25 20:13:52 +00005088 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5089 InFlag);
5090 InFlag = Chain.getValue(1);
5091 } else {
5092 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005093 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohman707e0182008-04-12 04:36:06 +00005094 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005095 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005096 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005097
Evan Cheng25ab6902006-09-08 06:48:29 +00005098 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5099 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005100 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005101 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005102 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005103 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005104
Chris Lattnerd96d0722007-02-25 06:40:16 +00005105 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005106 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005107 Ops.push_back(Chain);
5108 Ops.push_back(DAG.getValueType(AVT));
5109 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005110 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005111
Evan Cheng0db9fe62006-04-25 20:13:52 +00005112 if (TwoRepStos) {
5113 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005114 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005115 MVT CVT = Count.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005116 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005117 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5118 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5119 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005120 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005121 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005122 Ops.clear();
5123 Ops.push_back(Chain);
5124 Ops.push_back(DAG.getValueType(MVT::i8));
5125 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005126 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005127 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005128 // Handle the last 1 - 7 bytes.
5129 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005130 MVT AddrVT = Dst.getValueType();
5131 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005132
5133 Chain = DAG.getMemset(Chain,
5134 DAG.getNode(ISD::ADD, AddrVT, Dst,
5135 DAG.getConstant(Offset, AddrVT)),
5136 Src,
5137 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005138 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005139 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005140
Dan Gohman707e0182008-04-12 04:36:06 +00005141 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142 return Chain;
5143}
Evan Cheng11e15b32006-04-03 20:53:28 +00005144
Dan Gohman475871a2008-07-27 21:46:04 +00005145SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005146X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00005147 SDValue Chain,
5148 SDValue Dst, SDValue Src,
5149 SDValue Size, unsigned Align,
Dan Gohman707e0182008-04-12 04:36:06 +00005150 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005151 const Value *DstSV, uint64_t DstSVOff,
5152 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00005153
5154 // This requires the copy size to be a constant, preferrably
5155 // within a subtarget-specific limit.
5156 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5157 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005158 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005159 uint64_t SizeVal = ConstantSize->getValue();
5160 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005161 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005162
Duncan Sands83ec4b62008-06-06 12:08:01 +00005163 MVT AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005164 unsigned BytesLeft = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00005165 if (Align >= 8 && Subtarget->is64Bit())
5166 AVT = MVT::i64;
5167 else if (Align >= 4)
5168 AVT = MVT::i32;
5169 else if (Align >= 2)
5170 AVT = MVT::i16;
5171 else
5172 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005173
Duncan Sands83ec4b62008-06-06 12:08:01 +00005174 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005175 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005176 SDValue Count = DAG.getIntPtrConstant(CountVal);
Dan Gohman707e0182008-04-12 04:36:06 +00005177 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005178
Dan Gohman475871a2008-07-27 21:46:04 +00005179 SDValue InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005180 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5181 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005182 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005183 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005184 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005185 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005186 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005187 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005188 InFlag = Chain.getValue(1);
5189
Chris Lattnerd96d0722007-02-25 06:40:16 +00005190 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005191 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005192 Ops.push_back(Chain);
5193 Ops.push_back(DAG.getValueType(AVT));
5194 Ops.push_back(InFlag);
Dan Gohman475871a2008-07-27 21:46:04 +00005195 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005196
Dan Gohman475871a2008-07-27 21:46:04 +00005197 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005198 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005199 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005200 // Handle the last 1 - 7 bytes.
5201 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005202 MVT DstVT = Dst.getValueType();
5203 MVT SrcVT = Src.getValueType();
5204 MVT SizeVT = Size.getValueType();
Evan Cheng2749c722008-04-25 00:26:43 +00005205 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00005206 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005207 DAG.getConstant(Offset, DstVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005208 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005209 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005210 DAG.getConstant(BytesLeft, SizeVT),
5211 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005212 DstSV, DstSVOff + Offset,
5213 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005214 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005215
Dan Gohman707e0182008-04-12 04:36:06 +00005216 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005217}
5218
Chris Lattner27a6c732007-11-24 07:07:01 +00005219/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5220SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00005221 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005222 SDValue TheChain = N->getOperand(0);
5223 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005224 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005225 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5226 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005227 MVT::i64, rax.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00005228 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005229 DAG.getConstant(32, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00005230 SDValue Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00005231 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00005232 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00005233
Duncan Sands4bdcb612008-07-02 17:40:58 +00005234 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005235 }
Chris Lattner5a88b832007-02-25 07:10:00 +00005236
Dan Gohman475871a2008-07-27 21:46:04 +00005237 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5238 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005239 MVT::i32, eax.getValue(2));
5240 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman475871a2008-07-27 21:46:04 +00005241 SDValue Ops[] = { eax, edx };
Chris Lattner27a6c732007-11-24 07:07:01 +00005242 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5243
5244 // Use a MERGE_VALUES to return the value and chain.
5245 Ops[1] = edx.getValue(1);
Duncan Sands4bdcb612008-07-02 17:40:58 +00005246 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005247}
5248
Dan Gohman475871a2008-07-27 21:46:04 +00005249SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005250 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005251
Evan Cheng25ab6902006-09-08 06:48:29 +00005252 if (!Subtarget->is64Bit()) {
5253 // vastart just stores the address of the VarArgsFrameIndex slot into the
5254 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005255 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005256 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005257 }
5258
5259 // __va_list_tag:
5260 // gp_offset (0 - 6 * 8)
5261 // fp_offset (48 - 48 + 8 * 16)
5262 // overflow_arg_area (point to parameters coming in memory).
5263 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005264 SmallVector<SDValue, 8> MemOps;
5265 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005266 // Store gp_offset
Dan Gohman475871a2008-07-27 21:46:04 +00005267 SDValue Store = DAG.getStore(Op.getOperand(0),
Evan Cheng786225a2006-10-05 23:01:46 +00005268 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005269 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005270 MemOps.push_back(Store);
5271
5272 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00005273 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00005274 Store = DAG.getStore(Op.getOperand(0),
5275 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005276 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005277 MemOps.push_back(Store);
5278
5279 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00005280 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005281 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005282 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005283 MemOps.push_back(Store);
5284
5285 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00005286 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005287 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005288 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005289 MemOps.push_back(Store);
5290 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005291}
5292
Dan Gohman475871a2008-07-27 21:46:04 +00005293SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005294 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5295 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005296 SDValue Chain = Op.getOperand(0);
5297 SDValue SrcPtr = Op.getOperand(1);
5298 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005299
5300 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5301 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005302 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005303}
5304
Dan Gohman475871a2008-07-27 21:46:04 +00005305SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005306 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005307 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005308 SDValue Chain = Op.getOperand(0);
5309 SDValue DstPtr = Op.getOperand(1);
5310 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005311 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5312 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengae642192007-03-02 23:16:35 +00005313
Dan Gohman28269132008-04-18 20:55:41 +00005314 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5315 DAG.getIntPtrConstant(24), 8, false,
5316 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005317}
5318
Dan Gohman475871a2008-07-27 21:46:04 +00005319SDValue
5320X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005321 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5322 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005323 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005324 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005325 case Intrinsic::x86_sse_comieq_ss:
5326 case Intrinsic::x86_sse_comilt_ss:
5327 case Intrinsic::x86_sse_comile_ss:
5328 case Intrinsic::x86_sse_comigt_ss:
5329 case Intrinsic::x86_sse_comige_ss:
5330 case Intrinsic::x86_sse_comineq_ss:
5331 case Intrinsic::x86_sse_ucomieq_ss:
5332 case Intrinsic::x86_sse_ucomilt_ss:
5333 case Intrinsic::x86_sse_ucomile_ss:
5334 case Intrinsic::x86_sse_ucomigt_ss:
5335 case Intrinsic::x86_sse_ucomige_ss:
5336 case Intrinsic::x86_sse_ucomineq_ss:
5337 case Intrinsic::x86_sse2_comieq_sd:
5338 case Intrinsic::x86_sse2_comilt_sd:
5339 case Intrinsic::x86_sse2_comile_sd:
5340 case Intrinsic::x86_sse2_comigt_sd:
5341 case Intrinsic::x86_sse2_comige_sd:
5342 case Intrinsic::x86_sse2_comineq_sd:
5343 case Intrinsic::x86_sse2_ucomieq_sd:
5344 case Intrinsic::x86_sse2_ucomilt_sd:
5345 case Intrinsic::x86_sse2_ucomile_sd:
5346 case Intrinsic::x86_sse2_ucomigt_sd:
5347 case Intrinsic::x86_sse2_ucomige_sd:
5348 case Intrinsic::x86_sse2_ucomineq_sd: {
5349 unsigned Opc = 0;
5350 ISD::CondCode CC = ISD::SETCC_INVALID;
5351 switch (IntNo) {
5352 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005353 case Intrinsic::x86_sse_comieq_ss:
5354 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005355 Opc = X86ISD::COMI;
5356 CC = ISD::SETEQ;
5357 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005358 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005359 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005360 Opc = X86ISD::COMI;
5361 CC = ISD::SETLT;
5362 break;
5363 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005364 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005365 Opc = X86ISD::COMI;
5366 CC = ISD::SETLE;
5367 break;
5368 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005369 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005370 Opc = X86ISD::COMI;
5371 CC = ISD::SETGT;
5372 break;
5373 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005374 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005375 Opc = X86ISD::COMI;
5376 CC = ISD::SETGE;
5377 break;
5378 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005379 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005380 Opc = X86ISD::COMI;
5381 CC = ISD::SETNE;
5382 break;
5383 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005384 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005385 Opc = X86ISD::UCOMI;
5386 CC = ISD::SETEQ;
5387 break;
5388 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005389 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005390 Opc = X86ISD::UCOMI;
5391 CC = ISD::SETLT;
5392 break;
5393 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005394 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005395 Opc = X86ISD::UCOMI;
5396 CC = ISD::SETLE;
5397 break;
5398 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005399 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005400 Opc = X86ISD::UCOMI;
5401 CC = ISD::SETGT;
5402 break;
5403 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005404 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005405 Opc = X86ISD::UCOMI;
5406 CC = ISD::SETGE;
5407 break;
5408 case Intrinsic::x86_sse_ucomineq_ss:
5409 case Intrinsic::x86_sse2_ucomineq_sd:
5410 Opc = X86ISD::UCOMI;
5411 CC = ISD::SETNE;
5412 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005413 }
Evan Cheng734503b2006-09-11 02:19:56 +00005414
Evan Cheng0db9fe62006-04-25 20:13:52 +00005415 unsigned X86CC;
Dan Gohman475871a2008-07-27 21:46:04 +00005416 SDValue LHS = Op.getOperand(1);
5417 SDValue RHS = Op.getOperand(2);
Chris Lattnerf9570512006-09-13 03:22:10 +00005418 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005419
Dan Gohman475871a2008-07-27 21:46:04 +00005420 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5421 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Chenge5f62042007-09-29 00:00:36 +00005422 DAG.getConstant(X86CC, MVT::i8), Cond);
5423 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005424 }
Evan Cheng5759f972008-05-04 09:15:50 +00005425
5426 // Fix vector shift instructions where the last operand is a non-immediate
5427 // i32 value.
5428 case Intrinsic::x86_sse2_pslli_w:
5429 case Intrinsic::x86_sse2_pslli_d:
5430 case Intrinsic::x86_sse2_pslli_q:
5431 case Intrinsic::x86_sse2_psrli_w:
5432 case Intrinsic::x86_sse2_psrli_d:
5433 case Intrinsic::x86_sse2_psrli_q:
5434 case Intrinsic::x86_sse2_psrai_w:
5435 case Intrinsic::x86_sse2_psrai_d:
5436 case Intrinsic::x86_mmx_pslli_w:
5437 case Intrinsic::x86_mmx_pslli_d:
5438 case Intrinsic::x86_mmx_pslli_q:
5439 case Intrinsic::x86_mmx_psrli_w:
5440 case Intrinsic::x86_mmx_psrli_d:
5441 case Intrinsic::x86_mmx_psrli_q:
5442 case Intrinsic::x86_mmx_psrai_w:
5443 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005444 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005445 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005446 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005447
5448 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005449 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005450 switch (IntNo) {
5451 case Intrinsic::x86_sse2_pslli_w:
5452 NewIntNo = Intrinsic::x86_sse2_psll_w;
5453 break;
5454 case Intrinsic::x86_sse2_pslli_d:
5455 NewIntNo = Intrinsic::x86_sse2_psll_d;
5456 break;
5457 case Intrinsic::x86_sse2_pslli_q:
5458 NewIntNo = Intrinsic::x86_sse2_psll_q;
5459 break;
5460 case Intrinsic::x86_sse2_psrli_w:
5461 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5462 break;
5463 case Intrinsic::x86_sse2_psrli_d:
5464 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5465 break;
5466 case Intrinsic::x86_sse2_psrli_q:
5467 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5468 break;
5469 case Intrinsic::x86_sse2_psrai_w:
5470 NewIntNo = Intrinsic::x86_sse2_psra_w;
5471 break;
5472 case Intrinsic::x86_sse2_psrai_d:
5473 NewIntNo = Intrinsic::x86_sse2_psra_d;
5474 break;
5475 default: {
5476 ShAmtVT = MVT::v2i32;
5477 switch (IntNo) {
5478 case Intrinsic::x86_mmx_pslli_w:
5479 NewIntNo = Intrinsic::x86_mmx_psll_w;
5480 break;
5481 case Intrinsic::x86_mmx_pslli_d:
5482 NewIntNo = Intrinsic::x86_mmx_psll_d;
5483 break;
5484 case Intrinsic::x86_mmx_pslli_q:
5485 NewIntNo = Intrinsic::x86_mmx_psll_q;
5486 break;
5487 case Intrinsic::x86_mmx_psrli_w:
5488 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5489 break;
5490 case Intrinsic::x86_mmx_psrli_d:
5491 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5492 break;
5493 case Intrinsic::x86_mmx_psrli_q:
5494 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5495 break;
5496 case Intrinsic::x86_mmx_psrai_w:
5497 NewIntNo = Intrinsic::x86_mmx_psra_w;
5498 break;
5499 case Intrinsic::x86_mmx_psrai_d:
5500 NewIntNo = Intrinsic::x86_mmx_psra_d;
5501 break;
5502 default: abort(); // Can't reach here.
5503 }
5504 break;
5505 }
5506 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00005507 MVT VT = Op.getValueType();
Evan Cheng5759f972008-05-04 09:15:50 +00005508 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5509 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5510 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5511 DAG.getConstant(NewIntNo, MVT::i32),
5512 Op.getOperand(1), ShAmt);
5513 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005514 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005515}
Evan Cheng72261582005-12-20 06:22:03 +00005516
Dan Gohman475871a2008-07-27 21:46:04 +00005517SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Nate Begemanbcc5f362007-01-29 22:58:52 +00005518 // Depths > 0 not supported yet!
5519 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005520 return SDValue();
Nate Begemanbcc5f362007-01-29 22:58:52 +00005521
5522 // Just load the return address
Dan Gohman475871a2008-07-27 21:46:04 +00005523 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005524 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5525}
5526
Dan Gohman475871a2008-07-27 21:46:04 +00005527SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Nate Begemanbcc5f362007-01-29 22:58:52 +00005528 // Depths > 0 not supported yet!
5529 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005530 return SDValue();
Nate Begemanbcc5f362007-01-29 22:58:52 +00005531
Dan Gohman475871a2008-07-27 21:46:04 +00005532 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005533 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling71ca3532008-07-11 07:18:52 +00005534 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Nate Begemanbcc5f362007-01-29 22:58:52 +00005535}
5536
Dan Gohman475871a2008-07-27 21:46:04 +00005537SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005538 SelectionDAG &DAG) {
5539 // Is not yet supported on x86-64
5540 if (Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00005541 return SDValue();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005542
Chris Lattner0bd48932008-01-17 07:00:52 +00005543 return DAG.getIntPtrConstant(8);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005544}
5545
Dan Gohman475871a2008-07-27 21:46:04 +00005546SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005547{
5548 assert(!Subtarget->is64Bit() &&
5549 "Lowering of eh_return builtin is not supported yet on x86-64");
5550
5551 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00005552 SDValue Chain = Op.getOperand(0);
5553 SDValue Offset = Op.getOperand(1);
5554 SDValue Handler = Op.getOperand(2);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005555
Dan Gohman475871a2008-07-27 21:46:04 +00005556 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005557 getPointerTy());
5558
Dan Gohman475871a2008-07-27 21:46:04 +00005559 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner0bd48932008-01-17 07:00:52 +00005560 DAG.getIntPtrConstant(-4UL));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005561 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5562 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5563 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner84bc5422007-12-31 04:13:23 +00005564 MF.getRegInfo().addLiveOut(X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005565
5566 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5567 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5568}
5569
Dan Gohman475871a2008-07-27 21:46:04 +00005570SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00005571 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00005572 SDValue Root = Op.getOperand(0);
5573 SDValue Trmp = Op.getOperand(1); // trampoline
5574 SDValue FPtr = Op.getOperand(2); // nested function
5575 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsb116fac2007-07-27 20:02:49 +00005576
Dan Gohman69de1932008-02-06 22:27:42 +00005577 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005578
Duncan Sands339e14f2008-01-16 22:55:25 +00005579 const X86InstrInfo *TII =
5580 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5581
Duncan Sandsb116fac2007-07-27 20:02:49 +00005582 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005583 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00005584
5585 // Large code-model.
5586
5587 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5588 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5589
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005590 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5591 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00005592
5593 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5594
5595 // Load the pointer to the nested function into R11.
5596 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00005597 SDValue Addr = Trmp;
Duncan Sands339e14f2008-01-16 22:55:25 +00005598 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005599 TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00005600
5601 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005602 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005603
5604 // Load the 'nest' parameter value into R10.
5605 // R10 is specified in X86CallingConv.td
5606 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5607 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5608 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005609 TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005610
5611 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005612 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005613
5614 // Jump to the nested function.
5615 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5616 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5617 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005618 TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00005619
5620 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5621 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5622 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005623 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00005624
Dan Gohman475871a2008-07-27 21:46:04 +00005625 SDValue Ops[] =
Duncan Sands339e14f2008-01-16 22:55:25 +00005626 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005627 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005628 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00005629 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00005630 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5631 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005632 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005633
5634 switch (CC) {
5635 default:
5636 assert(0 && "Unsupported calling convention");
5637 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005638 case CallingConv::X86_StdCall: {
5639 // Pass 'nest' parameter in ECX.
5640 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005641 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005642
5643 // Check that ECX wasn't needed by an 'inreg' parameter.
5644 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner58d74912008-03-12 17:45:29 +00005645 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005646
Chris Lattner58d74912008-03-12 17:45:29 +00005647 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00005648 unsigned InRegCount = 0;
5649 unsigned Idx = 1;
5650
5651 for (FunctionType::param_iterator I = FTy->param_begin(),
5652 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner58d74912008-03-12 17:45:29 +00005653 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00005654 // FIXME: should only count parameters that are lowered to integers.
5655 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5656
5657 if (InRegCount > 2) {
5658 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5659 abort();
5660 }
5661 }
5662 break;
5663 }
5664 case CallingConv::X86_FastCall:
5665 // Pass 'nest' parameter in EAX.
5666 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005667 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005668 break;
5669 }
5670
Dan Gohman475871a2008-07-27 21:46:04 +00005671 SDValue OutChains[4];
5672 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005673
5674 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5675 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5676
Duncan Sands339e14f2008-01-16 22:55:25 +00005677 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005678 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005679 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00005680 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005681
5682 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005683 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005684
Duncan Sands339e14f2008-01-16 22:55:25 +00005685 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005686 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5687 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005688 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005689
5690 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005691 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005692
Dan Gohman475871a2008-07-27 21:46:04 +00005693 SDValue Ops[] =
Duncan Sandsf7331b32007-09-11 14:10:23 +00005694 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005695 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005696 }
5697}
5698
Dan Gohman475871a2008-07-27 21:46:04 +00005699SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005700 /*
5701 The rounding mode is in bits 11:10 of FPSR, and has the following
5702 settings:
5703 00 Round to nearest
5704 01 Round to -inf
5705 10 Round to +inf
5706 11 Round to 0
5707
5708 FLT_ROUNDS, on the other hand, expects the following:
5709 -1 Undefined
5710 0 Round to 0
5711 1 Round to nearest
5712 2 Round to +inf
5713 3 Round to -inf
5714
5715 To perform the conversion, we do:
5716 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5717 */
5718
5719 MachineFunction &MF = DAG.getMachineFunction();
5720 const TargetMachine &TM = MF.getTarget();
5721 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5722 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005723 MVT VT = Op.getValueType();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005724
5725 // Save FP Control Word to stack slot
5726 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00005727 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005728
Dan Gohman475871a2008-07-27 21:46:04 +00005729 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005730 DAG.getEntryNode(), StackSlot);
5731
5732 // Load FP Control Word from stack slot
Dan Gohman475871a2008-07-27 21:46:04 +00005733 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005734
5735 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00005736 SDValue CWD1 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005737 DAG.getNode(ISD::SRL, MVT::i16,
5738 DAG.getNode(ISD::AND, MVT::i16,
5739 CWD, DAG.getConstant(0x800, MVT::i16)),
5740 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00005741 SDValue CWD2 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005742 DAG.getNode(ISD::SRL, MVT::i16,
5743 DAG.getNode(ISD::AND, MVT::i16,
5744 CWD, DAG.getConstant(0x400, MVT::i16)),
5745 DAG.getConstant(9, MVT::i8));
5746
Dan Gohman475871a2008-07-27 21:46:04 +00005747 SDValue RetVal =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005748 DAG.getNode(ISD::AND, MVT::i16,
5749 DAG.getNode(ISD::ADD, MVT::i16,
5750 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5751 DAG.getConstant(1, MVT::i16)),
5752 DAG.getConstant(3, MVT::i16));
5753
5754
Duncan Sands83ec4b62008-06-06 12:08:01 +00005755 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005756 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5757}
5758
Dan Gohman475871a2008-07-27 21:46:04 +00005759SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005760 MVT VT = Op.getValueType();
5761 MVT OpVT = VT;
5762 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005763
5764 Op = Op.getOperand(0);
5765 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005766 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005767 OpVT = MVT::i32;
5768 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5769 }
Evan Cheng18efe262007-12-14 02:13:44 +00005770
Evan Cheng152804e2007-12-14 08:30:15 +00005771 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5772 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5773 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5774
5775 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00005776 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00005777 Ops.push_back(Op);
5778 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5779 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5780 Ops.push_back(Op.getValue(1));
5781 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5782
5783 // Finally xor with NumBits-1.
5784 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5785
Evan Cheng18efe262007-12-14 02:13:44 +00005786 if (VT == MVT::i8)
5787 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5788 return Op;
5789}
5790
Dan Gohman475871a2008-07-27 21:46:04 +00005791SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005792 MVT VT = Op.getValueType();
5793 MVT OpVT = VT;
5794 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005795
5796 Op = Op.getOperand(0);
5797 if (VT == MVT::i8) {
5798 OpVT = MVT::i32;
5799 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5800 }
Evan Cheng152804e2007-12-14 08:30:15 +00005801
5802 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5803 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5804 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5805
5806 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00005807 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00005808 Ops.push_back(Op);
5809 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5810 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5811 Ops.push_back(Op.getValue(1));
5812 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5813
Evan Cheng18efe262007-12-14 02:13:44 +00005814 if (VT == MVT::i8)
5815 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5816 return Op;
5817}
5818
Dan Gohman475871a2008-07-27 21:46:04 +00005819SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005820 MVT T = Op.getValueType();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00005821 unsigned Reg = 0;
5822 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005823 switch(T.getSimpleVT()) {
5824 default:
5825 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005826 case MVT::i8: Reg = X86::AL; size = 1; break;
5827 case MVT::i16: Reg = X86::AX; size = 2; break;
5828 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005829 case MVT::i64:
5830 if (Subtarget->is64Bit()) {
5831 Reg = X86::RAX; size = 8;
5832 } else //Should go away when LowerType stuff lands
Dan Gohman475871a2008-07-27 21:46:04 +00005833 return SDValue(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005834 break;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005835 };
Dan Gohman475871a2008-07-27 21:46:04 +00005836 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5837 Op.getOperand(3), SDValue());
5838 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005839 Op.getOperand(1),
5840 Op.getOperand(2),
5841 DAG.getTargetConstant(size, MVT::i8),
5842 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005843 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005844 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5845 SDValue cpOut =
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005846 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5847 return cpOut;
5848}
5849
Mon P Wang28873102008-06-25 08:15:39 +00005850SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005851 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00005852 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman475871a2008-07-27 21:46:04 +00005853 SDValue cpInL, cpInH;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005854 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5855 DAG.getConstant(0, MVT::i32));
5856 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5857 DAG.getConstant(1, MVT::i32));
5858 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman475871a2008-07-27 21:46:04 +00005859 cpInL, SDValue());
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005860 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5861 cpInH, cpInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005862 SDValue swapInL, swapInH;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005863 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5864 DAG.getConstant(0, MVT::i32));
5865 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5866 DAG.getConstant(1, MVT::i32));
5867 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5868 swapInL, cpInH.getValue(1));
5869 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5870 swapInH, swapInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005871 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005872 Op->getOperand(1),
5873 swapInH.getValue(1)};
5874 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005875 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5876 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005877 Result.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005878 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005879 cpOutL.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00005880 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5881 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5882 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005883 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005884}
5885
Mon P Wang28873102008-06-25 08:15:39 +00005886SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005887 MVT T = Op->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00005888 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang63307c32008-05-05 19:05:59 +00005889 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00005890 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005891 Op->getOperand(1), negOp,
Mon P Wang28873102008-06-25 08:15:39 +00005892 cast<AtomicSDNode>(Op)->getSrcValue(),
5893 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang63307c32008-05-05 19:05:59 +00005894}
5895
Evan Cheng0db9fe62006-04-25 20:13:52 +00005896/// LowerOperation - Provide custom lowering hooks for some operations.
5897///
Dan Gohman475871a2008-07-27 21:46:04 +00005898SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005899 switch (Op.getOpcode()) {
5900 default: assert(0 && "Should not custom lower this!");
Mon P Wang28873102008-06-25 08:15:39 +00005901 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005902 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5903 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5904 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5905 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5906 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5907 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5908 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005909 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005910 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5911 case ISD::SHL_PARTS:
5912 case ISD::SRA_PARTS:
5913 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5914 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5915 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5916 case ISD::FABS: return LowerFABS(Op, DAG);
5917 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005918 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005919 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00005920 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005921 case ISD::SELECT: return LowerSELECT(Op, DAG);
5922 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005923 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005924 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005925 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005926 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005927 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00005928 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005929 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005930 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005931 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5932 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005933 case ISD::FRAME_TO_ARGS_OFFSET:
5934 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005935 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005936 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005937 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005938 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005939 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5940 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005941
5942 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5943 case ISD::READCYCLECOUNTER:
Dan Gohman475871a2008-07-27 21:46:04 +00005944 return SDValue(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005945 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005946}
5947
Duncan Sands126d9072008-07-04 11:47:58 +00005948/// ReplaceNodeResults - Replace a node with an illegal result type
5949/// with a new node built out of custom code.
5950SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005951 switch (N->getOpcode()) {
5952 default: assert(0 && "Should not custom lower this!");
5953 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5954 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang28873102008-06-25 08:15:39 +00005955 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5956 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005957 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005958}
5959
Evan Cheng72261582005-12-20 06:22:03 +00005960const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5961 switch (Opcode) {
5962 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005963 case X86ISD::BSF: return "X86ISD::BSF";
5964 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005965 case X86ISD::SHLD: return "X86ISD::SHLD";
5966 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005967 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005968 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005969 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005970 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005971 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005972 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005973 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5974 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5975 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005976 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005977 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00005978 case X86ISD::CALL: return "X86ISD::CALL";
5979 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5980 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5981 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005982 case X86ISD::COMI: return "X86ISD::COMI";
5983 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005984 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005985 case X86ISD::CMOV: return "X86ISD::CMOV";
5986 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005987 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005988 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5989 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005990 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005991 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005992 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00005993 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005994 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5995 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00005996 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005997 case X86ISD::FMAX: return "X86ISD::FMAX";
5998 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005999 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6000 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006001 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6002 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006003 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006004 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006005 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006006 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6007 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006008 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6009 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006010 case X86ISD::VSHL: return "X86ISD::VSHL";
6011 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006012 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6013 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6014 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6015 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6016 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6017 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6018 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6019 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6020 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6021 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Evan Cheng72261582005-12-20 06:22:03 +00006022 }
6023}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006024
Chris Lattnerc9addb72007-03-30 23:15:24 +00006025// isLegalAddressingMode - Return true if the addressing mode represented
6026// by AM is legal for this target, for a load/store of the specified type.
6027bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6028 const Type *Ty) const {
6029 // X86 supports extremely general addressing modes.
6030
6031 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6032 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6033 return false;
6034
6035 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006036 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006037 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6038 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006039
6040 // X86-64 only supports addr of globals in small code model.
6041 if (Subtarget->is64Bit()) {
6042 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6043 return false;
6044 // If lower 4G is not available, then we must use rip-relative addressing.
6045 if (AM.BaseOffs || AM.Scale > 1)
6046 return false;
6047 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006048 }
6049
6050 switch (AM.Scale) {
6051 case 0:
6052 case 1:
6053 case 2:
6054 case 4:
6055 case 8:
6056 // These scales always work.
6057 break;
6058 case 3:
6059 case 5:
6060 case 9:
6061 // These scales are formed with basereg+scalereg. Only accept if there is
6062 // no basereg yet.
6063 if (AM.HasBaseReg)
6064 return false;
6065 break;
6066 default: // Other stuff never works.
6067 return false;
6068 }
6069
6070 return true;
6071}
6072
6073
Evan Cheng2bd122c2007-10-26 01:56:11 +00006074bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6075 if (!Ty1->isInteger() || !Ty2->isInteger())
6076 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006077 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6078 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006079 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006080 return false;
6081 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006082}
6083
Duncan Sands83ec4b62008-06-06 12:08:01 +00006084bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6085 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006086 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006087 unsigned NumBits1 = VT1.getSizeInBits();
6088 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006089 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006090 return false;
6091 return Subtarget->is64Bit() || NumBits1 < 64;
6092}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006093
Evan Cheng60c07e12006-07-05 22:17:51 +00006094/// isShuffleMaskLegal - Targets can use this to indicate that they only
6095/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6096/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6097/// are assumed to be legal.
6098bool
Dan Gohman475871a2008-07-27 21:46:04 +00006099X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006100 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006101 if (VT.getSizeInBits() == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006102 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00006103 isIdentityMask(Mask.Val) ||
6104 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00006105 isSplatMask(Mask.Val) ||
6106 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6107 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00006108 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00006109 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00006110 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00006111}
6112
Dan Gohman7d8143f2008-04-09 20:09:42 +00006113bool
Dan Gohman475871a2008-07-27 21:46:04 +00006114X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006115 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006116 unsigned NumElts = BVOps.size();
6117 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006118 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006119 if (NumElts == 2) return true;
6120 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00006121 return (isMOVLMask(&BVOps[0], 4) ||
6122 isCommutedMOVL(&BVOps[0], 4, true) ||
6123 isSHUFPMask(&BVOps[0], 4) ||
6124 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00006125 }
6126 return false;
6127}
6128
6129//===----------------------------------------------------------------------===//
6130// X86 Scheduler Hooks
6131//===----------------------------------------------------------------------===//
6132
Mon P Wang63307c32008-05-05 19:05:59 +00006133// private utility function
6134MachineBasicBlock *
6135X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6136 MachineBasicBlock *MBB,
6137 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006138 unsigned immOpc,
6139 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00006140 // For the atomic bitwise operator, we generate
6141 // thisMBB:
6142 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006143 // ld t1 = [bitinstr.addr]
6144 // op t2 = t1, [bitinstr.val]
6145 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006146 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6147 // bz newMBB
6148 // fallthrough -->nextMBB
6149 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6150 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006151 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006152 ++MBBIter;
6153
6154 /// First build the CFG
6155 MachineFunction *F = MBB->getParent();
6156 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006157 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6158 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6159 F->insert(MBBIter, newMBB);
6160 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006161
6162 // Move all successors to thisMBB to nextMBB
6163 nextMBB->transferSuccessors(thisMBB);
6164
6165 // Update thisMBB to fall through to newMBB
6166 thisMBB->addSuccessor(newMBB);
6167
6168 // newMBB jumps to itself and fall through to nextMBB
6169 newMBB->addSuccessor(nextMBB);
6170 newMBB->addSuccessor(newMBB);
6171
6172 // Insert instructions into newMBB based on incoming instruction
6173 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6174 MachineOperand& destOper = bInstr->getOperand(0);
6175 MachineOperand* argOpers[6];
6176 int numArgs = bInstr->getNumOperands() - 1;
6177 for (int i=0; i < numArgs; ++i)
6178 argOpers[i] = &bInstr->getOperand(i+1);
6179
6180 // x86 address has 4 operands: base, index, scale, and displacement
6181 int lastAddrIndx = 3; // [0,3]
6182 int valArgIndx = 4;
6183
Mon P Wangab3e7472008-05-05 22:56:23 +00006184 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6185 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006186 for (int i=0; i <= lastAddrIndx; ++i)
6187 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006188
6189 unsigned tt = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6190 if (invSrc) {
6191 MIB = BuildMI(newMBB, TII->get(X86::NOT32r), tt).addReg(t1);
6192 }
6193 else
6194 tt = t1;
6195
Mon P Wang63307c32008-05-05 19:05:59 +00006196 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6197 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6198 && "invalid operand");
6199 if (argOpers[valArgIndx]->isReg())
6200 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6201 else
6202 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006203 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00006204 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006205
Mon P Wangab3e7472008-05-05 22:56:23 +00006206 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6207 MIB.addReg(t1);
6208
Mon P Wang63307c32008-05-05 19:05:59 +00006209 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6210 for (int i=0; i <= lastAddrIndx; ++i)
6211 (*MIB).addOperand(*argOpers[i]);
6212 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00006213 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6214 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6215
Mon P Wang63307c32008-05-05 19:05:59 +00006216 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6217 MIB.addReg(X86::EAX);
6218
6219 // insert branch
6220 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6221
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006222 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006223 return nextMBB;
6224}
6225
6226// private utility function
6227MachineBasicBlock *
6228X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6229 MachineBasicBlock *MBB,
6230 unsigned cmovOpc) {
6231 // For the atomic min/max operator, we generate
6232 // thisMBB:
6233 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006234 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00006235 // mov t2 = [min/max.val]
6236 // cmp t1, t2
6237 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00006238 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006239 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6240 // bz newMBB
6241 // fallthrough -->nextMBB
6242 //
6243 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6244 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006245 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006246 ++MBBIter;
6247
6248 /// First build the CFG
6249 MachineFunction *F = MBB->getParent();
6250 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006251 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6252 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6253 F->insert(MBBIter, newMBB);
6254 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006255
6256 // Move all successors to thisMBB to nextMBB
6257 nextMBB->transferSuccessors(thisMBB);
6258
6259 // Update thisMBB to fall through to newMBB
6260 thisMBB->addSuccessor(newMBB);
6261
6262 // newMBB jumps to newMBB and fall through to nextMBB
6263 newMBB->addSuccessor(nextMBB);
6264 newMBB->addSuccessor(newMBB);
6265
6266 // Insert instructions into newMBB based on incoming instruction
6267 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6268 MachineOperand& destOper = mInstr->getOperand(0);
6269 MachineOperand* argOpers[6];
6270 int numArgs = mInstr->getNumOperands() - 1;
6271 for (int i=0; i < numArgs; ++i)
6272 argOpers[i] = &mInstr->getOperand(i+1);
6273
6274 // x86 address has 4 operands: base, index, scale, and displacement
6275 int lastAddrIndx = 3; // [0,3]
6276 int valArgIndx = 4;
6277
Mon P Wangab3e7472008-05-05 22:56:23 +00006278 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6279 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006280 for (int i=0; i <= lastAddrIndx; ++i)
6281 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00006282
Mon P Wang63307c32008-05-05 19:05:59 +00006283 // We only support register and immediate values
6284 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6285 && "invalid operand");
6286
6287 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6288 if (argOpers[valArgIndx]->isReg())
6289 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6290 else
6291 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6292 (*MIB).addOperand(*argOpers[valArgIndx]);
6293
Mon P Wangab3e7472008-05-05 22:56:23 +00006294 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6295 MIB.addReg(t1);
6296
Mon P Wang63307c32008-05-05 19:05:59 +00006297 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6298 MIB.addReg(t1);
6299 MIB.addReg(t2);
6300
6301 // Generate movc
6302 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6303 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6304 MIB.addReg(t2);
6305 MIB.addReg(t1);
6306
6307 // Cmp and exchange if none has modified the memory location
6308 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6309 for (int i=0; i <= lastAddrIndx; ++i)
6310 (*MIB).addOperand(*argOpers[i]);
6311 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00006312 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6313 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang63307c32008-05-05 19:05:59 +00006314
6315 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6316 MIB.addReg(X86::EAX);
6317
6318 // insert branch
6319 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6320
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006321 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006322 return nextMBB;
6323}
6324
6325
Evan Cheng60c07e12006-07-05 22:17:51 +00006326MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006327X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6328 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00006329 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00006330 switch (MI->getOpcode()) {
6331 default: assert(false && "Unexpected instr type to insert");
6332 case X86::CMOV_FR32:
6333 case X86::CMOV_FR64:
6334 case X86::CMOV_V4F32:
6335 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00006336 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006337 // To "insert" a SELECT_CC instruction, we actually have to insert the
6338 // diamond control-flow pattern. The incoming instruction knows the
6339 // destination vreg to set, the condition code register to branch on, the
6340 // true/false values to select between, and a branch opcode to use.
6341 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006342 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00006343 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006344
Evan Cheng60c07e12006-07-05 22:17:51 +00006345 // thisMBB:
6346 // ...
6347 // TrueVal = ...
6348 // cmpTY ccX, r1, r2
6349 // bCC copy1MBB
6350 // fallthrough --> copy0MBB
6351 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006352 MachineFunction *F = BB->getParent();
6353 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6354 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006355 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00006356 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00006357 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006358 F->insert(It, copy0MBB);
6359 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006360 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00006361 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00006362 sinkMBB->transferSuccessors(BB);
6363
6364 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00006365 BB->addSuccessor(copy0MBB);
6366 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006367
Evan Cheng60c07e12006-07-05 22:17:51 +00006368 // copy0MBB:
6369 // %FalseValue = ...
6370 // # fallthrough to sinkMBB
6371 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006372
Evan Cheng60c07e12006-07-05 22:17:51 +00006373 // Update machine-CFG edges
6374 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006375
Evan Cheng60c07e12006-07-05 22:17:51 +00006376 // sinkMBB:
6377 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6378 // ...
6379 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00006380 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00006381 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6382 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6383
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006384 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006385 return BB;
6386 }
6387
Dale Johannesen849f2142007-07-03 00:53:03 +00006388 case X86::FP32_TO_INT16_IN_MEM:
6389 case X86::FP32_TO_INT32_IN_MEM:
6390 case X86::FP32_TO_INT64_IN_MEM:
6391 case X86::FP64_TO_INT16_IN_MEM:
6392 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00006393 case X86::FP64_TO_INT64_IN_MEM:
6394 case X86::FP80_TO_INT16_IN_MEM:
6395 case X86::FP80_TO_INT32_IN_MEM:
6396 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006397 // Change the floating point control register to use "round towards zero"
6398 // mode when truncating to an integer value.
6399 MachineFunction *F = BB->getParent();
6400 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006401 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006402
6403 // Load the old value of the high byte of the control word...
6404 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00006405 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006406 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006407
6408 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006409 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6410 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00006411
6412 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006413 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006414
6415 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00006416 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6417 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00006418
6419 // Get the X86 opcode to use.
6420 unsigned Opc;
6421 switch (MI->getOpcode()) {
6422 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00006423 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6424 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6425 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6426 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6427 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6428 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00006429 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6430 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6431 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00006432 }
6433
6434 X86AddressMode AM;
6435 MachineOperand &Op = MI->getOperand(0);
6436 if (Op.isRegister()) {
6437 AM.BaseType = X86AddressMode::RegBase;
6438 AM.Base.Reg = Op.getReg();
6439 } else {
6440 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00006441 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00006442 }
6443 Op = MI->getOperand(1);
6444 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006445 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006446 Op = MI->getOperand(2);
6447 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006448 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006449 Op = MI->getOperand(3);
6450 if (Op.isGlobalAddress()) {
6451 AM.GV = Op.getGlobal();
6452 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00006453 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006454 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00006455 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6456 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00006457
6458 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00006459 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006460
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006461 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006462 return BB;
6463 }
Mon P Wang63307c32008-05-05 19:05:59 +00006464 case X86::ATOMAND32:
6465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6466 X86::AND32ri);
6467 case X86::ATOMOR32:
6468 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6469 X86::OR32ri);
6470 case X86::ATOMXOR32:
6471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6472 X86::XOR32ri);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006473 case X86::ATOMNAND32:
6474 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6475 X86::AND32ri, true);
Mon P Wang63307c32008-05-05 19:05:59 +00006476 case X86::ATOMMIN32:
6477 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6478 case X86::ATOMMAX32:
6479 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6480 case X86::ATOMUMIN32:
6481 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6482 case X86::ATOMUMAX32:
6483 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Evan Cheng60c07e12006-07-05 22:17:51 +00006484 }
6485}
6486
6487//===----------------------------------------------------------------------===//
6488// X86 Optimization Hooks
6489//===----------------------------------------------------------------------===//
6490
Dan Gohman475871a2008-07-27 21:46:04 +00006491void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006492 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006493 APInt &KnownZero,
6494 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006495 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00006496 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006497 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00006498 assert((Opc >= ISD::BUILTIN_OP_END ||
6499 Opc == ISD::INTRINSIC_WO_CHAIN ||
6500 Opc == ISD::INTRINSIC_W_CHAIN ||
6501 Opc == ISD::INTRINSIC_VOID) &&
6502 "Should use MaskedValueIsZero if you don't know whether Op"
6503 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006504
Dan Gohmanf4f92f52008-02-13 23:07:24 +00006505 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006506 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00006507 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006508 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006509 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6510 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00006511 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006512 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006513}
Chris Lattner259e97c2006-01-31 19:43:35 +00006514
Evan Cheng206ee9d2006-07-07 08:33:52 +00006515/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00006516/// node is a GlobalAddress + offset.
6517bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6518 GlobalValue* &GA, int64_t &Offset) const{
6519 if (N->getOpcode() == X86ISD::Wrapper) {
6520 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006521 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6522 return true;
6523 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00006524 }
Evan Chengad4196b2008-05-12 19:56:52 +00006525 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006526}
6527
Evan Chengad4196b2008-05-12 19:56:52 +00006528static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6529 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006530 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00006531 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00006532 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006533 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00006534 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00006535 return false;
6536}
6537
Dan Gohman475871a2008-07-27 21:46:04 +00006538static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006539 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00006540 SDNode *&Base,
6541 SelectionDAG &DAG, MachineFrameInfo *MFI,
6542 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006543 Base = NULL;
6544 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006545 SDValue Idx = PermMask.getOperand(i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006546 if (Idx.getOpcode() == ISD::UNDEF) {
6547 if (!Base)
6548 return false;
6549 continue;
6550 }
6551
Dan Gohman475871a2008-07-27 21:46:04 +00006552 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006553 if (!Elt.Val ||
6554 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6555 return false;
6556 if (!Base) {
6557 Base = Elt.Val;
Evan Cheng50d9e722008-05-10 06:46:49 +00006558 if (Base->getOpcode() == ISD::UNDEF)
6559 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00006560 continue;
6561 }
6562 if (Elt.getOpcode() == ISD::UNDEF)
6563 continue;
6564
Evan Chengad4196b2008-05-12 19:56:52 +00006565 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006566 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006567 return false;
6568 }
6569 return true;
6570}
Evan Cheng206ee9d2006-07-07 08:33:52 +00006571
6572/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6573/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6574/// if the load addresses are consecutive, non-overlapping, and in the right
6575/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00006576static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006577 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006578 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006579 MVT VT = N->getValueType(0);
6580 MVT EVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00006581 SDValue PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00006582 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006583 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00006584 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6585 DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00006586 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006587
Dan Gohmand3006222007-07-27 17:16:43 +00006588 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengad4196b2008-05-12 19:56:52 +00006589 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Evan Cheng466685d2006-10-09 20:57:25 +00006590 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00006591 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng71f489d2008-05-05 22:12:23 +00006592 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6593 LD->getSrcValueOffset(), LD->isVolatile(),
6594 LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00006595}
6596
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006597/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00006598static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006599 const X86Subtarget *Subtarget,
6600 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00006601 unsigned NumOps = N->getNumOperands();
6602
Evan Chengd880b972008-05-09 21:53:03 +00006603 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00006604 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00006605 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006606
Duncan Sands83ec4b62008-06-06 12:08:01 +00006607 MVT VT = N->getValueType(0);
6608 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00006609 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6610 // We are looking for load i64 and zero extend. We want to transform
6611 // it before legalizer has a chance to expand it. Also look for i64
6612 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00006613 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006614 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006615 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00006616 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00006617 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006618
6619 // Value must be a load.
Evan Chengd880b972008-05-09 21:53:03 +00006620 SDNode *Base = N->getOperand(0).Val;
6621 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006622 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00006623 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006624 Base = Base->getOperand(0).Val;
6625 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00006626 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006627 }
Evan Chengd880b972008-05-09 21:53:03 +00006628
6629 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006630 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00006631
6632 // Load must not be an extload.
6633 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00006634 return SDValue();
Nate Begemanf7333bf2008-05-28 00:24:25 +00006635
Evan Chengd880b972008-05-09 21:53:03 +00006636 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6637}
6638
Chris Lattner83e6c992006-10-04 06:57:07 +00006639/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006640static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner83e6c992006-10-04 06:57:07 +00006641 const X86Subtarget *Subtarget) {
Dan Gohman475871a2008-07-27 21:46:04 +00006642 SDValue Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006643
Chris Lattner83e6c992006-10-04 06:57:07 +00006644 // If we have SSE[12] support, try to form min/max nodes.
6645 if (Subtarget->hasSSE2() &&
6646 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6647 if (Cond.getOpcode() == ISD::SETCC) {
6648 // Get the LHS/RHS of the select.
Dan Gohman475871a2008-07-27 21:46:04 +00006649 SDValue LHS = N->getOperand(1);
6650 SDValue RHS = N->getOperand(2);
Chris Lattner83e6c992006-10-04 06:57:07 +00006651 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006652
Evan Cheng8ca29322006-11-10 21:43:37 +00006653 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00006654 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006655 switch (CC) {
6656 default: break;
6657 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6658 case ISD::SETULE:
6659 case ISD::SETLE:
6660 if (!UnsafeFPMath) break;
6661 // FALL THROUGH.
6662 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6663 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006664 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006665 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006666
Chris Lattner1907a7b2006-10-05 04:11:26 +00006667 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6668 case ISD::SETUGT:
6669 case ISD::SETGT:
6670 if (!UnsafeFPMath) break;
6671 // FALL THROUGH.
6672 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6673 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006674 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006675 break;
6676 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006677 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006678 switch (CC) {
6679 default: break;
6680 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6681 case ISD::SETUGT:
6682 case ISD::SETGT:
6683 if (!UnsafeFPMath) break;
6684 // FALL THROUGH.
6685 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6686 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006687 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006688 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006689
Chris Lattner1907a7b2006-10-05 04:11:26 +00006690 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6691 case ISD::SETULE:
6692 case ISD::SETLE:
6693 if (!UnsafeFPMath) break;
6694 // FALL THROUGH.
6695 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6696 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006697 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006698 break;
6699 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006700 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006701
Evan Cheng8ca29322006-11-10 21:43:37 +00006702 if (Opcode)
6703 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00006704 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006705
Chris Lattner83e6c992006-10-04 06:57:07 +00006706 }
6707
Dan Gohman475871a2008-07-27 21:46:04 +00006708 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00006709}
6710
Chris Lattner149a4e52008-02-22 02:09:43 +00006711/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006712static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00006713 const X86Subtarget *Subtarget) {
6714 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6715 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00006716 // A preferable solution to the general problem is to figure out the right
6717 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006718 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006719 if (St->getValue().getValueType().isVector() &&
6720 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00006721 isa<LoadSDNode>(St->getValue()) &&
6722 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6723 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006724 SDNode* LdVal = St->getValue().Val;
Dale Johannesen079f2a62008-02-25 19:20:14 +00006725 LoadSDNode *Ld = 0;
6726 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00006727 SmallVector<SDValue, 8> Ops;
Dale Johannesen079f2a62008-02-25 19:20:14 +00006728 SDNode* ChainVal = St->getChain().Val;
6729 // Must be a store of a load. We currently handle two cases: the load
6730 // is a direct child, and it's under an intervening TokenFactor. It is
6731 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006732 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00006733 Ld = cast<LoadSDNode>(St->getChain());
6734 else if (St->getValue().hasOneUse() &&
6735 ChainVal->getOpcode() == ISD::TokenFactor) {
6736 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006737 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00006738 TokenFactorIndex = i;
6739 Ld = cast<LoadSDNode>(St->getValue());
6740 } else
6741 Ops.push_back(ChainVal->getOperand(i));
6742 }
6743 }
6744 if (Ld) {
6745 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6746 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006747 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00006748 Ld->getBasePtr(), Ld->getSrcValue(),
6749 Ld->getSrcValueOffset(), Ld->isVolatile(),
6750 Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006751 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006752 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00006753 Ops.push_back(NewChain);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006754 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6755 Ops.size());
6756 }
6757 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6758 St->getSrcValue(), St->getSrcValueOffset(),
6759 St->isVolatile(), St->getAlignment());
6760 }
6761
6762 // Otherwise, lower to two 32-bit copies.
Dan Gohman475871a2008-07-27 21:46:04 +00006763 SDValue LoAddr = Ld->getBasePtr();
6764 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006765 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006766
Dan Gohman475871a2008-07-27 21:46:04 +00006767 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006768 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6769 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006770 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006771 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6772 Ld->isVolatile(),
6773 MinAlign(Ld->getAlignment(), 4));
6774
Dan Gohman475871a2008-07-27 21:46:04 +00006775 SDValue NewChain = LoLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006776 if (TokenFactorIndex != -1) {
6777 Ops.push_back(LoLd);
6778 Ops.push_back(HiLd);
6779 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6780 Ops.size());
6781 }
6782
6783 LoAddr = St->getBasePtr();
6784 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006785 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006786
Dan Gohman475871a2008-07-27 21:46:04 +00006787 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00006788 St->getSrcValue(), St->getSrcValueOffset(),
6789 St->isVolatile(), St->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006790 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006791 St->getSrcValue(), St->getSrcValueOffset()+4,
6792 St->isVolatile(),
6793 MinAlign(St->getAlignment(), 4));
6794 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00006795 }
Chris Lattner149a4e52008-02-22 02:09:43 +00006796 }
Dan Gohman475871a2008-07-27 21:46:04 +00006797 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00006798}
6799
Chris Lattner6cf73262008-01-25 06:14:17 +00006800/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6801/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006802static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00006803 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6804 // F[X]OR(0.0, x) -> x
6805 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00006806 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6807 if (C->getValueAPF().isPosZero())
6808 return N->getOperand(1);
6809 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6810 if (C->getValueAPF().isPosZero())
6811 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00006812 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00006813}
6814
6815/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006816static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00006817 // FAND(0.0, x) -> 0.0
6818 // FAND(x, 0.0) -> 0.0
6819 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6820 if (C->getValueAPF().isPosZero())
6821 return N->getOperand(0);
6822 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6823 if (C->getValueAPF().isPosZero())
6824 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006825 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00006826}
6827
Chris Lattner83e6c992006-10-04 06:57:07 +00006828
Dan Gohman475871a2008-07-27 21:46:04 +00006829SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00006830 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006831 SelectionDAG &DAG = DCI.DAG;
6832 switch (N->getOpcode()) {
6833 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00006834 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6835 case ISD::BUILD_VECTOR:
6836 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00006837 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006838 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00006839 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00006840 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6841 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006842 }
6843
Dan Gohman475871a2008-07-27 21:46:04 +00006844 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006845}
6846
Evan Cheng60c07e12006-07-05 22:17:51 +00006847//===----------------------------------------------------------------------===//
6848// X86 Inline Assembly Support
6849//===----------------------------------------------------------------------===//
6850
Chris Lattnerf4dff842006-07-11 02:54:03 +00006851/// getConstraintType - Given a constraint letter, return the type of
6852/// constraint it is for this target.
6853X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006854X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6855 if (Constraint.size() == 1) {
6856 switch (Constraint[0]) {
6857 case 'A':
Chris Lattnerfce84ac2008-03-11 19:06:29 +00006858 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00006859 case 'r':
6860 case 'R':
6861 case 'l':
6862 case 'q':
6863 case 'Q':
6864 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00006865 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00006866 case 'Y':
6867 return C_RegisterClass;
6868 default:
6869 break;
6870 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00006871 }
Chris Lattner4234f572007-03-25 02:14:49 +00006872 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00006873}
6874
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006875/// LowerXConstraint - try to replace an X constraint, which matches anything,
6876/// with another that has more specific requirements based on the type of the
6877/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00006878const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00006879LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00006880 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6881 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006882 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006883 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00006884 return "Y";
6885 if (Subtarget->hasSSE1())
6886 return "x";
6887 }
6888
6889 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006890}
6891
Chris Lattner48884cd2007-08-25 00:47:38 +00006892/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6893/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00006894void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00006895 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00006896 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00006897 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00006898 SDValue Result(0, 0);
Chris Lattner48884cd2007-08-25 00:47:38 +00006899
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006900 switch (Constraint) {
6901 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00006902 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00006903 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006904 if (C->getValue() <= 31) {
6905 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6906 break;
6907 }
Devang Patel84f7fd22007-03-17 00:13:28 +00006908 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006909 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00006910 case 'N':
6911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006912 if (C->getValue() <= 255) {
6913 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6914 break;
6915 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00006916 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006917 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00006918 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006919 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00006920 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6921 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6922 break;
6923 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006924
Chris Lattnerdc43a882007-05-03 16:52:29 +00006925 // If we are in non-pic codegen mode, we allow the address of a global (with
6926 // an optional displacement) to be used with 'i'.
6927 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6928 int64_t Offset = 0;
6929
6930 // Match either (GA) or (GA+C)
6931 if (GA) {
6932 Offset = GA->getOffset();
6933 } else if (Op.getOpcode() == ISD::ADD) {
6934 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6935 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6936 if (C && GA) {
6937 Offset = GA->getOffset()+C->getValue();
6938 } else {
6939 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6940 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6941 if (C && GA)
6942 Offset = GA->getOffset()+C->getValue();
6943 else
6944 C = 0, GA = 0;
6945 }
6946 }
6947
6948 if (GA) {
6949 // If addressing this global requires a load (e.g. in PIC mode), we can't
6950 // match.
6951 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6952 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00006953 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006954
Chris Lattnerdc43a882007-05-03 16:52:29 +00006955 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6956 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00006957 Result = Op;
6958 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006959 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006960
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006961 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00006962 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006963 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00006964 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006965
6966 if (Result.Val) {
6967 Ops.push_back(Result);
6968 return;
6969 }
6970 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006971}
6972
Chris Lattner259e97c2006-01-31 19:43:35 +00006973std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00006974getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006975 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00006976 if (Constraint.size() == 1) {
6977 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00006978 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00006979 default: break; // Unknown constraint letter
6980 case 'A': // EAX/EDX
6981 if (VT == MVT::i32 || VT == MVT::i64)
6982 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6983 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006984 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6985 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00006986 if (VT == MVT::i32)
6987 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6988 else if (VT == MVT::i16)
6989 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6990 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00006991 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00006992 else if (VT == MVT::i64)
6993 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6994 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006995 }
6996 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006997
Chris Lattner1efa40f2006-02-22 00:56:39 +00006998 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00006999}
Chris Lattnerf76d1802006-07-31 23:26:50 +00007000
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007001std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00007002X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007003 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00007004 // First, see if this is a constraint that directly corresponds to an LLVM
7005 // register class.
7006 if (Constraint.size() == 1) {
7007 // GCC Constraint Letters
7008 switch (Constraint[0]) {
7009 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00007010 case 'r': // GENERAL_REGS
7011 case 'R': // LEGACY_REGS
7012 case 'l': // INDEX_REGS
7013 if (VT == MVT::i64 && Subtarget->is64Bit())
7014 return std::make_pair(0U, X86::GR64RegisterClass);
7015 if (VT == MVT::i32)
7016 return std::make_pair(0U, X86::GR32RegisterClass);
7017 else if (VT == MVT::i16)
7018 return std::make_pair(0U, X86::GR16RegisterClass);
7019 else if (VT == MVT::i8)
7020 return std::make_pair(0U, X86::GR8RegisterClass);
7021 break;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00007022 case 'f': // FP Stack registers.
7023 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7024 // value to the correct fpstack register class.
7025 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7026 return std::make_pair(0U, X86::RFP32RegisterClass);
7027 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7028 return std::make_pair(0U, X86::RFP64RegisterClass);
7029 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00007030 case 'y': // MMX_REGS if MMX allowed.
7031 if (!Subtarget->hasMMX()) break;
7032 return std::make_pair(0U, X86::VR64RegisterClass);
7033 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00007034 case 'Y': // SSE_REGS if SSE2 allowed
7035 if (!Subtarget->hasSSE2()) break;
7036 // FALL THROUGH.
7037 case 'x': // SSE_REGS if SSE1 allowed
7038 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007039
7040 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00007041 default: break;
7042 // Scalar SSE types.
7043 case MVT::f32:
7044 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00007045 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007046 case MVT::f64:
7047 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00007048 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007049 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00007050 case MVT::v16i8:
7051 case MVT::v8i16:
7052 case MVT::v4i32:
7053 case MVT::v2i64:
7054 case MVT::v4f32:
7055 case MVT::v2f64:
7056 return std::make_pair(0U, X86::VR128RegisterClass);
7057 }
Chris Lattnerad043e82007-04-09 05:11:28 +00007058 break;
7059 }
7060 }
7061
Chris Lattnerf76d1802006-07-31 23:26:50 +00007062 // Use the default implementation in TargetLowering to convert the register
7063 // constraint into a member of a register class.
7064 std::pair<unsigned, const TargetRegisterClass*> Res;
7065 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00007066
7067 // Not found as a standard register?
7068 if (Res.second == 0) {
7069 // GCC calls "st(0)" just plain "st".
7070 if (StringsEqualNoCase("{st}", Constraint)) {
7071 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00007072 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00007073 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007074
Chris Lattner1a60aa72006-10-31 19:42:44 +00007075 return Res;
7076 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007077
Chris Lattnerf76d1802006-07-31 23:26:50 +00007078 // Otherwise, check to see if this is a register class of the wrong value
7079 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7080 // turn into {ax},{dx}.
7081 if (Res.second->hasType(VT))
7082 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007083
Chris Lattnerf76d1802006-07-31 23:26:50 +00007084 // All of the single-register GCC register classes map their values onto
7085 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7086 // really want an 8-bit or 32-bit register, map to the appropriate register
7087 // class and return the appropriate register.
7088 if (Res.second != X86::GR16RegisterClass)
7089 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007090
Chris Lattnerf76d1802006-07-31 23:26:50 +00007091 if (VT == MVT::i8) {
7092 unsigned DestReg = 0;
7093 switch (Res.first) {
7094 default: break;
7095 case X86::AX: DestReg = X86::AL; break;
7096 case X86::DX: DestReg = X86::DL; break;
7097 case X86::CX: DestReg = X86::CL; break;
7098 case X86::BX: DestReg = X86::BL; break;
7099 }
7100 if (DestReg) {
7101 Res.first = DestReg;
7102 Res.second = Res.second = X86::GR8RegisterClass;
7103 }
7104 } else if (VT == MVT::i32) {
7105 unsigned DestReg = 0;
7106 switch (Res.first) {
7107 default: break;
7108 case X86::AX: DestReg = X86::EAX; break;
7109 case X86::DX: DestReg = X86::EDX; break;
7110 case X86::CX: DestReg = X86::ECX; break;
7111 case X86::BX: DestReg = X86::EBX; break;
7112 case X86::SI: DestReg = X86::ESI; break;
7113 case X86::DI: DestReg = X86::EDI; break;
7114 case X86::BP: DestReg = X86::EBP; break;
7115 case X86::SP: DestReg = X86::ESP; break;
7116 }
7117 if (DestReg) {
7118 Res.first = DestReg;
7119 Res.second = Res.second = X86::GR32RegisterClass;
7120 }
Evan Cheng25ab6902006-09-08 06:48:29 +00007121 } else if (VT == MVT::i64) {
7122 unsigned DestReg = 0;
7123 switch (Res.first) {
7124 default: break;
7125 case X86::AX: DestReg = X86::RAX; break;
7126 case X86::DX: DestReg = X86::RDX; break;
7127 case X86::CX: DestReg = X86::RCX; break;
7128 case X86::BX: DestReg = X86::RBX; break;
7129 case X86::SI: DestReg = X86::RSI; break;
7130 case X86::DI: DestReg = X86::RDI; break;
7131 case X86::BP: DestReg = X86::RBP; break;
7132 case X86::SP: DestReg = X86::RSP; break;
7133 }
7134 if (DestReg) {
7135 Res.first = DestReg;
7136 Res.second = Res.second = X86::GR64RegisterClass;
7137 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00007138 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007139
Chris Lattnerf76d1802006-07-31 23:26:50 +00007140 return Res;
7141}