blob: f988048a1063c32035d27c3ee7502e9bdc4a18d0 [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000040#include "llvm/ADT/StringExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041using namespace llvm;
42
Evan Cheng10e86422008-04-25 19:11:04 +000043// Forward declarations.
Dan Gohman475871a2008-07-27 21:46:04 +000044static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng10e86422008-04-25 19:11:04 +000045
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000046X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000047 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000048 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000049 X86ScalarSSEf64 = Subtarget->hasSSE2();
50 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000051 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000052
Chris Lattnerd43d00c2008-01-24 08:07:48 +000053 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000054
Anton Korobeynikov2365f512007-07-14 14:06:15 +000055 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000056 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000057
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000058 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000063 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000065 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000066
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000067 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000068 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000071 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
79
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000081 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
Duncan Sandsf9c98e62008-01-23 20:39:46 +000087 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000088
Chris Lattnerddf89562008-01-17 19:59:44 +000089 // We don't accept any truncstore of integer registers.
90 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
95 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000097 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
98 // operation.
99 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000102
Evan Cheng25ab6902006-09-08 06:48:29 +0000103 if (Subtarget->is64Bit()) {
104 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +0000105 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000106 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000107 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +0000108 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
110 else
111 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
112 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000113
114 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
115 // this operation.
116 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
117 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000118 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000119 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000120 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000121 // f32 and f64 cases are Legal, f80 case is not
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000124 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
125 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
126 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000127
Dale Johannesen73328d12007-09-19 23:55:34 +0000128 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
129 // are Legal, f80 is custom lowered.
130 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000132
Evan Cheng02568ff2006-01-30 22:13:22 +0000133 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
134 // this operation.
135 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
137
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000138 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000139 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000142 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000144 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000145 }
146
147 // Handle FP_TO_UINT by promoting the destination to a larger signed
148 // conversion.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
151 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
152
Evan Cheng25ab6902006-09-08 06:48:29 +0000153 if (Subtarget->is64Bit()) {
154 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000156 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 // Expand FP_TO_UINT into a select.
159 // FIXME: We would like to use a Custom expander here eventually to do
160 // the optimal thing for SSE vs. the default expansion in the legalizer.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
162 else
163 // With SSE3 we can use fisttpll to convert to a signed i64.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
165 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166
Chris Lattner399610a2006-12-05 18:22:22 +0000167 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000168 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000169 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
170 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
171 }
Chris Lattner21f66852005-12-23 05:15:23 +0000172
Dan Gohmanb00ee212008-02-18 19:34:53 +0000173 // Scalar integer divide and remainder are lowered to use operations that
174 // produce two results, to match the available instructions. This exposes
175 // the two-result form to trivial CSE, which is able to combine x/y and x%y
176 // into a single instruction.
177 //
178 // Scalar integer multiply-high is also lowered to use two-result
179 // operations, to match the available instructions. However, plain multiply
180 // (low) operations are left as Legal, as there are single-result
181 // instructions for this in x86. Using the two-result multiply instructions
182 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000183 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
187 setOperationAction(ISD::SREM , MVT::i8 , Expand);
188 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000189 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
193 setOperationAction(ISD::SREM , MVT::i16 , Expand);
194 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000195 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
196 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
197 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
199 setOperationAction(ISD::SREM , MVT::i32 , Expand);
200 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000201 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
202 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
203 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
205 setOperationAction(ISD::SREM , MVT::i64 , Expand);
206 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000207
Evan Chengc35497f2006-10-30 08:02:39 +0000208 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000209 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000210 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
211 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000218 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000220 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000221 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000230 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 if (Subtarget->is64Bit()) {
233 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000236 }
237
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000238 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000239 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000240
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000241 // These should be promoted to a larger select which is supported.
242 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
243 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000244 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000245 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
246 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000249 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000250 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
252 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000255 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
257 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
258 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
259 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000260 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000261 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000262 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000263
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000264 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000265 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000266 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000267 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000268 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000269 if (Subtarget->is64Bit())
270 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000271 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000272 if (Subtarget->is64Bit()) {
273 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
274 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
275 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000276 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000277 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000278 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000279 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
280 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
284 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
286 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000287
Evan Chengd2cde682008-03-10 19:38:10 +0000288 if (Subtarget->hasSSE1())
289 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000290
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000291 if (!Subtarget->hasSSE2())
292 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
293
Mon P Wang63307c32008-05-05 19:05:59 +0000294 // Expand certain atomics
Dale Johannesene00a8a22008-08-28 02:44:49 +0000295 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
296 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000299
Dale Johannesene00a8a22008-08-28 02:44:49 +0000300 setOperationAction(ISD::ATOMIC_LOAD_SUB_8, MVT::i8, Expand);
301 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Expand);
303 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000304
Dan Gohman7f460202008-06-30 20:59:49 +0000305 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
306 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000307 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000308 if (!Subtarget->isTargetDarwin() &&
309 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000310 !Subtarget->isTargetCygMing()) {
311 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
312 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
313 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000314
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000315 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
316 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
317 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
318 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
319 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000320 setExceptionPointerRegister(X86::RAX);
321 setExceptionSelectorRegister(X86::RDX);
322 } else {
323 setExceptionPointerRegister(X86::EAX);
324 setExceptionSelectorRegister(X86::EDX);
325 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000326 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000327 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
328
Duncan Sandsf7331b32007-09-11 14:10:23 +0000329 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000330
Chris Lattnerda68d302008-01-15 21:58:22 +0000331 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000332
Nate Begemanacc398c2006-01-25 18:21:52 +0000333 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
334 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000335 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000336 if (Subtarget->is64Bit()) {
337 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000338 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000339 } else {
340 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000341 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000342 }
Evan Chengae642192007-03-02 23:16:35 +0000343
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000344 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000345 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000346 if (Subtarget->is64Bit())
347 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000348 if (Subtarget->isTargetCygMing())
349 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
350 else
351 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000352
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000353 if (X86ScalarSSEf64) {
354 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000355 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000356 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
357 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000358
Evan Cheng223547a2006-01-31 22:28:30 +0000359 // Use ANDPD to simulate FABS.
360 setOperationAction(ISD::FABS , MVT::f64, Custom);
361 setOperationAction(ISD::FABS , MVT::f32, Custom);
362
363 // Use XORP to simulate FNEG.
364 setOperationAction(ISD::FNEG , MVT::f64, Custom);
365 setOperationAction(ISD::FNEG , MVT::f32, Custom);
366
Evan Cheng68c47cb2007-01-05 07:55:56 +0000367 // Use ANDPD and ORPD to simulate FCOPYSIGN.
368 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
369 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
370
Evan Chengd25e9e82006-02-02 00:28:23 +0000371 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000372 setOperationAction(ISD::FSIN , MVT::f64, Expand);
373 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000374 setOperationAction(ISD::FSIN , MVT::f32, Expand);
375 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000376
Chris Lattnera54aa942006-01-29 06:26:08 +0000377 // Expand FP immediates into loads from the stack, except for the special
378 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000379 addLegalFPImmediate(APFloat(+0.0)); // xorpd
380 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000381
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000382 // Floating truncations from f80 and extensions to f80 go through memory.
383 // If optimizing, we lie about this though and handle it in
384 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
385 if (Fast) {
386 setConvertAction(MVT::f32, MVT::f80, Expand);
387 setConvertAction(MVT::f64, MVT::f80, Expand);
388 setConvertAction(MVT::f80, MVT::f32, Expand);
389 setConvertAction(MVT::f80, MVT::f64, Expand);
390 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000391 } else if (X86ScalarSSEf32) {
392 // Use SSE for f32, x87 for f64.
393 // Set up the FP register classes.
394 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
395 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
396
397 // Use ANDPS to simulate FABS.
398 setOperationAction(ISD::FABS , MVT::f32, Custom);
399
400 // Use XORP to simulate FNEG.
401 setOperationAction(ISD::FNEG , MVT::f32, Custom);
402
403 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
404
405 // Use ANDPS and ORPS to simulate FCOPYSIGN.
406 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
407 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
408
409 // We don't support sin/cos/fmod
410 setOperationAction(ISD::FSIN , MVT::f32, Expand);
411 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000412
Nate Begemane1795842008-02-14 08:57:00 +0000413 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000414 addLegalFPImmediate(APFloat(+0.0f)); // xorps
415 addLegalFPImmediate(APFloat(+0.0)); // FLD0
416 addLegalFPImmediate(APFloat(+1.0)); // FLD1
417 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
418 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
419
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000420 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
421 // this though and handle it in InstructionSelectPreprocess so that
422 // dagcombine2 can hack on these.
423 if (Fast) {
424 setConvertAction(MVT::f32, MVT::f64, Expand);
425 setConvertAction(MVT::f32, MVT::f80, Expand);
426 setConvertAction(MVT::f80, MVT::f32, Expand);
427 setConvertAction(MVT::f64, MVT::f32, Expand);
428 // And x87->x87 truncations also.
429 setConvertAction(MVT::f80, MVT::f64, Expand);
430 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000431
432 if (!UnsafeFPMath) {
433 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
434 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
435 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000436 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000437 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000438 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000439 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
440 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000441
Evan Cheng68c47cb2007-01-05 07:55:56 +0000442 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000443 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000444 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
445 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000446
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000447 // Floating truncations go through memory. If optimizing, we lie about
448 // this though and handle it in InstructionSelectPreprocess so that
449 // dagcombine2 can hack on these.
450 if (Fast) {
451 setConvertAction(MVT::f80, MVT::f32, Expand);
452 setConvertAction(MVT::f64, MVT::f32, Expand);
453 setConvertAction(MVT::f80, MVT::f64, Expand);
454 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000455
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000456 if (!UnsafeFPMath) {
457 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
458 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
459 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000460 addLegalFPImmediate(APFloat(+0.0)); // FLD0
461 addLegalFPImmediate(APFloat(+1.0)); // FLD1
462 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
463 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000464 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
465 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
466 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
467 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000468 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000469
Dale Johannesen59a58732007-08-05 18:49:15 +0000470 // Long double always uses X87.
471 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000472 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
473 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000474 {
Chris Lattner71d07a02008-01-27 06:19:31 +0000475 APFloat TmpFlt(+0.0);
476 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
477 addLegalFPImmediate(TmpFlt); // FLD0
478 TmpFlt.changeSign();
479 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
480 APFloat TmpFlt2(+1.0);
481 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
482 addLegalFPImmediate(TmpFlt2); // FLD1
483 TmpFlt2.changeSign();
484 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
485 }
486
Dale Johannesen2f429012007-09-26 21:10:55 +0000487 if (!UnsafeFPMath) {
488 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
489 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
490 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000491
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000492 // Always use a library call for pow.
493 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
494 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
496
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000497 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000498 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000499 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000500 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000501 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
502
Evan Chengd30bf012006-03-01 01:11:20 +0000503 // First set operation action for all vector types to expand. Then we
504 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000505 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
506 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000507 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000520 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
522 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000523 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000545 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000550 }
551
Evan Chenga88973f2006-03-22 19:22:18 +0000552 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000553 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
554 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
555 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000556 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000557 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000558
Evan Chengd30bf012006-03-01 01:11:20 +0000559 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000560
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000561 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
562 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
563 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000564 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000565
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000566 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
567 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
568 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000569 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000570
Bill Wendling74027e92007-03-15 21:24:36 +0000571 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
572 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
573
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000574 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000575 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000576 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000577 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
578 setOperationAction(ISD::AND, MVT::v2i32, Promote);
579 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
580 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000581
582 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000583 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000584 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000585 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
586 setOperationAction(ISD::OR, MVT::v2i32, Promote);
587 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
588 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000589
590 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000591 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000592 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000593 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
594 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
595 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
596 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000597
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000598 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000599 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000600 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000601 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
602 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
603 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000604 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
605 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000606 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000607
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000608 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
609 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
610 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000611 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000612 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000613
614 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
615 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
616 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000617 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000618
Evan Cheng52672b82008-07-22 18:39:19 +0000619 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000620 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
621 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000622 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000623
624 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000625 }
626
Evan Chenga88973f2006-03-22 19:22:18 +0000627 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000628 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
629
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000630 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
631 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
632 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
633 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000634 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
635 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000636 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
637 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
638 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000639 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000640 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000641 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000642 }
643
Evan Chenga88973f2006-03-22 19:22:18 +0000644 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000645 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
646 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
647 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
648 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
649 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
650
Evan Chengf7c378e2006-04-10 07:23:14 +0000651 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
652 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
653 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000654 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000655 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
656 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
657 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000658 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000659 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000660 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
661 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
662 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
663 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000664 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
665 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000666
Nate Begeman30a0de92008-07-17 16:51:19 +0000667 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
668 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
669 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
670 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000671
Evan Chengf7c378e2006-04-10 07:23:14 +0000672 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
673 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000674 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000675 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000676 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000677
Evan Cheng2c3ae372006-04-12 21:21:57 +0000678 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000679 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
680 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000681 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000682 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000683 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000684 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
685 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
686 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000687 }
688 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
689 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
690 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
691 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000693 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000694 if (Subtarget->is64Bit()) {
695 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000696 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000697 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000698
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000699 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000700 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000701 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
702 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
703 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
704 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
705 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
706 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
707 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
708 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
709 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
710 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000711 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000712
Chris Lattnerddf89562008-01-17 19:59:44 +0000713 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000714
Evan Cheng2c3ae372006-04-12 21:21:57 +0000715 // Custom lower v2i64 and v2f64 selects.
716 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000717 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000718 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000720
Evan Cheng470a6ad2006-02-22 02:26:30 +0000721 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000722
723 if (Subtarget->hasSSE41()) {
724 // FIXME: Do we need to handle scalar-to-vector here?
725 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohman0b924dc2008-05-23 17:49:40 +0000726 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000727
728 // i8 and i16 vectors are custom , because the source register and source
729 // source memory operand types are not the same width. f32 vectors are
730 // custom since the immediate controlling the insert encodes additional
731 // information.
732 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
733 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
734 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
736
737 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
738 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
739 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng62a3f152008-03-24 21:52:23 +0000740 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000741
742 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000743 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
744 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000745 }
746 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000747
Nate Begeman30a0de92008-07-17 16:51:19 +0000748 if (Subtarget->hasSSE42()) {
749 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
750 }
751
Evan Cheng6be2c582006-04-05 23:38:46 +0000752 // We want to custom lower some of our intrinsics.
753 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
754
Evan Cheng206ee9d2006-07-07 08:33:52 +0000755 // We have target-specific dag combine patterns for the following nodes:
756 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000757 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000758 setTargetDAGCombine(ISD::SELECT);
Chris Lattner149a4e52008-02-22 02:09:43 +0000759 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000760
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000761 computeRegisterProperties();
762
Evan Cheng87ed7162006-02-14 08:25:08 +0000763 // FIXME: These should be based on subtarget info. Plus, the values should
764 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000765 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
766 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
767 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000768 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000769 setPrefLoopAlignment(16);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000770}
771
Scott Michel5b8f82e2008-03-10 15:42:14 +0000772
Dan Gohman475871a2008-07-27 21:46:04 +0000773MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000774 return MVT::i8;
775}
776
777
Evan Cheng29286502008-01-23 23:17:41 +0000778/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
779/// the desired ByVal argument alignment.
780static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
781 if (MaxAlign == 16)
782 return;
783 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
784 if (VTy->getBitWidth() == 128)
785 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000786 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
787 unsigned EltAlign = 0;
788 getMaxByValAlign(ATy->getElementType(), EltAlign);
789 if (EltAlign > MaxAlign)
790 MaxAlign = EltAlign;
791 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
792 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
793 unsigned EltAlign = 0;
794 getMaxByValAlign(STy->getElementType(i), EltAlign);
795 if (EltAlign > MaxAlign)
796 MaxAlign = EltAlign;
797 if (MaxAlign == 16)
798 break;
799 }
800 }
801 return;
802}
803
804/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
805/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000806/// that contain SSE vectors are placed at 16-byte boundaries while the rest
807/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000808unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000809 if (Subtarget->is64Bit()) {
810 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000811 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000812 if (TyAlign > 8)
813 return TyAlign;
814 return 8;
815 }
816
Evan Cheng29286502008-01-23 23:17:41 +0000817 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000818 if (Subtarget->hasSSE1())
819 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000820 return Align;
821}
Chris Lattner2b02a442007-02-25 08:29:00 +0000822
Evan Chengf0df0312008-05-15 08:39:06 +0000823/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000824/// and store operations as a result of memset, memcpy, and memmove
825/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000826/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000827MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000828X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
829 bool isSrcConst, bool isSrcStr) const {
830 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
831 return MVT::v4i32;
832 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
833 return MVT::v4f32;
834 if (Subtarget->is64Bit() && Size >= 8)
835 return MVT::i64;
836 return MVT::i32;
837}
838
839
Evan Chengcc415862007-11-09 01:32:10 +0000840/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
841/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000842SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000843 SelectionDAG &DAG) const {
844 if (usesGlobalOffsetTable())
845 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
846 if (!Subtarget->isPICStyleRIPRel())
847 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
848 return Table;
849}
850
Chris Lattner2b02a442007-02-25 08:29:00 +0000851//===----------------------------------------------------------------------===//
852// Return Value Calling Convention Implementation
853//===----------------------------------------------------------------------===//
854
Chris Lattner59ed56b2007-02-28 04:55:35 +0000855#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000856
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000857/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000858SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000859 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
860
Chris Lattner9774c912007-02-27 05:28:59 +0000861 SmallVector<CCValAssign, 16> RVLocs;
862 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000863 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
864 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000865 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000866
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000867 // If this is the first return lowered for this function, add the regs to the
868 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000869 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000870 for (unsigned i = 0; i != RVLocs.size(); ++i)
871 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000872 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000873 }
Dan Gohman475871a2008-07-27 21:46:04 +0000874 SDValue Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000875
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000876 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000877 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000878 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000879 SDValue TailCall = Chain;
880 SDValue TargetAddress = TailCall.getOperand(1);
881 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000882 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000883 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000884 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000885 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000886 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
887 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000888 assert(StackAdjustment.getOpcode() == ISD::Constant &&
889 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000890
Dan Gohman475871a2008-07-27 21:46:04 +0000891 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000892 Operands.push_back(Chain.getOperand(0));
893 Operands.push_back(TargetAddress);
894 Operands.push_back(StackAdjustment);
895 // Copy registers used by the call. Last operand is a flag so it is not
896 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000897 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000898 Operands.push_back(Chain.getOperand(i));
899 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000900 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
901 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000902 }
903
904 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000905 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000906
Dan Gohman475871a2008-07-27 21:46:04 +0000907 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000908 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
909 // Operand #1 = Bytes To Pop
910 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
911
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000912 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000913 for (unsigned i = 0; i != RVLocs.size(); ++i) {
914 CCValAssign &VA = RVLocs[i];
915 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000916 SDValue ValToCopy = Op.getOperand(i*2+1);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000917
Chris Lattner447ff682008-03-11 03:23:40 +0000918 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
919 // the RET instruction and handled by the FP Stackifier.
920 if (RVLocs[i].getLocReg() == X86::ST0 ||
921 RVLocs[i].getLocReg() == X86::ST1) {
922 // If this is a copy from an xmm register to ST(0), use an FPExtend to
923 // change the value to the FP stack register class.
924 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
925 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
926 RetOps.push_back(ValToCopy);
927 // Don't emit a copytoreg.
928 continue;
929 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000930
Chris Lattner8e6da152008-03-10 21:08:41 +0000931 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000932 Flag = Chain.getValue(1);
933 }
Dan Gohman61a92132008-04-21 23:59:07 +0000934
935 // The x86-64 ABI for returning structs by value requires that we copy
936 // the sret argument into %rax for the return. We saved the argument into
937 // a virtual register in the entry block, so now we copy the value out
938 // and into %rax.
939 if (Subtarget->is64Bit() &&
940 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
941 MachineFunction &MF = DAG.getMachineFunction();
942 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
943 unsigned Reg = FuncInfo->getSRetReturnReg();
944 if (!Reg) {
945 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
946 FuncInfo->setSRetReturnReg(Reg);
947 }
Dan Gohman475871a2008-07-27 21:46:04 +0000948 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +0000949
950 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
951 Flag = Chain.getValue(1);
952 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000953
Chris Lattner447ff682008-03-11 03:23:40 +0000954 RetOps[0] = Chain; // Update chain.
955
956 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +0000957 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +0000958 RetOps.push_back(Flag);
959
960 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000961}
962
963
Chris Lattner3085e152007-02-25 08:59:22 +0000964/// LowerCallResult - Lower the result values of an ISD::CALL into the
965/// appropriate copies out of appropriate physical registers. This assumes that
966/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
967/// being lowered. The returns a SDNode with the same number of values as the
968/// ISD::CALL.
969SDNode *X86TargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +0000970LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +0000971 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000972
973 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000974 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +0000975 bool isVarArg = TheCall->isVarArg();
Chris Lattner52387be2007-06-19 00:13:10 +0000976 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000977 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
978
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SmallVector<SDValue, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000980
981 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +0000982 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000983 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattner8e6da152008-03-10 21:08:41 +0000984
985 // If this is a call to a function that returns an fp value on the floating
986 // point stack, but where we prefer to use the value in xmm registers, copy
987 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Mon P Wang7e665102008-08-21 19:54:16 +0000988 if ((RVLocs[i].getLocReg() == X86::ST0 ||
989 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattner8e6da152008-03-10 21:08:41 +0000990 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
991 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +0000992 }
Chris Lattner3085e152007-02-25 08:59:22 +0000993
Chris Lattner8e6da152008-03-10 21:08:41 +0000994 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
995 CopyVT, InFlag).getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +0000996 SDValue Val = Chain.getValue(0);
Chris Lattner8e6da152008-03-10 21:08:41 +0000997 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +0000998
Chris Lattner8e6da152008-03-10 21:08:41 +0000999 if (CopyVT != RVLocs[i].getValVT()) {
1000 // Round the F80 the right size, which also moves to the appropriate xmm
1001 // register.
1002 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1003 // This truncation won't change the value.
1004 DAG.getIntPtrConstant(1));
1005 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +00001006
Chris Lattner8e6da152008-03-10 21:08:41 +00001007 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001008 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001009
Chris Lattner3085e152007-02-25 08:59:22 +00001010 // Merge everything together with a MERGE_VALUES node.
1011 ResultVals.push_back(Chain);
Duncan Sandsf9516202008-06-30 10:19:09 +00001012 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greifba36cb52008-08-28 21:40:38 +00001013 ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001014}
1015
1016
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001017//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001018// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001019//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001020// StdCall calling convention seems to be standard for many Windows' API
1021// routines and around. It differs from C calling convention just a little:
1022// callee should clean up the stack, not caller. Symbols should be also
1023// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001024// For info on fast calling convention see Fast Calling Convention (tail call)
1025// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001026
Evan Cheng85e38002006-04-27 05:35:28 +00001027/// AddLiveIn - This helper function adds the specified physical register to the
1028/// MachineFunction as a live in value. It also creates a corresponding virtual
1029/// register for it.
1030static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001031 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +00001032 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +00001033 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1034 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +00001035 return VReg;
1036}
1037
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001038/// CallIsStructReturn - Determines whether a CALL node uses struct return
1039/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001040static bool CallIsStructReturn(CallSDNode *TheCall) {
1041 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001042 if (!NumOps)
1043 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001044
Dan Gohman095cc292008-09-13 01:54:27 +00001045 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001046}
1047
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001048/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1049/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001050static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001051 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001052 if (!NumArgs)
1053 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001054
1055 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001056}
1057
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001058/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1059/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001060/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001061bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001062 if (IsVarArg)
1063 return false;
1064
Dan Gohman095cc292008-09-13 01:54:27 +00001065 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001066 default:
1067 return false;
1068 case CallingConv::X86_StdCall:
1069 return !Subtarget->is64Bit();
1070 case CallingConv::X86_FastCall:
1071 return !Subtarget->is64Bit();
1072 case CallingConv::Fast:
1073 return PerformTailCallOpt;
1074 }
1075}
1076
Dan Gohman095cc292008-09-13 01:54:27 +00001077/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1078/// given CallingConvention value.
1079CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001080 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001081 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001082 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001083 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1084 return CC_X86_64_TailCall;
1085 else
1086 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001087 }
1088
Gordon Henriksen86737662008-01-05 16:56:59 +00001089 if (CC == CallingConv::X86_FastCall)
1090 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001091 else if (CC == CallingConv::Fast)
1092 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001093 else
1094 return CC_X86_32_C;
1095}
1096
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001097/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1098/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001099NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001100X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001101 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001102 if (CC == CallingConv::X86_FastCall)
1103 return FastCall;
1104 else if (CC == CallingConv::X86_StdCall)
1105 return StdCall;
1106 return None;
1107}
1108
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001109
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001110/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1111/// in a register before calling.
1112bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1113 return !IsTailCall && !Is64Bit &&
1114 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1115 Subtarget->isPICStyleGOT();
1116}
1117
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001118/// CallRequiresFnAddressInReg - Check whether the call requires the function
1119/// address to be loaded in a register.
1120bool
1121X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1122 return !Is64Bit && IsTailCall &&
1123 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1124 Subtarget->isPICStyleGOT();
1125}
1126
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001127/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1128/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001129/// the specific parameter attribute. The copy will be passed as a byval
1130/// function parameter.
Dan Gohman475871a2008-07-27 21:46:04 +00001131static SDValue
1132CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001133 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001134 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohman707e0182008-04-12 04:36:06 +00001135 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001136 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001137}
1138
Dan Gohman475871a2008-07-27 21:46:04 +00001139SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001140 const CCValAssign &VA,
1141 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001142 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001143 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001144 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001145 ISD::ArgFlagsTy Flags =
1146 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001147 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001148 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001149
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001150 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1151 // changed with more analysis.
1152 // In case of tail call optimization mark all arguments mutable. Since they
1153 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001154 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001155 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001156 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001157 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001158 return FIN;
Dan Gohman69de1932008-02-06 22:27:42 +00001159 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001160 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001161}
1162
Dan Gohman475871a2008-07-27 21:46:04 +00001163SDValue
1164X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001165 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001166 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1167
1168 const Function* Fn = MF.getFunction();
1169 if (Fn->hasExternalLinkage() &&
1170 Subtarget->isTargetCygMing() &&
1171 Fn->getName() == "main")
1172 FuncInfo->setForceFramePointer(true);
1173
1174 // Decorate the function name.
1175 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1176
Evan Cheng1bc78042006-04-26 01:20:17 +00001177 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001178 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001179 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001180 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001181 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001182 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001183
1184 assert(!(isVarArg && CC == CallingConv::Fast) &&
1185 "Var args not supported with calling convention fastcc");
1186
Chris Lattner638402b2007-02-28 07:00:42 +00001187 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001188 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001189 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001190 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001191
Dan Gohman475871a2008-07-27 21:46:04 +00001192 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001193 unsigned LastVal = ~0U;
1194 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1195 CCValAssign &VA = ArgLocs[i];
1196 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1197 // places.
1198 assert(VA.getValNo() != LastVal &&
1199 "Don't support value assigned to multiple locs yet");
1200 LastVal = VA.getValNo();
1201
1202 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001203 MVT RegVT = VA.getLocVT();
Chris Lattnerf39f7712007-02-28 05:46:49 +00001204 TargetRegisterClass *RC;
1205 if (RegVT == MVT::i32)
1206 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001207 else if (Is64Bit && RegVT == MVT::i64)
1208 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001209 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001210 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001211 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001212 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001213 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001214 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001215 else if (RegVT.isVector()) {
1216 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001217 if (!Is64Bit)
1218 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1219 else {
1220 // Darwin calling convention passes MMX values in either GPRs or
1221 // XMMs in x86-64. Other targets pass them in memory.
1222 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1223 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1224 RegVT = MVT::v2i64;
1225 } else {
1226 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1227 RegVT = MVT::i64;
1228 }
1229 }
1230 } else {
1231 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001232 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001233
Chris Lattner82932a52007-03-02 05:12:29 +00001234 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman475871a2008-07-27 21:46:04 +00001235 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001236
1237 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1238 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1239 // right size.
1240 if (VA.getLocInfo() == CCValAssign::SExt)
1241 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1242 DAG.getValueType(VA.getValVT()));
1243 else if (VA.getLocInfo() == CCValAssign::ZExt)
1244 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1245 DAG.getValueType(VA.getValVT()));
1246
1247 if (VA.getLocInfo() != CCValAssign::Full)
1248 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1249
Gordon Henriksen86737662008-01-05 16:56:59 +00001250 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001251 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001252 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Cheng44c0fd12008-04-25 20:13:28 +00001253 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1254 else if (RC == X86::VR128RegisterClass) {
1255 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1256 DAG.getConstant(0, MVT::i64));
1257 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1258 }
1259 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001260
Chris Lattnerf39f7712007-02-28 05:46:49 +00001261 ArgValues.push_back(ArgValue);
1262 } else {
1263 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001264 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001265 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001266 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001267
Dan Gohman61a92132008-04-21 23:59:07 +00001268 // The x86-64 ABI for returning structs by value requires that we copy
1269 // the sret argument into %rax for the return. Save the argument into
1270 // a virtual register so that we can access it from the return points.
1271 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1272 MachineFunction &MF = DAG.getMachineFunction();
1273 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1274 unsigned Reg = FuncInfo->getSRetReturnReg();
1275 if (!Reg) {
1276 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1277 FuncInfo->setSRetReturnReg(Reg);
1278 }
Dan Gohman475871a2008-07-27 21:46:04 +00001279 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohman61a92132008-04-21 23:59:07 +00001280 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1281 }
1282
Chris Lattnerf39f7712007-02-28 05:46:49 +00001283 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001284 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001285 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001286 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001287
Evan Cheng1bc78042006-04-26 01:20:17 +00001288 // If the function takes variable number of arguments, make a frame index for
1289 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001290 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001291 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1292 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1293 }
1294 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001295 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1296
1297 // FIXME: We should really autogenerate these arrays
1298 static const unsigned GPR64ArgRegsWin64[] = {
1299 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001300 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001301 static const unsigned XMMArgRegsWin64[] = {
1302 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1303 };
1304 static const unsigned GPR64ArgRegs64Bit[] = {
1305 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1306 };
1307 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001308 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1309 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1310 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001311 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1312
1313 if (IsWin64) {
1314 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1315 GPR64ArgRegs = GPR64ArgRegsWin64;
1316 XMMArgRegs = XMMArgRegsWin64;
1317 } else {
1318 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1319 GPR64ArgRegs = GPR64ArgRegs64Bit;
1320 XMMArgRegs = XMMArgRegs64Bit;
1321 }
1322 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1323 TotalNumIntRegs);
1324 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1325 TotalNumXMMRegs);
1326
Gordon Henriksen86737662008-01-05 16:56:59 +00001327 // For X86-64, if there are vararg parameters that are passed via
1328 // registers, then we must store them to their spots on the stack so they
1329 // may be loaded by deferencing the result of va_next.
1330 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001331 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1332 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1333 TotalNumXMMRegs * 16, 16);
1334
Gordon Henriksen86737662008-01-05 16:56:59 +00001335 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001336 SmallVector<SDValue, 8> MemOps;
1337 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1338 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001339 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001340 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001341 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1342 X86::GR64RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001343 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1344 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001345 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001346 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001347 MemOps.push_back(Store);
1348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001350 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001351
Gordon Henriksen86737662008-01-05 16:56:59 +00001352 // Now store the XMM (fp + vector) parameter registers.
1353 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001354 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001355 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001356 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1357 X86::VR128RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001358 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1359 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001360 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001361 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001362 MemOps.push_back(Store);
1363 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001364 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001365 }
1366 if (!MemOps.empty())
1367 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1368 &MemOps[0], MemOps.size());
1369 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001370 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001371
Gordon Henriksenae636f82008-01-03 16:47:34 +00001372 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001373
Gordon Henriksen86737662008-01-05 16:56:59 +00001374 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001375 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001376 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001377 BytesCallerReserves = 0;
1378 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001379 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001380 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001381 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001382 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001383 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001384 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001385
Gordon Henriksen86737662008-01-05 16:56:59 +00001386 if (!Is64Bit) {
1387 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1388 if (CC == CallingConv::X86_FastCall)
1389 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1390 }
Evan Cheng25caf632006-05-23 21:06:34 +00001391
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001392 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001393
Evan Cheng25caf632006-05-23 21:06:34 +00001394 // Return the new list of results.
Gabor Greifba36cb52008-08-28 21:40:38 +00001395 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif99a6cb92008-08-26 22:36:50 +00001396 ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001397}
1398
Dan Gohman475871a2008-07-27 21:46:04 +00001399SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001400X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001401 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001402 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001403 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001404 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman4fdad172008-02-07 16:28:05 +00001405 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001406 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001407 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001408 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001409 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001410 }
Dan Gohman4fdad172008-02-07 16:28:05 +00001411 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001412 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001413}
1414
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001415/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1416/// optimization is performed and it is required.
Dan Gohman475871a2008-07-27 21:46:04 +00001417SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001418X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001419 SDValue &OutRetAddr,
1420 SDValue Chain,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001421 bool IsTailCall,
1422 bool Is64Bit,
1423 int FPDiff) {
1424 if (!IsTailCall || FPDiff==0) return Chain;
1425
1426 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001427 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001428 OutRetAddr = getReturnAddressFrameIndex(DAG);
1429 // Load the "old" Return address.
1430 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001431 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001432}
1433
1434/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1435/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman475871a2008-07-27 21:46:04 +00001436static SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001437EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001438 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001439 bool Is64Bit, int FPDiff) {
1440 // Store the return address to the appropriate stack slot.
1441 if (!FPDiff) return Chain;
1442 // Calculate the new stack slot for the return address.
1443 int SlotSize = Is64Bit ? 8 : 4;
1444 int NewReturnAddrFI =
1445 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001446 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001447 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001448 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001449 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001450 return Chain;
1451}
1452
Dan Gohman475871a2008-07-27 21:46:04 +00001453SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001454 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001455 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1456 SDValue Chain = TheCall->getChain();
1457 unsigned CC = TheCall->getCallingConv();
1458 bool isVarArg = TheCall->isVarArg();
1459 bool IsTailCall = TheCall->isTailCall() &&
1460 CC == CallingConv::Fast && PerformTailCallOpt;
1461 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001462 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001463 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001464
1465 assert(!(isVarArg && CC == CallingConv::Fast) &&
1466 "Var args not supported with calling convention fastcc");
1467
Chris Lattner638402b2007-02-28 07:00:42 +00001468 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001469 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001470 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001471 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001472
Chris Lattner423c5f42007-02-28 05:31:48 +00001473 // Get a count of how many bytes are to be pushed on the stack.
1474 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001475 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001476 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001477
Gordon Henriksen86737662008-01-05 16:56:59 +00001478 int FPDiff = 0;
1479 if (IsTailCall) {
1480 // Lower arguments at fp - stackoffset + fpdiff.
1481 unsigned NumBytesCallerPushed =
1482 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1483 FPDiff = NumBytesCallerPushed - NumBytes;
1484
1485 // Set the delta of movement of the returnaddr stackslot.
1486 // But only set if delta is greater than previous delta.
1487 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1488 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1489 }
1490
Chris Lattner0bd48932008-01-17 07:00:52 +00001491 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001492
Dan Gohman475871a2008-07-27 21:46:04 +00001493 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001494 // Load return adress for tail calls.
1495 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1496 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001497
Dan Gohman475871a2008-07-27 21:46:04 +00001498 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1499 SmallVector<SDValue, 8> MemOpChains;
1500 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001501
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001502 // Walk the register/memloc assignments, inserting copies/loads. In the case
1503 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001504 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1505 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001506 SDValue Arg = TheCall->getArg(i);
1507 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1508 bool isByVal = Flags.isByVal();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001509
Chris Lattner423c5f42007-02-28 05:31:48 +00001510 // Promote the value if needed.
1511 switch (VA.getLocInfo()) {
1512 default: assert(0 && "Unknown loc info!");
1513 case CCValAssign::Full: break;
1514 case CCValAssign::SExt:
1515 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1516 break;
1517 case CCValAssign::ZExt:
1518 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1519 break;
1520 case CCValAssign::AExt:
1521 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1522 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001523 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001524
1525 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001526 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001527 MVT RegVT = VA.getLocVT();
1528 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001529 switch (VA.getLocReg()) {
1530 default:
1531 break;
1532 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1533 case X86::R8: {
1534 // Special case: passing MMX values in GPR registers.
1535 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1536 break;
1537 }
1538 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1539 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1540 // Special case: passing MMX values in XMM registers.
1541 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1542 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1543 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1544 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1545 getMOVLMask(2, DAG));
1546 break;
1547 }
1548 }
1549 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001550 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1551 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001552 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001553 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001554 if (StackPtr.getNode() == 0)
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001555 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1556
Dan Gohman095cc292008-09-13 01:54:27 +00001557 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1558 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001559 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001560 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001561 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001562
Evan Cheng32fe1032006-05-25 00:59:30 +00001563 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001564 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1565 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001566
Evan Cheng347d5f72006-04-28 21:29:37 +00001567 // Build a sequence of copy-to-reg nodes chained together with token chain
1568 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001569 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001570 // Tail call byval lowering might overwrite argument registers so in case of
1571 // tail call optimization the copies to registers are lowered later.
1572 if (!IsTailCall)
1573 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1574 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1575 InFlag);
1576 InFlag = Chain.getValue(1);
1577 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001578
Evan Chengf4684712007-02-21 21:18:14 +00001579 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001580 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001581 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1582 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1583 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1584 InFlag);
1585 InFlag = Chain.getValue(1);
1586 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001587 // If we are tail calling and generating PIC/GOT style code load the address
1588 // of the callee into ecx. The value in ecx is used as target of the tail
1589 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1590 // calls on PIC/GOT architectures. Normally we would just put the address of
1591 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1592 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001593 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001594 // Note: The actual moving to ecx is done further down.
1595 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1596 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1597 !G->getGlobal()->hasProtectedVisibility())
1598 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001599 else if (isa<ExternalSymbolSDNode>(Callee))
1600 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001601 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001602
Gordon Henriksen86737662008-01-05 16:56:59 +00001603 if (Is64Bit && isVarArg) {
1604 // From AMD64 ABI document:
1605 // For calls that may call functions that use varargs or stdargs
1606 // (prototype-less calls or calls to functions containing ellipsis (...) in
1607 // the declaration) %al is used as hidden argument to specify the number
1608 // of SSE registers used. The contents of %al do not need to match exactly
1609 // the number of registers, but must be an ubound on the number of SSE
1610 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001611
1612 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001613 // Count the number of XMM registers allocated.
1614 static const unsigned XMMArgRegs[] = {
1615 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1616 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1617 };
1618 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1619
1620 Chain = DAG.getCopyToReg(Chain, X86::AL,
1621 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1622 InFlag = Chain.getValue(1);
1623 }
1624
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001625
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001626 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001627 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001628 SmallVector<SDValue, 8> MemOpChains2;
1629 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001630 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001631 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001632 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001633 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1634 CCValAssign &VA = ArgLocs[i];
1635 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001636 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001637 SDValue Arg = TheCall->getArg(i);
1638 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001639 // Create frame index.
1640 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001641 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001642 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001643 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001644
Duncan Sands276dcbd2008-03-21 09:14:45 +00001645 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001646 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001647 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001648 if (StackPtr.getNode() == 0)
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001649 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1650 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1651
1652 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng8e5712b2008-01-12 01:08:07 +00001653 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001654 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001655 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001656 MemOpChains2.push_back(
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001657 DAG.getStore(Chain, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001658 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001659 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001660 }
1661 }
1662
1663 if (!MemOpChains2.empty())
1664 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001665 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001666
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001667 // Copy arguments to their registers.
1668 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1669 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1670 InFlag);
1671 InFlag = Chain.getValue(1);
1672 }
Dan Gohman475871a2008-07-27 21:46:04 +00001673 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001674
Gordon Henriksen86737662008-01-05 16:56:59 +00001675 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001676 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1677 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001678 }
1679
Evan Cheng32fe1032006-05-25 00:59:30 +00001680 // If the callee is a GlobalAddress node (quite common, every direct call is)
1681 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001682 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001683 // We should use extra load for direct calls to dllimported functions in
1684 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001685 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1686 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001687 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Bill Wendling056292f2008-09-16 21:48:12 +00001688 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1689 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001690 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001691 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001692
1693 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001694 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001695 Callee,InFlag);
1696 Callee = DAG.getRegister(Opc, getPointerTy());
1697 // Add register as live out.
1698 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001699 }
1700
Chris Lattnerd96d0722007-02-25 06:40:16 +00001701 // Returns a chain & a flag for retval copy to use.
1702 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001703 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001704
1705 if (IsTailCall) {
1706 Ops.push_back(Chain);
Chris Lattner0bd48932008-01-17 07:00:52 +00001707 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1708 Ops.push_back(DAG.getIntPtrConstant(0));
Gabor Greifba36cb52008-08-28 21:40:38 +00001709 if (InFlag.getNode())
Gordon Henriksen86737662008-01-05 16:56:59 +00001710 Ops.push_back(InFlag);
1711 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1712 InFlag = Chain.getValue(1);
1713
1714 // Returns a chain & a flag for retval copy to use.
1715 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1716 Ops.clear();
1717 }
1718
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001719 Ops.push_back(Chain);
1720 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001721
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 if (IsTailCall)
1723 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001724
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 // Add argument registers to the end of the list so that they are known live
1726 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001727 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1728 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1729 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001730
Evan Cheng586ccac2008-03-18 23:36:35 +00001731 // Add an implicit use GOT pointer in EBX.
1732 if (!IsTailCall && !Is64Bit &&
1733 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1734 Subtarget->isPICStyleGOT())
1735 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1736
1737 // Add an implicit use of AL for x86 vararg functions.
1738 if (Is64Bit && isVarArg)
1739 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1740
Gabor Greifba36cb52008-08-28 21:40:38 +00001741 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001742 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001743
Gordon Henriksen86737662008-01-05 16:56:59 +00001744 if (IsTailCall) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001745 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001746 "Flag must be set. Depend on flag being set in LowerRET");
1747 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman095cc292008-09-13 01:54:27 +00001748 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001749
Gabor Greifba36cb52008-08-28 21:40:38 +00001750 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001751 }
1752
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001753 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001754 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001755
Chris Lattner2d297092006-05-23 18:50:38 +00001756 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001758 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001759 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001760 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001761 // If this is is a call to a struct-return function, the callee
1762 // pops the hidden struct pointer, so we have to push it back.
1763 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001764 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001765 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001766 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001767
Gordon Henriksenae636f82008-01-03 16:47:34 +00001768 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001769 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner0bd48932008-01-17 07:00:52 +00001770 DAG.getIntPtrConstant(NumBytes),
1771 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001772 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001773 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001774
Chris Lattner3085e152007-02-25 08:59:22 +00001775 // Handle result values, copying them out of physregs into vregs that we
1776 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001777 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001778 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001779}
1780
Evan Cheng25ab6902006-09-08 06:48:29 +00001781
1782//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001783// Fast Calling Convention (tail call) implementation
1784//===----------------------------------------------------------------------===//
1785
1786// Like std call, callee cleans arguments, convention except that ECX is
1787// reserved for storing the tail called function address. Only 2 registers are
1788// free for argument passing (inreg). Tail call optimization is performed
1789// provided:
1790// * tailcallopt is enabled
1791// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001792// On X86_64 architecture with GOT-style position independent code only local
1793// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001794// To keep the stack aligned according to platform abi the function
1795// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1796// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001797// If a tail called function callee has more arguments than the caller the
1798// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001799// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001800// original REtADDR, but before the saved framepointer or the spilled registers
1801// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1802// stack layout:
1803// arg1
1804// arg2
1805// RETADDR
1806// [ new RETADDR
1807// move area ]
1808// (possible EBP)
1809// ESI
1810// EDI
1811// local1 ..
1812
1813/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1814/// for a 16 byte align requirement.
1815unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1816 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001817 MachineFunction &MF = DAG.getMachineFunction();
1818 const TargetMachine &TM = MF.getTarget();
1819 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1820 unsigned StackAlignment = TFI.getStackAlignment();
1821 uint64_t AlignMask = StackAlignment - 1;
1822 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001823 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001824 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1825 // Number smaller than 12 so just add the difference.
1826 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1827 } else {
1828 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1829 Offset = ((~AlignMask) & Offset) + StackAlignment +
1830 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001831 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001832 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001833}
1834
1835/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001836/// following the call is a return. A function is eligible if caller/callee
1837/// calling conventions match, currently only fastcc supports tail calls, and
1838/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001839bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001840 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001841 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001842 if (!PerformTailCallOpt)
1843 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001844
Dan Gohman095cc292008-09-13 01:54:27 +00001845 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001846 MachineFunction &MF = DAG.getMachineFunction();
1847 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001848 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001849 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001850 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001851 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001852 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001853 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001854 return true;
1855
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001856 // Can only do local tail calls (in same module, hidden or protected) on
1857 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001858 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1859 return G->getGlobal()->hasHiddenVisibility()
1860 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001861 }
1862 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001863
1864 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001865}
1866
Dan Gohman3df24e62008-09-03 23:12:08 +00001867FastISel *
1868X86TargetLowering::createFastISel(MachineFunction &mf,
1869 DenseMap<const Value *, unsigned> &vm,
1870 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001871 MachineBasicBlock *> &bm,
1872 DenseMap<const AllocaInst *, int> &am) {
1873
1874 return X86::createFastISel(mf, vm, bm, am);
Dan Gohmand9f3c482008-08-19 21:32:53 +00001875}
1876
1877
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001878//===----------------------------------------------------------------------===//
1879// Other Lowering Hooks
1880//===----------------------------------------------------------------------===//
1881
1882
Dan Gohman475871a2008-07-27 21:46:04 +00001883SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001884 MachineFunction &MF = DAG.getMachineFunction();
1885 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1886 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001887 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001888
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001889 if (ReturnAddrIndex == 0) {
1890 // Set up a frame object for the return address.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001891 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001892 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001893 }
1894
Evan Cheng25ab6902006-09-08 06:48:29 +00001895 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001896}
1897
1898
Evan Cheng6dfa9992006-01-30 23:41:35 +00001899/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1900/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001901/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1902/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001903static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman475871a2008-07-27 21:46:04 +00001904 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Chris Lattnerf9570512006-09-13 03:22:10 +00001905 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001906 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001907 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001908 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1909 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1910 // X > -1 -> X == 0, jump !sign.
1911 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001912 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001913 return true;
1914 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1915 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001916 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001917 return true;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001918 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00001919 // X < 1 -> X <= 0
1920 RHS = DAG.getConstant(0, RHS.getValueType());
1921 X86CC = X86::COND_LE;
1922 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001923 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001924 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001925
Evan Chengd9558e02006-01-06 00:43:03 +00001926 switch (SetCCOpcode) {
1927 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001928 case ISD::SETEQ: X86CC = X86::COND_E; break;
1929 case ISD::SETGT: X86CC = X86::COND_G; break;
1930 case ISD::SETGE: X86CC = X86::COND_GE; break;
1931 case ISD::SETLT: X86CC = X86::COND_L; break;
1932 case ISD::SETLE: X86CC = X86::COND_LE; break;
1933 case ISD::SETNE: X86CC = X86::COND_NE; break;
1934 case ISD::SETULT: X86CC = X86::COND_B; break;
1935 case ISD::SETUGT: X86CC = X86::COND_A; break;
1936 case ISD::SETULE: X86CC = X86::COND_BE; break;
1937 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001938 }
1939 } else {
Evan Cheng45670442008-08-29 23:22:12 +00001940 // First determine if it requires or is profitable to flip the operands.
1941 bool Flip = false;
1942 switch (SetCCOpcode) {
1943 default: break;
1944 case ISD::SETOLT:
1945 case ISD::SETOLE:
1946 case ISD::SETUGT:
1947 case ISD::SETUGE:
1948 Flip = true;
1949 break;
1950 }
1951
1952 // If LHS is a foldable load, but RHS is not, flip the condition.
1953 if (!Flip &&
1954 (ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1955 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1956 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1957 Flip = true;
1958 }
1959 if (Flip)
1960 std::swap(LHS, RHS);
1961
Evan Chengd9558e02006-01-06 00:43:03 +00001962 // On a floating point condition, the flags are set as follows:
1963 // ZF PF CF op
1964 // 0 | 0 | 0 | X > Y
1965 // 0 | 0 | 1 | X < Y
1966 // 1 | 0 | 0 | X == Y
1967 // 1 | 1 | 1 | unordered
1968 switch (SetCCOpcode) {
1969 default: break;
1970 case ISD::SETUEQ:
Evan Cheng45670442008-08-29 23:22:12 +00001971 case ISD::SETEQ:
1972 X86CC = X86::COND_E;
1973 break;
1974 case ISD::SETOLT: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00001975 case ISD::SETOGT:
Evan Cheng45670442008-08-29 23:22:12 +00001976 case ISD::SETGT:
1977 X86CC = X86::COND_A;
1978 break;
1979 case ISD::SETOLE: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00001980 case ISD::SETOGE:
Evan Cheng45670442008-08-29 23:22:12 +00001981 case ISD::SETGE:
1982 X86CC = X86::COND_AE;
1983 break;
1984 case ISD::SETUGT: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00001985 case ISD::SETULT:
Evan Cheng45670442008-08-29 23:22:12 +00001986 case ISD::SETLT:
1987 X86CC = X86::COND_B;
1988 break;
1989 case ISD::SETUGE: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00001990 case ISD::SETULE:
Evan Cheng45670442008-08-29 23:22:12 +00001991 case ISD::SETLE:
1992 X86CC = X86::COND_BE;
1993 break;
Evan Chengd9558e02006-01-06 00:43:03 +00001994 case ISD::SETONE:
Evan Cheng45670442008-08-29 23:22:12 +00001995 case ISD::SETNE:
1996 X86CC = X86::COND_NE;
1997 break;
1998 case ISD::SETUO:
1999 X86CC = X86::COND_P;
2000 break;
2001 case ISD::SETO:
2002 X86CC = X86::COND_NP;
2003 break;
Evan Chengd9558e02006-01-06 00:43:03 +00002004 }
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002005 }
2006
Evan Cheng94a50da2008-08-29 22:13:21 +00002007 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002008}
2009
Evan Cheng4a460802006-01-11 00:33:36 +00002010/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2011/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002012/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002013static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002014 switch (X86CC) {
2015 default:
2016 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002017 case X86::COND_B:
2018 case X86::COND_BE:
2019 case X86::COND_E:
2020 case X86::COND_P:
2021 case X86::COND_A:
2022 case X86::COND_AE:
2023 case X86::COND_NE:
2024 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002025 return true;
2026 }
2027}
2028
Evan Cheng5ced1d82006-04-06 23:23:56 +00002029/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002030/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman475871a2008-07-27 21:46:04 +00002031static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002032 if (Op.getOpcode() == ISD::UNDEF)
2033 return true;
2034
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002035 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002036 return (Val >= Low && Val < Hi);
2037}
2038
2039/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2040/// true if Op is undef or if its value equal to the specified value.
Dan Gohman475871a2008-07-27 21:46:04 +00002041static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Evan Chengc5cdff22006-04-07 21:53:05 +00002042 if (Op.getOpcode() == ISD::UNDEF)
2043 return true;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002044 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002045}
2046
Evan Cheng0188ecb2006-03-22 18:59:22 +00002047/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2048/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2049bool X86::isPSHUFDMask(SDNode *N) {
2050 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2051
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002052 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002053 return false;
2054
2055 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002056 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002057 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002058 if (Arg.getOpcode() == ISD::UNDEF) continue;
2059 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002060 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002061 return false;
2062 }
2063
2064 return true;
2065}
2066
2067/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002068/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002069bool X86::isPSHUFHWMask(SDNode *N) {
2070 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2071
2072 if (N->getNumOperands() != 8)
2073 return false;
2074
2075 // Lower quadword copied in order.
2076 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002077 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002078 if (Arg.getOpcode() == ISD::UNDEF) continue;
2079 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002080 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002081 return false;
2082 }
2083
2084 // Upper quadword shuffled.
2085 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002086 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002087 if (Arg.getOpcode() == ISD::UNDEF) continue;
2088 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002089 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002090 if (Val < 4 || Val > 7)
2091 return false;
2092 }
2093
2094 return true;
2095}
2096
2097/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002098/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002099bool X86::isPSHUFLWMask(SDNode *N) {
2100 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2101
2102 if (N->getNumOperands() != 8)
2103 return false;
2104
2105 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002106 for (unsigned i = 4; i != 8; ++i)
2107 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002108 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002109
2110 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002111 for (unsigned i = 0; i != 4; ++i)
2112 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002113 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002114
2115 return true;
2116}
2117
Evan Cheng14aed5e2006-03-24 01:18:28 +00002118/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2119/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002120static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002121 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002122
Evan Cheng39623da2006-04-20 08:58:49 +00002123 unsigned Half = NumElems / 2;
2124 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002125 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002126 return false;
2127 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002128 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002129 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002130
2131 return true;
2132}
2133
Evan Cheng39623da2006-04-20 08:58:49 +00002134bool X86::isSHUFPMask(SDNode *N) {
2135 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002136 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002137}
2138
Evan Cheng213d2cf2007-05-17 18:45:50 +00002139/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002140/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2141/// half elements to come from vector 1 (which would equal the dest.) and
2142/// the upper half to come from vector 2.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002143static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002144 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002145
Chris Lattner5a88b832007-02-25 07:10:00 +00002146 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002147 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002148 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002149 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002150 for (unsigned i = Half; i < NumOps; ++i)
2151 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002152 return false;
2153 return true;
2154}
2155
2156static bool isCommutedSHUFP(SDNode *N) {
2157 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002158 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002159}
2160
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002161/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2162/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2163bool X86::isMOVHLPSMask(SDNode *N) {
2164 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2165
Evan Cheng2064a2b2006-03-28 06:50:32 +00002166 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002167 return false;
2168
Evan Cheng2064a2b2006-03-28 06:50:32 +00002169 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002170 return isUndefOrEqual(N->getOperand(0), 6) &&
2171 isUndefOrEqual(N->getOperand(1), 7) &&
2172 isUndefOrEqual(N->getOperand(2), 2) &&
2173 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002174}
2175
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002176/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2177/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2178/// <2, 3, 2, 3>
2179bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2180 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2181
2182 if (N->getNumOperands() != 4)
2183 return false;
2184
2185 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2186 return isUndefOrEqual(N->getOperand(0), 2) &&
2187 isUndefOrEqual(N->getOperand(1), 3) &&
2188 isUndefOrEqual(N->getOperand(2), 2) &&
2189 isUndefOrEqual(N->getOperand(3), 3);
2190}
2191
Evan Cheng5ced1d82006-04-06 23:23:56 +00002192/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2193/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2194bool X86::isMOVLPMask(SDNode *N) {
2195 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2196
2197 unsigned NumElems = N->getNumOperands();
2198 if (NumElems != 2 && NumElems != 4)
2199 return false;
2200
Evan Chengc5cdff22006-04-07 21:53:05 +00002201 for (unsigned i = 0; i < NumElems/2; ++i)
2202 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2203 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002204
Evan Chengc5cdff22006-04-07 21:53:05 +00002205 for (unsigned i = NumElems/2; i < NumElems; ++i)
2206 if (!isUndefOrEqual(N->getOperand(i), i))
2207 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002208
2209 return true;
2210}
2211
2212/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002213/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2214/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002215bool X86::isMOVHPMask(SDNode *N) {
2216 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2217
2218 unsigned NumElems = N->getNumOperands();
2219 if (NumElems != 2 && NumElems != 4)
2220 return false;
2221
Evan Chengc5cdff22006-04-07 21:53:05 +00002222 for (unsigned i = 0; i < NumElems/2; ++i)
2223 if (!isUndefOrEqual(N->getOperand(i), i))
2224 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002225
2226 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002227 SDValue Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002228 if (!isUndefOrEqual(Arg, i + NumElems))
2229 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002230 }
2231
2232 return true;
2233}
2234
Evan Cheng0038e592006-03-28 00:39:58 +00002235/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2236/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002237bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002238 bool V2IsSplat = false) {
2239 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002240 return false;
2241
Chris Lattner5a88b832007-02-25 07:10:00 +00002242 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002243 SDValue BitI = Elts[i];
2244 SDValue BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002245 if (!isUndefOrEqual(BitI, j))
2246 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002247 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002248 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002249 return false;
2250 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002251 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002252 return false;
2253 }
Evan Cheng0038e592006-03-28 00:39:58 +00002254 }
2255
2256 return true;
2257}
2258
Evan Cheng39623da2006-04-20 08:58:49 +00002259bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2260 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002261 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002262}
2263
Evan Cheng4fcb9222006-03-28 02:43:26 +00002264/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2265/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002266bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002267 bool V2IsSplat = false) {
2268 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002269 return false;
2270
Chris Lattner5a88b832007-02-25 07:10:00 +00002271 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002272 SDValue BitI = Elts[i];
2273 SDValue BitI1 = Elts[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002274 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002275 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002276 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002277 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002278 return false;
2279 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002280 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002281 return false;
2282 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002283 }
2284
2285 return true;
2286}
2287
Evan Cheng39623da2006-04-20 08:58:49 +00002288bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2289 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002290 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002291}
2292
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002293/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2294/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2295/// <0, 0, 1, 1>
2296bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2297 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2298
2299 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002300 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002301 return false;
2302
2303 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002304 SDValue BitI = N->getOperand(i);
2305 SDValue BitI1 = N->getOperand(i+1);
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002306
Evan Chengc5cdff22006-04-07 21:53:05 +00002307 if (!isUndefOrEqual(BitI, j))
2308 return false;
2309 if (!isUndefOrEqual(BitI1, j))
2310 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002311 }
2312
2313 return true;
2314}
2315
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002316/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2317/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2318/// <2, 2, 3, 3>
2319bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2320 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2321
2322 unsigned NumElems = N->getNumOperands();
2323 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2324 return false;
2325
2326 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002327 SDValue BitI = N->getOperand(i);
2328 SDValue BitI1 = N->getOperand(i + 1);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002329
2330 if (!isUndefOrEqual(BitI, j))
2331 return false;
2332 if (!isUndefOrEqual(BitI1, j))
2333 return false;
2334 }
2335
2336 return true;
2337}
2338
Evan Cheng017dcc62006-04-21 01:05:10 +00002339/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2340/// specifies a shuffle of elements that is suitable for input to MOVSS,
2341/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002342static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002343 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002344 return false;
2345
Chris Lattner5a88b832007-02-25 07:10:00 +00002346 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002347 return false;
2348
Chris Lattner5a88b832007-02-25 07:10:00 +00002349 for (unsigned i = 1; i < NumElts; ++i) {
2350 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002351 return false;
2352 }
2353
2354 return true;
2355}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002356
Evan Cheng017dcc62006-04-21 01:05:10 +00002357bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002358 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002359 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002360}
2361
Evan Cheng017dcc62006-04-21 01:05:10 +00002362/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2363/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002364/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002365static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002366 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002367 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002368 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002369 return false;
2370
2371 if (!isUndefOrEqual(Ops[0], 0))
2372 return false;
2373
Chris Lattner5a88b832007-02-25 07:10:00 +00002374 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002375 SDValue Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002376 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2377 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2378 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002379 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002380 }
2381
2382 return true;
2383}
2384
Evan Cheng8cf723d2006-09-08 01:50:06 +00002385static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2386 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002387 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002388 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2389 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002390}
2391
Evan Chengd9539472006-04-14 21:59:03 +00002392/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2393/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2394bool X86::isMOVSHDUPMask(SDNode *N) {
2395 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2396
2397 if (N->getNumOperands() != 4)
2398 return false;
2399
2400 // Expect 1, 1, 3, 3
2401 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002402 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002403 if (Arg.getOpcode() == ISD::UNDEF) continue;
2404 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002405 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002406 if (Val != 1) return false;
2407 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002408
2409 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002410 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002411 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002412 if (Arg.getOpcode() == ISD::UNDEF) continue;
2413 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002414 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002415 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002416 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002417 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002418
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002419 // Don't use movshdup if it can be done with a shufps.
2420 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002421}
2422
2423/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2424/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2425bool X86::isMOVSLDUPMask(SDNode *N) {
2426 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2427
2428 if (N->getNumOperands() != 4)
2429 return false;
2430
2431 // Expect 0, 0, 2, 2
2432 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002433 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002434 if (Arg.getOpcode() == ISD::UNDEF) continue;
2435 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002436 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002437 if (Val != 0) return false;
2438 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002439
2440 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002441 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002442 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002443 if (Arg.getOpcode() == ISD::UNDEF) continue;
2444 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002445 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002446 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002447 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002448 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002449
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002450 // Don't use movshdup if it can be done with a shufps.
2451 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002452}
2453
Evan Cheng49892af2007-06-19 00:02:56 +00002454/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2455/// specifies a identity operation on the LHS or RHS.
2456static bool isIdentityMask(SDNode *N, bool RHS = false) {
2457 unsigned NumElems = N->getNumOperands();
2458 for (unsigned i = 0; i < NumElems; ++i)
2459 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2460 return false;
2461 return true;
2462}
2463
Evan Chengb9df0ca2006-03-22 02:53:00 +00002464/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2465/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002466static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002467 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2468
Evan Chengb9df0ca2006-03-22 02:53:00 +00002469 // This is a splat operation if each element of the permute is the same, and
2470 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002471 unsigned NumElems = N->getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002472 SDValue ElementBase;
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002473 unsigned i = 0;
2474 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002475 SDValue Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002476 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002477 ElementBase = Elt;
2478 break;
2479 }
2480 }
2481
Gabor Greifba36cb52008-08-28 21:40:38 +00002482 if (!ElementBase.getNode())
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002483 return false;
2484
2485 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002486 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002487 if (Arg.getOpcode() == ISD::UNDEF) continue;
2488 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002489 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002490 }
2491
2492 // Make sure it is a splat of the first vector operand.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002493 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002494}
2495
Evan Chengc575ca22006-04-17 20:43:08 +00002496/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2497/// a splat of a single element and it's a 2 or 4 element mask.
2498bool X86::isSplatMask(SDNode *N) {
2499 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2500
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002501 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002502 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2503 return false;
2504 return ::isSplatMask(N);
2505}
2506
Evan Chengf686d9b2006-10-27 21:08:32 +00002507/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2508/// specifies a splat of zero element.
2509bool X86::isSplatLoMask(SDNode *N) {
2510 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2511
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002512 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002513 if (!isUndefOrEqual(N->getOperand(i), 0))
2514 return false;
2515 return true;
2516}
2517
Evan Cheng63d33002006-03-22 08:01:21 +00002518/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2519/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2520/// instructions.
2521unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002522 unsigned NumOperands = N->getNumOperands();
2523 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2524 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002525 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002526 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002527 SDValue Arg = N->getOperand(NumOperands-i-1);
Evan Chengef698ca2006-03-31 00:30:29 +00002528 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002529 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002530 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002531 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002532 if (i != NumOperands - 1)
2533 Mask <<= Shift;
2534 }
Evan Cheng63d33002006-03-22 08:01:21 +00002535
2536 return Mask;
2537}
2538
Evan Cheng506d3df2006-03-29 23:07:14 +00002539/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2540/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2541/// instructions.
2542unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2543 unsigned Mask = 0;
2544 // 8 nodes, but we only care about the last 4.
2545 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002546 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002547 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002548 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002549 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002550 Mask |= (Val - 4);
2551 if (i != 4)
2552 Mask <<= 2;
2553 }
2554
2555 return Mask;
2556}
2557
2558/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2559/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2560/// instructions.
2561unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2562 unsigned Mask = 0;
2563 // 8 nodes, but we only care about the first 4.
2564 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002565 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002566 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002567 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002568 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002569 Mask |= Val;
2570 if (i != 0)
2571 Mask <<= 2;
2572 }
2573
2574 return Mask;
2575}
2576
Evan Chengc21a0532006-04-05 01:47:37 +00002577/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2578/// specifies a 8 element shuffle that can be broken into a pair of
2579/// PSHUFHW and PSHUFLW.
2580static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2581 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2582
2583 if (N->getNumOperands() != 8)
2584 return false;
2585
2586 // Lower quadword shuffled.
2587 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002588 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002589 if (Arg.getOpcode() == ISD::UNDEF) continue;
2590 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002591 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002592 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002593 return false;
2594 }
2595
2596 // Upper quadword shuffled.
2597 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002598 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002599 if (Arg.getOpcode() == ISD::UNDEF) continue;
2600 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002601 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengc21a0532006-04-05 01:47:37 +00002602 if (Val < 4 || Val > 7)
2603 return false;
2604 }
2605
2606 return true;
2607}
2608
Chris Lattner8a594482007-11-25 00:24:49 +00002609/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002610/// values in ther permute mask.
Dan Gohman475871a2008-07-27 21:46:04 +00002611static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2612 SDValue &V2, SDValue &Mask,
Evan Cheng9eca5e82006-10-25 21:49:50 +00002613 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002614 MVT VT = Op.getValueType();
2615 MVT MaskVT = Mask.getValueType();
2616 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002617 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002618 SmallVector<SDValue, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002619
2620 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002621 SDValue Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002622 if (Arg.getOpcode() == ISD::UNDEF) {
2623 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2624 continue;
2625 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002626 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002627 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002628 if (Val < NumElems)
2629 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2630 else
2631 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2632 }
2633
Evan Cheng9eca5e82006-10-25 21:49:50 +00002634 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002635 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002636 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002637}
2638
Evan Cheng779ccea2007-12-07 21:30:01 +00002639/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2640/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002641static
Dan Gohman475871a2008-07-27 21:46:04 +00002642SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002643 MVT MaskVT = Mask.getValueType();
2644 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002645 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002646 SmallVector<SDValue, 8> MaskVec;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002647 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002648 SDValue Arg = Mask.getOperand(i);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002649 if (Arg.getOpcode() == ISD::UNDEF) {
2650 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2651 continue;
2652 }
2653 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002654 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002655 if (Val < NumElems)
2656 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2657 else
2658 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2659 }
2660 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2661}
2662
2663
Evan Cheng533a0aa2006-04-19 20:35:22 +00002664/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2665/// match movhlps. The lower half elements should come from upper half of
2666/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002667/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002668static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2669 unsigned NumElems = Mask->getNumOperands();
2670 if (NumElems != 4)
2671 return false;
2672 for (unsigned i = 0, e = 2; i != e; ++i)
2673 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2674 return false;
2675 for (unsigned i = 2; i != 4; ++i)
2676 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2677 return false;
2678 return true;
2679}
2680
Evan Cheng5ced1d82006-04-06 23:23:56 +00002681/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002682/// is promoted to a vector. It also returns the LoadSDNode by reference if
2683/// required.
2684static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng533a0aa2006-04-19 20:35:22 +00002685 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002686 N = N->getOperand(0).getNode();
Evan Cheng7e2ff772008-05-08 00:57:18 +00002687 if (ISD::isNON_EXTLoad(N)) {
2688 if (LD)
2689 *LD = cast<LoadSDNode>(N);
2690 return true;
2691 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002692 }
2693 return false;
2694}
2695
Evan Cheng533a0aa2006-04-19 20:35:22 +00002696/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2697/// match movlp{s|d}. The lower half elements should come from lower half of
2698/// V1 (and in order), and the upper half elements should come from the upper
2699/// half of V2 (and in order). And since V1 will become the source of the
2700/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002701static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002702 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002703 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002704 // Is V2 is a vector load, don't do this transformation. We will try to use
2705 // load folding shufps op.
2706 if (ISD::isNON_EXTLoad(V2))
2707 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002708
Evan Cheng533a0aa2006-04-19 20:35:22 +00002709 unsigned NumElems = Mask->getNumOperands();
2710 if (NumElems != 2 && NumElems != 4)
2711 return false;
2712 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2713 if (!isUndefOrEqual(Mask->getOperand(i), i))
2714 return false;
2715 for (unsigned i = NumElems/2; i != NumElems; ++i)
2716 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2717 return false;
2718 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002719}
2720
Evan Cheng39623da2006-04-20 08:58:49 +00002721/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2722/// all the same.
2723static bool isSplatVector(SDNode *N) {
2724 if (N->getOpcode() != ISD::BUILD_VECTOR)
2725 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002726
Dan Gohman475871a2008-07-27 21:46:04 +00002727 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002728 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2729 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002730 return false;
2731 return true;
2732}
2733
Evan Cheng8cf723d2006-09-08 01:50:06 +00002734/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2735/// to an undef.
2736static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002737 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002738 return false;
2739
Dan Gohman475871a2008-07-27 21:46:04 +00002740 SDValue V1 = N->getOperand(0);
2741 SDValue V2 = N->getOperand(1);
2742 SDValue Mask = N->getOperand(2);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002743 unsigned NumElems = Mask.getNumOperands();
2744 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002745 SDValue Arg = Mask.getOperand(i);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002746 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002747 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8cf723d2006-09-08 01:50:06 +00002748 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2749 return false;
2750 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2751 return false;
2752 }
2753 }
2754 return true;
2755}
2756
Evan Cheng213d2cf2007-05-17 18:45:50 +00002757/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2758/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002759static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002760 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002761 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002762 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002763 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002764}
2765
2766/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2767/// to an zero vector.
2768static bool isZeroShuffle(SDNode *N) {
2769 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2770 return false;
2771
Dan Gohman475871a2008-07-27 21:46:04 +00002772 SDValue V1 = N->getOperand(0);
2773 SDValue V2 = N->getOperand(1);
2774 SDValue Mask = N->getOperand(2);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002775 unsigned NumElems = Mask.getNumOperands();
2776 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002777 SDValue Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002778 if (Arg.getOpcode() == ISD::UNDEF)
2779 continue;
2780
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002781 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattner8a594482007-11-25 00:24:49 +00002782 if (Idx < NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002783 unsigned Opc = V1.getNode()->getOpcode();
2784 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002785 continue;
2786 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002787 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002788 return false;
2789 } else if (Idx >= NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002790 unsigned Opc = V2.getNode()->getOpcode();
2791 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002792 continue;
2793 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002794 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
Chris Lattner8a594482007-11-25 00:24:49 +00002795 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002796 }
2797 }
2798 return true;
2799}
2800
2801/// getZeroVector - Returns a vector of specified type with all zero elements.
2802///
Dan Gohman475871a2008-07-27 21:46:04 +00002803static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002804 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002805
2806 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2807 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002808 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002809 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002810 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002811 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002812 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002813 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002814 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002815 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002816 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chengf0df0312008-05-15 08:39:06 +00002817 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2818 }
Chris Lattner8a594482007-11-25 00:24:49 +00002819 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002820}
2821
Chris Lattner8a594482007-11-25 00:24:49 +00002822/// getOnesVector - Returns a vector of specified type with all bits set.
2823///
Dan Gohman475871a2008-07-27 21:46:04 +00002824static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002825 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002826
2827 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2828 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002829 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2830 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002831 if (VT.getSizeInBits() == 64) // MMX
Chris Lattner8a594482007-11-25 00:24:49 +00002832 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2833 else // SSE
2834 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2835 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2836}
2837
2838
Evan Cheng39623da2006-04-20 08:58:49 +00002839/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2840/// that point to V2 points to its first element.
Dan Gohman475871a2008-07-27 21:46:04 +00002841static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002842 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2843
2844 bool Changed = false;
Dan Gohman475871a2008-07-27 21:46:04 +00002845 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002846 unsigned NumElems = Mask.getNumOperands();
2847 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002848 SDValue Arg = Mask.getOperand(i);
Evan Cheng39623da2006-04-20 08:58:49 +00002849 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002850 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng39623da2006-04-20 08:58:49 +00002851 if (Val > NumElems) {
2852 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2853 Changed = true;
2854 }
2855 }
2856 MaskVec.push_back(Arg);
2857 }
2858
2859 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002860 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2861 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002862 return Mask;
2863}
2864
Evan Cheng017dcc62006-04-21 01:05:10 +00002865/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2866/// operation of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002867static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002868 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2869 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002870
Dan Gohman475871a2008-07-27 21:46:04 +00002871 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002872 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2873 for (unsigned i = 1; i != NumElems; ++i)
2874 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002875 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002876}
2877
Evan Chengc575ca22006-04-17 20:43:08 +00002878/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2879/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002880static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002881 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2882 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002883 SmallVector<SDValue, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002884 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2885 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2886 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2887 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002888 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002889}
2890
Evan Cheng39623da2006-04-20 08:58:49 +00002891/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2892/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002893static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002894 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2895 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002896 unsigned Half = NumElems/2;
Dan Gohman475871a2008-07-27 21:46:04 +00002897 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002898 for (unsigned i = 0; i != Half; ++i) {
2899 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2900 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2901 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002902 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002903}
2904
Chris Lattner62098042008-03-09 01:05:04 +00002905/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2906/// element #0 of a vector with the specified index, leaving the rest of the
2907/// elements in place.
Dan Gohman475871a2008-07-27 21:46:04 +00002908static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner62098042008-03-09 01:05:04 +00002909 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002910 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2911 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002912 SmallVector<SDValue, 8> MaskVec;
Chris Lattner62098042008-03-09 01:05:04 +00002913 // Element #0 of the result gets the elt we are replacing.
2914 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2915 for (unsigned i = 1; i != NumElems; ++i)
2916 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2917 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2918}
2919
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002920/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman475871a2008-07-27 21:46:04 +00002921static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002922 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2923 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002924 if (PVT == VT)
2925 return Op;
Dan Gohman475871a2008-07-27 21:46:04 +00002926 SDValue V1 = Op.getOperand(0);
2927 SDValue Mask = Op.getOperand(2);
Evan Cheng017dcc62006-04-21 01:05:10 +00002928 unsigned NumElems = Mask.getNumOperands();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002929 // Special handling of v4f32 -> v4i32.
2930 if (VT != MVT::v4f32) {
2931 Mask = getUnpacklMask(NumElems, DAG);
2932 while (NumElems > 4) {
2933 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2934 NumElems >>= 1;
2935 }
Evan Chengf0df0312008-05-15 08:39:06 +00002936 Mask = getZeroVector(MVT::v4i32, true, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002937 }
Evan Chengc575ca22006-04-17 20:43:08 +00002938
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002939 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman475871a2008-07-27 21:46:04 +00002940 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002941 DAG.getNode(ISD::UNDEF, PVT), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002942 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2943}
2944
Evan Chengba05f722006-04-21 23:03:30 +00002945/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002946/// vector of zero or undef vector. This produces a shuffle where the low
2947/// element of V2 is swizzled into the zero/undef vector, landing at element
2948/// Idx. This produces a shuffle mask like 4,1,2,3 (idx=0) or 0,1,2,4 (idx=3).
Dan Gohman475871a2008-07-27 21:46:04 +00002949static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002950 bool isZero, bool HasSSE2,
2951 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002952 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002953 SDValue V1 = isZero
Evan Chengf0df0312008-05-15 08:39:06 +00002954 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002955 unsigned NumElems = V2.getValueType().getVectorNumElements();
2956 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2957 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002958 SmallVector<SDValue, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002959 for (unsigned i = 0; i != NumElems; ++i)
2960 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2961 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2962 else
2963 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00002964 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002965 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002966 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002967}
2968
Evan Chengf26ffe92008-05-29 08:22:04 +00002969/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2970/// a shuffle that is zero.
2971static
Dan Gohman475871a2008-07-27 21:46:04 +00002972unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengf26ffe92008-05-29 08:22:04 +00002973 unsigned NumElems, bool Low,
2974 SelectionDAG &DAG) {
2975 unsigned NumZeros = 0;
2976 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002977 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman475871a2008-07-27 21:46:04 +00002978 SDValue Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00002979 if (Idx.getOpcode() == ISD::UNDEF) {
2980 ++NumZeros;
2981 continue;
2982 }
Gabor Greifba36cb52008-08-28 21:40:38 +00002983 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
2984 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002985 ++NumZeros;
2986 else
2987 break;
2988 }
2989 return NumZeros;
2990}
2991
2992/// isVectorShift - Returns true if the shuffle can be implemented as a
2993/// logical left or right shift of a vector.
Dan Gohman475871a2008-07-27 21:46:04 +00002994static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2995 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002996 unsigned NumElems = Mask.getNumOperands();
2997
2998 isLeft = true;
2999 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3000 if (!NumZeros) {
3001 isLeft = false;
3002 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3003 if (!NumZeros)
3004 return false;
3005 }
3006
3007 bool SeenV1 = false;
3008 bool SeenV2 = false;
3009 for (unsigned i = NumZeros; i < NumElems; ++i) {
3010 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman475871a2008-07-27 21:46:04 +00003011 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengf26ffe92008-05-29 08:22:04 +00003012 if (Idx.getOpcode() == ISD::UNDEF)
3013 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003014 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengf26ffe92008-05-29 08:22:04 +00003015 if (Index < NumElems)
3016 SeenV1 = true;
3017 else {
3018 Index -= NumElems;
3019 SeenV2 = true;
3020 }
3021 if (Index != Val)
3022 return false;
3023 }
3024 if (SeenV1 && SeenV2)
3025 return false;
3026
3027 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3028 ShAmt = NumZeros;
3029 return true;
3030}
3031
3032
Evan Chengc78d3b42006-04-24 18:01:45 +00003033/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3034///
Dan Gohman475871a2008-07-27 21:46:04 +00003035static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003036 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003037 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003038 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003039 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003040
Dan Gohman475871a2008-07-27 21:46:04 +00003041 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003042 bool First = true;
3043 for (unsigned i = 0; i < 16; ++i) {
3044 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3045 if (ThisIsNonZero && First) {
3046 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003047 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003048 else
3049 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3050 First = false;
3051 }
3052
3053 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003054 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003055 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3056 if (LastIsNonZero) {
3057 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3058 }
3059 if (ThisIsNonZero) {
3060 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3061 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3062 ThisElt, DAG.getConstant(8, MVT::i8));
3063 if (LastIsNonZero)
3064 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3065 } else
3066 ThisElt = LastElt;
3067
Gabor Greifba36cb52008-08-28 21:40:38 +00003068 if (ThisElt.getNode())
Evan Chengc78d3b42006-04-24 18:01:45 +00003069 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003070 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003071 }
3072 }
3073
3074 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3075}
3076
Bill Wendlinga348c562007-03-22 18:42:45 +00003077/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003078///
Dan Gohman475871a2008-07-27 21:46:04 +00003079static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003080 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003081 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003082 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003083 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003084
Dan Gohman475871a2008-07-27 21:46:04 +00003085 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003086 bool First = true;
3087 for (unsigned i = 0; i < 8; ++i) {
3088 bool isNonZero = (NonZeros & (1 << i)) != 0;
3089 if (isNonZero) {
3090 if (First) {
3091 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003092 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003093 else
3094 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3095 First = false;
3096 }
3097 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003098 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003099 }
3100 }
3101
3102 return V;
3103}
3104
Evan Chengf26ffe92008-05-29 08:22:04 +00003105/// getVShift - Return a vector logical shift node.
3106///
Dan Gohman475871a2008-07-27 21:46:04 +00003107static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003108 unsigned NumBits, SelectionDAG &DAG,
3109 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003110 bool isMMX = VT.getSizeInBits() == 64;
3111 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003112 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3113 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3114 return DAG.getNode(ISD::BIT_CONVERT, VT,
3115 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003116 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003117}
3118
Dan Gohman475871a2008-07-27 21:46:04 +00003119SDValue
3120X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003121 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003122 if (ISD::isBuildVectorAllZeros(Op.getNode())
3123 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003124 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3125 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3126 // eliminated on x86-32 hosts.
3127 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3128 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003129
Gabor Greifba36cb52008-08-28 21:40:38 +00003130 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00003131 return getOnesVector(Op.getValueType(), DAG);
Evan Chengf0df0312008-05-15 08:39:06 +00003132 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattner8a594482007-11-25 00:24:49 +00003133 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003134
Duncan Sands83ec4b62008-06-06 12:08:01 +00003135 MVT VT = Op.getValueType();
3136 MVT EVT = VT.getVectorElementType();
3137 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003138
3139 unsigned NumElems = Op.getNumOperands();
3140 unsigned NumZero = 0;
3141 unsigned NumNonZero = 0;
3142 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003143 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003144 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003145 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003146 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003147 if (Elt.getOpcode() == ISD::UNDEF)
3148 continue;
3149 Values.insert(Elt);
3150 if (Elt.getOpcode() != ISD::Constant &&
3151 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003152 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003153 if (isZeroNode(Elt))
3154 NumZero++;
3155 else {
3156 NonZeros |= (1 << i);
3157 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003158 }
3159 }
3160
Dan Gohman7f321562007-06-25 16:23:39 +00003161 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003162 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3163 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003164 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003165
Chris Lattner67f453a2008-03-09 05:42:06 +00003166 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003167 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003168 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003169 SDValue Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003170
Chris Lattner62098042008-03-09 01:05:04 +00003171 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3172 // the value are obviously zero, truncate the value to i32 and do the
3173 // insertion that way. Only do this if the value is non-constant or if the
3174 // value is a constant being inserted into element 0. It is cheaper to do
3175 // a constant pool load than it is to do a movd + shuffle.
3176 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3177 (!IsAllConstants || Idx == 0)) {
3178 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3179 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003180 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3181 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003182
3183 // Truncate the value (which may itself be a constant) to i32, and
3184 // convert it to a vector with movd (S2V+shuffle to zero extend).
3185 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3186 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003187 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3188 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003189
3190 // Now we have our 32-bit value zero extended in the low element of
3191 // a vector. If Idx != 0, swizzle it into place.
3192 if (Idx != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003193 SDValue Ops[] = {
Chris Lattner62098042008-03-09 01:05:04 +00003194 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3195 getSwapEltZeroMask(VecElts, Idx, DAG)
3196 };
3197 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3198 }
3199 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3200 }
3201 }
3202
Chris Lattner19f79692008-03-08 22:59:52 +00003203 // If we have a constant or non-constant insertion into the low element of
3204 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3205 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3206 // depending on what the source datatype is. Because we can only get here
3207 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3208 if (Idx == 0 &&
3209 // Don't do this for i64 values on x86-32.
3210 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003211 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003212 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003213 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3214 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003215 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003216
3217 // Is it a vector logical left shift?
3218 if (NumElems == 2 && Idx == 1 &&
3219 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003220 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003221 return getVShift(true, VT,
3222 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3223 NumBits/2, DAG, *this);
3224 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003225
3226 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003227 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003228
Chris Lattner19f79692008-03-08 22:59:52 +00003229 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3230 // is a non-constant being inserted into an element other than the low one,
3231 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3232 // movd/movss) to move this into the low element, then shuffle it into
3233 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003234 if (EVTBits == 32) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003235 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3236
Evan Cheng0db9fe62006-04-25 20:13:52 +00003237 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003238 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3239 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003240 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3241 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003242 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003243 for (unsigned i = 0; i < NumElems; i++)
3244 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003245 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003246 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003247 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3248 DAG.getNode(ISD::UNDEF, VT), Mask);
3249 }
3250 }
3251
Chris Lattner67f453a2008-03-09 05:42:06 +00003252 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3253 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003254 return SDValue();
Chris Lattner67f453a2008-03-09 05:42:06 +00003255
Dan Gohmana3941172007-07-24 22:55:08 +00003256 // A vector full of immediates; various special cases are already
3257 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003258 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003259 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003260
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003261 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003262 if (EVTBits == 64) {
3263 if (NumNonZero == 1) {
3264 // One half is zero or undef.
3265 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003266 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003267 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003268 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3269 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003270 }
Dan Gohman475871a2008-07-27 21:46:04 +00003271 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003272 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003273
3274 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003275 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003276 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003277 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003278 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003279 }
3280
Bill Wendling826f36f2007-03-28 00:57:11 +00003281 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003282 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003283 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003284 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003285 }
3286
3287 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003288 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003289 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003290 if (NumElems == 4 && NumZero > 0) {
3291 for (unsigned i = 0; i < 4; ++i) {
3292 bool isZero = !(NonZeros & (1 << i));
3293 if (isZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003294 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003295 else
3296 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3297 }
3298
3299 for (unsigned i = 0; i < 2; ++i) {
3300 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3301 default: break;
3302 case 0:
3303 V[i] = V[i*2]; // Must be a zero vector.
3304 break;
3305 case 1:
3306 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3307 getMOVLMask(NumElems, DAG));
3308 break;
3309 case 2:
3310 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3311 getMOVLMask(NumElems, DAG));
3312 break;
3313 case 3:
3314 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3315 getUnpacklMask(NumElems, DAG));
3316 break;
3317 }
3318 }
3319
Duncan Sands83ec4b62008-06-06 12:08:01 +00003320 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3321 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003322 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003323 bool Reverse = (NonZeros & 0x3) == 2;
3324 for (unsigned i = 0; i < 2; ++i)
3325 if (Reverse)
3326 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3327 else
3328 MaskVec.push_back(DAG.getConstant(i, EVT));
3329 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3330 for (unsigned i = 0; i < 2; ++i)
3331 if (Reverse)
3332 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3333 else
3334 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003335 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00003336 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003337 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3338 }
3339
3340 if (Values.size() > 2) {
3341 // Expand into a number of unpckl*.
3342 // e.g. for v4f32
3343 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3344 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3345 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman475871a2008-07-27 21:46:04 +00003346 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003347 for (unsigned i = 0; i < NumElems; ++i)
3348 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3349 NumElems >>= 1;
3350 while (NumElems != 0) {
3351 for (unsigned i = 0; i < NumElems; ++i)
3352 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3353 UnpckMask);
3354 NumElems >>= 1;
3355 }
3356 return V[0];
3357 }
3358
Dan Gohman475871a2008-07-27 21:46:04 +00003359 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003360}
3361
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003362static
Dan Gohman475871a2008-07-27 21:46:04 +00003363SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendlinge85dc492008-08-21 22:35:37 +00003364 SDValue PermMask, SelectionDAG &DAG,
3365 TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +00003366 SDValue NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003367 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3368 MVT MaskEVT = MaskVT.getVectorElementType();
3369 MVT PtrVT = TLI.getPointerTy();
Gabor Greifba36cb52008-08-28 21:40:38 +00003370 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3371 PermMask.getNode()->op_end());
Evan Cheng14b32e12007-12-11 01:46:18 +00003372
3373 // First record which half of which vector the low elements come from.
3374 SmallVector<unsigned, 4> LowQuad(4);
3375 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003376 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003377 if (Elt.getOpcode() == ISD::UNDEF)
3378 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003379 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003380 int QuadIdx = EltIdx / 4;
3381 ++LowQuad[QuadIdx];
3382 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003383
Evan Cheng14b32e12007-12-11 01:46:18 +00003384 int BestLowQuad = -1;
3385 unsigned MaxQuad = 1;
3386 for (unsigned i = 0; i < 4; ++i) {
3387 if (LowQuad[i] > MaxQuad) {
3388 BestLowQuad = i;
3389 MaxQuad = LowQuad[i];
3390 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003391 }
3392
Evan Cheng14b32e12007-12-11 01:46:18 +00003393 // Record which half of which vector the high elements come from.
3394 SmallVector<unsigned, 4> HighQuad(4);
3395 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003396 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003397 if (Elt.getOpcode() == ISD::UNDEF)
3398 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003399 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003400 int QuadIdx = EltIdx / 4;
3401 ++HighQuad[QuadIdx];
3402 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003403
Evan Cheng14b32e12007-12-11 01:46:18 +00003404 int BestHighQuad = -1;
3405 MaxQuad = 1;
3406 for (unsigned i = 0; i < 4; ++i) {
3407 if (HighQuad[i] > MaxQuad) {
3408 BestHighQuad = i;
3409 MaxQuad = HighQuad[i];
3410 }
3411 }
3412
3413 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3414 if (BestLowQuad != -1 || BestHighQuad != -1) {
3415 // First sort the 4 chunks in order using shufpd.
Dan Gohman475871a2008-07-27 21:46:04 +00003416 SmallVector<SDValue, 8> MaskVec;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003417
Evan Cheng14b32e12007-12-11 01:46:18 +00003418 if (BestLowQuad != -1)
3419 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3420 else
3421 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003422
Evan Cheng14b32e12007-12-11 01:46:18 +00003423 if (BestHighQuad != -1)
3424 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3425 else
3426 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003427
Dan Gohman475871a2008-07-27 21:46:04 +00003428 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng14b32e12007-12-11 01:46:18 +00003429 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3430 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3431 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3432 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3433
3434 // Now sort high and low parts separately.
3435 BitVector InOrder(8);
3436 if (BestLowQuad != -1) {
3437 // Sort lower half in order using PSHUFLW.
3438 MaskVec.clear();
3439 bool AnyOutOrder = false;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003440
Evan Cheng14b32e12007-12-11 01:46:18 +00003441 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003442 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003443 if (Elt.getOpcode() == ISD::UNDEF) {
3444 MaskVec.push_back(Elt);
3445 InOrder.set(i);
3446 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003447 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003448 if (EltIdx != i)
3449 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003450
Evan Cheng14b32e12007-12-11 01:46:18 +00003451 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003452
Evan Cheng14b32e12007-12-11 01:46:18 +00003453 // If this element is in the right place after this shuffle, then
3454 // remember it.
3455 if ((int)(EltIdx / 4) == BestLowQuad)
3456 InOrder.set(i);
3457 }
3458 }
3459 if (AnyOutOrder) {
3460 for (unsigned i = 4; i != 8; ++i)
3461 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003462 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003463 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3464 }
3465 }
3466
3467 if (BestHighQuad != -1) {
3468 // Sort high half in order using PSHUFHW if possible.
3469 MaskVec.clear();
Bill Wendlinge85dc492008-08-21 22:35:37 +00003470
Evan Cheng14b32e12007-12-11 01:46:18 +00003471 for (unsigned i = 0; i != 4; ++i)
3472 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003473
Evan Cheng14b32e12007-12-11 01:46:18 +00003474 bool AnyOutOrder = false;
3475 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003476 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003477 if (Elt.getOpcode() == ISD::UNDEF) {
3478 MaskVec.push_back(Elt);
3479 InOrder.set(i);
3480 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003481 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003482 if (EltIdx != i)
3483 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003484
Evan Cheng14b32e12007-12-11 01:46:18 +00003485 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003486
Evan Cheng14b32e12007-12-11 01:46:18 +00003487 // If this element is in the right place after this shuffle, then
3488 // remember it.
3489 if ((int)(EltIdx / 4) == BestHighQuad)
3490 InOrder.set(i);
3491 }
3492 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003493
Evan Cheng14b32e12007-12-11 01:46:18 +00003494 if (AnyOutOrder) {
Dan Gohman475871a2008-07-27 21:46:04 +00003495 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003496 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3497 }
3498 }
3499
3500 // The other elements are put in the right place using pextrw and pinsrw.
3501 for (unsigned i = 0; i != 8; ++i) {
3502 if (InOrder[i])
3503 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003504 SDValue Elt = MaskElts[i];
Bill Wendlingae0218c2008-08-21 22:36:36 +00003505 if (Elt.getOpcode() == ISD::UNDEF)
3506 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003507 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003508 SDValue ExtOp = (EltIdx < 8)
Evan Cheng14b32e12007-12-11 01:46:18 +00003509 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3510 DAG.getConstant(EltIdx, PtrVT))
3511 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3512 DAG.getConstant(EltIdx - 8, PtrVT));
3513 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3514 DAG.getConstant(i, PtrVT));
3515 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003516
Evan Cheng14b32e12007-12-11 01:46:18 +00003517 return NewV;
3518 }
3519
Bill Wendlinge85dc492008-08-21 22:35:37 +00003520 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3521 // few as possible. First, let's find out how many elements are already in the
3522 // right order.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003523 unsigned V1InOrder = 0;
3524 unsigned V1FromV1 = 0;
3525 unsigned V2InOrder = 0;
3526 unsigned V2FromV2 = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003527 SmallVector<SDValue, 8> V1Elts;
3528 SmallVector<SDValue, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003529 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003530 SDValue Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003531 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003532 V1Elts.push_back(Elt);
3533 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003534 ++V1InOrder;
3535 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003536 continue;
3537 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003538 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003539 if (EltIdx == i) {
3540 V1Elts.push_back(Elt);
3541 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3542 ++V1InOrder;
3543 } else if (EltIdx == i+8) {
3544 V1Elts.push_back(Elt);
3545 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3546 ++V2InOrder;
3547 } else if (EltIdx < 8) {
3548 V1Elts.push_back(Elt);
3549 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003550 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003551 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3552 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003553 }
3554 }
3555
3556 if (V2InOrder > V1InOrder) {
3557 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3558 std::swap(V1, V2);
3559 std::swap(V1Elts, V2Elts);
3560 std::swap(V1FromV1, V2FromV2);
3561 }
3562
Evan Cheng14b32e12007-12-11 01:46:18 +00003563 if ((V1FromV1 + V1InOrder) != 8) {
3564 // Some elements are from V2.
3565 if (V1FromV1) {
3566 // If there are elements that are from V1 but out of place,
3567 // then first sort them in place
Dan Gohman475871a2008-07-27 21:46:04 +00003568 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003569 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003570 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003571 if (Elt.getOpcode() == ISD::UNDEF) {
3572 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3573 continue;
3574 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003575 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003576 if (EltIdx >= 8)
3577 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3578 else
3579 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3580 }
Dan Gohman475871a2008-07-27 21:46:04 +00003581 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003582 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003583 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003584
3585 NewV = V1;
3586 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003588 if (Elt.getOpcode() == ISD::UNDEF)
3589 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003590 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003591 if (EltIdx < 8)
3592 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003593 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003594 DAG.getConstant(EltIdx - 8, PtrVT));
3595 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3596 DAG.getConstant(i, PtrVT));
3597 }
3598 return NewV;
3599 } else {
3600 // All elements are from V1.
3601 NewV = V1;
3602 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003603 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003604 if (Elt.getOpcode() == ISD::UNDEF)
3605 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003606 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003607 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003608 DAG.getConstant(EltIdx, PtrVT));
3609 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3610 DAG.getConstant(i, PtrVT));
3611 }
3612 return NewV;
3613 }
3614}
3615
Evan Cheng7a831ce2007-12-15 03:00:47 +00003616/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3617/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3618/// done when every pair / quad of shuffle mask elements point to elements in
3619/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003620/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3621static
Dan Gohman475871a2008-07-27 21:46:04 +00003622SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003623 MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00003624 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng14b32e12007-12-11 01:46:18 +00003625 TargetLowering &TLI) {
3626 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003627 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003628 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003629 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003630 MVT NewVT = MaskVT;
3631 switch (VT.getSimpleVT()) {
3632 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003633 case MVT::v4f32: NewVT = MVT::v2f64; break;
3634 case MVT::v4i32: NewVT = MVT::v2i64; break;
3635 case MVT::v8i16: NewVT = MVT::v4i32; break;
3636 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003637 }
3638
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003639 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003640 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003641 NewVT = MVT::v2i64;
3642 else
3643 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003644 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003645 unsigned Scale = NumElems / NewWidth;
Dan Gohman475871a2008-07-27 21:46:04 +00003646 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003647 for (unsigned i = 0; i < NumElems; i += Scale) {
3648 unsigned StartIdx = ~0U;
3649 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003650 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng14b32e12007-12-11 01:46:18 +00003651 if (Elt.getOpcode() == ISD::UNDEF)
3652 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003653 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003654 if (StartIdx == ~0U)
3655 StartIdx = EltIdx - (EltIdx % Scale);
3656 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003657 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003658 }
3659 if (StartIdx == ~0U)
Duncan Sandsd038e042008-07-21 10:20:31 +00003660 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003661 else
Duncan Sandsd038e042008-07-21 10:20:31 +00003662 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003663 }
3664
Evan Cheng7a831ce2007-12-15 03:00:47 +00003665 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3666 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3667 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3668 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3669 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003670}
3671
Evan Chengd880b972008-05-09 21:53:03 +00003672/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003673///
Dan Gohman475871a2008-07-27 21:46:04 +00003674static SDValue getVZextMovL(MVT VT, MVT OpVT,
3675 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003676 const X86Subtarget *Subtarget) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003677 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3678 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003679 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003680 LD = dyn_cast<LoadSDNode>(SrcOp);
3681 if (!LD) {
3682 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3683 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003684 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003685 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3686 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3687 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3688 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3689 // PR2108
3690 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3691 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003692 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003693 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003694 SrcOp.getOperand(0)
3695 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003696 }
3697 }
3698 }
3699
3700 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003701 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003702 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3703}
3704
Evan Chengace3c172008-07-22 21:13:36 +00003705/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3706/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003707static SDValue
3708LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3709 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengace3c172008-07-22 21:13:36 +00003710 MVT MaskVT = PermMask.getValueType();
3711 MVT MaskEVT = MaskVT.getVectorElementType();
3712 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003713 Locs.resize(4);
Dan Gohman475871a2008-07-27 21:46:04 +00003714 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003715 unsigned NumHi = 0;
3716 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003717 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003718 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003719 if (Elt.getOpcode() == ISD::UNDEF) {
3720 Locs[i] = std::make_pair(-1, -1);
3721 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003722 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmand0859942008-08-04 23:09:15 +00003723 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengace3c172008-07-22 21:13:36 +00003724 if (Val < 4) {
3725 Locs[i] = std::make_pair(0, NumLo);
3726 Mask1[NumLo] = Elt;
3727 NumLo++;
3728 } else {
3729 Locs[i] = std::make_pair(1, NumHi);
3730 if (2+NumHi < 4)
3731 Mask1[2+NumHi] = Elt;
3732 NumHi++;
3733 }
3734 }
3735 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003736
Evan Chengace3c172008-07-22 21:13:36 +00003737 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003738 // If no more than two elements come from either vector. This can be
3739 // implemented with two shuffles. First shuffle gather the elements.
3740 // The second shuffle, which takes the first shuffle as both of its
3741 // vector operands, put the elements into the right order.
Evan Chengace3c172008-07-22 21:13:36 +00003742 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3743 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3744 &Mask1[0], Mask1.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003745
Dan Gohman475871a2008-07-27 21:46:04 +00003746 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003747 for (unsigned i = 0; i != 4; ++i) {
3748 if (Locs[i].first == -1)
3749 continue;
3750 else {
3751 unsigned Idx = (i < 2) ? 0 : 4;
3752 Idx += Locs[i].first * 2 + Locs[i].second;
3753 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3754 }
3755 }
3756
3757 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3758 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3759 &Mask2[0], Mask2.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003760 } else if (NumLo == 3 || NumHi == 3) {
3761 // Otherwise, we must have three elements from one vector, call it X, and
3762 // one element from the other, call it Y. First, use a shufps to build an
3763 // intermediate vector with the one element from Y and the element from X
3764 // that will be in the same half in the final destination (the indexes don't
3765 // matter). Then, use a shufps to build the final vector, taking the half
3766 // containing the element from Y from the intermediate, and the other half
3767 // from X.
3768 if (NumHi == 3) {
3769 // Normalize it so the 3 elements come from V1.
3770 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3771 std::swap(V1, V2);
3772 }
3773
3774 // Find the element from V2.
3775 unsigned HiIndex;
3776 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman475871a2008-07-27 21:46:04 +00003777 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003778 if (Elt.getOpcode() == ISD::UNDEF)
3779 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003780 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003781 if (Val >= 4)
3782 break;
3783 }
3784
3785 Mask1[0] = PermMask.getOperand(HiIndex);
3786 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3787 Mask1[2] = PermMask.getOperand(HiIndex^1);
3788 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3789 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3790 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3791
3792 if (HiIndex >= 2) {
3793 Mask1[0] = PermMask.getOperand(0);
3794 Mask1[1] = PermMask.getOperand(1);
3795 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3796 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3797 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3798 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3799 } else {
3800 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3801 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3802 Mask1[2] = PermMask.getOperand(2);
3803 Mask1[3] = PermMask.getOperand(3);
3804 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003805 Mask1[2] =
3806 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3807 MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003808 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003809 Mask1[3] =
3810 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3811 MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003812 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3813 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3814 }
Evan Chengace3c172008-07-22 21:13:36 +00003815 }
3816
3817 // Break it into (shuffle shuffle_hi, shuffle_lo).
3818 Locs.clear();
Dan Gohman475871a2008-07-27 21:46:04 +00003819 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3820 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3821 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003822 unsigned MaskIdx = 0;
3823 unsigned LoIdx = 0;
3824 unsigned HiIdx = 2;
3825 for (unsigned i = 0; i != 4; ++i) {
3826 if (i == 2) {
3827 MaskPtr = &HiMask;
3828 MaskIdx = 1;
3829 LoIdx = 0;
3830 HiIdx = 2;
3831 }
Dan Gohman475871a2008-07-27 21:46:04 +00003832 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003833 if (Elt.getOpcode() == ISD::UNDEF) {
3834 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003835 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003836 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3837 (*MaskPtr)[LoIdx] = Elt;
3838 LoIdx++;
3839 } else {
3840 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3841 (*MaskPtr)[HiIdx] = Elt;
3842 HiIdx++;
3843 }
3844 }
3845
Dan Gohman475871a2008-07-27 21:46:04 +00003846 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003847 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3848 &LoMask[0], LoMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003849 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003850 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3851 &HiMask[0], HiMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003852 SmallVector<SDValue, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003853 for (unsigned i = 0; i != 4; ++i) {
3854 if (Locs[i].first == -1) {
3855 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3856 } else {
3857 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3858 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3859 }
3860 }
3861 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3862 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3863 &MaskOps[0], MaskOps.size()));
3864}
3865
Dan Gohman475871a2008-07-27 21:46:04 +00003866SDValue
3867X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3868 SDValue V1 = Op.getOperand(0);
3869 SDValue V2 = Op.getOperand(1);
3870 SDValue PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003871 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003872 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003873 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003874 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3875 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003876 bool V1IsSplat = false;
3877 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003878
Gabor Greifba36cb52008-08-28 21:40:38 +00003879 if (isUndefShuffle(Op.getNode()))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003880 return DAG.getNode(ISD::UNDEF, VT);
3881
Gabor Greifba36cb52008-08-28 21:40:38 +00003882 if (isZeroShuffle(Op.getNode()))
Evan Chengf0df0312008-05-15 08:39:06 +00003883 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003884
Gabor Greifba36cb52008-08-28 21:40:38 +00003885 if (isIdentityMask(PermMask.getNode()))
Evan Cheng49892af2007-06-19 00:02:56 +00003886 return V1;
Gabor Greifba36cb52008-08-28 21:40:38 +00003887 else if (isIdentityMask(PermMask.getNode(), true))
Evan Cheng49892af2007-06-19 00:02:56 +00003888 return V2;
3889
Gabor Greifba36cb52008-08-28 21:40:38 +00003890 if (isSplatMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003891 if (isMMX || NumElems < 4) return Op;
3892 // Promote it to a v4{if}32 splat.
3893 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003894 }
3895
Evan Cheng7a831ce2007-12-15 03:00:47 +00003896 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3897 // do it!
3898 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003899 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003900 if (NewOp.getNode())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003901 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3902 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3903 // FIXME: Figure out a cleaner way to do this.
3904 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003905 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00003906 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003907 DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003908 if (NewOp.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00003909 SDValue NewV1 = NewOp.getOperand(0);
3910 SDValue NewV2 = NewOp.getOperand(1);
3911 SDValue NewMask = NewOp.getOperand(2);
Gabor Greifba36cb52008-08-28 21:40:38 +00003912 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00003913 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chengd880b972008-05-09 21:53:03 +00003914 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003915 }
3916 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003917 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00003918 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003919 DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003920 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chengd880b972008-05-09 21:53:03 +00003921 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng7e2ff772008-05-08 00:57:18 +00003922 DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003923 }
3924 }
3925
Evan Chengf26ffe92008-05-29 08:22:04 +00003926 // Check if this can be converted into a logical shift.
3927 bool isLeft = false;
3928 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003929 SDValue ShVal;
Evan Chengf26ffe92008-05-29 08:22:04 +00003930 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3931 if (isShift && ShVal.hasOneUse()) {
3932 // If the shifted value has multiple uses, it may be cheaper to use
3933 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003934 MVT EVT = VT.getVectorElementType();
3935 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003936 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3937 }
3938
Gabor Greifba36cb52008-08-28 21:40:38 +00003939 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003940 if (V1IsUndef)
3941 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003942 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chengd880b972008-05-09 21:53:03 +00003943 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003944 if (!isMMX)
3945 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003946 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003947
Gabor Greifba36cb52008-08-28 21:40:38 +00003948 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
3949 X86::isMOVSLDUPMask(PermMask.getNode()) ||
3950 X86::isMOVHLPSMask(PermMask.getNode()) ||
3951 X86::isMOVHPMask(PermMask.getNode()) ||
3952 X86::isMOVLPMask(PermMask.getNode())))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003953 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003954
Gabor Greifba36cb52008-08-28 21:40:38 +00003955 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
3956 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003957 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003958
Evan Chengf26ffe92008-05-29 08:22:04 +00003959 if (isShift) {
3960 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003961 MVT EVT = VT.getVectorElementType();
3962 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003963 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3964 }
3965
Evan Cheng9eca5e82006-10-25 21:49:50 +00003966 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003967 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3968 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00003969 V1IsSplat = isSplatVector(V1.getNode());
3970 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattner8a594482007-11-25 00:24:49 +00003971
3972 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003973 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003974 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003975 std::swap(V1IsSplat, V2IsSplat);
3976 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003977 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003978 }
3979
Evan Cheng7a831ce2007-12-15 03:00:47 +00003980 // FIXME: Figure out a cleaner way to do this.
Gabor Greifba36cb52008-08-28 21:40:38 +00003981 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Evan Cheng9bbbb982006-10-25 20:48:19 +00003982 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003983 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003984 if (V2IsSplat) {
3985 // V2 is a splat, so the mask may be malformed. That is, it may point
3986 // to any V2 element. The instruction selectior won't like this. Get
3987 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman475871a2008-07-27 21:46:04 +00003988 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00003989 if (NewMask.getNode() != PermMask.getNode())
Evan Cheng9bbbb982006-10-25 20:48:19 +00003990 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003991 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003992 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003993 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003994
Gabor Greifba36cb52008-08-28 21:40:38 +00003995 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
3996 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
3997 X86::isUNPCKLMask(PermMask.getNode()) ||
3998 X86::isUNPCKHMask(PermMask.getNode()))
Evan Chengd9b8e402006-10-16 06:36:00 +00003999 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004000
Evan Cheng9bbbb982006-10-25 20:48:19 +00004001 if (V2IsSplat) {
4002 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004003 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004004 // new vector_shuffle with the corrected mask.
Dan Gohman475871a2008-07-27 21:46:04 +00004005 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004006 if (NewMask.getNode() != PermMask.getNode()) {
4007 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004008 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004009 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00004010 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004011 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004012 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004013 }
4014 }
4015 }
4016
4017 // Normalize the node to match x86 shuffle ops if needed
Gabor Greifba36cb52008-08-28 21:40:38 +00004018 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004019 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4020
4021 if (Commuted) {
4022 // Commute is back and try unpck* again.
4023 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004024 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4025 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4026 X86::isUNPCKLMask(PermMask.getNode()) ||
4027 X86::isUNPCKHMask(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004028 return Op;
4029 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004030
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004031 // Try PSHUF* first, then SHUFP*.
4032 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4033 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greifba36cb52008-08-28 21:40:38 +00004034 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004035 if (V2.getOpcode() != ISD::UNDEF)
4036 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4037 DAG.getNode(ISD::UNDEF, VT), PermMask);
4038 return Op;
4039 }
4040
4041 if (!isMMX) {
4042 if (Subtarget->hasSSE2() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004043 (X86::isPSHUFDMask(PermMask.getNode()) ||
4044 X86::isPSHUFHWMask(PermMask.getNode()) ||
4045 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004046 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004047 if (VT == MVT::v4f32) {
4048 RVT = MVT::v4i32;
4049 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4050 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4051 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4052 } else if (V2.getOpcode() != ISD::UNDEF)
4053 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4054 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4055 if (RVT != VT)
4056 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004057 return Op;
4058 }
4059
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004060 // Binary or unary shufps.
Gabor Greifba36cb52008-08-28 21:40:38 +00004061 if (X86::isSHUFPMask(PermMask.getNode()) ||
4062 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Evan Cheng0db9fe62006-04-25 20:13:52 +00004063 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004064 }
4065
Evan Cheng14b32e12007-12-11 01:46:18 +00004066 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4067 if (VT == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004068 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004069 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004070 return NewOp;
4071 }
4072
Evan Chengace3c172008-07-22 21:13:36 +00004073 // Handle all 4 wide cases with a number of shuffles except for MMX.
4074 if (NumElems == 4 && !isMMX)
4075 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004076
Dan Gohman475871a2008-07-27 21:46:04 +00004077 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004078}
4079
Dan Gohman475871a2008-07-27 21:46:04 +00004080SDValue
4081X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004082 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004083 MVT VT = Op.getValueType();
4084 if (VT.getSizeInBits() == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004085 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004086 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004087 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004088 DAG.getValueType(VT));
4089 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004090 } else if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004091 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004092 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004093 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004094 DAG.getValueType(VT));
4095 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004096 } else if (VT == MVT::f32) {
4097 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4098 // the result back to FR32 register. It's only worth matching if the
Dan Gohman171c11e2008-04-16 02:32:24 +00004099 // result has a single use which is a store or a bitcast to i32.
Evan Cheng62a3f152008-03-24 21:52:23 +00004100 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004101 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004102 SDNode *User = *Op.getNode()->use_begin();
Dan Gohman171c11e2008-04-16 02:32:24 +00004103 if (User->getOpcode() != ISD::STORE &&
4104 (User->getOpcode() != ISD::BIT_CONVERT ||
4105 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004106 return SDValue();
4107 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng62a3f152008-03-24 21:52:23 +00004108 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4109 Op.getOperand(1));
4110 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004111 }
Dan Gohman475871a2008-07-27 21:46:04 +00004112 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004113}
4114
4115
Dan Gohman475871a2008-07-27 21:46:04 +00004116SDValue
4117X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004118 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004119 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004120
Evan Cheng62a3f152008-03-24 21:52:23 +00004121 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004122 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004123 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004124 return Res;
4125 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004126
Duncan Sands83ec4b62008-06-06 12:08:01 +00004127 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004128 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004129 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004130 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004131 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004132 if (Idx == 0)
4133 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4134 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4135 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4136 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004137 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004138 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman475871a2008-07-27 21:46:04 +00004139 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004140 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004141 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004142 DAG.getValueType(VT));
4143 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004144 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004145 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004146 if (Idx == 0)
4147 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004148 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004149 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman475871a2008-07-27 21:46:04 +00004150 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004151 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004152 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004153 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004154 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004155 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004156 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004157 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004158 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004159 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004160 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004161 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004162 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00004163 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004164 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004165 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004166 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004167 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4168 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4169 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004170 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004171 if (Idx == 0)
4172 return Op;
4173
4174 // UNPCKHPD the element to the lowest double word, then movsd.
4175 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4176 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd038e042008-07-21 10:20:31 +00004177 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman475871a2008-07-27 21:46:04 +00004178 SmallVector<SDValue, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004179 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004180 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004182 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004183 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004184 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004185 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4186 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4187 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004188 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004189 }
4190
Dan Gohman475871a2008-07-27 21:46:04 +00004191 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004192}
4193
Dan Gohman475871a2008-07-27 21:46:04 +00004194SDValue
4195X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004196 MVT VT = Op.getValueType();
4197 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004198
Dan Gohman475871a2008-07-27 21:46:04 +00004199 SDValue N0 = Op.getOperand(0);
4200 SDValue N1 = Op.getOperand(1);
4201 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004202
Dan Gohmanef521f12008-08-14 22:53:18 +00004203 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4204 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004205 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004206 : X86ISD::PINSRW;
4207 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4208 // argument.
4209 if (N1.getValueType() != MVT::i32)
4210 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4211 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004212 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begeman14d12ca2008-02-11 04:19:36 +00004213 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004214 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004215 // Bits [7:6] of the constant are the source select. This will always be
4216 // zero here. The DAG Combiner may combine an extract_elt index into these
4217 // bits. For example (insert (extract, 3), 2) could be matched by putting
4218 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4219 // Bits [5:4] of the constant are the destination select. This is the
4220 // value of the incoming immediate.
4221 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4222 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004223 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004224 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4225 }
Dan Gohman475871a2008-07-27 21:46:04 +00004226 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004227}
4228
Dan Gohman475871a2008-07-27 21:46:04 +00004229SDValue
4230X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004231 MVT VT = Op.getValueType();
4232 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004233
4234 if (Subtarget->hasSSE41())
4235 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4236
Evan Cheng794405e2007-12-12 07:55:34 +00004237 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004238 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004239
Dan Gohman475871a2008-07-27 21:46:04 +00004240 SDValue N0 = Op.getOperand(0);
4241 SDValue N1 = Op.getOperand(1);
4242 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004243
Duncan Sands83ec4b62008-06-06 12:08:01 +00004244 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004245 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4246 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004247 if (N1.getValueType() != MVT::i32)
4248 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4249 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004250 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004251 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004252 }
Dan Gohman475871a2008-07-27 21:46:04 +00004253 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004254}
4255
Dan Gohman475871a2008-07-27 21:46:04 +00004256SDValue
4257X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng52672b82008-07-22 18:39:19 +00004258 if (Op.getValueType() == MVT::v2f32)
4259 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4260 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4261 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4262 Op.getOperand(0))));
4263
Dan Gohman475871a2008-07-27 21:46:04 +00004264 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004265 MVT VT = MVT::v2i32;
4266 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004267 default: break;
4268 case MVT::v16i8:
4269 case MVT::v8i16:
4270 VT = MVT::v4i32;
4271 break;
4272 }
4273 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4274 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004275}
4276
Bill Wendling056292f2008-09-16 21:48:12 +00004277// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4278// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4279// one of the above mentioned nodes. It has to be wrapped because otherwise
4280// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4281// be used to form addressing mode. These wrapped nodes will be selected
4282// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004283SDValue
4284X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004285 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004286 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Evan Chengd0ff02c2006-11-29 23:19:46 +00004287 getPointerTy(),
4288 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004289 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004290 // With PIC, the address is actually $g + Offset.
4291 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4292 !Subtarget->isPICStyleRIPRel()) {
4293 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4294 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4295 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004296 }
4297
4298 return Result;
4299}
4300
Dan Gohman475871a2008-07-27 21:46:04 +00004301SDValue
4302X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004303 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00004304 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004305 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004306 // With PIC, the address is actually $g + Offset.
4307 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4308 !Subtarget->isPICStyleRIPRel()) {
4309 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4310 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4311 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004312 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004313
4314 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4315 // load the value at address GV, not the value of GV itself. This means that
4316 // the GlobalAddress must be in the base or index register of the address, not
4317 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004318 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004319 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman69de1932008-02-06 22:27:42 +00004320 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004321 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004322
4323 return Result;
4324}
4325
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004326// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004327static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004328LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004329 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004330 SDValue InFlag;
4331 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004332 DAG.getNode(X86ISD::GlobalBaseReg,
4333 PtrVT), InFlag);
4334 InFlag = Chain.getValue(1);
4335
4336 // emit leal symbol@TLSGD(,%ebx,1), %eax
4337 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004338 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004339 GA->getValueType(0),
4340 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004341 SDValue Ops[] = { Chain, TGA, InFlag };
4342 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004343 InFlag = Result.getValue(2);
4344 Chain = Result.getValue(1);
4345
4346 // call ___tls_get_addr. This function receives its argument in
4347 // the register EAX.
4348 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4349 InFlag = Chain.getValue(1);
4350
4351 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004352 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004353 DAG.getTargetExternalSymbol("___tls_get_addr",
4354 PtrVT),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004355 DAG.getRegister(X86::EAX, PtrVT),
4356 DAG.getRegister(X86::EBX, PtrVT),
4357 InFlag };
4358 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4359 InFlag = Chain.getValue(1);
4360
4361 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4362}
4363
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004364// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004365static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004366LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004367 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004368 SDValue InFlag, Chain;
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004369
4370 // emit leaq symbol@TLSGD(%rip), %rdi
4371 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004372 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004373 GA->getValueType(0),
4374 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004375 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4376 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004377 Chain = Result.getValue(1);
4378 InFlag = Result.getValue(2);
4379
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004380 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004381 // the register RDI.
4382 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4383 InFlag = Chain.getValue(1);
4384
4385 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004386 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004387 DAG.getTargetExternalSymbol("__tls_get_addr",
4388 PtrVT),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004389 DAG.getRegister(X86::RDI, PtrVT),
4390 InFlag };
4391 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4392 InFlag = Chain.getValue(1);
4393
4394 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4395}
4396
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004397// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4398// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004399static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004400 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004401 // Get the Thread Pointer
Dan Gohman475871a2008-07-27 21:46:04 +00004402 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004403 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4404 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004405 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004406 GA->getValueType(0),
4407 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004408 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004409
4410 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman69de1932008-02-06 22:27:42 +00004411 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004412 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004413
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004414 // The address of the thread local variable is the add of the thread
4415 // pointer with the offset of the variable.
4416 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4417}
4418
Dan Gohman475871a2008-07-27 21:46:04 +00004419SDValue
4420X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004421 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004422 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004423 assert(Subtarget->isTargetELF() &&
4424 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004425 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4426 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4427 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004428 if (Subtarget->is64Bit()) {
4429 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4430 } else {
4431 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4432 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4433 else
4434 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4435 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004436}
4437
Dan Gohman475871a2008-07-27 21:46:04 +00004438SDValue
4439X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendling056292f2008-09-16 21:48:12 +00004440 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4441 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004442 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004443 // With PIC, the address is actually $g + Offset.
4444 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4445 !Subtarget->isPICStyleRIPRel()) {
4446 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4447 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4448 Result);
4449 }
4450
4451 return Result;
4452}
4453
Dan Gohman475871a2008-07-27 21:46:04 +00004454SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004455 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004456 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004457 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4458 // With PIC, the address is actually $g + Offset.
4459 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4460 !Subtarget->isPICStyleRIPRel()) {
4461 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4462 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4463 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004464 }
4465
4466 return Result;
4467}
4468
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004469/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4470/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004471SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004472 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004473 MVT VT = Op.getValueType();
4474 unsigned VTBits = VT.getSizeInBits();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004475 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004476 SDValue ShOpLo = Op.getOperand(0);
4477 SDValue ShOpHi = Op.getOperand(1);
4478 SDValue ShAmt = Op.getOperand(2);
4479 SDValue Tmp1 = isSRA ?
Dan Gohman4c1fa612008-03-03 22:22:09 +00004480 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4481 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004482
Dan Gohman475871a2008-07-27 21:46:04 +00004483 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004484 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004485 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4486 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004487 } else {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004488 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4489 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004490 }
Evan Chenge3413162006-01-09 18:33:28 +00004491
Dan Gohman475871a2008-07-27 21:46:04 +00004492 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004493 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00004494 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004495 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004496
Dan Gohman475871a2008-07-27 21:46:04 +00004497 SDValue Hi, Lo;
4498 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4499 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4500 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004501
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004502 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf9516202008-06-30 10:19:09 +00004503 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4504 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004505 } else {
Duncan Sandsf9516202008-06-30 10:19:09 +00004506 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4507 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004508 }
4509
Dan Gohman475871a2008-07-27 21:46:04 +00004510 SDValue Ops[2] = { Lo, Hi };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004511 return DAG.getMergeValues(Ops, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004512}
Evan Chenga3195e82006-01-12 22:54:21 +00004513
Dan Gohman475871a2008-07-27 21:46:04 +00004514SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004515 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004516 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004517 "Unknown SINT_TO_FP to lower!");
4518
4519 // These are really Legal; caller falls through into that case.
4520 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004521 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004522 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4523 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004524 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004525
Duncan Sands83ec4b62008-06-06 12:08:01 +00004526 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004527 MachineFunction &MF = DAG.getMachineFunction();
4528 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004529 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4530 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004531 StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004532 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004533
4534 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004535 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004536 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004537 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004538 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4539 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004540 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004541 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004542 Ops.push_back(Chain);
4543 Ops.push_back(StackSlot);
4544 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman475871a2008-07-27 21:46:04 +00004545 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004546 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004547
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004548 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004549 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004550 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004551
4552 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4553 // shouldn't be necessary except that RFP cannot be live across
4554 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004555 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004556 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004557 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004558 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004559 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004560 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004561 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004562 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 Ops.push_back(DAG.getValueType(Op.getValueType()));
4564 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004565 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman69de1932008-02-06 22:27:42 +00004566 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004567 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004568 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004569
Evan Cheng0db9fe62006-04-25 20:13:52 +00004570 return Result;
4571}
4572
Dan Gohman475871a2008-07-27 21:46:04 +00004573std::pair<SDValue,SDValue> X86TargetLowering::
4574FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004575 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4576 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004577 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004578
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004579 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004580 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004581 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004582 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004583 if (Subtarget->is64Bit() &&
4584 Op.getValueType() == MVT::i64 &&
4585 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00004586 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004587
Evan Cheng87c89352007-10-15 20:11:21 +00004588 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4589 // stack slot.
4590 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004591 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004592 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004593 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004594 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004595 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004596 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4597 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4598 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4599 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004600 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004601
Dan Gohman475871a2008-07-27 21:46:04 +00004602 SDValue Chain = DAG.getEntryNode();
4603 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004604 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004605 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman69de1932008-02-06 22:27:42 +00004606 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004607 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004608 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004609 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004610 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4611 };
4612 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 Chain = Value.getValue(1);
4614 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4615 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4616 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004617
Evan Cheng0db9fe62006-04-25 20:13:52 +00004618 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004619 SDValue Ops[] = { Chain, Value, StackSlot };
4620 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004621
Chris Lattner27a6c732007-11-24 07:07:01 +00004622 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004623}
4624
Dan Gohman475871a2008-07-27 21:46:04 +00004625SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4626 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4627 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00004628 if (FIST.getNode() == 0) return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00004629
4630 // Load the result.
4631 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4632}
4633
4634SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00004635 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4636 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00004637 if (FIST.getNode() == 0) return 0;
Duncan Sandsf9516202008-06-30 10:19:09 +00004638
4639 MVT VT = N->getValueType(0);
4640
4641 // Return a load from the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004642 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004643
Duncan Sands4bdcb612008-07-02 17:40:58 +00004644 // Use MERGE_VALUES to drop the chain result value and get a node with one
4645 // result. This requires turning off getMergeValues simplification, since
4646 // otherwise it will give us Res back.
Gabor Greifba36cb52008-08-28 21:40:38 +00004647 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf9516202008-06-30 10:19:09 +00004648}
Chris Lattner27a6c732007-11-24 07:07:01 +00004649
Dan Gohman475871a2008-07-27 21:46:04 +00004650SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004651 MVT VT = Op.getValueType();
4652 MVT EltVT = VT;
4653 if (VT.isVector())
4654 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004655 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004656 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004657 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004658 CV.push_back(C);
4659 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004660 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004661 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004662 CV.push_back(C);
4663 CV.push_back(C);
4664 CV.push_back(C);
4665 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004666 }
Dan Gohmand3006222007-07-27 17:16:43 +00004667 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004668 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4669 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004670 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004671 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004672 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4673}
4674
Dan Gohman475871a2008-07-27 21:46:04 +00004675SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004676 MVT VT = Op.getValueType();
4677 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004678 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004679 if (VT.isVector()) {
4680 EltVT = VT.getVectorElementType();
4681 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004682 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004684 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004685 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004686 CV.push_back(C);
4687 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004688 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004689 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004690 CV.push_back(C);
4691 CV.push_back(C);
4692 CV.push_back(C);
4693 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004694 }
Dan Gohmand3006222007-07-27 17:16:43 +00004695 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004696 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4697 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004698 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004699 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004700 if (VT.isVector()) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004701 return DAG.getNode(ISD::BIT_CONVERT, VT,
4702 DAG.getNode(ISD::XOR, MVT::v2i64,
4703 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4704 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4705 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004706 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4707 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004708}
4709
Dan Gohman475871a2008-07-27 21:46:04 +00004710SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4711 SDValue Op0 = Op.getOperand(0);
4712 SDValue Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004713 MVT VT = Op.getValueType();
4714 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004715
4716 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004717 if (SrcVT.bitsLT(VT)) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004718 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4719 SrcVT = VT;
4720 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004721 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004722 if (SrcVT.bitsGT(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004723 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004724 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004725 }
4726
4727 // At this point the operands and the result should have the same
4728 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004729
Evan Cheng68c47cb2007-01-05 07:55:56 +00004730 // First get the sign bit of second operand.
4731 std::vector<Constant*> CV;
4732 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004733 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4734 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004735 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004736 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4737 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4738 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4739 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004740 }
Dan Gohmand3006222007-07-27 17:16:43 +00004741 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004742 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4743 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004744 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004745 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004746 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004747
4748 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004749 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004750 // Op0 is MVT::f32, Op1 is MVT::f64.
4751 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4752 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4753 DAG.getConstant(32, MVT::i32));
4754 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4755 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004756 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004757 }
4758
Evan Cheng73d6cf12007-01-05 21:37:56 +00004759 // Clear first operand sign bit.
4760 CV.clear();
4761 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004762 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4763 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004764 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004765 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4766 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4767 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4768 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004769 }
Dan Gohmand3006222007-07-27 17:16:43 +00004770 C = ConstantVector::get(CV);
4771 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman475871a2008-07-27 21:46:04 +00004772 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004773 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004774 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004775 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004776
4777 // Or the value with the sign bit.
4778 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004779}
4780
Dan Gohman475871a2008-07-27 21:46:04 +00004781SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004782 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00004783 SDValue Cond;
4784 SDValue Op0 = Op.getOperand(0);
4785 SDValue Op1 = Op.getOperand(1);
4786 SDValue CC = Op.getOperand(2);
Evan Cheng0488db92007-09-25 01:57:46 +00004787 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004788 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng0488db92007-09-25 01:57:46 +00004789 unsigned X86CC;
4790
Evan Cheng0488db92007-09-25 01:57:46 +00004791 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004792 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004793 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4794 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004795 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004796 }
Evan Cheng0488db92007-09-25 01:57:46 +00004797
4798 assert(isFP && "Illegal integer SetCC!");
4799
Evan Chenge5f62042007-09-29 00:00:36 +00004800 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004801 switch (SetCCOpcode) {
4802 default: assert(false && "Illegal floating point SetCC!");
4803 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman475871a2008-07-27 21:46:04 +00004804 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004805 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman475871a2008-07-27 21:46:04 +00004806 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004807 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4808 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4809 }
4810 case ISD::SETUNE: { // PF | !ZF
Dan Gohman475871a2008-07-27 21:46:04 +00004811 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004812 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman475871a2008-07-27 21:46:04 +00004813 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004814 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4815 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4816 }
4817 }
4818}
4819
Dan Gohman475871a2008-07-27 21:46:04 +00004820SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4821 SDValue Cond;
4822 SDValue Op0 = Op.getOperand(0);
4823 SDValue Op1 = Op.getOperand(1);
4824 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00004825 MVT VT = Op.getValueType();
4826 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4827 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4828
4829 if (isFP) {
4830 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00004831 MVT VT0 = Op0.getValueType();
4832 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4833 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00004834 bool Swap = false;
4835
4836 switch (SetCCOpcode) {
4837 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004838 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00004839 case ISD::SETEQ: SSECC = 0; break;
4840 case ISD::SETOGT:
4841 case ISD::SETGT: Swap = true; // Fallthrough
4842 case ISD::SETLT:
4843 case ISD::SETOLT: SSECC = 1; break;
4844 case ISD::SETOGE:
4845 case ISD::SETGE: Swap = true; // Fallthrough
4846 case ISD::SETLE:
4847 case ISD::SETOLE: SSECC = 2; break;
4848 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004849 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00004850 case ISD::SETNE: SSECC = 4; break;
4851 case ISD::SETULE: Swap = true;
4852 case ISD::SETUGE: SSECC = 5; break;
4853 case ISD::SETULT: Swap = true;
4854 case ISD::SETUGT: SSECC = 6; break;
4855 case ISD::SETO: SSECC = 7; break;
4856 }
4857 if (Swap)
4858 std::swap(Op0, Op1);
4859
Nate Begemanfb8ead02008-07-25 19:05:58 +00004860 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00004861 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00004862 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00004863 SDValue UNORD, EQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004864 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4865 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4866 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4867 }
4868 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004869 SDValue ORD, NEQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004870 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4871 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4872 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4873 }
4874 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00004875 }
4876 // Handle all other FP comparisons here.
4877 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4878 }
4879
4880 // We are handling one of the integer comparisons here. Since SSE only has
4881 // GT and EQ comparisons for integer, swapping operands and multiple
4882 // operations may be required for some comparisons.
4883 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4884 bool Swap = false, Invert = false, FlipSigns = false;
4885
4886 switch (VT.getSimpleVT()) {
4887 default: break;
4888 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4889 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4890 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4891 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4892 }
4893
4894 switch (SetCCOpcode) {
4895 default: break;
4896 case ISD::SETNE: Invert = true;
4897 case ISD::SETEQ: Opc = EQOpc; break;
4898 case ISD::SETLT: Swap = true;
4899 case ISD::SETGT: Opc = GTOpc; break;
4900 case ISD::SETGE: Swap = true;
4901 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4902 case ISD::SETULT: Swap = true;
4903 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4904 case ISD::SETUGE: Swap = true;
4905 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4906 }
4907 if (Swap)
4908 std::swap(Op0, Op1);
4909
4910 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4911 // bits of the inputs before performing those operations.
4912 if (FlipSigns) {
4913 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00004914 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4915 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4916 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00004917 SignBits.size());
4918 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4919 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4920 }
4921
Dan Gohman475871a2008-07-27 21:46:04 +00004922 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00004923
4924 // If the logical-not of the result is required, perform that now.
4925 if (Invert) {
4926 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00004927 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4928 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4929 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00004930 NegOnes.size());
4931 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4932 }
4933 return Result;
4934}
Evan Cheng0488db92007-09-25 01:57:46 +00004935
Dan Gohman475871a2008-07-27 21:46:04 +00004936SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004937 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004938 SDValue Cond = Op.getOperand(0);
4939 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004940
Evan Cheng734503b2006-09-11 02:19:56 +00004941 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004942 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004943
Evan Cheng3f41d662007-10-08 22:16:29 +00004944 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4945 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004946 if (Cond.getOpcode() == X86ISD::SETCC) {
4947 CC = Cond.getOperand(0);
4948
Dan Gohman475871a2008-07-27 21:46:04 +00004949 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00004950 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004951 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00004952
Evan Cheng3f41d662007-10-08 22:16:29 +00004953 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004954 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00004955 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng3f41d662007-10-08 22:16:29 +00004956 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner1956d152008-01-16 06:19:45 +00004957
Evan Chenge5f62042007-09-29 00:00:36 +00004958 if ((Opc == X86ISD::CMP ||
4959 Opc == X86ISD::COMI ||
4960 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004961 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004962 addTest = false;
4963 }
4964 }
4965
4966 if (addTest) {
4967 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004968 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004969 }
4970
Duncan Sands83ec4b62008-06-06 12:08:01 +00004971 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004972 MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004973 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00004974 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4975 // condition is true.
4976 Ops.push_back(Op.getOperand(2));
4977 Ops.push_back(Op.getOperand(1));
4978 Ops.push_back(CC);
4979 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004980 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004981}
4982
Dan Gohman475871a2008-07-27 21:46:04 +00004983SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004984 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004985 SDValue Chain = Op.getOperand(0);
4986 SDValue Cond = Op.getOperand(1);
4987 SDValue Dest = Op.getOperand(2);
4988 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004989
Evan Cheng0db9fe62006-04-25 20:13:52 +00004990 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004991 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004992
Evan Cheng3f41d662007-10-08 22:16:29 +00004993 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4994 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004995 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004996 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004997
Dan Gohman475871a2008-07-27 21:46:04 +00004998 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00004999 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00005000 if (Opc == X86ISD::CMP ||
5001 Opc == X86ISD::COMI ||
5002 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005003 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005004 addTest = false;
5005 }
5006 }
5007
5008 if (addTest) {
5009 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00005010 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00005011 }
Evan Chenge5f62042007-09-29 00:00:36 +00005012 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00005013 Chain, Op.getOperand(2), CC, Cond);
5014}
5015
Anton Korobeynikove060b532007-04-17 19:34:00 +00005016
5017// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5018// Calls to _alloca is needed to probe the stack when allocating more than 4k
5019// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5020// that the guard pages used by the OS virtual memory manager are allocated in
5021// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005022SDValue
5023X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005024 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005025 assert(Subtarget->isTargetCygMing() &&
5026 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005027
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005028 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005029 SDValue Chain = Op.getOperand(0);
5030 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005031 // FIXME: Ensure alignment here
5032
Dan Gohman475871a2008-07-27 21:46:04 +00005033 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005034
Duncan Sands83ec4b62008-06-06 12:08:01 +00005035 MVT IntPtr = getPointerTy();
5036 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005037
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005038 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
5039
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005040 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5041 Flag = Chain.getValue(1);
5042
5043 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005044 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005045 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005046 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005047 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005048 Flag };
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005049 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005050 Flag = Chain.getValue(1);
5051
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005052 Chain = DAG.getCALLSEQ_END(Chain,
5053 DAG.getIntPtrConstant(0),
5054 DAG.getIntPtrConstant(0),
5055 Flag);
5056
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005057 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005058
Dan Gohman475871a2008-07-27 21:46:04 +00005059 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005060 return DAG.getMergeValues(Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005061}
5062
Dan Gohman475871a2008-07-27 21:46:04 +00005063SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005064X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005065 SDValue Chain,
5066 SDValue Dst, SDValue Src,
5067 SDValue Size, unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00005068 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005069 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005070
Dan Gohman707e0182008-04-12 04:36:06 +00005071 /// If not DWORD aligned or size is more than the threshold, call the library.
5072 /// The libc version is likely to be faster for these cases. It can use the
5073 /// address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005074 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005075 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005076 ConstantSize->getZExtValue() >
5077 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005078 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005079
5080 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005081 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5082 if (const char *bzeroEntry =
5083 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005084 MVT IntPtr = getPointerTy();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005085 const Type *IntPtrTy = TD->getIntPtrType();
Dan Gohman707e0182008-04-12 04:36:06 +00005086 TargetLowering::ArgListTy Args;
5087 TargetLowering::ArgListEntry Entry;
5088 Entry.Node = Dst;
Dan Gohman68d599d2008-04-01 20:38:36 +00005089 Entry.Ty = IntPtrTy;
5090 Args.push_back(Entry);
Dan Gohman707e0182008-04-12 04:36:06 +00005091 Entry.Node = Size;
5092 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00005093 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00005094 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
Bill Wendling056292f2008-09-16 21:48:12 +00005095 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
Dan Gohman707e0182008-04-12 04:36:06 +00005096 Args, DAG);
5097 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005098 }
5099
Dan Gohman707e0182008-04-12 04:36:06 +00005100 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005101 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005102 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005103
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005104 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005105 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005106 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005107 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005108 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005109 unsigned BytesLeft = 0;
5110 bool TwoRepStos = false;
5111 if (ValC) {
5112 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005113 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005114
Evan Cheng0db9fe62006-04-25 20:13:52 +00005115 // If the value is a constant, then we can potentially use larger sets.
5116 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005117 case 2: // WORD aligned
5118 AVT = MVT::i16;
5119 ValReg = X86::AX;
5120 Val = (Val << 8) | Val;
5121 break;
5122 case 0: // DWORD aligned
5123 AVT = MVT::i32;
5124 ValReg = X86::EAX;
5125 Val = (Val << 8) | Val;
5126 Val = (Val << 16) | Val;
5127 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5128 AVT = MVT::i64;
5129 ValReg = X86::RAX;
5130 Val = (Val << 32) | Val;
5131 }
5132 break;
5133 default: // Byte aligned
5134 AVT = MVT::i8;
5135 ValReg = X86::AL;
5136 Count = DAG.getIntPtrConstant(SizeVal);
5137 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005138 }
5139
Duncan Sands8e4eb092008-06-08 20:54:56 +00005140 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005141 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005142 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5143 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005144 }
5145
Evan Cheng0db9fe62006-04-25 20:13:52 +00005146 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5147 InFlag);
5148 InFlag = Chain.getValue(1);
5149 } else {
5150 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005151 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohman707e0182008-04-12 04:36:06 +00005152 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005153 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005154 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005155
Evan Cheng25ab6902006-09-08 06:48:29 +00005156 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5157 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005158 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005159 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005160 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005161 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005162
Chris Lattnerd96d0722007-02-25 06:40:16 +00005163 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005164 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005165 Ops.push_back(Chain);
5166 Ops.push_back(DAG.getValueType(AVT));
5167 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005168 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005169
Evan Cheng0db9fe62006-04-25 20:13:52 +00005170 if (TwoRepStos) {
5171 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005172 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005173 MVT CVT = Count.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005174 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005175 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5176 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5177 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005178 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005179 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005180 Ops.clear();
5181 Ops.push_back(Chain);
5182 Ops.push_back(DAG.getValueType(MVT::i8));
5183 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005184 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005185 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005186 // Handle the last 1 - 7 bytes.
5187 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005188 MVT AddrVT = Dst.getValueType();
5189 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005190
5191 Chain = DAG.getMemset(Chain,
5192 DAG.getNode(ISD::ADD, AddrVT, Dst,
5193 DAG.getConstant(Offset, AddrVT)),
5194 Src,
5195 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005196 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005197 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005198
Dan Gohman707e0182008-04-12 04:36:06 +00005199 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005200 return Chain;
5201}
Evan Cheng11e15b32006-04-03 20:53:28 +00005202
Dan Gohman475871a2008-07-27 21:46:04 +00005203SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005204X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005205 SDValue Chain, SDValue Dst, SDValue Src,
5206 SDValue Size, unsigned Align,
5207 bool AlwaysInline,
5208 const Value *DstSV, uint64_t DstSVOff,
5209 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005210 // This requires the copy size to be a constant, preferrably
5211 // within a subtarget-specific limit.
5212 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5213 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005214 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005215 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005216 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005217 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005218
Evan Cheng1887c1c2008-08-21 21:00:15 +00005219 /// If not DWORD aligned, call the library.
5220 if ((Align & 3) != 0)
5221 return SDValue();
5222
5223 // DWORD aligned
5224 MVT AVT = MVT::i32;
5225 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005226 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005227
Duncan Sands83ec4b62008-06-06 12:08:01 +00005228 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005229 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005230 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005231 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005232
Dan Gohman475871a2008-07-27 21:46:04 +00005233 SDValue InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005234 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5235 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005236 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005237 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005238 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005239 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005240 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005241 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005242 InFlag = Chain.getValue(1);
5243
Chris Lattnerd96d0722007-02-25 06:40:16 +00005244 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005245 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005246 Ops.push_back(Chain);
5247 Ops.push_back(DAG.getValueType(AVT));
5248 Ops.push_back(InFlag);
Dan Gohman475871a2008-07-27 21:46:04 +00005249 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005250
Dan Gohman475871a2008-07-27 21:46:04 +00005251 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005252 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005253 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005254 // Handle the last 1 - 7 bytes.
5255 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005256 MVT DstVT = Dst.getValueType();
5257 MVT SrcVT = Src.getValueType();
5258 MVT SizeVT = Size.getValueType();
Evan Cheng2749c722008-04-25 00:26:43 +00005259 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00005260 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005261 DAG.getConstant(Offset, DstVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005262 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005263 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005264 DAG.getConstant(BytesLeft, SizeVT),
5265 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005266 DstSV, DstSVOff + Offset,
5267 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005268 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005269
Dan Gohman707e0182008-04-12 04:36:06 +00005270 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005271}
5272
Chris Lattner27a6c732007-11-24 07:07:01 +00005273/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5274SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00005275 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005276 SDValue TheChain = N->getOperand(0);
5277 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005278 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005279 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5280 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005281 MVT::i64, rax.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00005282 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005283 DAG.getConstant(32, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00005284 SDValue Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00005285 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00005286 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00005287
Gabor Greifba36cb52008-08-28 21:40:38 +00005288 return DAG.getMergeValues(Ops, 2).getNode();
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005289 }
Chris Lattner5a88b832007-02-25 07:10:00 +00005290
Dan Gohman475871a2008-07-27 21:46:04 +00005291 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5292 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005293 MVT::i32, eax.getValue(2));
5294 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman475871a2008-07-27 21:46:04 +00005295 SDValue Ops[] = { eax, edx };
Chris Lattner27a6c732007-11-24 07:07:01 +00005296 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5297
5298 // Use a MERGE_VALUES to return the value and chain.
5299 Ops[1] = edx.getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00005300 return DAG.getMergeValues(Ops, 2).getNode();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005301}
5302
Dan Gohman475871a2008-07-27 21:46:04 +00005303SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005304 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005305
Evan Cheng25ab6902006-09-08 06:48:29 +00005306 if (!Subtarget->is64Bit()) {
5307 // vastart just stores the address of the VarArgsFrameIndex slot into the
5308 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005309 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005310 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005311 }
5312
5313 // __va_list_tag:
5314 // gp_offset (0 - 6 * 8)
5315 // fp_offset (48 - 48 + 8 * 16)
5316 // overflow_arg_area (point to parameters coming in memory).
5317 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005318 SmallVector<SDValue, 8> MemOps;
5319 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005320 // Store gp_offset
Dan Gohman475871a2008-07-27 21:46:04 +00005321 SDValue Store = DAG.getStore(Op.getOperand(0),
Evan Cheng786225a2006-10-05 23:01:46 +00005322 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005323 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005324 MemOps.push_back(Store);
5325
5326 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00005327 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00005328 Store = DAG.getStore(Op.getOperand(0),
5329 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005330 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005331 MemOps.push_back(Store);
5332
5333 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00005334 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005335 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005336 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005337 MemOps.push_back(Store);
5338
5339 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00005340 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005341 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005342 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005343 MemOps.push_back(Store);
5344 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005345}
5346
Dan Gohman475871a2008-07-27 21:46:04 +00005347SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005348 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5349 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005350 SDValue Chain = Op.getOperand(0);
5351 SDValue SrcPtr = Op.getOperand(1);
5352 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005353
5354 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5355 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005356 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005357}
5358
Dan Gohman475871a2008-07-27 21:46:04 +00005359SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005360 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005361 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005362 SDValue Chain = Op.getOperand(0);
5363 SDValue DstPtr = Op.getOperand(1);
5364 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005365 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5366 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengae642192007-03-02 23:16:35 +00005367
Dan Gohman28269132008-04-18 20:55:41 +00005368 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5369 DAG.getIntPtrConstant(24), 8, false,
5370 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005371}
5372
Dan Gohman475871a2008-07-27 21:46:04 +00005373SDValue
5374X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005375 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005377 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005378 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005379 case Intrinsic::x86_sse_comieq_ss:
5380 case Intrinsic::x86_sse_comilt_ss:
5381 case Intrinsic::x86_sse_comile_ss:
5382 case Intrinsic::x86_sse_comigt_ss:
5383 case Intrinsic::x86_sse_comige_ss:
5384 case Intrinsic::x86_sse_comineq_ss:
5385 case Intrinsic::x86_sse_ucomieq_ss:
5386 case Intrinsic::x86_sse_ucomilt_ss:
5387 case Intrinsic::x86_sse_ucomile_ss:
5388 case Intrinsic::x86_sse_ucomigt_ss:
5389 case Intrinsic::x86_sse_ucomige_ss:
5390 case Intrinsic::x86_sse_ucomineq_ss:
5391 case Intrinsic::x86_sse2_comieq_sd:
5392 case Intrinsic::x86_sse2_comilt_sd:
5393 case Intrinsic::x86_sse2_comile_sd:
5394 case Intrinsic::x86_sse2_comigt_sd:
5395 case Intrinsic::x86_sse2_comige_sd:
5396 case Intrinsic::x86_sse2_comineq_sd:
5397 case Intrinsic::x86_sse2_ucomieq_sd:
5398 case Intrinsic::x86_sse2_ucomilt_sd:
5399 case Intrinsic::x86_sse2_ucomile_sd:
5400 case Intrinsic::x86_sse2_ucomigt_sd:
5401 case Intrinsic::x86_sse2_ucomige_sd:
5402 case Intrinsic::x86_sse2_ucomineq_sd: {
5403 unsigned Opc = 0;
5404 ISD::CondCode CC = ISD::SETCC_INVALID;
5405 switch (IntNo) {
5406 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005407 case Intrinsic::x86_sse_comieq_ss:
5408 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005409 Opc = X86ISD::COMI;
5410 CC = ISD::SETEQ;
5411 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005412 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005413 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005414 Opc = X86ISD::COMI;
5415 CC = ISD::SETLT;
5416 break;
5417 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005418 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005419 Opc = X86ISD::COMI;
5420 CC = ISD::SETLE;
5421 break;
5422 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005423 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005424 Opc = X86ISD::COMI;
5425 CC = ISD::SETGT;
5426 break;
5427 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005428 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005429 Opc = X86ISD::COMI;
5430 CC = ISD::SETGE;
5431 break;
5432 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005433 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005434 Opc = X86ISD::COMI;
5435 CC = ISD::SETNE;
5436 break;
5437 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005438 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005439 Opc = X86ISD::UCOMI;
5440 CC = ISD::SETEQ;
5441 break;
5442 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005443 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005444 Opc = X86ISD::UCOMI;
5445 CC = ISD::SETLT;
5446 break;
5447 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005448 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005449 Opc = X86ISD::UCOMI;
5450 CC = ISD::SETLE;
5451 break;
5452 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005453 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005454 Opc = X86ISD::UCOMI;
5455 CC = ISD::SETGT;
5456 break;
5457 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005458 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005459 Opc = X86ISD::UCOMI;
5460 CC = ISD::SETGE;
5461 break;
5462 case Intrinsic::x86_sse_ucomineq_ss:
5463 case Intrinsic::x86_sse2_ucomineq_sd:
5464 Opc = X86ISD::UCOMI;
5465 CC = ISD::SETNE;
5466 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005467 }
Evan Cheng734503b2006-09-11 02:19:56 +00005468
Evan Cheng0db9fe62006-04-25 20:13:52 +00005469 unsigned X86CC;
Dan Gohman475871a2008-07-27 21:46:04 +00005470 SDValue LHS = Op.getOperand(1);
5471 SDValue RHS = Op.getOperand(2);
Chris Lattnerf9570512006-09-13 03:22:10 +00005472 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005473
Dan Gohman475871a2008-07-27 21:46:04 +00005474 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5475 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005476 DAG.getConstant(X86CC, MVT::i8), Cond);
5477 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005478 }
Evan Cheng5759f972008-05-04 09:15:50 +00005479
5480 // Fix vector shift instructions where the last operand is a non-immediate
5481 // i32 value.
5482 case Intrinsic::x86_sse2_pslli_w:
5483 case Intrinsic::x86_sse2_pslli_d:
5484 case Intrinsic::x86_sse2_pslli_q:
5485 case Intrinsic::x86_sse2_psrli_w:
5486 case Intrinsic::x86_sse2_psrli_d:
5487 case Intrinsic::x86_sse2_psrli_q:
5488 case Intrinsic::x86_sse2_psrai_w:
5489 case Intrinsic::x86_sse2_psrai_d:
5490 case Intrinsic::x86_mmx_pslli_w:
5491 case Intrinsic::x86_mmx_pslli_d:
5492 case Intrinsic::x86_mmx_pslli_q:
5493 case Intrinsic::x86_mmx_psrli_w:
5494 case Intrinsic::x86_mmx_psrli_d:
5495 case Intrinsic::x86_mmx_psrli_q:
5496 case Intrinsic::x86_mmx_psrai_w:
5497 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005498 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005499 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005500 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005501
5502 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005503 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005504 switch (IntNo) {
5505 case Intrinsic::x86_sse2_pslli_w:
5506 NewIntNo = Intrinsic::x86_sse2_psll_w;
5507 break;
5508 case Intrinsic::x86_sse2_pslli_d:
5509 NewIntNo = Intrinsic::x86_sse2_psll_d;
5510 break;
5511 case Intrinsic::x86_sse2_pslli_q:
5512 NewIntNo = Intrinsic::x86_sse2_psll_q;
5513 break;
5514 case Intrinsic::x86_sse2_psrli_w:
5515 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5516 break;
5517 case Intrinsic::x86_sse2_psrli_d:
5518 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5519 break;
5520 case Intrinsic::x86_sse2_psrli_q:
5521 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5522 break;
5523 case Intrinsic::x86_sse2_psrai_w:
5524 NewIntNo = Intrinsic::x86_sse2_psra_w;
5525 break;
5526 case Intrinsic::x86_sse2_psrai_d:
5527 NewIntNo = Intrinsic::x86_sse2_psra_d;
5528 break;
5529 default: {
5530 ShAmtVT = MVT::v2i32;
5531 switch (IntNo) {
5532 case Intrinsic::x86_mmx_pslli_w:
5533 NewIntNo = Intrinsic::x86_mmx_psll_w;
5534 break;
5535 case Intrinsic::x86_mmx_pslli_d:
5536 NewIntNo = Intrinsic::x86_mmx_psll_d;
5537 break;
5538 case Intrinsic::x86_mmx_pslli_q:
5539 NewIntNo = Intrinsic::x86_mmx_psll_q;
5540 break;
5541 case Intrinsic::x86_mmx_psrli_w:
5542 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5543 break;
5544 case Intrinsic::x86_mmx_psrli_d:
5545 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5546 break;
5547 case Intrinsic::x86_mmx_psrli_q:
5548 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5549 break;
5550 case Intrinsic::x86_mmx_psrai_w:
5551 NewIntNo = Intrinsic::x86_mmx_psra_w;
5552 break;
5553 case Intrinsic::x86_mmx_psrai_d:
5554 NewIntNo = Intrinsic::x86_mmx_psra_d;
5555 break;
5556 default: abort(); // Can't reach here.
5557 }
5558 break;
5559 }
5560 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00005561 MVT VT = Op.getValueType();
Evan Cheng5759f972008-05-04 09:15:50 +00005562 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5563 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5564 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5565 DAG.getConstant(NewIntNo, MVT::i32),
5566 Op.getOperand(1), ShAmt);
5567 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005568 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005569}
Evan Cheng72261582005-12-20 06:22:03 +00005570
Dan Gohman475871a2008-07-27 21:46:04 +00005571SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Nate Begemanbcc5f362007-01-29 22:58:52 +00005572 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005573 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005574 return SDValue();
Nate Begemanbcc5f362007-01-29 22:58:52 +00005575
5576 // Just load the return address
Dan Gohman475871a2008-07-27 21:46:04 +00005577 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005578 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5579}
5580
Dan Gohman475871a2008-07-27 21:46:04 +00005581SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Nate Begemanbcc5f362007-01-29 22:58:52 +00005582 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005583 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005584 return SDValue();
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00005585
Dan Gohman475871a2008-07-27 21:46:04 +00005586 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00005587 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005588 DAG.getIntPtrConstant(TD->getPointerSize()));
Nate Begemanbcc5f362007-01-29 22:58:52 +00005589}
5590
Dan Gohman475871a2008-07-27 21:46:04 +00005591SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00005592 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005593 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005594}
5595
Dan Gohman475871a2008-07-27 21:46:04 +00005596SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005597{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005598 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00005599 SDValue Chain = Op.getOperand(0);
5600 SDValue Offset = Op.getOperand(1);
5601 SDValue Handler = Op.getOperand(2);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005602
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00005603 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5604 getPointerTy());
5605 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005606
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005608 DAG.getIntPtrConstant(-TD->getPointerSize()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005609 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5610 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00005611 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5612 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005613
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00005614 return DAG.getNode(X86ISD::EH_RETURN,
5615 MVT::Other,
5616 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005617}
5618
Dan Gohman475871a2008-07-27 21:46:04 +00005619SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00005620 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00005621 SDValue Root = Op.getOperand(0);
5622 SDValue Trmp = Op.getOperand(1); // trampoline
5623 SDValue FPtr = Op.getOperand(2); // nested function
5624 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsb116fac2007-07-27 20:02:49 +00005625
Dan Gohman69de1932008-02-06 22:27:42 +00005626 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005627
Duncan Sands339e14f2008-01-16 22:55:25 +00005628 const X86InstrInfo *TII =
5629 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5630
Duncan Sandsb116fac2007-07-27 20:02:49 +00005631 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005632 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00005633
5634 // Large code-model.
5635
5636 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5637 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5638
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005639 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5640 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00005641
5642 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5643
5644 // Load the pointer to the nested function into R11.
5645 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00005646 SDValue Addr = Trmp;
Duncan Sands339e14f2008-01-16 22:55:25 +00005647 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005648 TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00005649
5650 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005651 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005652
5653 // Load the 'nest' parameter value into R10.
5654 // R10 is specified in X86CallingConv.td
5655 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5656 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5657 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005658 TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005659
5660 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005661 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005662
5663 // Jump to the nested function.
5664 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5665 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5666 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005667 TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00005668
5669 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5670 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5671 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005672 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00005673
Dan Gohman475871a2008-07-27 21:46:04 +00005674 SDValue Ops[] =
Duncan Sands339e14f2008-01-16 22:55:25 +00005675 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005676 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005677 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00005678 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00005679 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5680 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005681 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005682
5683 switch (CC) {
5684 default:
5685 assert(0 && "Unsupported calling convention");
5686 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005687 case CallingConv::X86_StdCall: {
5688 // Pass 'nest' parameter in ECX.
5689 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005690 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005691
5692 // Check that ECX wasn't needed by an 'inreg' parameter.
5693 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner58d74912008-03-12 17:45:29 +00005694 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005695
Chris Lattner58d74912008-03-12 17:45:29 +00005696 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00005697 unsigned InRegCount = 0;
5698 unsigned Idx = 1;
5699
5700 for (FunctionType::param_iterator I = FTy->param_begin(),
5701 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner58d74912008-03-12 17:45:29 +00005702 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00005703 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005704 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005705
5706 if (InRegCount > 2) {
5707 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5708 abort();
5709 }
5710 }
5711 break;
5712 }
5713 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00005714 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005715 // Pass 'nest' parameter in EAX.
5716 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005717 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005718 break;
5719 }
5720
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue OutChains[4];
5722 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005723
5724 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5725 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5726
Duncan Sands339e14f2008-01-16 22:55:25 +00005727 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005728 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005729 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00005730 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005731
5732 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005733 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005734
Duncan Sands339e14f2008-01-16 22:55:25 +00005735 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005736 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5737 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005738 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005739
5740 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005741 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005742
Dan Gohman475871a2008-07-27 21:46:04 +00005743 SDValue Ops[] =
Duncan Sandsf7331b32007-09-11 14:10:23 +00005744 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005745 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005746 }
5747}
5748
Dan Gohman475871a2008-07-27 21:46:04 +00005749SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005750 /*
5751 The rounding mode is in bits 11:10 of FPSR, and has the following
5752 settings:
5753 00 Round to nearest
5754 01 Round to -inf
5755 10 Round to +inf
5756 11 Round to 0
5757
5758 FLT_ROUNDS, on the other hand, expects the following:
5759 -1 Undefined
5760 0 Round to 0
5761 1 Round to nearest
5762 2 Round to +inf
5763 3 Round to -inf
5764
5765 To perform the conversion, we do:
5766 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5767 */
5768
5769 MachineFunction &MF = DAG.getMachineFunction();
5770 const TargetMachine &TM = MF.getTarget();
5771 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5772 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005773 MVT VT = Op.getValueType();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005774
5775 // Save FP Control Word to stack slot
5776 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00005777 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005778
Dan Gohman475871a2008-07-27 21:46:04 +00005779 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005780 DAG.getEntryNode(), StackSlot);
5781
5782 // Load FP Control Word from stack slot
Dan Gohman475871a2008-07-27 21:46:04 +00005783 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005784
5785 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00005786 SDValue CWD1 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005787 DAG.getNode(ISD::SRL, MVT::i16,
5788 DAG.getNode(ISD::AND, MVT::i16,
5789 CWD, DAG.getConstant(0x800, MVT::i16)),
5790 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00005791 SDValue CWD2 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005792 DAG.getNode(ISD::SRL, MVT::i16,
5793 DAG.getNode(ISD::AND, MVT::i16,
5794 CWD, DAG.getConstant(0x400, MVT::i16)),
5795 DAG.getConstant(9, MVT::i8));
5796
Dan Gohman475871a2008-07-27 21:46:04 +00005797 SDValue RetVal =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005798 DAG.getNode(ISD::AND, MVT::i16,
5799 DAG.getNode(ISD::ADD, MVT::i16,
5800 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5801 DAG.getConstant(1, MVT::i16)),
5802 DAG.getConstant(3, MVT::i16));
5803
5804
Duncan Sands83ec4b62008-06-06 12:08:01 +00005805 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005806 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5807}
5808
Dan Gohman475871a2008-07-27 21:46:04 +00005809SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005810 MVT VT = Op.getValueType();
5811 MVT OpVT = VT;
5812 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005813
5814 Op = Op.getOperand(0);
5815 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005816 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005817 OpVT = MVT::i32;
5818 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5819 }
Evan Cheng18efe262007-12-14 02:13:44 +00005820
Evan Cheng152804e2007-12-14 08:30:15 +00005821 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5822 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5823 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5824
5825 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00005826 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00005827 Ops.push_back(Op);
5828 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5829 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5830 Ops.push_back(Op.getValue(1));
5831 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5832
5833 // Finally xor with NumBits-1.
5834 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5835
Evan Cheng18efe262007-12-14 02:13:44 +00005836 if (VT == MVT::i8)
5837 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5838 return Op;
5839}
5840
Dan Gohman475871a2008-07-27 21:46:04 +00005841SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005842 MVT VT = Op.getValueType();
5843 MVT OpVT = VT;
5844 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005845
5846 Op = Op.getOperand(0);
5847 if (VT == MVT::i8) {
5848 OpVT = MVT::i32;
5849 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5850 }
Evan Cheng152804e2007-12-14 08:30:15 +00005851
5852 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5853 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5854 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5855
5856 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00005857 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00005858 Ops.push_back(Op);
5859 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5860 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5861 Ops.push_back(Op.getValue(1));
5862 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5863
Evan Cheng18efe262007-12-14 02:13:44 +00005864 if (VT == MVT::i8)
5865 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5866 return Op;
5867}
5868
Dan Gohman475871a2008-07-27 21:46:04 +00005869SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005870 MVT T = Op.getValueType();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00005871 unsigned Reg = 0;
5872 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005873 switch(T.getSimpleVT()) {
5874 default:
5875 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005876 case MVT::i8: Reg = X86::AL; size = 1; break;
5877 case MVT::i16: Reg = X86::AX; size = 2; break;
5878 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005879 case MVT::i64:
5880 if (Subtarget->is64Bit()) {
5881 Reg = X86::RAX; size = 8;
5882 } else //Should go away when LowerType stuff lands
Gabor Greifba36cb52008-08-28 21:40:38 +00005883 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005884 break;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005885 };
Dan Gohman475871a2008-07-27 21:46:04 +00005886 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00005887 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00005888 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005889 Op.getOperand(1),
Dale Johannesend18a4622008-09-11 03:12:59 +00005890 Op.getOperand(3),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005891 DAG.getTargetConstant(size, MVT::i8),
5892 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005893 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005894 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5895 SDValue cpOut =
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005896 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5897 return cpOut;
5898}
5899
Gabor Greif327ef032008-08-28 23:19:51 +00005900SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
5901 SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005902 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00005903 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman475871a2008-07-27 21:46:04 +00005904 SDValue cpInL, cpInH;
Dale Johannesend18a4622008-09-11 03:12:59 +00005905 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005906 DAG.getConstant(0, MVT::i32));
Dale Johannesend18a4622008-09-11 03:12:59 +00005907 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005908 DAG.getConstant(1, MVT::i32));
5909 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman475871a2008-07-27 21:46:04 +00005910 cpInL, SDValue());
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005911 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5912 cpInH, cpInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005913 SDValue swapInL, swapInH;
Dale Johannesend18a4622008-09-11 03:12:59 +00005914 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005915 DAG.getConstant(0, MVT::i32));
Dale Johannesend18a4622008-09-11 03:12:59 +00005916 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005917 DAG.getConstant(1, MVT::i32));
5918 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5919 swapInL, cpInH.getValue(1));
5920 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5921 swapInH, swapInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005922 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005923 Op->getOperand(1),
5924 swapInH.getValue(1)};
5925 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005926 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5927 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005928 Result.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005929 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005930 cpOutL.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00005931 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5932 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5933 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greifba36cb52008-08-28 21:40:38 +00005934 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005935}
5936
Gabor Greif327ef032008-08-28 23:19:51 +00005937SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op,
5938 SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005939 MVT T = Op->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00005940 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang63307c32008-05-05 19:05:59 +00005941 DAG.getConstant(0, T), Op->getOperand(2));
Dale Johannesene00a8a22008-08-28 02:44:49 +00005942 return DAG.getAtomic((T==MVT::i8 ? ISD::ATOMIC_LOAD_ADD_8:
5943 T==MVT::i16 ? ISD::ATOMIC_LOAD_ADD_16:
5944 T==MVT::i32 ? ISD::ATOMIC_LOAD_ADD_32:
5945 T==MVT::i64 ? ISD::ATOMIC_LOAD_ADD_64: 0),
5946 Op->getOperand(0), Op->getOperand(1), negOp,
Mon P Wang28873102008-06-25 08:15:39 +00005947 cast<AtomicSDNode>(Op)->getSrcValue(),
Gabor Greifba36cb52008-08-28 21:40:38 +00005948 cast<AtomicSDNode>(Op)->getAlignment()).getNode();
Mon P Wang63307c32008-05-05 19:05:59 +00005949}
5950
Evan Cheng0db9fe62006-04-25 20:13:52 +00005951/// LowerOperation - Provide custom lowering hooks for some operations.
5952///
Dan Gohman475871a2008-07-27 21:46:04 +00005953SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005954 switch (Op.getOpcode()) {
5955 default: assert(0 && "Should not custom lower this!");
Dale Johannesene00a8a22008-08-28 02:44:49 +00005956 case ISD::ATOMIC_CMP_SWAP_8: return LowerCMP_SWAP(Op,DAG);
5957 case ISD::ATOMIC_CMP_SWAP_16: return LowerCMP_SWAP(Op,DAG);
5958 case ISD::ATOMIC_CMP_SWAP_32: return LowerCMP_SWAP(Op,DAG);
5959 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005960 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5961 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5962 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5963 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5964 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5965 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5966 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005967 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00005968 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005969 case ISD::SHL_PARTS:
5970 case ISD::SRA_PARTS:
5971 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5972 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5973 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5974 case ISD::FABS: return LowerFABS(Op, DAG);
5975 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005976 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005977 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00005978 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005979 case ISD::SELECT: return LowerSELECT(Op, DAG);
5980 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005981 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005982 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005983 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005984 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005985 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00005986 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005987 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005988 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005989 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5990 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005991 case ISD::FRAME_TO_ARGS_OFFSET:
5992 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005993 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005994 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005995 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005996 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005997 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5998 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005999
6000 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6001 case ISD::READCYCLECOUNTER:
Gabor Greifba36cb52008-08-28 21:40:38 +00006002 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006003 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006004}
6005
Duncan Sands126d9072008-07-04 11:47:58 +00006006/// ReplaceNodeResults - Replace a node with an illegal result type
6007/// with a new node built out of custom code.
6008SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00006009 switch (N->getOpcode()) {
6010 default: assert(0 && "Should not custom lower this!");
6011 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6012 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesene00a8a22008-08-28 02:44:49 +00006013 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
6014 case ISD::ATOMIC_LOAD_SUB_8: return ExpandATOMIC_LOAD_SUB(N,DAG);
6015 case ISD::ATOMIC_LOAD_SUB_16: return ExpandATOMIC_LOAD_SUB(N,DAG);
6016 case ISD::ATOMIC_LOAD_SUB_32: return ExpandATOMIC_LOAD_SUB(N,DAG);
6017 case ISD::ATOMIC_LOAD_SUB_64: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00006018 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006019}
6020
Evan Cheng72261582005-12-20 06:22:03 +00006021const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6022 switch (Opcode) {
6023 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006024 case X86ISD::BSF: return "X86ISD::BSF";
6025 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006026 case X86ISD::SHLD: return "X86ISD::SHLD";
6027 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006028 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006029 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006030 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006031 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006032 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006033 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006034 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6035 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6036 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006037 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006038 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006039 case X86ISD::CALL: return "X86ISD::CALL";
6040 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6041 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6042 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006043 case X86ISD::COMI: return "X86ISD::COMI";
6044 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006045 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006046 case X86ISD::CMOV: return "X86ISD::CMOV";
6047 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006048 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006049 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6050 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006051 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006052 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006053 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006054 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006055 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6056 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006057 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00006058 case X86ISD::FMAX: return "X86ISD::FMAX";
6059 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006060 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6061 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006062 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6063 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006064 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006065 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006066 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006067 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6068 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006069 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6070 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006071 case X86ISD::VSHL: return "X86ISD::VSHL";
6072 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006073 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6074 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6075 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6076 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6077 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6078 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6079 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6080 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6081 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6082 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Evan Cheng72261582005-12-20 06:22:03 +00006083 }
6084}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006085
Chris Lattnerc9addb72007-03-30 23:15:24 +00006086// isLegalAddressingMode - Return true if the addressing mode represented
6087// by AM is legal for this target, for a load/store of the specified type.
6088bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6089 const Type *Ty) const {
6090 // X86 supports extremely general addressing modes.
6091
6092 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6093 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6094 return false;
6095
6096 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006097 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006098 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6099 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006100
6101 // X86-64 only supports addr of globals in small code model.
6102 if (Subtarget->is64Bit()) {
6103 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6104 return false;
6105 // If lower 4G is not available, then we must use rip-relative addressing.
6106 if (AM.BaseOffs || AM.Scale > 1)
6107 return false;
6108 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006109 }
6110
6111 switch (AM.Scale) {
6112 case 0:
6113 case 1:
6114 case 2:
6115 case 4:
6116 case 8:
6117 // These scales always work.
6118 break;
6119 case 3:
6120 case 5:
6121 case 9:
6122 // These scales are formed with basereg+scalereg. Only accept if there is
6123 // no basereg yet.
6124 if (AM.HasBaseReg)
6125 return false;
6126 break;
6127 default: // Other stuff never works.
6128 return false;
6129 }
6130
6131 return true;
6132}
6133
6134
Evan Cheng2bd122c2007-10-26 01:56:11 +00006135bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6136 if (!Ty1->isInteger() || !Ty2->isInteger())
6137 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006138 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6139 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006140 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006141 return false;
6142 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006143}
6144
Duncan Sands83ec4b62008-06-06 12:08:01 +00006145bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6146 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006147 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006148 unsigned NumBits1 = VT1.getSizeInBits();
6149 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006150 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006151 return false;
6152 return Subtarget->is64Bit() || NumBits1 < 64;
6153}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006154
Evan Cheng60c07e12006-07-05 22:17:51 +00006155/// isShuffleMaskLegal - Targets can use this to indicate that they only
6156/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6157/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6158/// are assumed to be legal.
6159bool
Dan Gohman475871a2008-07-27 21:46:04 +00006160X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006161 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006162 if (VT.getSizeInBits() == 64) return false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006163 return (Mask.getNode()->getNumOperands() <= 4 ||
6164 isIdentityMask(Mask.getNode()) ||
6165 isIdentityMask(Mask.getNode(), true) ||
6166 isSplatMask(Mask.getNode()) ||
6167 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6168 X86::isUNPCKLMask(Mask.getNode()) ||
6169 X86::isUNPCKHMask(Mask.getNode()) ||
6170 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6171 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Evan Cheng60c07e12006-07-05 22:17:51 +00006172}
6173
Dan Gohman7d8143f2008-04-09 20:09:42 +00006174bool
Dan Gohman475871a2008-07-27 21:46:04 +00006175X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006176 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006177 unsigned NumElts = BVOps.size();
6178 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006179 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006180 if (NumElts == 2) return true;
6181 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00006182 return (isMOVLMask(&BVOps[0], 4) ||
6183 isCommutedMOVL(&BVOps[0], 4, true) ||
6184 isSHUFPMask(&BVOps[0], 4) ||
6185 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00006186 }
6187 return false;
6188}
6189
6190//===----------------------------------------------------------------------===//
6191// X86 Scheduler Hooks
6192//===----------------------------------------------------------------------===//
6193
Mon P Wang63307c32008-05-05 19:05:59 +00006194// private utility function
6195MachineBasicBlock *
6196X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6197 MachineBasicBlock *MBB,
6198 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006199 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006200 unsigned LoadOpc,
6201 unsigned CXchgOpc,
6202 unsigned copyOpc,
6203 unsigned notOpc,
6204 unsigned EAXreg,
6205 TargetRegisterClass *RC,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006206 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00006207 // For the atomic bitwise operator, we generate
6208 // thisMBB:
6209 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006210 // ld t1 = [bitinstr.addr]
6211 // op t2 = t1, [bitinstr.val]
6212 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006213 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6214 // bz newMBB
6215 // fallthrough -->nextMBB
6216 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6217 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006218 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006219 ++MBBIter;
6220
6221 /// First build the CFG
6222 MachineFunction *F = MBB->getParent();
6223 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006224 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6225 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6226 F->insert(MBBIter, newMBB);
6227 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006228
6229 // Move all successors to thisMBB to nextMBB
6230 nextMBB->transferSuccessors(thisMBB);
6231
6232 // Update thisMBB to fall through to newMBB
6233 thisMBB->addSuccessor(newMBB);
6234
6235 // newMBB jumps to itself and fall through to nextMBB
6236 newMBB->addSuccessor(nextMBB);
6237 newMBB->addSuccessor(newMBB);
6238
6239 // Insert instructions into newMBB based on incoming instruction
6240 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6241 MachineOperand& destOper = bInstr->getOperand(0);
6242 MachineOperand* argOpers[6];
6243 int numArgs = bInstr->getNumOperands() - 1;
6244 for (int i=0; i < numArgs; ++i)
6245 argOpers[i] = &bInstr->getOperand(i+1);
6246
6247 // x86 address has 4 operands: base, index, scale, and displacement
6248 int lastAddrIndx = 3; // [0,3]
6249 int valArgIndx = 4;
6250
Dale Johannesen140be2d2008-08-19 18:47:28 +00006251 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6252 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006253 for (int i=0; i <= lastAddrIndx; ++i)
6254 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006255
Dale Johannesen140be2d2008-08-19 18:47:28 +00006256 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006257 if (invSrc) {
Dale Johannesen140be2d2008-08-19 18:47:28 +00006258 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006259 }
6260 else
6261 tt = t1;
6262
Dale Johannesen140be2d2008-08-19 18:47:28 +00006263 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohman014278e2008-09-13 17:58:21 +00006264 assert((argOpers[valArgIndx]->isRegister() ||
6265 argOpers[valArgIndx]->isImmediate()) &&
6266 "invalid operand");
6267 if (argOpers[valArgIndx]->isRegister())
Mon P Wang63307c32008-05-05 19:05:59 +00006268 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6269 else
6270 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006271 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00006272 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006273
Dale Johannesen140be2d2008-08-19 18:47:28 +00006274 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00006275 MIB.addReg(t1);
6276
Dale Johannesen140be2d2008-08-19 18:47:28 +00006277 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00006278 for (int i=0; i <= lastAddrIndx; ++i)
6279 (*MIB).addOperand(*argOpers[i]);
6280 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00006281 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6282 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6283
Dale Johannesen140be2d2008-08-19 18:47:28 +00006284 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6285 MIB.addReg(EAXreg);
Mon P Wang63307c32008-05-05 19:05:59 +00006286
6287 // insert branch
6288 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6289
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006290 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006291 return nextMBB;
6292}
6293
6294// private utility function
6295MachineBasicBlock *
6296X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6297 MachineBasicBlock *MBB,
6298 unsigned cmovOpc) {
6299 // For the atomic min/max operator, we generate
6300 // thisMBB:
6301 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006302 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00006303 // mov t2 = [min/max.val]
6304 // cmp t1, t2
6305 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00006306 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006307 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6308 // bz newMBB
6309 // fallthrough -->nextMBB
6310 //
6311 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6312 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006313 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006314 ++MBBIter;
6315
6316 /// First build the CFG
6317 MachineFunction *F = MBB->getParent();
6318 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006319 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6320 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6321 F->insert(MBBIter, newMBB);
6322 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006323
6324 // Move all successors to thisMBB to nextMBB
6325 nextMBB->transferSuccessors(thisMBB);
6326
6327 // Update thisMBB to fall through to newMBB
6328 thisMBB->addSuccessor(newMBB);
6329
6330 // newMBB jumps to newMBB and fall through to nextMBB
6331 newMBB->addSuccessor(nextMBB);
6332 newMBB->addSuccessor(newMBB);
6333
6334 // Insert instructions into newMBB based on incoming instruction
6335 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6336 MachineOperand& destOper = mInstr->getOperand(0);
6337 MachineOperand* argOpers[6];
6338 int numArgs = mInstr->getNumOperands() - 1;
6339 for (int i=0; i < numArgs; ++i)
6340 argOpers[i] = &mInstr->getOperand(i+1);
6341
6342 // x86 address has 4 operands: base, index, scale, and displacement
6343 int lastAddrIndx = 3; // [0,3]
6344 int valArgIndx = 4;
6345
Mon P Wangab3e7472008-05-05 22:56:23 +00006346 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6347 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006348 for (int i=0; i <= lastAddrIndx; ++i)
6349 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00006350
Mon P Wang63307c32008-05-05 19:05:59 +00006351 // We only support register and immediate values
Dan Gohman014278e2008-09-13 17:58:21 +00006352 assert((argOpers[valArgIndx]->isRegister() ||
6353 argOpers[valArgIndx]->isImmediate()) &&
6354 "invalid operand");
Mon P Wang63307c32008-05-05 19:05:59 +00006355
6356 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohman014278e2008-09-13 17:58:21 +00006357 if (argOpers[valArgIndx]->isRegister())
Mon P Wang63307c32008-05-05 19:05:59 +00006358 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6359 else
6360 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6361 (*MIB).addOperand(*argOpers[valArgIndx]);
6362
Mon P Wangab3e7472008-05-05 22:56:23 +00006363 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6364 MIB.addReg(t1);
6365
Mon P Wang63307c32008-05-05 19:05:59 +00006366 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6367 MIB.addReg(t1);
6368 MIB.addReg(t2);
6369
6370 // Generate movc
6371 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6372 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6373 MIB.addReg(t2);
6374 MIB.addReg(t1);
6375
6376 // Cmp and exchange if none has modified the memory location
6377 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6378 for (int i=0; i <= lastAddrIndx; ++i)
6379 (*MIB).addOperand(*argOpers[i]);
6380 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00006381 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6382 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang63307c32008-05-05 19:05:59 +00006383
6384 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6385 MIB.addReg(X86::EAX);
6386
6387 // insert branch
6388 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6389
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006390 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006391 return nextMBB;
6392}
6393
6394
Evan Cheng60c07e12006-07-05 22:17:51 +00006395MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006396X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6397 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00006398 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00006399 switch (MI->getOpcode()) {
6400 default: assert(false && "Unexpected instr type to insert");
6401 case X86::CMOV_FR32:
6402 case X86::CMOV_FR64:
6403 case X86::CMOV_V4F32:
6404 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00006405 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006406 // To "insert" a SELECT_CC instruction, we actually have to insert the
6407 // diamond control-flow pattern. The incoming instruction knows the
6408 // destination vreg to set, the condition code register to branch on, the
6409 // true/false values to select between, and a branch opcode to use.
6410 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006411 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00006412 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006413
Evan Cheng60c07e12006-07-05 22:17:51 +00006414 // thisMBB:
6415 // ...
6416 // TrueVal = ...
6417 // cmpTY ccX, r1, r2
6418 // bCC copy1MBB
6419 // fallthrough --> copy0MBB
6420 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006421 MachineFunction *F = BB->getParent();
6422 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6423 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006424 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00006425 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00006426 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006427 F->insert(It, copy0MBB);
6428 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006429 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00006430 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00006431 sinkMBB->transferSuccessors(BB);
6432
6433 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00006434 BB->addSuccessor(copy0MBB);
6435 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006436
Evan Cheng60c07e12006-07-05 22:17:51 +00006437 // copy0MBB:
6438 // %FalseValue = ...
6439 // # fallthrough to sinkMBB
6440 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006441
Evan Cheng60c07e12006-07-05 22:17:51 +00006442 // Update machine-CFG edges
6443 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006444
Evan Cheng60c07e12006-07-05 22:17:51 +00006445 // sinkMBB:
6446 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6447 // ...
6448 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00006449 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00006450 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6451 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6452
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006453 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006454 return BB;
6455 }
6456
Dale Johannesen849f2142007-07-03 00:53:03 +00006457 case X86::FP32_TO_INT16_IN_MEM:
6458 case X86::FP32_TO_INT32_IN_MEM:
6459 case X86::FP32_TO_INT64_IN_MEM:
6460 case X86::FP64_TO_INT16_IN_MEM:
6461 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00006462 case X86::FP64_TO_INT64_IN_MEM:
6463 case X86::FP80_TO_INT16_IN_MEM:
6464 case X86::FP80_TO_INT32_IN_MEM:
6465 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006466 // Change the floating point control register to use "round towards zero"
6467 // mode when truncating to an integer value.
6468 MachineFunction *F = BB->getParent();
6469 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006470 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006471
6472 // Load the old value of the high byte of the control word...
6473 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00006474 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006475 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006476
6477 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006478 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6479 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00006480
6481 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006482 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006483
6484 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00006485 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6486 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00006487
6488 // Get the X86 opcode to use.
6489 unsigned Opc;
6490 switch (MI->getOpcode()) {
6491 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00006492 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6493 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6494 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6495 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6496 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6497 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00006498 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6499 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6500 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00006501 }
6502
6503 X86AddressMode AM;
6504 MachineOperand &Op = MI->getOperand(0);
6505 if (Op.isRegister()) {
6506 AM.BaseType = X86AddressMode::RegBase;
6507 AM.Base.Reg = Op.getReg();
6508 } else {
6509 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00006510 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00006511 }
6512 Op = MI->getOperand(1);
6513 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006514 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006515 Op = MI->getOperand(2);
6516 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006517 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006518 Op = MI->getOperand(3);
6519 if (Op.isGlobalAddress()) {
6520 AM.GV = Op.getGlobal();
6521 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00006522 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006523 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00006524 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6525 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00006526
6527 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00006528 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006529
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006530 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006531 return BB;
6532 }
Mon P Wang63307c32008-05-05 19:05:59 +00006533 case X86::ATOMAND32:
6534 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006535 X86::AND32ri, X86::MOV32rm,
6536 X86::LCMPXCHG32, X86::MOV32rr,
6537 X86::NOT32r, X86::EAX,
6538 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00006539 case X86::ATOMOR32:
6540 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006541 X86::OR32ri, X86::MOV32rm,
6542 X86::LCMPXCHG32, X86::MOV32rr,
6543 X86::NOT32r, X86::EAX,
6544 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00006545 case X86::ATOMXOR32:
6546 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006547 X86::XOR32ri, X86::MOV32rm,
6548 X86::LCMPXCHG32, X86::MOV32rr,
6549 X86::NOT32r, X86::EAX,
6550 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006551 case X86::ATOMNAND32:
6552 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006553 X86::AND32ri, X86::MOV32rm,
6554 X86::LCMPXCHG32, X86::MOV32rr,
6555 X86::NOT32r, X86::EAX,
6556 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00006557 case X86::ATOMMIN32:
6558 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6559 case X86::ATOMMAX32:
6560 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6561 case X86::ATOMUMIN32:
6562 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6563 case X86::ATOMUMAX32:
6564 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00006565
6566 case X86::ATOMAND16:
6567 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6568 X86::AND16ri, X86::MOV16rm,
6569 X86::LCMPXCHG16, X86::MOV16rr,
6570 X86::NOT16r, X86::AX,
6571 X86::GR16RegisterClass);
6572 case X86::ATOMOR16:
6573 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6574 X86::OR16ri, X86::MOV16rm,
6575 X86::LCMPXCHG16, X86::MOV16rr,
6576 X86::NOT16r, X86::AX,
6577 X86::GR16RegisterClass);
6578 case X86::ATOMXOR16:
6579 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6580 X86::XOR16ri, X86::MOV16rm,
6581 X86::LCMPXCHG16, X86::MOV16rr,
6582 X86::NOT16r, X86::AX,
6583 X86::GR16RegisterClass);
6584 case X86::ATOMNAND16:
6585 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6586 X86::AND16ri, X86::MOV16rm,
6587 X86::LCMPXCHG16, X86::MOV16rr,
6588 X86::NOT16r, X86::AX,
6589 X86::GR16RegisterClass, true);
6590 case X86::ATOMMIN16:
6591 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6592 case X86::ATOMMAX16:
6593 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6594 case X86::ATOMUMIN16:
6595 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6596 case X86::ATOMUMAX16:
6597 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6598
6599 case X86::ATOMAND8:
6600 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6601 X86::AND8ri, X86::MOV8rm,
6602 X86::LCMPXCHG8, X86::MOV8rr,
6603 X86::NOT8r, X86::AL,
6604 X86::GR8RegisterClass);
6605 case X86::ATOMOR8:
6606 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6607 X86::OR8ri, X86::MOV8rm,
6608 X86::LCMPXCHG8, X86::MOV8rr,
6609 X86::NOT8r, X86::AL,
6610 X86::GR8RegisterClass);
6611 case X86::ATOMXOR8:
6612 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6613 X86::XOR8ri, X86::MOV8rm,
6614 X86::LCMPXCHG8, X86::MOV8rr,
6615 X86::NOT8r, X86::AL,
6616 X86::GR8RegisterClass);
6617 case X86::ATOMNAND8:
6618 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6619 X86::AND8ri, X86::MOV8rm,
6620 X86::LCMPXCHG8, X86::MOV8rr,
6621 X86::NOT8r, X86::AL,
6622 X86::GR8RegisterClass, true);
6623 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesena99e3842008-08-20 00:48:50 +00006624 case X86::ATOMAND64:
6625 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6626 X86::AND64ri32, X86::MOV64rm,
6627 X86::LCMPXCHG64, X86::MOV64rr,
6628 X86::NOT64r, X86::RAX,
6629 X86::GR64RegisterClass);
6630 case X86::ATOMOR64:
6631 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6632 X86::OR64ri32, X86::MOV64rm,
6633 X86::LCMPXCHG64, X86::MOV64rr,
6634 X86::NOT64r, X86::RAX,
6635 X86::GR64RegisterClass);
6636 case X86::ATOMXOR64:
6637 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6638 X86::XOR64ri32, X86::MOV64rm,
6639 X86::LCMPXCHG64, X86::MOV64rr,
6640 X86::NOT64r, X86::RAX,
6641 X86::GR64RegisterClass);
6642 case X86::ATOMNAND64:
6643 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6644 X86::AND64ri32, X86::MOV64rm,
6645 X86::LCMPXCHG64, X86::MOV64rr,
6646 X86::NOT64r, X86::RAX,
6647 X86::GR64RegisterClass, true);
6648 case X86::ATOMMIN64:
6649 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6650 case X86::ATOMMAX64:
6651 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6652 case X86::ATOMUMIN64:
6653 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6654 case X86::ATOMUMAX64:
6655 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Evan Cheng60c07e12006-07-05 22:17:51 +00006656 }
6657}
6658
6659//===----------------------------------------------------------------------===//
6660// X86 Optimization Hooks
6661//===----------------------------------------------------------------------===//
6662
Dan Gohman475871a2008-07-27 21:46:04 +00006663void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006664 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006665 APInt &KnownZero,
6666 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006667 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00006668 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006669 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00006670 assert((Opc >= ISD::BUILTIN_OP_END ||
6671 Opc == ISD::INTRINSIC_WO_CHAIN ||
6672 Opc == ISD::INTRINSIC_W_CHAIN ||
6673 Opc == ISD::INTRINSIC_VOID) &&
6674 "Should use MaskedValueIsZero if you don't know whether Op"
6675 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006676
Dan Gohmanf4f92f52008-02-13 23:07:24 +00006677 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006678 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00006679 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006680 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006681 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6682 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00006683 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006684 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006685}
Chris Lattner259e97c2006-01-31 19:43:35 +00006686
Evan Cheng206ee9d2006-07-07 08:33:52 +00006687/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00006688/// node is a GlobalAddress + offset.
6689bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6690 GlobalValue* &GA, int64_t &Offset) const{
6691 if (N->getOpcode() == X86ISD::Wrapper) {
6692 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006693 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6694 return true;
6695 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00006696 }
Evan Chengad4196b2008-05-12 19:56:52 +00006697 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006698}
6699
Evan Chengad4196b2008-05-12 19:56:52 +00006700static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6701 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006702 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00006703 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00006704 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006705 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00006706 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00006707 return false;
6708}
6709
Dan Gohman475871a2008-07-27 21:46:04 +00006710static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006711 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00006712 SDNode *&Base,
6713 SelectionDAG &DAG, MachineFrameInfo *MFI,
6714 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006715 Base = NULL;
6716 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006717 SDValue Idx = PermMask.getOperand(i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006718 if (Idx.getOpcode() == ISD::UNDEF) {
6719 if (!Base)
6720 return false;
6721 continue;
6722 }
6723
Dan Gohman475871a2008-07-27 21:46:04 +00006724 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00006725 if (!Elt.getNode() ||
6726 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006727 return false;
6728 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006729 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00006730 if (Base->getOpcode() == ISD::UNDEF)
6731 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00006732 continue;
6733 }
6734 if (Elt.getOpcode() == ISD::UNDEF)
6735 continue;
6736
Gabor Greifba36cb52008-08-28 21:40:38 +00006737 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006738 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006739 return false;
6740 }
6741 return true;
6742}
Evan Cheng206ee9d2006-07-07 08:33:52 +00006743
6744/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6745/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6746/// if the load addresses are consecutive, non-overlapping, and in the right
6747/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00006748static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006749 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006750 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006751 MVT VT = N->getValueType(0);
6752 MVT EVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00006753 SDValue PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00006754 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006755 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00006756 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6757 DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00006758 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006759
Dan Gohmand3006222007-07-27 17:16:43 +00006760 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00006761 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Evan Cheng466685d2006-10-09 20:57:25 +00006762 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00006763 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng71f489d2008-05-05 22:12:23 +00006764 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6765 LD->getSrcValueOffset(), LD->isVolatile(),
6766 LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00006767}
6768
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006769/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00006770static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006771 const X86Subtarget *Subtarget,
6772 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00006773 unsigned NumOps = N->getNumOperands();
6774
Evan Chengd880b972008-05-09 21:53:03 +00006775 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00006776 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00006777 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006778
Duncan Sands83ec4b62008-06-06 12:08:01 +00006779 MVT VT = N->getValueType(0);
6780 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00006781 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6782 // We are looking for load i64 and zero extend. We want to transform
6783 // it before legalizer has a chance to expand it. Also look for i64
6784 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00006785 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006786 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006787 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00006788 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00006789 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006790
6791 // Value must be a load.
Gabor Greifba36cb52008-08-28 21:40:38 +00006792 SDNode *Base = N->getOperand(0).getNode();
Evan Chengd880b972008-05-09 21:53:03 +00006793 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006794 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00006795 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00006796 Base = Base->getOperand(0).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006797 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00006798 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006799 }
Evan Chengd880b972008-05-09 21:53:03 +00006800
6801 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006802 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00006803
6804 // Load must not be an extload.
6805 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00006806 return SDValue();
Nate Begemanf7333bf2008-05-28 00:24:25 +00006807
Evan Chengd880b972008-05-09 21:53:03 +00006808 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6809}
6810
Chris Lattner83e6c992006-10-04 06:57:07 +00006811/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006812static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner83e6c992006-10-04 06:57:07 +00006813 const X86Subtarget *Subtarget) {
Dan Gohman475871a2008-07-27 21:46:04 +00006814 SDValue Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006815
Chris Lattner83e6c992006-10-04 06:57:07 +00006816 // If we have SSE[12] support, try to form min/max nodes.
6817 if (Subtarget->hasSSE2() &&
6818 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6819 if (Cond.getOpcode() == ISD::SETCC) {
6820 // Get the LHS/RHS of the select.
Dan Gohman475871a2008-07-27 21:46:04 +00006821 SDValue LHS = N->getOperand(1);
6822 SDValue RHS = N->getOperand(2);
Chris Lattner83e6c992006-10-04 06:57:07 +00006823 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006824
Evan Cheng8ca29322006-11-10 21:43:37 +00006825 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00006826 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006827 switch (CC) {
6828 default: break;
6829 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6830 case ISD::SETULE:
6831 case ISD::SETLE:
6832 if (!UnsafeFPMath) break;
6833 // FALL THROUGH.
6834 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6835 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006836 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006837 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006838
Chris Lattner1907a7b2006-10-05 04:11:26 +00006839 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6840 case ISD::SETUGT:
6841 case ISD::SETGT:
6842 if (!UnsafeFPMath) break;
6843 // FALL THROUGH.
6844 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6845 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006846 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006847 break;
6848 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006849 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006850 switch (CC) {
6851 default: break;
6852 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6853 case ISD::SETUGT:
6854 case ISD::SETGT:
6855 if (!UnsafeFPMath) break;
6856 // FALL THROUGH.
6857 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6858 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006859 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006860 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006861
Chris Lattner1907a7b2006-10-05 04:11:26 +00006862 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6863 case ISD::SETULE:
6864 case ISD::SETLE:
6865 if (!UnsafeFPMath) break;
6866 // FALL THROUGH.
6867 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6868 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006869 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006870 break;
6871 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006872 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006873
Evan Cheng8ca29322006-11-10 21:43:37 +00006874 if (Opcode)
6875 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00006876 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006877
Chris Lattner83e6c992006-10-04 06:57:07 +00006878 }
6879
Dan Gohman475871a2008-07-27 21:46:04 +00006880 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00006881}
6882
Chris Lattner149a4e52008-02-22 02:09:43 +00006883/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006884static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00006885 const X86Subtarget *Subtarget) {
6886 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6887 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00006888 // A preferable solution to the general problem is to figure out the right
6889 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006890 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006891 if (St->getValue().getValueType().isVector() &&
6892 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00006893 isa<LoadSDNode>(St->getValue()) &&
6894 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6895 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006896 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00006897 LoadSDNode *Ld = 0;
6898 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00006899 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00006900 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00006901 // Must be a store of a load. We currently handle two cases: the load
6902 // is a direct child, and it's under an intervening TokenFactor. It is
6903 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006904 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00006905 Ld = cast<LoadSDNode>(St->getChain());
6906 else if (St->getValue().hasOneUse() &&
6907 ChainVal->getOpcode() == ISD::TokenFactor) {
6908 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00006909 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00006910 TokenFactorIndex = i;
6911 Ld = cast<LoadSDNode>(St->getValue());
6912 } else
6913 Ops.push_back(ChainVal->getOperand(i));
6914 }
6915 }
6916 if (Ld) {
6917 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6918 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006919 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00006920 Ld->getBasePtr(), Ld->getSrcValue(),
6921 Ld->getSrcValueOffset(), Ld->isVolatile(),
6922 Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006923 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006924 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00006925 Ops.push_back(NewChain);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006926 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6927 Ops.size());
6928 }
6929 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6930 St->getSrcValue(), St->getSrcValueOffset(),
6931 St->isVolatile(), St->getAlignment());
6932 }
6933
6934 // Otherwise, lower to two 32-bit copies.
Dan Gohman475871a2008-07-27 21:46:04 +00006935 SDValue LoAddr = Ld->getBasePtr();
6936 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006937 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006938
Dan Gohman475871a2008-07-27 21:46:04 +00006939 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006940 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6941 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006942 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006943 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6944 Ld->isVolatile(),
6945 MinAlign(Ld->getAlignment(), 4));
6946
Dan Gohman475871a2008-07-27 21:46:04 +00006947 SDValue NewChain = LoLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006948 if (TokenFactorIndex != -1) {
6949 Ops.push_back(LoLd);
6950 Ops.push_back(HiLd);
6951 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6952 Ops.size());
6953 }
6954
6955 LoAddr = St->getBasePtr();
6956 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006957 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006958
Dan Gohman475871a2008-07-27 21:46:04 +00006959 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00006960 St->getSrcValue(), St->getSrcValueOffset(),
6961 St->isVolatile(), St->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006962 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif327ef032008-08-28 23:19:51 +00006963 St->getSrcValue(),
6964 St->getSrcValueOffset() + 4,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006965 St->isVolatile(),
6966 MinAlign(St->getAlignment(), 4));
6967 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00006968 }
Chris Lattner149a4e52008-02-22 02:09:43 +00006969 }
Dan Gohman475871a2008-07-27 21:46:04 +00006970 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00006971}
6972
Chris Lattner6cf73262008-01-25 06:14:17 +00006973/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6974/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006975static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00006976 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6977 // F[X]OR(0.0, x) -> x
6978 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00006979 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6980 if (C->getValueAPF().isPosZero())
6981 return N->getOperand(1);
6982 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6983 if (C->getValueAPF().isPosZero())
6984 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00006985 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00006986}
6987
6988/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006989static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00006990 // FAND(0.0, x) -> 0.0
6991 // FAND(x, 0.0) -> 0.0
6992 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6993 if (C->getValueAPF().isPosZero())
6994 return N->getOperand(0);
6995 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6996 if (C->getValueAPF().isPosZero())
6997 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006998 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00006999}
7000
Chris Lattner83e6c992006-10-04 06:57:07 +00007001
Dan Gohman475871a2008-07-27 21:46:04 +00007002SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00007003 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007004 SelectionDAG &DAG = DCI.DAG;
7005 switch (N->getOpcode()) {
7006 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00007007 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7008 case ISD::BUILD_VECTOR:
7009 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00007010 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00007011 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00007012 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00007013 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7014 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007015 }
7016
Dan Gohman475871a2008-07-27 21:46:04 +00007017 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007018}
7019
Evan Cheng60c07e12006-07-05 22:17:51 +00007020//===----------------------------------------------------------------------===//
7021// X86 Inline Assembly Support
7022//===----------------------------------------------------------------------===//
7023
Chris Lattnerf4dff842006-07-11 02:54:03 +00007024/// getConstraintType - Given a constraint letter, return the type of
7025/// constraint it is for this target.
7026X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007027X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7028 if (Constraint.size() == 1) {
7029 switch (Constraint[0]) {
7030 case 'A':
Chris Lattnerfce84ac2008-03-11 19:06:29 +00007031 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00007032 case 'r':
7033 case 'R':
7034 case 'l':
7035 case 'q':
7036 case 'Q':
7037 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00007038 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00007039 case 'Y':
7040 return C_RegisterClass;
7041 default:
7042 break;
7043 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00007044 }
Chris Lattner4234f572007-03-25 02:14:49 +00007045 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00007046}
7047
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007048/// LowerXConstraint - try to replace an X constraint, which matches anything,
7049/// with another that has more specific requirements based on the type of the
7050/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00007051const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00007052LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00007053 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7054 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007055 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007056 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00007057 return "Y";
7058 if (Subtarget->hasSSE1())
7059 return "x";
7060 }
7061
7062 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007063}
7064
Chris Lattner48884cd2007-08-25 00:47:38 +00007065/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7066/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00007067void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00007068 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00007069 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00007070 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007071 SDValue Result(0, 0);
Chris Lattner48884cd2007-08-25 00:47:38 +00007072
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007073 switch (Constraint) {
7074 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00007075 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00007076 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007077 if (C->getZExtValue() <= 31) {
7078 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00007079 break;
7080 }
Devang Patel84f7fd22007-03-17 00:13:28 +00007081 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007082 return;
Evan Cheng364091e2008-09-22 23:57:37 +00007083 case 'J':
7084 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7085 if (C->getZExtValue() <= 63) {
7086 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7087 break;
7088 }
7089 }
7090 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00007091 case 'N':
7092 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007093 if (C->getZExtValue() <= 255) {
7094 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00007095 break;
7096 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00007097 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007098 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00007099 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007100 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00007101 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007102 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00007103 break;
7104 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007105
Chris Lattnerdc43a882007-05-03 16:52:29 +00007106 // If we are in non-pic codegen mode, we allow the address of a global (with
7107 // an optional displacement) to be used with 'i'.
7108 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7109 int64_t Offset = 0;
7110
7111 // Match either (GA) or (GA+C)
7112 if (GA) {
7113 Offset = GA->getOffset();
7114 } else if (Op.getOpcode() == ISD::ADD) {
7115 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7116 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7117 if (C && GA) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007118 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00007119 } else {
7120 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7121 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7122 if (C && GA)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007123 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00007124 else
7125 C = 0, GA = 0;
7126 }
7127 }
7128
7129 if (GA) {
7130 // If addressing this global requires a load (e.g. in PIC mode), we can't
7131 // match.
7132 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7133 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00007134 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007135
Chris Lattnerdc43a882007-05-03 16:52:29 +00007136 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7137 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00007138 Result = Op;
7139 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007140 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007141
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007142 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00007143 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007144 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00007145 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007146
Gabor Greifba36cb52008-08-28 21:40:38 +00007147 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00007148 Ops.push_back(Result);
7149 return;
7150 }
7151 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007152}
7153
Chris Lattner259e97c2006-01-31 19:43:35 +00007154std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00007155getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007156 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00007157 if (Constraint.size() == 1) {
7158 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00007159 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00007160 default: break; // Unknown constraint letter
7161 case 'A': // EAX/EDX
7162 if (VT == MVT::i32 || VT == MVT::i64)
7163 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7164 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00007165 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7166 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00007167 if (VT == MVT::i32)
7168 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7169 else if (VT == MVT::i16)
7170 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7171 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00007172 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00007173 else if (VT == MVT::i64)
7174 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7175 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00007176 }
7177 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007178
Chris Lattner1efa40f2006-02-22 00:56:39 +00007179 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00007180}
Chris Lattnerf76d1802006-07-31 23:26:50 +00007181
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007182std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00007183X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007184 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00007185 // First, see if this is a constraint that directly corresponds to an LLVM
7186 // register class.
7187 if (Constraint.size() == 1) {
7188 // GCC Constraint Letters
7189 switch (Constraint[0]) {
7190 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00007191 case 'r': // GENERAL_REGS
7192 case 'R': // LEGACY_REGS
7193 case 'l': // INDEX_REGS
7194 if (VT == MVT::i64 && Subtarget->is64Bit())
7195 return std::make_pair(0U, X86::GR64RegisterClass);
7196 if (VT == MVT::i32)
7197 return std::make_pair(0U, X86::GR32RegisterClass);
7198 else if (VT == MVT::i16)
7199 return std::make_pair(0U, X86::GR16RegisterClass);
7200 else if (VT == MVT::i8)
7201 return std::make_pair(0U, X86::GR8RegisterClass);
7202 break;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00007203 case 'f': // FP Stack registers.
7204 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7205 // value to the correct fpstack register class.
7206 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7207 return std::make_pair(0U, X86::RFP32RegisterClass);
7208 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7209 return std::make_pair(0U, X86::RFP64RegisterClass);
7210 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00007211 case 'y': // MMX_REGS if MMX allowed.
7212 if (!Subtarget->hasMMX()) break;
7213 return std::make_pair(0U, X86::VR64RegisterClass);
7214 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00007215 case 'Y': // SSE_REGS if SSE2 allowed
7216 if (!Subtarget->hasSSE2()) break;
7217 // FALL THROUGH.
7218 case 'x': // SSE_REGS if SSE1 allowed
7219 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007220
7221 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00007222 default: break;
7223 // Scalar SSE types.
7224 case MVT::f32:
7225 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00007226 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007227 case MVT::f64:
7228 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00007229 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007230 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00007231 case MVT::v16i8:
7232 case MVT::v8i16:
7233 case MVT::v4i32:
7234 case MVT::v2i64:
7235 case MVT::v4f32:
7236 case MVT::v2f64:
7237 return std::make_pair(0U, X86::VR128RegisterClass);
7238 }
Chris Lattnerad043e82007-04-09 05:11:28 +00007239 break;
7240 }
7241 }
7242
Chris Lattnerf76d1802006-07-31 23:26:50 +00007243 // Use the default implementation in TargetLowering to convert the register
7244 // constraint into a member of a register class.
7245 std::pair<unsigned, const TargetRegisterClass*> Res;
7246 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00007247
7248 // Not found as a standard register?
7249 if (Res.second == 0) {
7250 // GCC calls "st(0)" just plain "st".
7251 if (StringsEqualNoCase("{st}", Constraint)) {
7252 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00007253 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00007254 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007255
Chris Lattner1a60aa72006-10-31 19:42:44 +00007256 return Res;
7257 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007258
Chris Lattnerf76d1802006-07-31 23:26:50 +00007259 // Otherwise, check to see if this is a register class of the wrong value
7260 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7261 // turn into {ax},{dx}.
7262 if (Res.second->hasType(VT))
7263 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007264
Chris Lattnerf76d1802006-07-31 23:26:50 +00007265 // All of the single-register GCC register classes map their values onto
7266 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7267 // really want an 8-bit or 32-bit register, map to the appropriate register
7268 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00007269 if (Res.second == X86::GR16RegisterClass) {
7270 if (VT == MVT::i8) {
7271 unsigned DestReg = 0;
7272 switch (Res.first) {
7273 default: break;
7274 case X86::AX: DestReg = X86::AL; break;
7275 case X86::DX: DestReg = X86::DL; break;
7276 case X86::CX: DestReg = X86::CL; break;
7277 case X86::BX: DestReg = X86::BL; break;
7278 }
7279 if (DestReg) {
7280 Res.first = DestReg;
7281 Res.second = Res.second = X86::GR8RegisterClass;
7282 }
7283 } else if (VT == MVT::i32) {
7284 unsigned DestReg = 0;
7285 switch (Res.first) {
7286 default: break;
7287 case X86::AX: DestReg = X86::EAX; break;
7288 case X86::DX: DestReg = X86::EDX; break;
7289 case X86::CX: DestReg = X86::ECX; break;
7290 case X86::BX: DestReg = X86::EBX; break;
7291 case X86::SI: DestReg = X86::ESI; break;
7292 case X86::DI: DestReg = X86::EDI; break;
7293 case X86::BP: DestReg = X86::EBP; break;
7294 case X86::SP: DestReg = X86::ESP; break;
7295 }
7296 if (DestReg) {
7297 Res.first = DestReg;
7298 Res.second = Res.second = X86::GR32RegisterClass;
7299 }
7300 } else if (VT == MVT::i64) {
7301 unsigned DestReg = 0;
7302 switch (Res.first) {
7303 default: break;
7304 case X86::AX: DestReg = X86::RAX; break;
7305 case X86::DX: DestReg = X86::RDX; break;
7306 case X86::CX: DestReg = X86::RCX; break;
7307 case X86::BX: DestReg = X86::RBX; break;
7308 case X86::SI: DestReg = X86::RSI; break;
7309 case X86::DI: DestReg = X86::RDI; break;
7310 case X86::BP: DestReg = X86::RBP; break;
7311 case X86::SP: DestReg = X86::RSP; break;
7312 }
7313 if (DestReg) {
7314 Res.first = DestReg;
7315 Res.second = Res.second = X86::GR64RegisterClass;
7316 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00007317 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00007318 } else if (Res.second == X86::FR32RegisterClass ||
7319 Res.second == X86::FR64RegisterClass ||
7320 Res.second == X86::VR128RegisterClass) {
7321 // Handle references to XMM physical registers that got mapped into the
7322 // wrong class. This can happen with constraints like {xmm0} where the
7323 // target independent register mapper will just pick the first match it can
7324 // find, ignoring the required type.
7325 if (VT == MVT::f32)
7326 Res.second = X86::FR32RegisterClass;
7327 else if (VT == MVT::f64)
7328 Res.second = X86::FR64RegisterClass;
7329 else if (X86::VR128RegisterClass->hasType(VT))
7330 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00007331 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007332
Chris Lattnerf76d1802006-07-31 23:26:50 +00007333 return Res;
7334}