blob: 9b57f3ac43a237f97412e8fef483f865eafe0c74 [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.
44static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
45
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);
299 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i32, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000300
Dan Gohman7f460202008-06-30 20:59:49 +0000301 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
302 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000303 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000304 if (!Subtarget->isTargetDarwin() &&
305 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000306 !Subtarget->isTargetCygMing()) {
307 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
308 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
309 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000310
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000311 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
312 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
313 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
314 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
315 if (Subtarget->is64Bit()) {
316 // FIXME: Verify
317 setExceptionPointerRegister(X86::RAX);
318 setExceptionSelectorRegister(X86::RDX);
319 } else {
320 setExceptionPointerRegister(X86::EAX);
321 setExceptionSelectorRegister(X86::EDX);
322 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000323 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000324
Duncan Sandsf7331b32007-09-11 14:10:23 +0000325 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000326
Chris Lattnerda68d302008-01-15 21:58:22 +0000327 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000328
Nate Begemanacc398c2006-01-25 18:21:52 +0000329 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
330 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000331 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000332 if (Subtarget->is64Bit()) {
333 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000334 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000335 } else {
336 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000337 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000338 }
Evan Chengae642192007-03-02 23:16:35 +0000339
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000340 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000341 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000342 if (Subtarget->is64Bit())
343 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000344 if (Subtarget->isTargetCygMing())
345 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
346 else
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000348
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000349 if (X86ScalarSSEf64) {
350 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000351 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000352 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
353 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000354
Evan Cheng223547a2006-01-31 22:28:30 +0000355 // Use ANDPD to simulate FABS.
356 setOperationAction(ISD::FABS , MVT::f64, Custom);
357 setOperationAction(ISD::FABS , MVT::f32, Custom);
358
359 // Use XORP to simulate FNEG.
360 setOperationAction(ISD::FNEG , MVT::f64, Custom);
361 setOperationAction(ISD::FNEG , MVT::f32, Custom);
362
Evan Cheng68c47cb2007-01-05 07:55:56 +0000363 // Use ANDPD and ORPD to simulate FCOPYSIGN.
364 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
365 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
366
Evan Chengd25e9e82006-02-02 00:28:23 +0000367 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000368 setOperationAction(ISD::FSIN , MVT::f64, Expand);
369 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000370 setOperationAction(ISD::FSIN , MVT::f32, Expand);
371 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000372
Chris Lattnera54aa942006-01-29 06:26:08 +0000373 // Expand FP immediates into loads from the stack, except for the special
374 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000375 addLegalFPImmediate(APFloat(+0.0)); // xorpd
376 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000377
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000378 // Floating truncations from f80 and extensions to f80 go through memory.
379 // If optimizing, we lie about this though and handle it in
380 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
381 if (Fast) {
382 setConvertAction(MVT::f32, MVT::f80, Expand);
383 setConvertAction(MVT::f64, MVT::f80, Expand);
384 setConvertAction(MVT::f80, MVT::f32, Expand);
385 setConvertAction(MVT::f80, MVT::f64, Expand);
386 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000387 } else if (X86ScalarSSEf32) {
388 // Use SSE for f32, x87 for f64.
389 // Set up the FP register classes.
390 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
391 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
392
393 // Use ANDPS to simulate FABS.
394 setOperationAction(ISD::FABS , MVT::f32, Custom);
395
396 // Use XORP to simulate FNEG.
397 setOperationAction(ISD::FNEG , MVT::f32, Custom);
398
399 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
400
401 // Use ANDPS and ORPS to simulate FCOPYSIGN.
402 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
403 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
404
405 // We don't support sin/cos/fmod
406 setOperationAction(ISD::FSIN , MVT::f32, Expand);
407 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000408
Nate Begemane1795842008-02-14 08:57:00 +0000409 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000410 addLegalFPImmediate(APFloat(+0.0f)); // xorps
411 addLegalFPImmediate(APFloat(+0.0)); // FLD0
412 addLegalFPImmediate(APFloat(+1.0)); // FLD1
413 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
414 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
415
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000416 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
417 // this though and handle it in InstructionSelectPreprocess so that
418 // dagcombine2 can hack on these.
419 if (Fast) {
420 setConvertAction(MVT::f32, MVT::f64, Expand);
421 setConvertAction(MVT::f32, MVT::f80, Expand);
422 setConvertAction(MVT::f80, MVT::f32, Expand);
423 setConvertAction(MVT::f64, MVT::f32, Expand);
424 // And x87->x87 truncations also.
425 setConvertAction(MVT::f80, MVT::f64, Expand);
426 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000427
428 if (!UnsafeFPMath) {
429 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
430 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
431 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000432 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000433 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000434 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000435 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
436 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000437
Evan Cheng68c47cb2007-01-05 07:55:56 +0000438 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000439 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000440 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
441 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000442
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000443 // Floating truncations go through memory. If optimizing, we lie about
444 // this though and handle it in InstructionSelectPreprocess so that
445 // dagcombine2 can hack on these.
446 if (Fast) {
447 setConvertAction(MVT::f80, MVT::f32, Expand);
448 setConvertAction(MVT::f64, MVT::f32, Expand);
449 setConvertAction(MVT::f80, MVT::f64, Expand);
450 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000451
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000456 addLegalFPImmediate(APFloat(+0.0)); // FLD0
457 addLegalFPImmediate(APFloat(+1.0)); // FLD1
458 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
459 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000464 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000465
Dale Johannesen59a58732007-08-05 18:49:15 +0000466 // Long double always uses X87.
467 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000468 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
469 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000470 {
Chris Lattner71d07a02008-01-27 06:19:31 +0000471 APFloat TmpFlt(+0.0);
472 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
473 addLegalFPImmediate(TmpFlt); // FLD0
474 TmpFlt.changeSign();
475 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
476 APFloat TmpFlt2(+1.0);
477 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
478 addLegalFPImmediate(TmpFlt2); // FLD1
479 TmpFlt2.changeSign();
480 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
481 }
482
Dale Johannesen2f429012007-09-26 21:10:55 +0000483 if (!UnsafeFPMath) {
484 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
485 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
486 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000487
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000488 // Always use a library call for pow.
489 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
490 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
491 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
492
Evan Chengd30bf012006-03-01 01:11:20 +0000493 // First set operation action for all vector types to expand. Then we
494 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000495 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
496 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000497 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
498 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
499 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
500 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
501 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
502 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
503 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
504 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
505 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
506 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
507 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000535 }
536
Evan Chenga88973f2006-03-22 19:22:18 +0000537 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000538 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
539 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
540 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000541 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000542 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000543
Evan Chengd30bf012006-03-01 01:11:20 +0000544 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000545
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000546 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
547 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
548 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000549 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000550
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000551 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
552 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
553 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000554 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000555
Bill Wendling74027e92007-03-15 21:24:36 +0000556 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
557 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
558
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000559 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000560 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000561 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000562 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
563 setOperationAction(ISD::AND, MVT::v2i32, Promote);
564 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
565 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000566
567 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000568 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000569 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000570 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
571 setOperationAction(ISD::OR, MVT::v2i32, Promote);
572 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
573 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000574
575 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000576 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000577 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000578 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
579 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
580 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
581 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000582
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000583 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000584 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000585 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000586 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
587 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
588 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000589 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
590 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000591 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000592
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000593 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
594 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
595 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000596 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000597 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000598
599 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
600 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
601 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000602 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000603
604 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
605 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000606 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000607 }
608
Evan Chenga88973f2006-03-22 19:22:18 +0000609 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000610 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
611
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000612 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
613 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
614 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
615 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000616 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
617 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000618 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
619 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000621 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000622 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000623 setOperationAction(ISD::VSETCC, MVT::v4f32, Legal);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000624 }
625
Evan Chenga88973f2006-03-22 19:22:18 +0000626 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000627 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
628 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
629 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
630 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
631 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
632
Evan Chengf7c378e2006-04-10 07:23:14 +0000633 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
634 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
635 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000636 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000637 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
638 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
639 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000640 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000641 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000642 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
643 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
644 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
645 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000646 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
647 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000648
Nate Begemanc2616e42008-05-12 20:34:32 +0000649 setOperationAction(ISD::VSETCC, MVT::v2f64, Legal);
650 setOperationAction(ISD::VSETCC, MVT::v16i8, Legal);
651 setOperationAction(ISD::VSETCC, MVT::v8i16, Legal);
652 setOperationAction(ISD::VSETCC, MVT::v4i32, Legal);
653 setOperationAction(ISD::VSETCC, MVT::v2i64, Legal);
654
Evan Chengf7c378e2006-04-10 07:23:14 +0000655 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
656 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000657 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000658 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000659 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000660
Evan Cheng2c3ae372006-04-12 21:21:57 +0000661 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000662 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
663 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000664 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000665 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000666 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000667 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
668 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
669 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000670 }
671 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
674 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000675 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000676 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000677 if (Subtarget->is64Bit()) {
678 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000679 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000680 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000681
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000682 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000683 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000684 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
685 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
686 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
687 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
688 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
689 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
690 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
691 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
692 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
693 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000694 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000695
Chris Lattnerddf89562008-01-17 19:59:44 +0000696 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000697
Evan Cheng2c3ae372006-04-12 21:21:57 +0000698 // Custom lower v2i64 and v2f64 selects.
699 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000700 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000701 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000702 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000703
Evan Cheng470a6ad2006-02-22 02:26:30 +0000704 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000705
706 if (Subtarget->hasSSE41()) {
707 // FIXME: Do we need to handle scalar-to-vector here?
708 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohman0b924dc2008-05-23 17:49:40 +0000709 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000710
711 // i8 and i16 vectors are custom , because the source register and source
712 // source memory operand types are not the same width. f32 vectors are
713 // custom since the immediate controlling the insert encodes additional
714 // information.
715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
716 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
717 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
718 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
719
720 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
721 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
722 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng62a3f152008-03-24 21:52:23 +0000723 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000724
725 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000726 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
727 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000728 }
729 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000730
Evan Cheng6be2c582006-04-05 23:38:46 +0000731 // We want to custom lower some of our intrinsics.
732 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
733
Evan Cheng206ee9d2006-07-07 08:33:52 +0000734 // We have target-specific dag combine patterns for the following nodes:
735 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000736 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000737 setTargetDAGCombine(ISD::SELECT);
Chris Lattner149a4e52008-02-22 02:09:43 +0000738 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000739
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000740 computeRegisterProperties();
741
Evan Cheng87ed7162006-02-14 08:25:08 +0000742 // FIXME: These should be based on subtarget info. Plus, the values should
743 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000744 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
745 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
746 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000747 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000748 setPrefLoopAlignment(16);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000749}
750
Scott Michel5b8f82e2008-03-10 15:42:14 +0000751
Duncan Sands83ec4b62008-06-06 12:08:01 +0000752MVT X86TargetLowering::getSetCCResultType(const SDOperand &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000753 return MVT::i8;
754}
755
756
Evan Cheng29286502008-01-23 23:17:41 +0000757/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
758/// the desired ByVal argument alignment.
759static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
760 if (MaxAlign == 16)
761 return;
762 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
763 if (VTy->getBitWidth() == 128)
764 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000765 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
766 unsigned EltAlign = 0;
767 getMaxByValAlign(ATy->getElementType(), EltAlign);
768 if (EltAlign > MaxAlign)
769 MaxAlign = EltAlign;
770 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
771 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
772 unsigned EltAlign = 0;
773 getMaxByValAlign(STy->getElementType(i), EltAlign);
774 if (EltAlign > MaxAlign)
775 MaxAlign = EltAlign;
776 if (MaxAlign == 16)
777 break;
778 }
779 }
780 return;
781}
782
783/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
784/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000785/// that contain SSE vectors are placed at 16-byte boundaries while the rest
786/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000787unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
788 if (Subtarget->is64Bit())
789 return getTargetData()->getABITypeAlignment(Ty);
790 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000791 if (Subtarget->hasSSE1())
792 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000793 return Align;
794}
Chris Lattner2b02a442007-02-25 08:29:00 +0000795
Evan Chengf0df0312008-05-15 08:39:06 +0000796/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000797/// and store operations as a result of memset, memcpy, and memmove
798/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000799/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000800MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000801X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
802 bool isSrcConst, bool isSrcStr) const {
803 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
804 return MVT::v4i32;
805 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
806 return MVT::v4f32;
807 if (Subtarget->is64Bit() && Size >= 8)
808 return MVT::i64;
809 return MVT::i32;
810}
811
812
Evan Chengcc415862007-11-09 01:32:10 +0000813/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
814/// jumptable.
815SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
816 SelectionDAG &DAG) const {
817 if (usesGlobalOffsetTable())
818 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
819 if (!Subtarget->isPICStyleRIPRel())
820 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
821 return Table;
822}
823
Chris Lattner2b02a442007-02-25 08:29:00 +0000824//===----------------------------------------------------------------------===//
825// Return Value Calling Convention Implementation
826//===----------------------------------------------------------------------===//
827
Chris Lattner59ed56b2007-02-28 04:55:35 +0000828#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000829
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000830/// LowerRET - Lower an ISD::RET node.
831SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
832 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
833
Chris Lattner9774c912007-02-27 05:28:59 +0000834 SmallVector<CCValAssign, 16> RVLocs;
835 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000836 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
837 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000838 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000839
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000840 // If this is the first return lowered for this function, add the regs to the
841 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000842 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000843 for (unsigned i = 0; i != RVLocs.size(); ++i)
844 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000845 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000846 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000847 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000848
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000849 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000850 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000851 if (Chain.getOpcode() == X86ISD::TAILCALL) {
852 SDOperand TailCall = Chain;
853 SDOperand TargetAddress = TailCall.getOperand(1);
854 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000855 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000856 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
857 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
858 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
859 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
860 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000861 assert(StackAdjustment.getOpcode() == ISD::Constant &&
862 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000863
864 SmallVector<SDOperand,8> Operands;
865 Operands.push_back(Chain.getOperand(0));
866 Operands.push_back(TargetAddress);
867 Operands.push_back(StackAdjustment);
868 // Copy registers used by the call. Last operand is a flag so it is not
869 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000870 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000871 Operands.push_back(Chain.getOperand(i));
872 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000873 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
874 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000875 }
876
877 // Regular return.
878 SDOperand Flag;
879
Chris Lattner447ff682008-03-11 03:23:40 +0000880 SmallVector<SDOperand, 6> RetOps;
881 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
882 // Operand #1 = Bytes To Pop
883 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
884
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000885 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000886 for (unsigned i = 0; i != RVLocs.size(); ++i) {
887 CCValAssign &VA = RVLocs[i];
888 assert(VA.isRegLoc() && "Can only return in registers!");
889 SDOperand ValToCopy = Op.getOperand(i*2+1);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000890
Chris Lattner447ff682008-03-11 03:23:40 +0000891 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
892 // the RET instruction and handled by the FP Stackifier.
893 if (RVLocs[i].getLocReg() == X86::ST0 ||
894 RVLocs[i].getLocReg() == X86::ST1) {
895 // If this is a copy from an xmm register to ST(0), use an FPExtend to
896 // change the value to the FP stack register class.
897 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
898 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
899 RetOps.push_back(ValToCopy);
900 // Don't emit a copytoreg.
901 continue;
902 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000903
Chris Lattner8e6da152008-03-10 21:08:41 +0000904 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000905 Flag = Chain.getValue(1);
906 }
Dan Gohman61a92132008-04-21 23:59:07 +0000907
908 // The x86-64 ABI for returning structs by value requires that we copy
909 // the sret argument into %rax for the return. We saved the argument into
910 // a virtual register in the entry block, so now we copy the value out
911 // and into %rax.
912 if (Subtarget->is64Bit() &&
913 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
914 MachineFunction &MF = DAG.getMachineFunction();
915 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
916 unsigned Reg = FuncInfo->getSRetReturnReg();
917 if (!Reg) {
918 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
919 FuncInfo->setSRetReturnReg(Reg);
920 }
921 SDOperand Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
922
923 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
924 Flag = Chain.getValue(1);
925 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000926
Chris Lattner447ff682008-03-11 03:23:40 +0000927 RetOps[0] = Chain; // Update chain.
928
929 // Add the flag if we have it.
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000930 if (Flag.Val)
Chris Lattner447ff682008-03-11 03:23:40 +0000931 RetOps.push_back(Flag);
932
933 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000934}
935
936
Chris Lattner3085e152007-02-25 08:59:22 +0000937/// LowerCallResult - Lower the result values of an ISD::CALL into the
938/// appropriate copies out of appropriate physical registers. This assumes that
939/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
940/// being lowered. The returns a SDNode with the same number of values as the
941/// ISD::CALL.
942SDNode *X86TargetLowering::
943LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
944 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000945
946 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000947 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000948 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
949 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000950 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
951
Chris Lattnere32bbf62007-02-28 07:09:55 +0000952 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000953
954 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +0000955 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000956 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattner8e6da152008-03-10 21:08:41 +0000957
958 // If this is a call to a function that returns an fp value on the floating
959 // point stack, but where we prefer to use the value in xmm registers, copy
960 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
961 if (RVLocs[i].getLocReg() == X86::ST0 &&
962 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
963 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +0000964 }
Chris Lattner3085e152007-02-25 08:59:22 +0000965
Chris Lattner8e6da152008-03-10 21:08:41 +0000966 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
967 CopyVT, InFlag).getValue(1);
968 SDOperand Val = Chain.getValue(0);
969 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +0000970
Chris Lattner8e6da152008-03-10 21:08:41 +0000971 if (CopyVT != RVLocs[i].getValVT()) {
972 // Round the F80 the right size, which also moves to the appropriate xmm
973 // register.
974 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
975 // This truncation won't change the value.
976 DAG.getIntPtrConstant(1));
977 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000978
Chris Lattner8e6da152008-03-10 21:08:41 +0000979 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +0000980 }
Duncan Sands4bdcb612008-07-02 17:40:58 +0000981
Chris Lattner3085e152007-02-25 08:59:22 +0000982 // Merge everything together with a MERGE_VALUES node.
983 ResultVals.push_back(Chain);
Duncan Sandsf9516202008-06-30 10:19:09 +0000984 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
985 ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000986}
987
988
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000989//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000990// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000991//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000992// StdCall calling convention seems to be standard for many Windows' API
993// routines and around. It differs from C calling convention just a little:
994// callee should clean up the stack, not caller. Symbols should be also
995// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000996// For info on fast calling convention see Fast Calling Convention (tail call)
997// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000998
Evan Cheng85e38002006-04-27 05:35:28 +0000999/// AddLiveIn - This helper function adds the specified physical register to the
1000/// MachineFunction as a live in value. It also creates a corresponding virtual
1001/// register for it.
1002static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001003 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +00001004 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +00001005 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1006 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +00001007 return VReg;
1008}
1009
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001010/// CallIsStructReturn - Determines whether a CALL node uses struct return
1011/// semantics.
Gordon Henriksen86737662008-01-05 16:56:59 +00001012static bool CallIsStructReturn(SDOperand Op) {
1013 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1014 if (!NumOps)
1015 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001016
1017 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001018}
1019
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001020/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1021/// return semantics.
Gordon Henriksen86737662008-01-05 16:56:59 +00001022static bool ArgsAreStructReturn(SDOperand Op) {
1023 unsigned NumArgs = Op.Val->getNumValues() - 1;
1024 if (!NumArgs)
1025 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001026
1027 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001028}
1029
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001030/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1031/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001032/// calls.
Gordon Henriksen86737662008-01-05 16:56:59 +00001033bool X86TargetLowering::IsCalleePop(SDOperand Op) {
1034 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1035 if (IsVarArg)
1036 return false;
1037
1038 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1039 default:
1040 return false;
1041 case CallingConv::X86_StdCall:
1042 return !Subtarget->is64Bit();
1043 case CallingConv::X86_FastCall:
1044 return !Subtarget->is64Bit();
1045 case CallingConv::Fast:
1046 return PerformTailCallOpt;
1047 }
1048}
1049
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001050/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1051/// FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001052CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1053 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1054
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001055 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001056 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001057 return CC_X86_Win64_C;
1058 else {
1059 if (CC == CallingConv::Fast && PerformTailCallOpt)
1060 return CC_X86_64_TailCall;
1061 else
1062 return CC_X86_64_C;
1063 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001064 }
1065
Gordon Henriksen86737662008-01-05 16:56:59 +00001066 if (CC == CallingConv::X86_FastCall)
1067 return CC_X86_32_FastCall;
1068 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1069 return CC_X86_32_TailCall;
1070 else
1071 return CC_X86_32_C;
1072}
1073
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001074/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1075/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001076NameDecorationStyle
1077X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1078 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1079 if (CC == CallingConv::X86_FastCall)
1080 return FastCall;
1081 else if (CC == CallingConv::X86_StdCall)
1082 return StdCall;
1083 return None;
1084}
1085
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001086
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001087/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1088/// in a register before calling.
1089bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1090 return !IsTailCall && !Is64Bit &&
1091 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1092 Subtarget->isPICStyleGOT();
1093}
1094
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001095/// CallRequiresFnAddressInReg - Check whether the call requires the function
1096/// address to be loaded in a register.
1097bool
1098X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1099 return !Is64Bit && IsTailCall &&
1100 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1101 Subtarget->isPICStyleGOT();
1102}
1103
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001104/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1105/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001106/// the specific parameter attribute. The copy will be passed as a byval
1107/// function parameter.
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001108static SDOperand
Evan Cheng8e5712b2008-01-12 01:08:07 +00001109CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001110 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Duncan Sands276dcbd2008-03-21 09:14:45 +00001111 SDOperand SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohman707e0182008-04-12 04:36:06 +00001112 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001113 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001114}
1115
Rafael Espindola7effac52007-09-14 15:48:13 +00001116SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1117 const CCValAssign &VA,
1118 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001119 unsigned CC,
Rafael Espindola7effac52007-09-14 15:48:13 +00001120 SDOperand Root, unsigned i) {
1121 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001122 ISD::ArgFlagsTy Flags =
1123 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001124 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001125 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001126
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001127 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1128 // changed with more analysis.
1129 // In case of tail call optimization mark all arguments mutable. Since they
1130 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001131 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001132 VA.getLocMemOffset(), isImmutable);
Rafael Espindola7effac52007-09-14 15:48:13 +00001133 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001134 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001135 return FIN;
Dan Gohman69de1932008-02-06 22:27:42 +00001136 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001137 PseudoSourceValue::getFixedStack(), FI);
Rafael Espindola7effac52007-09-14 15:48:13 +00001138}
1139
Gordon Henriksen86737662008-01-05 16:56:59 +00001140SDOperand
1141X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001142 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001143 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1144
1145 const Function* Fn = MF.getFunction();
1146 if (Fn->hasExternalLinkage() &&
1147 Subtarget->isTargetCygMing() &&
1148 Fn->getName() == "main")
1149 FuncInfo->setForceFramePointer(true);
1150
1151 // Decorate the function name.
1152 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1153
Evan Cheng1bc78042006-04-26 01:20:17 +00001154 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001155 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001156 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001157 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001158 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001159 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001160
1161 assert(!(isVarArg && CC == CallingConv::Fast) &&
1162 "Var args not supported with calling convention fastcc");
1163
Chris Lattner638402b2007-02-28 07:00:42 +00001164 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001165 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001166 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen86737662008-01-05 16:56:59 +00001167 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001168
Chris Lattnerf39f7712007-02-28 05:46:49 +00001169 SmallVector<SDOperand, 8> ArgValues;
1170 unsigned LastVal = ~0U;
1171 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1172 CCValAssign &VA = ArgLocs[i];
1173 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1174 // places.
1175 assert(VA.getValNo() != LastVal &&
1176 "Don't support value assigned to multiple locs yet");
1177 LastVal = VA.getValNo();
1178
1179 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001180 MVT RegVT = VA.getLocVT();
Chris Lattnerf39f7712007-02-28 05:46:49 +00001181 TargetRegisterClass *RC;
1182 if (RegVT == MVT::i32)
1183 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001184 else if (Is64Bit && RegVT == MVT::i64)
1185 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001186 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001187 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001188 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001189 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001190 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001191 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001192 else if (RegVT.isVector()) {
1193 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001194 if (!Is64Bit)
1195 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1196 else {
1197 // Darwin calling convention passes MMX values in either GPRs or
1198 // XMMs in x86-64. Other targets pass them in memory.
1199 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1200 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1201 RegVT = MVT::v2i64;
1202 } else {
1203 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1204 RegVT = MVT::i64;
1205 }
1206 }
1207 } else {
1208 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001209 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001210
Chris Lattner82932a52007-03-02 05:12:29 +00001211 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1212 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001213
1214 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1215 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1216 // right size.
1217 if (VA.getLocInfo() == CCValAssign::SExt)
1218 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1219 DAG.getValueType(VA.getValVT()));
1220 else if (VA.getLocInfo() == CCValAssign::ZExt)
1221 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1222 DAG.getValueType(VA.getValVT()));
1223
1224 if (VA.getLocInfo() != CCValAssign::Full)
1225 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1226
Gordon Henriksen86737662008-01-05 16:56:59 +00001227 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001228 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001229 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Cheng44c0fd12008-04-25 20:13:28 +00001230 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1231 else if (RC == X86::VR128RegisterClass) {
1232 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1233 DAG.getConstant(0, MVT::i64));
1234 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1235 }
1236 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001237
Chris Lattnerf39f7712007-02-28 05:46:49 +00001238 ArgValues.push_back(ArgValue);
1239 } else {
1240 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001241 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001242 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001243 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001244
Dan Gohman61a92132008-04-21 23:59:07 +00001245 // The x86-64 ABI for returning structs by value requires that we copy
1246 // the sret argument into %rax for the return. Save the argument into
1247 // a virtual register so that we can access it from the return points.
1248 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1249 MachineFunction &MF = DAG.getMachineFunction();
1250 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1251 unsigned Reg = FuncInfo->getSRetReturnReg();
1252 if (!Reg) {
1253 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1254 FuncInfo->setSRetReturnReg(Reg);
1255 }
1256 SDOperand Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
1257 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1258 }
1259
Chris Lattnerf39f7712007-02-28 05:46:49 +00001260 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001261 // align stack specially for tail calls
Gordon Henriksenae636f82008-01-03 16:47:34 +00001262 if (CC == CallingConv::Fast)
1263 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001264
Evan Cheng1bc78042006-04-26 01:20:17 +00001265 // If the function takes variable number of arguments, make a frame index for
1266 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001267 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001268 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1269 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1270 }
1271 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001272 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1273
1274 // FIXME: We should really autogenerate these arrays
1275 static const unsigned GPR64ArgRegsWin64[] = {
1276 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001277 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001278 static const unsigned XMMArgRegsWin64[] = {
1279 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1280 };
1281 static const unsigned GPR64ArgRegs64Bit[] = {
1282 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1283 };
1284 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001285 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1286 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1287 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001288 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1289
1290 if (IsWin64) {
1291 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1292 GPR64ArgRegs = GPR64ArgRegsWin64;
1293 XMMArgRegs = XMMArgRegsWin64;
1294 } else {
1295 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1296 GPR64ArgRegs = GPR64ArgRegs64Bit;
1297 XMMArgRegs = XMMArgRegs64Bit;
1298 }
1299 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1300 TotalNumIntRegs);
1301 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1302 TotalNumXMMRegs);
1303
Gordon Henriksen86737662008-01-05 16:56:59 +00001304 // For X86-64, if there are vararg parameters that are passed via
1305 // registers, then we must store them to their spots on the stack so they
1306 // may be loaded by deferencing the result of va_next.
1307 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001308 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1309 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1310 TotalNumXMMRegs * 16, 16);
1311
Gordon Henriksen86737662008-01-05 16:56:59 +00001312 // Store the integer parameter registers.
1313 SmallVector<SDOperand, 8> MemOps;
1314 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1315 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001316 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001317 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001318 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1319 X86::GR64RegisterClass);
1320 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
Dan Gohman69de1932008-02-06 22:27:42 +00001321 SDOperand Store =
1322 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001323 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001324 RegSaveFrameIndex);
Gordon Henriksen86737662008-01-05 16:56:59 +00001325 MemOps.push_back(Store);
1326 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001327 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001328 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001329
Gordon Henriksen86737662008-01-05 16:56:59 +00001330 // Now store the XMM (fp + vector) parameter registers.
1331 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001332 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001333 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001334 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1335 X86::VR128RegisterClass);
1336 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
Dan Gohman69de1932008-02-06 22:27:42 +00001337 SDOperand Store =
1338 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001339 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00001340 RegSaveFrameIndex);
Gordon Henriksen86737662008-01-05 16:56:59 +00001341 MemOps.push_back(Store);
1342 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001343 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001344 }
1345 if (!MemOps.empty())
1346 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1347 &MemOps[0], MemOps.size());
1348 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001349 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001350
1351 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1352 // arguments and the arguments after the retaddr has been pushed are
1353 // aligned.
1354 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1355 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1356 (StackSize & 7) == 0)
1357 StackSize += 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001358
Gordon Henriksenae636f82008-01-03 16:47:34 +00001359 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001360
Gordon Henriksen86737662008-01-05 16:56:59 +00001361 // Some CCs need callee pop.
1362 if (IsCalleePop(Op)) {
1363 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001364 BytesCallerReserves = 0;
1365 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001366 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001367 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001368 if (!Is64Bit && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001369 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001370 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001371 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001372
Gordon Henriksen86737662008-01-05 16:56:59 +00001373 if (!Is64Bit) {
1374 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1375 if (CC == CallingConv::X86_FastCall)
1376 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1377 }
Evan Cheng25caf632006-05-23 21:06:34 +00001378
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001379 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001380
Evan Cheng25caf632006-05-23 21:06:34 +00001381 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +00001382 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1383 ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001384}
1385
Evan Chengdffbd832008-01-10 00:09:10 +00001386SDOperand
1387X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1388 const SDOperand &StackPtr,
1389 const CCValAssign &VA,
1390 SDOperand Chain,
1391 SDOperand Arg) {
Dan Gohman4fdad172008-02-07 16:28:05 +00001392 unsigned LocMemOffset = VA.getLocMemOffset();
1393 SDOperand PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001394 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001395 ISD::ArgFlagsTy Flags =
1396 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1397 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001398 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001399 }
Dan Gohman4fdad172008-02-07 16:28:05 +00001400 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001401 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001402}
1403
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001404/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1405/// optimization is performed and it is required.
1406SDOperand
1407X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
1408 SDOperand &OutRetAddr,
1409 SDOperand Chain,
1410 bool IsTailCall,
1411 bool Is64Bit,
1412 int FPDiff) {
1413 if (!IsTailCall || FPDiff==0) return Chain;
1414
1415 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001416 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001417 OutRetAddr = getReturnAddressFrameIndex(DAG);
1418 // Load the "old" Return address.
1419 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
1420 return SDOperand(OutRetAddr.Val, 1);
1421}
1422
1423/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1424/// optimization is performed and it is required (FPDiff!=0).
1425static SDOperand
1426EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
1427 SDOperand Chain, SDOperand RetAddrFrIdx,
1428 bool Is64Bit, int FPDiff) {
1429 // Store the return address to the appropriate stack slot.
1430 if (!FPDiff) return Chain;
1431 // Calculate the new stack slot for the return address.
1432 int SlotSize = Is64Bit ? 8 : 4;
1433 int NewReturnAddrFI =
1434 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001435 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001436 SDOperand NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1437 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
1438 PseudoSourceValue::getFixedStack(), NewReturnAddrFI);
1439 return Chain;
1440}
1441
Gordon Henriksen86737662008-01-05 16:56:59 +00001442SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1443 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng32fe1032006-05-25 00:59:30 +00001444 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001445 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001446 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen86737662008-01-05 16:56:59 +00001447 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1448 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng32fe1032006-05-25 00:59:30 +00001449 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen86737662008-01-05 16:56:59 +00001450 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng0d9e9762008-01-29 19:34:22 +00001451 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001452
1453 assert(!(isVarArg && CC == CallingConv::Fast) &&
1454 "Var args not supported with calling convention fastcc");
1455
Chris Lattner638402b2007-02-28 07:00:42 +00001456 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001457 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001458 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner920c37a2008-03-21 06:50:21 +00001459 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001460
Chris Lattner423c5f42007-02-28 05:31:48 +00001461 // Get a count of how many bytes are to be pushed on the stack.
1462 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001463 if (CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001464 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001465
Gordon Henriksen86737662008-01-05 16:56:59 +00001466 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1467 // arguments and the arguments after the retaddr has been pushed are aligned.
1468 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1469 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1470 (NumBytes & 7) == 0)
1471 NumBytes += 4;
1472
1473 int FPDiff = 0;
1474 if (IsTailCall) {
1475 // Lower arguments at fp - stackoffset + fpdiff.
1476 unsigned NumBytesCallerPushed =
1477 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1478 FPDiff = NumBytesCallerPushed - NumBytes;
1479
1480 // Set the delta of movement of the returnaddr stackslot.
1481 // But only set if delta is greater than previous delta.
1482 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1483 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1484 }
1485
Chris Lattner0bd48932008-01-17 07:00:52 +00001486 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001487
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001488 SDOperand RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001489 // Load return adress for tail calls.
1490 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1491 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001492
Chris Lattner5a88b832007-02-25 07:10:00 +00001493 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1494 SmallVector<SDOperand, 8> MemOpChains;
Chris Lattner423c5f42007-02-28 05:31:48 +00001495 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001496
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001497 // Walk the register/memloc assignments, inserting copies/loads. In the case
1498 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001499 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1500 CCValAssign &VA = ArgLocs[i];
1501 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001502 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1503 getArgFlags().isByVal();
1504
Chris Lattner423c5f42007-02-28 05:31:48 +00001505 // Promote the value if needed.
1506 switch (VA.getLocInfo()) {
1507 default: assert(0 && "Unknown loc info!");
1508 case CCValAssign::Full: break;
1509 case CCValAssign::SExt:
1510 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1511 break;
1512 case CCValAssign::ZExt:
1513 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1514 break;
1515 case CCValAssign::AExt:
1516 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1517 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001518 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001519
1520 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001521 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001522 MVT RegVT = VA.getLocVT();
1523 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001524 switch (VA.getLocReg()) {
1525 default:
1526 break;
1527 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1528 case X86::R8: {
1529 // Special case: passing MMX values in GPR registers.
1530 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1531 break;
1532 }
1533 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1534 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1535 // Special case: passing MMX values in XMM registers.
1536 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1537 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1538 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1539 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1540 getMOVLMask(2, DAG));
1541 break;
1542 }
1543 }
1544 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001545 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1546 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001547 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001548 assert(VA.isMemLoc());
1549 if (StackPtr.Val == 0)
1550 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1551
1552 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1553 Arg));
1554 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001555 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001556 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001557
Evan Cheng32fe1032006-05-25 00:59:30 +00001558 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001559 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1560 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001561
Evan Cheng347d5f72006-04-28 21:29:37 +00001562 // Build a sequence of copy-to-reg nodes chained together with token chain
1563 // and flag operands which copy the outgoing args into registers.
1564 SDOperand InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001565 // Tail call byval lowering might overwrite argument registers so in case of
1566 // tail call optimization the copies to registers are lowered later.
1567 if (!IsTailCall)
1568 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1569 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1570 InFlag);
1571 InFlag = Chain.getValue(1);
1572 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001573
Evan Chengf4684712007-02-21 21:18:14 +00001574 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001575 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001576 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1577 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1578 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1579 InFlag);
1580 InFlag = Chain.getValue(1);
1581 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001582 // If we are tail calling and generating PIC/GOT style code load the address
1583 // of the callee into ecx. The value in ecx is used as target of the tail
1584 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1585 // calls on PIC/GOT architectures. Normally we would just put the address of
1586 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1587 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001588 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001589 // Note: The actual moving to ecx is done further down.
1590 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1591 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1592 !G->getGlobal()->hasProtectedVisibility())
1593 Callee = LowerGlobalAddress(Callee, DAG);
1594 else if (isa<ExternalSymbolSDNode>(Callee))
1595 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001596 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001597
Gordon Henriksen86737662008-01-05 16:56:59 +00001598 if (Is64Bit && isVarArg) {
1599 // From AMD64 ABI document:
1600 // For calls that may call functions that use varargs or stdargs
1601 // (prototype-less calls or calls to functions containing ellipsis (...) in
1602 // the declaration) %al is used as hidden argument to specify the number
1603 // of SSE registers used. The contents of %al do not need to match exactly
1604 // the number of registers, but must be an ubound on the number of SSE
1605 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001606
1607 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001608 // Count the number of XMM registers allocated.
1609 static const unsigned XMMArgRegs[] = {
1610 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1611 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1612 };
1613 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1614
1615 Chain = DAG.getCopyToReg(Chain, X86::AL,
1616 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1617 InFlag = Chain.getValue(1);
1618 }
1619
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001620
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001621 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001622 if (IsTailCall) {
1623 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen86737662008-01-05 16:56:59 +00001624 SDOperand FIN;
1625 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001626 // Do not flag preceeding copytoreg stuff together with the following stuff.
1627 InFlag = SDOperand();
Gordon Henriksen86737662008-01-05 16:56:59 +00001628 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1629 CCValAssign &VA = ArgLocs[i];
1630 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001631 assert(VA.isMemLoc());
1632 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001633 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001634 ISD::ArgFlagsTy Flags =
1635 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen86737662008-01-05 16:56:59 +00001636 // Create frame index.
1637 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001638 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001640 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001641
Duncan Sands276dcbd2008-03-21 09:14:45 +00001642 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001643 // Copy relative to framepointer.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001644 SDOperand Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
1645 if (StackPtr.Val == 0)
1646 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1647 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1648
1649 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng8e5712b2008-01-12 01:08:07 +00001650 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001651 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001652 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001653 MemOpChains2.push_back(
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001654 DAG.getStore(Chain, Arg, FIN,
Dan Gohman3069b872008-02-07 18:41:25 +00001655 PseudoSourceValue::getFixedStack(), FI));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001656 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001657 }
1658 }
1659
1660 if (!MemOpChains2.empty())
1661 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001662 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001663
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001664 // Copy arguments to their registers.
1665 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1666 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1667 InFlag);
1668 InFlag = Chain.getValue(1);
1669 }
1670 InFlag =SDOperand();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001671
Gordon Henriksen86737662008-01-05 16:56:59 +00001672 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001673 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1674 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001675 }
1676
Evan Cheng32fe1032006-05-25 00:59:30 +00001677 // If the callee is a GlobalAddress node (quite common, every direct call is)
1678 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001679 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001680 // We should use extra load for direct calls to dllimported functions in
1681 // non-JIT mode.
Gordon Henriksen86737662008-01-05 16:56:59 +00001682 if ((IsTailCall || !Is64Bit ||
1683 getTargetMachine().getCodeModel() != CodeModel::Large)
1684 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1685 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001686 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksenae636f82008-01-03 16:47:34 +00001687 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001688 if (IsTailCall || !Is64Bit ||
1689 getTargetMachine().getCodeModel() != CodeModel::Large)
1690 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1691 } else if (IsTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001692 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1693
1694 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001695 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001696 Callee,InFlag);
1697 Callee = DAG.getRegister(Opc, getPointerTy());
1698 // Add register as live out.
1699 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001700 }
1701
Chris Lattnerd96d0722007-02-25 06:40:16 +00001702 // Returns a chain & a flag for retval copy to use.
1703 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001704 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001705
1706 if (IsTailCall) {
1707 Ops.push_back(Chain);
Chris Lattner0bd48932008-01-17 07:00:52 +00001708 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1709 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 if (InFlag.Val)
1711 Ops.push_back(InFlag);
1712 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1713 InFlag = Chain.getValue(1);
1714
1715 // Returns a chain & a flag for retval copy to use.
1716 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1717 Ops.clear();
1718 }
1719
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001720 Ops.push_back(Chain);
1721 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001722
Gordon Henriksen86737662008-01-05 16:56:59 +00001723 if (IsTailCall)
1724 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001725
Gordon Henriksen86737662008-01-05 16:56:59 +00001726 // Add argument registers to the end of the list so that they are known live
1727 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001728 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1729 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1730 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001731
Evan Cheng586ccac2008-03-18 23:36:35 +00001732 // Add an implicit use GOT pointer in EBX.
1733 if (!IsTailCall && !Is64Bit &&
1734 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1735 Subtarget->isPICStyleGOT())
1736 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1737
1738 // Add an implicit use of AL for x86 vararg functions.
1739 if (Is64Bit && isVarArg)
1740 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1741
Evan Cheng347d5f72006-04-28 21:29:37 +00001742 if (InFlag.Val)
1743 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001744
Gordon Henriksen86737662008-01-05 16:56:59 +00001745 if (IsTailCall) {
1746 assert(InFlag.Val &&
1747 "Flag must be set. Depend on flag being set in LowerRET");
1748 Chain = DAG.getNode(X86ISD::TAILCALL,
1749 Op.Val->getVTList(), &Ops[0], Ops.size());
1750
1751 return SDOperand(Chain.Val, Op.ResNo);
1752 }
1753
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001754 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001755 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001756
Chris Lattner2d297092006-05-23 18:50:38 +00001757 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001758 unsigned NumBytesForCalleeToPush;
1759 if (IsCalleePop(Op))
1760 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng0d9e9762008-01-29 19:34:22 +00001761 else if (!Is64Bit && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001762 // If this is is a call to a struct-return function, the callee
1763 // pops the hidden struct pointer, so we have to push it back.
1764 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001765 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001766 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001767 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001768
Gordon Henriksenae636f82008-01-03 16:47:34 +00001769 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001770 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner0bd48932008-01-17 07:00:52 +00001771 DAG.getIntPtrConstant(NumBytes),
1772 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001773 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001774 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001775
Chris Lattner3085e152007-02-25 08:59:22 +00001776 // Handle result values, copying them out of physregs into vregs that we
1777 // return.
Chris Lattner920c37a2008-03-21 06:50:21 +00001778 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001779}
1780
Evan Cheng25ab6902006-09-08 06:48:29 +00001781
1782//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001783// Fast Calling Convention (tail call) implementation
1784//===----------------------------------------------------------------------===//
1785
1786// Like std call, callee cleans arguments, convention except that ECX is
1787// reserved for storing the tail called function address. Only 2 registers are
1788// free for argument passing (inreg). Tail call optimization is performed
1789// provided:
1790// * tailcallopt is enabled
1791// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001792// On X86_64 architecture with GOT-style position independent code only local
1793// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001794// To keep the stack aligned according to platform abi the function
1795// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1796// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001797// If a tail called function callee has more arguments than the caller the
1798// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001799// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001800// original REtADDR, but before the saved framepointer or the spilled registers
1801// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1802// stack layout:
1803// arg1
1804// arg2
1805// RETADDR
1806// [ new RETADDR
1807// move area ]
1808// (possible EBP)
1809// ESI
1810// EDI
1811// local1 ..
1812
1813/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1814/// for a 16 byte align requirement.
1815unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1816 SelectionDAG& DAG) {
1817 if (PerformTailCallOpt) {
1818 MachineFunction &MF = DAG.getMachineFunction();
1819 const TargetMachine &TM = MF.getTarget();
1820 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1821 unsigned StackAlignment = TFI.getStackAlignment();
1822 uint64_t AlignMask = StackAlignment - 1;
1823 int64_t Offset = StackSize;
1824 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1825 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1826 // Number smaller than 12 so just add the difference.
1827 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1828 } else {
1829 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1830 Offset = ((~AlignMask) & Offset) + StackAlignment +
1831 (StackAlignment-SlotSize);
1832 }
1833 StackSize = Offset;
1834 }
1835 return StackSize;
1836}
1837
1838/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001839/// following the call is a return. A function is eligible if caller/callee
1840/// calling conventions match, currently only fastcc supports tail calls, and
1841/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001842bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1843 SDOperand Ret,
1844 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001845 if (!PerformTailCallOpt)
1846 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001847
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001848 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001849 MachineFunction &MF = DAG.getMachineFunction();
1850 unsigned CallerCC = MF.getFunction()->getCallingConv();
1851 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1852 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1853 SDOperand Callee = Call.getOperand(4);
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001854 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001855 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001856 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001857 return true;
1858
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001859 // Can only do local tail calls (in same module, hidden or protected) on
1860 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1862 return G->getGlobal()->hasHiddenVisibility()
1863 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001864 }
1865 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001866
1867 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001868}
1869
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001870//===----------------------------------------------------------------------===//
1871// Other Lowering Hooks
1872//===----------------------------------------------------------------------===//
1873
1874
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001875SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001876 MachineFunction &MF = DAG.getMachineFunction();
1877 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1878 int ReturnAddrIndex = FuncInfo->getRAIndex();
1879
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001880 if (ReturnAddrIndex == 0) {
1881 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00001882 if (Subtarget->is64Bit())
1883 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1884 else
1885 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001886
1887 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001888 }
1889
Evan Cheng25ab6902006-09-08 06:48:29 +00001890 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001891}
1892
1893
1894
Evan Cheng6dfa9992006-01-30 23:41:35 +00001895/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1896/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001897/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1898/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001899static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001900 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1901 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001902 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001903 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001904 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1905 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1906 // X > -1 -> X == 0, jump !sign.
1907 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001908 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001909 return true;
1910 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1911 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001912 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001913 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00001914 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1915 // X < 1 -> X <= 0
1916 RHS = DAG.getConstant(0, RHS.getValueType());
1917 X86CC = X86::COND_LE;
1918 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001919 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001920 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001921
Evan Chengd9558e02006-01-06 00:43:03 +00001922 switch (SetCCOpcode) {
1923 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001924 case ISD::SETEQ: X86CC = X86::COND_E; break;
1925 case ISD::SETGT: X86CC = X86::COND_G; break;
1926 case ISD::SETGE: X86CC = X86::COND_GE; break;
1927 case ISD::SETLT: X86CC = X86::COND_L; break;
1928 case ISD::SETLE: X86CC = X86::COND_LE; break;
1929 case ISD::SETNE: X86CC = X86::COND_NE; break;
1930 case ISD::SETULT: X86CC = X86::COND_B; break;
1931 case ISD::SETUGT: X86CC = X86::COND_A; break;
1932 case ISD::SETULE: X86CC = X86::COND_BE; break;
1933 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001934 }
1935 } else {
1936 // On a floating point condition, the flags are set as follows:
1937 // ZF PF CF op
1938 // 0 | 0 | 0 | X > Y
1939 // 0 | 0 | 1 | X < Y
1940 // 1 | 0 | 0 | X == Y
1941 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001942 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001943 switch (SetCCOpcode) {
1944 default: break;
1945 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001946 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001947 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001948 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001949 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001950 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001951 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001952 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001953 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001954 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001955 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001956 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001957 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001958 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001959 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001960 case ISD::SETNE: X86CC = X86::COND_NE; break;
1961 case ISD::SETUO: X86CC = X86::COND_P; break;
1962 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001963 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001964 if (Flip)
1965 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001966 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001967
Chris Lattner7fbe9722006-10-20 17:42:20 +00001968 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001969}
1970
Evan Cheng4a460802006-01-11 00:33:36 +00001971/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1972/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001973/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001974static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001975 switch (X86CC) {
1976 default:
1977 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001978 case X86::COND_B:
1979 case X86::COND_BE:
1980 case X86::COND_E:
1981 case X86::COND_P:
1982 case X86::COND_A:
1983 case X86::COND_AE:
1984 case X86::COND_NE:
1985 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001986 return true;
1987 }
1988}
1989
Evan Cheng5ced1d82006-04-06 23:23:56 +00001990/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001991/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001992static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1993 if (Op.getOpcode() == ISD::UNDEF)
1994 return true;
1995
1996 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001997 return (Val >= Low && Val < Hi);
1998}
1999
2000/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2001/// true if Op is undef or if its value equal to the specified value.
2002static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
2003 if (Op.getOpcode() == ISD::UNDEF)
2004 return true;
2005 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002006}
2007
Evan Cheng0188ecb2006-03-22 18:59:22 +00002008/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2009/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2010bool X86::isPSHUFDMask(SDNode *N) {
2011 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2012
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002013 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002014 return false;
2015
2016 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002017 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002018 SDOperand Arg = N->getOperand(i);
2019 if (Arg.getOpcode() == ISD::UNDEF) continue;
2020 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002021 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002022 return false;
2023 }
2024
2025 return true;
2026}
2027
2028/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002029/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002030bool X86::isPSHUFHWMask(SDNode *N) {
2031 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2032
2033 if (N->getNumOperands() != 8)
2034 return false;
2035
2036 // Lower quadword copied in order.
2037 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002038 SDOperand Arg = N->getOperand(i);
2039 if (Arg.getOpcode() == ISD::UNDEF) continue;
2040 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2041 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002042 return false;
2043 }
2044
2045 // Upper quadword shuffled.
2046 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002047 SDOperand Arg = N->getOperand(i);
2048 if (Arg.getOpcode() == ISD::UNDEF) continue;
2049 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2050 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002051 if (Val < 4 || Val > 7)
2052 return false;
2053 }
2054
2055 return true;
2056}
2057
2058/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002059/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002060bool X86::isPSHUFLWMask(SDNode *N) {
2061 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2062
2063 if (N->getNumOperands() != 8)
2064 return false;
2065
2066 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002067 for (unsigned i = 4; i != 8; ++i)
2068 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002069 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002070
2071 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002072 for (unsigned i = 0; i != 4; ++i)
2073 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002074 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002075
2076 return true;
2077}
2078
Evan Cheng14aed5e2006-03-24 01:18:28 +00002079/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2080/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002081static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002082 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002083
Evan Cheng39623da2006-04-20 08:58:49 +00002084 unsigned Half = NumElems / 2;
2085 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002086 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002087 return false;
2088 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002089 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002090 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002091
2092 return true;
2093}
2094
Evan Cheng39623da2006-04-20 08:58:49 +00002095bool X86::isSHUFPMask(SDNode *N) {
2096 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002097 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002098}
2099
Evan Cheng213d2cf2007-05-17 18:45:50 +00002100/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002101/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2102/// half elements to come from vector 1 (which would equal the dest.) and
2103/// the upper half to come from vector 2.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002104static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002105 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002106
Chris Lattner5a88b832007-02-25 07:10:00 +00002107 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002108 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002109 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002110 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002111 for (unsigned i = Half; i < NumOps; ++i)
2112 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002113 return false;
2114 return true;
2115}
2116
2117static bool isCommutedSHUFP(SDNode *N) {
2118 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002119 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002120}
2121
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002122/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2123/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2124bool X86::isMOVHLPSMask(SDNode *N) {
2125 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2126
Evan Cheng2064a2b2006-03-28 06:50:32 +00002127 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002128 return false;
2129
Evan Cheng2064a2b2006-03-28 06:50:32 +00002130 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002131 return isUndefOrEqual(N->getOperand(0), 6) &&
2132 isUndefOrEqual(N->getOperand(1), 7) &&
2133 isUndefOrEqual(N->getOperand(2), 2) &&
2134 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002135}
2136
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002137/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2138/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2139/// <2, 3, 2, 3>
2140bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2141 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2142
2143 if (N->getNumOperands() != 4)
2144 return false;
2145
2146 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2147 return isUndefOrEqual(N->getOperand(0), 2) &&
2148 isUndefOrEqual(N->getOperand(1), 3) &&
2149 isUndefOrEqual(N->getOperand(2), 2) &&
2150 isUndefOrEqual(N->getOperand(3), 3);
2151}
2152
Evan Cheng5ced1d82006-04-06 23:23:56 +00002153/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2154/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2155bool X86::isMOVLPMask(SDNode *N) {
2156 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2157
2158 unsigned NumElems = N->getNumOperands();
2159 if (NumElems != 2 && NumElems != 4)
2160 return false;
2161
Evan Chengc5cdff22006-04-07 21:53:05 +00002162 for (unsigned i = 0; i < NumElems/2; ++i)
2163 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2164 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002165
Evan Chengc5cdff22006-04-07 21:53:05 +00002166 for (unsigned i = NumElems/2; i < NumElems; ++i)
2167 if (!isUndefOrEqual(N->getOperand(i), i))
2168 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002169
2170 return true;
2171}
2172
2173/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002174/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2175/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002176bool X86::isMOVHPMask(SDNode *N) {
2177 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2178
2179 unsigned NumElems = N->getNumOperands();
2180 if (NumElems != 2 && NumElems != 4)
2181 return false;
2182
Evan Chengc5cdff22006-04-07 21:53:05 +00002183 for (unsigned i = 0; i < NumElems/2; ++i)
2184 if (!isUndefOrEqual(N->getOperand(i), i))
2185 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002186
2187 for (unsigned i = 0; i < NumElems/2; ++i) {
2188 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002189 if (!isUndefOrEqual(Arg, i + NumElems))
2190 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002191 }
2192
2193 return true;
2194}
2195
Evan Cheng0038e592006-03-28 00:39:58 +00002196/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2197/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002198bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002199 bool V2IsSplat = false) {
2200 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002201 return false;
2202
Chris Lattner5a88b832007-02-25 07:10:00 +00002203 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2204 SDOperand BitI = Elts[i];
2205 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002206 if (!isUndefOrEqual(BitI, j))
2207 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002208 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002209 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002210 return false;
2211 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002212 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002213 return false;
2214 }
Evan Cheng0038e592006-03-28 00:39:58 +00002215 }
2216
2217 return true;
2218}
2219
Evan Cheng39623da2006-04-20 08:58:49 +00002220bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2221 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002222 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002223}
2224
Evan Cheng4fcb9222006-03-28 02:43:26 +00002225/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2226/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002227bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002228 bool V2IsSplat = false) {
2229 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002230 return false;
2231
Chris Lattner5a88b832007-02-25 07:10:00 +00002232 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2233 SDOperand BitI = Elts[i];
2234 SDOperand BitI1 = Elts[i+1];
2235 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002236 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002237 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002238 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002239 return false;
2240 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002241 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002242 return false;
2243 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002244 }
2245
2246 return true;
2247}
2248
Evan Cheng39623da2006-04-20 08:58:49 +00002249bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2250 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002251 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002252}
2253
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002254/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2255/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2256/// <0, 0, 1, 1>
2257bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2258 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2259
2260 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002261 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002262 return false;
2263
2264 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2265 SDOperand BitI = N->getOperand(i);
2266 SDOperand BitI1 = N->getOperand(i+1);
2267
Evan Chengc5cdff22006-04-07 21:53:05 +00002268 if (!isUndefOrEqual(BitI, j))
2269 return false;
2270 if (!isUndefOrEqual(BitI1, j))
2271 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002272 }
2273
2274 return true;
2275}
2276
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002277/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2278/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2279/// <2, 2, 3, 3>
2280bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2281 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2282
2283 unsigned NumElems = N->getNumOperands();
2284 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2285 return false;
2286
2287 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2288 SDOperand BitI = N->getOperand(i);
2289 SDOperand BitI1 = N->getOperand(i + 1);
2290
2291 if (!isUndefOrEqual(BitI, j))
2292 return false;
2293 if (!isUndefOrEqual(BitI1, j))
2294 return false;
2295 }
2296
2297 return true;
2298}
2299
Evan Cheng017dcc62006-04-21 01:05:10 +00002300/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2301/// specifies a shuffle of elements that is suitable for input to MOVSS,
2302/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002303static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002304 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002305 return false;
2306
Chris Lattner5a88b832007-02-25 07:10:00 +00002307 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002308 return false;
2309
Chris Lattner5a88b832007-02-25 07:10:00 +00002310 for (unsigned i = 1; i < NumElts; ++i) {
2311 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002312 return false;
2313 }
2314
2315 return true;
2316}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002317
Evan Cheng017dcc62006-04-21 01:05:10 +00002318bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002319 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002320 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002321}
2322
Evan Cheng017dcc62006-04-21 01:05:10 +00002323/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2324/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002325/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002326static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002327 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002328 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002329 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002330 return false;
2331
2332 if (!isUndefOrEqual(Ops[0], 0))
2333 return false;
2334
Chris Lattner5a88b832007-02-25 07:10:00 +00002335 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002336 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002337 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2338 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2339 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002340 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002341 }
2342
2343 return true;
2344}
2345
Evan Cheng8cf723d2006-09-08 01:50:06 +00002346static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2347 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002348 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002349 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2350 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002351}
2352
Evan Chengd9539472006-04-14 21:59:03 +00002353/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2354/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2355bool X86::isMOVSHDUPMask(SDNode *N) {
2356 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2357
2358 if (N->getNumOperands() != 4)
2359 return false;
2360
2361 // Expect 1, 1, 3, 3
2362 for (unsigned i = 0; i < 2; ++i) {
2363 SDOperand Arg = N->getOperand(i);
2364 if (Arg.getOpcode() == ISD::UNDEF) continue;
2365 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2366 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2367 if (Val != 1) return false;
2368 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002369
2370 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002371 for (unsigned i = 2; i < 4; ++i) {
2372 SDOperand Arg = N->getOperand(i);
2373 if (Arg.getOpcode() == ISD::UNDEF) continue;
2374 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2375 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2376 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002377 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002378 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002379
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002380 // Don't use movshdup if it can be done with a shufps.
2381 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002382}
2383
2384/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2385/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2386bool X86::isMOVSLDUPMask(SDNode *N) {
2387 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2388
2389 if (N->getNumOperands() != 4)
2390 return false;
2391
2392 // Expect 0, 0, 2, 2
2393 for (unsigned i = 0; i < 2; ++i) {
2394 SDOperand Arg = N->getOperand(i);
2395 if (Arg.getOpcode() == ISD::UNDEF) continue;
2396 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2397 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2398 if (Val != 0) return false;
2399 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002400
2401 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002402 for (unsigned i = 2; i < 4; ++i) {
2403 SDOperand Arg = N->getOperand(i);
2404 if (Arg.getOpcode() == ISD::UNDEF) continue;
2405 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2406 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2407 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002408 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002409 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002410
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002411 // Don't use movshdup if it can be done with a shufps.
2412 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002413}
2414
Evan Cheng49892af2007-06-19 00:02:56 +00002415/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2416/// specifies a identity operation on the LHS or RHS.
2417static bool isIdentityMask(SDNode *N, bool RHS = false) {
2418 unsigned NumElems = N->getNumOperands();
2419 for (unsigned i = 0; i < NumElems; ++i)
2420 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2421 return false;
2422 return true;
2423}
2424
Evan Chengb9df0ca2006-03-22 02:53:00 +00002425/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2426/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002427static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002428 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2429
Evan Chengb9df0ca2006-03-22 02:53:00 +00002430 // This is a splat operation if each element of the permute is the same, and
2431 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002432 unsigned NumElems = N->getNumOperands();
2433 SDOperand ElementBase;
2434 unsigned i = 0;
2435 for (; i != NumElems; ++i) {
2436 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002437 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002438 ElementBase = Elt;
2439 break;
2440 }
2441 }
2442
2443 if (!ElementBase.Val)
2444 return false;
2445
2446 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002447 SDOperand Arg = N->getOperand(i);
2448 if (Arg.getOpcode() == ISD::UNDEF) continue;
2449 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002450 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002451 }
2452
2453 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002454 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002455}
2456
Evan Chengc575ca22006-04-17 20:43:08 +00002457/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2458/// a splat of a single element and it's a 2 or 4 element mask.
2459bool X86::isSplatMask(SDNode *N) {
2460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2461
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002462 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002463 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2464 return false;
2465 return ::isSplatMask(N);
2466}
2467
Evan Chengf686d9b2006-10-27 21:08:32 +00002468/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2469/// specifies a splat of zero element.
2470bool X86::isSplatLoMask(SDNode *N) {
2471 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2472
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002473 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002474 if (!isUndefOrEqual(N->getOperand(i), 0))
2475 return false;
2476 return true;
2477}
2478
Evan Cheng63d33002006-03-22 08:01:21 +00002479/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2480/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2481/// instructions.
2482unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002483 unsigned NumOperands = N->getNumOperands();
2484 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2485 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002486 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002487 unsigned Val = 0;
2488 SDOperand Arg = N->getOperand(NumOperands-i-1);
2489 if (Arg.getOpcode() != ISD::UNDEF)
2490 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002491 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002492 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002493 if (i != NumOperands - 1)
2494 Mask <<= Shift;
2495 }
Evan Cheng63d33002006-03-22 08:01:21 +00002496
2497 return Mask;
2498}
2499
Evan Cheng506d3df2006-03-29 23:07:14 +00002500/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2501/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2502/// instructions.
2503unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2504 unsigned Mask = 0;
2505 // 8 nodes, but we only care about the last 4.
2506 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002507 unsigned Val = 0;
2508 SDOperand Arg = N->getOperand(i);
2509 if (Arg.getOpcode() != ISD::UNDEF)
2510 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002511 Mask |= (Val - 4);
2512 if (i != 4)
2513 Mask <<= 2;
2514 }
2515
2516 return Mask;
2517}
2518
2519/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2520/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2521/// instructions.
2522unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2523 unsigned Mask = 0;
2524 // 8 nodes, but we only care about the first 4.
2525 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002526 unsigned Val = 0;
2527 SDOperand Arg = N->getOperand(i);
2528 if (Arg.getOpcode() != ISD::UNDEF)
2529 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002530 Mask |= Val;
2531 if (i != 0)
2532 Mask <<= 2;
2533 }
2534
2535 return Mask;
2536}
2537
Evan Chengc21a0532006-04-05 01:47:37 +00002538/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2539/// specifies a 8 element shuffle that can be broken into a pair of
2540/// PSHUFHW and PSHUFLW.
2541static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2542 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2543
2544 if (N->getNumOperands() != 8)
2545 return false;
2546
2547 // Lower quadword shuffled.
2548 for (unsigned i = 0; i != 4; ++i) {
2549 SDOperand Arg = N->getOperand(i);
2550 if (Arg.getOpcode() == ISD::UNDEF) continue;
2551 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2552 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002553 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002554 return false;
2555 }
2556
2557 // Upper quadword shuffled.
2558 for (unsigned i = 4; i != 8; ++i) {
2559 SDOperand Arg = N->getOperand(i);
2560 if (Arg.getOpcode() == ISD::UNDEF) continue;
2561 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2562 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2563 if (Val < 4 || Val > 7)
2564 return false;
2565 }
2566
2567 return true;
2568}
2569
Chris Lattner8a594482007-11-25 00:24:49 +00002570/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002571/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002572static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2573 SDOperand &V2, SDOperand &Mask,
2574 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002575 MVT VT = Op.getValueType();
2576 MVT MaskVT = Mask.getValueType();
2577 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002578 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002579 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002580
2581 for (unsigned i = 0; i != NumElems; ++i) {
2582 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002583 if (Arg.getOpcode() == ISD::UNDEF) {
2584 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2585 continue;
2586 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002587 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2588 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2589 if (Val < NumElems)
2590 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2591 else
2592 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2593 }
2594
Evan Cheng9eca5e82006-10-25 21:49:50 +00002595 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002596 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002597 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002598}
2599
Evan Cheng779ccea2007-12-07 21:30:01 +00002600/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2601/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002602static
2603SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002604 MVT MaskVT = Mask.getValueType();
2605 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002606 unsigned NumElems = Mask.getNumOperands();
2607 SmallVector<SDOperand, 8> MaskVec;
2608 for (unsigned i = 0; i != NumElems; ++i) {
2609 SDOperand Arg = Mask.getOperand(i);
2610 if (Arg.getOpcode() == ISD::UNDEF) {
2611 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2612 continue;
2613 }
2614 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2615 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2616 if (Val < NumElems)
2617 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2618 else
2619 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2620 }
2621 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2622}
2623
2624
Evan Cheng533a0aa2006-04-19 20:35:22 +00002625/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2626/// match movhlps. The lower half elements should come from upper half of
2627/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002628/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002629static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2630 unsigned NumElems = Mask->getNumOperands();
2631 if (NumElems != 4)
2632 return false;
2633 for (unsigned i = 0, e = 2; i != e; ++i)
2634 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2635 return false;
2636 for (unsigned i = 2; i != 4; ++i)
2637 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2638 return false;
2639 return true;
2640}
2641
Evan Cheng5ced1d82006-04-06 23:23:56 +00002642/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002643/// is promoted to a vector. It also returns the LoadSDNode by reference if
2644/// required.
2645static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng533a0aa2006-04-19 20:35:22 +00002646 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2647 N = N->getOperand(0).Val;
Evan Cheng7e2ff772008-05-08 00:57:18 +00002648 if (ISD::isNON_EXTLoad(N)) {
2649 if (LD)
2650 *LD = cast<LoadSDNode>(N);
2651 return true;
2652 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002653 }
2654 return false;
2655}
2656
Evan Cheng533a0aa2006-04-19 20:35:22 +00002657/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2658/// match movlp{s|d}. The lower half elements should come from lower half of
2659/// V1 (and in order), and the upper half elements should come from the upper
2660/// half of V2 (and in order). And since V1 will become the source of the
2661/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002662static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002663 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002664 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002665 // Is V2 is a vector load, don't do this transformation. We will try to use
2666 // load folding shufps op.
2667 if (ISD::isNON_EXTLoad(V2))
2668 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002669
Evan Cheng533a0aa2006-04-19 20:35:22 +00002670 unsigned NumElems = Mask->getNumOperands();
2671 if (NumElems != 2 && NumElems != 4)
2672 return false;
2673 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2674 if (!isUndefOrEqual(Mask->getOperand(i), i))
2675 return false;
2676 for (unsigned i = NumElems/2; i != NumElems; ++i)
2677 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2678 return false;
2679 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002680}
2681
Evan Cheng39623da2006-04-20 08:58:49 +00002682/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2683/// all the same.
2684static bool isSplatVector(SDNode *N) {
2685 if (N->getOpcode() != ISD::BUILD_VECTOR)
2686 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002687
Evan Cheng39623da2006-04-20 08:58:49 +00002688 SDOperand SplatValue = N->getOperand(0);
2689 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2690 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002691 return false;
2692 return true;
2693}
2694
Evan Cheng8cf723d2006-09-08 01:50:06 +00002695/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2696/// to an undef.
2697static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002698 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002699 return false;
2700
2701 SDOperand V1 = N->getOperand(0);
2702 SDOperand V2 = N->getOperand(1);
2703 SDOperand Mask = N->getOperand(2);
2704 unsigned NumElems = Mask.getNumOperands();
2705 for (unsigned i = 0; i != NumElems; ++i) {
2706 SDOperand Arg = Mask.getOperand(i);
2707 if (Arg.getOpcode() != ISD::UNDEF) {
2708 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2709 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2710 return false;
2711 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2712 return false;
2713 }
2714 }
2715 return true;
2716}
2717
Evan Cheng213d2cf2007-05-17 18:45:50 +00002718/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2719/// constant +0.0.
2720static inline bool isZeroNode(SDOperand Elt) {
2721 return ((isa<ConstantSDNode>(Elt) &&
2722 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2723 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002724 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002725}
2726
2727/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2728/// to an zero vector.
2729static bool isZeroShuffle(SDNode *N) {
2730 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2731 return false;
2732
2733 SDOperand V1 = N->getOperand(0);
2734 SDOperand V2 = N->getOperand(1);
2735 SDOperand Mask = N->getOperand(2);
2736 unsigned NumElems = Mask.getNumOperands();
2737 for (unsigned i = 0; i != NumElems; ++i) {
2738 SDOperand Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002739 if (Arg.getOpcode() == ISD::UNDEF)
2740 continue;
2741
2742 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2743 if (Idx < NumElems) {
2744 unsigned Opc = V1.Val->getOpcode();
2745 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2746 continue;
2747 if (Opc != ISD::BUILD_VECTOR ||
2748 !isZeroNode(V1.Val->getOperand(Idx)))
2749 return false;
2750 } else if (Idx >= NumElems) {
2751 unsigned Opc = V2.Val->getOpcode();
2752 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2753 continue;
2754 if (Opc != ISD::BUILD_VECTOR ||
2755 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2756 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002757 }
2758 }
2759 return true;
2760}
2761
2762/// getZeroVector - Returns a vector of specified type with all zero elements.
2763///
Duncan Sands83ec4b62008-06-06 12:08:01 +00002764static SDOperand getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
2765 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002766
2767 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2768 // type. This ensures they get CSE'd.
Chris Lattner8a594482007-11-25 00:24:49 +00002769 SDOperand Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002770 if (VT.getSizeInBits() == 64) { // MMX
Evan Chengf0df0312008-05-15 08:39:06 +00002771 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002772 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002773 } else if (HasSSE2) { // SSE2
2774 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002775 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002776 } else { // SSE1
2777 SDOperand Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
2778 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2779 }
Chris Lattner8a594482007-11-25 00:24:49 +00002780 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002781}
2782
Chris Lattner8a594482007-11-25 00:24:49 +00002783/// getOnesVector - Returns a vector of specified type with all bits set.
2784///
Duncan Sands83ec4b62008-06-06 12:08:01 +00002785static SDOperand getOnesVector(MVT VT, SelectionDAG &DAG) {
2786 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002787
2788 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2789 // type. This ensures they get CSE'd.
2790 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2791 SDOperand Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002792 if (VT.getSizeInBits() == 64) // MMX
Chris Lattner8a594482007-11-25 00:24:49 +00002793 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2794 else // SSE
2795 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2796 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2797}
2798
2799
Evan Cheng39623da2006-04-20 08:58:49 +00002800/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2801/// that point to V2 points to its first element.
2802static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2803 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2804
2805 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002806 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002807 unsigned NumElems = Mask.getNumOperands();
2808 for (unsigned i = 0; i != NumElems; ++i) {
2809 SDOperand Arg = Mask.getOperand(i);
2810 if (Arg.getOpcode() != ISD::UNDEF) {
2811 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2812 if (Val > NumElems) {
2813 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2814 Changed = true;
2815 }
2816 }
2817 MaskVec.push_back(Arg);
2818 }
2819
2820 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002821 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2822 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002823 return Mask;
2824}
2825
Evan Cheng017dcc62006-04-21 01:05:10 +00002826/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2827/// operation of specified width.
2828static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002829 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2830 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002831
Chris Lattner5a88b832007-02-25 07:10:00 +00002832 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002833 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2834 for (unsigned i = 1; i != NumElems; ++i)
2835 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002836 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002837}
2838
Evan Chengc575ca22006-04-17 20:43:08 +00002839/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2840/// of specified width.
2841static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002842 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2843 MVT BaseVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00002844 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002845 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2846 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2847 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2848 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002849 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002850}
2851
Evan Cheng39623da2006-04-20 08:58:49 +00002852/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2853/// of specified width.
2854static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002855 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2856 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002857 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002858 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002859 for (unsigned i = 0; i != Half; ++i) {
2860 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2861 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2862 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002863 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002864}
2865
Chris Lattner62098042008-03-09 01:05:04 +00002866/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2867/// element #0 of a vector with the specified index, leaving the rest of the
2868/// elements in place.
2869static SDOperand getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
2870 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002871 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2872 MVT BaseVT = MaskVT.getVectorElementType();
Chris Lattner62098042008-03-09 01:05:04 +00002873 SmallVector<SDOperand, 8> MaskVec;
2874 // Element #0 of the result gets the elt we are replacing.
2875 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2876 for (unsigned i = 1; i != NumElems; ++i)
2877 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2878 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2879}
2880
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002881/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
2882static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002883 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2884 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002885 if (PVT == VT)
2886 return Op;
Evan Chengc575ca22006-04-17 20:43:08 +00002887 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002888 SDOperand Mask = Op.getOperand(2);
Evan Cheng017dcc62006-04-21 01:05:10 +00002889 unsigned NumElems = Mask.getNumOperands();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002890 // Special handling of v4f32 -> v4i32.
2891 if (VT != MVT::v4f32) {
2892 Mask = getUnpacklMask(NumElems, DAG);
2893 while (NumElems > 4) {
2894 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2895 NumElems >>= 1;
2896 }
Evan Chengf0df0312008-05-15 08:39:06 +00002897 Mask = getZeroVector(MVT::v4i32, true, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002898 }
Evan Chengc575ca22006-04-17 20:43:08 +00002899
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002900 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
2901 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
2902 DAG.getNode(ISD::UNDEF, PVT), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002903 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2904}
2905
Evan Chengba05f722006-04-21 23:03:30 +00002906/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002907/// vector of zero or undef vector. This produces a shuffle where the low
2908/// element of V2 is swizzled into the zero/undef vector, landing at element
2909/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Chris Lattner62098042008-03-09 01:05:04 +00002910static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002911 bool isZero, bool HasSSE2,
2912 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002913 MVT VT = V2.getValueType();
Evan Chengf0df0312008-05-15 08:39:06 +00002914 SDOperand V1 = isZero
2915 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002916 unsigned NumElems = V2.getValueType().getVectorNumElements();
2917 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2918 MVT EVT = MaskVT.getVectorElementType();
Chris Lattner8a594482007-11-25 00:24:49 +00002919 SmallVector<SDOperand, 16> MaskVec;
2920 for (unsigned i = 0; i != NumElems; ++i)
2921 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2922 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2923 else
2924 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002925 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2926 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002927 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002928}
2929
Evan Chengf26ffe92008-05-29 08:22:04 +00002930/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2931/// a shuffle that is zero.
2932static
2933unsigned getNumOfConsecutiveZeros(SDOperand Op, SDOperand Mask,
2934 unsigned NumElems, bool Low,
2935 SelectionDAG &DAG) {
2936 unsigned NumZeros = 0;
2937 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002938 unsigned Index = Low ? i : NumElems-i-1;
2939 SDOperand Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00002940 if (Idx.getOpcode() == ISD::UNDEF) {
2941 ++NumZeros;
2942 continue;
2943 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002944 SDOperand Elt = DAG.getShuffleScalarElt(Op.Val, Index);
2945 if (Elt.Val && isZeroNode(Elt))
2946 ++NumZeros;
2947 else
2948 break;
2949 }
2950 return NumZeros;
2951}
2952
2953/// isVectorShift - Returns true if the shuffle can be implemented as a
2954/// logical left or right shift of a vector.
2955static bool isVectorShift(SDOperand Op, SDOperand Mask, SelectionDAG &DAG,
2956 bool &isLeft, SDOperand &ShVal, unsigned &ShAmt) {
2957 unsigned NumElems = Mask.getNumOperands();
2958
2959 isLeft = true;
2960 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2961 if (!NumZeros) {
2962 isLeft = false;
2963 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2964 if (!NumZeros)
2965 return false;
2966 }
2967
2968 bool SeenV1 = false;
2969 bool SeenV2 = false;
2970 for (unsigned i = NumZeros; i < NumElems; ++i) {
2971 unsigned Val = isLeft ? (i - NumZeros) : i;
2972 SDOperand Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
2973 if (Idx.getOpcode() == ISD::UNDEF)
2974 continue;
2975 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2976 if (Index < NumElems)
2977 SeenV1 = true;
2978 else {
2979 Index -= NumElems;
2980 SeenV2 = true;
2981 }
2982 if (Index != Val)
2983 return false;
2984 }
2985 if (SeenV1 && SeenV2)
2986 return false;
2987
2988 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
2989 ShAmt = NumZeros;
2990 return true;
2991}
2992
2993
Evan Chengc78d3b42006-04-24 18:01:45 +00002994/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2995///
2996static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2997 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002998 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002999 if (NumNonZero > 8)
3000 return SDOperand();
3001
3002 SDOperand V(0, 0);
3003 bool First = true;
3004 for (unsigned i = 0; i < 16; ++i) {
3005 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3006 if (ThisIsNonZero && First) {
3007 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003008 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003009 else
3010 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3011 First = false;
3012 }
3013
3014 if ((i & 1) != 0) {
3015 SDOperand ThisElt(0, 0), LastElt(0, 0);
3016 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3017 if (LastIsNonZero) {
3018 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3019 }
3020 if (ThisIsNonZero) {
3021 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3022 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3023 ThisElt, DAG.getConstant(8, MVT::i8));
3024 if (LastIsNonZero)
3025 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3026 } else
3027 ThisElt = LastElt;
3028
3029 if (ThisElt.Val)
3030 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003031 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003032 }
3033 }
3034
3035 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3036}
3037
Bill Wendlinga348c562007-03-22 18:42:45 +00003038/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003039///
3040static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
3041 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003042 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003043 if (NumNonZero > 4)
3044 return SDOperand();
3045
3046 SDOperand V(0, 0);
3047 bool First = true;
3048 for (unsigned i = 0; i < 8; ++i) {
3049 bool isNonZero = (NonZeros & (1 << i)) != 0;
3050 if (isNonZero) {
3051 if (First) {
3052 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003053 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003054 else
3055 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3056 First = false;
3057 }
3058 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003059 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003060 }
3061 }
3062
3063 return V;
3064}
3065
Evan Chengf26ffe92008-05-29 08:22:04 +00003066/// getVShift - Return a vector logical shift node.
3067///
Duncan Sands83ec4b62008-06-06 12:08:01 +00003068static SDOperand getVShift(bool isLeft, MVT VT, SDOperand SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003069 unsigned NumBits, SelectionDAG &DAG,
3070 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003071 bool isMMX = VT.getSizeInBits() == 64;
3072 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003073 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3074 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3075 return DAG.getNode(ISD::BIT_CONVERT, VT,
3076 DAG.getNode(Opc, ShVT, SrcOp,
3077 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3078}
3079
Evan Cheng0db9fe62006-04-25 20:13:52 +00003080SDOperand
3081X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003082 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3083 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3084 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3085 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3086 // eliminated on x86-32 hosts.
3087 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3088 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003089
Chris Lattner8a594482007-11-25 00:24:49 +00003090 if (ISD::isBuildVectorAllOnes(Op.Val))
3091 return getOnesVector(Op.getValueType(), DAG);
Evan Chengf0df0312008-05-15 08:39:06 +00003092 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattner8a594482007-11-25 00:24:49 +00003093 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003094
Duncan Sands83ec4b62008-06-06 12:08:01 +00003095 MVT VT = Op.getValueType();
3096 MVT EVT = VT.getVectorElementType();
3097 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003098
3099 unsigned NumElems = Op.getNumOperands();
3100 unsigned NumZero = 0;
3101 unsigned NumNonZero = 0;
3102 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003103 bool IsAllConstants = true;
Evan Cheng14b32e12007-12-11 01:46:18 +00003104 SmallSet<SDOperand, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003105 for (unsigned i = 0; i < NumElems; ++i) {
3106 SDOperand Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003107 if (Elt.getOpcode() == ISD::UNDEF)
3108 continue;
3109 Values.insert(Elt);
3110 if (Elt.getOpcode() != ISD::Constant &&
3111 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003112 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003113 if (isZeroNode(Elt))
3114 NumZero++;
3115 else {
3116 NonZeros |= (1 << i);
3117 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003118 }
3119 }
3120
Dan Gohman7f321562007-06-25 16:23:39 +00003121 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003122 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3123 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003124 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003125
Chris Lattner67f453a2008-03-09 05:42:06 +00003126 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003127 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003128 unsigned Idx = CountTrailingZeros_32(NonZeros);
3129 SDOperand Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003130
Chris Lattner62098042008-03-09 01:05:04 +00003131 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3132 // the value are obviously zero, truncate the value to i32 and do the
3133 // insertion that way. Only do this if the value is non-constant or if the
3134 // value is a constant being inserted into element 0. It is cheaper to do
3135 // a constant pool load than it is to do a movd + shuffle.
3136 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3137 (!IsAllConstants || Idx == 0)) {
3138 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3139 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003140 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3141 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003142
3143 // Truncate the value (which may itself be a constant) to i32, and
3144 // convert it to a vector with movd (S2V+shuffle to zero extend).
3145 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3146 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003147 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3148 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003149
3150 // Now we have our 32-bit value zero extended in the low element of
3151 // a vector. If Idx != 0, swizzle it into place.
3152 if (Idx != 0) {
3153 SDOperand Ops[] = {
3154 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3155 getSwapEltZeroMask(VecElts, Idx, DAG)
3156 };
3157 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3158 }
3159 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3160 }
3161 }
3162
Chris Lattner19f79692008-03-08 22:59:52 +00003163 // If we have a constant or non-constant insertion into the low element of
3164 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3165 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3166 // depending on what the source datatype is. Because we can only get here
3167 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3168 if (Idx == 0 &&
3169 // Don't do this for i64 values on x86-32.
3170 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003171 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003172 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003173 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3174 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003175 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003176
3177 // Is it a vector logical left shift?
3178 if (NumElems == 2 && Idx == 1 &&
3179 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003180 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003181 return getVShift(true, VT,
3182 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3183 NumBits/2, DAG, *this);
3184 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003185
3186 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Evan Chengdb2d5242007-12-12 06:45:40 +00003187 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003188
Chris Lattner19f79692008-03-08 22:59:52 +00003189 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3190 // is a non-constant being inserted into an element other than the low one,
3191 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3192 // movd/movss) to move this into the low element, then shuffle it into
3193 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003194 if (EVTBits == 32) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003195 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3196
Evan Cheng0db9fe62006-04-25 20:13:52 +00003197 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003198 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3199 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003200 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3201 MVT MaskEVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00003202 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003203 for (unsigned i = 0; i < NumElems; i++)
3204 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003205 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3206 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003207 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3208 DAG.getNode(ISD::UNDEF, VT), Mask);
3209 }
3210 }
3211
Chris Lattner67f453a2008-03-09 05:42:06 +00003212 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3213 if (Values.size() == 1)
3214 return SDOperand();
3215
Dan Gohmana3941172007-07-24 22:55:08 +00003216 // A vector full of immediates; various special cases are already
3217 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003218 if (IsAllConstants)
Dan Gohmana3941172007-07-24 22:55:08 +00003219 return SDOperand();
3220
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003221 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003222 if (EVTBits == 64) {
3223 if (NumNonZero == 1) {
3224 // One half is zero or undef.
3225 unsigned Idx = CountTrailingZeros_32(NonZeros);
3226 SDOperand V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
3227 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003228 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3229 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003230 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003231 return SDOperand();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003232 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003233
3234 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003235 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003236 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
3237 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003238 if (V.Val) return V;
3239 }
3240
Bill Wendling826f36f2007-03-28 00:57:11 +00003241 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00003242 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
3243 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003244 if (V.Val) return V;
3245 }
3246
3247 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00003248 SmallVector<SDOperand, 8> V;
3249 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003250 if (NumElems == 4 && NumZero > 0) {
3251 for (unsigned i = 0; i < 4; ++i) {
3252 bool isZero = !(NonZeros & (1 << i));
3253 if (isZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003254 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003255 else
3256 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3257 }
3258
3259 for (unsigned i = 0; i < 2; ++i) {
3260 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3261 default: break;
3262 case 0:
3263 V[i] = V[i*2]; // Must be a zero vector.
3264 break;
3265 case 1:
3266 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3267 getMOVLMask(NumElems, DAG));
3268 break;
3269 case 2:
3270 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3271 getMOVLMask(NumElems, DAG));
3272 break;
3273 case 3:
3274 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3275 getUnpacklMask(NumElems, DAG));
3276 break;
3277 }
3278 }
3279
Duncan Sands83ec4b62008-06-06 12:08:01 +00003280 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3281 MVT EVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00003282 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003283 bool Reverse = (NonZeros & 0x3) == 2;
3284 for (unsigned i = 0; i < 2; ++i)
3285 if (Reverse)
3286 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3287 else
3288 MaskVec.push_back(DAG.getConstant(i, EVT));
3289 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3290 for (unsigned i = 0; i < 2; ++i)
3291 if (Reverse)
3292 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3293 else
3294 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003295 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3296 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003297 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3298 }
3299
3300 if (Values.size() > 2) {
3301 // Expand into a number of unpckl*.
3302 // e.g. for v4f32
3303 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3304 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3305 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3306 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3307 for (unsigned i = 0; i < NumElems; ++i)
3308 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3309 NumElems >>= 1;
3310 while (NumElems != 0) {
3311 for (unsigned i = 0; i < NumElems; ++i)
3312 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3313 UnpckMask);
3314 NumElems >>= 1;
3315 }
3316 return V[0];
3317 }
3318
3319 return SDOperand();
3320}
3321
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003322static
3323SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3324 SDOperand PermMask, SelectionDAG &DAG,
3325 TargetLowering &TLI) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003326 SDOperand NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003327 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3328 MVT MaskEVT = MaskVT.getVectorElementType();
3329 MVT PtrVT = TLI.getPointerTy();
Evan Cheng14b32e12007-12-11 01:46:18 +00003330 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3331 PermMask.Val->op_end());
3332
3333 // First record which half of which vector the low elements come from.
3334 SmallVector<unsigned, 4> LowQuad(4);
3335 for (unsigned i = 0; i < 4; ++i) {
3336 SDOperand Elt = MaskElts[i];
3337 if (Elt.getOpcode() == ISD::UNDEF)
3338 continue;
3339 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3340 int QuadIdx = EltIdx / 4;
3341 ++LowQuad[QuadIdx];
3342 }
3343 int BestLowQuad = -1;
3344 unsigned MaxQuad = 1;
3345 for (unsigned i = 0; i < 4; ++i) {
3346 if (LowQuad[i] > MaxQuad) {
3347 BestLowQuad = i;
3348 MaxQuad = LowQuad[i];
3349 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003350 }
3351
Evan Cheng14b32e12007-12-11 01:46:18 +00003352 // Record which half of which vector the high elements come from.
3353 SmallVector<unsigned, 4> HighQuad(4);
3354 for (unsigned i = 4; i < 8; ++i) {
3355 SDOperand Elt = MaskElts[i];
3356 if (Elt.getOpcode() == ISD::UNDEF)
3357 continue;
3358 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3359 int QuadIdx = EltIdx / 4;
3360 ++HighQuad[QuadIdx];
3361 }
3362 int BestHighQuad = -1;
3363 MaxQuad = 1;
3364 for (unsigned i = 0; i < 4; ++i) {
3365 if (HighQuad[i] > MaxQuad) {
3366 BestHighQuad = i;
3367 MaxQuad = HighQuad[i];
3368 }
3369 }
3370
3371 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3372 if (BestLowQuad != -1 || BestHighQuad != -1) {
3373 // First sort the 4 chunks in order using shufpd.
3374 SmallVector<SDOperand, 8> MaskVec;
3375 if (BestLowQuad != -1)
3376 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3377 else
3378 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3379 if (BestHighQuad != -1)
3380 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3381 else
3382 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3383 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3384 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3385 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3386 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3387 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3388
3389 // Now sort high and low parts separately.
3390 BitVector InOrder(8);
3391 if (BestLowQuad != -1) {
3392 // Sort lower half in order using PSHUFLW.
3393 MaskVec.clear();
3394 bool AnyOutOrder = false;
3395 for (unsigned i = 0; i != 4; ++i) {
3396 SDOperand Elt = MaskElts[i];
3397 if (Elt.getOpcode() == ISD::UNDEF) {
3398 MaskVec.push_back(Elt);
3399 InOrder.set(i);
3400 } else {
3401 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3402 if (EltIdx != i)
3403 AnyOutOrder = true;
3404 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3405 // If this element is in the right place after this shuffle, then
3406 // remember it.
3407 if ((int)(EltIdx / 4) == BestLowQuad)
3408 InOrder.set(i);
3409 }
3410 }
3411 if (AnyOutOrder) {
3412 for (unsigned i = 4; i != 8; ++i)
3413 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3414 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3415 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3416 }
3417 }
3418
3419 if (BestHighQuad != -1) {
3420 // Sort high half in order using PSHUFHW if possible.
3421 MaskVec.clear();
3422 for (unsigned i = 0; i != 4; ++i)
3423 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3424 bool AnyOutOrder = false;
3425 for (unsigned i = 4; i != 8; ++i) {
3426 SDOperand Elt = MaskElts[i];
3427 if (Elt.getOpcode() == ISD::UNDEF) {
3428 MaskVec.push_back(Elt);
3429 InOrder.set(i);
3430 } else {
3431 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3432 if (EltIdx != i)
3433 AnyOutOrder = true;
3434 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3435 // If this element is in the right place after this shuffle, then
3436 // remember it.
3437 if ((int)(EltIdx / 4) == BestHighQuad)
3438 InOrder.set(i);
3439 }
3440 }
3441 if (AnyOutOrder) {
3442 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3443 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3444 }
3445 }
3446
3447 // The other elements are put in the right place using pextrw and pinsrw.
3448 for (unsigned i = 0; i != 8; ++i) {
3449 if (InOrder[i])
3450 continue;
3451 SDOperand Elt = MaskElts[i];
3452 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003453 SDOperand ExtOp = (EltIdx < 8)
3454 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3455 DAG.getConstant(EltIdx, PtrVT))
3456 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3457 DAG.getConstant(EltIdx - 8, PtrVT));
3458 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3459 DAG.getConstant(i, PtrVT));
3460 }
3461 return NewV;
3462 }
3463
3464 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3465 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003466 // First, let's find out how many elements are already in the right order.
3467 unsigned V1InOrder = 0;
3468 unsigned V1FromV1 = 0;
3469 unsigned V2InOrder = 0;
3470 unsigned V2FromV2 = 0;
Evan Cheng14b32e12007-12-11 01:46:18 +00003471 SmallVector<SDOperand, 8> V1Elts;
3472 SmallVector<SDOperand, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003473 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003474 SDOperand Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003475 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003476 V1Elts.push_back(Elt);
3477 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003478 ++V1InOrder;
3479 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003480 continue;
3481 }
3482 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3483 if (EltIdx == i) {
3484 V1Elts.push_back(Elt);
3485 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3486 ++V1InOrder;
3487 } else if (EltIdx == i+8) {
3488 V1Elts.push_back(Elt);
3489 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3490 ++V2InOrder;
3491 } else if (EltIdx < 8) {
3492 V1Elts.push_back(Elt);
3493 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003494 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003495 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3496 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003497 }
3498 }
3499
3500 if (V2InOrder > V1InOrder) {
3501 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3502 std::swap(V1, V2);
3503 std::swap(V1Elts, V2Elts);
3504 std::swap(V1FromV1, V2FromV2);
3505 }
3506
Evan Cheng14b32e12007-12-11 01:46:18 +00003507 if ((V1FromV1 + V1InOrder) != 8) {
3508 // Some elements are from V2.
3509 if (V1FromV1) {
3510 // If there are elements that are from V1 but out of place,
3511 // then first sort them in place
3512 SmallVector<SDOperand, 8> MaskVec;
3513 for (unsigned i = 0; i < 8; ++i) {
3514 SDOperand Elt = V1Elts[i];
3515 if (Elt.getOpcode() == ISD::UNDEF) {
3516 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3517 continue;
3518 }
3519 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3520 if (EltIdx >= 8)
3521 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3522 else
3523 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3524 }
3525 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3526 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003527 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003528
3529 NewV = V1;
3530 for (unsigned i = 0; i < 8; ++i) {
3531 SDOperand Elt = V1Elts[i];
3532 if (Elt.getOpcode() == ISD::UNDEF)
3533 continue;
3534 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3535 if (EltIdx < 8)
3536 continue;
3537 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3538 DAG.getConstant(EltIdx - 8, PtrVT));
3539 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3540 DAG.getConstant(i, PtrVT));
3541 }
3542 return NewV;
3543 } else {
3544 // All elements are from V1.
3545 NewV = V1;
3546 for (unsigned i = 0; i < 8; ++i) {
3547 SDOperand Elt = V1Elts[i];
3548 if (Elt.getOpcode() == ISD::UNDEF)
3549 continue;
3550 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3551 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3552 DAG.getConstant(EltIdx, PtrVT));
3553 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3554 DAG.getConstant(i, PtrVT));
3555 }
3556 return NewV;
3557 }
3558}
3559
Evan Cheng7a831ce2007-12-15 03:00:47 +00003560/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3561/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3562/// done when every pair / quad of shuffle mask elements point to elements in
3563/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003564/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3565static
Evan Cheng7a831ce2007-12-15 03:00:47 +00003566SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003567 MVT VT,
Evan Cheng14b32e12007-12-11 01:46:18 +00003568 SDOperand PermMask, SelectionDAG &DAG,
3569 TargetLowering &TLI) {
3570 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003571 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003572 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3573 MVT NewVT = MaskVT;
3574 switch (VT.getSimpleVT()) {
3575 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003576 case MVT::v4f32: NewVT = MVT::v2f64; break;
3577 case MVT::v4i32: NewVT = MVT::v2i64; break;
3578 case MVT::v8i16: NewVT = MVT::v4i32; break;
3579 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003580 }
3581
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003582 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003583 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003584 NewVT = MVT::v2i64;
3585 else
3586 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003587 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003588 unsigned Scale = NumElems / NewWidth;
3589 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003590 for (unsigned i = 0; i < NumElems; i += Scale) {
3591 unsigned StartIdx = ~0U;
3592 for (unsigned j = 0; j < Scale; ++j) {
3593 SDOperand Elt = PermMask.getOperand(i+j);
3594 if (Elt.getOpcode() == ISD::UNDEF)
3595 continue;
3596 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3597 if (StartIdx == ~0U)
3598 StartIdx = EltIdx - (EltIdx % Scale);
3599 if (EltIdx != StartIdx + j)
3600 return SDOperand();
3601 }
3602 if (StartIdx == ~0U)
3603 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3604 else
3605 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003606 }
3607
Evan Cheng7a831ce2007-12-15 03:00:47 +00003608 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3609 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3610 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3611 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3612 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003613}
3614
Evan Chengd880b972008-05-09 21:53:03 +00003615/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003616///
Duncan Sands83ec4b62008-06-06 12:08:01 +00003617static SDOperand getVZextMovL(MVT VT, MVT OpVT,
3618 SDOperand SrcOp, SelectionDAG &DAG,
3619 const X86Subtarget *Subtarget) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003620 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3621 LoadSDNode *LD = NULL;
3622 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3623 LD = dyn_cast<LoadSDNode>(SrcOp);
3624 if (!LD) {
3625 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3626 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003627 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003628 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3629 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3630 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3631 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3632 // PR2108
3633 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3634 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003635 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003636 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3637 SrcOp.getOperand(0).getOperand(0))));
3638 }
3639 }
3640 }
3641
3642 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003643 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003644 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3645}
3646
Evan Cheng0db9fe62006-04-25 20:13:52 +00003647SDOperand
3648X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3649 SDOperand V1 = Op.getOperand(0);
3650 SDOperand V2 = Op.getOperand(1);
3651 SDOperand PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003652 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003653 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003654 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003655 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3656 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003657 bool V1IsSplat = false;
3658 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003659
Evan Cheng8cf723d2006-09-08 01:50:06 +00003660 if (isUndefShuffle(Op.Val))
3661 return DAG.getNode(ISD::UNDEF, VT);
3662
Evan Cheng213d2cf2007-05-17 18:45:50 +00003663 if (isZeroShuffle(Op.Val))
Evan Chengf0df0312008-05-15 08:39:06 +00003664 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003665
Evan Cheng49892af2007-06-19 00:02:56 +00003666 if (isIdentityMask(PermMask.Val))
3667 return V1;
3668 else if (isIdentityMask(PermMask.Val, true))
3669 return V2;
3670
Evan Cheng0db9fe62006-04-25 20:13:52 +00003671 if (isSplatMask(PermMask.Val)) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003672 if (isMMX || NumElems < 4) return Op;
3673 // Promote it to a v4{if}32 splat.
3674 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003675 }
3676
Evan Cheng7a831ce2007-12-15 03:00:47 +00003677 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3678 // do it!
3679 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3680 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3681 if (NewOp.Val)
3682 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3683 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3684 // FIXME: Figure out a cleaner way to do this.
3685 // Try to make use of movq to zero out the top part.
3686 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003687 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3688 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003689 if (NewOp.Val) {
3690 SDOperand NewV1 = NewOp.getOperand(0);
3691 SDOperand NewV2 = NewOp.getOperand(1);
3692 SDOperand NewMask = NewOp.getOperand(2);
3693 if (isCommutedMOVL(NewMask.Val, true, false)) {
3694 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chengd880b972008-05-09 21:53:03 +00003695 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003696 }
3697 }
3698 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003699 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
3700 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003701 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chengd880b972008-05-09 21:53:03 +00003702 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng7e2ff772008-05-08 00:57:18 +00003703 DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003704 }
3705 }
3706
Evan Chengf26ffe92008-05-29 08:22:04 +00003707 // Check if this can be converted into a logical shift.
3708 bool isLeft = false;
3709 unsigned ShAmt = 0;
3710 SDOperand ShVal;
3711 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3712 if (isShift && ShVal.hasOneUse()) {
3713 // If the shifted value has multiple uses, it may be cheaper to use
3714 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003715 MVT EVT = VT.getVectorElementType();
3716 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003717 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3718 }
3719
Evan Cheng7e2ff772008-05-08 00:57:18 +00003720 if (X86::isMOVLMask(PermMask.Val)) {
3721 if (V1IsUndef)
3722 return V2;
3723 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chengd880b972008-05-09 21:53:03 +00003724 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003725 return Op;
3726 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003727
Evan Cheng9bbbb982006-10-25 20:48:19 +00003728 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3729 X86::isMOVSLDUPMask(PermMask.Val) ||
3730 X86::isMOVHLPSMask(PermMask.Val) ||
3731 X86::isMOVHPMask(PermMask.Val) ||
3732 X86::isMOVLPMask(PermMask.Val))
3733 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003734
Evan Cheng9bbbb982006-10-25 20:48:19 +00003735 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3736 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003737 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003738
Evan Chengf26ffe92008-05-29 08:22:04 +00003739 if (isShift) {
3740 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003741 MVT EVT = VT.getVectorElementType();
3742 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003743 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3744 }
3745
Evan Cheng9eca5e82006-10-25 21:49:50 +00003746 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003747 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3748 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003749 V1IsSplat = isSplatVector(V1.Val);
3750 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003751
3752 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003753 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003754 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003755 std::swap(V1IsSplat, V2IsSplat);
3756 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003757 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003758 }
3759
Evan Cheng7a831ce2007-12-15 03:00:47 +00003760 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003761 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3762 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003763 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003764 if (V2IsSplat) {
3765 // V2 is a splat, so the mask may be malformed. That is, it may point
3766 // to any V2 element. The instruction selectior won't like this. Get
3767 // a corrected mask and commute to form a proper MOVS{S|D}.
3768 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3769 if (NewMask.Val != PermMask.Val)
3770 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003771 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003772 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003773 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003774
Evan Chengd9b8e402006-10-16 06:36:00 +00003775 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003776 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003777 X86::isUNPCKLMask(PermMask.Val) ||
3778 X86::isUNPCKHMask(PermMask.Val))
3779 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003780
Evan Cheng9bbbb982006-10-25 20:48:19 +00003781 if (V2IsSplat) {
3782 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003783 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003784 // new vector_shuffle with the corrected mask.
3785 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3786 if (NewMask.Val != PermMask.Val) {
3787 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3788 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3789 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3790 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3791 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3792 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003793 }
3794 }
3795 }
3796
3797 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003798 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3799 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3800
3801 if (Commuted) {
3802 // Commute is back and try unpck* again.
3803 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3804 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003805 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003806 X86::isUNPCKLMask(PermMask.Val) ||
3807 X86::isUNPCKHMask(PermMask.Val))
3808 return Op;
3809 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003810
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003811 // Try PSHUF* first, then SHUFP*.
3812 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3813 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3814 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3815 if (V2.getOpcode() != ISD::UNDEF)
3816 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3817 DAG.getNode(ISD::UNDEF, VT), PermMask);
3818 return Op;
3819 }
3820
3821 if (!isMMX) {
3822 if (Subtarget->hasSSE2() &&
3823 (X86::isPSHUFDMask(PermMask.Val) ||
3824 X86::isPSHUFHWMask(PermMask.Val) ||
3825 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003826 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003827 if (VT == MVT::v4f32) {
3828 RVT = MVT::v4i32;
3829 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
3830 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
3831 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3832 } else if (V2.getOpcode() != ISD::UNDEF)
3833 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
3834 DAG.getNode(ISD::UNDEF, RVT), PermMask);
3835 if (RVT != VT)
3836 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003837 return Op;
3838 }
3839
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003840 // Binary or unary shufps.
3841 if (X86::isSHUFPMask(PermMask.Val) ||
3842 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Evan Cheng0db9fe62006-04-25 20:13:52 +00003843 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003844 }
3845
Evan Cheng14b32e12007-12-11 01:46:18 +00003846 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3847 if (VT == MVT::v8i16) {
3848 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3849 if (NewOp.Val)
3850 return NewOp;
3851 }
3852
3853 // Handle all 4 wide cases with a number of shuffles.
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003854 if (NumElems == 4 && !isMMX) {
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003855 // Don't do this for MMX.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003856 MVT MaskVT = PermMask.getValueType();
3857 MVT MaskEVT = MaskVT.getVectorElementType();
Chris Lattner5a88b832007-02-25 07:10:00 +00003858 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003859 Locs.reserve(NumElems);
Evan Cheng14b32e12007-12-11 01:46:18 +00003860 SmallVector<SDOperand, 8> Mask1(NumElems,
3861 DAG.getNode(ISD::UNDEF, MaskEVT));
3862 SmallVector<SDOperand, 8> Mask2(NumElems,
3863 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003864 unsigned NumHi = 0;
3865 unsigned NumLo = 0;
3866 // If no more than two elements come from either vector. This can be
3867 // implemented with two shuffles. First shuffle gather the elements.
3868 // The second shuffle, which takes the first shuffle as both of its
3869 // vector operands, put the elements into the right order.
3870 for (unsigned i = 0; i != NumElems; ++i) {
3871 SDOperand Elt = PermMask.getOperand(i);
3872 if (Elt.getOpcode() == ISD::UNDEF) {
3873 Locs[i] = std::make_pair(-1, -1);
3874 } else {
3875 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3876 if (Val < NumElems) {
3877 Locs[i] = std::make_pair(0, NumLo);
3878 Mask1[NumLo] = Elt;
3879 NumLo++;
3880 } else {
3881 Locs[i] = std::make_pair(1, NumHi);
3882 if (2+NumHi < NumElems)
3883 Mask1[2+NumHi] = Elt;
3884 NumHi++;
3885 }
3886 }
3887 }
3888 if (NumLo <= 2 && NumHi <= 2) {
3889 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003890 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3891 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003892 for (unsigned i = 0; i != NumElems; ++i) {
3893 if (Locs[i].first == -1)
3894 continue;
3895 else {
3896 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3897 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3898 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3899 }
3900 }
3901
3902 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003903 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3904 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003905 }
3906
3907 // Break it into (shuffle shuffle_hi, shuffle_lo).
3908 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003909 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3910 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3911 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003912 unsigned MaskIdx = 0;
3913 unsigned LoIdx = 0;
3914 unsigned HiIdx = NumElems/2;
3915 for (unsigned i = 0; i != NumElems; ++i) {
3916 if (i == NumElems/2) {
3917 MaskPtr = &HiMask;
3918 MaskIdx = 1;
3919 LoIdx = 0;
3920 HiIdx = NumElems/2;
3921 }
3922 SDOperand Elt = PermMask.getOperand(i);
3923 if (Elt.getOpcode() == ISD::UNDEF) {
3924 Locs[i] = std::make_pair(-1, -1);
3925 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3926 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3927 (*MaskPtr)[LoIdx] = Elt;
3928 LoIdx++;
3929 } else {
3930 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3931 (*MaskPtr)[HiIdx] = Elt;
3932 HiIdx++;
3933 }
3934 }
3935
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003936 SDOperand LoShuffle =
3937 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003938 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3939 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003940 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003941 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003942 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3943 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003944 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003945 for (unsigned i = 0; i != NumElems; ++i) {
3946 if (Locs[i].first == -1) {
3947 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3948 } else {
3949 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3950 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3951 }
3952 }
3953 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003954 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3955 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003956 }
3957
3958 return SDOperand();
3959}
3960
3961SDOperand
Nate Begeman14d12ca2008-02-11 04:19:36 +00003962X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDOperand Op,
3963 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003964 MVT VT = Op.getValueType();
3965 if (VT.getSizeInBits() == 8) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00003966 SDOperand Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
3967 Op.getOperand(0), Op.getOperand(1));
3968 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3969 DAG.getValueType(VT));
3970 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003971 } else if (VT.getSizeInBits() == 16) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00003972 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
3973 Op.getOperand(0), Op.getOperand(1));
3974 SDOperand Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
3975 DAG.getValueType(VT));
3976 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00003977 } else if (VT == MVT::f32) {
3978 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
3979 // the result back to FR32 register. It's only worth matching if the
Dan Gohman171c11e2008-04-16 02:32:24 +00003980 // result has a single use which is a store or a bitcast to i32.
Evan Cheng62a3f152008-03-24 21:52:23 +00003981 if (!Op.hasOneUse())
3982 return SDOperand();
Roman Levensteindc1adac2008-04-07 10:06:32 +00003983 SDNode *User = Op.Val->use_begin()->getUser();
Dan Gohman171c11e2008-04-16 02:32:24 +00003984 if (User->getOpcode() != ISD::STORE &&
3985 (User->getOpcode() != ISD::BIT_CONVERT ||
3986 User->getValueType(0) != MVT::i32))
Evan Cheng62a3f152008-03-24 21:52:23 +00003987 return SDOperand();
3988 SDOperand Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3989 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
3990 Op.getOperand(1));
3991 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begeman14d12ca2008-02-11 04:19:36 +00003992 }
3993 return SDOperand();
3994}
3995
3996
3997SDOperand
Evan Cheng0db9fe62006-04-25 20:13:52 +00003998X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3999 if (!isa<ConstantSDNode>(Op.getOperand(1)))
4000 return SDOperand();
4001
Evan Cheng62a3f152008-03-24 21:52:23 +00004002 if (Subtarget->hasSSE41()) {
4003 SDOperand Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
4004 if (Res.Val)
4005 return Res;
4006 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004007
Duncan Sands83ec4b62008-06-06 12:08:01 +00004008 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004009 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004010 if (VT.getSizeInBits() == 16) {
Evan Cheng14b32e12007-12-11 01:46:18 +00004011 SDOperand Vec = Op.getOperand(0);
4012 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4013 if (Idx == 0)
4014 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4015 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4016 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4017 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004018 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004019 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004020 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
4021 Op.getOperand(0), Op.getOperand(1));
4022 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
4023 DAG.getValueType(VT));
4024 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004025 } else if (VT.getSizeInBits() == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004026 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4027 if (Idx == 0)
4028 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004029 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004030 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00004031 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004032 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004033 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004034 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004035 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004036 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004037 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004038 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004039 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Chris Lattnere2199452006-08-11 17:38:39 +00004040 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4041 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00004042 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004043 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00004044 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004045 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004046 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004047 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004048 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4049 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4050 // to match extract_elt for f64.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004051 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4052 if (Idx == 0)
4053 return Op;
4054
4055 // UNPCKHPD the element to the lowest double word, then movsd.
4056 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4057 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004058 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00004059 SmallVector<SDOperand, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004060 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004061 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004062 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Chris Lattnere2199452006-08-11 17:38:39 +00004063 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
4064 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00004065 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004066 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4067 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4068 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004069 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004070 }
4071
4072 return SDOperand();
4073}
4074
4075SDOperand
Nate Begeman14d12ca2008-02-11 04:19:36 +00004076X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDOperand Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004077 MVT VT = Op.getValueType();
4078 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004079
4080 SDOperand N0 = Op.getOperand(0);
4081 SDOperand N1 = Op.getOperand(1);
4082 SDOperand N2 = Op.getOperand(2);
4083
Duncan Sands83ec4b62008-06-06 12:08:01 +00004084 if ((EVT.getSizeInBits() == 8) || (EVT.getSizeInBits() == 16)) {
4085 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004086 : X86ISD::PINSRW;
4087 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4088 // argument.
4089 if (N1.getValueType() != MVT::i32)
4090 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4091 if (N2.getValueType() != MVT::i32)
4092 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4093 return DAG.getNode(Opc, VT, N0, N1, N2);
4094 } else if (EVT == MVT::f32) {
4095 // Bits [7:6] of the constant are the source select. This will always be
4096 // zero here. The DAG Combiner may combine an extract_elt index into these
4097 // bits. For example (insert (extract, 3), 2) could be matched by putting
4098 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4099 // Bits [5:4] of the constant are the destination select. This is the
4100 // value of the incoming immediate.
4101 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4102 // combine either bitwise AND or insert of float 0.0 to set these bits.
4103 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4104 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4105 }
4106 return SDOperand();
4107}
4108
4109SDOperand
Evan Cheng0db9fe62006-04-25 20:13:52 +00004110X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004111 MVT VT = Op.getValueType();
4112 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004113
4114 if (Subtarget->hasSSE41())
4115 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4116
Evan Cheng794405e2007-12-12 07:55:34 +00004117 if (EVT == MVT::i8)
4118 return SDOperand();
4119
Evan Cheng0db9fe62006-04-25 20:13:52 +00004120 SDOperand N0 = Op.getOperand(0);
4121 SDOperand N1 = Op.getOperand(1);
4122 SDOperand N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004123
Duncan Sands83ec4b62008-06-06 12:08:01 +00004124 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004125 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4126 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004127 if (N1.getValueType() != MVT::i32)
4128 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4129 if (N2.getValueType() != MVT::i32)
Chris Lattner0bd48932008-01-17 07:00:52 +00004130 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004131 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004132 }
Nate Begeman219f67f2008-01-05 20:51:30 +00004133 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004134}
4135
4136SDOperand
4137X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
4138 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004139 MVT VT = MVT::v2i32;
4140 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004141 default: break;
4142 case MVT::v16i8:
4143 case MVT::v8i16:
4144 VT = MVT::v4i32;
4145 break;
4146 }
4147 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4148 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004149}
4150
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004151// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00004152// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4153// one of the above mentioned nodes. It has to be wrapped because otherwise
4154// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4155// be used to form addressing mode. These wrapped nodes will be selected
4156// into MOV32ri.
4157SDOperand
4158X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
4159 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00004160 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
4161 getPointerTy(),
4162 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004163 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004164 // With PIC, the address is actually $g + Offset.
4165 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4166 !Subtarget->isPICStyleRIPRel()) {
4167 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4168 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4169 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004170 }
4171
4172 return Result;
4173}
4174
4175SDOperand
4176X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
4177 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004178 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004179 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004180 // With PIC, the address is actually $g + Offset.
4181 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4182 !Subtarget->isPICStyleRIPRel()) {
4183 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4184 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4185 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004186 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004187
4188 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4189 // load the value at address GV, not the value of GV itself. This means that
4190 // the GlobalAddress must be in the base or index register of the address, not
4191 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004192 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004193 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman69de1932008-02-06 22:27:42 +00004194 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004195 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004196
4197 return Result;
4198}
4199
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004200// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004201static SDOperand
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004202LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004203 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004204 SDOperand InFlag;
4205 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
4206 DAG.getNode(X86ISD::GlobalBaseReg,
4207 PtrVT), InFlag);
4208 InFlag = Chain.getValue(1);
4209
4210 // emit leal symbol@TLSGD(,%ebx,1), %eax
4211 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4212 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4213 GA->getValueType(0),
4214 GA->getOffset());
4215 SDOperand Ops[] = { Chain, TGA, InFlag };
4216 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
4217 InFlag = Result.getValue(2);
4218 Chain = Result.getValue(1);
4219
4220 // call ___tls_get_addr. This function receives its argument in
4221 // the register EAX.
4222 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4223 InFlag = Chain.getValue(1);
4224
4225 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4226 SDOperand Ops1[] = { Chain,
4227 DAG.getTargetExternalSymbol("___tls_get_addr",
4228 PtrVT),
4229 DAG.getRegister(X86::EAX, PtrVT),
4230 DAG.getRegister(X86::EBX, PtrVT),
4231 InFlag };
4232 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4233 InFlag = Chain.getValue(1);
4234
4235 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4236}
4237
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004238// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
4239static SDOperand
4240LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004241 const MVT PtrVT) {
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004242 SDOperand InFlag, Chain;
4243
4244 // emit leaq symbol@TLSGD(%rip), %rdi
4245 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
4246 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4247 GA->getValueType(0),
4248 GA->getOffset());
4249 SDOperand Ops[] = { DAG.getEntryNode(), TGA};
4250 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
4251 Chain = Result.getValue(1);
4252 InFlag = Result.getValue(2);
4253
4254 // call ___tls_get_addr. This function receives its argument in
4255 // the register RDI.
4256 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4257 InFlag = Chain.getValue(1);
4258
4259 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4260 SDOperand Ops1[] = { Chain,
4261 DAG.getTargetExternalSymbol("___tls_get_addr",
4262 PtrVT),
4263 DAG.getRegister(X86::RDI, PtrVT),
4264 InFlag };
4265 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4266 InFlag = Chain.getValue(1);
4267
4268 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4269}
4270
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004271// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4272// "local exec" model.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004273static SDOperand LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
4274 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004275 // Get the Thread Pointer
4276 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
4277 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4278 // exec)
4279 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4280 GA->getValueType(0),
4281 GA->getOffset());
4282 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004283
4284 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman69de1932008-02-06 22:27:42 +00004285 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004286 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004287
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004288 // The address of the thread local variable is the add of the thread
4289 // pointer with the offset of the variable.
4290 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4291}
4292
4293SDOperand
4294X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
4295 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004296 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004297 assert(Subtarget->isTargetELF() &&
4298 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004299 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4300 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4301 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004302 if (Subtarget->is64Bit()) {
4303 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4304 } else {
4305 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4306 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4307 else
4308 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4309 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004310}
4311
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312SDOperand
4313X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
4314 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00004315 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004316 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004317 // With PIC, the address is actually $g + Offset.
4318 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4319 !Subtarget->isPICStyleRIPRel()) {
4320 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4321 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4322 Result);
4323 }
4324
4325 return Result;
4326}
4327
4328SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
4329 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4330 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
4331 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4332 // With PIC, the address is actually $g + Offset.
4333 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4334 !Subtarget->isPICStyleRIPRel()) {
4335 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4336 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4337 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 }
4339
4340 return Result;
4341}
4342
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004343/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4344/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004345SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004346 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004347 MVT VT = Op.getValueType();
4348 unsigned VTBits = VT.getSizeInBits();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004349 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
4350 SDOperand ShOpLo = Op.getOperand(0);
4351 SDOperand ShOpHi = Op.getOperand(1);
4352 SDOperand ShAmt = Op.getOperand(2);
4353 SDOperand Tmp1 = isSRA ?
Dan Gohman4c1fa612008-03-03 22:22:09 +00004354 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4355 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004356
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004357 SDOperand Tmp2, Tmp3;
4358 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004359 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4360 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004361 } else {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004362 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4363 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004364 }
Evan Chenge3413162006-01-09 18:33:28 +00004365
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004366 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004367 DAG.getConstant(VTBits, MVT::i8));
4368 SDOperand Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004369 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004370
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004371 SDOperand Hi, Lo;
4372 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Duncan Sandsf9516202008-06-30 10:19:09 +00004373 SDOperand Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4374 SDOperand Ops1[4] = { Tmp3, Tmp1, CC, Cond };
4375
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004376 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf9516202008-06-30 10:19:09 +00004377 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4378 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004379 } else {
Duncan Sandsf9516202008-06-30 10:19:09 +00004380 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4381 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004382 }
4383
Duncan Sandsf9516202008-06-30 10:19:09 +00004384 SDOperand Ops[2] = { Lo, Hi };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004385 return DAG.getMergeValues(Ops, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004386}
Evan Chenga3195e82006-01-12 22:54:21 +00004387
Evan Cheng0db9fe62006-04-25 20:13:52 +00004388SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004389 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004390 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004391 "Unknown SINT_TO_FP to lower!");
4392
4393 // These are really Legal; caller falls through into that case.
4394 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
4395 return SDOperand();
4396 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4397 Subtarget->is64Bit())
4398 return SDOperand();
4399
Duncan Sands83ec4b62008-06-06 12:08:01 +00004400 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004401 MachineFunction &MF = DAG.getMachineFunction();
4402 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
4403 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00004404 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004405 StackSlot,
Dan Gohman3069b872008-02-07 18:41:25 +00004406 PseudoSourceValue::getFixedStack(),
Dan Gohman69de1932008-02-06 22:27:42 +00004407 SSFI);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004408
4409 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004410 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004411 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004412 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004413 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4414 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004415 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004416 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004417 Ops.push_back(Chain);
4418 Ops.push_back(StackSlot);
4419 Ops.push_back(DAG.getValueType(SrcVT));
Chris Lattnerb09916b2008-02-27 05:57:41 +00004420 SDOperand Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
4421 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004423 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004424 Chain = Result.getValue(1);
4425 SDOperand InFlag = Result.getValue(2);
4426
4427 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4428 // shouldn't be necessary except that RFP cannot be live across
4429 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004430 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004431 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004432 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004433 Tys = DAG.getVTList(MVT::Other);
4434 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004435 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004436 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004437 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004438 Ops.push_back(DAG.getValueType(Op.getValueType()));
4439 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004440 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman69de1932008-02-06 22:27:42 +00004441 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohman3069b872008-02-07 18:41:25 +00004442 PseudoSourceValue::getFixedStack(), SSFI);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004443 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004444
Evan Cheng0db9fe62006-04-25 20:13:52 +00004445 return Result;
4446}
4447
Chris Lattner27a6c732007-11-24 07:07:01 +00004448std::pair<SDOperand,SDOperand> X86TargetLowering::
4449FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004450 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4451 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004452 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004453
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004454 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004455 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004456 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattner27a6c732007-11-24 07:07:01 +00004457 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen73328d12007-09-19 23:55:34 +00004458 if (Subtarget->is64Bit() &&
4459 Op.getValueType() == MVT::i64 &&
4460 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattner27a6c732007-11-24 07:07:01 +00004461 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004462
Evan Cheng87c89352007-10-15 20:11:21 +00004463 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4464 // stack slot.
4465 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004466 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004467 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4468 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004469 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004470 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004471 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4472 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4473 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4474 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004475 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004476
Evan Cheng0db9fe62006-04-25 20:13:52 +00004477 SDOperand Chain = DAG.getEntryNode();
4478 SDOperand Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004479 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004480 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman69de1932008-02-06 22:27:42 +00004481 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohman3069b872008-02-07 18:41:25 +00004482 PseudoSourceValue::getFixedStack(), SSFI);
Dale Johannesen849f2142007-07-03 00:53:03 +00004483 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004484 SDOperand Ops[] = {
4485 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4486 };
4487 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004488 Chain = Value.getValue(1);
4489 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4490 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4491 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004492
Evan Cheng0db9fe62006-04-25 20:13:52 +00004493 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00004494 SDOperand Ops[] = { Chain, Value, StackSlot };
4495 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004496
Chris Lattner27a6c732007-11-24 07:07:01 +00004497 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004498}
4499
Chris Lattner27a6c732007-11-24 07:07:01 +00004500SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004501 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4502 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4503 if (FIST.Val == 0) return SDOperand();
4504
4505 // Load the result.
4506 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4507}
4508
4509SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4510 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4511 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4512 if (FIST.Val == 0) return 0;
Duncan Sandsf9516202008-06-30 10:19:09 +00004513
4514 MVT VT = N->getValueType(0);
4515
4516 // Return a load from the stack slot.
4517 SDOperand Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004518
Duncan Sands4bdcb612008-07-02 17:40:58 +00004519 // Use MERGE_VALUES to drop the chain result value and get a node with one
4520 // result. This requires turning off getMergeValues simplification, since
4521 // otherwise it will give us Res back.
4522 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf9516202008-06-30 10:19:09 +00004523}
Chris Lattner27a6c732007-11-24 07:07:01 +00004524
Evan Cheng0db9fe62006-04-25 20:13:52 +00004525SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004526 MVT VT = Op.getValueType();
4527 MVT EltVT = VT;
4528 if (VT.isVector())
4529 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004530 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004531 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004532 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004533 CV.push_back(C);
4534 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004535 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004536 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004537 CV.push_back(C);
4538 CV.push_back(C);
4539 CV.push_back(C);
4540 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004541 }
Dan Gohmand3006222007-07-27 17:16:43 +00004542 Constant *C = ConstantVector::get(CV);
4543 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004544 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004545 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004546 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004547 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4548}
4549
4550SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004551 MVT VT = Op.getValueType();
4552 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004553 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004554 if (VT.isVector()) {
4555 EltVT = VT.getVectorElementType();
4556 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004557 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004558 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004559 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004560 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004561 CV.push_back(C);
4562 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004564 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004565 CV.push_back(C);
4566 CV.push_back(C);
4567 CV.push_back(C);
4568 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004569 }
Dan Gohmand3006222007-07-27 17:16:43 +00004570 Constant *C = ConstantVector::get(CV);
4571 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004572 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004573 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004574 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004575 if (VT.isVector()) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004576 return DAG.getNode(ISD::BIT_CONVERT, VT,
4577 DAG.getNode(ISD::XOR, MVT::v2i64,
4578 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4579 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4580 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004581 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4582 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004583}
4584
Evan Cheng68c47cb2007-01-05 07:55:56 +00004585SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004586 SDOperand Op0 = Op.getOperand(0);
4587 SDOperand Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004588 MVT VT = Op.getValueType();
4589 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004590
4591 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004592 if (SrcVT.bitsLT(VT)) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004593 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4594 SrcVT = VT;
4595 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004596 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004597 if (SrcVT.bitsGT(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004598 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004599 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004600 }
4601
4602 // At this point the operands and the result should have the same
4603 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004604
Evan Cheng68c47cb2007-01-05 07:55:56 +00004605 // First get the sign bit of second operand.
4606 std::vector<Constant*> CV;
4607 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004608 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4609 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004610 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004611 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4612 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4613 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4614 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004615 }
Dan Gohmand3006222007-07-27 17:16:43 +00004616 Constant *C = ConstantVector::get(CV);
4617 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004618 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004619 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004620 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004621 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004622
4623 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004624 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004625 // Op0 is MVT::f32, Op1 is MVT::f64.
4626 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4627 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4628 DAG.getConstant(32, MVT::i32));
4629 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4630 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004631 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004632 }
4633
Evan Cheng73d6cf12007-01-05 21:37:56 +00004634 // Clear first operand sign bit.
4635 CV.clear();
4636 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004637 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4638 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004639 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004640 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4641 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4642 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4643 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004644 }
Dan Gohmand3006222007-07-27 17:16:43 +00004645 C = ConstantVector::get(CV);
4646 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman69de1932008-02-06 22:27:42 +00004647 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004648 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004649 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004650 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4651
4652 // Or the value with the sign bit.
4653 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004654}
4655
Evan Chenge5f62042007-09-29 00:00:36 +00004656SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004657 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004658 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004659 SDOperand Op0 = Op.getOperand(0);
4660 SDOperand Op1 = Op.getOperand(1);
4661 SDOperand CC = Op.getOperand(2);
4662 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004663 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng0488db92007-09-25 01:57:46 +00004664 unsigned X86CC;
4665
Evan Cheng0488db92007-09-25 01:57:46 +00004666 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004667 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004668 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4669 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004670 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004671 }
Evan Cheng0488db92007-09-25 01:57:46 +00004672
4673 assert(isFP && "Illegal integer SetCC!");
4674
Evan Chenge5f62042007-09-29 00:00:36 +00004675 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004676 switch (SetCCOpcode) {
4677 default: assert(false && "Illegal floating point SetCC!");
4678 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004679 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004680 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004681 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004682 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4683 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4684 }
4685 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004686 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004687 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004688 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004689 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4690 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4691 }
4692 }
4693}
4694
4695
Evan Cheng0db9fe62006-04-25 20:13:52 +00004696SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004697 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004698 SDOperand Cond = Op.getOperand(0);
4699 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004700
Evan Cheng734503b2006-09-11 02:19:56 +00004701 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004702 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004703
Evan Cheng3f41d662007-10-08 22:16:29 +00004704 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4705 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004706 if (Cond.getOpcode() == X86ISD::SETCC) {
4707 CC = Cond.getOperand(0);
4708
Evan Cheng734503b2006-09-11 02:19:56 +00004709 SDOperand Cmp = Cond.getOperand(1);
4710 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004711 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00004712
Evan Cheng3f41d662007-10-08 22:16:29 +00004713 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004714 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00004715 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng3f41d662007-10-08 22:16:29 +00004716 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner1956d152008-01-16 06:19:45 +00004717
Evan Chenge5f62042007-09-29 00:00:36 +00004718 if ((Opc == X86ISD::CMP ||
4719 Opc == X86ISD::COMI ||
4720 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004721 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004722 addTest = false;
4723 }
4724 }
4725
4726 if (addTest) {
4727 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004728 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004729 }
4730
Duncan Sands83ec4b62008-06-06 12:08:01 +00004731 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004732 MVT::Flag);
4733 SmallVector<SDOperand, 4> Ops;
4734 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4735 // condition is true.
4736 Ops.push_back(Op.getOperand(2));
4737 Ops.push_back(Op.getOperand(1));
4738 Ops.push_back(CC);
4739 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004740 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004741}
4742
Evan Cheng0db9fe62006-04-25 20:13:52 +00004743SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004744 bool addTest = true;
4745 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004746 SDOperand Cond = Op.getOperand(1);
4747 SDOperand Dest = Op.getOperand(2);
4748 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004749
Evan Cheng0db9fe62006-04-25 20:13:52 +00004750 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004751 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004752
Evan Cheng3f41d662007-10-08 22:16:29 +00004753 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4754 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004755 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004756 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004757
Evan Cheng734503b2006-09-11 02:19:56 +00004758 SDOperand Cmp = Cond.getOperand(1);
4759 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004760 if (Opc == X86ISD::CMP ||
4761 Opc == X86ISD::COMI ||
4762 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004763 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004764 addTest = false;
4765 }
4766 }
4767
4768 if (addTest) {
4769 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004770 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004771 }
Evan Chenge5f62042007-09-29 00:00:36 +00004772 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004773 Chain, Op.getOperand(2), CC, Cond);
4774}
4775
Anton Korobeynikove060b532007-04-17 19:34:00 +00004776
4777// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4778// Calls to _alloca is needed to probe the stack when allocating more than 4k
4779// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4780// that the guard pages used by the OS virtual memory manager are allocated in
4781// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004782SDOperand
4783X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4784 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004785 assert(Subtarget->isTargetCygMing() &&
4786 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004787
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004788 // Get the inputs.
4789 SDOperand Chain = Op.getOperand(0);
4790 SDOperand Size = Op.getOperand(1);
4791 // FIXME: Ensure alignment here
4792
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004793 SDOperand Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004794
Duncan Sands83ec4b62008-06-06 12:08:01 +00004795 MVT IntPtr = getPointerTy();
4796 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004797
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004798 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4799
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004800 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4801 Flag = Chain.getValue(1);
4802
4803 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4804 SDOperand Ops[] = { Chain,
4805 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4806 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004807 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004808 Flag };
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004809 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004810 Flag = Chain.getValue(1);
4811
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004812 Chain = DAG.getCALLSEQ_END(Chain,
4813 DAG.getIntPtrConstant(0),
4814 DAG.getIntPtrConstant(0),
4815 Flag);
4816
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004817 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004818
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004819 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004820 return DAG.getMergeValues(Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004821}
4822
Dan Gohman707e0182008-04-12 04:36:06 +00004823SDOperand
4824X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
4825 SDOperand Chain,
4826 SDOperand Dst, SDOperand Src,
4827 SDOperand Size, unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00004828 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00004829 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004830
Dan Gohman707e0182008-04-12 04:36:06 +00004831 /// If not DWORD aligned or size is more than the threshold, call the library.
4832 /// The libc version is likely to be faster for these cases. It can use the
4833 /// address value and run time information about the CPU.
4834 if ((Align & 3) == 0 ||
4835 !ConstantSize ||
4836 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
4837 SDOperand InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00004838
4839 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00004840 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
4841 if (const char *bzeroEntry =
4842 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004843 MVT IntPtr = getPointerTy();
Dan Gohman707e0182008-04-12 04:36:06 +00004844 const Type *IntPtrTy = getTargetData()->getIntPtrType();
4845 TargetLowering::ArgListTy Args;
4846 TargetLowering::ArgListEntry Entry;
4847 Entry.Node = Dst;
Dan Gohman68d599d2008-04-01 20:38:36 +00004848 Entry.Ty = IntPtrTy;
4849 Args.push_back(Entry);
Dan Gohman707e0182008-04-12 04:36:06 +00004850 Entry.Node = Size;
4851 Args.push_back(Entry);
4852 std::pair<SDOperand,SDOperand> CallResult =
4853 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
4854 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
4855 Args, DAG);
4856 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00004857 }
4858
Dan Gohman707e0182008-04-12 04:36:06 +00004859 // Otherwise have the target-independent code call memset.
4860 return SDOperand();
Evan Cheng48090aa2006-03-21 23:01:21 +00004861 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004862
Dan Gohman707e0182008-04-12 04:36:06 +00004863 uint64_t SizeVal = ConstantSize->getValue();
4864 SDOperand InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004865 MVT AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004866 SDOperand Count;
Dan Gohman707e0182008-04-12 04:36:06 +00004867 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004868 unsigned BytesLeft = 0;
4869 bool TwoRepStos = false;
4870 if (ValC) {
4871 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004872 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004873
Evan Cheng0db9fe62006-04-25 20:13:52 +00004874 // If the value is a constant, then we can potentially use larger sets.
4875 switch (Align & 3) {
4876 case 2: // WORD aligned
4877 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004878 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004879 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004880 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004881 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004882 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004883 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004884 Val = (Val << 8) | Val;
4885 Val = (Val << 16) | Val;
Dan Gohman6f836ad2008-04-12 02:35:39 +00004886 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
Evan Cheng25ab6902006-09-08 06:48:29 +00004887 AVT = MVT::i64;
4888 ValReg = X86::RAX;
4889 Val = (Val << 32) | Val;
4890 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004891 break;
4892 default: // Byte aligned
4893 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004894 ValReg = X86::AL;
Dan Gohmanbcda2852008-04-16 01:32:32 +00004895 Count = DAG.getIntPtrConstant(SizeVal);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004896 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004897 }
4898
Duncan Sands8e4eb092008-06-08 20:54:56 +00004899 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004900 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00004901 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
4902 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004903 }
4904
Evan Cheng0db9fe62006-04-25 20:13:52 +00004905 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4906 InFlag);
4907 InFlag = Chain.getValue(1);
4908 } else {
4909 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00004910 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohman707e0182008-04-12 04:36:06 +00004911 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004912 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004913 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004914
Evan Cheng25ab6902006-09-08 06:48:29 +00004915 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4916 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004917 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004918 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00004919 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004920 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004921
Chris Lattnerd96d0722007-02-25 06:40:16 +00004922 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004923 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004924 Ops.push_back(Chain);
4925 Ops.push_back(DAG.getValueType(AVT));
4926 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004927 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004928
Evan Cheng0db9fe62006-04-25 20:13:52 +00004929 if (TwoRepStos) {
4930 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00004931 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004932 MVT CVT = Count.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004933 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004934 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4935 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4936 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004937 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004938 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004939 Ops.clear();
4940 Ops.push_back(Chain);
4941 Ops.push_back(DAG.getValueType(MVT::i8));
4942 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004943 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004944 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00004945 // Handle the last 1 - 7 bytes.
4946 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004947 MVT AddrVT = Dst.getValueType();
4948 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00004949
4950 Chain = DAG.getMemset(Chain,
4951 DAG.getNode(ISD::ADD, AddrVT, Dst,
4952 DAG.getConstant(Offset, AddrVT)),
4953 Src,
4954 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00004955 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00004956 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004957
Dan Gohman707e0182008-04-12 04:36:06 +00004958 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004959 return Chain;
4960}
Evan Cheng11e15b32006-04-03 20:53:28 +00004961
Dan Gohman707e0182008-04-12 04:36:06 +00004962SDOperand
4963X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
4964 SDOperand Chain,
4965 SDOperand Dst, SDOperand Src,
4966 SDOperand Size, unsigned Align,
4967 bool AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00004968 const Value *DstSV, uint64_t DstSVOff,
4969 const Value *SrcSV, uint64_t SrcSVOff){
Dan Gohman707e0182008-04-12 04:36:06 +00004970
4971 // This requires the copy size to be a constant, preferrably
4972 // within a subtarget-specific limit.
4973 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
4974 if (!ConstantSize)
4975 return SDOperand();
4976 uint64_t SizeVal = ConstantSize->getValue();
4977 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
4978 return SDOperand();
4979
Duncan Sands83ec4b62008-06-06 12:08:01 +00004980 MVT AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004981 unsigned BytesLeft = 0;
Dan Gohman707e0182008-04-12 04:36:06 +00004982 if (Align >= 8 && Subtarget->is64Bit())
4983 AVT = MVT::i64;
4984 else if (Align >= 4)
4985 AVT = MVT::i32;
4986 else if (Align >= 2)
4987 AVT = MVT::i16;
4988 else
4989 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004990
Duncan Sands83ec4b62008-06-06 12:08:01 +00004991 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00004992 unsigned CountVal = SizeVal / UBytes;
4993 SDOperand Count = DAG.getIntPtrConstant(CountVal);
4994 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004995
Evan Cheng0db9fe62006-04-25 20:13:52 +00004996 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004997 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4998 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004999 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005000 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005001 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005002 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005003 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005004 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005005 InFlag = Chain.getValue(1);
5006
Chris Lattnerd96d0722007-02-25 06:40:16 +00005007 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00005008 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005009 Ops.push_back(Chain);
5010 Ops.push_back(DAG.getValueType(AVT));
5011 Ops.push_back(InFlag);
Evan Cheng2749c722008-04-25 00:26:43 +00005012 SDOperand RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005013
Evan Cheng2749c722008-04-25 00:26:43 +00005014 SmallVector<SDOperand, 4> Results;
5015 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005016 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005017 // Handle the last 1 - 7 bytes.
5018 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005019 MVT DstVT = Dst.getValueType();
5020 MVT SrcVT = Src.getValueType();
5021 MVT SizeVT = Size.getValueType();
Evan Cheng2749c722008-04-25 00:26:43 +00005022 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00005023 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005024 DAG.getConstant(Offset, DstVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005025 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005026 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005027 DAG.getConstant(BytesLeft, SizeVT),
5028 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005029 DstSV, DstSVOff + Offset,
5030 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005031 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005032
Dan Gohman707e0182008-04-12 04:36:06 +00005033 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005034}
5035
Chris Lattner27a6c732007-11-24 07:07:01 +00005036/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5037SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00005038 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner27a6c732007-11-24 07:07:01 +00005039 SDOperand TheChain = N->getOperand(0);
5040 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005041 if (Subtarget->is64Bit()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005042 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5043 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
5044 MVT::i64, rax.getValue(2));
5045 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005046 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00005047 SDOperand Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00005048 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00005049 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00005050
Duncan Sands4bdcb612008-07-02 17:40:58 +00005051 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005052 }
Chris Lattner5a88b832007-02-25 07:10:00 +00005053
Chris Lattner27a6c732007-11-24 07:07:01 +00005054 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5055 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
5056 MVT::i32, eax.getValue(2));
5057 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
5058 SDOperand Ops[] = { eax, edx };
5059 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5060
5061 // Use a MERGE_VALUES to return the value and chain.
5062 Ops[1] = edx.getValue(1);
Duncan Sands4bdcb612008-07-02 17:40:58 +00005063 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005064}
5065
5066SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005067 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005068
Evan Cheng25ab6902006-09-08 06:48:29 +00005069 if (!Subtarget->is64Bit()) {
5070 // vastart just stores the address of the VarArgsFrameIndex slot into the
5071 // memory location argument.
5072 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005073 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005074 }
5075
5076 // __va_list_tag:
5077 // gp_offset (0 - 6 * 8)
5078 // fp_offset (48 - 48 + 8 * 16)
5079 // overflow_arg_area (point to parameters coming in memory).
5080 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00005081 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00005082 SDOperand FIN = Op.getOperand(1);
5083 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00005084 SDOperand Store = DAG.getStore(Op.getOperand(0),
5085 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005086 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005087 MemOps.push_back(Store);
5088
5089 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00005090 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00005091 Store = DAG.getStore(Op.getOperand(0),
5092 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005093 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005094 MemOps.push_back(Store);
5095
5096 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00005097 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng25ab6902006-09-08 06:48:29 +00005098 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005099 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005100 MemOps.push_back(Store);
5101
5102 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00005103 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Evan Cheng25ab6902006-09-08 06:48:29 +00005104 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005105 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005106 MemOps.push_back(Store);
5107 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005108}
5109
Dan Gohman9018e832008-05-10 01:26:14 +00005110SDOperand X86TargetLowering::LowerVAARG(SDOperand Op, SelectionDAG &DAG) {
5111 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5112 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
5113 SDOperand Chain = Op.getOperand(0);
5114 SDOperand SrcPtr = Op.getOperand(1);
5115 SDOperand SrcSV = Op.getOperand(2);
5116
5117 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5118 abort();
Dan Gohman2ce38982008-05-12 16:17:19 +00005119 return SDOperand();
Dan Gohman9018e832008-05-10 01:26:14 +00005120}
5121
Evan Chengae642192007-03-02 23:16:35 +00005122SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
5123 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005124 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Evan Chengae642192007-03-02 23:16:35 +00005125 SDOperand Chain = Op.getOperand(0);
5126 SDOperand DstPtr = Op.getOperand(1);
5127 SDOperand SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005128 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5129 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengae642192007-03-02 23:16:35 +00005130
Dan Gohman28269132008-04-18 20:55:41 +00005131 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5132 DAG.getIntPtrConstant(24), 8, false,
5133 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005134}
5135
Evan Cheng0db9fe62006-04-25 20:13:52 +00005136SDOperand
5137X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
5138 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5139 switch (IntNo) {
5140 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005141 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142 case Intrinsic::x86_sse_comieq_ss:
5143 case Intrinsic::x86_sse_comilt_ss:
5144 case Intrinsic::x86_sse_comile_ss:
5145 case Intrinsic::x86_sse_comigt_ss:
5146 case Intrinsic::x86_sse_comige_ss:
5147 case Intrinsic::x86_sse_comineq_ss:
5148 case Intrinsic::x86_sse_ucomieq_ss:
5149 case Intrinsic::x86_sse_ucomilt_ss:
5150 case Intrinsic::x86_sse_ucomile_ss:
5151 case Intrinsic::x86_sse_ucomigt_ss:
5152 case Intrinsic::x86_sse_ucomige_ss:
5153 case Intrinsic::x86_sse_ucomineq_ss:
5154 case Intrinsic::x86_sse2_comieq_sd:
5155 case Intrinsic::x86_sse2_comilt_sd:
5156 case Intrinsic::x86_sse2_comile_sd:
5157 case Intrinsic::x86_sse2_comigt_sd:
5158 case Intrinsic::x86_sse2_comige_sd:
5159 case Intrinsic::x86_sse2_comineq_sd:
5160 case Intrinsic::x86_sse2_ucomieq_sd:
5161 case Intrinsic::x86_sse2_ucomilt_sd:
5162 case Intrinsic::x86_sse2_ucomile_sd:
5163 case Intrinsic::x86_sse2_ucomigt_sd:
5164 case Intrinsic::x86_sse2_ucomige_sd:
5165 case Intrinsic::x86_sse2_ucomineq_sd: {
5166 unsigned Opc = 0;
5167 ISD::CondCode CC = ISD::SETCC_INVALID;
5168 switch (IntNo) {
5169 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005170 case Intrinsic::x86_sse_comieq_ss:
5171 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005172 Opc = X86ISD::COMI;
5173 CC = ISD::SETEQ;
5174 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005175 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005176 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005177 Opc = X86ISD::COMI;
5178 CC = ISD::SETLT;
5179 break;
5180 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005181 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005182 Opc = X86ISD::COMI;
5183 CC = ISD::SETLE;
5184 break;
5185 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005186 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005187 Opc = X86ISD::COMI;
5188 CC = ISD::SETGT;
5189 break;
5190 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005191 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005192 Opc = X86ISD::COMI;
5193 CC = ISD::SETGE;
5194 break;
5195 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005196 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005197 Opc = X86ISD::COMI;
5198 CC = ISD::SETNE;
5199 break;
5200 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005201 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005202 Opc = X86ISD::UCOMI;
5203 CC = ISD::SETEQ;
5204 break;
5205 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005206 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005207 Opc = X86ISD::UCOMI;
5208 CC = ISD::SETLT;
5209 break;
5210 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005211 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005212 Opc = X86ISD::UCOMI;
5213 CC = ISD::SETLE;
5214 break;
5215 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005216 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005217 Opc = X86ISD::UCOMI;
5218 CC = ISD::SETGT;
5219 break;
5220 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005221 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005222 Opc = X86ISD::UCOMI;
5223 CC = ISD::SETGE;
5224 break;
5225 case Intrinsic::x86_sse_ucomineq_ss:
5226 case Intrinsic::x86_sse2_ucomineq_sd:
5227 Opc = X86ISD::UCOMI;
5228 CC = ISD::SETNE;
5229 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005230 }
Evan Cheng734503b2006-09-11 02:19:56 +00005231
Evan Cheng0db9fe62006-04-25 20:13:52 +00005232 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00005233 SDOperand LHS = Op.getOperand(1);
5234 SDOperand RHS = Op.getOperand(2);
5235 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005236
Evan Chenge5f62042007-09-29 00:00:36 +00005237 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5238 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
5239 DAG.getConstant(X86CC, MVT::i8), Cond);
5240 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005241 }
Evan Cheng5759f972008-05-04 09:15:50 +00005242
5243 // Fix vector shift instructions where the last operand is a non-immediate
5244 // i32 value.
5245 case Intrinsic::x86_sse2_pslli_w:
5246 case Intrinsic::x86_sse2_pslli_d:
5247 case Intrinsic::x86_sse2_pslli_q:
5248 case Intrinsic::x86_sse2_psrli_w:
5249 case Intrinsic::x86_sse2_psrli_d:
5250 case Intrinsic::x86_sse2_psrli_q:
5251 case Intrinsic::x86_sse2_psrai_w:
5252 case Intrinsic::x86_sse2_psrai_d:
5253 case Intrinsic::x86_mmx_pslli_w:
5254 case Intrinsic::x86_mmx_pslli_d:
5255 case Intrinsic::x86_mmx_pslli_q:
5256 case Intrinsic::x86_mmx_psrli_w:
5257 case Intrinsic::x86_mmx_psrli_d:
5258 case Intrinsic::x86_mmx_psrli_q:
5259 case Intrinsic::x86_mmx_psrai_w:
5260 case Intrinsic::x86_mmx_psrai_d: {
5261 SDOperand ShAmt = Op.getOperand(2);
5262 if (isa<ConstantSDNode>(ShAmt))
5263 return SDOperand();
5264
5265 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005266 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005267 switch (IntNo) {
5268 case Intrinsic::x86_sse2_pslli_w:
5269 NewIntNo = Intrinsic::x86_sse2_psll_w;
5270 break;
5271 case Intrinsic::x86_sse2_pslli_d:
5272 NewIntNo = Intrinsic::x86_sse2_psll_d;
5273 break;
5274 case Intrinsic::x86_sse2_pslli_q:
5275 NewIntNo = Intrinsic::x86_sse2_psll_q;
5276 break;
5277 case Intrinsic::x86_sse2_psrli_w:
5278 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5279 break;
5280 case Intrinsic::x86_sse2_psrli_d:
5281 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5282 break;
5283 case Intrinsic::x86_sse2_psrli_q:
5284 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5285 break;
5286 case Intrinsic::x86_sse2_psrai_w:
5287 NewIntNo = Intrinsic::x86_sse2_psra_w;
5288 break;
5289 case Intrinsic::x86_sse2_psrai_d:
5290 NewIntNo = Intrinsic::x86_sse2_psra_d;
5291 break;
5292 default: {
5293 ShAmtVT = MVT::v2i32;
5294 switch (IntNo) {
5295 case Intrinsic::x86_mmx_pslli_w:
5296 NewIntNo = Intrinsic::x86_mmx_psll_w;
5297 break;
5298 case Intrinsic::x86_mmx_pslli_d:
5299 NewIntNo = Intrinsic::x86_mmx_psll_d;
5300 break;
5301 case Intrinsic::x86_mmx_pslli_q:
5302 NewIntNo = Intrinsic::x86_mmx_psll_q;
5303 break;
5304 case Intrinsic::x86_mmx_psrli_w:
5305 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5306 break;
5307 case Intrinsic::x86_mmx_psrli_d:
5308 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5309 break;
5310 case Intrinsic::x86_mmx_psrli_q:
5311 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5312 break;
5313 case Intrinsic::x86_mmx_psrai_w:
5314 NewIntNo = Intrinsic::x86_mmx_psra_w;
5315 break;
5316 case Intrinsic::x86_mmx_psrai_d:
5317 NewIntNo = Intrinsic::x86_mmx_psra_d;
5318 break;
5319 default: abort(); // Can't reach here.
5320 }
5321 break;
5322 }
5323 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00005324 MVT VT = Op.getValueType();
Evan Cheng5759f972008-05-04 09:15:50 +00005325 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5326 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5327 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5328 DAG.getConstant(NewIntNo, MVT::i32),
5329 Op.getOperand(1), ShAmt);
5330 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005331 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005332}
Evan Cheng72261582005-12-20 06:22:03 +00005333
Nate Begemanbcc5f362007-01-29 22:58:52 +00005334SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
5335 // Depths > 0 not supported yet!
5336 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5337 return SDOperand();
5338
5339 // Just load the return address
5340 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5341 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5342}
5343
5344SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
5345 // Depths > 0 not supported yet!
5346 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
5347 return SDOperand();
5348
5349 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
5350 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner0bd48932008-01-17 07:00:52 +00005351 DAG.getIntPtrConstant(4));
Nate Begemanbcc5f362007-01-29 22:58:52 +00005352}
5353
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005354SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
5355 SelectionDAG &DAG) {
5356 // Is not yet supported on x86-64
5357 if (Subtarget->is64Bit())
5358 return SDOperand();
5359
Chris Lattner0bd48932008-01-17 07:00:52 +00005360 return DAG.getIntPtrConstant(8);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005361}
5362
5363SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
5364{
5365 assert(!Subtarget->is64Bit() &&
5366 "Lowering of eh_return builtin is not supported yet on x86-64");
5367
5368 MachineFunction &MF = DAG.getMachineFunction();
5369 SDOperand Chain = Op.getOperand(0);
5370 SDOperand Offset = Op.getOperand(1);
5371 SDOperand Handler = Op.getOperand(2);
5372
5373 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
5374 getPointerTy());
5375
5376 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner0bd48932008-01-17 07:00:52 +00005377 DAG.getIntPtrConstant(-4UL));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005378 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5379 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5380 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner84bc5422007-12-31 04:13:23 +00005381 MF.getRegInfo().addLiveOut(X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005382
5383 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5384 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5385}
5386
Duncan Sandsb116fac2007-07-27 20:02:49 +00005387SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
5388 SelectionDAG &DAG) {
5389 SDOperand Root = Op.getOperand(0);
5390 SDOperand Trmp = Op.getOperand(1); // trampoline
5391 SDOperand FPtr = Op.getOperand(2); // nested function
5392 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
5393
Dan Gohman69de1932008-02-06 22:27:42 +00005394 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005395
Duncan Sands339e14f2008-01-16 22:55:25 +00005396 const X86InstrInfo *TII =
5397 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5398
Duncan Sandsb116fac2007-07-27 20:02:49 +00005399 if (Subtarget->is64Bit()) {
Duncan Sands339e14f2008-01-16 22:55:25 +00005400 SDOperand OutChains[6];
5401
5402 // Large code-model.
5403
5404 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5405 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5406
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005407 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5408 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00005409
5410 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5411
5412 // Load the pointer to the nested function into R11.
5413 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
5414 SDOperand Addr = Trmp;
5415 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005416 TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00005417
5418 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005419 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005420
5421 // Load the 'nest' parameter value into R10.
5422 // R10 is specified in X86CallingConv.td
5423 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5424 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5425 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005426 TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005427
5428 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005429 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005430
5431 // Jump to the nested function.
5432 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5433 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5434 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005435 TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00005436
5437 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5438 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5439 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005440 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00005441
5442 SDOperand Ops[] =
5443 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005444 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005445 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00005446 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00005447 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5448 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005449 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005450
5451 switch (CC) {
5452 default:
5453 assert(0 && "Unsupported calling convention");
5454 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005455 case CallingConv::X86_StdCall: {
5456 // Pass 'nest' parameter in ECX.
5457 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005458 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005459
5460 // Check that ECX wasn't needed by an 'inreg' parameter.
5461 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner58d74912008-03-12 17:45:29 +00005462 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005463
Chris Lattner58d74912008-03-12 17:45:29 +00005464 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00005465 unsigned InRegCount = 0;
5466 unsigned Idx = 1;
5467
5468 for (FunctionType::param_iterator I = FTy->param_begin(),
5469 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner58d74912008-03-12 17:45:29 +00005470 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00005471 // FIXME: should only count parameters that are lowered to integers.
5472 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5473
5474 if (InRegCount > 2) {
5475 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5476 abort();
5477 }
5478 }
5479 break;
5480 }
5481 case CallingConv::X86_FastCall:
5482 // Pass 'nest' parameter in EAX.
5483 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005484 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005485 break;
5486 }
5487
5488 SDOperand OutChains[4];
5489 SDOperand Addr, Disp;
5490
5491 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5492 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5493
Duncan Sands339e14f2008-01-16 22:55:25 +00005494 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005495 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005496 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00005497 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005498
5499 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005500 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005501
Duncan Sands339e14f2008-01-16 22:55:25 +00005502 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005503 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5504 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005505 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005506
5507 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005508 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005509
Duncan Sandsf7331b32007-09-11 14:10:23 +00005510 SDOperand Ops[] =
5511 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005512 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005513 }
5514}
5515
Dan Gohman1a024862008-01-31 00:41:03 +00005516SDOperand X86TargetLowering::LowerFLT_ROUNDS_(SDOperand Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005517 /*
5518 The rounding mode is in bits 11:10 of FPSR, and has the following
5519 settings:
5520 00 Round to nearest
5521 01 Round to -inf
5522 10 Round to +inf
5523 11 Round to 0
5524
5525 FLT_ROUNDS, on the other hand, expects the following:
5526 -1 Undefined
5527 0 Round to 0
5528 1 Round to nearest
5529 2 Round to +inf
5530 3 Round to -inf
5531
5532 To perform the conversion, we do:
5533 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5534 */
5535
5536 MachineFunction &MF = DAG.getMachineFunction();
5537 const TargetMachine &TM = MF.getTarget();
5538 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5539 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005540 MVT VT = Op.getValueType();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005541
5542 // Save FP Control Word to stack slot
5543 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5544 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5545
5546 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5547 DAG.getEntryNode(), StackSlot);
5548
5549 // Load FP Control Word from stack slot
5550 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5551
5552 // Transform as necessary
5553 SDOperand CWD1 =
5554 DAG.getNode(ISD::SRL, MVT::i16,
5555 DAG.getNode(ISD::AND, MVT::i16,
5556 CWD, DAG.getConstant(0x800, MVT::i16)),
5557 DAG.getConstant(11, MVT::i8));
5558 SDOperand CWD2 =
5559 DAG.getNode(ISD::SRL, MVT::i16,
5560 DAG.getNode(ISD::AND, MVT::i16,
5561 CWD, DAG.getConstant(0x400, MVT::i16)),
5562 DAG.getConstant(9, MVT::i8));
5563
5564 SDOperand RetVal =
5565 DAG.getNode(ISD::AND, MVT::i16,
5566 DAG.getNode(ISD::ADD, MVT::i16,
5567 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5568 DAG.getConstant(1, MVT::i16)),
5569 DAG.getConstant(3, MVT::i16));
5570
5571
Duncan Sands83ec4b62008-06-06 12:08:01 +00005572 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005573 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5574}
5575
Evan Cheng18efe262007-12-14 02:13:44 +00005576SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005577 MVT VT = Op.getValueType();
5578 MVT OpVT = VT;
5579 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005580
5581 Op = Op.getOperand(0);
5582 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005583 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005584 OpVT = MVT::i32;
5585 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5586 }
Evan Cheng18efe262007-12-14 02:13:44 +00005587
Evan Cheng152804e2007-12-14 08:30:15 +00005588 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5589 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5590 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5591
5592 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5593 SmallVector<SDOperand, 4> Ops;
5594 Ops.push_back(Op);
5595 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5596 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5597 Ops.push_back(Op.getValue(1));
5598 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5599
5600 // Finally xor with NumBits-1.
5601 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5602
Evan Cheng18efe262007-12-14 02:13:44 +00005603 if (VT == MVT::i8)
5604 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5605 return Op;
5606}
5607
5608SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005609 MVT VT = Op.getValueType();
5610 MVT OpVT = VT;
5611 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005612
5613 Op = Op.getOperand(0);
5614 if (VT == MVT::i8) {
5615 OpVT = MVT::i32;
5616 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5617 }
Evan Cheng152804e2007-12-14 08:30:15 +00005618
5619 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5620 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5621 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5622
5623 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5624 SmallVector<SDOperand, 4> Ops;
5625 Ops.push_back(Op);
5626 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5627 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5628 Ops.push_back(Op.getValue(1));
5629 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5630
Evan Cheng18efe262007-12-14 02:13:44 +00005631 if (VT == MVT::i8)
5632 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5633 return Op;
5634}
5635
Mon P Wang28873102008-06-25 08:15:39 +00005636SDOperand X86TargetLowering::LowerCMP_SWAP(SDOperand Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005637 MVT T = Op.getValueType();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00005638 unsigned Reg = 0;
5639 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005640 switch(T.getSimpleVT()) {
5641 default:
5642 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005643 case MVT::i8: Reg = X86::AL; size = 1; break;
5644 case MVT::i16: Reg = X86::AX; size = 2; break;
5645 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005646 case MVT::i64:
5647 if (Subtarget->is64Bit()) {
5648 Reg = X86::RAX; size = 8;
5649 } else //Should go away when LowerType stuff lands
Mon P Wang28873102008-06-25 08:15:39 +00005650 return SDOperand(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005651 break;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005652 };
5653 SDOperand cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Andrew Lenharthce1105d2008-03-01 22:27:48 +00005654 Op.getOperand(3), SDOperand());
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005655 SDOperand Ops[] = { cpIn.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005656 Op.getOperand(1),
5657 Op.getOperand(2),
5658 DAG.getTargetConstant(size, MVT::i8),
5659 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005660 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5661 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5662 SDOperand cpOut =
5663 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5664 return cpOut;
5665}
5666
Mon P Wang28873102008-06-25 08:15:39 +00005667SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005668 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00005669 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005670 SDOperand cpInL, cpInH;
5671 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5672 DAG.getConstant(0, MVT::i32));
5673 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5674 DAG.getConstant(1, MVT::i32));
5675 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
5676 cpInL, SDOperand());
5677 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5678 cpInH, cpInL.getValue(1));
5679 SDOperand swapInL, swapInH;
5680 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5681 DAG.getConstant(0, MVT::i32));
5682 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5683 DAG.getConstant(1, MVT::i32));
5684 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5685 swapInL, cpInH.getValue(1));
5686 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5687 swapInH, swapInL.getValue(1));
5688 SDOperand Ops[] = { swapInH.getValue(0),
5689 Op->getOperand(1),
5690 swapInH.getValue(1)};
5691 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
5692 SDOperand Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5693 SDOperand cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
5694 Result.getValue(1));
5695 SDOperand cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
5696 cpOutL.getValue(2));
5697 SDOperand OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5698 SDOperand ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
Duncan Sandsf9516202008-06-30 10:19:09 +00005699 SDOperand Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005700 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005701}
5702
Mon P Wang28873102008-06-25 08:15:39 +00005703SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005704 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00005705 assert (T == MVT::i32 && "Only know how to expand i32 Atomic Load Sub");
Mon P Wang63307c32008-05-05 19:05:59 +00005706 SDOperand negOp = DAG.getNode(ISD::SUB, T,
5707 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00005708 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005709 Op->getOperand(1), negOp,
Mon P Wang28873102008-06-25 08:15:39 +00005710 cast<AtomicSDNode>(Op)->getSrcValue(),
5711 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang63307c32008-05-05 19:05:59 +00005712}
5713
Evan Cheng0db9fe62006-04-25 20:13:52 +00005714/// LowerOperation - Provide custom lowering hooks for some operations.
5715///
5716SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5717 switch (Op.getOpcode()) {
5718 default: assert(0 && "Should not custom lower this!");
Mon P Wang28873102008-06-25 08:15:39 +00005719 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005720 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5721 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5722 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5723 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5724 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5725 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5726 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005727 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005728 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5729 case ISD::SHL_PARTS:
5730 case ISD::SRA_PARTS:
5731 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5732 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5733 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5734 case ISD::FABS: return LowerFABS(Op, DAG);
5735 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005736 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005737 case ISD::SETCC: return LowerSETCC(Op, DAG);
5738 case ISD::SELECT: return LowerSELECT(Op, DAG);
5739 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005740 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005741 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005742 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005743 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005744 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00005745 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005746 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005747 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005748 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5749 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005750 case ISD::FRAME_TO_ARGS_OFFSET:
5751 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005752 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005753 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005754 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005755 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005756 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5757 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005758
5759 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5760 case ISD::READCYCLECOUNTER:
5761 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005762 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005763}
5764
5765/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5766SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5767 switch (N->getOpcode()) {
5768 default: assert(0 && "Should not custom lower this!");
5769 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5770 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang28873102008-06-25 08:15:39 +00005771 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5772 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005773 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005774}
5775
Evan Cheng72261582005-12-20 06:22:03 +00005776const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5777 switch (Opcode) {
5778 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005779 case X86ISD::BSF: return "X86ISD::BSF";
5780 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005781 case X86ISD::SHLD: return "X86ISD::SHLD";
5782 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005783 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005784 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005785 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005786 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005787 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005788 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005789 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5790 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5791 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005792 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005793 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00005794 case X86ISD::CALL: return "X86ISD::CALL";
5795 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5796 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5797 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005798 case X86ISD::COMI: return "X86ISD::COMI";
5799 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005800 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005801 case X86ISD::CMOV: return "X86ISD::CMOV";
5802 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005803 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005804 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5805 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005806 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005807 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005808 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00005809 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00005810 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
5811 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00005812 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005813 case X86ISD::FMAX: return "X86ISD::FMAX";
5814 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005815 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5816 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005817 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5818 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005819 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005820 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005821 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00005822 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
5823 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00005824 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
5825 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00005826 case X86ISD::VSHL: return "X86ISD::VSHL";
5827 case X86ISD::VSRL: return "X86ISD::VSRL";
Evan Cheng72261582005-12-20 06:22:03 +00005828 }
5829}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005830
Chris Lattnerc9addb72007-03-30 23:15:24 +00005831// isLegalAddressingMode - Return true if the addressing mode represented
5832// by AM is legal for this target, for a load/store of the specified type.
5833bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5834 const Type *Ty) const {
5835 // X86 supports extremely general addressing modes.
5836
5837 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5838 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5839 return false;
5840
5841 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005842 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005843 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5844 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005845
5846 // X86-64 only supports addr of globals in small code model.
5847 if (Subtarget->is64Bit()) {
5848 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5849 return false;
5850 // If lower 4G is not available, then we must use rip-relative addressing.
5851 if (AM.BaseOffs || AM.Scale > 1)
5852 return false;
5853 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005854 }
5855
5856 switch (AM.Scale) {
5857 case 0:
5858 case 1:
5859 case 2:
5860 case 4:
5861 case 8:
5862 // These scales always work.
5863 break;
5864 case 3:
5865 case 5:
5866 case 9:
5867 // These scales are formed with basereg+scalereg. Only accept if there is
5868 // no basereg yet.
5869 if (AM.HasBaseReg)
5870 return false;
5871 break;
5872 default: // Other stuff never works.
5873 return false;
5874 }
5875
5876 return true;
5877}
5878
5879
Evan Cheng2bd122c2007-10-26 01:56:11 +00005880bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5881 if (!Ty1->isInteger() || !Ty2->isInteger())
5882 return false;
Evan Chenge127a732007-10-29 07:57:50 +00005883 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5884 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00005885 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00005886 return false;
5887 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00005888}
5889
Duncan Sands83ec4b62008-06-06 12:08:01 +00005890bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
5891 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005892 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005893 unsigned NumBits1 = VT1.getSizeInBits();
5894 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00005895 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005896 return false;
5897 return Subtarget->is64Bit() || NumBits1 < 64;
5898}
Evan Cheng2bd122c2007-10-26 01:56:11 +00005899
Evan Cheng60c07e12006-07-05 22:17:51 +00005900/// isShuffleMaskLegal - Targets can use this to indicate that they only
5901/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5902/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5903/// are assumed to be legal.
5904bool
Duncan Sands83ec4b62008-06-06 12:08:01 +00005905X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00005906 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005907 if (VT.getSizeInBits() == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00005908 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005909 isIdentityMask(Mask.Val) ||
5910 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005911 isSplatMask(Mask.Val) ||
5912 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5913 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005914 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005915 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005916 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005917}
5918
Dan Gohman7d8143f2008-04-09 20:09:42 +00005919bool
5920X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDOperand> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00005921 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00005922 unsigned NumElts = BVOps.size();
5923 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00005924 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00005925 if (NumElts == 2) return true;
5926 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005927 return (isMOVLMask(&BVOps[0], 4) ||
5928 isCommutedMOVL(&BVOps[0], 4, true) ||
5929 isSHUFPMask(&BVOps[0], 4) ||
5930 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005931 }
5932 return false;
5933}
5934
5935//===----------------------------------------------------------------------===//
5936// X86 Scheduler Hooks
5937//===----------------------------------------------------------------------===//
5938
Mon P Wang63307c32008-05-05 19:05:59 +00005939// private utility function
5940MachineBasicBlock *
5941X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
5942 MachineBasicBlock *MBB,
5943 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00005944 unsigned immOpc,
5945 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00005946 // For the atomic bitwise operator, we generate
5947 // thisMBB:
5948 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00005949 // ld t1 = [bitinstr.addr]
5950 // op t2 = t1, [bitinstr.val]
5951 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00005952 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
5953 // bz newMBB
5954 // fallthrough -->nextMBB
5955 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
5956 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
5957 ilist<MachineBasicBlock>::iterator MBBIter = MBB;
5958 ++MBBIter;
5959
5960 /// First build the CFG
5961 MachineFunction *F = MBB->getParent();
5962 MachineBasicBlock *thisMBB = MBB;
5963 MachineBasicBlock *newMBB = new MachineBasicBlock(LLVM_BB);
5964 MachineBasicBlock *nextMBB = new MachineBasicBlock(LLVM_BB);
5965 F->getBasicBlockList().insert(MBBIter, newMBB);
5966 F->getBasicBlockList().insert(MBBIter, nextMBB);
5967
5968 // Move all successors to thisMBB to nextMBB
5969 nextMBB->transferSuccessors(thisMBB);
5970
5971 // Update thisMBB to fall through to newMBB
5972 thisMBB->addSuccessor(newMBB);
5973
5974 // newMBB jumps to itself and fall through to nextMBB
5975 newMBB->addSuccessor(nextMBB);
5976 newMBB->addSuccessor(newMBB);
5977
5978 // Insert instructions into newMBB based on incoming instruction
5979 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
5980 MachineOperand& destOper = bInstr->getOperand(0);
5981 MachineOperand* argOpers[6];
5982 int numArgs = bInstr->getNumOperands() - 1;
5983 for (int i=0; i < numArgs; ++i)
5984 argOpers[i] = &bInstr->getOperand(i+1);
5985
5986 // x86 address has 4 operands: base, index, scale, and displacement
5987 int lastAddrIndx = 3; // [0,3]
5988 int valArgIndx = 4;
5989
Mon P Wangab3e7472008-05-05 22:56:23 +00005990 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
5991 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00005992 for (int i=0; i <= lastAddrIndx; ++i)
5993 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00005994
5995 unsigned tt = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
5996 if (invSrc) {
5997 MIB = BuildMI(newMBB, TII->get(X86::NOT32r), tt).addReg(t1);
5998 }
5999 else
6000 tt = t1;
6001
Mon P Wang63307c32008-05-05 19:05:59 +00006002 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6003 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6004 && "invalid operand");
6005 if (argOpers[valArgIndx]->isReg())
6006 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6007 else
6008 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006009 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00006010 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006011
Mon P Wangab3e7472008-05-05 22:56:23 +00006012 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6013 MIB.addReg(t1);
6014
Mon P Wang63307c32008-05-05 19:05:59 +00006015 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6016 for (int i=0; i <= lastAddrIndx; ++i)
6017 (*MIB).addOperand(*argOpers[i]);
6018 MIB.addReg(t2);
6019
6020 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6021 MIB.addReg(X86::EAX);
6022
6023 // insert branch
6024 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6025
6026 delete bInstr; // The pseudo instruction is gone now.
6027 return nextMBB;
6028}
6029
6030// private utility function
6031MachineBasicBlock *
6032X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6033 MachineBasicBlock *MBB,
6034 unsigned cmovOpc) {
6035 // For the atomic min/max operator, we generate
6036 // thisMBB:
6037 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006038 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00006039 // mov t2 = [min/max.val]
6040 // cmp t1, t2
6041 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00006042 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006043 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6044 // bz newMBB
6045 // fallthrough -->nextMBB
6046 //
6047 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6048 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6049 ilist<MachineBasicBlock>::iterator MBBIter = MBB;
6050 ++MBBIter;
6051
6052 /// First build the CFG
6053 MachineFunction *F = MBB->getParent();
6054 MachineBasicBlock *thisMBB = MBB;
6055 MachineBasicBlock *newMBB = new MachineBasicBlock(LLVM_BB);
6056 MachineBasicBlock *nextMBB = new MachineBasicBlock(LLVM_BB);
6057 F->getBasicBlockList().insert(MBBIter, newMBB);
6058 F->getBasicBlockList().insert(MBBIter, nextMBB);
6059
6060 // Move all successors to thisMBB to nextMBB
6061 nextMBB->transferSuccessors(thisMBB);
6062
6063 // Update thisMBB to fall through to newMBB
6064 thisMBB->addSuccessor(newMBB);
6065
6066 // newMBB jumps to newMBB and fall through to nextMBB
6067 newMBB->addSuccessor(nextMBB);
6068 newMBB->addSuccessor(newMBB);
6069
6070 // Insert instructions into newMBB based on incoming instruction
6071 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6072 MachineOperand& destOper = mInstr->getOperand(0);
6073 MachineOperand* argOpers[6];
6074 int numArgs = mInstr->getNumOperands() - 1;
6075 for (int i=0; i < numArgs; ++i)
6076 argOpers[i] = &mInstr->getOperand(i+1);
6077
6078 // x86 address has 4 operands: base, index, scale, and displacement
6079 int lastAddrIndx = 3; // [0,3]
6080 int valArgIndx = 4;
6081
Mon P Wangab3e7472008-05-05 22:56:23 +00006082 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6083 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006084 for (int i=0; i <= lastAddrIndx; ++i)
6085 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00006086
Mon P Wang63307c32008-05-05 19:05:59 +00006087 // We only support register and immediate values
6088 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6089 && "invalid operand");
6090
6091 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6092 if (argOpers[valArgIndx]->isReg())
6093 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6094 else
6095 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6096 (*MIB).addOperand(*argOpers[valArgIndx]);
6097
Mon P Wangab3e7472008-05-05 22:56:23 +00006098 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6099 MIB.addReg(t1);
6100
Mon P Wang63307c32008-05-05 19:05:59 +00006101 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6102 MIB.addReg(t1);
6103 MIB.addReg(t2);
6104
6105 // Generate movc
6106 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6107 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6108 MIB.addReg(t2);
6109 MIB.addReg(t1);
6110
6111 // Cmp and exchange if none has modified the memory location
6112 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6113 for (int i=0; i <= lastAddrIndx; ++i)
6114 (*MIB).addOperand(*argOpers[i]);
6115 MIB.addReg(t3);
6116
6117 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6118 MIB.addReg(X86::EAX);
6119
6120 // insert branch
6121 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6122
6123 delete mInstr; // The pseudo instruction is gone now.
6124 return nextMBB;
6125}
6126
6127
Evan Cheng60c07e12006-07-05 22:17:51 +00006128MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006129X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6130 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00006131 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00006132 switch (MI->getOpcode()) {
6133 default: assert(false && "Unexpected instr type to insert");
6134 case X86::CMOV_FR32:
6135 case X86::CMOV_FR64:
6136 case X86::CMOV_V4F32:
6137 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00006138 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006139 // To "insert" a SELECT_CC instruction, we actually have to insert the
6140 // diamond control-flow pattern. The incoming instruction knows the
6141 // destination vreg to set, the condition code register to branch on, the
6142 // true/false values to select between, and a branch opcode to use.
6143 const BasicBlock *LLVM_BB = BB->getBasicBlock();
6144 ilist<MachineBasicBlock>::iterator It = BB;
6145 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006146
Evan Cheng60c07e12006-07-05 22:17:51 +00006147 // thisMBB:
6148 // ...
6149 // TrueVal = ...
6150 // cmpTY ccX, r1, r2
6151 // bCC copy1MBB
6152 // fallthrough --> copy0MBB
6153 MachineBasicBlock *thisMBB = BB;
6154 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
6155 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006156 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00006157 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00006158 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00006159 MachineFunction *F = BB->getParent();
6160 F->getBasicBlockList().insert(It, copy0MBB);
6161 F->getBasicBlockList().insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006162 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00006163 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00006164 sinkMBB->transferSuccessors(BB);
6165
6166 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00006167 BB->addSuccessor(copy0MBB);
6168 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006169
Evan Cheng60c07e12006-07-05 22:17:51 +00006170 // copy0MBB:
6171 // %FalseValue = ...
6172 // # fallthrough to sinkMBB
6173 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006174
Evan Cheng60c07e12006-07-05 22:17:51 +00006175 // Update machine-CFG edges
6176 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006177
Evan Cheng60c07e12006-07-05 22:17:51 +00006178 // sinkMBB:
6179 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6180 // ...
6181 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00006182 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00006183 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6184 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6185
6186 delete MI; // The pseudo instruction is gone now.
6187 return BB;
6188 }
6189
Dale Johannesen849f2142007-07-03 00:53:03 +00006190 case X86::FP32_TO_INT16_IN_MEM:
6191 case X86::FP32_TO_INT32_IN_MEM:
6192 case X86::FP32_TO_INT64_IN_MEM:
6193 case X86::FP64_TO_INT16_IN_MEM:
6194 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00006195 case X86::FP64_TO_INT64_IN_MEM:
6196 case X86::FP80_TO_INT16_IN_MEM:
6197 case X86::FP80_TO_INT32_IN_MEM:
6198 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006199 // Change the floating point control register to use "round towards zero"
6200 // mode when truncating to an integer value.
6201 MachineFunction *F = BB->getParent();
6202 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006203 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006204
6205 // Load the old value of the high byte of the control word...
6206 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00006207 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006208 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006209
6210 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006211 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6212 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00006213
6214 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006215 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006216
6217 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00006218 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6219 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00006220
6221 // Get the X86 opcode to use.
6222 unsigned Opc;
6223 switch (MI->getOpcode()) {
6224 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00006225 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6226 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6227 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6228 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6229 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6230 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00006231 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6232 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6233 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00006234 }
6235
6236 X86AddressMode AM;
6237 MachineOperand &Op = MI->getOperand(0);
6238 if (Op.isRegister()) {
6239 AM.BaseType = X86AddressMode::RegBase;
6240 AM.Base.Reg = Op.getReg();
6241 } else {
6242 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00006243 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00006244 }
6245 Op = MI->getOperand(1);
6246 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006247 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006248 Op = MI->getOperand(2);
6249 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006250 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006251 Op = MI->getOperand(3);
6252 if (Op.isGlobalAddress()) {
6253 AM.GV = Op.getGlobal();
6254 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00006255 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006256 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00006257 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6258 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00006259
6260 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00006261 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006262
6263 delete MI; // The pseudo instruction is gone now.
6264 return BB;
6265 }
Mon P Wang63307c32008-05-05 19:05:59 +00006266 case X86::ATOMAND32:
6267 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6268 X86::AND32ri);
6269 case X86::ATOMOR32:
6270 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
6271 X86::OR32ri);
6272 case X86::ATOMXOR32:
6273 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
6274 X86::XOR32ri);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006275 case X86::ATOMNAND32:
6276 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
6277 X86::AND32ri, true);
Mon P Wang63307c32008-05-05 19:05:59 +00006278 case X86::ATOMMIN32:
6279 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6280 case X86::ATOMMAX32:
6281 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6282 case X86::ATOMUMIN32:
6283 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6284 case X86::ATOMUMAX32:
6285 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Evan Cheng60c07e12006-07-05 22:17:51 +00006286 }
6287}
6288
6289//===----------------------------------------------------------------------===//
6290// X86 Optimization Hooks
6291//===----------------------------------------------------------------------===//
6292
Nate Begeman368e18d2006-02-16 21:11:51 +00006293void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006294 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006295 APInt &KnownZero,
6296 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006297 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00006298 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006299 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00006300 assert((Opc >= ISD::BUILTIN_OP_END ||
6301 Opc == ISD::INTRINSIC_WO_CHAIN ||
6302 Opc == ISD::INTRINSIC_W_CHAIN ||
6303 Opc == ISD::INTRINSIC_VOID) &&
6304 "Should use MaskedValueIsZero if you don't know whether Op"
6305 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006306
Dan Gohmanf4f92f52008-02-13 23:07:24 +00006307 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006308 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00006309 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006310 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006311 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6312 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00006313 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006314 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006315}
Chris Lattner259e97c2006-01-31 19:43:35 +00006316
Evan Cheng206ee9d2006-07-07 08:33:52 +00006317/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00006318/// node is a GlobalAddress + offset.
6319bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6320 GlobalValue* &GA, int64_t &Offset) const{
6321 if (N->getOpcode() == X86ISD::Wrapper) {
6322 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006323 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6324 return true;
6325 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00006326 }
Evan Chengad4196b2008-05-12 19:56:52 +00006327 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006328}
6329
Evan Chengad4196b2008-05-12 19:56:52 +00006330static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6331 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006332 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00006333 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00006334 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006335 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00006336 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00006337 return false;
6338}
6339
Evan Cheng7e2ff772008-05-08 00:57:18 +00006340static bool EltsFromConsecutiveLoads(SDNode *N, SDOperand PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006341 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00006342 SDNode *&Base,
6343 SelectionDAG &DAG, MachineFrameInfo *MFI,
6344 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006345 Base = NULL;
6346 for (unsigned i = 0; i < NumElems; ++i) {
6347 SDOperand Idx = PermMask.getOperand(i);
6348 if (Idx.getOpcode() == ISD::UNDEF) {
6349 if (!Base)
6350 return false;
6351 continue;
6352 }
6353
Evan Chengab262272008-06-25 20:52:59 +00006354 SDOperand Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006355 if (!Elt.Val ||
6356 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6357 return false;
6358 if (!Base) {
6359 Base = Elt.Val;
Evan Cheng50d9e722008-05-10 06:46:49 +00006360 if (Base->getOpcode() == ISD::UNDEF)
6361 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00006362 continue;
6363 }
6364 if (Elt.getOpcode() == ISD::UNDEF)
6365 continue;
6366
Evan Chengad4196b2008-05-12 19:56:52 +00006367 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006368 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006369 return false;
6370 }
6371 return true;
6372}
Evan Cheng206ee9d2006-07-07 08:33:52 +00006373
6374/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6375/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6376/// if the load addresses are consecutive, non-overlapping, and in the right
6377/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00006378static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006379 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006380 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006381 MVT VT = N->getValueType(0);
6382 MVT EVT = VT.getVectorElementType();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006383 SDOperand PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00006384 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006385 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00006386 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6387 DAG, MFI, TLI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006388 return SDOperand();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006389
Dan Gohmand3006222007-07-27 17:16:43 +00006390 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengad4196b2008-05-12 19:56:52 +00006391 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Evan Cheng466685d2006-10-09 20:57:25 +00006392 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00006393 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng71f489d2008-05-05 22:12:23 +00006394 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6395 LD->getSrcValueOffset(), LD->isVolatile(),
6396 LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00006397}
6398
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006399/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Evan Chengd880b972008-05-09 21:53:03 +00006400static SDOperand PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006401 const X86Subtarget *Subtarget,
6402 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00006403 unsigned NumOps = N->getNumOperands();
6404
Evan Chengd880b972008-05-09 21:53:03 +00006405 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00006406 if (NumOps == 1)
Evan Chengd880b972008-05-09 21:53:03 +00006407 return SDOperand();
6408
Duncan Sands83ec4b62008-06-06 12:08:01 +00006409 MVT VT = N->getValueType(0);
6410 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00006411 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6412 // We are looking for load i64 and zero extend. We want to transform
6413 // it before legalizer has a chance to expand it. Also look for i64
6414 // BUILD_PAIR bit casted to f64.
6415 return SDOperand();
6416 // This must be an insertion into a zero vector.
6417 SDOperand HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00006418 if (!isZeroNode(HighElt))
Evan Chengd880b972008-05-09 21:53:03 +00006419 return SDOperand();
6420
6421 // Value must be a load.
Evan Chengd880b972008-05-09 21:53:03 +00006422 SDNode *Base = N->getOperand(0).Val;
6423 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006424 if (Base->getOpcode() != ISD::BIT_CONVERT)
Evan Chengd880b972008-05-09 21:53:03 +00006425 return SDOperand();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006426 Base = Base->getOperand(0).Val;
6427 if (!isa<LoadSDNode>(Base))
Evan Chengd880b972008-05-09 21:53:03 +00006428 return SDOperand();
6429 }
Evan Chengd880b972008-05-09 21:53:03 +00006430
6431 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006432 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00006433
6434 // Load must not be an extload.
6435 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
6436 return SDOperand();
6437
Evan Chengd880b972008-05-09 21:53:03 +00006438 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6439}
6440
Chris Lattner83e6c992006-10-04 06:57:07 +00006441/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
6442static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
6443 const X86Subtarget *Subtarget) {
6444 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006445
Chris Lattner83e6c992006-10-04 06:57:07 +00006446 // If we have SSE[12] support, try to form min/max nodes.
6447 if (Subtarget->hasSSE2() &&
6448 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6449 if (Cond.getOpcode() == ISD::SETCC) {
6450 // Get the LHS/RHS of the select.
6451 SDOperand LHS = N->getOperand(1);
6452 SDOperand RHS = N->getOperand(2);
6453 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006454
Evan Cheng8ca29322006-11-10 21:43:37 +00006455 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00006456 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006457 switch (CC) {
6458 default: break;
6459 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6460 case ISD::SETULE:
6461 case ISD::SETLE:
6462 if (!UnsafeFPMath) break;
6463 // FALL THROUGH.
6464 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6465 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006466 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006467 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006468
Chris Lattner1907a7b2006-10-05 04:11:26 +00006469 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6470 case ISD::SETUGT:
6471 case ISD::SETGT:
6472 if (!UnsafeFPMath) break;
6473 // FALL THROUGH.
6474 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6475 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006476 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006477 break;
6478 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006479 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006480 switch (CC) {
6481 default: break;
6482 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6483 case ISD::SETUGT:
6484 case ISD::SETGT:
6485 if (!UnsafeFPMath) break;
6486 // FALL THROUGH.
6487 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6488 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006489 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006490 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006491
Chris Lattner1907a7b2006-10-05 04:11:26 +00006492 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6493 case ISD::SETULE:
6494 case ISD::SETLE:
6495 if (!UnsafeFPMath) break;
6496 // FALL THROUGH.
6497 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6498 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006499 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006500 break;
6501 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006502 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006503
Evan Cheng8ca29322006-11-10 21:43:37 +00006504 if (Opcode)
6505 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00006506 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006507
Chris Lattner83e6c992006-10-04 06:57:07 +00006508 }
6509
6510 return SDOperand();
6511}
6512
Chris Lattner149a4e52008-02-22 02:09:43 +00006513/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006514static SDOperand PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00006515 const X86Subtarget *Subtarget) {
6516 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6517 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00006518 // A preferable solution to the general problem is to figure out the right
6519 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006520 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006521 if (St->getValue().getValueType().isVector() &&
6522 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00006523 isa<LoadSDNode>(St->getValue()) &&
6524 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6525 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006526 SDNode* LdVal = St->getValue().Val;
Dale Johannesen079f2a62008-02-25 19:20:14 +00006527 LoadSDNode *Ld = 0;
6528 int TokenFactorIndex = -1;
6529 SmallVector<SDOperand, 8> Ops;
6530 SDNode* ChainVal = St->getChain().Val;
6531 // Must be a store of a load. We currently handle two cases: the load
6532 // is a direct child, and it's under an intervening TokenFactor. It is
6533 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006534 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00006535 Ld = cast<LoadSDNode>(St->getChain());
6536 else if (St->getValue().hasOneUse() &&
6537 ChainVal->getOpcode() == ISD::TokenFactor) {
6538 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006539 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00006540 TokenFactorIndex = i;
6541 Ld = cast<LoadSDNode>(St->getValue());
6542 } else
6543 Ops.push_back(ChainVal->getOperand(i));
6544 }
6545 }
6546 if (Ld) {
6547 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6548 if (Subtarget->is64Bit()) {
6549 SDOperand NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
6550 Ld->getBasePtr(), Ld->getSrcValue(),
6551 Ld->getSrcValueOffset(), Ld->isVolatile(),
6552 Ld->getAlignment());
6553 SDOperand NewChain = NewLd.getValue(1);
6554 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00006555 Ops.push_back(NewChain);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006556 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6557 Ops.size());
6558 }
6559 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6560 St->getSrcValue(), St->getSrcValueOffset(),
6561 St->isVolatile(), St->getAlignment());
6562 }
6563
6564 // Otherwise, lower to two 32-bit copies.
6565 SDOperand LoAddr = Ld->getBasePtr();
6566 SDOperand HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006567 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006568
6569 SDOperand LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
6570 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6571 Ld->isVolatile(), Ld->getAlignment());
6572 SDOperand HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
6573 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6574 Ld->isVolatile(),
6575 MinAlign(Ld->getAlignment(), 4));
6576
6577 SDOperand NewChain = LoLd.getValue(1);
6578 if (TokenFactorIndex != -1) {
6579 Ops.push_back(LoLd);
6580 Ops.push_back(HiLd);
6581 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6582 Ops.size());
6583 }
6584
6585 LoAddr = St->getBasePtr();
6586 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006587 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006588
6589 SDOperand LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00006590 St->getSrcValue(), St->getSrcValueOffset(),
6591 St->isVolatile(), St->getAlignment());
Dale Johannesen079f2a62008-02-25 19:20:14 +00006592 SDOperand HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
6593 St->getSrcValue(), St->getSrcValueOffset()+4,
6594 St->isVolatile(),
6595 MinAlign(St->getAlignment(), 4));
6596 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00006597 }
Chris Lattner149a4e52008-02-22 02:09:43 +00006598 }
6599 return SDOperand();
6600}
6601
Chris Lattner6cf73262008-01-25 06:14:17 +00006602/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6603/// X86ISD::FXOR nodes.
Chris Lattneraf723b92008-01-25 05:46:26 +00006604static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00006605 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6606 // F[X]OR(0.0, x) -> x
6607 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00006608 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6609 if (C->getValueAPF().isPosZero())
6610 return N->getOperand(1);
6611 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6612 if (C->getValueAPF().isPosZero())
6613 return N->getOperand(0);
6614 return SDOperand();
6615}
6616
6617/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
6618static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
6619 // FAND(0.0, x) -> 0.0
6620 // FAND(x, 0.0) -> 0.0
6621 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6622 if (C->getValueAPF().isPosZero())
6623 return N->getOperand(0);
6624 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6625 if (C->getValueAPF().isPosZero())
6626 return N->getOperand(1);
6627 return SDOperand();
6628}
6629
Chris Lattner83e6c992006-10-04 06:57:07 +00006630
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006631SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00006632 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006633 SelectionDAG &DAG = DCI.DAG;
6634 switch (N->getOpcode()) {
6635 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00006636 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6637 case ISD::BUILD_VECTOR:
6638 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00006639 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006640 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00006641 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00006642 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6643 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006644 }
6645
6646 return SDOperand();
6647}
6648
Evan Cheng60c07e12006-07-05 22:17:51 +00006649//===----------------------------------------------------------------------===//
6650// X86 Inline Assembly Support
6651//===----------------------------------------------------------------------===//
6652
Chris Lattnerf4dff842006-07-11 02:54:03 +00006653/// getConstraintType - Given a constraint letter, return the type of
6654/// constraint it is for this target.
6655X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006656X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6657 if (Constraint.size() == 1) {
6658 switch (Constraint[0]) {
6659 case 'A':
Chris Lattnerfce84ac2008-03-11 19:06:29 +00006660 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00006661 case 'r':
6662 case 'R':
6663 case 'l':
6664 case 'q':
6665 case 'Q':
6666 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00006667 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00006668 case 'Y':
6669 return C_RegisterClass;
6670 default:
6671 break;
6672 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00006673 }
Chris Lattner4234f572007-03-25 02:14:49 +00006674 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00006675}
6676
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006677/// LowerXConstraint - try to replace an X constraint, which matches anything,
6678/// with another that has more specific requirements based on the type of the
6679/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00006680const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00006681LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00006682 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
6683 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006684 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006685 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00006686 return "Y";
6687 if (Subtarget->hasSSE1())
6688 return "x";
6689 }
6690
6691 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006692}
6693
Chris Lattner48884cd2007-08-25 00:47:38 +00006694/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
6695/// vector. If it is invalid, don't add anything to Ops.
6696void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
6697 char Constraint,
6698 std::vector<SDOperand>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00006699 SelectionDAG &DAG) const {
Chris Lattner48884cd2007-08-25 00:47:38 +00006700 SDOperand Result(0, 0);
6701
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006702 switch (Constraint) {
6703 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00006704 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00006705 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006706 if (C->getValue() <= 31) {
6707 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6708 break;
6709 }
Devang Patel84f7fd22007-03-17 00:13:28 +00006710 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006711 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00006712 case 'N':
6713 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00006714 if (C->getValue() <= 255) {
6715 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
6716 break;
6717 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00006718 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006719 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00006720 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006721 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00006722 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
6723 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
6724 break;
6725 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006726
Chris Lattnerdc43a882007-05-03 16:52:29 +00006727 // If we are in non-pic codegen mode, we allow the address of a global (with
6728 // an optional displacement) to be used with 'i'.
6729 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
6730 int64_t Offset = 0;
6731
6732 // Match either (GA) or (GA+C)
6733 if (GA) {
6734 Offset = GA->getOffset();
6735 } else if (Op.getOpcode() == ISD::ADD) {
6736 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6737 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6738 if (C && GA) {
6739 Offset = GA->getOffset()+C->getValue();
6740 } else {
6741 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
6742 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
6743 if (C && GA)
6744 Offset = GA->getOffset()+C->getValue();
6745 else
6746 C = 0, GA = 0;
6747 }
6748 }
6749
6750 if (GA) {
6751 // If addressing this global requires a load (e.g. in PIC mode), we can't
6752 // match.
6753 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
6754 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00006755 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006756
Chris Lattnerdc43a882007-05-03 16:52:29 +00006757 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
6758 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00006759 Result = Op;
6760 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006761 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006762
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006763 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00006764 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006765 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00006766 }
Chris Lattner48884cd2007-08-25 00:47:38 +00006767
6768 if (Result.Val) {
6769 Ops.push_back(Result);
6770 return;
6771 }
6772 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00006773}
6774
Chris Lattner259e97c2006-01-31 19:43:35 +00006775std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00006776getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006777 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00006778 if (Constraint.size() == 1) {
6779 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00006780 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00006781 default: break; // Unknown constraint letter
6782 case 'A': // EAX/EDX
6783 if (VT == MVT::i32 || VT == MVT::i64)
6784 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
6785 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006786 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
6787 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00006788 if (VT == MVT::i32)
6789 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
6790 else if (VT == MVT::i16)
6791 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
6792 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00006793 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00006794 else if (VT == MVT::i64)
6795 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
6796 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00006797 }
6798 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006799
Chris Lattner1efa40f2006-02-22 00:56:39 +00006800 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00006801}
Chris Lattnerf76d1802006-07-31 23:26:50 +00006802
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006803std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00006804X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006805 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00006806 // First, see if this is a constraint that directly corresponds to an LLVM
6807 // register class.
6808 if (Constraint.size() == 1) {
6809 // GCC Constraint Letters
6810 switch (Constraint[0]) {
6811 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006812 case 'r': // GENERAL_REGS
6813 case 'R': // LEGACY_REGS
6814 case 'l': // INDEX_REGS
6815 if (VT == MVT::i64 && Subtarget->is64Bit())
6816 return std::make_pair(0U, X86::GR64RegisterClass);
6817 if (VT == MVT::i32)
6818 return std::make_pair(0U, X86::GR32RegisterClass);
6819 else if (VT == MVT::i16)
6820 return std::make_pair(0U, X86::GR16RegisterClass);
6821 else if (VT == MVT::i8)
6822 return std::make_pair(0U, X86::GR8RegisterClass);
6823 break;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00006824 case 'f': // FP Stack registers.
6825 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
6826 // value to the correct fpstack register class.
6827 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
6828 return std::make_pair(0U, X86::RFP32RegisterClass);
6829 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
6830 return std::make_pair(0U, X86::RFP64RegisterClass);
6831 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00006832 case 'y': // MMX_REGS if MMX allowed.
6833 if (!Subtarget->hasMMX()) break;
6834 return std::make_pair(0U, X86::VR64RegisterClass);
6835 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00006836 case 'Y': // SSE_REGS if SSE2 allowed
6837 if (!Subtarget->hasSSE2()) break;
6838 // FALL THROUGH.
6839 case 'x': // SSE_REGS if SSE1 allowed
6840 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006841
6842 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00006843 default: break;
6844 // Scalar SSE types.
6845 case MVT::f32:
6846 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00006847 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006848 case MVT::f64:
6849 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00006850 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00006851 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00006852 case MVT::v16i8:
6853 case MVT::v8i16:
6854 case MVT::v4i32:
6855 case MVT::v2i64:
6856 case MVT::v4f32:
6857 case MVT::v2f64:
6858 return std::make_pair(0U, X86::VR128RegisterClass);
6859 }
Chris Lattnerad043e82007-04-09 05:11:28 +00006860 break;
6861 }
6862 }
6863
Chris Lattnerf76d1802006-07-31 23:26:50 +00006864 // Use the default implementation in TargetLowering to convert the register
6865 // constraint into a member of a register class.
6866 std::pair<unsigned, const TargetRegisterClass*> Res;
6867 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00006868
6869 // Not found as a standard register?
6870 if (Res.second == 0) {
6871 // GCC calls "st(0)" just plain "st".
6872 if (StringsEqualNoCase("{st}", Constraint)) {
6873 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00006874 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00006875 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006876
Chris Lattner1a60aa72006-10-31 19:42:44 +00006877 return Res;
6878 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006879
Chris Lattnerf76d1802006-07-31 23:26:50 +00006880 // Otherwise, check to see if this is a register class of the wrong value
6881 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6882 // turn into {ax},{dx}.
6883 if (Res.second->hasType(VT))
6884 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006885
Chris Lattnerf76d1802006-07-31 23:26:50 +00006886 // All of the single-register GCC register classes map their values onto
6887 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6888 // really want an 8-bit or 32-bit register, map to the appropriate register
6889 // class and return the appropriate register.
6890 if (Res.second != X86::GR16RegisterClass)
6891 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006892
Chris Lattnerf76d1802006-07-31 23:26:50 +00006893 if (VT == MVT::i8) {
6894 unsigned DestReg = 0;
6895 switch (Res.first) {
6896 default: break;
6897 case X86::AX: DestReg = X86::AL; break;
6898 case X86::DX: DestReg = X86::DL; break;
6899 case X86::CX: DestReg = X86::CL; break;
6900 case X86::BX: DestReg = X86::BL; break;
6901 }
6902 if (DestReg) {
6903 Res.first = DestReg;
6904 Res.second = Res.second = X86::GR8RegisterClass;
6905 }
6906 } else if (VT == MVT::i32) {
6907 unsigned DestReg = 0;
6908 switch (Res.first) {
6909 default: break;
6910 case X86::AX: DestReg = X86::EAX; break;
6911 case X86::DX: DestReg = X86::EDX; break;
6912 case X86::CX: DestReg = X86::ECX; break;
6913 case X86::BX: DestReg = X86::EBX; break;
6914 case X86::SI: DestReg = X86::ESI; break;
6915 case X86::DI: DestReg = X86::EDI; break;
6916 case X86::BP: DestReg = X86::EBP; break;
6917 case X86::SP: DestReg = X86::ESP; break;
6918 }
6919 if (DestReg) {
6920 Res.first = DestReg;
6921 Res.second = Res.second = X86::GR32RegisterClass;
6922 }
Evan Cheng25ab6902006-09-08 06:48:29 +00006923 } else if (VT == MVT::i64) {
6924 unsigned DestReg = 0;
6925 switch (Res.first) {
6926 default: break;
6927 case X86::AX: DestReg = X86::RAX; break;
6928 case X86::DX: DestReg = X86::RDX; break;
6929 case X86::CX: DestReg = X86::RCX; break;
6930 case X86::BX: DestReg = X86::RBX; break;
6931 case X86::SI: DestReg = X86::RSI; break;
6932 case X86::DI: DestReg = X86::RDI; break;
6933 case X86::BP: DestReg = X86::RBP; break;
6934 case X86::SP: DestReg = X86::RSP; break;
6935 }
6936 if (DestReg) {
6937 Res.first = DestReg;
6938 Res.second = Res.second = X86::GR64RegisterClass;
6939 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00006940 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006941
Chris Lattnerf76d1802006-07-31 23:26:50 +00006942 return Res;
6943}