blob: af3d0e13818aacf027bce9f915111d11b33ccd3a [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"
28#include "llvm/Analysis/ScalarEvolutionExpressions.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000029#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000034#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000035#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000036#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000037#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000038#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000039#include "llvm/ADT/StringExtras.h"
Duncan Sandsb116fac2007-07-27 20:02:49 +000040#include "llvm/ParameterAttributes.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041using namespace llvm;
42
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000043X86TargetLowering::X86TargetLowering(TargetMachine &TM)
44 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000045 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000046 X86ScalarSSEf64 = Subtarget->hasSSE2();
47 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000048 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000049
Chris Lattnerd43d00c2008-01-24 08:07:48 +000050 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000051
Anton Korobeynikov2365f512007-07-14 14:06:15 +000052 RegInfo = TM.getRegisterInfo();
53
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000054 // Set up the TargetLowering object.
55
56 // X86 is weird, it always uses i8 for shift amounts and setcc results.
57 setShiftAmountType(MVT::i8);
58 setSetCCResultType(MVT::i8);
59 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000060 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000061 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000062 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000063
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000064 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000065 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000066 setUseUnderscoreSetJmp(false);
67 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000068 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 // MS runtime is weird: it exports _setjmp, but longjmp!
70 setUseUnderscoreSetJmp(true);
71 setUseUnderscoreLongJmp(false);
72 } else {
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(true);
75 }
76
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000077 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000078 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
79 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
80 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000081 if (Subtarget->is64Bit())
82 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000083
Duncan Sandsf9c98e62008-01-23 20:39:46 +000084 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000085
Chris Lattnerddf89562008-01-17 19:59:44 +000086 // We don't accept any truncstore of integer registers.
87 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
88 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
89 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
90 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
91 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
92 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
93
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000094 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
95 // operation.
96 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
97 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
98 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +000099
Evan Cheng25ab6902006-09-08 06:48:29 +0000100 if (Subtarget->is64Bit()) {
101 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +0000102 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000103 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000104 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +0000105 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
106 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
107 else
108 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
109 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000110
111 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
112 // this operation.
113 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
114 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000115 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000116 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000117 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000118 // f32 and f64 cases are Legal, f80 case is not
119 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
120 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000121 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000124
Dale Johannesen73328d12007-09-19 23:55:34 +0000125 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
126 // are Legal, f80 is custom lowered.
127 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
128 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000129
Evan Cheng02568ff2006-01-30 22:13:22 +0000130 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
131 // this operation.
132 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
133 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
134
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000135 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000136 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000137 // f32 and f64 cases are Legal, f80 case is not
138 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000139 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000140 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000142 }
143
144 // Handle FP_TO_UINT by promoting the destination to a larger signed
145 // conversion.
146 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
147 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
148 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
149
Evan Cheng25ab6902006-09-08 06:48:29 +0000150 if (Subtarget->is64Bit()) {
151 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000152 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000153 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000154 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000155 // Expand FP_TO_UINT into a select.
156 // FIXME: We would like to use a Custom expander here eventually to do
157 // the optimal thing for SSE vs. the default expansion in the legalizer.
158 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
159 else
160 // With SSE3 we can use fisttpll to convert to a signed i64.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
162 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000163
Chris Lattner399610a2006-12-05 18:22:22 +0000164 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000165 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000166 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
167 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
168 }
Chris Lattner21f66852005-12-23 05:15:23 +0000169
Dan Gohman525178c2007-10-08 18:33:35 +0000170 // Scalar integer multiply, multiply-high, divide, and remainder are
171 // lowered to use operations that produce two results, to match the
172 // available instructions. This exposes the two-result form to trivial
173 // CSE, which is able to combine x/y and x%y into a single instruction,
174 // for example. The single-result multiply instructions are introduced
175 // in X86ISelDAGToDAG.cpp, after CSE, for uses where the the high part
176 // is not needed.
177 setOperationAction(ISD::MUL , MVT::i8 , Expand);
178 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
179 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
180 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
181 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
182 setOperationAction(ISD::SREM , MVT::i8 , Expand);
183 setOperationAction(ISD::UREM , MVT::i8 , Expand);
184 setOperationAction(ISD::MUL , MVT::i16 , Expand);
185 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
186 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
187 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
188 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
189 setOperationAction(ISD::SREM , MVT::i16 , Expand);
190 setOperationAction(ISD::UREM , MVT::i16 , Expand);
191 setOperationAction(ISD::MUL , MVT::i32 , Expand);
192 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
193 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
194 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
195 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
196 setOperationAction(ISD::SREM , MVT::i32 , Expand);
197 setOperationAction(ISD::UREM , MVT::i32 , Expand);
198 setOperationAction(ISD::MUL , MVT::i64 , Expand);
199 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
200 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
201 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
202 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
203 setOperationAction(ISD::SREM , MVT::i64 , Expand);
204 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000205
Evan Chengc35497f2006-10-30 08:02:39 +0000206 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000207 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000208 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
209 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000210 setOperationAction(ISD::MEMMOVE , 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 Lattnerdbdbf0c2005-11-15 00:40:23 +0000217 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000218 setOperationAction(ISD::FLT_ROUNDS , MVT::i32 , Custom);
219
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000220 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000221 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
222 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000229 if (Subtarget->is64Bit()) {
230 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000231 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
232 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000233 }
234
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000235 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000236 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000237
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000238 // These should be promoted to a larger select which is supported.
239 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
240 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000241 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000242 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
243 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
244 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
245 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000246 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000247 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
248 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
249 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
250 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
251 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000252 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000253 if (Subtarget->is64Bit()) {
254 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
255 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
256 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000257 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000258 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000259 if (!Subtarget->is64Bit())
260 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
261
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000262 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000263 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000264 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000265 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000266 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000267 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000268 if (Subtarget->is64Bit()) {
269 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
270 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
271 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
272 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
273 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000274 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000275 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
276 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
277 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000278 // X86 wants to expand memset / memcpy itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000279 setOperationAction(ISD::MEMSET , MVT::Other, Custom);
280 setOperationAction(ISD::MEMCPY , MVT::Other, Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000281
Dan Gohmanc3b0b5c2007-09-25 15:10:49 +0000282 // Use the default ISD::LOCATION expansion.
Chris Lattnerf73bae12005-11-29 06:16:21 +0000283 setOperationAction(ISD::LOCATION, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000284 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000285 if (!Subtarget->isTargetDarwin() &&
286 !Subtarget->isTargetELF() &&
Anton Korobeynikov317848f2007-01-03 11:43:14 +0000287 !Subtarget->isTargetCygMing())
Jim Laskey1ee29252007-01-26 14:34:52 +0000288 setOperationAction(ISD::LABEL, MVT::Other, Expand);
Chris Lattnerf73bae12005-11-29 06:16:21 +0000289
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000290 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
291 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
292 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
293 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
294 if (Subtarget->is64Bit()) {
295 // FIXME: Verify
296 setExceptionPointerRegister(X86::RAX);
297 setExceptionSelectorRegister(X86::RDX);
298 } else {
299 setExceptionPointerRegister(X86::EAX);
300 setExceptionSelectorRegister(X86::EDX);
301 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000302 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000303
Duncan Sandsf7331b32007-09-11 14:10:23 +0000304 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000305
Chris Lattnerda68d302008-01-15 21:58:22 +0000306 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000307
Nate Begemanacc398c2006-01-25 18:21:52 +0000308 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
309 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000310 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Nate Begemanacc398c2006-01-25 18:21:52 +0000311 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000312 if (Subtarget->is64Bit())
313 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
314 else
315 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
316
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000317 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000318 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000319 if (Subtarget->is64Bit())
320 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000321 if (Subtarget->isTargetCygMing())
322 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
323 else
324 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000325
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000326 if (X86ScalarSSEf64) {
327 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000328 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000329 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
330 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000331
Evan Cheng223547a2006-01-31 22:28:30 +0000332 // Use ANDPD to simulate FABS.
333 setOperationAction(ISD::FABS , MVT::f64, Custom);
334 setOperationAction(ISD::FABS , MVT::f32, Custom);
335
336 // Use XORP to simulate FNEG.
337 setOperationAction(ISD::FNEG , MVT::f64, Custom);
338 setOperationAction(ISD::FNEG , MVT::f32, Custom);
339
Evan Cheng68c47cb2007-01-05 07:55:56 +0000340 // Use ANDPD and ORPD to simulate FCOPYSIGN.
341 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
342 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
343
Evan Chengd25e9e82006-02-02 00:28:23 +0000344 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000345 setOperationAction(ISD::FSIN , MVT::f64, Expand);
346 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000347 setOperationAction(ISD::FREM , MVT::f64, Expand);
348 setOperationAction(ISD::FSIN , MVT::f32, Expand);
349 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000350 setOperationAction(ISD::FREM , MVT::f32, Expand);
351
Chris Lattnera54aa942006-01-29 06:26:08 +0000352 // Expand FP immediates into loads from the stack, except for the special
353 // cases we handle.
354 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
355 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000356 addLegalFPImmediate(APFloat(+0.0)); // xorpd
357 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000358
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000359 // Floating truncations from f80 and extensions to f80 go through memory.
360 // If optimizing, we lie about this though and handle it in
361 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
362 if (Fast) {
363 setConvertAction(MVT::f32, MVT::f80, Expand);
364 setConvertAction(MVT::f64, MVT::f80, Expand);
365 setConvertAction(MVT::f80, MVT::f32, Expand);
366 setConvertAction(MVT::f80, MVT::f64, Expand);
367 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000368 } else if (X86ScalarSSEf32) {
369 // Use SSE for f32, x87 for f64.
370 // Set up the FP register classes.
371 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
372 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
373
374 // Use ANDPS to simulate FABS.
375 setOperationAction(ISD::FABS , MVT::f32, Custom);
376
377 // Use XORP to simulate FNEG.
378 setOperationAction(ISD::FNEG , MVT::f32, Custom);
379
380 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
381
382 // Use ANDPS and ORPS to simulate FCOPYSIGN.
383 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
384 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
385
386 // We don't support sin/cos/fmod
387 setOperationAction(ISD::FSIN , MVT::f32, Expand);
388 setOperationAction(ISD::FCOS , MVT::f32, Expand);
389 setOperationAction(ISD::FREM , MVT::f32, Expand);
390
391 // Expand FP immediates into loads from the stack, except for the special
392 // cases we handle.
393 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
394 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
395 addLegalFPImmediate(APFloat(+0.0f)); // xorps
396 addLegalFPImmediate(APFloat(+0.0)); // FLD0
397 addLegalFPImmediate(APFloat(+1.0)); // FLD1
398 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
399 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
400
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000401 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
402 // this though and handle it in InstructionSelectPreprocess so that
403 // dagcombine2 can hack on these.
404 if (Fast) {
405 setConvertAction(MVT::f32, MVT::f64, Expand);
406 setConvertAction(MVT::f32, MVT::f80, Expand);
407 setConvertAction(MVT::f80, MVT::f32, Expand);
408 setConvertAction(MVT::f64, MVT::f32, Expand);
409 // And x87->x87 truncations also.
410 setConvertAction(MVT::f80, MVT::f64, Expand);
411 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000412
413 if (!UnsafeFPMath) {
414 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
415 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
416 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000417 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000418 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000419 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000420 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
421 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000422
Evan Cheng68c47cb2007-01-05 07:55:56 +0000423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000424 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000425 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
426 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000427
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000428 // Floating truncations go through memory. If optimizing, we lie about
429 // this though and handle it in InstructionSelectPreprocess so that
430 // dagcombine2 can hack on these.
431 if (Fast) {
432 setConvertAction(MVT::f80, MVT::f32, Expand);
433 setConvertAction(MVT::f64, MVT::f32, Expand);
434 setConvertAction(MVT::f80, MVT::f64, Expand);
435 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000436
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 if (!UnsafeFPMath) {
438 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
439 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
440 }
441
Chris Lattnera54aa942006-01-29 06:26:08 +0000442 setOperationAction(ISD::ConstantFP, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000443 setOperationAction(ISD::ConstantFP, MVT::f32, Expand);
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000444 addLegalFPImmediate(APFloat(+0.0)); // FLD0
445 addLegalFPImmediate(APFloat(+1.0)); // FLD1
446 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
447 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000448 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
449 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
450 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
451 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000452 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000453
Dale Johannesen59a58732007-08-05 18:49:15 +0000454 // Long double always uses X87.
455 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000456 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
457 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000458 {
459 setOperationAction(ISD::ConstantFP, MVT::f80, Expand);
460 APFloat TmpFlt(+0.0);
461 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
462 addLegalFPImmediate(TmpFlt); // FLD0
463 TmpFlt.changeSign();
464 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
465 APFloat TmpFlt2(+1.0);
466 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
467 addLegalFPImmediate(TmpFlt2); // FLD1
468 TmpFlt2.changeSign();
469 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
470 }
471
Dale Johannesen2f429012007-09-26 21:10:55 +0000472 if (!UnsafeFPMath) {
473 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
474 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
475 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000476
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000477 // Always use a library call for pow.
478 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
479 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
480 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
481
Evan Chengd30bf012006-03-01 01:11:20 +0000482 // First set operation action for all vector types to expand. Then we
483 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000484 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
485 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Evan Chengd30bf012006-03-01 01:11:20 +0000486 setOperationAction(ISD::ADD , (MVT::ValueType)VT, Expand);
487 setOperationAction(ISD::SUB , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000488 setOperationAction(ISD::FADD, (MVT::ValueType)VT, Expand);
Evan Chenga72cb0e2007-06-29 00:18:15 +0000489 setOperationAction(ISD::FNEG, (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000490 setOperationAction(ISD::FSUB, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000491 setOperationAction(ISD::MUL , (MVT::ValueType)VT, Expand);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000492 setOperationAction(ISD::FMUL, (MVT::ValueType)VT, Expand);
493 setOperationAction(ISD::SDIV, (MVT::ValueType)VT, Expand);
494 setOperationAction(ISD::UDIV, (MVT::ValueType)VT, Expand);
495 setOperationAction(ISD::FDIV, (MVT::ValueType)VT, Expand);
496 setOperationAction(ISD::SREM, (MVT::ValueType)VT, Expand);
497 setOperationAction(ISD::UREM, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000498 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000499 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Expand);
Chris Lattner9b3bd462006-03-21 20:51:05 +0000500 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Evan Chengb067a1e2006-03-31 19:22:53 +0000501 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::ValueType)VT, Expand);
Dan Gohman20382522007-07-10 00:05:58 +0000502 setOperationAction(ISD::FABS, (MVT::ValueType)VT, Expand);
503 setOperationAction(ISD::FSIN, (MVT::ValueType)VT, Expand);
504 setOperationAction(ISD::FCOS, (MVT::ValueType)VT, Expand);
505 setOperationAction(ISD::FREM, (MVT::ValueType)VT, Expand);
506 setOperationAction(ISD::FPOWI, (MVT::ValueType)VT, Expand);
507 setOperationAction(ISD::FSQRT, (MVT::ValueType)VT, Expand);
508 setOperationAction(ISD::FCOPYSIGN, (MVT::ValueType)VT, Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000509 setOperationAction(ISD::SMUL_LOHI, (MVT::ValueType)VT, Expand);
510 setOperationAction(ISD::UMUL_LOHI, (MVT::ValueType)VT, Expand);
511 setOperationAction(ISD::SDIVREM, (MVT::ValueType)VT, Expand);
512 setOperationAction(ISD::UDIVREM, (MVT::ValueType)VT, Expand);
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000513 setOperationAction(ISD::FPOW, (MVT::ValueType)VT, Expand);
Dan Gohmanf0d00892007-10-12 14:09:42 +0000514 setOperationAction(ISD::CTPOP, (MVT::ValueType)VT, Expand);
515 setOperationAction(ISD::CTTZ, (MVT::ValueType)VT, Expand);
516 setOperationAction(ISD::CTLZ, (MVT::ValueType)VT, Expand);
Dan Gohman89081322007-12-12 22:21:26 +0000517 setOperationAction(ISD::SHL, (MVT::ValueType)VT, Expand);
518 setOperationAction(ISD::SRA, (MVT::ValueType)VT, Expand);
519 setOperationAction(ISD::SRL, (MVT::ValueType)VT, Expand);
520 setOperationAction(ISD::ROTL, (MVT::ValueType)VT, Expand);
521 setOperationAction(ISD::ROTR, (MVT::ValueType)VT, Expand);
522 setOperationAction(ISD::BSWAP, (MVT::ValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000523 }
524
Evan Chenga88973f2006-03-22 19:22:18 +0000525 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000526 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
527 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
528 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000529 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000530
Evan Chengd30bf012006-03-01 01:11:20 +0000531 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000532
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000533 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
534 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
535 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000536 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000537
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000538 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
539 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
540 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000541 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000542
Bill Wendling74027e92007-03-15 21:24:36 +0000543 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
544 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
545
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000546 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000547 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000548 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000549 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
550 setOperationAction(ISD::AND, MVT::v2i32, Promote);
551 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
552 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000553
554 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000555 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000556 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000557 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
558 setOperationAction(ISD::OR, MVT::v2i32, Promote);
559 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
560 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000561
562 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000563 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000564 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000565 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
566 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
567 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
568 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000569
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000570 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000571 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000572 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000573 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
574 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
575 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
576 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000577
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000578 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
579 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
580 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
581 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000582
583 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
584 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
585 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000586 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000587
588 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
589 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000590 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2i32, Custom);
591 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000592 }
593
Evan Chenga88973f2006-03-22 19:22:18 +0000594 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000595 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
596
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000597 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
598 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
599 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
600 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000601 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
602 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000603 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
604 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
605 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000606 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000607 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000608 }
609
Evan Chenga88973f2006-03-22 19:22:18 +0000610 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000611 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
612 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
613 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
614 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
615 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
616
Evan Chengf7c378e2006-04-10 07:23:14 +0000617 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
618 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
619 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000620 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000621 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
622 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
623 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000624 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000625 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000626 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
627 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
628 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
629 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000630 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
631 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000632
Evan Chengf7c378e2006-04-10 07:23:14 +0000633 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
634 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000635 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000636 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
637 // Implement v4f32 insert_vector_elt in terms of SSE2 v8i16 ones.
638 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000639
Evan Cheng2c3ae372006-04-12 21:21:57 +0000640 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
641 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Nate Begeman844e0f92007-12-11 01:41:33 +0000642 // Do not attempt to custom lower non-power-of-2 vectors
643 if (!isPowerOf2_32(MVT::getVectorNumElements(VT)))
644 continue;
Evan Cheng2c3ae372006-04-12 21:21:57 +0000645 setOperationAction(ISD::BUILD_VECTOR, (MVT::ValueType)VT, Custom);
646 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::ValueType)VT, Custom);
647 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::ValueType)VT, Custom);
648 }
649 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
650 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
651 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
652 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
653 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000654 if (Subtarget->is64Bit())
655 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000656
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000657 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000658 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
659 setOperationAction(ISD::AND, (MVT::ValueType)VT, Promote);
660 AddPromotedToType (ISD::AND, (MVT::ValueType)VT, MVT::v2i64);
661 setOperationAction(ISD::OR, (MVT::ValueType)VT, Promote);
662 AddPromotedToType (ISD::OR, (MVT::ValueType)VT, MVT::v2i64);
663 setOperationAction(ISD::XOR, (MVT::ValueType)VT, Promote);
664 AddPromotedToType (ISD::XOR, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng91b740d2006-04-12 17:12:36 +0000665 setOperationAction(ISD::LOAD, (MVT::ValueType)VT, Promote);
666 AddPromotedToType (ISD::LOAD, (MVT::ValueType)VT, MVT::v2i64);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000667 setOperationAction(ISD::SELECT, (MVT::ValueType)VT, Promote);
668 AddPromotedToType (ISD::SELECT, (MVT::ValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000669 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000670
Chris Lattnerddf89562008-01-17 19:59:44 +0000671 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000672
Evan Cheng2c3ae372006-04-12 21:21:57 +0000673 // Custom lower v2i64 and v2f64 selects.
674 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000675 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000676 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000677 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000678 }
679
Evan Cheng6be2c582006-04-05 23:38:46 +0000680 // We want to custom lower some of our intrinsics.
681 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
682
Evan Cheng206ee9d2006-07-07 08:33:52 +0000683 // We have target-specific dag combine patterns for the following nodes:
684 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Chris Lattner83e6c992006-10-04 06:57:07 +0000685 setTargetDAGCombine(ISD::SELECT);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000686
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000687 computeRegisterProperties();
688
Evan Cheng87ed7162006-02-14 08:25:08 +0000689 // FIXME: These should be based on subtarget info. Plus, the values should
690 // be smaller when we are in optimizing for size mode.
Evan Chenga03a5dc2006-02-14 08:38:30 +0000691 maxStoresPerMemset = 16; // For %llvm.memset -> sequence of stores
692 maxStoresPerMemcpy = 16; // For %llvm.memcpy -> sequence of stores
693 maxStoresPerMemmove = 16; // For %llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000694 allowUnalignedMemoryAccesses = true; // x86 supports it!
695}
696
Evan Cheng29286502008-01-23 23:17:41 +0000697/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
698/// the desired ByVal argument alignment.
699static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
700 if (MaxAlign == 16)
701 return;
702 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
703 if (VTy->getBitWidth() == 128)
704 MaxAlign = 16;
705 else if (VTy->getBitWidth() == 64)
706 if (MaxAlign < 8)
707 MaxAlign = 8;
708 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
709 unsigned EltAlign = 0;
710 getMaxByValAlign(ATy->getElementType(), EltAlign);
711 if (EltAlign > MaxAlign)
712 MaxAlign = EltAlign;
713 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
714 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
715 unsigned EltAlign = 0;
716 getMaxByValAlign(STy->getElementType(i), EltAlign);
717 if (EltAlign > MaxAlign)
718 MaxAlign = EltAlign;
719 if (MaxAlign == 16)
720 break;
721 }
722 }
723 return;
724}
725
726/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
727/// function arguments in the caller parameter area. For X86, aggregates
728/// that contains are placed at 16-byte boundaries while the rest are at
729/// 4-byte boundaries.
730unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
731 if (Subtarget->is64Bit())
732 return getTargetData()->getABITypeAlignment(Ty);
733 unsigned Align = 4;
734 getMaxByValAlign(Ty, Align);
735 return Align;
736}
Chris Lattner2b02a442007-02-25 08:29:00 +0000737
Evan Chengcc415862007-11-09 01:32:10 +0000738/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
739/// jumptable.
740SDOperand X86TargetLowering::getPICJumpTableRelocBase(SDOperand Table,
741 SelectionDAG &DAG) const {
742 if (usesGlobalOffsetTable())
743 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
744 if (!Subtarget->isPICStyleRIPRel())
745 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
746 return Table;
747}
748
Chris Lattner2b02a442007-02-25 08:29:00 +0000749//===----------------------------------------------------------------------===//
750// Return Value Calling Convention Implementation
751//===----------------------------------------------------------------------===//
752
Chris Lattner59ed56b2007-02-28 04:55:35 +0000753#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000754
755/// GetPossiblePreceedingTailCall - Get preceeding X86ISD::TAILCALL node if it
756/// exists skip possible ISD:TokenFactor.
757static SDOperand GetPossiblePreceedingTailCall(SDOperand Chain) {
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000758 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000759 return Chain;
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000760 } else if (Chain.getOpcode() == ISD::TokenFactor) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000761 if (Chain.getNumOperands() &&
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000762 Chain.getOperand(0).getOpcode() == X86ISD::TAILCALL)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000763 return Chain.getOperand(0);
764 }
765 return Chain;
766}
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000767
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000768/// LowerRET - Lower an ISD::RET node.
769SDOperand X86TargetLowering::LowerRET(SDOperand Op, SelectionDAG &DAG) {
770 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
771
Chris Lattner9774c912007-02-27 05:28:59 +0000772 SmallVector<CCValAssign, 16> RVLocs;
773 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000774 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
775 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000776 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000777
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000778 // If this is the first return lowered for this function, add the regs to the
779 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000780 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000781 for (unsigned i = 0; i != RVLocs.size(); ++i)
782 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000783 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000784 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000785 SDOperand Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000786
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000787 // Handle tail call return.
788 Chain = GetPossiblePreceedingTailCall(Chain);
789 if (Chain.getOpcode() == X86ISD::TAILCALL) {
790 SDOperand TailCall = Chain;
791 SDOperand TargetAddress = TailCall.getOperand(1);
792 SDOperand StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000793 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000794 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
795 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
796 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
797 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
798 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000799 assert(StackAdjustment.getOpcode() == ISD::Constant &&
800 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000801
802 SmallVector<SDOperand,8> Operands;
803 Operands.push_back(Chain.getOperand(0));
804 Operands.push_back(TargetAddress);
805 Operands.push_back(StackAdjustment);
806 // Copy registers used by the call. Last operand is a flag so it is not
807 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000808 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000809 Operands.push_back(Chain.getOperand(i));
810 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000811 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
812 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000813 }
814
815 // Regular return.
816 SDOperand Flag;
817
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000818 // Copy the result values into the output registers.
Chris Lattner9774c912007-02-27 05:28:59 +0000819 if (RVLocs.size() != 1 || !RVLocs[0].isRegLoc() ||
820 RVLocs[0].getLocReg() != X86::ST0) {
821 for (unsigned i = 0; i != RVLocs.size(); ++i) {
822 CCValAssign &VA = RVLocs[i];
823 assert(VA.isRegLoc() && "Can only return in registers!");
824 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), Op.getOperand(i*2+1),
825 Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000826 Flag = Chain.getValue(1);
827 }
828 } else {
829 // We need to handle a destination of ST0 specially, because it isn't really
830 // a register.
831 SDOperand Value = Op.getOperand(1);
832
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000833 // an XMM register onto the fp-stack. Do this with an FP_EXTEND to f80.
834 // This will get legalized into a load/store if it can't get optimized away.
835 if (isScalarFPTypeInSSEReg(RVLocs[0].getValVT()))
836 Value = DAG.getNode(ISD::FP_EXTEND, MVT::f80, Value);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000837
838 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
839 SDOperand Ops[] = { Chain, Value };
840 Chain = DAG.getNode(X86ISD::FP_SET_RESULT, Tys, Ops, 2);
841 Flag = Chain.getValue(1);
842 }
843
844 SDOperand BytesToPop = DAG.getConstant(getBytesToPopOnReturn(), MVT::i16);
845 if (Flag.Val)
846 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop, Flag);
847 else
848 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, Chain, BytesToPop);
849}
850
851
Chris Lattner3085e152007-02-25 08:59:22 +0000852/// LowerCallResult - Lower the result values of an ISD::CALL into the
853/// appropriate copies out of appropriate physical registers. This assumes that
854/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
855/// being lowered. The returns a SDNode with the same number of values as the
856/// ISD::CALL.
857SDNode *X86TargetLowering::
858LowerCallResult(SDOperand Chain, SDOperand InFlag, SDNode *TheCall,
859 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000860
861 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000862 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000863 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
864 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000865 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
866
Chris Lattnere32bbf62007-02-28 07:09:55 +0000867 SmallVector<SDOperand, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000868
869 // Copy all of the result registers out of their specified physreg.
Chris Lattner9774c912007-02-27 05:28:59 +0000870 if (RVLocs.size() != 1 || RVLocs[0].getLocReg() != X86::ST0) {
871 for (unsigned i = 0; i != RVLocs.size(); ++i) {
872 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
873 RVLocs[i].getValVT(), InFlag).getValue(1);
Chris Lattner3085e152007-02-25 08:59:22 +0000874 InFlag = Chain.getValue(2);
875 ResultVals.push_back(Chain.getValue(0));
876 }
877 } else {
878 // Copies from the FP stack are special, as ST0 isn't a valid register
879 // before the fp stackifier runs.
880
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000881 // Copy ST0 into an RFP register with FP_GET_RESULT. If this will end up
882 // in an SSE register, copy it out as F80 and do a truncate, otherwise use
883 // the specified value type.
884 MVT::ValueType GetResultTy = RVLocs[0].getValVT();
885 if (isScalarFPTypeInSSEReg(GetResultTy))
886 GetResultTy = MVT::f80;
887 SDVTList Tys = DAG.getVTList(GetResultTy, MVT::Other, MVT::Flag);
888
Chris Lattner3085e152007-02-25 08:59:22 +0000889 SDOperand GROps[] = { Chain, InFlag };
890 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT, Tys, GROps, 2);
891 Chain = RetVal.getValue(1);
892 InFlag = RetVal.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +0000893
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000894 // If we want the result in an SSE register, use an FP_TRUNCATE to get it
895 // there.
896 if (GetResultTy != RVLocs[0].getValVT())
897 RetVal = DAG.getNode(ISD::FP_ROUND, RVLocs[0].getValVT(), RetVal,
898 // This truncation won't change the value.
899 DAG.getIntPtrConstant(1));
900
Chris Lattner3085e152007-02-25 08:59:22 +0000901 ResultVals.push_back(RetVal);
902 }
903
904 // Merge everything together with a MERGE_VALUES node.
905 ResultVals.push_back(Chain);
906 return DAG.getNode(ISD::MERGE_VALUES, TheCall->getVTList(),
907 &ResultVals[0], ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +0000908}
909
Evan Cheng0d9e9762008-01-29 19:34:22 +0000910/// LowerCallResultToTwo64BitRegs - Lower the result values of an x86-64
911/// ISD::CALL where the results are known to be in two 64-bit registers,
912/// e.g. XMM0 and XMM1. This simplify store the two values back to the
913/// fixed stack slot allocated for StructRet.
914SDNode *X86TargetLowering::
915LowerCallResultToTwo64BitRegs(SDOperand Chain, SDOperand InFlag,
916 SDNode *TheCall, unsigned Reg1, unsigned Reg2,
917 MVT::ValueType VT, SelectionDAG &DAG) {
918 SDOperand RetVal1 = DAG.getCopyFromReg(Chain, Reg1, VT, InFlag);
919 Chain = RetVal1.getValue(1);
920 InFlag = RetVal1.getValue(2);
921 SDOperand RetVal2 = DAG.getCopyFromReg(Chain, Reg2, VT, InFlag);
922 Chain = RetVal2.getValue(1);
923 InFlag = RetVal2.getValue(2);
924 SDOperand FIN = TheCall->getOperand(5);
925 Chain = DAG.getStore(Chain, RetVal1, FIN, NULL, 0);
926 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
927 Chain = DAG.getStore(Chain, RetVal2, FIN, NULL, 0);
928 return Chain.Val;
929}
930
931/// LowerCallResultToTwoX87Regs - Lower the result values of an x86-64 ISD::CALL
932/// where the results are known to be in ST0 and ST1.
933SDNode *X86TargetLowering::
934LowerCallResultToTwoX87Regs(SDOperand Chain, SDOperand InFlag,
935 SDNode *TheCall, SelectionDAG &DAG) {
936 SmallVector<SDOperand, 8> ResultVals;
937 const MVT::ValueType VTs[] = { MVT::f80, MVT::f80, MVT::Other, MVT::Flag };
938 SDVTList Tys = DAG.getVTList(VTs, 4);
939 SDOperand Ops[] = { Chain, InFlag };
940 SDOperand RetVal = DAG.getNode(X86ISD::FP_GET_RESULT2, Tys, Ops, 2);
941 Chain = RetVal.getValue(2);
942 SDOperand FIN = TheCall->getOperand(5);
943 Chain = DAG.getStore(Chain, RetVal.getValue(1), FIN, NULL, 0);
944 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(16));
945 Chain = DAG.getStore(Chain, RetVal, FIN, NULL, 0);
946 return Chain.Val;
947}
Chris Lattner2b02a442007-02-25 08:29:00 +0000948
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000949//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000950// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000951//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000952// StdCall calling convention seems to be standard for many Windows' API
953// routines and around. It differs from C calling convention just a little:
954// callee should clean up the stack, not caller. Symbols should be also
955// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000956// For info on fast calling convention see Fast Calling Convention (tail call)
957// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000958
Evan Cheng85e38002006-04-27 05:35:28 +0000959/// AddLiveIn - This helper function adds the specified physical register to the
960/// MachineFunction as a live in value. It also creates a corresponding virtual
961/// register for it.
962static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +0000963 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +0000964 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +0000965 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
966 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +0000967 return VReg;
968}
969
Gordon Henriksen86737662008-01-05 16:56:59 +0000970// Determines whether a CALL node uses struct return semantics.
971static bool CallIsStructReturn(SDOperand Op) {
972 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
973 if (!NumOps)
974 return false;
975
976 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(6));
977 return Flags->getValue() & ISD::ParamFlags::StructReturn;
978}
979
980// Determines whether a FORMAL_ARGUMENTS node uses struct return semantics.
981static bool ArgsAreStructReturn(SDOperand Op) {
982 unsigned NumArgs = Op.Val->getNumValues() - 1;
983 if (!NumArgs)
984 return false;
985
986 ConstantSDNode *Flags = cast<ConstantSDNode>(Op.getOperand(3));
987 return Flags->getValue() & ISD::ParamFlags::StructReturn;
988}
989
990// Determines whether a CALL or FORMAL_ARGUMENTS node requires the callee to pop
991// its own arguments. Callee pop is necessary to support tail calls.
992bool X86TargetLowering::IsCalleePop(SDOperand Op) {
993 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
994 if (IsVarArg)
995 return false;
996
997 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
998 default:
999 return false;
1000 case CallingConv::X86_StdCall:
1001 return !Subtarget->is64Bit();
1002 case CallingConv::X86_FastCall:
1003 return !Subtarget->is64Bit();
1004 case CallingConv::Fast:
1005 return PerformTailCallOpt;
1006 }
1007}
1008
1009// Selects the correct CCAssignFn for a CALL or FORMAL_ARGUMENTS node.
1010CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDOperand Op) const {
1011 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1012
1013 if (Subtarget->is64Bit())
1014 if (CC == CallingConv::Fast && PerformTailCallOpt)
1015 return CC_X86_64_TailCall;
1016 else
1017 return CC_X86_64_C;
1018
1019 if (CC == CallingConv::X86_FastCall)
1020 return CC_X86_32_FastCall;
1021 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1022 return CC_X86_32_TailCall;
1023 else
1024 return CC_X86_32_C;
1025}
1026
1027// Selects the appropriate decoration to apply to a MachineFunction containing a
1028// given FORMAL_ARGUMENTS node.
1029NameDecorationStyle
1030X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDOperand Op) {
1031 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1032 if (CC == CallingConv::X86_FastCall)
1033 return FastCall;
1034 else if (CC == CallingConv::X86_StdCall)
1035 return StdCall;
1036 return None;
1037}
1038
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001039
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001040// IsPossiblyOverwrittenArgumentOfTailCall - Check if the operand could possibly
1041// be overwritten when lowering the outgoing arguments in a tail call. Currently
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001042// the implementation of this call is very conservative and assumes all
1043// arguments sourcing from FORMAL_ARGUMENTS or a CopyFromReg with virtual
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001044// registers would be overwritten by direct lowering.
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001045// Possible improvement:
1046// Check FORMAL_ARGUMENTS corresponding MERGE_VALUES for CopyFromReg nodes
1047// indicating inreg passed arguments which also need not be lowered to a safe
1048// stack slot.
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001049static bool IsPossiblyOverwrittenArgumentOfTailCall(SDOperand Op) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001050 RegisterSDNode * OpReg = NULL;
1051 if (Op.getOpcode() == ISD::FORMAL_ARGUMENTS ||
1052 (Op.getOpcode()== ISD::CopyFromReg &&
1053 (OpReg = cast<RegisterSDNode>(Op.getOperand(1))) &&
1054 OpReg->getReg() >= MRegisterInfo::FirstVirtualRegister))
1055 return true;
1056 return false;
1057}
1058
Evan Cheng8e5712b2008-01-12 01:08:07 +00001059// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1060// by "Src" to address "Dst" with size and alignment information specified by
1061// the specific parameter attribute. The copy will be passed as a byval function
1062// parameter.
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001063static SDOperand
Evan Cheng8e5712b2008-01-12 01:08:07 +00001064CreateCopyOfByValArgument(SDOperand Src, SDOperand Dst, SDOperand Chain,
1065 unsigned Flags, SelectionDAG &DAG) {
1066 unsigned Align = 1 <<
1067 ((Flags & ISD::ParamFlags::ByValAlign) >> ISD::ParamFlags::ByValAlignOffs);
1068 unsigned Size = (Flags & ISD::ParamFlags::ByValSize) >>
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001069 ISD::ParamFlags::ByValSizeOffs;
Evan Cheng8e5712b2008-01-12 01:08:07 +00001070 SDOperand AlignNode = DAG.getConstant(Align, MVT::i32);
1071 SDOperand SizeNode = DAG.getConstant(Size, MVT::i32);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001072 SDOperand AlwaysInline = DAG.getConstant(1, MVT::i32);
Evan Cheng8e5712b2008-01-12 01:08:07 +00001073 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, AlignNode, AlwaysInline);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001074}
1075
Rafael Espindola7effac52007-09-14 15:48:13 +00001076SDOperand X86TargetLowering::LowerMemArgument(SDOperand Op, SelectionDAG &DAG,
1077 const CCValAssign &VA,
1078 MachineFrameInfo *MFI,
1079 SDOperand Root, unsigned i) {
1080 // Create the nodes corresponding to a load from this parameter slot.
Evan Chenge70bb592008-01-10 02:24:25 +00001081 unsigned Flags = cast<ConstantSDNode>(Op.getOperand(3 + i))->getValue();
1082 bool isByVal = Flags & ISD::ParamFlags::ByVal;
1083
1084 // FIXME: For now, all byval parameter objects are marked mutable. This
1085 // can be changed with more analysis.
Rafael Espindola7effac52007-09-14 15:48:13 +00001086 int FI = MFI->CreateFixedObject(MVT::getSizeInBits(VA.getValVT())/8,
Evan Chenge70bb592008-01-10 02:24:25 +00001087 VA.getLocMemOffset(), !isByVal);
Rafael Espindola7effac52007-09-14 15:48:13 +00001088 SDOperand FIN = DAG.getFrameIndex(FI, getPointerTy());
Evan Chenge70bb592008-01-10 02:24:25 +00001089 if (isByVal)
Rafael Espindola7effac52007-09-14 15:48:13 +00001090 return FIN;
Evan Chenge70bb592008-01-10 02:24:25 +00001091 return DAG.getLoad(VA.getValVT(), Root, FIN, NULL, 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001092}
1093
Gordon Henriksen86737662008-01-05 16:56:59 +00001094SDOperand
1095X86TargetLowering::LowerFORMAL_ARGUMENTS(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001096 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001097 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1098
1099 const Function* Fn = MF.getFunction();
1100 if (Fn->hasExternalLinkage() &&
1101 Subtarget->isTargetCygMing() &&
1102 Fn->getName() == "main")
1103 FuncInfo->setForceFramePointer(true);
1104
1105 // Decorate the function name.
1106 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1107
Evan Cheng1bc78042006-04-26 01:20:17 +00001108 MachineFrameInfo *MFI = MF.getFrameInfo();
Evan Cheng25caf632006-05-23 21:06:34 +00001109 SDOperand Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001110 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001111 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001112 bool Is64Bit = Subtarget->is64Bit();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001113
1114 assert(!(isVarArg && CC == CallingConv::Fast) &&
1115 "Var args not supported with calling convention fastcc");
1116
Chris Lattner638402b2007-02-28 07:00:42 +00001117 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001118 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001119 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen86737662008-01-05 16:56:59 +00001120 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001121
Chris Lattnerf39f7712007-02-28 05:46:49 +00001122 SmallVector<SDOperand, 8> ArgValues;
1123 unsigned LastVal = ~0U;
1124 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1125 CCValAssign &VA = ArgLocs[i];
1126 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1127 // places.
1128 assert(VA.getValNo() != LastVal &&
1129 "Don't support value assigned to multiple locs yet");
1130 LastVal = VA.getValNo();
1131
1132 if (VA.isRegLoc()) {
1133 MVT::ValueType RegVT = VA.getLocVT();
1134 TargetRegisterClass *RC;
1135 if (RegVT == MVT::i32)
1136 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001137 else if (Is64Bit && RegVT == MVT::i64)
1138 RC = X86::GR64RegisterClass;
1139 else if (Is64Bit && RegVT == MVT::f32)
1140 RC = X86::FR32RegisterClass;
1141 else if (Is64Bit && RegVT == MVT::f64)
1142 RC = X86::FR64RegisterClass;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001143 else {
1144 assert(MVT::isVector(RegVT));
Gordon Henriksen86737662008-01-05 16:56:59 +00001145 if (Is64Bit && MVT::getSizeInBits(RegVT) == 64) {
1146 RC = X86::GR64RegisterClass; // MMX values are passed in GPRs.
1147 RegVT = MVT::i64;
1148 } else
1149 RC = X86::VR128RegisterClass;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001150 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001151
Chris Lattner82932a52007-03-02 05:12:29 +00001152 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
1153 SDOperand ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001154
1155 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1156 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1157 // right size.
1158 if (VA.getLocInfo() == CCValAssign::SExt)
1159 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1160 DAG.getValueType(VA.getValVT()));
1161 else if (VA.getLocInfo() == CCValAssign::ZExt)
1162 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1163 DAG.getValueType(VA.getValVT()));
1164
1165 if (VA.getLocInfo() != CCValAssign::Full)
1166 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1167
Gordon Henriksen86737662008-01-05 16:56:59 +00001168 // Handle MMX values passed in GPRs.
1169 if (Is64Bit && RegVT != VA.getLocVT() && RC == X86::GR64RegisterClass &&
1170 MVT::getSizeInBits(RegVT) == 64)
1171 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1172
Chris Lattnerf39f7712007-02-28 05:46:49 +00001173 ArgValues.push_back(ArgValue);
1174 } else {
1175 assert(VA.isMemLoc());
Rafael Espindola7effac52007-09-14 15:48:13 +00001176 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001177 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001178 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001179
Chris Lattnerf39f7712007-02-28 05:46:49 +00001180 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001181 // align stack specially for tail calls
Gordon Henriksenae636f82008-01-03 16:47:34 +00001182 if (CC == CallingConv::Fast)
1183 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001184
Evan Cheng1bc78042006-04-26 01:20:17 +00001185 // If the function takes variable number of arguments, make a frame index for
1186 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001187 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001188 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1189 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1190 }
1191 if (Is64Bit) {
1192 static const unsigned GPR64ArgRegs[] = {
1193 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1194 };
1195 static const unsigned XMMArgRegs[] = {
1196 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1197 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1198 };
1199
1200 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs, 6);
1201 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1202
1203 // For X86-64, if there are vararg parameters that are passed via
1204 // registers, then we must store them to their spots on the stack so they
1205 // may be loaded by deferencing the result of va_next.
1206 VarArgsGPOffset = NumIntRegs * 8;
1207 VarArgsFPOffset = 6 * 8 + NumXMMRegs * 16;
1208 RegSaveFrameIndex = MFI->CreateStackObject(6 * 8 + 8 * 16, 16);
1209
1210 // Store the integer parameter registers.
1211 SmallVector<SDOperand, 8> MemOps;
1212 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1213 SDOperand FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001214 DAG.getIntPtrConstant(VarArgsGPOffset));
Gordon Henriksen86737662008-01-05 16:56:59 +00001215 for (; NumIntRegs != 6; ++NumIntRegs) {
1216 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1217 X86::GR64RegisterClass);
1218 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1219 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1220 MemOps.push_back(Store);
1221 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001222 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001223 }
1224
1225 // Now store the XMM (fp + vector) parameter registers.
1226 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001227 DAG.getIntPtrConstant(VarArgsFPOffset));
Gordon Henriksen86737662008-01-05 16:56:59 +00001228 for (; NumXMMRegs != 8; ++NumXMMRegs) {
1229 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1230 X86::VR128RegisterClass);
1231 SDOperand Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1232 SDOperand Store = DAG.getStore(Val.getValue(1), Val, FIN, NULL, 0);
1233 MemOps.push_back(Store);
1234 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001235 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001236 }
1237 if (!MemOps.empty())
1238 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1239 &MemOps[0], MemOps.size());
1240 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001241 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001242
1243 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1244 // arguments and the arguments after the retaddr has been pushed are
1245 // aligned.
1246 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1247 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1248 (StackSize & 7) == 0)
1249 StackSize += 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001250
Gordon Henriksenae636f82008-01-03 16:47:34 +00001251 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001252
Gordon Henriksen86737662008-01-05 16:56:59 +00001253 // Some CCs need callee pop.
1254 if (IsCalleePop(Op)) {
1255 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001256 BytesCallerReserves = 0;
1257 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001258 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001259 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001260 if (!Is64Bit && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001261 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001262 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001263 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001264
Gordon Henriksen86737662008-01-05 16:56:59 +00001265 if (!Is64Bit) {
1266 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1267 if (CC == CallingConv::X86_FastCall)
1268 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1269 }
Evan Cheng25caf632006-05-23 21:06:34 +00001270
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001271 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001272
Evan Cheng25caf632006-05-23 21:06:34 +00001273 // Return the new list of results.
Chris Lattner5a88b832007-02-25 07:10:00 +00001274 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(),
Chris Lattner14dd4c92007-02-26 07:50:02 +00001275 &ArgValues[0], ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001276}
1277
Evan Chengdffbd832008-01-10 00:09:10 +00001278SDOperand
1279X86TargetLowering::LowerMemOpCallTo(SDOperand Op, SelectionDAG &DAG,
1280 const SDOperand &StackPtr,
1281 const CCValAssign &VA,
1282 SDOperand Chain,
1283 SDOperand Arg) {
Chris Lattner0bd48932008-01-17 07:00:52 +00001284 SDOperand PtrOff = DAG.getIntPtrConstant(VA.getLocMemOffset());
Evan Chengdffbd832008-01-10 00:09:10 +00001285 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
1286 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1287 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
1288 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001289 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001290 }
Evan Cheng8e5712b2008-01-12 01:08:07 +00001291 return DAG.getStore(Chain, Arg, PtrOff, NULL, 0);
Evan Chengdffbd832008-01-10 00:09:10 +00001292}
1293
Evan Cheng0d9e9762008-01-29 19:34:22 +00001294/// ClassifyX86_64SRetCallReturn - Classify how to implement a x86-64
1295/// struct return call to the specified function. X86-64 ABI specifies
1296/// some SRet calls are actually returned in registers. Since current
1297/// LLVM cannot represent multi-value calls, they are represent as
1298/// calls where the results are passed in a hidden struct provided by
1299/// the caller. This function examines the type of the struct to
1300/// determine the correct way to implement the call.
1301X86::X86_64SRet
1302X86TargetLowering::ClassifyX86_64SRetCallReturn(const Function *Fn) {
1303 // FIXME: Disabled for now.
1304 return X86::InMemory;
1305
1306 const PointerType *PTy = cast<PointerType>(Fn->arg_begin()->getType());
1307 const Type *RTy = PTy->getElementType();
1308 unsigned Size = getTargetData()->getABITypeSize(RTy);
1309 if (Size != 16 && Size != 32)
1310 return X86::InMemory;
1311
1312 if (Size == 32) {
1313 const StructType *STy = dyn_cast<StructType>(RTy);
1314 if (!STy) return X86::InMemory;
1315 if (STy->getNumElements() == 2 &&
1316 STy->getElementType(0) == Type::X86_FP80Ty &&
1317 STy->getElementType(1) == Type::X86_FP80Ty)
1318 return X86::InX87;
1319 }
1320
1321 bool AllFP = true;
1322 for (Type::subtype_iterator I = RTy->subtype_begin(), E = RTy->subtype_end();
1323 I != E; ++I) {
1324 const Type *STy = I->get();
1325 if (!STy->isFPOrFPVector()) {
1326 AllFP = false;
1327 break;
1328 }
1329 }
1330
1331 if (AllFP)
1332 return X86::InSSE;
1333 return X86::InGPR64;
1334}
1335
1336void X86TargetLowering::X86_64AnalyzeSRetCallOperands(SDNode *TheCall,
1337 CCAssignFn *Fn,
1338 CCState &CCInfo) {
1339 unsigned NumOps = (TheCall->getNumOperands() - 5) / 2;
1340 for (unsigned i = 1; i != NumOps; ++i) {
1341 MVT::ValueType ArgVT = TheCall->getOperand(5+2*i).getValueType();
1342 SDOperand FlagOp = TheCall->getOperand(5+2*i+1);
1343 unsigned ArgFlags =cast<ConstantSDNode>(FlagOp)->getValue();
1344 if (Fn(i, ArgVT, ArgVT, CCValAssign::Full, ArgFlags, CCInfo)) {
1345 cerr << "Call operand #" << i << " has unhandled type "
1346 << MVT::getValueTypeString(ArgVT) << "\n";
1347 abort();
1348 }
1349 }
1350}
1351
Gordon Henriksen86737662008-01-05 16:56:59 +00001352SDOperand X86TargetLowering::LowerCALL(SDOperand Op, SelectionDAG &DAG) {
1353 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng32fe1032006-05-25 00:59:30 +00001354 SDOperand Chain = Op.getOperand(0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001355 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001356 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen86737662008-01-05 16:56:59 +00001357 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1358 && CC == CallingConv::Fast && PerformTailCallOpt;
Evan Cheng32fe1032006-05-25 00:59:30 +00001359 SDOperand Callee = Op.getOperand(4);
Gordon Henriksen86737662008-01-05 16:56:59 +00001360 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng0d9e9762008-01-29 19:34:22 +00001361 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001362
1363 assert(!(isVarArg && CC == CallingConv::Fast) &&
1364 "Var args not supported with calling convention fastcc");
1365
Chris Lattner638402b2007-02-28 07:00:42 +00001366 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001367 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001368 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Evan Cheng0d9e9762008-01-29 19:34:22 +00001369 CCAssignFn *CCFn = CCAssignFnForNode(Op);
1370
1371 X86::X86_64SRet SRetMethod = X86::InMemory;
1372 if (Is64Bit && IsStructRet)
1373 // FIXME: We can't figure out type of the sret structure for indirect
1374 // calls. We need to copy more information from CallSite to the ISD::CALL
1375 // node.
1376 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1377 SRetMethod =
1378 ClassifyX86_64SRetCallReturn(dyn_cast<Function>(G->getGlobal()));
1379
1380 // UGLY HACK! For x86-64, some 128-bit aggregates are returns in a pair of
1381 // registers. Unfortunately, llvm does not support i128 yet so we pretend it's
1382 // a sret call.
1383 if (SRetMethod != X86::InMemory)
1384 X86_64AnalyzeSRetCallOperands(Op.Val, CCFn, CCInfo);
1385 else
1386 CCInfo.AnalyzeCallOperands(Op.Val, CCFn);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001387
Chris Lattner423c5f42007-02-28 05:31:48 +00001388 // Get a count of how many bytes are to be pushed on the stack.
1389 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001390 if (CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001391 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001392
Gordon Henriksen86737662008-01-05 16:56:59 +00001393 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1394 // arguments and the arguments after the retaddr has been pushed are aligned.
1395 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1396 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1397 (NumBytes & 7) == 0)
1398 NumBytes += 4;
1399
1400 int FPDiff = 0;
1401 if (IsTailCall) {
1402 // Lower arguments at fp - stackoffset + fpdiff.
1403 unsigned NumBytesCallerPushed =
1404 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1405 FPDiff = NumBytesCallerPushed - NumBytes;
1406
1407 // Set the delta of movement of the returnaddr stackslot.
1408 // But only set if delta is greater than previous delta.
1409 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1410 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1411 }
1412
Chris Lattner0bd48932008-01-17 07:00:52 +00001413 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001414
Gordon Henriksen86737662008-01-05 16:56:59 +00001415 SDOperand RetAddrFrIdx, NewRetAddrFrIdx;
1416 if (IsTailCall) {
1417 // Adjust the Return address stack slot.
1418 if (FPDiff) {
1419 MVT::ValueType VT = Is64Bit ? MVT::i64 : MVT::i32;
1420 RetAddrFrIdx = getReturnAddressFrameIndex(DAG);
1421 // Load the "old" Return address.
1422 RetAddrFrIdx =
1423 DAG.getLoad(VT, Chain,RetAddrFrIdx, NULL, 0);
1424 // Calculate the new stack slot for the return address.
1425 int SlotSize = Is64Bit ? 8 : 4;
1426 int NewReturnAddrFI =
1427 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
1428 NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
1429 Chain = SDOperand(RetAddrFrIdx.Val, 1);
1430 }
1431 }
1432
Chris Lattner5a88b832007-02-25 07:10:00 +00001433 SmallVector<std::pair<unsigned, SDOperand>, 8> RegsToPass;
1434 SmallVector<SDOperand, 8> MemOpChains;
Evan Cheng32fe1032006-05-25 00:59:30 +00001435
Chris Lattner423c5f42007-02-28 05:31:48 +00001436 SDOperand StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001437
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001438 // Walk the register/memloc assignments, inserting copies/loads. For tail
1439 // calls, lower arguments which could otherwise be possibly overwritten to the
1440 // stack slot where they would go on normal function calls.
Chris Lattner423c5f42007-02-28 05:31:48 +00001441 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1442 CCValAssign &VA = ArgLocs[i];
1443 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001444
Chris Lattner423c5f42007-02-28 05:31:48 +00001445 // Promote the value if needed.
1446 switch (VA.getLocInfo()) {
1447 default: assert(0 && "Unknown loc info!");
1448 case CCValAssign::Full: break;
1449 case CCValAssign::SExt:
1450 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1451 break;
1452 case CCValAssign::ZExt:
1453 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1454 break;
1455 case CCValAssign::AExt:
1456 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1457 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001458 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001459
1460 if (VA.isRegLoc()) {
1461 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1462 } else {
Arnold Schwaighofera51cf0f2008-01-11 17:10:15 +00001463 if (!IsTailCall || IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001464 assert(VA.isMemLoc());
1465 if (StackPtr.Val == 0)
1466 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1467
1468 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1469 Arg));
1470 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001471 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001472 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001473
Evan Cheng32fe1032006-05-25 00:59:30 +00001474 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001475 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1476 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001477
Evan Cheng347d5f72006-04-28 21:29:37 +00001478 // Build a sequence of copy-to-reg nodes chained together with token chain
1479 // and flag operands which copy the outgoing args into registers.
1480 SDOperand InFlag;
Evan Cheng32fe1032006-05-25 00:59:30 +00001481 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1482 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1483 InFlag);
Evan Cheng347d5f72006-04-28 21:29:37 +00001484 InFlag = Chain.getValue(1);
1485 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001486
1487 if (IsTailCall)
1488 InFlag = SDOperand(); // ??? Isn't this nuking the preceding loop's output?
1489
Evan Chengf4684712007-02-21 21:18:14 +00001490 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1491 // GOT pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001492 // Does not work with tail call since ebx is not restored correctly by
1493 // tailcaller. TODO: at least for x86 - verify for x86-64
1494 if (!IsTailCall && !Is64Bit &&
1495 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Evan Cheng706535d2007-01-22 21:34:25 +00001496 Subtarget->isPICStyleGOT()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001497 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1498 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1499 InFlag);
1500 InFlag = Chain.getValue(1);
1501 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001502
Gordon Henriksen86737662008-01-05 16:56:59 +00001503 if (Is64Bit && isVarArg) {
1504 // From AMD64 ABI document:
1505 // For calls that may call functions that use varargs or stdargs
1506 // (prototype-less calls or calls to functions containing ellipsis (...) in
1507 // the declaration) %al is used as hidden argument to specify the number
1508 // of SSE registers used. The contents of %al do not need to match exactly
1509 // the number of registers, but must be an ubound on the number of SSE
1510 // registers used and is in the range 0 - 8 inclusive.
1511
1512 // Count the number of XMM registers allocated.
1513 static const unsigned XMMArgRegs[] = {
1514 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1515 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1516 };
1517 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1518
1519 Chain = DAG.getCopyToReg(Chain, X86::AL,
1520 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1521 InFlag = Chain.getValue(1);
1522 }
1523
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001524 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001525 if (IsTailCall) {
1526 SmallVector<SDOperand, 8> MemOpChains2;
Gordon Henriksen86737662008-01-05 16:56:59 +00001527 SDOperand FIN;
1528 int FI = 0;
1529 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1530 CCValAssign &VA = ArgLocs[i];
1531 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001532 assert(VA.isMemLoc());
1533 SDOperand Arg = Op.getOperand(5+2*VA.getValNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001534 SDOperand FlagsOp = Op.getOperand(6+2*VA.getValNo());
1535 unsigned Flags = cast<ConstantSDNode>(FlagsOp)->getValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001536 // Create frame index.
1537 int32_t Offset = VA.getLocMemOffset()+FPDiff;
1538 uint32_t OpSize = (MVT::getSizeInBits(VA.getLocVT())+7)/8;
1539 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
1540 FIN = DAG.getFrameIndex(FI, MVT::i32);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001541 SDOperand Source = Arg;
Evan Cheng8e5712b2008-01-12 01:08:07 +00001542 if (IsPossiblyOverwrittenArgumentOfTailCall(Arg)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001543 // Copy from stack slots to stack slot of a tail called function. This
1544 // needs to be done because if we would lower the arguments directly
1545 // to their real stack slot we might end up overwriting each other.
1546 // Get source stack slot.
Chris Lattner0bd48932008-01-17 07:00:52 +00001547 Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001548 if (StackPtr.Val == 0)
1549 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1550 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1551 if ((Flags & ISD::ParamFlags::ByVal)==0)
Duncan Sands9e9cf0c2008-01-13 21:20:29 +00001552 Source = DAG.getLoad(VA.getValVT(), Chain, Source, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001553 }
1554
Gordon Henriksen86737662008-01-05 16:56:59 +00001555 if (Flags & ISD::ParamFlags::ByVal) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001556 // Copy relative to framepointer.
1557 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
1558 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001559 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001560 // Store relative to framepointer.
1561 MemOpChains2.push_back(DAG.getStore(Chain, Source, FIN, NULL, 0));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001562 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001563 }
1564 }
1565
1566 if (!MemOpChains2.empty())
1567 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001568 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001569
1570 // Store the return address to the appropriate stack slot.
1571 if (FPDiff)
1572 Chain = DAG.getStore(Chain,RetAddrFrIdx, NewRetAddrFrIdx, NULL, 0);
1573 }
1574
Evan Cheng32fe1032006-05-25 00:59:30 +00001575 // If the callee is a GlobalAddress node (quite common, every direct call is)
1576 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001577 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001578 // We should use extra load for direct calls to dllimported functions in
1579 // non-JIT mode.
Gordon Henriksen86737662008-01-05 16:56:59 +00001580 if ((IsTailCall || !Is64Bit ||
1581 getTargetMachine().getCodeModel() != CodeModel::Large)
1582 && !Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1583 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001584 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksenae636f82008-01-03 16:47:34 +00001585 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001586 if (IsTailCall || !Is64Bit ||
1587 getTargetMachine().getCodeModel() != CodeModel::Large)
1588 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
1589 } else if (IsTailCall) {
1590 assert(Callee.getOpcode() == ISD::LOAD &&
1591 "Function destination must be loaded into virtual register");
1592 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1593
1594 Chain = DAG.getCopyToReg(Chain,
1595 DAG.getRegister(Opc, getPointerTy()) ,
1596 Callee,InFlag);
1597 Callee = DAG.getRegister(Opc, getPointerTy());
1598 // Add register as live out.
1599 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001600 }
1601
Chris Lattnerd96d0722007-02-25 06:40:16 +00001602 // Returns a chain & a flag for retval copy to use.
1603 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00001604 SmallVector<SDOperand, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001605
1606 if (IsTailCall) {
1607 Ops.push_back(Chain);
Chris Lattner0bd48932008-01-17 07:00:52 +00001608 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1609 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen86737662008-01-05 16:56:59 +00001610 if (InFlag.Val)
1611 Ops.push_back(InFlag);
1612 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1613 InFlag = Chain.getValue(1);
1614
1615 // Returns a chain & a flag for retval copy to use.
1616 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1617 Ops.clear();
1618 }
1619
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001620 Ops.push_back(Chain);
1621 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001622
Gordon Henriksen86737662008-01-05 16:56:59 +00001623 if (IsTailCall)
1624 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001625
1626 // Add an implicit use GOT pointer in EBX.
Gordon Henriksen86737662008-01-05 16:56:59 +00001627 if (!IsTailCall && !Is64Bit &&
1628 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Evan Chengf4684712007-02-21 21:18:14 +00001629 Subtarget->isPICStyleGOT())
1630 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
Gordon Henriksenae636f82008-01-03 16:47:34 +00001631
Gordon Henriksen86737662008-01-05 16:56:59 +00001632 // Add argument registers to the end of the list so that they are known live
1633 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001634 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1635 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1636 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001637
Evan Cheng347d5f72006-04-28 21:29:37 +00001638 if (InFlag.Val)
1639 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001640
Gordon Henriksen86737662008-01-05 16:56:59 +00001641 if (IsTailCall) {
1642 assert(InFlag.Val &&
1643 "Flag must be set. Depend on flag being set in LowerRET");
1644 Chain = DAG.getNode(X86ISD::TAILCALL,
1645 Op.Val->getVTList(), &Ops[0], Ops.size());
1646
1647 return SDOperand(Chain.Val, Op.ResNo);
1648 }
1649
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001650 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001651 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001652
Chris Lattner2d297092006-05-23 18:50:38 +00001653 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 unsigned NumBytesForCalleeToPush;
1655 if (IsCalleePop(Op))
1656 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng0d9e9762008-01-29 19:34:22 +00001657 else if (!Is64Bit && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001658 // If this is is a call to a struct-return function, the callee
1659 // pops the hidden struct pointer, so we have to push it back.
1660 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001661 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001662 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001663 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001664
Gordon Henriksenae636f82008-01-03 16:47:34 +00001665 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001666 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner0bd48932008-01-17 07:00:52 +00001667 DAG.getIntPtrConstant(NumBytes),
1668 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001669 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001670 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001671
Chris Lattner3085e152007-02-25 08:59:22 +00001672 // Handle result values, copying them out of physregs into vregs that we
1673 // return.
Evan Cheng0d9e9762008-01-29 19:34:22 +00001674 switch (SRetMethod) {
1675 default:
1676 return SDOperand(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
1677 case X86::InGPR64:
1678 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1679 X86::RAX, X86::RDX,
1680 MVT::i64, DAG), Op.ResNo);
1681 case X86::InSSE:
1682 return SDOperand(LowerCallResultToTwo64BitRegs(Chain, InFlag, Op.Val,
1683 X86::XMM0, X86::XMM1,
1684 MVT::f64, DAG), Op.ResNo);
1685 case X86::InX87:
1686 return SDOperand(LowerCallResultToTwoX87Regs(Chain, InFlag, Op.Val, DAG),
1687 Op.ResNo);
1688 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001689}
1690
Evan Cheng25ab6902006-09-08 06:48:29 +00001691
1692//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001693// Fast Calling Convention (tail call) implementation
1694//===----------------------------------------------------------------------===//
1695
1696// Like std call, callee cleans arguments, convention except that ECX is
1697// reserved for storing the tail called function address. Only 2 registers are
1698// free for argument passing (inreg). Tail call optimization is performed
1699// provided:
1700// * tailcallopt is enabled
1701// * caller/callee are fastcc
1702// * elf/pic is disabled OR
1703// * elf/pic enabled + callee is in module + callee has
1704// visibility protected or hidden
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001705// To keep the stack aligned according to platform abi the function
1706// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1707// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001708// If a tail called function callee has more arguments than the caller the
1709// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001710// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001711// original REtADDR, but before the saved framepointer or the spilled registers
1712// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1713// stack layout:
1714// arg1
1715// arg2
1716// RETADDR
1717// [ new RETADDR
1718// move area ]
1719// (possible EBP)
1720// ESI
1721// EDI
1722// local1 ..
1723
1724/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1725/// for a 16 byte align requirement.
1726unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1727 SelectionDAG& DAG) {
1728 if (PerformTailCallOpt) {
1729 MachineFunction &MF = DAG.getMachineFunction();
1730 const TargetMachine &TM = MF.getTarget();
1731 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1732 unsigned StackAlignment = TFI.getStackAlignment();
1733 uint64_t AlignMask = StackAlignment - 1;
1734 int64_t Offset = StackSize;
1735 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1736 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1737 // Number smaller than 12 so just add the difference.
1738 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1739 } else {
1740 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1741 Offset = ((~AlignMask) & Offset) + StackAlignment +
1742 (StackAlignment-SlotSize);
1743 }
1744 StackSize = Offset;
1745 }
1746 return StackSize;
1747}
1748
1749/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001750/// following the call is a return. A function is eligible if caller/callee
1751/// calling conventions match, currently only fastcc supports tail calls, and
1752/// the function CALL is immediatly followed by a RET.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001753bool X86TargetLowering::IsEligibleForTailCallOptimization(SDOperand Call,
1754 SDOperand Ret,
1755 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001756 if (!PerformTailCallOpt)
1757 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001758
1759 // Check whether CALL node immediatly preceeds the RET node and whether the
1760 // return uses the result of the node or is a void return.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001761 unsigned NumOps = Ret.getNumOperands();
1762 if ((NumOps == 1 &&
1763 (Ret.getOperand(0) == SDOperand(Call.Val,1) ||
1764 Ret.getOperand(0) == SDOperand(Call.Val,0))) ||
Evan Chenga9d641e2007-11-02 17:45:40 +00001765 (NumOps > 1 &&
Evan Cheng9df7dc52007-11-02 01:26:22 +00001766 Ret.getOperand(0) == SDOperand(Call.Val,Call.Val->getNumValues()-1) &&
1767 Ret.getOperand(1) == SDOperand(Call.Val,0))) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001768 MachineFunction &MF = DAG.getMachineFunction();
1769 unsigned CallerCC = MF.getFunction()->getCallingConv();
1770 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1771 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
1772 SDOperand Callee = Call.getOperand(4);
1773 // On elf/pic %ebx needs to be livein.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001774 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
1775 !Subtarget->isPICStyleGOT())
1776 return true;
1777
1778 // Can only do local tail calls with PIC.
Gordon Henriksen86737662008-01-05 16:56:59 +00001779 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1780 return G->getGlobal()->hasHiddenVisibility()
1781 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001782 }
1783 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001784
1785 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001786}
1787
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001788//===----------------------------------------------------------------------===//
1789// Other Lowering Hooks
1790//===----------------------------------------------------------------------===//
1791
1792
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001793SDOperand X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001794 MachineFunction &MF = DAG.getMachineFunction();
1795 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1796 int ReturnAddrIndex = FuncInfo->getRAIndex();
1797
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001798 if (ReturnAddrIndex == 0) {
1799 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00001800 if (Subtarget->is64Bit())
1801 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1802 else
1803 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001804
1805 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001806 }
1807
Evan Cheng25ab6902006-09-08 06:48:29 +00001808 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001809}
1810
1811
1812
Evan Cheng6dfa9992006-01-30 23:41:35 +00001813/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1814/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001815/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1816/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001817static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Chris Lattnerf9570512006-09-13 03:22:10 +00001818 unsigned &X86CC, SDOperand &LHS, SDOperand &RHS,
1819 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001820 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001821 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001822 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1823 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1824 // X > -1 -> X == 0, jump !sign.
1825 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001826 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001827 return true;
1828 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1829 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001830 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001831 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00001832 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1833 // X < 1 -> X <= 0
1834 RHS = DAG.getConstant(0, RHS.getValueType());
1835 X86CC = X86::COND_LE;
1836 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001837 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001838 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001839
Evan Chengd9558e02006-01-06 00:43:03 +00001840 switch (SetCCOpcode) {
1841 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001842 case ISD::SETEQ: X86CC = X86::COND_E; break;
1843 case ISD::SETGT: X86CC = X86::COND_G; break;
1844 case ISD::SETGE: X86CC = X86::COND_GE; break;
1845 case ISD::SETLT: X86CC = X86::COND_L; break;
1846 case ISD::SETLE: X86CC = X86::COND_LE; break;
1847 case ISD::SETNE: X86CC = X86::COND_NE; break;
1848 case ISD::SETULT: X86CC = X86::COND_B; break;
1849 case ISD::SETUGT: X86CC = X86::COND_A; break;
1850 case ISD::SETULE: X86CC = X86::COND_BE; break;
1851 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001852 }
1853 } else {
1854 // On a floating point condition, the flags are set as follows:
1855 // ZF PF CF op
1856 // 0 | 0 | 0 | X > Y
1857 // 0 | 0 | 1 | X < Y
1858 // 1 | 0 | 0 | X == Y
1859 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001860 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001861 switch (SetCCOpcode) {
1862 default: break;
1863 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001864 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001865 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001866 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001867 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001868 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001869 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001870 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001871 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001872 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001873 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001874 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001875 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001876 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001877 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001878 case ISD::SETNE: X86CC = X86::COND_NE; break;
1879 case ISD::SETUO: X86CC = X86::COND_P; break;
1880 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001881 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001882 if (Flip)
1883 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001884 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001885
Chris Lattner7fbe9722006-10-20 17:42:20 +00001886 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001887}
1888
Evan Cheng4a460802006-01-11 00:33:36 +00001889/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1890/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001891/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001892static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001893 switch (X86CC) {
1894 default:
1895 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001896 case X86::COND_B:
1897 case X86::COND_BE:
1898 case X86::COND_E:
1899 case X86::COND_P:
1900 case X86::COND_A:
1901 case X86::COND_AE:
1902 case X86::COND_NE:
1903 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00001904 return true;
1905 }
1906}
1907
Evan Cheng5ced1d82006-04-06 23:23:56 +00001908/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00001909/// true if Op is undef or if its value falls within the specified range (L, H].
Evan Cheng5ced1d82006-04-06 23:23:56 +00001910static bool isUndefOrInRange(SDOperand Op, unsigned Low, unsigned Hi) {
1911 if (Op.getOpcode() == ISD::UNDEF)
1912 return true;
1913
1914 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00001915 return (Val >= Low && Val < Hi);
1916}
1917
1918/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
1919/// true if Op is undef or if its value equal to the specified value.
1920static bool isUndefOrEqual(SDOperand Op, unsigned Val) {
1921 if (Op.getOpcode() == ISD::UNDEF)
1922 return true;
1923 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00001924}
1925
Evan Cheng0188ecb2006-03-22 18:59:22 +00001926/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
1927/// specifies a shuffle of elements that is suitable for input to PSHUFD.
1928bool X86::isPSHUFDMask(SDNode *N) {
1929 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1930
Dan Gohman7f55fcb2007-08-02 21:17:01 +00001931 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00001932 return false;
1933
1934 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00001935 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001936 SDOperand Arg = N->getOperand(i);
1937 if (Arg.getOpcode() == ISD::UNDEF) continue;
1938 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00001939 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00001940 return false;
1941 }
1942
1943 return true;
1944}
1945
1946/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001947/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001948bool X86::isPSHUFHWMask(SDNode *N) {
1949 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1950
1951 if (N->getNumOperands() != 8)
1952 return false;
1953
1954 // Lower quadword copied in order.
1955 for (unsigned i = 0; i != 4; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001956 SDOperand Arg = N->getOperand(i);
1957 if (Arg.getOpcode() == ISD::UNDEF) continue;
1958 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1959 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00001960 return false;
1961 }
1962
1963 // Upper quadword shuffled.
1964 for (unsigned i = 4; i != 8; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00001965 SDOperand Arg = N->getOperand(i);
1966 if (Arg.getOpcode() == ISD::UNDEF) continue;
1967 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
1968 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00001969 if (Val < 4 || Val > 7)
1970 return false;
1971 }
1972
1973 return true;
1974}
1975
1976/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00001977/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00001978bool X86::isPSHUFLWMask(SDNode *N) {
1979 assert(N->getOpcode() == ISD::BUILD_VECTOR);
1980
1981 if (N->getNumOperands() != 8)
1982 return false;
1983
1984 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00001985 for (unsigned i = 4; i != 8; ++i)
1986 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00001987 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00001988
1989 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00001990 for (unsigned i = 0; i != 4; ++i)
1991 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00001992 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00001993
1994 return true;
1995}
1996
Evan Cheng14aed5e2006-03-24 01:18:28 +00001997/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
1998/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Chris Lattner5a88b832007-02-25 07:10:00 +00001999static bool isSHUFPMask(const SDOperand *Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002000 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002001
Evan Cheng39623da2006-04-20 08:58:49 +00002002 unsigned Half = NumElems / 2;
2003 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002004 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002005 return false;
2006 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002007 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002008 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002009
2010 return true;
2011}
2012
Evan Cheng39623da2006-04-20 08:58:49 +00002013bool X86::isSHUFPMask(SDNode *N) {
2014 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002015 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002016}
2017
Evan Cheng213d2cf2007-05-17 18:45:50 +00002018/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002019/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2020/// half elements to come from vector 1 (which would equal the dest.) and
2021/// the upper half to come from vector 2.
Chris Lattner5a88b832007-02-25 07:10:00 +00002022static bool isCommutedSHUFP(const SDOperand *Ops, unsigned NumOps) {
2023 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002024
Chris Lattner5a88b832007-02-25 07:10:00 +00002025 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002026 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002027 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002028 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002029 for (unsigned i = Half; i < NumOps; ++i)
2030 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002031 return false;
2032 return true;
2033}
2034
2035static bool isCommutedSHUFP(SDNode *N) {
2036 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002037 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002038}
2039
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002040/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2041/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2042bool X86::isMOVHLPSMask(SDNode *N) {
2043 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2044
Evan Cheng2064a2b2006-03-28 06:50:32 +00002045 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002046 return false;
2047
Evan Cheng2064a2b2006-03-28 06:50:32 +00002048 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002049 return isUndefOrEqual(N->getOperand(0), 6) &&
2050 isUndefOrEqual(N->getOperand(1), 7) &&
2051 isUndefOrEqual(N->getOperand(2), 2) &&
2052 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002053}
2054
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002055/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2056/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2057/// <2, 3, 2, 3>
2058bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2059 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2060
2061 if (N->getNumOperands() != 4)
2062 return false;
2063
2064 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2065 return isUndefOrEqual(N->getOperand(0), 2) &&
2066 isUndefOrEqual(N->getOperand(1), 3) &&
2067 isUndefOrEqual(N->getOperand(2), 2) &&
2068 isUndefOrEqual(N->getOperand(3), 3);
2069}
2070
Evan Cheng5ced1d82006-04-06 23:23:56 +00002071/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2072/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2073bool X86::isMOVLPMask(SDNode *N) {
2074 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2075
2076 unsigned NumElems = N->getNumOperands();
2077 if (NumElems != 2 && NumElems != 4)
2078 return false;
2079
Evan Chengc5cdff22006-04-07 21:53:05 +00002080 for (unsigned i = 0; i < NumElems/2; ++i)
2081 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2082 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002083
Evan Chengc5cdff22006-04-07 21:53:05 +00002084 for (unsigned i = NumElems/2; i < NumElems; ++i)
2085 if (!isUndefOrEqual(N->getOperand(i), i))
2086 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002087
2088 return true;
2089}
2090
2091/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002092/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2093/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002094bool X86::isMOVHPMask(SDNode *N) {
2095 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2096
2097 unsigned NumElems = N->getNumOperands();
2098 if (NumElems != 2 && NumElems != 4)
2099 return false;
2100
Evan Chengc5cdff22006-04-07 21:53:05 +00002101 for (unsigned i = 0; i < NumElems/2; ++i)
2102 if (!isUndefOrEqual(N->getOperand(i), i))
2103 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002104
2105 for (unsigned i = 0; i < NumElems/2; ++i) {
2106 SDOperand Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002107 if (!isUndefOrEqual(Arg, i + NumElems))
2108 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002109 }
2110
2111 return true;
2112}
2113
Evan Cheng0038e592006-03-28 00:39:58 +00002114/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2115/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Chris Lattner5a88b832007-02-25 07:10:00 +00002116bool static isUNPCKLMask(const SDOperand *Elts, unsigned NumElts,
2117 bool V2IsSplat = false) {
2118 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002119 return false;
2120
Chris Lattner5a88b832007-02-25 07:10:00 +00002121 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2122 SDOperand BitI = Elts[i];
2123 SDOperand BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002124 if (!isUndefOrEqual(BitI, j))
2125 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002126 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002127 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002128 return false;
2129 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002130 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002131 return false;
2132 }
Evan Cheng0038e592006-03-28 00:39:58 +00002133 }
2134
2135 return true;
2136}
2137
Evan Cheng39623da2006-04-20 08:58:49 +00002138bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2139 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002140 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002141}
2142
Evan Cheng4fcb9222006-03-28 02:43:26 +00002143/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2144/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Chris Lattner5a88b832007-02-25 07:10:00 +00002145bool static isUNPCKHMask(const SDOperand *Elts, unsigned NumElts,
2146 bool V2IsSplat = false) {
2147 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002148 return false;
2149
Chris Lattner5a88b832007-02-25 07:10:00 +00002150 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
2151 SDOperand BitI = Elts[i];
2152 SDOperand BitI1 = Elts[i+1];
2153 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002154 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002155 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002156 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002157 return false;
2158 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002159 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002160 return false;
2161 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002162 }
2163
2164 return true;
2165}
2166
Evan Cheng39623da2006-04-20 08:58:49 +00002167bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2168 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002169 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002170}
2171
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002172/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2173/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2174/// <0, 0, 1, 1>
2175bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2176 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2177
2178 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002179 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002180 return false;
2181
2182 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
2183 SDOperand BitI = N->getOperand(i);
2184 SDOperand BitI1 = N->getOperand(i+1);
2185
Evan Chengc5cdff22006-04-07 21:53:05 +00002186 if (!isUndefOrEqual(BitI, j))
2187 return false;
2188 if (!isUndefOrEqual(BitI1, j))
2189 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002190 }
2191
2192 return true;
2193}
2194
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002195/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2196/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2197/// <2, 2, 3, 3>
2198bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2199 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2200
2201 unsigned NumElems = N->getNumOperands();
2202 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2203 return false;
2204
2205 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2206 SDOperand BitI = N->getOperand(i);
2207 SDOperand BitI1 = N->getOperand(i + 1);
2208
2209 if (!isUndefOrEqual(BitI, j))
2210 return false;
2211 if (!isUndefOrEqual(BitI1, j))
2212 return false;
2213 }
2214
2215 return true;
2216}
2217
Evan Cheng017dcc62006-04-21 01:05:10 +00002218/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2219/// specifies a shuffle of elements that is suitable for input to MOVSS,
2220/// MOVSD, and MOVD, i.e. setting the lowest element.
Chris Lattner5a88b832007-02-25 07:10:00 +00002221static bool isMOVLMask(const SDOperand *Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002222 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002223 return false;
2224
Chris Lattner5a88b832007-02-25 07:10:00 +00002225 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002226 return false;
2227
Chris Lattner5a88b832007-02-25 07:10:00 +00002228 for (unsigned i = 1; i < NumElts; ++i) {
2229 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002230 return false;
2231 }
2232
2233 return true;
2234}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002235
Evan Cheng017dcc62006-04-21 01:05:10 +00002236bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002237 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002238 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002239}
2240
Evan Cheng017dcc62006-04-21 01:05:10 +00002241/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2242/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002243/// element of vector 2 and the other elements to come from vector 1 in order.
Chris Lattner5a88b832007-02-25 07:10:00 +00002244static bool isCommutedMOVL(const SDOperand *Ops, unsigned NumOps,
2245 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002246 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002247 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002248 return false;
2249
2250 if (!isUndefOrEqual(Ops[0], 0))
2251 return false;
2252
Chris Lattner5a88b832007-02-25 07:10:00 +00002253 for (unsigned i = 1; i < NumOps; ++i) {
Evan Cheng39623da2006-04-20 08:58:49 +00002254 SDOperand Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002255 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2256 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2257 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002258 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002259 }
2260
2261 return true;
2262}
2263
Evan Cheng8cf723d2006-09-08 01:50:06 +00002264static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2265 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002266 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002267 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2268 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002269}
2270
Evan Chengd9539472006-04-14 21:59:03 +00002271/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2272/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2273bool X86::isMOVSHDUPMask(SDNode *N) {
2274 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2275
2276 if (N->getNumOperands() != 4)
2277 return false;
2278
2279 // Expect 1, 1, 3, 3
2280 for (unsigned i = 0; i < 2; ++i) {
2281 SDOperand Arg = N->getOperand(i);
2282 if (Arg.getOpcode() == ISD::UNDEF) continue;
2283 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2284 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2285 if (Val != 1) return false;
2286 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002287
2288 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002289 for (unsigned i = 2; i < 4; ++i) {
2290 SDOperand Arg = N->getOperand(i);
2291 if (Arg.getOpcode() == ISD::UNDEF) continue;
2292 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2293 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2294 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002295 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002296 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002297
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002298 // Don't use movshdup if it can be done with a shufps.
2299 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002300}
2301
2302/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2304bool X86::isMOVSLDUPMask(SDNode *N) {
2305 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2306
2307 if (N->getNumOperands() != 4)
2308 return false;
2309
2310 // Expect 0, 0, 2, 2
2311 for (unsigned i = 0; i < 2; ++i) {
2312 SDOperand Arg = N->getOperand(i);
2313 if (Arg.getOpcode() == ISD::UNDEF) continue;
2314 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2315 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2316 if (Val != 0) return false;
2317 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002318
2319 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002320 for (unsigned i = 2; i < 4; ++i) {
2321 SDOperand Arg = N->getOperand(i);
2322 if (Arg.getOpcode() == ISD::UNDEF) continue;
2323 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2324 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2325 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002326 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002327 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002328
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002329 // Don't use movshdup if it can be done with a shufps.
2330 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002331}
2332
Evan Cheng49892af2007-06-19 00:02:56 +00002333/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2334/// specifies a identity operation on the LHS or RHS.
2335static bool isIdentityMask(SDNode *N, bool RHS = false) {
2336 unsigned NumElems = N->getNumOperands();
2337 for (unsigned i = 0; i < NumElems; ++i)
2338 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2339 return false;
2340 return true;
2341}
2342
Evan Chengb9df0ca2006-03-22 02:53:00 +00002343/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2344/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002345static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002346 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2347
Evan Chengb9df0ca2006-03-22 02:53:00 +00002348 // This is a splat operation if each element of the permute is the same, and
2349 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002350 unsigned NumElems = N->getNumOperands();
2351 SDOperand ElementBase;
2352 unsigned i = 0;
2353 for (; i != NumElems; ++i) {
2354 SDOperand Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002355 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002356 ElementBase = Elt;
2357 break;
2358 }
2359 }
2360
2361 if (!ElementBase.Val)
2362 return false;
2363
2364 for (; i != NumElems; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002365 SDOperand Arg = N->getOperand(i);
2366 if (Arg.getOpcode() == ISD::UNDEF) continue;
2367 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002368 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002369 }
2370
2371 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002372 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002373}
2374
Evan Chengc575ca22006-04-17 20:43:08 +00002375/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2376/// a splat of a single element and it's a 2 or 4 element mask.
2377bool X86::isSplatMask(SDNode *N) {
2378 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2379
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002380 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002381 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2382 return false;
2383 return ::isSplatMask(N);
2384}
2385
Evan Chengf686d9b2006-10-27 21:08:32 +00002386/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2387/// specifies a splat of zero element.
2388bool X86::isSplatLoMask(SDNode *N) {
2389 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2390
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002391 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002392 if (!isUndefOrEqual(N->getOperand(i), 0))
2393 return false;
2394 return true;
2395}
2396
Evan Cheng63d33002006-03-22 08:01:21 +00002397/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2398/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2399/// instructions.
2400unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002401 unsigned NumOperands = N->getNumOperands();
2402 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2403 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002404 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002405 unsigned Val = 0;
2406 SDOperand Arg = N->getOperand(NumOperands-i-1);
2407 if (Arg.getOpcode() != ISD::UNDEF)
2408 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002409 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002410 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002411 if (i != NumOperands - 1)
2412 Mask <<= Shift;
2413 }
Evan Cheng63d33002006-03-22 08:01:21 +00002414
2415 return Mask;
2416}
2417
Evan Cheng506d3df2006-03-29 23:07:14 +00002418/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2419/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2420/// instructions.
2421unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2422 unsigned Mask = 0;
2423 // 8 nodes, but we only care about the last 4.
2424 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002425 unsigned Val = 0;
2426 SDOperand Arg = N->getOperand(i);
2427 if (Arg.getOpcode() != ISD::UNDEF)
2428 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002429 Mask |= (Val - 4);
2430 if (i != 4)
2431 Mask <<= 2;
2432 }
2433
2434 return Mask;
2435}
2436
2437/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2438/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2439/// instructions.
2440unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2441 unsigned Mask = 0;
2442 // 8 nodes, but we only care about the first 4.
2443 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002444 unsigned Val = 0;
2445 SDOperand Arg = N->getOperand(i);
2446 if (Arg.getOpcode() != ISD::UNDEF)
2447 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002448 Mask |= Val;
2449 if (i != 0)
2450 Mask <<= 2;
2451 }
2452
2453 return Mask;
2454}
2455
Evan Chengc21a0532006-04-05 01:47:37 +00002456/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2457/// specifies a 8 element shuffle that can be broken into a pair of
2458/// PSHUFHW and PSHUFLW.
2459static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2460 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2461
2462 if (N->getNumOperands() != 8)
2463 return false;
2464
2465 // Lower quadword shuffled.
2466 for (unsigned i = 0; i != 4; ++i) {
2467 SDOperand Arg = N->getOperand(i);
2468 if (Arg.getOpcode() == ISD::UNDEF) continue;
2469 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2470 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002471 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002472 return false;
2473 }
2474
2475 // Upper quadword shuffled.
2476 for (unsigned i = 4; i != 8; ++i) {
2477 SDOperand Arg = N->getOperand(i);
2478 if (Arg.getOpcode() == ISD::UNDEF) continue;
2479 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2480 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2481 if (Val < 4 || Val > 7)
2482 return false;
2483 }
2484
2485 return true;
2486}
2487
Chris Lattner8a594482007-11-25 00:24:49 +00002488/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002489/// values in ther permute mask.
Evan Cheng9eca5e82006-10-25 21:49:50 +00002490static SDOperand CommuteVectorShuffle(SDOperand Op, SDOperand &V1,
2491 SDOperand &V2, SDOperand &Mask,
2492 SelectionDAG &DAG) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002493 MVT::ValueType VT = Op.getValueType();
2494 MVT::ValueType MaskVT = Mask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002495 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002496 unsigned NumElems = Mask.getNumOperands();
Chris Lattner5a88b832007-02-25 07:10:00 +00002497 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002498
2499 for (unsigned i = 0; i != NumElems; ++i) {
2500 SDOperand Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002501 if (Arg.getOpcode() == ISD::UNDEF) {
2502 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2503 continue;
2504 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002505 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2506 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2507 if (Val < NumElems)
2508 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2509 else
2510 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2511 }
2512
Evan Cheng9eca5e82006-10-25 21:49:50 +00002513 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002514 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002515 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002516}
2517
Evan Cheng779ccea2007-12-07 21:30:01 +00002518/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2519/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002520static
2521SDOperand CommuteVectorShuffleMask(SDOperand Mask, SelectionDAG &DAG) {
2522 MVT::ValueType MaskVT = Mask.getValueType();
2523 MVT::ValueType EltVT = MVT::getVectorElementType(MaskVT);
2524 unsigned NumElems = Mask.getNumOperands();
2525 SmallVector<SDOperand, 8> MaskVec;
2526 for (unsigned i = 0; i != NumElems; ++i) {
2527 SDOperand Arg = Mask.getOperand(i);
2528 if (Arg.getOpcode() == ISD::UNDEF) {
2529 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2530 continue;
2531 }
2532 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2533 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2534 if (Val < NumElems)
2535 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2536 else
2537 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2538 }
2539 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2540}
2541
2542
Evan Cheng533a0aa2006-04-19 20:35:22 +00002543/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2544/// match movhlps. The lower half elements should come from upper half of
2545/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002546/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002547static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2548 unsigned NumElems = Mask->getNumOperands();
2549 if (NumElems != 4)
2550 return false;
2551 for (unsigned i = 0, e = 2; i != e; ++i)
2552 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2553 return false;
2554 for (unsigned i = 2; i != 4; ++i)
2555 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2556 return false;
2557 return true;
2558}
2559
Evan Cheng5ced1d82006-04-06 23:23:56 +00002560/// isScalarLoadToVector - Returns true if the node is a scalar load that
2561/// is promoted to a vector.
Evan Cheng533a0aa2006-04-19 20:35:22 +00002562static inline bool isScalarLoadToVector(SDNode *N) {
2563 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2564 N = N->getOperand(0).Val;
Evan Cheng466685d2006-10-09 20:57:25 +00002565 return ISD::isNON_EXTLoad(N);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002566 }
2567 return false;
2568}
2569
Evan Cheng533a0aa2006-04-19 20:35:22 +00002570/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2571/// match movlp{s|d}. The lower half elements should come from lower half of
2572/// V1 (and in order), and the upper half elements should come from the upper
2573/// half of V2 (and in order). And since V1 will become the source of the
2574/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002575static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002576 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002577 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002578 // Is V2 is a vector load, don't do this transformation. We will try to use
2579 // load folding shufps op.
2580 if (ISD::isNON_EXTLoad(V2))
2581 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002582
Evan Cheng533a0aa2006-04-19 20:35:22 +00002583 unsigned NumElems = Mask->getNumOperands();
2584 if (NumElems != 2 && NumElems != 4)
2585 return false;
2586 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2587 if (!isUndefOrEqual(Mask->getOperand(i), i))
2588 return false;
2589 for (unsigned i = NumElems/2; i != NumElems; ++i)
2590 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2591 return false;
2592 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002593}
2594
Evan Cheng39623da2006-04-20 08:58:49 +00002595/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2596/// all the same.
2597static bool isSplatVector(SDNode *N) {
2598 if (N->getOpcode() != ISD::BUILD_VECTOR)
2599 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002600
Evan Cheng39623da2006-04-20 08:58:49 +00002601 SDOperand SplatValue = N->getOperand(0);
2602 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2603 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002604 return false;
2605 return true;
2606}
2607
Evan Cheng8cf723d2006-09-08 01:50:06 +00002608/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2609/// to an undef.
2610static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002611 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002612 return false;
2613
2614 SDOperand V1 = N->getOperand(0);
2615 SDOperand V2 = N->getOperand(1);
2616 SDOperand Mask = N->getOperand(2);
2617 unsigned NumElems = Mask.getNumOperands();
2618 for (unsigned i = 0; i != NumElems; ++i) {
2619 SDOperand Arg = Mask.getOperand(i);
2620 if (Arg.getOpcode() != ISD::UNDEF) {
2621 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2622 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2623 return false;
2624 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2625 return false;
2626 }
2627 }
2628 return true;
2629}
2630
Evan Cheng213d2cf2007-05-17 18:45:50 +00002631/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2632/// constant +0.0.
2633static inline bool isZeroNode(SDOperand Elt) {
2634 return ((isa<ConstantSDNode>(Elt) &&
2635 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2636 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002637 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002638}
2639
2640/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2641/// to an zero vector.
2642static bool isZeroShuffle(SDNode *N) {
2643 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2644 return false;
2645
2646 SDOperand V1 = N->getOperand(0);
2647 SDOperand V2 = N->getOperand(1);
2648 SDOperand Mask = N->getOperand(2);
2649 unsigned NumElems = Mask.getNumOperands();
2650 for (unsigned i = 0; i != NumElems; ++i) {
2651 SDOperand Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002652 if (Arg.getOpcode() == ISD::UNDEF)
2653 continue;
2654
2655 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2656 if (Idx < NumElems) {
2657 unsigned Opc = V1.Val->getOpcode();
2658 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2659 continue;
2660 if (Opc != ISD::BUILD_VECTOR ||
2661 !isZeroNode(V1.Val->getOperand(Idx)))
2662 return false;
2663 } else if (Idx >= NumElems) {
2664 unsigned Opc = V2.Val->getOpcode();
2665 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2666 continue;
2667 if (Opc != ISD::BUILD_VECTOR ||
2668 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2669 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002670 }
2671 }
2672 return true;
2673}
2674
2675/// getZeroVector - Returns a vector of specified type with all zero elements.
2676///
2677static SDOperand getZeroVector(MVT::ValueType VT, SelectionDAG &DAG) {
2678 assert(MVT::isVector(VT) && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002679
2680 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2681 // type. This ensures they get CSE'd.
2682 SDOperand Cst = DAG.getTargetConstant(0, MVT::i32);
2683 SDOperand Vec;
2684 if (MVT::getSizeInBits(VT) == 64) // MMX
2685 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2686 else // SSE
2687 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2688 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002689}
2690
Chris Lattner8a594482007-11-25 00:24:49 +00002691/// getOnesVector - Returns a vector of specified type with all bits set.
2692///
2693static SDOperand getOnesVector(MVT::ValueType VT, SelectionDAG &DAG) {
2694 assert(MVT::isVector(VT) && "Expected a vector type");
2695
2696 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2697 // type. This ensures they get CSE'd.
2698 SDOperand Cst = DAG.getTargetConstant(~0U, MVT::i32);
2699 SDOperand Vec;
2700 if (MVT::getSizeInBits(VT) == 64) // MMX
2701 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2702 else // SSE
2703 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2704 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2705}
2706
2707
Evan Cheng39623da2006-04-20 08:58:49 +00002708/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2709/// that point to V2 points to its first element.
2710static SDOperand NormalizeMask(SDOperand Mask, SelectionDAG &DAG) {
2711 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2712
2713 bool Changed = false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002714 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002715 unsigned NumElems = Mask.getNumOperands();
2716 for (unsigned i = 0; i != NumElems; ++i) {
2717 SDOperand Arg = Mask.getOperand(i);
2718 if (Arg.getOpcode() != ISD::UNDEF) {
2719 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2720 if (Val > NumElems) {
2721 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2722 Changed = true;
2723 }
2724 }
2725 MaskVec.push_back(Arg);
2726 }
2727
2728 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002729 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2730 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002731 return Mask;
2732}
2733
Evan Cheng017dcc62006-04-21 01:05:10 +00002734/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2735/// operation of specified width.
2736static SDOperand getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002737 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002738 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002739
Chris Lattner5a88b832007-02-25 07:10:00 +00002740 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002741 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2742 for (unsigned i = 1; i != NumElems; ++i)
2743 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002744 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002745}
2746
Evan Chengc575ca22006-04-17 20:43:08 +00002747/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2748/// of specified width.
2749static SDOperand getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
2750 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002751 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002752 SmallVector<SDOperand, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002753 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2754 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2755 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2756 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002757 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002758}
2759
Evan Cheng39623da2006-04-20 08:58:49 +00002760/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2761/// of specified width.
2762static SDOperand getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
2763 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002764 MVT::ValueType BaseVT = MVT::getVectorElementType(MaskVT);
Evan Cheng39623da2006-04-20 08:58:49 +00002765 unsigned Half = NumElems/2;
Chris Lattner5a88b832007-02-25 07:10:00 +00002766 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002767 for (unsigned i = 0; i != Half; ++i) {
2768 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2769 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2770 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002771 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002772}
2773
Evan Chengc575ca22006-04-17 20:43:08 +00002774/// PromoteSplat - Promote a splat of v8i16 or v16i8 to v4i32.
2775///
2776static SDOperand PromoteSplat(SDOperand Op, SelectionDAG &DAG) {
2777 SDOperand V1 = Op.getOperand(0);
Evan Cheng017dcc62006-04-21 01:05:10 +00002778 SDOperand Mask = Op.getOperand(2);
Evan Chengc575ca22006-04-17 20:43:08 +00002779 MVT::ValueType VT = Op.getValueType();
Evan Cheng017dcc62006-04-21 01:05:10 +00002780 unsigned NumElems = Mask.getNumOperands();
2781 Mask = getUnpacklMask(NumElems, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002782 while (NumElems != 4) {
Evan Cheng017dcc62006-04-21 01:05:10 +00002783 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002784 NumElems >>= 1;
2785 }
2786 V1 = DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, V1);
2787
Chris Lattner8a594482007-11-25 00:24:49 +00002788 Mask = getZeroVector(MVT::v4i32, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002789 SDOperand Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32, V1,
Evan Cheng017dcc62006-04-21 01:05:10 +00002790 DAG.getNode(ISD::UNDEF, MVT::v4i32), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002791 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2792}
2793
Evan Chengba05f722006-04-21 23:03:30 +00002794/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002795/// vector of zero or undef vector. This produces a shuffle where the low
2796/// element of V2 is swizzled into the zero/undef vector, landing at element
2797/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Evan Chengba05f722006-04-21 23:03:30 +00002798static SDOperand getShuffleVectorZeroOrUndef(SDOperand V2, MVT::ValueType VT,
Evan Cheng017dcc62006-04-21 01:05:10 +00002799 unsigned NumElems, unsigned Idx,
Evan Chengba05f722006-04-21 23:03:30 +00002800 bool isZero, SelectionDAG &DAG) {
2801 SDOperand V1 = isZero ? getZeroVector(VT, DAG) : DAG.getNode(ISD::UNDEF, VT);
Evan Cheng017dcc62006-04-21 01:05:10 +00002802 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002803 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner8a594482007-11-25 00:24:49 +00002804 SmallVector<SDOperand, 16> MaskVec;
2805 for (unsigned i = 0; i != NumElems; ++i)
2806 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2807 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2808 else
2809 MaskVec.push_back(DAG.getConstant(i, EVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002810 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2811 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002812 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002813}
2814
Evan Chengc78d3b42006-04-24 18:01:45 +00002815/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2816///
2817static SDOperand LowerBuildVectorv16i8(SDOperand Op, unsigned NonZeros,
2818 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002819 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002820 if (NumNonZero > 8)
2821 return SDOperand();
2822
2823 SDOperand V(0, 0);
2824 bool First = true;
2825 for (unsigned i = 0; i < 16; ++i) {
2826 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2827 if (ThisIsNonZero && First) {
2828 if (NumZero)
2829 V = getZeroVector(MVT::v8i16, DAG);
2830 else
2831 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2832 First = false;
2833 }
2834
2835 if ((i & 1) != 0) {
2836 SDOperand ThisElt(0, 0), LastElt(0, 0);
2837 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2838 if (LastIsNonZero) {
2839 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
2840 }
2841 if (ThisIsNonZero) {
2842 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
2843 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
2844 ThisElt, DAG.getConstant(8, MVT::i8));
2845 if (LastIsNonZero)
2846 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
2847 } else
2848 ThisElt = LastElt;
2849
2850 if (ThisElt.Val)
2851 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002852 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002853 }
2854 }
2855
2856 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
2857}
2858
Bill Wendlinga348c562007-03-22 18:42:45 +00002859/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002860///
2861static SDOperand LowerBuildVectorv8i16(SDOperand Op, unsigned NonZeros,
2862 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002863 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002864 if (NumNonZero > 4)
2865 return SDOperand();
2866
2867 SDOperand V(0, 0);
2868 bool First = true;
2869 for (unsigned i = 0; i < 8; ++i) {
2870 bool isNonZero = (NonZeros & (1 << i)) != 0;
2871 if (isNonZero) {
2872 if (First) {
2873 if (NumZero)
2874 V = getZeroVector(MVT::v8i16, DAG);
2875 else
2876 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
2877 First = false;
2878 }
2879 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00002880 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00002881 }
2882 }
2883
2884 return V;
2885}
2886
Evan Cheng0db9fe62006-04-25 20:13:52 +00002887SDOperand
2888X86TargetLowering::LowerBUILD_VECTOR(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00002889 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
2890 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
2891 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
2892 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
2893 // eliminated on x86-32 hosts.
2894 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
2895 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002896
Chris Lattner8a594482007-11-25 00:24:49 +00002897 if (ISD::isBuildVectorAllOnes(Op.Val))
2898 return getOnesVector(Op.getValueType(), DAG);
2899 return getZeroVector(Op.getValueType(), DAG);
2900 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002901
2902 MVT::ValueType VT = Op.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00002903 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002904 unsigned EVTBits = MVT::getSizeInBits(EVT);
2905
2906 unsigned NumElems = Op.getNumOperands();
2907 unsigned NumZero = 0;
2908 unsigned NumNonZero = 0;
2909 unsigned NonZeros = 0;
Evan Chengdb2d5242007-12-12 06:45:40 +00002910 bool HasNonImms = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00002911 SmallSet<SDOperand, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002912 for (unsigned i = 0; i < NumElems; ++i) {
2913 SDOperand Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00002914 if (Elt.getOpcode() == ISD::UNDEF)
2915 continue;
2916 Values.insert(Elt);
2917 if (Elt.getOpcode() != ISD::Constant &&
2918 Elt.getOpcode() != ISD::ConstantFP)
2919 HasNonImms = true;
2920 if (isZeroNode(Elt))
2921 NumZero++;
2922 else {
2923 NonZeros |= (1 << i);
2924 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002925 }
2926 }
2927
Dan Gohman7f321562007-06-25 16:23:39 +00002928 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00002929 // All undef vector. Return an UNDEF. All zero vectors were handled above.
2930 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00002931 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00002932
2933 // Splat is obviously ok. Let legalizer expand it to a shuffle.
2934 if (Values.size() == 1)
2935 return SDOperand();
2936
2937 // Special case for single non-zero element.
Evan Chengdb2d5242007-12-12 06:45:40 +00002938 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00002939 unsigned Idx = CountTrailingZeros_32(NonZeros);
2940 SDOperand Item = Op.getOperand(Idx);
2941 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
2942 if (Idx == 0)
2943 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
2944 return getShuffleVectorZeroOrUndef(Item, VT, NumElems, Idx,
2945 NumZero > 0, DAG);
Evan Chengdb2d5242007-12-12 06:45:40 +00002946 else if (!HasNonImms) // Otherwise, it's better to do a constpool load.
2947 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00002948
2949 if (EVTBits == 32) {
2950 // Turn it into a shuffle of zero and zero-extended scalar to vector.
2951 Item = getShuffleVectorZeroOrUndef(Item, VT, NumElems, 0, NumZero > 0,
2952 DAG);
2953 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00002954 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00002955 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00002956 for (unsigned i = 0; i < NumElems; i++)
2957 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002958 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
2959 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00002960 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
2961 DAG.getNode(ISD::UNDEF, VT), Mask);
2962 }
2963 }
2964
Dan Gohmana3941172007-07-24 22:55:08 +00002965 // A vector full of immediates; various special cases are already
2966 // handled, so this is best done with a single constant-pool load.
Evan Chengdb2d5242007-12-12 06:45:40 +00002967 if (!HasNonImms)
Dan Gohmana3941172007-07-24 22:55:08 +00002968 return SDOperand();
2969
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002970 // Let legalizer expand 2-wide build_vectors.
Evan Cheng0db9fe62006-04-25 20:13:52 +00002971 if (EVTBits == 64)
2972 return SDOperand();
2973
2974 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00002975 if (EVTBits == 8 && NumElems == 16) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002976 SDOperand V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
2977 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002978 if (V.Val) return V;
2979 }
2980
Bill Wendling826f36f2007-03-28 00:57:11 +00002981 if (EVTBits == 16 && NumElems == 8) {
Evan Cheng25ab6902006-09-08 06:48:29 +00002982 SDOperand V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
2983 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002984 if (V.Val) return V;
2985 }
2986
2987 // If element VT is == 32 bits, turn it into a number of shuffles.
Chris Lattner5a88b832007-02-25 07:10:00 +00002988 SmallVector<SDOperand, 8> V;
2989 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00002990 if (NumElems == 4 && NumZero > 0) {
2991 for (unsigned i = 0; i < 4; ++i) {
2992 bool isZero = !(NonZeros & (1 << i));
2993 if (isZero)
2994 V[i] = getZeroVector(VT, DAG);
2995 else
2996 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
2997 }
2998
2999 for (unsigned i = 0; i < 2; ++i) {
3000 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3001 default: break;
3002 case 0:
3003 V[i] = V[i*2]; // Must be a zero vector.
3004 break;
3005 case 1:
3006 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3007 getMOVLMask(NumElems, DAG));
3008 break;
3009 case 2:
3010 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3011 getMOVLMask(NumElems, DAG));
3012 break;
3013 case 3:
3014 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3015 getUnpacklMask(NumElems, DAG));
3016 break;
3017 }
3018 }
3019
Evan Cheng069287d2006-05-16 07:21:53 +00003020 // Take advantage of the fact GR32 to VR128 scalar_to_vector (i.e. movd)
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003021 // clears the upper bits.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003022 // FIXME: we can do the same for v4f32 case when we know both parts of
3023 // the lower half come from scalar_to_vector (loadf32). We should do
3024 // that in post legalizer dag combiner with target specific hooks.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003025 if (MVT::isInteger(EVT) && (NonZeros & (0x3 << 2)) == 0)
Evan Cheng0db9fe62006-04-25 20:13:52 +00003026 return V[0];
3027 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NumElems);
Dan Gohman51eaa862007-06-14 22:58:02 +00003028 MVT::ValueType EVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003029 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003030 bool Reverse = (NonZeros & 0x3) == 2;
3031 for (unsigned i = 0; i < 2; ++i)
3032 if (Reverse)
3033 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3034 else
3035 MaskVec.push_back(DAG.getConstant(i, EVT));
3036 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3037 for (unsigned i = 0; i < 2; ++i)
3038 if (Reverse)
3039 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3040 else
3041 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Chris Lattnere2199452006-08-11 17:38:39 +00003042 SDOperand ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3043 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003044 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3045 }
3046
3047 if (Values.size() > 2) {
3048 // Expand into a number of unpckl*.
3049 // e.g. for v4f32
3050 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3051 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3052 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
3053 SDOperand UnpckMask = getUnpacklMask(NumElems, DAG);
3054 for (unsigned i = 0; i < NumElems; ++i)
3055 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3056 NumElems >>= 1;
3057 while (NumElems != 0) {
3058 for (unsigned i = 0; i < NumElems; ++i)
3059 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3060 UnpckMask);
3061 NumElems >>= 1;
3062 }
3063 return V[0];
3064 }
3065
3066 return SDOperand();
3067}
3068
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003069static
3070SDOperand LowerVECTOR_SHUFFLEv8i16(SDOperand V1, SDOperand V2,
3071 SDOperand PermMask, SelectionDAG &DAG,
3072 TargetLowering &TLI) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003073 SDOperand NewV;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003074 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(8);
3075 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Evan Cheng14b32e12007-12-11 01:46:18 +00003076 MVT::ValueType PtrVT = TLI.getPointerTy();
3077 SmallVector<SDOperand, 8> MaskElts(PermMask.Val->op_begin(),
3078 PermMask.Val->op_end());
3079
3080 // First record which half of which vector the low elements come from.
3081 SmallVector<unsigned, 4> LowQuad(4);
3082 for (unsigned i = 0; i < 4; ++i) {
3083 SDOperand Elt = MaskElts[i];
3084 if (Elt.getOpcode() == ISD::UNDEF)
3085 continue;
3086 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3087 int QuadIdx = EltIdx / 4;
3088 ++LowQuad[QuadIdx];
3089 }
3090 int BestLowQuad = -1;
3091 unsigned MaxQuad = 1;
3092 for (unsigned i = 0; i < 4; ++i) {
3093 if (LowQuad[i] > MaxQuad) {
3094 BestLowQuad = i;
3095 MaxQuad = LowQuad[i];
3096 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003097 }
3098
Evan Cheng14b32e12007-12-11 01:46:18 +00003099 // Record which half of which vector the high elements come from.
3100 SmallVector<unsigned, 4> HighQuad(4);
3101 for (unsigned i = 4; i < 8; ++i) {
3102 SDOperand Elt = MaskElts[i];
3103 if (Elt.getOpcode() == ISD::UNDEF)
3104 continue;
3105 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3106 int QuadIdx = EltIdx / 4;
3107 ++HighQuad[QuadIdx];
3108 }
3109 int BestHighQuad = -1;
3110 MaxQuad = 1;
3111 for (unsigned i = 0; i < 4; ++i) {
3112 if (HighQuad[i] > MaxQuad) {
3113 BestHighQuad = i;
3114 MaxQuad = HighQuad[i];
3115 }
3116 }
3117
3118 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3119 if (BestLowQuad != -1 || BestHighQuad != -1) {
3120 // First sort the 4 chunks in order using shufpd.
3121 SmallVector<SDOperand, 8> MaskVec;
3122 if (BestLowQuad != -1)
3123 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3124 else
3125 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3126 if (BestHighQuad != -1)
3127 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3128 else
3129 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
3130 SDOperand Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
3131 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3132 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3133 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3134 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3135
3136 // Now sort high and low parts separately.
3137 BitVector InOrder(8);
3138 if (BestLowQuad != -1) {
3139 // Sort lower half in order using PSHUFLW.
3140 MaskVec.clear();
3141 bool AnyOutOrder = false;
3142 for (unsigned i = 0; i != 4; ++i) {
3143 SDOperand Elt = MaskElts[i];
3144 if (Elt.getOpcode() == ISD::UNDEF) {
3145 MaskVec.push_back(Elt);
3146 InOrder.set(i);
3147 } else {
3148 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3149 if (EltIdx != i)
3150 AnyOutOrder = true;
3151 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3152 // If this element is in the right place after this shuffle, then
3153 // remember it.
3154 if ((int)(EltIdx / 4) == BestLowQuad)
3155 InOrder.set(i);
3156 }
3157 }
3158 if (AnyOutOrder) {
3159 for (unsigned i = 4; i != 8; ++i)
3160 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3161 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3162 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3163 }
3164 }
3165
3166 if (BestHighQuad != -1) {
3167 // Sort high half in order using PSHUFHW if possible.
3168 MaskVec.clear();
3169 for (unsigned i = 0; i != 4; ++i)
3170 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3171 bool AnyOutOrder = false;
3172 for (unsigned i = 4; i != 8; ++i) {
3173 SDOperand Elt = MaskElts[i];
3174 if (Elt.getOpcode() == ISD::UNDEF) {
3175 MaskVec.push_back(Elt);
3176 InOrder.set(i);
3177 } else {
3178 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3179 if (EltIdx != i)
3180 AnyOutOrder = true;
3181 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3182 // If this element is in the right place after this shuffle, then
3183 // remember it.
3184 if ((int)(EltIdx / 4) == BestHighQuad)
3185 InOrder.set(i);
3186 }
3187 }
3188 if (AnyOutOrder) {
3189 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3190 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3191 }
3192 }
3193
3194 // The other elements are put in the right place using pextrw and pinsrw.
3195 for (unsigned i = 0; i != 8; ++i) {
3196 if (InOrder[i])
3197 continue;
3198 SDOperand Elt = MaskElts[i];
3199 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3200 if (EltIdx == i)
3201 continue;
3202 SDOperand ExtOp = (EltIdx < 8)
3203 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3204 DAG.getConstant(EltIdx, PtrVT))
3205 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3206 DAG.getConstant(EltIdx - 8, PtrVT));
3207 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3208 DAG.getConstant(i, PtrVT));
3209 }
3210 return NewV;
3211 }
3212
3213 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3214 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003215 // First, let's find out how many elements are already in the right order.
3216 unsigned V1InOrder = 0;
3217 unsigned V1FromV1 = 0;
3218 unsigned V2InOrder = 0;
3219 unsigned V2FromV2 = 0;
Evan Cheng14b32e12007-12-11 01:46:18 +00003220 SmallVector<SDOperand, 8> V1Elts;
3221 SmallVector<SDOperand, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003222 for (unsigned i = 0; i < 8; ++i) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003223 SDOperand Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003224 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003225 V1Elts.push_back(Elt);
3226 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003227 ++V1InOrder;
3228 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003229 continue;
3230 }
3231 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3232 if (EltIdx == i) {
3233 V1Elts.push_back(Elt);
3234 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3235 ++V1InOrder;
3236 } else if (EltIdx == i+8) {
3237 V1Elts.push_back(Elt);
3238 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3239 ++V2InOrder;
3240 } else if (EltIdx < 8) {
3241 V1Elts.push_back(Elt);
3242 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003243 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003244 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3245 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003246 }
3247 }
3248
3249 if (V2InOrder > V1InOrder) {
3250 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3251 std::swap(V1, V2);
3252 std::swap(V1Elts, V2Elts);
3253 std::swap(V1FromV1, V2FromV2);
3254 }
3255
Evan Cheng14b32e12007-12-11 01:46:18 +00003256 if ((V1FromV1 + V1InOrder) != 8) {
3257 // Some elements are from V2.
3258 if (V1FromV1) {
3259 // If there are elements that are from V1 but out of place,
3260 // then first sort them in place
3261 SmallVector<SDOperand, 8> MaskVec;
3262 for (unsigned i = 0; i < 8; ++i) {
3263 SDOperand Elt = V1Elts[i];
3264 if (Elt.getOpcode() == ISD::UNDEF) {
3265 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3266 continue;
3267 }
3268 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3269 if (EltIdx >= 8)
3270 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3271 else
3272 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3273 }
3274 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
3275 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003276 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003277
3278 NewV = V1;
3279 for (unsigned i = 0; i < 8; ++i) {
3280 SDOperand Elt = V1Elts[i];
3281 if (Elt.getOpcode() == ISD::UNDEF)
3282 continue;
3283 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3284 if (EltIdx < 8)
3285 continue;
3286 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3287 DAG.getConstant(EltIdx - 8, PtrVT));
3288 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3289 DAG.getConstant(i, PtrVT));
3290 }
3291 return NewV;
3292 } else {
3293 // All elements are from V1.
3294 NewV = V1;
3295 for (unsigned i = 0; i < 8; ++i) {
3296 SDOperand Elt = V1Elts[i];
3297 if (Elt.getOpcode() == ISD::UNDEF)
3298 continue;
3299 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3300 SDOperand ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3301 DAG.getConstant(EltIdx, PtrVT));
3302 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3303 DAG.getConstant(i, PtrVT));
3304 }
3305 return NewV;
3306 }
3307}
3308
Evan Cheng7a831ce2007-12-15 03:00:47 +00003309/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3310/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3311/// done when every pair / quad of shuffle mask elements point to elements in
3312/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003313/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3314static
Evan Cheng7a831ce2007-12-15 03:00:47 +00003315SDOperand RewriteAsNarrowerShuffle(SDOperand V1, SDOperand V2,
3316 MVT::ValueType VT,
Evan Cheng14b32e12007-12-11 01:46:18 +00003317 SDOperand PermMask, SelectionDAG &DAG,
3318 TargetLowering &TLI) {
3319 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003320 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
3321 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
3322 MVT::ValueType NewVT = MaskVT;
3323 switch (VT) {
3324 case MVT::v4f32: NewVT = MVT::v2f64; break;
3325 case MVT::v4i32: NewVT = MVT::v2i64; break;
3326 case MVT::v8i16: NewVT = MVT::v4i32; break;
3327 case MVT::v16i8: NewVT = MVT::v4i32; break;
3328 default: assert(false && "Unexpected!");
3329 }
3330
3331 if (NewWidth == 2)
3332 if (MVT::isInteger(VT))
3333 NewVT = MVT::v2i64;
3334 else
3335 NewVT = MVT::v2f64;
3336 unsigned Scale = NumElems / NewWidth;
3337 SmallVector<SDOperand, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003338 for (unsigned i = 0; i < NumElems; i += Scale) {
3339 unsigned StartIdx = ~0U;
3340 for (unsigned j = 0; j < Scale; ++j) {
3341 SDOperand Elt = PermMask.getOperand(i+j);
3342 if (Elt.getOpcode() == ISD::UNDEF)
3343 continue;
3344 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3345 if (StartIdx == ~0U)
3346 StartIdx = EltIdx - (EltIdx % Scale);
3347 if (EltIdx != StartIdx + j)
3348 return SDOperand();
3349 }
3350 if (StartIdx == ~0U)
3351 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MVT::i32));
3352 else
3353 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MVT::i32));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003354 }
3355
Evan Cheng7a831ce2007-12-15 03:00:47 +00003356 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3357 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3358 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3359 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3360 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003361}
3362
Evan Cheng0db9fe62006-04-25 20:13:52 +00003363SDOperand
3364X86TargetLowering::LowerVECTOR_SHUFFLE(SDOperand Op, SelectionDAG &DAG) {
3365 SDOperand V1 = Op.getOperand(0);
3366 SDOperand V2 = Op.getOperand(1);
3367 SDOperand PermMask = Op.getOperand(2);
3368 MVT::ValueType VT = Op.getValueType();
3369 unsigned NumElems = PermMask.getNumOperands();
3370 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3371 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003372 bool V1IsSplat = false;
3373 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003374
Evan Cheng8cf723d2006-09-08 01:50:06 +00003375 if (isUndefShuffle(Op.Val))
3376 return DAG.getNode(ISD::UNDEF, VT);
3377
Evan Cheng213d2cf2007-05-17 18:45:50 +00003378 if (isZeroShuffle(Op.Val))
3379 return getZeroVector(VT, DAG);
3380
Evan Cheng49892af2007-06-19 00:02:56 +00003381 if (isIdentityMask(PermMask.Val))
3382 return V1;
3383 else if (isIdentityMask(PermMask.Val, true))
3384 return V2;
3385
Evan Cheng0db9fe62006-04-25 20:13:52 +00003386 if (isSplatMask(PermMask.Val)) {
3387 if (NumElems <= 4) return Op;
3388 // Promote it to a v4i32 splat.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003389 return PromoteSplat(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003390 }
3391
Evan Cheng7a831ce2007-12-15 03:00:47 +00003392 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3393 // do it!
3394 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
3395 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3396 if (NewOp.Val)
3397 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3398 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3399 // FIXME: Figure out a cleaner way to do this.
3400 // Try to make use of movq to zero out the top part.
3401 if (ISD::isBuildVectorAllZeros(V2.Val)) {
3402 SDOperand NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3403 if (NewOp.Val) {
3404 SDOperand NewV1 = NewOp.getOperand(0);
3405 SDOperand NewV2 = NewOp.getOperand(1);
3406 SDOperand NewMask = NewOp.getOperand(2);
3407 if (isCommutedMOVL(NewMask.Val, true, false)) {
3408 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
3409 NewOp = DAG.getNode(ISD::VECTOR_SHUFFLE, NewOp.getValueType(),
3410 NewV1, NewV2, getMOVLMask(2, DAG));
3411 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3412 }
3413 }
3414 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
3415 SDOperand NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
3416 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
3417 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3418 }
3419 }
3420
Evan Cheng9bbbb982006-10-25 20:48:19 +00003421 if (X86::isMOVLMask(PermMask.Val))
3422 return (V1IsUndef) ? V2 : Op;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003423
Evan Cheng9bbbb982006-10-25 20:48:19 +00003424 if (X86::isMOVSHDUPMask(PermMask.Val) ||
3425 X86::isMOVSLDUPMask(PermMask.Val) ||
3426 X86::isMOVHLPSMask(PermMask.Val) ||
3427 X86::isMOVHPMask(PermMask.Val) ||
3428 X86::isMOVLPMask(PermMask.Val))
3429 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003430
Evan Cheng9bbbb982006-10-25 20:48:19 +00003431 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3432 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003433 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003434
Evan Cheng9eca5e82006-10-25 21:49:50 +00003435 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003436 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3437 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003438 V1IsSplat = isSplatVector(V1.Val);
3439 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003440
3441 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003442 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003443 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003444 std::swap(V1IsSplat, V2IsSplat);
3445 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003446 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003447 }
3448
Evan Cheng7a831ce2007-12-15 03:00:47 +00003449 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003450 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3451 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003452 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003453 if (V2IsSplat) {
3454 // V2 is a splat, so the mask may be malformed. That is, it may point
3455 // to any V2 element. The instruction selectior won't like this. Get
3456 // a corrected mask and commute to form a proper MOVS{S|D}.
3457 SDOperand NewMask = getMOVLMask(NumElems, DAG);
3458 if (NewMask.Val != PermMask.Val)
3459 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003460 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003461 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003462 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003463
Evan Chengd9b8e402006-10-16 06:36:00 +00003464 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003465 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003466 X86::isUNPCKLMask(PermMask.Val) ||
3467 X86::isUNPCKHMask(PermMask.Val))
3468 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003469
Evan Cheng9bbbb982006-10-25 20:48:19 +00003470 if (V2IsSplat) {
3471 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003472 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003473 // new vector_shuffle with the corrected mask.
3474 SDOperand NewMask = NormalizeMask(PermMask, DAG);
3475 if (NewMask.Val != PermMask.Val) {
3476 if (X86::isUNPCKLMask(PermMask.Val, true)) {
3477 SDOperand NewMask = getUnpacklMask(NumElems, DAG);
3478 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3479 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
3480 SDOperand NewMask = getUnpackhMask(NumElems, DAG);
3481 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003482 }
3483 }
3484 }
3485
3486 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003487 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3488 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3489
3490 if (Commuted) {
3491 // Commute is back and try unpck* again.
3492 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3493 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003494 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003495 X86::isUNPCKLMask(PermMask.Val) ||
3496 X86::isUNPCKHMask(PermMask.Val))
3497 return Op;
3498 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003499
3500 // If VT is integer, try PSHUF* first, then SHUFP*.
3501 if (MVT::isInteger(VT)) {
Dan Gohman7f55fcb2007-08-02 21:17:01 +00003502 // MMX doesn't have PSHUFD; it does have PSHUFW. While it's theoretically
3503 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3504 if (((MVT::getSizeInBits(VT) != 64 || NumElems == 4) &&
3505 X86::isPSHUFDMask(PermMask.Val)) ||
Evan Cheng0db9fe62006-04-25 20:13:52 +00003506 X86::isPSHUFHWMask(PermMask.Val) ||
3507 X86::isPSHUFLWMask(PermMask.Val)) {
3508 if (V2.getOpcode() != ISD::UNDEF)
3509 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3510 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3511 return Op;
3512 }
3513
Chris Lattner07c70cd2007-05-17 17:13:13 +00003514 if (X86::isSHUFPMask(PermMask.Val) &&
3515 MVT::getSizeInBits(VT) != 64) // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003516 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003517 } else {
3518 // Floating point cases in the other order.
3519 if (X86::isSHUFPMask(PermMask.Val))
3520 return Op;
3521 if (X86::isPSHUFDMask(PermMask.Val) ||
3522 X86::isPSHUFHWMask(PermMask.Val) ||
3523 X86::isPSHUFLWMask(PermMask.Val)) {
3524 if (V2.getOpcode() != ISD::UNDEF)
3525 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3526 DAG.getNode(ISD::UNDEF, V1.getValueType()),PermMask);
3527 return Op;
3528 }
3529 }
3530
Evan Cheng14b32e12007-12-11 01:46:18 +00003531 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
3532 if (VT == MVT::v8i16) {
3533 SDOperand NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
3534 if (NewOp.Val)
3535 return NewOp;
3536 }
3537
3538 // Handle all 4 wide cases with a number of shuffles.
3539 if (NumElems == 4 && MVT::getSizeInBits(VT) != 64) {
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003540 // Don't do this for MMX.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003541 MVT::ValueType MaskVT = PermMask.getValueType();
Dan Gohman51eaa862007-06-14 22:58:02 +00003542 MVT::ValueType MaskEVT = MVT::getVectorElementType(MaskVT);
Chris Lattner5a88b832007-02-25 07:10:00 +00003543 SmallVector<std::pair<int, int>, 8> Locs;
Evan Cheng43f3bd32006-04-28 07:03:38 +00003544 Locs.reserve(NumElems);
Evan Cheng14b32e12007-12-11 01:46:18 +00003545 SmallVector<SDOperand, 8> Mask1(NumElems,
3546 DAG.getNode(ISD::UNDEF, MaskEVT));
3547 SmallVector<SDOperand, 8> Mask2(NumElems,
3548 DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003549 unsigned NumHi = 0;
3550 unsigned NumLo = 0;
3551 // If no more than two elements come from either vector. This can be
3552 // implemented with two shuffles. First shuffle gather the elements.
3553 // The second shuffle, which takes the first shuffle as both of its
3554 // vector operands, put the elements into the right order.
3555 for (unsigned i = 0; i != NumElems; ++i) {
3556 SDOperand Elt = PermMask.getOperand(i);
3557 if (Elt.getOpcode() == ISD::UNDEF) {
3558 Locs[i] = std::make_pair(-1, -1);
3559 } else {
3560 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3561 if (Val < NumElems) {
3562 Locs[i] = std::make_pair(0, NumLo);
3563 Mask1[NumLo] = Elt;
3564 NumLo++;
3565 } else {
3566 Locs[i] = std::make_pair(1, NumHi);
3567 if (2+NumHi < NumElems)
3568 Mask1[2+NumHi] = Elt;
3569 NumHi++;
3570 }
3571 }
3572 }
3573 if (NumLo <= 2 && NumHi <= 2) {
3574 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003575 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3576 &Mask1[0], Mask1.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003577 for (unsigned i = 0; i != NumElems; ++i) {
3578 if (Locs[i].first == -1)
3579 continue;
3580 else {
3581 unsigned Idx = (i < NumElems/2) ? 0 : NumElems;
3582 Idx += Locs[i].first * (NumElems/2) + Locs[i].second;
3583 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3584 }
3585 }
3586
3587 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
Chris Lattnere2199452006-08-11 17:38:39 +00003588 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3589 &Mask2[0], Mask2.size()));
Evan Cheng43f3bd32006-04-28 07:03:38 +00003590 }
3591
3592 // Break it into (shuffle shuffle_hi, shuffle_lo).
3593 Locs.clear();
Chris Lattner5a88b832007-02-25 07:10:00 +00003594 SmallVector<SDOperand,8> LoMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3595 SmallVector<SDOperand,8> HiMask(NumElems, DAG.getNode(ISD::UNDEF, MaskEVT));
3596 SmallVector<SDOperand,8> *MaskPtr = &LoMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003597 unsigned MaskIdx = 0;
3598 unsigned LoIdx = 0;
3599 unsigned HiIdx = NumElems/2;
3600 for (unsigned i = 0; i != NumElems; ++i) {
3601 if (i == NumElems/2) {
3602 MaskPtr = &HiMask;
3603 MaskIdx = 1;
3604 LoIdx = 0;
3605 HiIdx = NumElems/2;
3606 }
3607 SDOperand Elt = PermMask.getOperand(i);
3608 if (Elt.getOpcode() == ISD::UNDEF) {
3609 Locs[i] = std::make_pair(-1, -1);
3610 } else if (cast<ConstantSDNode>(Elt)->getValue() < NumElems) {
3611 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3612 (*MaskPtr)[LoIdx] = Elt;
3613 LoIdx++;
3614 } else {
3615 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3616 (*MaskPtr)[HiIdx] = Elt;
3617 HiIdx++;
3618 }
3619 }
3620
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003621 SDOperand LoShuffle =
3622 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003623 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3624 &LoMask[0], LoMask.size()));
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003625 SDOperand HiShuffle =
Chris Lattner8c0c10c2006-05-16 06:45:34 +00003626 DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Chris Lattnere2199452006-08-11 17:38:39 +00003627 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3628 &HiMask[0], HiMask.size()));
Chris Lattner5a88b832007-02-25 07:10:00 +00003629 SmallVector<SDOperand, 8> MaskOps;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003630 for (unsigned i = 0; i != NumElems; ++i) {
3631 if (Locs[i].first == -1) {
3632 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3633 } else {
3634 unsigned Idx = Locs[i].first * NumElems + Locs[i].second;
3635 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3636 }
3637 }
3638 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
Chris Lattnere2199452006-08-11 17:38:39 +00003639 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3640 &MaskOps[0], MaskOps.size()));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003641 }
3642
3643 return SDOperand();
3644}
3645
3646SDOperand
3647X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
3648 if (!isa<ConstantSDNode>(Op.getOperand(1)))
3649 return SDOperand();
3650
3651 MVT::ValueType VT = Op.getValueType();
3652 // TODO: handle v16i8.
3653 if (MVT::getSizeInBits(VT) == 16) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003654 SDOperand Vec = Op.getOperand(0);
3655 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3656 if (Idx == 0)
3657 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
3658 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
3659 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
3660 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003661 // Transform it so it match pextrw which produces a 32-bit result.
3662 MVT::ValueType EVT = (MVT::ValueType)(VT+1);
3663 SDOperand Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
3664 Op.getOperand(0), Op.getOperand(1));
3665 SDOperand Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
3666 DAG.getValueType(VT));
3667 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
3668 } else if (MVT::getSizeInBits(VT) == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003669 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3670 if (Idx == 0)
3671 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003672 // SHUFPS the element to the lowest double word, then movss.
3673 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003674 SmallVector<SDOperand, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003675 IdxVec.
3676 push_back(DAG.getConstant(Idx, MVT::getVectorElementType(MaskVT)));
3677 IdxVec.
3678 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3679 IdxVec.
3680 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
3681 IdxVec.
3682 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003683 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3684 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00003685 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003686 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00003687 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003688 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00003689 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003690 } else if (MVT::getSizeInBits(VT) == 64) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003691 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
3692 if (Idx == 0)
3693 return Op;
3694
3695 // UNPCKHPD the element to the lowest double word, then movsd.
3696 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
3697 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
3698 MVT::ValueType MaskVT = MVT::getIntVectorWithNumElements(4);
Chris Lattner5a88b832007-02-25 07:10:00 +00003699 SmallVector<SDOperand, 8> IdxVec;
Dan Gohman51eaa862007-06-14 22:58:02 +00003700 IdxVec.push_back(DAG.getConstant(1, MVT::getVectorElementType(MaskVT)));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00003701 IdxVec.
3702 push_back(DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(MaskVT)));
Chris Lattnere2199452006-08-11 17:38:39 +00003703 SDOperand Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3704 &IdxVec[0], IdxVec.size());
Evan Cheng14b32e12007-12-11 01:46:18 +00003705 SDOperand Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003706 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
3707 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
3708 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00003709 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003710 }
3711
3712 return SDOperand();
3713}
3714
3715SDOperand
3716X86TargetLowering::LowerINSERT_VECTOR_ELT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003717 MVT::ValueType VT = Op.getValueType();
Evan Cheng794405e2007-12-12 07:55:34 +00003718 MVT::ValueType EVT = MVT::getVectorElementType(VT);
3719 if (EVT == MVT::i8)
3720 return SDOperand();
3721
Evan Cheng0db9fe62006-04-25 20:13:52 +00003722 SDOperand N0 = Op.getOperand(0);
3723 SDOperand N1 = Op.getOperand(1);
3724 SDOperand N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00003725
3726 if (MVT::getSizeInBits(EVT) == 16) {
3727 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
3728 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003729 if (N1.getValueType() != MVT::i32)
3730 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
3731 if (N2.getValueType() != MVT::i32)
Chris Lattner0bd48932008-01-17 07:00:52 +00003732 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003733 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003734 }
Nate Begeman219f67f2008-01-05 20:51:30 +00003735 return SDOperand();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003736}
3737
3738SDOperand
3739X86TargetLowering::LowerSCALAR_TO_VECTOR(SDOperand Op, SelectionDAG &DAG) {
3740 SDOperand AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
3741 return DAG.getNode(X86ISD::S2VEC, Op.getValueType(), AnyExt);
3742}
3743
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003744// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00003745// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
3746// one of the above mentioned nodes. It has to be wrapped because otherwise
3747// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
3748// be used to form addressing mode. These wrapped nodes will be selected
3749// into MOV32ri.
3750SDOperand
3751X86TargetLowering::LowerConstantPool(SDOperand Op, SelectionDAG &DAG) {
3752 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Evan Chengd0ff02c2006-11-29 23:19:46 +00003753 SDOperand Result = DAG.getTargetConstantPool(CP->getConstVal(),
3754 getPointerTy(),
3755 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003756 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003757 // With PIC, the address is actually $g + Offset.
3758 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3759 !Subtarget->isPICStyleRIPRel()) {
3760 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3761 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3762 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003763 }
3764
3765 return Result;
3766}
3767
3768SDOperand
3769X86TargetLowering::LowerGlobalAddress(SDOperand Op, SelectionDAG &DAG) {
3770 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003771 SDOperand Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003772 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003773 // With PIC, the address is actually $g + Offset.
3774 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3775 !Subtarget->isPICStyleRIPRel()) {
3776 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3777 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3778 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003779 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003780
3781 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
3782 // load the value at address GV, not the value of GV itself. This means that
3783 // the GlobalAddress must be in the base or index register of the address, not
3784 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003785 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00003786 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
3787 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003788
3789 return Result;
3790}
3791
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003792// Lower ISD::GlobalTLSAddress using the "general dynamic" model
3793static SDOperand
3794LowerToTLSGeneralDynamicModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3795 const MVT::ValueType PtrVT) {
3796 SDOperand InFlag;
3797 SDOperand Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
3798 DAG.getNode(X86ISD::GlobalBaseReg,
3799 PtrVT), InFlag);
3800 InFlag = Chain.getValue(1);
3801
3802 // emit leal symbol@TLSGD(,%ebx,1), %eax
3803 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
3804 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3805 GA->getValueType(0),
3806 GA->getOffset());
3807 SDOperand Ops[] = { Chain, TGA, InFlag };
3808 SDOperand Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
3809 InFlag = Result.getValue(2);
3810 Chain = Result.getValue(1);
3811
3812 // call ___tls_get_addr. This function receives its argument in
3813 // the register EAX.
3814 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
3815 InFlag = Chain.getValue(1);
3816
3817 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
3818 SDOperand Ops1[] = { Chain,
3819 DAG.getTargetExternalSymbol("___tls_get_addr",
3820 PtrVT),
3821 DAG.getRegister(X86::EAX, PtrVT),
3822 DAG.getRegister(X86::EBX, PtrVT),
3823 InFlag };
3824 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
3825 InFlag = Chain.getValue(1);
3826
3827 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
3828}
3829
3830// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
3831// "local exec" model.
3832static SDOperand
3833LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
3834 const MVT::ValueType PtrVT) {
3835 // Get the Thread Pointer
3836 SDOperand ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
3837 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
3838 // exec)
3839 SDOperand TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
3840 GA->getValueType(0),
3841 GA->getOffset());
3842 SDOperand Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00003843
3844 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
3845 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset, NULL, 0);
3846
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003847 // The address of the thread local variable is the add of the thread
3848 // pointer with the offset of the variable.
3849 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
3850}
3851
3852SDOperand
3853X86TargetLowering::LowerGlobalTLSAddress(SDOperand Op, SelectionDAG &DAG) {
3854 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00003855 // TODO: implement the "initial exec"model for pic executables
3856 assert(!Subtarget->is64Bit() && Subtarget->isTargetELF() &&
3857 "TLS not implemented for non-ELF and 64-bit targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00003858 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
3859 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
3860 // otherwise use the "Local Exec"TLS Model
3861 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
3862 return LowerToTLSGeneralDynamicModel(GA, DAG, getPointerTy());
3863 else
3864 return LowerToTLSExecModel(GA, DAG, getPointerTy());
3865}
3866
Evan Cheng0db9fe62006-04-25 20:13:52 +00003867SDOperand
3868X86TargetLowering::LowerExternalSymbol(SDOperand Op, SelectionDAG &DAG) {
3869 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Evan Chengd0ff02c2006-11-29 23:19:46 +00003870 SDOperand Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00003871 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00003872 // With PIC, the address is actually $g + Offset.
3873 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3874 !Subtarget->isPICStyleRIPRel()) {
3875 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3876 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3877 Result);
3878 }
3879
3880 return Result;
3881}
3882
3883SDOperand X86TargetLowering::LowerJumpTable(SDOperand Op, SelectionDAG &DAG) {
3884 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
3885 SDOperand Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
3886 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
3887 // With PIC, the address is actually $g + Offset.
3888 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
3889 !Subtarget->isPICStyleRIPRel()) {
3890 Result = DAG.getNode(ISD::ADD, getPointerTy(),
3891 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
3892 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003893 }
3894
3895 return Result;
3896}
3897
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003898/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
3899/// take a 2 x i32 value to shift plus a shift amount.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003900SDOperand X86TargetLowering::LowerShift(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003901 assert(Op.getNumOperands() == 3 && Op.getValueType() == MVT::i32 &&
3902 "Not an i64 shift!");
3903 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
3904 SDOperand ShOpLo = Op.getOperand(0);
3905 SDOperand ShOpHi = Op.getOperand(1);
3906 SDOperand ShAmt = Op.getOperand(2);
3907 SDOperand Tmp1 = isSRA ?
3908 DAG.getNode(ISD::SRA, MVT::i32, ShOpHi, DAG.getConstant(31, MVT::i8)) :
3909 DAG.getConstant(0, MVT::i32);
Evan Chenge3413162006-01-09 18:33:28 +00003910
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003911 SDOperand Tmp2, Tmp3;
3912 if (Op.getOpcode() == ISD::SHL_PARTS) {
3913 Tmp2 = DAG.getNode(X86ISD::SHLD, MVT::i32, ShOpHi, ShOpLo, ShAmt);
3914 Tmp3 = DAG.getNode(ISD::SHL, MVT::i32, ShOpLo, ShAmt);
3915 } else {
3916 Tmp2 = DAG.getNode(X86ISD::SHRD, MVT::i32, ShOpLo, ShOpHi, ShAmt);
3917 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, MVT::i32, ShOpHi, ShAmt);
3918 }
Evan Chenge3413162006-01-09 18:33:28 +00003919
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003920 const MVT::ValueType *VTs = DAG.getNodeValueTypes(MVT::Other, MVT::Flag);
3921 SDOperand AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
3922 DAG.getConstant(32, MVT::i8));
3923 SDOperand Cond = DAG.getNode(X86ISD::CMP, MVT::i32,
3924 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00003925
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003926 SDOperand Hi, Lo;
3927 SDOperand CC = DAG.getConstant(X86::COND_NE, MVT::i8);
3928 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::Flag);
3929 SmallVector<SDOperand, 4> Ops;
3930 if (Op.getOpcode() == ISD::SHL_PARTS) {
3931 Ops.push_back(Tmp2);
3932 Ops.push_back(Tmp3);
3933 Ops.push_back(CC);
3934 Ops.push_back(Cond);
3935 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
Evan Chenge3413162006-01-09 18:33:28 +00003936
Evan Chenge3413162006-01-09 18:33:28 +00003937 Ops.clear();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00003938 Ops.push_back(Tmp3);
3939 Ops.push_back(Tmp1);
3940 Ops.push_back(CC);
3941 Ops.push_back(Cond);
3942 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3943 } else {
3944 Ops.push_back(Tmp2);
3945 Ops.push_back(Tmp3);
3946 Ops.push_back(CC);
3947 Ops.push_back(Cond);
3948 Lo = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3949
3950 Ops.clear();
3951 Ops.push_back(Tmp3);
3952 Ops.push_back(Tmp1);
3953 Ops.push_back(CC);
3954 Ops.push_back(Cond);
3955 Hi = DAG.getNode(X86ISD::CMOV, MVT::i32, &Ops[0], Ops.size());
3956 }
3957
3958 VTs = DAG.getNodeValueTypes(MVT::i32, MVT::i32);
3959 Ops.clear();
3960 Ops.push_back(Lo);
3961 Ops.push_back(Hi);
3962 return DAG.getNode(ISD::MERGE_VALUES, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003963}
Evan Chenga3195e82006-01-12 22:54:21 +00003964
Evan Cheng0db9fe62006-04-25 20:13:52 +00003965SDOperand X86TargetLowering::LowerSINT_TO_FP(SDOperand Op, SelectionDAG &DAG) {
3966 assert(Op.getOperand(0).getValueType() <= MVT::i64 &&
3967 Op.getOperand(0).getValueType() >= MVT::i16 &&
3968 "Unknown SINT_TO_FP to lower!");
3969
3970 SDOperand Result;
3971 MVT::ValueType SrcVT = Op.getOperand(0).getValueType();
3972 unsigned Size = MVT::getSizeInBits(SrcVT)/8;
3973 MachineFunction &MF = DAG.getMachineFunction();
3974 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
3975 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng786225a2006-10-05 23:01:46 +00003976 SDOperand Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Evan Cheng8b2794a2006-10-13 21:14:26 +00003977 StackSlot, NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003978
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003979 // These are really Legal; caller falls through into that case.
Chris Lattner78631162008-01-16 06:24:21 +00003980 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00003981 return Result;
Chris Lattner1956d152008-01-16 06:19:45 +00003982 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
Dale Johannesen73328d12007-09-19 23:55:34 +00003983 Subtarget->is64Bit())
3984 return Result;
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003985
Evan Cheng0db9fe62006-04-25 20:13:52 +00003986 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00003987 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00003988 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003989 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00003990 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
3991 else
Dale Johannesen849f2142007-07-03 00:53:03 +00003992 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00003993 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003994 Ops.push_back(Chain);
3995 Ops.push_back(StackSlot);
3996 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00003997 Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG :X86ISD::FILD,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003998 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003999
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004000 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004001 Chain = Result.getValue(1);
4002 SDOperand InFlag = Result.getValue(2);
4003
4004 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4005 // shouldn't be necessary except that RFP cannot be live across
4006 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004007 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004008 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004009 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004010 Tys = DAG.getVTList(MVT::Other);
4011 SmallVector<SDOperand, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004012 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004013 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004014 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004015 Ops.push_back(DAG.getValueType(Op.getValueType()));
4016 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004017 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Evan Cheng466685d2006-10-09 20:57:25 +00004018 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot, NULL, 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004019 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004020
Evan Cheng0db9fe62006-04-25 20:13:52 +00004021 return Result;
4022}
4023
Chris Lattner27a6c732007-11-24 07:07:01 +00004024std::pair<SDOperand,SDOperand> X86TargetLowering::
4025FP_TO_SINTHelper(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004026 assert(Op.getValueType() <= MVT::i64 && Op.getValueType() >= MVT::i16 &&
4027 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004028
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004029 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004030 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004031 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Chris Lattner27a6c732007-11-24 07:07:01 +00004032 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen73328d12007-09-19 23:55:34 +00004033 if (Subtarget->is64Bit() &&
4034 Op.getValueType() == MVT::i64 &&
4035 Op.getOperand(0).getValueType() != MVT::f80)
Chris Lattner27a6c732007-11-24 07:07:01 +00004036 return std::make_pair(SDOperand(), SDOperand());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004037
Evan Cheng87c89352007-10-15 20:11:21 +00004038 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4039 // stack slot.
4040 MachineFunction &MF = DAG.getMachineFunction();
4041 unsigned MemSize = MVT::getSizeInBits(Op.getValueType())/8;
4042 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4043 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004044 unsigned Opc;
4045 switch (Op.getValueType()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004046 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4047 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4048 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4049 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004050 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004051
Evan Cheng0db9fe62006-04-25 20:13:52 +00004052 SDOperand Chain = DAG.getEntryNode();
4053 SDOperand Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004054 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004055 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Evan Cheng8b2794a2006-10-13 21:14:26 +00004056 Chain = DAG.getStore(Chain, Value, StackSlot, NULL, 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004057 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Chris Lattner5a88b832007-02-25 07:10:00 +00004058 SDOperand Ops[] = {
4059 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4060 };
4061 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004062 Chain = Value.getValue(1);
4063 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4064 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4065 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004066
Evan Cheng0db9fe62006-04-25 20:13:52 +00004067 // Build the FP_TO_INT*_IN_MEM
Chris Lattner5a88b832007-02-25 07:10:00 +00004068 SDOperand Ops[] = { Chain, Value, StackSlot };
4069 SDOperand FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004070
Chris Lattner27a6c732007-11-24 07:07:01 +00004071 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004072}
4073
Chris Lattner27a6c732007-11-24 07:07:01 +00004074SDOperand X86TargetLowering::LowerFP_TO_SINT(SDOperand Op, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004075 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(Op, DAG);
4076 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4077 if (FIST.Val == 0) return SDOperand();
4078
4079 // Load the result.
4080 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4081}
4082
4083SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
4084 std::pair<SDOperand,SDOperand> Vals = FP_TO_SINTHelper(SDOperand(N, 0), DAG);
4085 SDOperand FIST = Vals.first, StackSlot = Vals.second;
4086 if (FIST.Val == 0) return 0;
4087
4088 // Return an i64 load from the stack slot.
4089 SDOperand Res = DAG.getLoad(MVT::i64, FIST, StackSlot, NULL, 0);
4090
4091 // Use a MERGE_VALUES node to drop the chain result value.
4092 return DAG.getNode(ISD::MERGE_VALUES, MVT::i64, Res).Val;
4093}
4094
Evan Cheng0db9fe62006-04-25 20:13:52 +00004095SDOperand X86TargetLowering::LowerFABS(SDOperand Op, SelectionDAG &DAG) {
4096 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004097 MVT::ValueType EltVT = VT;
4098 if (MVT::isVector(VT))
4099 EltVT = MVT::getVectorElementType(VT);
4100 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004101 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004102 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004103 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004104 CV.push_back(C);
4105 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004106 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004107 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004108 CV.push_back(C);
4109 CV.push_back(C);
4110 CV.push_back(C);
4111 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004112 }
Dan Gohmand3006222007-07-27 17:16:43 +00004113 Constant *C = ConstantVector::get(CV);
4114 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4115 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4116 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004117 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4118}
4119
4120SDOperand X86TargetLowering::LowerFNEG(SDOperand Op, SelectionDAG &DAG) {
4121 MVT::ValueType VT = Op.getValueType();
Dan Gohman20382522007-07-10 00:05:58 +00004122 MVT::ValueType EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004123 unsigned EltNum = 1;
4124 if (MVT::isVector(VT)) {
Dan Gohman20382522007-07-10 00:05:58 +00004125 EltVT = MVT::getVectorElementType(VT);
Evan Chengd4d01b72007-07-19 23:36:01 +00004126 EltNum = MVT::getVectorNumElements(VT);
4127 }
Dan Gohman20382522007-07-10 00:05:58 +00004128 const Type *OpNTy = MVT::getTypeForValueType(EltVT);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004129 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004130 if (EltVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004131 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004132 CV.push_back(C);
4133 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004134 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004135 Constant *C = ConstantFP::get(OpNTy, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004136 CV.push_back(C);
4137 CV.push_back(C);
4138 CV.push_back(C);
4139 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004140 }
Dan Gohmand3006222007-07-27 17:16:43 +00004141 Constant *C = ConstantVector::get(CV);
4142 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4143 SDOperand Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4144 false, 16);
Evan Chengd4d01b72007-07-19 23:36:01 +00004145 if (MVT::isVector(VT)) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004146 return DAG.getNode(ISD::BIT_CONVERT, VT,
4147 DAG.getNode(ISD::XOR, MVT::v2i64,
4148 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4149 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4150 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004151 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4152 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004153}
4154
Evan Cheng68c47cb2007-01-05 07:55:56 +00004155SDOperand X86TargetLowering::LowerFCOPYSIGN(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004156 SDOperand Op0 = Op.getOperand(0);
4157 SDOperand Op1 = Op.getOperand(1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004158 MVT::ValueType VT = Op.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004159 MVT::ValueType SrcVT = Op1.getValueType();
Evan Cheng68c47cb2007-01-05 07:55:56 +00004160 const Type *SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004161
4162 // If second operand is smaller, extend it first.
4163 if (MVT::getSizeInBits(SrcVT) < MVT::getSizeInBits(VT)) {
4164 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4165 SrcVT = VT;
Dale Johannesen43421b32007-09-06 18:13:44 +00004166 SrcTy = MVT::getTypeForValueType(SrcVT);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004167 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004168 // And if it is bigger, shrink it first.
4169 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004170 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004171 SrcVT = VT;
4172 SrcTy = MVT::getTypeForValueType(SrcVT);
4173 }
4174
4175 // At this point the operands and the result should have the same
4176 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004177
Evan Cheng68c47cb2007-01-05 07:55:56 +00004178 // First get the sign bit of second operand.
4179 std::vector<Constant*> CV;
4180 if (SrcVT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004181 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 1ULL << 63))));
4182 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004183 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004184 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 1U << 31))));
4185 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4186 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4187 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004188 }
Dan Gohmand3006222007-07-27 17:16:43 +00004189 Constant *C = ConstantVector::get(CV);
4190 SDOperand CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4191 SDOperand Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx, NULL, 0,
4192 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004193 SDOperand SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004194
4195 // Shift sign bit right or left if the two operands have different types.
4196 if (MVT::getSizeInBits(SrcVT) > MVT::getSizeInBits(VT)) {
4197 // Op0 is MVT::f32, Op1 is MVT::f64.
4198 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4199 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4200 DAG.getConstant(32, MVT::i32));
4201 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4202 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004203 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004204 }
4205
Evan Cheng73d6cf12007-01-05 21:37:56 +00004206 // Clear first operand sign bit.
4207 CV.clear();
4208 if (VT == MVT::f64) {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004209 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, ~(1ULL << 63)))));
4210 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004211 } else {
Dale Johannesen3f6eb742007-09-11 18:32:33 +00004212 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, ~(1U << 31)))));
4213 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4214 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
4215 CV.push_back(ConstantFP::get(SrcTy, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004216 }
Dan Gohmand3006222007-07-27 17:16:43 +00004217 C = ConstantVector::get(CV);
4218 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4219 SDOperand Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx, NULL, 0,
4220 false, 16);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004221 SDOperand Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
4222
4223 // Or the value with the sign bit.
4224 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004225}
4226
Evan Chenge5f62042007-09-29 00:00:36 +00004227SDOperand X86TargetLowering::LowerSETCC(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004228 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Evan Cheng1a35edb2007-09-26 00:45:55 +00004229 SDOperand Cond;
Evan Cheng0488db92007-09-25 01:57:46 +00004230 SDOperand Op0 = Op.getOperand(0);
4231 SDOperand Op1 = Op.getOperand(1);
4232 SDOperand CC = Op.getOperand(2);
4233 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4234 bool isFP = MVT::isFloatingPoint(Op.getOperand(1).getValueType());
4235 unsigned X86CC;
4236
Evan Cheng0488db92007-09-25 01:57:46 +00004237 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004238 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004239 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4240 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004241 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004242 }
Evan Cheng0488db92007-09-25 01:57:46 +00004243
4244 assert(isFP && "Illegal integer SetCC!");
4245
Evan Chenge5f62042007-09-29 00:00:36 +00004246 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004247 switch (SetCCOpcode) {
4248 default: assert(false && "Illegal floating point SetCC!");
4249 case ISD::SETOEQ: { // !PF & ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004250 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004251 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004252 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004253 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4254 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4255 }
4256 case ISD::SETUNE: { // PF | !ZF
Evan Chenge5f62042007-09-29 00:00:36 +00004257 SDOperand Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004258 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004259 SDOperand Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004260 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4261 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4262 }
4263 }
4264}
4265
4266
Evan Cheng0db9fe62006-04-25 20:13:52 +00004267SDOperand X86TargetLowering::LowerSELECT(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004268 bool addTest = true;
Evan Cheng734503b2006-09-11 02:19:56 +00004269 SDOperand Cond = Op.getOperand(0);
4270 SDOperand CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004271
Evan Cheng734503b2006-09-11 02:19:56 +00004272 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004273 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004274
Evan Cheng3f41d662007-10-08 22:16:29 +00004275 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4276 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004277 if (Cond.getOpcode() == X86ISD::SETCC) {
4278 CC = Cond.getOperand(0);
4279
Evan Cheng734503b2006-09-11 02:19:56 +00004280 SDOperand Cmp = Cond.getOperand(1);
4281 unsigned Opc = Cmp.getOpcode();
Evan Cheng3f41d662007-10-08 22:16:29 +00004282 MVT::ValueType VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00004283
Evan Cheng3f41d662007-10-08 22:16:29 +00004284 bool IllegalFPCMov = false;
Chris Lattner1956d152008-01-16 06:19:45 +00004285 if (MVT::isFloatingPoint(VT) && !MVT::isVector(VT) &&
Chris Lattner78631162008-01-16 06:24:21 +00004286 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng3f41d662007-10-08 22:16:29 +00004287 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner1956d152008-01-16 06:19:45 +00004288
Evan Chenge5f62042007-09-29 00:00:36 +00004289 if ((Opc == X86ISD::CMP ||
4290 Opc == X86ISD::COMI ||
4291 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004292 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004293 addTest = false;
4294 }
4295 }
4296
4297 if (addTest) {
4298 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004299 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004300 }
4301
4302 const MVT::ValueType *VTs = DAG.getNodeValueTypes(Op.getValueType(),
4303 MVT::Flag);
4304 SmallVector<SDOperand, 4> Ops;
4305 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4306 // condition is true.
4307 Ops.push_back(Op.getOperand(2));
4308 Ops.push_back(Op.getOperand(1));
4309 Ops.push_back(CC);
4310 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004311 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004312}
4313
Evan Cheng0db9fe62006-04-25 20:13:52 +00004314SDOperand X86TargetLowering::LowerBRCOND(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004315 bool addTest = true;
4316 SDOperand Chain = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004317 SDOperand Cond = Op.getOperand(1);
4318 SDOperand Dest = Op.getOperand(2);
4319 SDOperand CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004320
Evan Cheng0db9fe62006-04-25 20:13:52 +00004321 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004322 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004323
Evan Cheng3f41d662007-10-08 22:16:29 +00004324 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4325 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004326 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004327 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004328
Evan Cheng734503b2006-09-11 02:19:56 +00004329 SDOperand Cmp = Cond.getOperand(1);
4330 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004331 if (Opc == X86ISD::CMP ||
4332 Opc == X86ISD::COMI ||
4333 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004334 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004335 addTest = false;
4336 }
4337 }
4338
4339 if (addTest) {
4340 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004341 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004342 }
Evan Chenge5f62042007-09-29 00:00:36 +00004343 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004344 Chain, Op.getOperand(2), CC, Cond);
4345}
4346
Anton Korobeynikove060b532007-04-17 19:34:00 +00004347
4348// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4349// Calls to _alloca is needed to probe the stack when allocating more than 4k
4350// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4351// that the guard pages used by the OS virtual memory manager are allocated in
4352// correct sequence.
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004353SDOperand
4354X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDOperand Op,
4355 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004356 assert(Subtarget->isTargetCygMing() &&
4357 "This should be used only on Cygwin/Mingw targets");
4358
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004359 // Get the inputs.
4360 SDOperand Chain = Op.getOperand(0);
4361 SDOperand Size = Op.getOperand(1);
4362 // FIXME: Ensure alignment here
4363
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004364 SDOperand Flag;
4365
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004366 MVT::ValueType IntPtr = getPointerTy();
Chris Lattner0bd48932008-01-17 07:00:52 +00004367 MVT::ValueType SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004368
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004369 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4370 Flag = Chain.getValue(1);
4371
4372 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4373 SDOperand Ops[] = { Chain,
4374 DAG.getTargetExternalSymbol("_alloca", IntPtr),
4375 DAG.getRegister(X86::EAX, IntPtr),
4376 Flag };
4377 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 4);
4378 Flag = Chain.getValue(1);
4379
4380 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004381
4382 std::vector<MVT::ValueType> Tys;
4383 Tys.push_back(SPTy);
4384 Tys.push_back(MVT::Other);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004385 SDOperand Ops1[2] = { Chain.getValue(0), Chain };
4386 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004387}
4388
Evan Cheng0db9fe62006-04-25 20:13:52 +00004389SDOperand X86TargetLowering::LowerMEMSET(SDOperand Op, SelectionDAG &DAG) {
4390 SDOperand InFlag(0, 0);
4391 SDOperand Chain = Op.getOperand(0);
4392 unsigned Align =
4393 (unsigned)cast<ConstantSDNode>(Op.getOperand(4))->getValue();
4394 if (Align == 0) Align = 1;
4395
4396 ConstantSDNode *I = dyn_cast<ConstantSDNode>(Op.getOperand(3));
Rafael Espindola6b83b5d2007-08-27 10:18:20 +00004397 // If not DWORD aligned or size is more than the threshold, call memset.
Rafael Espindola44c82652007-08-27 17:48:26 +00004398 // The libc version is likely to be faster for these cases. It can use the
4399 // address value and run time information about the CPU.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400 if ((Align & 3) != 0 ||
Rafael Espindolafc05f402007-10-31 11:52:06 +00004401 (I && I->getValue() > Subtarget->getMaxInlineSizeThreshold())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004402 MVT::ValueType IntPtr = getPointerTy();
Owen Andersona69571c2006-05-03 01:29:57 +00004403 const Type *IntPtrTy = getTargetData()->getIntPtrType();
Reid Spencer47857812006-12-31 05:55:36 +00004404 TargetLowering::ArgListTy Args;
4405 TargetLowering::ArgListEntry Entry;
4406 Entry.Node = Op.getOperand(1);
4407 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004408 Args.push_back(Entry);
Reid Spenceraff93872007-01-03 17:24:59 +00004409 // Extend the unsigned i8 argument to be an int value for the call.
Reid Spencer47857812006-12-31 05:55:36 +00004410 Entry.Node = DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, Op.getOperand(2));
4411 Entry.Ty = IntPtrTy;
Reid Spencer47857812006-12-31 05:55:36 +00004412 Args.push_back(Entry);
4413 Entry.Node = Op.getOperand(3);
4414 Args.push_back(Entry);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004415 std::pair<SDOperand,SDOperand> CallResult =
Reid Spencer47857812006-12-31 05:55:36 +00004416 LowerCallTo(Chain, Type::VoidTy, false, false, CallingConv::C, false,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004417 DAG.getExternalSymbol("memset", IntPtr), Args, DAG);
4418 return CallResult.second;
Evan Cheng48090aa2006-03-21 23:01:21 +00004419 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00004420
Evan Cheng0db9fe62006-04-25 20:13:52 +00004421 MVT::ValueType AVT;
4422 SDOperand Count;
4423 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Op.getOperand(2));
4424 unsigned BytesLeft = 0;
4425 bool TwoRepStos = false;
4426 if (ValC) {
4427 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00004428 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00004429
Evan Cheng0db9fe62006-04-25 20:13:52 +00004430 // If the value is a constant, then we can potentially use larger sets.
4431 switch (Align & 3) {
4432 case 2: // WORD aligned
4433 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004434 ValReg = X86::AX;
Evan Cheng25ab6902006-09-08 06:48:29 +00004435 Val = (Val << 8) | Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004436 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004437 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004438 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004439 ValReg = X86::EAX;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004440 Val = (Val << 8) | Val;
4441 Val = (Val << 16) | Val;
Evan Cheng25ab6902006-09-08 06:48:29 +00004442 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) { // QWORD aligned
4443 AVT = MVT::i64;
4444 ValReg = X86::RAX;
4445 Val = (Val << 32) | Val;
4446 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004447 break;
4448 default: // Byte aligned
4449 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004450 ValReg = X86::AL;
Evan Cheng25ab6902006-09-08 06:48:29 +00004451 Count = Op.getOperand(3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004452 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00004453 }
4454
Evan Cheng25ab6902006-09-08 06:48:29 +00004455 if (AVT > MVT::i8) {
4456 if (I) {
4457 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner0bd48932008-01-17 07:00:52 +00004458 Count = DAG.getIntPtrConstant(I->getValue() / UBytes);
Evan Cheng25ab6902006-09-08 06:48:29 +00004459 BytesLeft = I->getValue() % UBytes;
4460 } else {
4461 assert(AVT >= MVT::i32 &&
4462 "Do not use rep;stos if not at least DWORD aligned");
4463 Count = DAG.getNode(ISD::SRL, Op.getOperand(3).getValueType(),
4464 Op.getOperand(3), DAG.getConstant(2, MVT::i8));
4465 TwoRepStos = true;
4466 }
4467 }
4468
Evan Cheng0db9fe62006-04-25 20:13:52 +00004469 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
4470 InFlag);
4471 InFlag = Chain.getValue(1);
4472 } else {
4473 AVT = MVT::i8;
4474 Count = Op.getOperand(3);
4475 Chain = DAG.getCopyToReg(Chain, X86::AL, Op.getOperand(2), InFlag);
4476 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00004477 }
Evan Chengc78d3b42006-04-24 18:01:45 +00004478
Evan Cheng25ab6902006-09-08 06:48:29 +00004479 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4480 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004481 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004482 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
4483 Op.getOperand(1), InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004484 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00004485
Chris Lattnerd96d0722007-02-25 06:40:16 +00004486 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004487 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004488 Ops.push_back(Chain);
4489 Ops.push_back(DAG.getValueType(AVT));
4490 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004491 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00004492
Evan Cheng0db9fe62006-04-25 20:13:52 +00004493 if (TwoRepStos) {
4494 InFlag = Chain.getValue(1);
4495 Count = Op.getOperand(3);
4496 MVT::ValueType CVT = Count.getValueType();
4497 SDOperand Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00004498 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
4499 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
4500 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004501 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00004502 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004503 Ops.clear();
4504 Ops.push_back(Chain);
4505 Ops.push_back(DAG.getValueType(MVT::i8));
4506 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004507 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004508 } else if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004509 // Issue stores for the last 1 - 7 bytes.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004510 SDOperand Value;
4511 unsigned Val = ValC->getValue() & 255;
4512 unsigned Offset = I->getValue() - BytesLeft;
4513 SDOperand DstAddr = Op.getOperand(1);
4514 MVT::ValueType AddrVT = DstAddr.getValueType();
Evan Cheng25ab6902006-09-08 06:48:29 +00004515 if (BytesLeft >= 4) {
4516 Val = (Val << 8) | Val;
4517 Val = (Val << 16) | Val;
4518 Value = DAG.getConstant(Val, MVT::i32);
Evan Cheng786225a2006-10-05 23:01:46 +00004519 Chain = DAG.getStore(Chain, Value,
4520 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4521 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004522 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004523 BytesLeft -= 4;
4524 Offset += 4;
4525 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004526 if (BytesLeft >= 2) {
4527 Value = DAG.getConstant((Val << 8) | Val, MVT::i16);
Evan Cheng786225a2006-10-05 23:01:46 +00004528 Chain = DAG.getStore(Chain, Value,
4529 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4530 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004531 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004532 BytesLeft -= 2;
4533 Offset += 2;
Evan Cheng386031a2006-03-24 07:29:27 +00004534 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004535 if (BytesLeft == 1) {
4536 Value = DAG.getConstant(Val, MVT::i8);
Evan Cheng786225a2006-10-05 23:01:46 +00004537 Chain = DAG.getStore(Chain, Value,
4538 DAG.getNode(ISD::ADD, AddrVT, DstAddr,
4539 DAG.getConstant(Offset, AddrVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004540 NULL, 0);
Evan Chengba05f722006-04-21 23:03:30 +00004541 }
Evan Cheng386031a2006-03-24 07:29:27 +00004542 }
Evan Cheng11e15b32006-04-03 20:53:28 +00004543
Evan Cheng0db9fe62006-04-25 20:13:52 +00004544 return Chain;
4545}
Evan Cheng11e15b32006-04-03 20:53:28 +00004546
Rafael Espindola068317b2007-09-28 12:53:01 +00004547SDOperand X86TargetLowering::LowerMEMCPYInline(SDOperand Chain,
4548 SDOperand Dest,
4549 SDOperand Source,
4550 unsigned Size,
4551 unsigned Align,
4552 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004553 MVT::ValueType AVT;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004554 unsigned BytesLeft = 0;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004555 switch (Align & 3) {
4556 case 2: // WORD aligned
4557 AVT = MVT::i16;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004558 break;
Evan Cheng25ab6902006-09-08 06:48:29 +00004559 case 0: // DWORD aligned
Evan Cheng0db9fe62006-04-25 20:13:52 +00004560 AVT = MVT::i32;
Evan Cheng25ab6902006-09-08 06:48:29 +00004561 if (Subtarget->is64Bit() && ((Align & 0xF) == 0)) // QWORD aligned
4562 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 break;
4564 default: // Byte aligned
4565 AVT = MVT::i8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004566 break;
4567 }
4568
Rafael Espindola068317b2007-09-28 12:53:01 +00004569 unsigned UBytes = MVT::getSizeInBits(AVT) / 8;
Chris Lattner0bd48932008-01-17 07:00:52 +00004570 SDOperand Count = DAG.getIntPtrConstant(Size / UBytes);
Rafael Espindola068317b2007-09-28 12:53:01 +00004571 BytesLeft = Size % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00004572
Evan Cheng0db9fe62006-04-25 20:13:52 +00004573 SDOperand InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004574 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
4575 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004576 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004577 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004578 Dest, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004579 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00004580 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Rafael Espindola068317b2007-09-28 12:53:01 +00004581 Source, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582 InFlag = Chain.getValue(1);
4583
Chris Lattnerd96d0722007-02-25 06:40:16 +00004584 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner5a88b832007-02-25 07:10:00 +00004585 SmallVector<SDOperand, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004586 Ops.push_back(Chain);
4587 Ops.push_back(DAG.getValueType(AVT));
4588 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00004589 Chain = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004590
Rafael Espindola068317b2007-09-28 12:53:01 +00004591 if (BytesLeft) {
Evan Cheng25ab6902006-09-08 06:48:29 +00004592 // Issue loads and stores for the last 1 - 7 bytes.
Rafael Espindola068317b2007-09-28 12:53:01 +00004593 unsigned Offset = Size - BytesLeft;
4594 SDOperand DstAddr = Dest;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595 MVT::ValueType DstVT = DstAddr.getValueType();
Rafael Espindola068317b2007-09-28 12:53:01 +00004596 SDOperand SrcAddr = Source;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004597 MVT::ValueType SrcVT = SrcAddr.getValueType();
4598 SDOperand Value;
Evan Cheng25ab6902006-09-08 06:48:29 +00004599 if (BytesLeft >= 4) {
4600 Value = DAG.getLoad(MVT::i32, Chain,
4601 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4602 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004603 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004604 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004605 Chain = DAG.getStore(Chain, Value,
4606 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4607 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004608 NULL, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00004609 BytesLeft -= 4;
4610 Offset += 4;
4611 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004612 if (BytesLeft >= 2) {
4613 Value = DAG.getLoad(MVT::i16, Chain,
4614 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4615 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004616 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004617 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004618 Chain = DAG.getStore(Chain, Value,
4619 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4620 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004621 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004622 BytesLeft -= 2;
4623 Offset += 2;
Evan Chengb067a1e2006-03-31 19:22:53 +00004624 }
4625
Evan Cheng0db9fe62006-04-25 20:13:52 +00004626 if (BytesLeft == 1) {
4627 Value = DAG.getLoad(MVT::i8, Chain,
4628 DAG.getNode(ISD::ADD, SrcVT, SrcAddr,
4629 DAG.getConstant(Offset, SrcVT)),
Evan Cheng466685d2006-10-09 20:57:25 +00004630 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004631 Chain = Value.getValue(1);
Evan Cheng786225a2006-10-05 23:01:46 +00004632 Chain = DAG.getStore(Chain, Value,
4633 DAG.getNode(ISD::ADD, DstVT, DstAddr,
4634 DAG.getConstant(Offset, DstVT)),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004635 NULL, 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004636 }
Evan Chengb067a1e2006-03-31 19:22:53 +00004637 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004638
4639 return Chain;
4640}
4641
Chris Lattner27a6c732007-11-24 07:07:01 +00004642/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
4643SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00004644 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Chris Lattner27a6c732007-11-24 07:07:01 +00004645 SDOperand TheChain = N->getOperand(0);
4646 SDOperand rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004647 if (Subtarget->is64Bit()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004648 SDOperand rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
4649 SDOperand rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
4650 MVT::i64, rax.getValue(2));
4651 SDOperand Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004652 DAG.getConstant(32, MVT::i8));
Chris Lattner5a88b832007-02-25 07:10:00 +00004653 SDOperand Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00004654 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00004655 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00004656
4657 Tys = DAG.getVTList(MVT::i64, MVT::Other);
Chris Lattner27a6c732007-11-24 07:07:01 +00004658 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00004659 }
Chris Lattner5a88b832007-02-25 07:10:00 +00004660
Chris Lattner27a6c732007-11-24 07:07:01 +00004661 SDOperand eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
4662 SDOperand edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
4663 MVT::i32, eax.getValue(2));
4664 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
4665 SDOperand Ops[] = { eax, edx };
4666 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
4667
4668 // Use a MERGE_VALUES to return the value and chain.
4669 Ops[1] = edx.getValue(1);
4670 Tys = DAG.getVTList(MVT::i64, MVT::Other);
4671 return DAG.getNode(ISD::MERGE_VALUES, Tys, Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004672}
4673
4674SDOperand X86TargetLowering::LowerVASTART(SDOperand Op, SelectionDAG &DAG) {
Evan Cheng8b2794a2006-10-13 21:14:26 +00004675 SrcValueSDNode *SV = cast<SrcValueSDNode>(Op.getOperand(2));
4676
Evan Cheng25ab6902006-09-08 06:48:29 +00004677 if (!Subtarget->is64Bit()) {
4678 // vastart just stores the address of the VarArgsFrameIndex slot into the
4679 // memory location argument.
4680 SDOperand FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004681 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV->getValue(),
4682 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004683 }
4684
4685 // __va_list_tag:
4686 // gp_offset (0 - 6 * 8)
4687 // fp_offset (48 - 48 + 8 * 16)
4688 // overflow_arg_area (point to parameters coming in memory).
4689 // reg_save_area
Chris Lattner5a88b832007-02-25 07:10:00 +00004690 SmallVector<SDOperand, 8> MemOps;
Evan Cheng25ab6902006-09-08 06:48:29 +00004691 SDOperand FIN = Op.getOperand(1);
4692 // Store gp_offset
Evan Cheng786225a2006-10-05 23:01:46 +00004693 SDOperand Store = DAG.getStore(Op.getOperand(0),
4694 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004695 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004696 MemOps.push_back(Store);
4697
4698 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00004699 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00004700 Store = DAG.getStore(Op.getOperand(0),
4701 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Evan Cheng8b2794a2006-10-13 21:14:26 +00004702 FIN, SV->getValue(), SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004703 MemOps.push_back(Store);
4704
4705 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00004706 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng25ab6902006-09-08 06:48:29 +00004707 SDOperand OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004708 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV->getValue(),
4709 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004710 MemOps.push_back(Store);
4711
4712 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00004713 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Evan Cheng25ab6902006-09-08 06:48:29 +00004714 SDOperand RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Evan Cheng8b2794a2006-10-13 21:14:26 +00004715 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV->getValue(),
4716 SV->getOffset());
Evan Cheng25ab6902006-09-08 06:48:29 +00004717 MemOps.push_back(Store);
4718 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004719}
4720
Evan Chengae642192007-03-02 23:16:35 +00004721SDOperand X86TargetLowering::LowerVACOPY(SDOperand Op, SelectionDAG &DAG) {
4722 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
4723 SDOperand Chain = Op.getOperand(0);
4724 SDOperand DstPtr = Op.getOperand(1);
4725 SDOperand SrcPtr = Op.getOperand(2);
4726 SrcValueSDNode *DstSV = cast<SrcValueSDNode>(Op.getOperand(3));
4727 SrcValueSDNode *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4));
4728
4729 SrcPtr = DAG.getLoad(getPointerTy(), Chain, SrcPtr,
4730 SrcSV->getValue(), SrcSV->getOffset());
4731 Chain = SrcPtr.getValue(1);
4732 for (unsigned i = 0; i < 3; ++i) {
4733 SDOperand Val = DAG.getLoad(MVT::i64, Chain, SrcPtr,
4734 SrcSV->getValue(), SrcSV->getOffset());
4735 Chain = Val.getValue(1);
4736 Chain = DAG.getStore(Chain, Val, DstPtr,
4737 DstSV->getValue(), DstSV->getOffset());
4738 if (i == 2)
4739 break;
4740 SrcPtr = DAG.getNode(ISD::ADD, getPointerTy(), SrcPtr,
Chris Lattner0bd48932008-01-17 07:00:52 +00004741 DAG.getIntPtrConstant(8));
Evan Chengae642192007-03-02 23:16:35 +00004742 DstPtr = DAG.getNode(ISD::ADD, getPointerTy(), DstPtr,
Chris Lattner0bd48932008-01-17 07:00:52 +00004743 DAG.getIntPtrConstant(8));
Evan Chengae642192007-03-02 23:16:35 +00004744 }
4745 return Chain;
4746}
4747
Evan Cheng0db9fe62006-04-25 20:13:52 +00004748SDOperand
4749X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDOperand Op, SelectionDAG &DAG) {
4750 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
4751 switch (IntNo) {
4752 default: return SDOperand(); // Don't custom lower most intrinsics.
Evan Cheng6be2c582006-04-05 23:38:46 +00004753 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004754 case Intrinsic::x86_sse_comieq_ss:
4755 case Intrinsic::x86_sse_comilt_ss:
4756 case Intrinsic::x86_sse_comile_ss:
4757 case Intrinsic::x86_sse_comigt_ss:
4758 case Intrinsic::x86_sse_comige_ss:
4759 case Intrinsic::x86_sse_comineq_ss:
4760 case Intrinsic::x86_sse_ucomieq_ss:
4761 case Intrinsic::x86_sse_ucomilt_ss:
4762 case Intrinsic::x86_sse_ucomile_ss:
4763 case Intrinsic::x86_sse_ucomigt_ss:
4764 case Intrinsic::x86_sse_ucomige_ss:
4765 case Intrinsic::x86_sse_ucomineq_ss:
4766 case Intrinsic::x86_sse2_comieq_sd:
4767 case Intrinsic::x86_sse2_comilt_sd:
4768 case Intrinsic::x86_sse2_comile_sd:
4769 case Intrinsic::x86_sse2_comigt_sd:
4770 case Intrinsic::x86_sse2_comige_sd:
4771 case Intrinsic::x86_sse2_comineq_sd:
4772 case Intrinsic::x86_sse2_ucomieq_sd:
4773 case Intrinsic::x86_sse2_ucomilt_sd:
4774 case Intrinsic::x86_sse2_ucomile_sd:
4775 case Intrinsic::x86_sse2_ucomigt_sd:
4776 case Intrinsic::x86_sse2_ucomige_sd:
4777 case Intrinsic::x86_sse2_ucomineq_sd: {
4778 unsigned Opc = 0;
4779 ISD::CondCode CC = ISD::SETCC_INVALID;
4780 switch (IntNo) {
4781 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004782 case Intrinsic::x86_sse_comieq_ss:
4783 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004784 Opc = X86ISD::COMI;
4785 CC = ISD::SETEQ;
4786 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004787 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004788 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004789 Opc = X86ISD::COMI;
4790 CC = ISD::SETLT;
4791 break;
4792 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004793 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004794 Opc = X86ISD::COMI;
4795 CC = ISD::SETLE;
4796 break;
4797 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004798 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004799 Opc = X86ISD::COMI;
4800 CC = ISD::SETGT;
4801 break;
4802 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004803 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004804 Opc = X86ISD::COMI;
4805 CC = ISD::SETGE;
4806 break;
4807 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004808 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004809 Opc = X86ISD::COMI;
4810 CC = ISD::SETNE;
4811 break;
4812 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004813 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004814 Opc = X86ISD::UCOMI;
4815 CC = ISD::SETEQ;
4816 break;
4817 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004818 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004819 Opc = X86ISD::UCOMI;
4820 CC = ISD::SETLT;
4821 break;
4822 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004823 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004824 Opc = X86ISD::UCOMI;
4825 CC = ISD::SETLE;
4826 break;
4827 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004828 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004829 Opc = X86ISD::UCOMI;
4830 CC = ISD::SETGT;
4831 break;
4832 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00004833 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00004834 Opc = X86ISD::UCOMI;
4835 CC = ISD::SETGE;
4836 break;
4837 case Intrinsic::x86_sse_ucomineq_ss:
4838 case Intrinsic::x86_sse2_ucomineq_sd:
4839 Opc = X86ISD::UCOMI;
4840 CC = ISD::SETNE;
4841 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00004842 }
Evan Cheng734503b2006-09-11 02:19:56 +00004843
Evan Cheng0db9fe62006-04-25 20:13:52 +00004844 unsigned X86CC;
Chris Lattnerf9570512006-09-13 03:22:10 +00004845 SDOperand LHS = Op.getOperand(1);
4846 SDOperand RHS = Op.getOperand(2);
4847 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004848
Evan Chenge5f62042007-09-29 00:00:36 +00004849 SDOperand Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
4850 SDOperand SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
4851 DAG.getConstant(X86CC, MVT::i8), Cond);
4852 return DAG.getNode(ISD::ANY_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00004853 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00004854 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004855}
Evan Cheng72261582005-12-20 06:22:03 +00004856
Nate Begemanbcc5f362007-01-29 22:58:52 +00004857SDOperand X86TargetLowering::LowerRETURNADDR(SDOperand Op, SelectionDAG &DAG) {
4858 // Depths > 0 not supported yet!
4859 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4860 return SDOperand();
4861
4862 // Just load the return address
4863 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4864 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
4865}
4866
4867SDOperand X86TargetLowering::LowerFRAMEADDR(SDOperand Op, SelectionDAG &DAG) {
4868 // Depths > 0 not supported yet!
4869 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
4870 return SDOperand();
4871
4872 SDOperand RetAddrFI = getReturnAddressFrameIndex(DAG);
4873 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Chris Lattner0bd48932008-01-17 07:00:52 +00004874 DAG.getIntPtrConstant(4));
Nate Begemanbcc5f362007-01-29 22:58:52 +00004875}
4876
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004877SDOperand X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDOperand Op,
4878 SelectionDAG &DAG) {
4879 // Is not yet supported on x86-64
4880 if (Subtarget->is64Bit())
4881 return SDOperand();
4882
Chris Lattner0bd48932008-01-17 07:00:52 +00004883 return DAG.getIntPtrConstant(8);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004884}
4885
4886SDOperand X86TargetLowering::LowerEH_RETURN(SDOperand Op, SelectionDAG &DAG)
4887{
4888 assert(!Subtarget->is64Bit() &&
4889 "Lowering of eh_return builtin is not supported yet on x86-64");
4890
4891 MachineFunction &MF = DAG.getMachineFunction();
4892 SDOperand Chain = Op.getOperand(0);
4893 SDOperand Offset = Op.getOperand(1);
4894 SDOperand Handler = Op.getOperand(2);
4895
4896 SDOperand Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
4897 getPointerTy());
4898
4899 SDOperand StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner0bd48932008-01-17 07:00:52 +00004900 DAG.getIntPtrConstant(-4UL));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004901 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
4902 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
4903 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner84bc5422007-12-31 04:13:23 +00004904 MF.getRegInfo().addLiveOut(X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00004905
4906 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
4907 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
4908}
4909
Duncan Sandsb116fac2007-07-27 20:02:49 +00004910SDOperand X86TargetLowering::LowerTRAMPOLINE(SDOperand Op,
4911 SelectionDAG &DAG) {
4912 SDOperand Root = Op.getOperand(0);
4913 SDOperand Trmp = Op.getOperand(1); // trampoline
4914 SDOperand FPtr = Op.getOperand(2); // nested function
4915 SDOperand Nest = Op.getOperand(3); // 'nest' parameter value
4916
4917 SrcValueSDNode *TrmpSV = cast<SrcValueSDNode>(Op.getOperand(4));
4918
Duncan Sands339e14f2008-01-16 22:55:25 +00004919 const X86InstrInfo *TII =
4920 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
4921
Duncan Sandsb116fac2007-07-27 20:02:49 +00004922 if (Subtarget->is64Bit()) {
Duncan Sands339e14f2008-01-16 22:55:25 +00004923 SDOperand OutChains[6];
4924
4925 // Large code-model.
4926
4927 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
4928 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
4929
4930 const unsigned char N86R10 =
4931 ((X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R10);
4932 const unsigned char N86R11 =
4933 ((X86RegisterInfo*)RegInfo)->getX86RegNum(X86::R11);
4934
4935 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
4936
4937 // Load the pointer to the nested function into R11.
4938 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
4939 SDOperand Addr = Trmp;
4940 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
4941 TrmpSV->getValue(), TrmpSV->getOffset());
4942
4943 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
4944 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpSV->getValue(),
4945 TrmpSV->getOffset() + 2, false, 2);
4946
4947 // Load the 'nest' parameter value into R10.
4948 // R10 is specified in X86CallingConv.td
4949 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
4950 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
4951 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
4952 TrmpSV->getValue(), TrmpSV->getOffset() + 10);
4953
4954 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
4955 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
4956 TrmpSV->getOffset() + 12, false, 2);
4957
4958 // Jump to the nested function.
4959 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
4960 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
4961 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
4962 TrmpSV->getValue(), TrmpSV->getOffset() + 20);
4963
4964 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
4965 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
4966 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
4967 TrmpSV->getValue(), TrmpSV->getOffset() + 22);
4968
4969 SDOperand Ops[] =
4970 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
4971 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00004972 } else {
4973 Function *Func = (Function *)
4974 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
4975 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00004976 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004977
4978 switch (CC) {
4979 default:
4980 assert(0 && "Unsupported calling convention");
4981 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00004982 case CallingConv::X86_StdCall: {
4983 // Pass 'nest' parameter in ECX.
4984 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00004985 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00004986
4987 // Check that ECX wasn't needed by an 'inreg' parameter.
4988 const FunctionType *FTy = Func->getFunctionType();
Duncan Sandsdc024672007-11-27 13:23:08 +00004989 const ParamAttrsList *Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00004990
4991 if (Attrs && !Func->isVarArg()) {
4992 unsigned InRegCount = 0;
4993 unsigned Idx = 1;
4994
4995 for (FunctionType::param_iterator I = FTy->param_begin(),
4996 E = FTy->param_end(); I != E; ++I, ++Idx)
4997 if (Attrs->paramHasAttr(Idx, ParamAttr::InReg))
4998 // FIXME: should only count parameters that are lowered to integers.
4999 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5000
5001 if (InRegCount > 2) {
5002 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5003 abort();
5004 }
5005 }
5006 break;
5007 }
5008 case CallingConv::X86_FastCall:
5009 // Pass 'nest' parameter in EAX.
5010 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005011 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005012 break;
5013 }
5014
5015 SDOperand OutChains[4];
5016 SDOperand Addr, Disp;
5017
5018 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5019 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5020
Duncan Sands339e14f2008-01-16 22:55:25 +00005021 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
5022 const unsigned char N86Reg =
5023 ((X86RegisterInfo*)RegInfo)->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005024 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Duncan Sandsb116fac2007-07-27 20:02:49 +00005025 Trmp, TrmpSV->getValue(), TrmpSV->getOffset());
5026
5027 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
5028 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpSV->getValue(),
5029 TrmpSV->getOffset() + 1, false, 1);
5030
Duncan Sands339e14f2008-01-16 22:55:25 +00005031 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005032 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5033 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
5034 TrmpSV->getValue() + 5, TrmpSV->getOffset());
5035
5036 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
5037 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpSV->getValue(),
5038 TrmpSV->getOffset() + 6, false, 1);
5039
Duncan Sandsf7331b32007-09-11 14:10:23 +00005040 SDOperand Ops[] =
5041 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
5042 return DAG.getNode(ISD::MERGE_VALUES, Op.Val->getVTList(), Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005043 }
5044}
5045
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005046SDOperand X86TargetLowering::LowerFLT_ROUNDS(SDOperand Op, SelectionDAG &DAG) {
5047 /*
5048 The rounding mode is in bits 11:10 of FPSR, and has the following
5049 settings:
5050 00 Round to nearest
5051 01 Round to -inf
5052 10 Round to +inf
5053 11 Round to 0
5054
5055 FLT_ROUNDS, on the other hand, expects the following:
5056 -1 Undefined
5057 0 Round to 0
5058 1 Round to nearest
5059 2 Round to +inf
5060 3 Round to -inf
5061
5062 To perform the conversion, we do:
5063 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5064 */
5065
5066 MachineFunction &MF = DAG.getMachineFunction();
5067 const TargetMachine &TM = MF.getTarget();
5068 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5069 unsigned StackAlignment = TFI.getStackAlignment();
5070 MVT::ValueType VT = Op.getValueType();
5071
5072 // Save FP Control Word to stack slot
5073 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
5074 SDOperand StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5075
5076 SDOperand Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
5077 DAG.getEntryNode(), StackSlot);
5078
5079 // Load FP Control Word from stack slot
5080 SDOperand CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
5081
5082 // Transform as necessary
5083 SDOperand CWD1 =
5084 DAG.getNode(ISD::SRL, MVT::i16,
5085 DAG.getNode(ISD::AND, MVT::i16,
5086 CWD, DAG.getConstant(0x800, MVT::i16)),
5087 DAG.getConstant(11, MVT::i8));
5088 SDOperand CWD2 =
5089 DAG.getNode(ISD::SRL, MVT::i16,
5090 DAG.getNode(ISD::AND, MVT::i16,
5091 CWD, DAG.getConstant(0x400, MVT::i16)),
5092 DAG.getConstant(9, MVT::i8));
5093
5094 SDOperand RetVal =
5095 DAG.getNode(ISD::AND, MVT::i16,
5096 DAG.getNode(ISD::ADD, MVT::i16,
5097 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5098 DAG.getConstant(1, MVT::i16)),
5099 DAG.getConstant(3, MVT::i16));
5100
5101
5102 return DAG.getNode((MVT::getSizeInBits(VT) < 16 ?
5103 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5104}
5105
Evan Cheng18efe262007-12-14 02:13:44 +00005106SDOperand X86TargetLowering::LowerCTLZ(SDOperand Op, SelectionDAG &DAG) {
5107 MVT::ValueType VT = Op.getValueType();
5108 MVT::ValueType OpVT = VT;
5109 unsigned NumBits = MVT::getSizeInBits(VT);
5110
5111 Op = Op.getOperand(0);
5112 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005113 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005114 OpVT = MVT::i32;
5115 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5116 }
Evan Cheng18efe262007-12-14 02:13:44 +00005117
Evan Cheng152804e2007-12-14 08:30:15 +00005118 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5119 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5120 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5121
5122 // If src is zero (i.e. bsr sets ZF), returns NumBits.
5123 SmallVector<SDOperand, 4> Ops;
5124 Ops.push_back(Op);
5125 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5126 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5127 Ops.push_back(Op.getValue(1));
5128 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5129
5130 // Finally xor with NumBits-1.
5131 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5132
Evan Cheng18efe262007-12-14 02:13:44 +00005133 if (VT == MVT::i8)
5134 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5135 return Op;
5136}
5137
5138SDOperand X86TargetLowering::LowerCTTZ(SDOperand Op, SelectionDAG &DAG) {
5139 MVT::ValueType VT = Op.getValueType();
5140 MVT::ValueType OpVT = VT;
Evan Cheng152804e2007-12-14 08:30:15 +00005141 unsigned NumBits = MVT::getSizeInBits(VT);
Evan Cheng18efe262007-12-14 02:13:44 +00005142
5143 Op = Op.getOperand(0);
5144 if (VT == MVT::i8) {
5145 OpVT = MVT::i32;
5146 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5147 }
Evan Cheng152804e2007-12-14 08:30:15 +00005148
5149 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5150 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5151 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5152
5153 // If src is zero (i.e. bsf sets ZF), returns NumBits.
5154 SmallVector<SDOperand, 4> Ops;
5155 Ops.push_back(Op);
5156 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5157 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5158 Ops.push_back(Op.getValue(1));
5159 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5160
Evan Cheng18efe262007-12-14 02:13:44 +00005161 if (VT == MVT::i8)
5162 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5163 return Op;
5164}
5165
Evan Cheng0db9fe62006-04-25 20:13:52 +00005166/// LowerOperation - Provide custom lowering hooks for some operations.
5167///
5168SDOperand X86TargetLowering::LowerOperation(SDOperand Op, SelectionDAG &DAG) {
5169 switch (Op.getOpcode()) {
5170 default: assert(0 && "Should not custom lower this!");
5171 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5172 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5173 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5174 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5175 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5176 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5177 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005178 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005179 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5180 case ISD::SHL_PARTS:
5181 case ISD::SRA_PARTS:
5182 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5183 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5184 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5185 case ISD::FABS: return LowerFABS(Op, DAG);
5186 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005187 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005188 case ISD::SETCC: return LowerSETCC(Op, DAG);
5189 case ISD::SELECT: return LowerSELECT(Op, DAG);
5190 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005191 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005192 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005193 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005194 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005195 case ISD::MEMSET: return LowerMEMSET(Op, DAG);
5196 case ISD::MEMCPY: return LowerMEMCPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005197 case ISD::VASTART: return LowerVASTART(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005198 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005199 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005200 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5201 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005202 case ISD::FRAME_TO_ARGS_OFFSET:
5203 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005204 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005205 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005206 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005207 case ISD::FLT_ROUNDS: return LowerFLT_ROUNDS(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005208 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5209 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005210
5211 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5212 case ISD::READCYCLECOUNTER:
5213 return SDOperand(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005214 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005215}
5216
5217/// ExpandOperation - Provide custom lowering hooks for expanding operations.
5218SDNode *X86TargetLowering::ExpandOperationResult(SDNode *N, SelectionDAG &DAG) {
5219 switch (N->getOpcode()) {
5220 default: assert(0 && "Should not custom lower this!");
5221 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5222 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
5223 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005224}
5225
Evan Cheng72261582005-12-20 06:22:03 +00005226const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5227 switch (Opcode) {
5228 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005229 case X86ISD::BSF: return "X86ISD::BSF";
5230 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005231 case X86ISD::SHLD: return "X86ISD::SHLD";
5232 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005233 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005234 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005235 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005236 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005237 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005238 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005239 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5240 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5241 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005242 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005243 case X86ISD::FST: return "X86ISD::FST";
5244 case X86ISD::FP_GET_RESULT: return "X86ISD::FP_GET_RESULT";
Evan Cheng0d9e9762008-01-29 19:34:22 +00005245 case X86ISD::FP_GET_RESULT2: return "X86ISD::FP_GET_RESULT2";
Evan Chengb077b842005-12-21 02:39:21 +00005246 case X86ISD::FP_SET_RESULT: return "X86ISD::FP_SET_RESULT";
Evan Cheng72261582005-12-20 06:22:03 +00005247 case X86ISD::CALL: return "X86ISD::CALL";
5248 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5249 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5250 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005251 case X86ISD::COMI: return "X86ISD::COMI";
5252 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005253 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005254 case X86ISD::CMOV: return "X86ISD::CMOV";
5255 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005256 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005257 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
5258 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00005259 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00005260 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Evan Chengbc4832b2006-03-24 23:15:12 +00005261 case X86ISD::S2VEC: return "X86ISD::S2VEC";
Evan Chengb067a1e2006-03-31 19:22:53 +00005262 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Evan Cheng653159f2006-03-31 21:55:24 +00005263 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00005264 case X86ISD::FMAX: return "X86ISD::FMAX";
5265 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00005266 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
5267 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005268 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
5269 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005270 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005271 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005272 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng72261582005-12-20 06:22:03 +00005273 }
5274}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005275
Chris Lattnerc9addb72007-03-30 23:15:24 +00005276// isLegalAddressingMode - Return true if the addressing mode represented
5277// by AM is legal for this target, for a load/store of the specified type.
5278bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
5279 const Type *Ty) const {
5280 // X86 supports extremely general addressing modes.
5281
5282 // X86 allows a sign-extended 32-bit immediate field as a displacement.
5283 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
5284 return false;
5285
5286 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00005287 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00005288 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
5289 return false;
Evan Cheng52787842007-08-01 23:46:47 +00005290
5291 // X86-64 only supports addr of globals in small code model.
5292 if (Subtarget->is64Bit()) {
5293 if (getTargetMachine().getCodeModel() != CodeModel::Small)
5294 return false;
5295 // If lower 4G is not available, then we must use rip-relative addressing.
5296 if (AM.BaseOffs || AM.Scale > 1)
5297 return false;
5298 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00005299 }
5300
5301 switch (AM.Scale) {
5302 case 0:
5303 case 1:
5304 case 2:
5305 case 4:
5306 case 8:
5307 // These scales always work.
5308 break;
5309 case 3:
5310 case 5:
5311 case 9:
5312 // These scales are formed with basereg+scalereg. Only accept if there is
5313 // no basereg yet.
5314 if (AM.HasBaseReg)
5315 return false;
5316 break;
5317 default: // Other stuff never works.
5318 return false;
5319 }
5320
5321 return true;
5322}
5323
5324
Evan Cheng2bd122c2007-10-26 01:56:11 +00005325bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
5326 if (!Ty1->isInteger() || !Ty2->isInteger())
5327 return false;
Evan Chenge127a732007-10-29 07:57:50 +00005328 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
5329 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
5330 if (NumBits1 <= NumBits2)
5331 return false;
5332 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00005333}
5334
Evan Cheng3c3ddb32007-10-29 19:58:20 +00005335bool X86TargetLowering::isTruncateFree(MVT::ValueType VT1,
5336 MVT::ValueType VT2) const {
5337 if (!MVT::isInteger(VT1) || !MVT::isInteger(VT2))
5338 return false;
5339 unsigned NumBits1 = MVT::getSizeInBits(VT1);
5340 unsigned NumBits2 = MVT::getSizeInBits(VT2);
5341 if (NumBits1 <= NumBits2)
5342 return false;
5343 return Subtarget->is64Bit() || NumBits1 < 64;
5344}
Evan Cheng2bd122c2007-10-26 01:56:11 +00005345
Evan Cheng60c07e12006-07-05 22:17:51 +00005346/// isShuffleMaskLegal - Targets can use this to indicate that they only
5347/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
5348/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
5349/// are assumed to be legal.
5350bool
5351X86TargetLowering::isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
5352 // Only do shuffles on 128-bit vector types for now.
5353 if (MVT::getSizeInBits(VT) == 64) return false;
5354 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00005355 isIdentityMask(Mask.Val) ||
5356 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005357 isSplatMask(Mask.Val) ||
5358 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
5359 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005360 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00005361 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00005362 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00005363}
5364
5365bool X86TargetLowering::isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
5366 MVT::ValueType EVT,
5367 SelectionDAG &DAG) const {
5368 unsigned NumElts = BVOps.size();
5369 // Only do shuffles on 128-bit vector types for now.
5370 if (MVT::getSizeInBits(EVT) * NumElts == 64) return false;
5371 if (NumElts == 2) return true;
5372 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00005373 return (isMOVLMask(&BVOps[0], 4) ||
5374 isCommutedMOVL(&BVOps[0], 4, true) ||
5375 isSHUFPMask(&BVOps[0], 4) ||
5376 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00005377 }
5378 return false;
5379}
5380
5381//===----------------------------------------------------------------------===//
5382// X86 Scheduler Hooks
5383//===----------------------------------------------------------------------===//
5384
5385MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00005386X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
5387 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00005388 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00005389 switch (MI->getOpcode()) {
5390 default: assert(false && "Unexpected instr type to insert");
5391 case X86::CMOV_FR32:
5392 case X86::CMOV_FR64:
5393 case X86::CMOV_V4F32:
5394 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00005395 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005396 // To "insert" a SELECT_CC instruction, we actually have to insert the
5397 // diamond control-flow pattern. The incoming instruction knows the
5398 // destination vreg to set, the condition code register to branch on, the
5399 // true/false values to select between, and a branch opcode to use.
5400 const BasicBlock *LLVM_BB = BB->getBasicBlock();
5401 ilist<MachineBasicBlock>::iterator It = BB;
5402 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005403
Evan Cheng60c07e12006-07-05 22:17:51 +00005404 // thisMBB:
5405 // ...
5406 // TrueVal = ...
5407 // cmpTY ccX, r1, r2
5408 // bCC copy1MBB
5409 // fallthrough --> copy0MBB
5410 MachineBasicBlock *thisMBB = BB;
5411 MachineBasicBlock *copy0MBB = new MachineBasicBlock(LLVM_BB);
5412 MachineBasicBlock *sinkMBB = new MachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005413 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00005414 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00005415 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Evan Cheng60c07e12006-07-05 22:17:51 +00005416 MachineFunction *F = BB->getParent();
5417 F->getBasicBlockList().insert(It, copy0MBB);
5418 F->getBasicBlockList().insert(It, sinkMBB);
5419 // Update machine-CFG edges by first adding all successors of the current
5420 // block to the new block which will contain the Phi node for the select.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005421 for(MachineBasicBlock::succ_iterator i = BB->succ_begin(),
Evan Cheng60c07e12006-07-05 22:17:51 +00005422 e = BB->succ_end(); i != e; ++i)
5423 sinkMBB->addSuccessor(*i);
5424 // Next, remove all successors of the current block, and add the true
5425 // and fallthrough blocks as its successors.
5426 while(!BB->succ_empty())
5427 BB->removeSuccessor(BB->succ_begin());
5428 BB->addSuccessor(copy0MBB);
5429 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005430
Evan Cheng60c07e12006-07-05 22:17:51 +00005431 // copy0MBB:
5432 // %FalseValue = ...
5433 // # fallthrough to sinkMBB
5434 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005435
Evan Cheng60c07e12006-07-05 22:17:51 +00005436 // Update machine-CFG edges
5437 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005438
Evan Cheng60c07e12006-07-05 22:17:51 +00005439 // sinkMBB:
5440 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
5441 // ...
5442 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00005443 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00005444 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
5445 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
5446
5447 delete MI; // The pseudo instruction is gone now.
5448 return BB;
5449 }
5450
Dale Johannesen849f2142007-07-03 00:53:03 +00005451 case X86::FP32_TO_INT16_IN_MEM:
5452 case X86::FP32_TO_INT32_IN_MEM:
5453 case X86::FP32_TO_INT64_IN_MEM:
5454 case X86::FP64_TO_INT16_IN_MEM:
5455 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00005456 case X86::FP64_TO_INT64_IN_MEM:
5457 case X86::FP80_TO_INT16_IN_MEM:
5458 case X86::FP80_TO_INT32_IN_MEM:
5459 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00005460 // Change the floating point control register to use "round towards zero"
5461 // mode when truncating to an integer value.
5462 MachineFunction *F = BB->getParent();
5463 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005464 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005465
5466 // Load the old value of the high byte of the control word...
5467 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00005468 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00005469 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005470
5471 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005472 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
5473 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00005474
5475 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00005476 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005477
5478 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00005479 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
5480 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00005481
5482 // Get the X86 opcode to use.
5483 unsigned Opc;
5484 switch (MI->getOpcode()) {
5485 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00005486 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
5487 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
5488 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
5489 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
5490 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
5491 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00005492 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
5493 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
5494 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00005495 }
5496
5497 X86AddressMode AM;
5498 MachineOperand &Op = MI->getOperand(0);
5499 if (Op.isRegister()) {
5500 AM.BaseType = X86AddressMode::RegBase;
5501 AM.Base.Reg = Op.getReg();
5502 } else {
5503 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00005504 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00005505 }
5506 Op = MI->getOperand(1);
5507 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005508 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005509 Op = MI->getOperand(2);
5510 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00005511 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005512 Op = MI->getOperand(3);
5513 if (Op.isGlobalAddress()) {
5514 AM.GV = Op.getGlobal();
5515 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00005516 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00005517 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00005518 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
5519 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00005520
5521 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00005522 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00005523
5524 delete MI; // The pseudo instruction is gone now.
5525 return BB;
5526 }
5527 }
5528}
5529
5530//===----------------------------------------------------------------------===//
5531// X86 Optimization Hooks
5532//===----------------------------------------------------------------------===//
5533
Nate Begeman368e18d2006-02-16 21:11:51 +00005534void X86TargetLowering::computeMaskedBitsForTargetNode(const SDOperand Op,
5535 uint64_t Mask,
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005536 uint64_t &KnownZero,
Nate Begeman368e18d2006-02-16 21:11:51 +00005537 uint64_t &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00005538 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00005539 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005540 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00005541 assert((Opc >= ISD::BUILTIN_OP_END ||
5542 Opc == ISD::INTRINSIC_WO_CHAIN ||
5543 Opc == ISD::INTRINSIC_W_CHAIN ||
5544 Opc == ISD::INTRINSIC_VOID) &&
5545 "Should use MaskedValueIsZero if you don't know whether Op"
5546 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005547
Evan Cheng865f0602006-04-05 06:11:20 +00005548 KnownZero = KnownOne = 0; // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005549 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00005550 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005551 case X86ISD::SETCC:
Nate Begeman368e18d2006-02-16 21:11:51 +00005552 KnownZero |= (MVT::getIntVTBitMask(Op.getValueType()) ^ 1ULL);
5553 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005554 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00005555}
Chris Lattner259e97c2006-01-31 19:43:35 +00005556
Evan Cheng206ee9d2006-07-07 08:33:52 +00005557/// getShuffleScalarElt - Returns the scalar element that will make up the ith
5558/// element of the result of the vector shuffle.
5559static SDOperand getShuffleScalarElt(SDNode *N, unsigned i, SelectionDAG &DAG) {
5560 MVT::ValueType VT = N->getValueType(0);
5561 SDOperand PermMask = N->getOperand(2);
5562 unsigned NumElems = PermMask.getNumOperands();
5563 SDOperand V = (i < NumElems) ? N->getOperand(0) : N->getOperand(1);
5564 i %= NumElems;
5565 if (V.getOpcode() == ISD::SCALAR_TO_VECTOR) {
5566 return (i == 0)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00005567 ? V.getOperand(0) : DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005568 } else if (V.getOpcode() == ISD::VECTOR_SHUFFLE) {
5569 SDOperand Idx = PermMask.getOperand(i);
5570 if (Idx.getOpcode() == ISD::UNDEF)
Dan Gohman51eaa862007-06-14 22:58:02 +00005571 return DAG.getNode(ISD::UNDEF, MVT::getVectorElementType(VT));
Evan Cheng206ee9d2006-07-07 08:33:52 +00005572 return getShuffleScalarElt(V.Val,cast<ConstantSDNode>(Idx)->getValue(),DAG);
5573 }
5574 return SDOperand();
5575}
5576
5577/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
5578/// node is a GlobalAddress + an offset.
5579static bool isGAPlusOffset(SDNode *N, GlobalValue* &GA, int64_t &Offset) {
Evan Cheng0085a282006-11-30 21:55:46 +00005580 unsigned Opc = N->getOpcode();
Evan Cheng19f2ffc2006-12-05 04:01:03 +00005581 if (Opc == X86ISD::Wrapper) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005582 if (dyn_cast<GlobalAddressSDNode>(N->getOperand(0))) {
5583 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
5584 return true;
5585 }
Evan Cheng0085a282006-11-30 21:55:46 +00005586 } else if (Opc == ISD::ADD) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005587 SDOperand N1 = N->getOperand(0);
5588 SDOperand N2 = N->getOperand(1);
5589 if (isGAPlusOffset(N1.Val, GA, Offset)) {
5590 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N2);
5591 if (V) {
5592 Offset += V->getSignExtended();
5593 return true;
5594 }
5595 } else if (isGAPlusOffset(N2.Val, GA, Offset)) {
5596 ConstantSDNode *V = dyn_cast<ConstantSDNode>(N1);
5597 if (V) {
5598 Offset += V->getSignExtended();
5599 return true;
5600 }
5601 }
5602 }
5603 return false;
5604}
5605
5606/// isConsecutiveLoad - Returns true if N is loading from an address of Base
5607/// + Dist * Size.
5608static bool isConsecutiveLoad(SDNode *N, SDNode *Base, int Dist, int Size,
5609 MachineFrameInfo *MFI) {
5610 if (N->getOperand(0).Val != Base->getOperand(0).Val)
5611 return false;
5612
5613 SDOperand Loc = N->getOperand(1);
5614 SDOperand BaseLoc = Base->getOperand(1);
5615 if (Loc.getOpcode() == ISD::FrameIndex) {
5616 if (BaseLoc.getOpcode() != ISD::FrameIndex)
5617 return false;
Dan Gohman275769a2007-07-23 20:24:29 +00005618 int FI = cast<FrameIndexSDNode>(Loc)->getIndex();
5619 int BFI = cast<FrameIndexSDNode>(BaseLoc)->getIndex();
Evan Cheng206ee9d2006-07-07 08:33:52 +00005620 int FS = MFI->getObjectSize(FI);
5621 int BFS = MFI->getObjectSize(BFI);
5622 if (FS != BFS || FS != Size) return false;
5623 return MFI->getObjectOffset(FI) == (MFI->getObjectOffset(BFI) + Dist*Size);
5624 } else {
5625 GlobalValue *GV1 = NULL;
5626 GlobalValue *GV2 = NULL;
5627 int64_t Offset1 = 0;
5628 int64_t Offset2 = 0;
5629 bool isGA1 = isGAPlusOffset(Loc.Val, GV1, Offset1);
5630 bool isGA2 = isGAPlusOffset(BaseLoc.Val, GV2, Offset2);
5631 if (isGA1 && isGA2 && GV1 == GV2)
5632 return Offset1 == (Offset2 + Dist*Size);
5633 }
5634
5635 return false;
5636}
5637
Evan Cheng1e60c092006-07-10 21:37:44 +00005638static bool isBaseAlignment16(SDNode *Base, MachineFrameInfo *MFI,
5639 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005640 GlobalValue *GV;
5641 int64_t Offset;
5642 if (isGAPlusOffset(Base, GV, Offset))
5643 return (GV->getAlignment() >= 16 && (Offset % 16) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00005644 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00005645 return false;
5646}
5647
5648
5649/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
5650/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
5651/// if the load addresses are consecutive, non-overlapping, and in the right
5652/// order.
Evan Cheng1e60c092006-07-10 21:37:44 +00005653static SDOperand PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
5654 const X86Subtarget *Subtarget) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005655 MachineFunction &MF = DAG.getMachineFunction();
5656 MachineFrameInfo *MFI = MF.getFrameInfo();
5657 MVT::ValueType VT = N->getValueType(0);
Dan Gohman51eaa862007-06-14 22:58:02 +00005658 MVT::ValueType EVT = MVT::getVectorElementType(VT);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005659 SDOperand PermMask = N->getOperand(2);
5660 int NumElems = (int)PermMask.getNumOperands();
5661 SDNode *Base = NULL;
5662 for (int i = 0; i < NumElems; ++i) {
5663 SDOperand Idx = PermMask.getOperand(i);
5664 if (Idx.getOpcode() == ISD::UNDEF) {
5665 if (!Base) return SDOperand();
5666 } else {
5667 SDOperand Arg =
5668 getShuffleScalarElt(N, cast<ConstantSDNode>(Idx)->getValue(), DAG);
Evan Cheng466685d2006-10-09 20:57:25 +00005669 if (!Arg.Val || !ISD::isNON_EXTLoad(Arg.Val))
Evan Cheng206ee9d2006-07-07 08:33:52 +00005670 return SDOperand();
5671 if (!Base)
5672 Base = Arg.Val;
5673 else if (!isConsecutiveLoad(Arg.Val, Base,
5674 i, MVT::getSizeInBits(EVT)/8,MFI))
5675 return SDOperand();
5676 }
5677 }
5678
Evan Cheng1e60c092006-07-10 21:37:44 +00005679 bool isAlign16 = isBaseAlignment16(Base->getOperand(1).Val, MFI, Subtarget);
Dan Gohmand3006222007-07-27 17:16:43 +00005680 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Cheng466685d2006-10-09 20:57:25 +00005681 if (isAlign16) {
Evan Cheng466685d2006-10-09 20:57:25 +00005682 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00005683 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng466685d2006-10-09 20:57:25 +00005684 } else {
Dan Gohmand3006222007-07-27 17:16:43 +00005685 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
5686 LD->getSrcValueOffset(), LD->isVolatile(),
5687 LD->getAlignment());
Evan Cheng311ace02006-08-11 07:35:45 +00005688 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00005689}
5690
Chris Lattner83e6c992006-10-04 06:57:07 +00005691/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
5692static SDOperand PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
5693 const X86Subtarget *Subtarget) {
5694 SDOperand Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005695
Chris Lattner83e6c992006-10-04 06:57:07 +00005696 // If we have SSE[12] support, try to form min/max nodes.
5697 if (Subtarget->hasSSE2() &&
5698 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
5699 if (Cond.getOpcode() == ISD::SETCC) {
5700 // Get the LHS/RHS of the select.
5701 SDOperand LHS = N->getOperand(1);
5702 SDOperand RHS = N->getOperand(2);
5703 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005704
Evan Cheng8ca29322006-11-10 21:43:37 +00005705 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00005706 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005707 switch (CC) {
5708 default: break;
5709 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
5710 case ISD::SETULE:
5711 case ISD::SETLE:
5712 if (!UnsafeFPMath) break;
5713 // FALL THROUGH.
5714 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
5715 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005716 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005717 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005718
Chris Lattner1907a7b2006-10-05 04:11:26 +00005719 case ISD::SETOGT: // (X > Y) ? X : Y -> max
5720 case ISD::SETUGT:
5721 case ISD::SETGT:
5722 if (!UnsafeFPMath) break;
5723 // FALL THROUGH.
5724 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
5725 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005726 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005727 break;
5728 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005729 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00005730 switch (CC) {
5731 default: break;
5732 case ISD::SETOGT: // (X > Y) ? Y : X -> min
5733 case ISD::SETUGT:
5734 case ISD::SETGT:
5735 if (!UnsafeFPMath) break;
5736 // FALL THROUGH.
5737 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
5738 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00005739 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005740 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005741
Chris Lattner1907a7b2006-10-05 04:11:26 +00005742 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
5743 case ISD::SETULE:
5744 case ISD::SETLE:
5745 if (!UnsafeFPMath) break;
5746 // FALL THROUGH.
5747 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
5748 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00005749 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00005750 break;
5751 }
Chris Lattner83e6c992006-10-04 06:57:07 +00005752 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005753
Evan Cheng8ca29322006-11-10 21:43:37 +00005754 if (Opcode)
5755 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00005756 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005757
Chris Lattner83e6c992006-10-04 06:57:07 +00005758 }
5759
5760 return SDOperand();
5761}
5762
Chris Lattner6cf73262008-01-25 06:14:17 +00005763/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
5764/// X86ISD::FXOR nodes.
Chris Lattneraf723b92008-01-25 05:46:26 +00005765static SDOperand PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00005766 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
5767 // F[X]OR(0.0, x) -> x
5768 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00005769 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5770 if (C->getValueAPF().isPosZero())
5771 return N->getOperand(1);
5772 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5773 if (C->getValueAPF().isPosZero())
5774 return N->getOperand(0);
5775 return SDOperand();
5776}
5777
5778/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
5779static SDOperand PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
5780 // FAND(0.0, x) -> 0.0
5781 // FAND(x, 0.0) -> 0.0
5782 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
5783 if (C->getValueAPF().isPosZero())
5784 return N->getOperand(0);
5785 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
5786 if (C->getValueAPF().isPosZero())
5787 return N->getOperand(1);
5788 return SDOperand();
5789}
5790
Chris Lattner83e6c992006-10-04 06:57:07 +00005791
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005792SDOperand X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00005793 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00005794 SelectionDAG &DAG = DCI.DAG;
5795 switch (N->getOpcode()) {
5796 default: break;
Chris Lattneraf723b92008-01-25 05:46:26 +00005797 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, Subtarget);
5798 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00005799 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00005800 case X86ISD::FOR: return PerformFORCombine(N, DAG);
5801 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00005802 }
5803
5804 return SDOperand();
5805}
5806
Evan Cheng60c07e12006-07-05 22:17:51 +00005807//===----------------------------------------------------------------------===//
5808// X86 Inline Assembly Support
5809//===----------------------------------------------------------------------===//
5810
Chris Lattnerf4dff842006-07-11 02:54:03 +00005811/// getConstraintType - Given a constraint letter, return the type of
5812/// constraint it is for this target.
5813X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00005814X86TargetLowering::getConstraintType(const std::string &Constraint) const {
5815 if (Constraint.size() == 1) {
5816 switch (Constraint[0]) {
5817 case 'A':
5818 case 'r':
5819 case 'R':
5820 case 'l':
5821 case 'q':
5822 case 'Q':
5823 case 'x':
5824 case 'Y':
5825 return C_RegisterClass;
5826 default:
5827 break;
5828 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00005829 }
Chris Lattner4234f572007-03-25 02:14:49 +00005830 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00005831}
5832
Dale Johannesenba2a0b92008-01-29 02:21:21 +00005833/// LowerXConstraint - try to replace an X constraint, which matches anything,
5834/// with another that has more specific requirements based on the type of the
5835/// corresponding operand.
5836void X86TargetLowering::lowerXConstraint(MVT::ValueType ConstraintVT,
5837 std::string& s) const {
5838 if (MVT::isFloatingPoint(ConstraintVT)) {
5839 if (Subtarget->hasSSE2())
5840 s = "Y";
5841 else if (Subtarget->hasSSE1())
5842 s = "x";
5843 else
5844 s = "f";
5845 } else
5846 return TargetLowering::lowerXConstraint(ConstraintVT, s);
5847}
5848
Chris Lattner48884cd2007-08-25 00:47:38 +00005849/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
5850/// vector. If it is invalid, don't add anything to Ops.
5851void X86TargetLowering::LowerAsmOperandForConstraint(SDOperand Op,
5852 char Constraint,
5853 std::vector<SDOperand>&Ops,
5854 SelectionDAG &DAG) {
5855 SDOperand Result(0, 0);
5856
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005857 switch (Constraint) {
5858 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00005859 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00005860 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005861 if (C->getValue() <= 31) {
5862 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5863 break;
5864 }
Devang Patel84f7fd22007-03-17 00:13:28 +00005865 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005866 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00005867 case 'N':
5868 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00005869 if (C->getValue() <= 255) {
5870 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
5871 break;
5872 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00005873 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005874 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00005875 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005876 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00005877 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
5878 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
5879 break;
5880 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005881
Chris Lattnerdc43a882007-05-03 16:52:29 +00005882 // If we are in non-pic codegen mode, we allow the address of a global (with
5883 // an optional displacement) to be used with 'i'.
5884 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
5885 int64_t Offset = 0;
5886
5887 // Match either (GA) or (GA+C)
5888 if (GA) {
5889 Offset = GA->getOffset();
5890 } else if (Op.getOpcode() == ISD::ADD) {
5891 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5892 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5893 if (C && GA) {
5894 Offset = GA->getOffset()+C->getValue();
5895 } else {
5896 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5897 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
5898 if (C && GA)
5899 Offset = GA->getOffset()+C->getValue();
5900 else
5901 C = 0, GA = 0;
5902 }
5903 }
5904
5905 if (GA) {
5906 // If addressing this global requires a load (e.g. in PIC mode), we can't
5907 // match.
5908 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
5909 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00005910 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005911
Chris Lattnerdc43a882007-05-03 16:52:29 +00005912 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
5913 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00005914 Result = Op;
5915 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005916 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005917
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005918 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00005919 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005920 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00005921 }
Chris Lattner48884cd2007-08-25 00:47:38 +00005922
5923 if (Result.Val) {
5924 Ops.push_back(Result);
5925 return;
5926 }
5927 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00005928}
5929
Chris Lattner259e97c2006-01-31 19:43:35 +00005930std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00005931getRegClassForInlineAsmConstraint(const std::string &Constraint,
5932 MVT::ValueType VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00005933 if (Constraint.size() == 1) {
5934 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00005935 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00005936 default: break; // Unknown constraint letter
5937 case 'A': // EAX/EDX
5938 if (VT == MVT::i32 || VT == MVT::i64)
5939 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
5940 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005941 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
5942 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00005943 if (VT == MVT::i32)
5944 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
5945 else if (VT == MVT::i16)
5946 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
5947 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00005948 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00005949 else if (VT == MVT::i64)
5950 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
5951 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00005952 }
5953 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005954
Chris Lattner1efa40f2006-02-22 00:56:39 +00005955 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00005956}
Chris Lattnerf76d1802006-07-31 23:26:50 +00005957
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005958std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00005959X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
5960 MVT::ValueType VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00005961 // First, see if this is a constraint that directly corresponds to an LLVM
5962 // register class.
5963 if (Constraint.size() == 1) {
5964 // GCC Constraint Letters
5965 switch (Constraint[0]) {
5966 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005967 case 'r': // GENERAL_REGS
5968 case 'R': // LEGACY_REGS
5969 case 'l': // INDEX_REGS
5970 if (VT == MVT::i64 && Subtarget->is64Bit())
5971 return std::make_pair(0U, X86::GR64RegisterClass);
5972 if (VT == MVT::i32)
5973 return std::make_pair(0U, X86::GR32RegisterClass);
5974 else if (VT == MVT::i16)
5975 return std::make_pair(0U, X86::GR16RegisterClass);
5976 else if (VT == MVT::i8)
5977 return std::make_pair(0U, X86::GR8RegisterClass);
5978 break;
Chris Lattner6c284d72007-04-12 04:14:49 +00005979 case 'y': // MMX_REGS if MMX allowed.
5980 if (!Subtarget->hasMMX()) break;
5981 return std::make_pair(0U, X86::VR64RegisterClass);
5982 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00005983 case 'Y': // SSE_REGS if SSE2 allowed
5984 if (!Subtarget->hasSSE2()) break;
5985 // FALL THROUGH.
5986 case 'x': // SSE_REGS if SSE1 allowed
5987 if (!Subtarget->hasSSE1()) break;
5988
5989 switch (VT) {
5990 default: break;
5991 // Scalar SSE types.
5992 case MVT::f32:
5993 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00005994 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005995 case MVT::f64:
5996 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00005997 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00005998 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00005999 case MVT::v16i8:
6000 case MVT::v8i16:
6001 case MVT::v4i32:
6002 case MVT::v2i64:
6003 case MVT::v4f32:
6004 case MVT::v2f64:
6005 return std::make_pair(0U, X86::VR128RegisterClass);
6006 }
Chris Lattnerad043e82007-04-09 05:11:28 +00006007 break;
6008 }
6009 }
6010
Chris Lattnerf76d1802006-07-31 23:26:50 +00006011 // Use the default implementation in TargetLowering to convert the register
6012 // constraint into a member of a register class.
6013 std::pair<unsigned, const TargetRegisterClass*> Res;
6014 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00006015
6016 // Not found as a standard register?
6017 if (Res.second == 0) {
6018 // GCC calls "st(0)" just plain "st".
6019 if (StringsEqualNoCase("{st}", Constraint)) {
6020 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00006021 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00006022 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006023
Chris Lattner1a60aa72006-10-31 19:42:44 +00006024 return Res;
6025 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006026
Chris Lattnerf76d1802006-07-31 23:26:50 +00006027 // Otherwise, check to see if this is a register class of the wrong value
6028 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
6029 // turn into {ax},{dx}.
6030 if (Res.second->hasType(VT))
6031 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006032
Chris Lattnerf76d1802006-07-31 23:26:50 +00006033 // All of the single-register GCC register classes map their values onto
6034 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
6035 // really want an 8-bit or 32-bit register, map to the appropriate register
6036 // class and return the appropriate register.
6037 if (Res.second != X86::GR16RegisterClass)
6038 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006039
Chris Lattnerf76d1802006-07-31 23:26:50 +00006040 if (VT == MVT::i8) {
6041 unsigned DestReg = 0;
6042 switch (Res.first) {
6043 default: break;
6044 case X86::AX: DestReg = X86::AL; break;
6045 case X86::DX: DestReg = X86::DL; break;
6046 case X86::CX: DestReg = X86::CL; break;
6047 case X86::BX: DestReg = X86::BL; break;
6048 }
6049 if (DestReg) {
6050 Res.first = DestReg;
6051 Res.second = Res.second = X86::GR8RegisterClass;
6052 }
6053 } else if (VT == MVT::i32) {
6054 unsigned DestReg = 0;
6055 switch (Res.first) {
6056 default: break;
6057 case X86::AX: DestReg = X86::EAX; break;
6058 case X86::DX: DestReg = X86::EDX; break;
6059 case X86::CX: DestReg = X86::ECX; break;
6060 case X86::BX: DestReg = X86::EBX; break;
6061 case X86::SI: DestReg = X86::ESI; break;
6062 case X86::DI: DestReg = X86::EDI; break;
6063 case X86::BP: DestReg = X86::EBP; break;
6064 case X86::SP: DestReg = X86::ESP; break;
6065 }
6066 if (DestReg) {
6067 Res.first = DestReg;
6068 Res.second = Res.second = X86::GR32RegisterClass;
6069 }
Evan Cheng25ab6902006-09-08 06:48:29 +00006070 } else if (VT == MVT::i64) {
6071 unsigned DestReg = 0;
6072 switch (Res.first) {
6073 default: break;
6074 case X86::AX: DestReg = X86::RAX; break;
6075 case X86::DX: DestReg = X86::RDX; break;
6076 case X86::CX: DestReg = X86::RCX; break;
6077 case X86::BX: DestReg = X86::RBX; break;
6078 case X86::SI: DestReg = X86::RSI; break;
6079 case X86::DI: DestReg = X86::RDI; break;
6080 case X86::BP: DestReg = X86::RBP; break;
6081 case X86::SP: DestReg = X86::RSP; break;
6082 }
6083 if (DestReg) {
6084 Res.first = DestReg;
6085 Res.second = Res.second = X86::GR64RegisterClass;
6086 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00006087 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006088
Chris Lattnerf76d1802006-07-31 23:26:50 +00006089 return Res;
6090}