blob: b89eef0fb214493210c7bf6f20f55a0d02d0f370 [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"
Mon P Wang3c81d352008-11-23 04:37:22 +000041#include "llvm/Support/CommandLine.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042using namespace llvm;
43
Mon P Wang3c81d352008-11-23 04:37:22 +000044static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000045DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000046
Evan Cheng10e86422008-04-25 19:11:04 +000047// Forward declarations.
Nate Begeman9008ca62009-04-27 18:41:29 +000048static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
49 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000050
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000051X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000052 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000053 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000054 X86ScalarSSEf64 = Subtarget->hasSSE2();
55 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000056 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000057
Anton Korobeynikov2365f512007-07-14 14:06:15 +000058 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000059 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000060
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000061 // Set up the TargetLowering object.
62
63 // X86 is weird, it always uses i8 for shift amounts and setcc results.
64 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000065 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000066 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000067 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000068 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000069
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000070 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000071 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 setUseUnderscoreSetJmp(false);
73 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000074 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000075 // MS runtime is weird: it exports _setjmp, but longjmp!
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(false);
78 } else {
79 setUseUnderscoreSetJmp(true);
80 setUseUnderscoreLongJmp(true);
81 }
Scott Michelfdc40a02009-02-17 22:15:04 +000082
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000083 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000084 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
85 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
86 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000087 if (Subtarget->is64Bit())
88 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000089
Evan Cheng03294662008-10-14 21:26:46 +000090 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000091
Scott Michelfdc40a02009-02-17 22:15:04 +000092 // We don't accept any truncstore of integer registers.
Chris Lattnerddf89562008-01-17 19:59:44 +000093 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
94 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
95 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
96 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
97 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +000098 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
99
100 // SETOEQ and SETUNE require checking two conditions.
101 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
102 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
103 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
104 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
105 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
106 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000107
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000108 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
109 // operation.
110 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
111 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
112 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000113
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000115 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000116 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000117 } else if (!UseSoftFloat) {
118 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000119 // We have an impenetrably clever algorithm for ui64->double only.
120 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000121 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000122 // We have an algorithm for SSE2, and we turn this into a 64-bit
123 // FILD for other targets.
124 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000125 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000126
127 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
128 // this operation.
129 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
130 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000131
132 if (!UseSoftFloat && !NoImplicitFloat) {
133 // SSE has no i16 to fp conversion, only i32
134 if (X86ScalarSSEf32) {
135 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
136 // f32 and f64 cases are Legal, f80 case is not
137 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
138 } else {
139 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
140 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
141 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000142 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000143 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
144 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000145 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000146
Dale Johannesen73328d12007-09-19 23:55:34 +0000147 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
148 // are Legal, f80 is custom lowered.
149 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
150 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000151
Evan Cheng02568ff2006-01-30 22:13:22 +0000152 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
153 // this operation.
154 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
155 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
156
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000158 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000159 // f32 and f64 cases are Legal, f80 case is not
160 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000161 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000162 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000163 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000164 }
165
166 // Handle FP_TO_UINT by promoting the destination to a larger signed
167 // conversion.
168 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
169 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
170 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
171
Evan Cheng25ab6902006-09-08 06:48:29 +0000172 if (Subtarget->is64Bit()) {
173 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000174 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000175 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000176 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000177 // Expand FP_TO_UINT into a select.
178 // FIXME: We would like to use a Custom expander here eventually to do
179 // the optimal thing for SSE vs. the default expansion in the legalizer.
180 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
181 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000182 // With SSE3 we can use fisttpll to convert to a signed i64; without
183 // SSE, we're stuck with a fistpll.
184 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000185 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000186
Chris Lattner399610a2006-12-05 18:22:22 +0000187 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000188 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000189 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
190 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
191 }
Chris Lattner21f66852005-12-23 05:15:23 +0000192
Dan Gohmanb00ee212008-02-18 19:34:53 +0000193 // Scalar integer divide and remainder are lowered to use operations that
194 // produce two results, to match the available instructions. This exposes
195 // the two-result form to trivial CSE, which is able to combine x/y and x%y
196 // into a single instruction.
197 //
198 // Scalar integer multiply-high is also lowered to use two-result
199 // operations, to match the available instructions. However, plain multiply
200 // (low) operations are left as Legal, as there are single-result
201 // instructions for this in x86. Using the two-result multiply instructions
202 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000203 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
204 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
205 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
206 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
207 setOperationAction(ISD::SREM , MVT::i8 , Expand);
208 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000209 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
210 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
211 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
212 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
213 setOperationAction(ISD::SREM , MVT::i16 , Expand);
214 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000215 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
216 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
217 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
218 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
219 setOperationAction(ISD::SREM , MVT::i32 , Expand);
220 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000221 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
222 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
223 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
224 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
225 setOperationAction(ISD::SREM , MVT::i64 , Expand);
226 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000227
Evan Chengc35497f2006-10-30 08:02:39 +0000228 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000229 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000230 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
231 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000233 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
234 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
235 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
237 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000238 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000239 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000240 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000241 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000242
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000243 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000246 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000247 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
248 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000249 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000250 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
251 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000252 if (Subtarget->is64Bit()) {
253 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000254 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
255 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 }
257
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000258 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000259 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000260
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000261 // These should be promoted to a larger select which is supported.
262 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
263 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000264 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000265 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
266 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
267 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
268 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000269 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000270 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
271 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
272 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
273 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
274 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000275 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000276 if (Subtarget->is64Bit()) {
277 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
278 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
279 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000280 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000281 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000282 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000283
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000284 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000285 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000286 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000288 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000289 if (Subtarget->is64Bit())
290 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000291 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000292 if (Subtarget->is64Bit()) {
293 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
294 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
295 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000296 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000297 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000298 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000299 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
300 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
301 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000302 if (Subtarget->is64Bit()) {
303 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
304 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
305 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
306 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000307
Evan Chengd2cde682008-03-10 19:38:10 +0000308 if (Subtarget->hasSSE1())
309 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000310
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000311 if (!Subtarget->hasSSE2())
312 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
313
Mon P Wang63307c32008-05-05 19:05:59 +0000314 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000315 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
316 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
317 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
318 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000319
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000320 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
322 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
323 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000324
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000325 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000326 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
327 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
328 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
329 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
330 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
331 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
332 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000333 }
334
Dan Gohman7f460202008-06-30 20:59:49 +0000335 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
336 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000337 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000338 if (!Subtarget->isTargetDarwin() &&
339 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000340 !Subtarget->isTargetCygMing()) {
341 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
342 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
343 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000344
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000345 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
346 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
347 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
348 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
349 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000350 setExceptionPointerRegister(X86::RAX);
351 setExceptionSelectorRegister(X86::RDX);
352 } else {
353 setExceptionPointerRegister(X86::EAX);
354 setExceptionSelectorRegister(X86::EDX);
355 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000356 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000357 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
358
Duncan Sandsf7331b32007-09-11 14:10:23 +0000359 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000360
Chris Lattnerda68d302008-01-15 21:58:22 +0000361 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000362
Nate Begemanacc398c2006-01-25 18:21:52 +0000363 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
364 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000365 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000366 if (Subtarget->is64Bit()) {
367 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000368 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000369 } else {
370 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000371 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000372 }
Evan Chengae642192007-03-02 23:16:35 +0000373
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000374 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000375 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000376 if (Subtarget->is64Bit())
377 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000378 if (Subtarget->isTargetCygMing())
379 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
380 else
381 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000382
Evan Chengc7ce29b2009-02-13 22:36:38 +0000383 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000384 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000385 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000386 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
387 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000388
Evan Cheng223547a2006-01-31 22:28:30 +0000389 // Use ANDPD to simulate FABS.
390 setOperationAction(ISD::FABS , MVT::f64, Custom);
391 setOperationAction(ISD::FABS , MVT::f32, Custom);
392
393 // Use XORP to simulate FNEG.
394 setOperationAction(ISD::FNEG , MVT::f64, Custom);
395 setOperationAction(ISD::FNEG , MVT::f32, Custom);
396
Evan Cheng68c47cb2007-01-05 07:55:56 +0000397 // Use ANDPD and ORPD to simulate FCOPYSIGN.
398 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
399 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
400
Evan Chengd25e9e82006-02-02 00:28:23 +0000401 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000402 setOperationAction(ISD::FSIN , MVT::f64, Expand);
403 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000404 setOperationAction(ISD::FSIN , MVT::f32, Expand);
405 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000406
Chris Lattnera54aa942006-01-29 06:26:08 +0000407 // Expand FP immediates into loads from the stack, except for the special
408 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000409 addLegalFPImmediate(APFloat(+0.0)); // xorpd
410 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000411 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000412 // Use SSE for f32, x87 for f64.
413 // Set up the FP register classes.
414 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
415 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
416
417 // Use ANDPS to simulate FABS.
418 setOperationAction(ISD::FABS , MVT::f32, Custom);
419
420 // Use XORP to simulate FNEG.
421 setOperationAction(ISD::FNEG , MVT::f32, Custom);
422
423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
424
425 // Use ANDPS and ORPS to simulate FCOPYSIGN.
426 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
427 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
428
429 // We don't support sin/cos/fmod
430 setOperationAction(ISD::FSIN , MVT::f32, Expand);
431 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000432
Nate Begemane1795842008-02-14 08:57:00 +0000433 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000434 addLegalFPImmediate(APFloat(+0.0f)); // xorps
435 addLegalFPImmediate(APFloat(+0.0)); // FLD0
436 addLegalFPImmediate(APFloat(+1.0)); // FLD1
437 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
438 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
439
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000440 if (!UnsafeFPMath) {
441 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
442 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
443 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000444 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000445 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000446 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000447 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
448 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000449
Evan Cheng68c47cb2007-01-05 07:55:56 +0000450 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000451 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000452 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
453 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000454
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000455 if (!UnsafeFPMath) {
456 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
457 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
458 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000459 addLegalFPImmediate(APFloat(+0.0)); // FLD0
460 addLegalFPImmediate(APFloat(+1.0)); // FLD1
461 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
462 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000463 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
464 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
465 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
466 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000467 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000468
Dale Johannesen59a58732007-08-05 18:49:15 +0000469 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000470 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000471 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
472 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
473 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
474 {
475 bool ignored;
476 APFloat TmpFlt(+0.0);
477 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
478 &ignored);
479 addLegalFPImmediate(TmpFlt); // FLD0
480 TmpFlt.changeSign();
481 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
482 APFloat TmpFlt2(+1.0);
483 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
484 &ignored);
485 addLegalFPImmediate(TmpFlt2); // FLD1
486 TmpFlt2.changeSign();
487 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
488 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000489
Evan Chengc7ce29b2009-02-13 22:36:38 +0000490 if (!UnsafeFPMath) {
491 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
492 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
493 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000494 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000495
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000496 // Always use a library call for pow.
497 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
498 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
499 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
500
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000501 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000502 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000503 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000504 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000505 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
506
Mon P Wangf007a8b2008-11-06 05:31:54 +0000507 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000508 // (for widening) or expand (for scalarization). Then we will selectively
509 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000510 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
511 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000512 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000525 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Eli Friedman108b5192009-05-23 22:44:52 +0000527 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000528 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000529 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
544 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000551 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000556 }
557
Evan Chengc7ce29b2009-02-13 22:36:38 +0000558 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
559 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000560 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000561 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
562 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
563 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000564 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000565 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000566
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000567 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
568 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
569 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000570 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000571
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000572 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
573 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
574 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000575 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000576
Bill Wendling74027e92007-03-15 21:24:36 +0000577 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
578 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
579
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000580 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000581 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000582 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000583 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
584 setOperationAction(ISD::AND, MVT::v2i32, Promote);
585 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
586 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000587
588 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000589 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000590 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000591 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
592 setOperationAction(ISD::OR, MVT::v2i32, Promote);
593 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
594 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000595
596 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000597 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000598 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000599 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
600 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
601 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
602 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000603
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000604 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000605 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000606 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000607 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
608 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
609 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000610 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
611 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000612 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000613
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000614 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
615 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
616 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000617 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000618 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000619
620 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
621 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
622 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000623 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000624
Evan Cheng52672b82008-07-22 18:39:19 +0000625 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000626 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
627 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000628 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000629
630 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000631
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000632 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000633 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
634 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
635 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
636 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
637 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000638 }
639
Evan Cheng92722532009-03-26 23:06:32 +0000640 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000641 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
642
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000643 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
644 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
645 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
646 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000647 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
648 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000649 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
650 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
651 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000652 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000653 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000654 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000655 }
656
Evan Cheng92722532009-03-26 23:06:32 +0000657 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000658 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000659
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000660 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
661 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000662 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
663 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
664 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
665 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
666
Evan Chengf7c378e2006-04-10 07:23:14 +0000667 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
668 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
669 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000670 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000671 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000672 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
673 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
674 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000675 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000676 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000677 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
678 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
679 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
680 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000681 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
682 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000683
Nate Begeman30a0de92008-07-17 16:51:19 +0000684 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
685 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
686 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
687 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000688
Evan Chengf7c378e2006-04-10 07:23:14 +0000689 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
690 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000691 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000692 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000693 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000694
Evan Cheng2c3ae372006-04-12 21:21:57 +0000695 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000696 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
697 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000698 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000699 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000700 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000701 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
702 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
703 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000704 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000705
Evan Cheng2c3ae372006-04-12 21:21:57 +0000706 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
707 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
708 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
709 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000710 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000711 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000712
Nate Begemancdd1eec2008-02-12 22:51:28 +0000713 if (Subtarget->is64Bit()) {
714 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000715 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000716 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000717
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000718 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000720 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
721 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
722 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
723 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
724 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
725 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
726 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
727 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
728 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
729 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000730 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000731
Chris Lattnerddf89562008-01-17 19:59:44 +0000732 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000733
Evan Cheng2c3ae372006-04-12 21:21:57 +0000734 // Custom lower v2i64 and v2f64 selects.
735 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000736 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000737 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000738 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000739
Evan Cheng470a6ad2006-02-22 02:26:30 +0000740 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000741
Nate Begeman14d12ca2008-02-11 04:19:36 +0000742 if (Subtarget->hasSSE41()) {
743 // FIXME: Do we need to handle scalar-to-vector here?
744 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
745
746 // i8 and i16 vectors are custom , because the source register and source
747 // source memory operand types are not the same width. f32 vectors are
748 // custom since the immediate controlling the insert encodes additional
749 // information.
750 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
751 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000752 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000753 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
754
755 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
756 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000757 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000758 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000759
760 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000761 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
762 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000763 }
764 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000765
Nate Begeman30a0de92008-07-17 16:51:19 +0000766 if (Subtarget->hasSSE42()) {
767 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
768 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000769
Evan Cheng6be2c582006-04-05 23:38:46 +0000770 // We want to custom lower some of our intrinsics.
771 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
772
Bill Wendling74c37652008-12-09 22:08:41 +0000773 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000774 setOperationAction(ISD::SADDO, MVT::i32, Custom);
775 setOperationAction(ISD::SADDO, MVT::i64, Custom);
776 setOperationAction(ISD::UADDO, MVT::i32, Custom);
777 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000778 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
779 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
780 setOperationAction(ISD::USUBO, MVT::i32, Custom);
781 setOperationAction(ISD::USUBO, MVT::i64, Custom);
782 setOperationAction(ISD::SMULO, MVT::i32, Custom);
783 setOperationAction(ISD::SMULO, MVT::i64, Custom);
784 setOperationAction(ISD::UMULO, MVT::i32, Custom);
785 setOperationAction(ISD::UMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000786
Evan Chengd54f2d52009-03-31 19:38:51 +0000787 if (!Subtarget->is64Bit()) {
788 // These libcalls are not available in 32-bit.
789 setLibcallName(RTLIB::SHL_I128, 0);
790 setLibcallName(RTLIB::SRL_I128, 0);
791 setLibcallName(RTLIB::SRA_I128, 0);
792 }
793
Evan Cheng206ee9d2006-07-07 08:33:52 +0000794 // We have target-specific dag combine patterns for the following nodes:
795 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000796 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000797 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000798 setTargetDAGCombine(ISD::SHL);
799 setTargetDAGCombine(ISD::SRA);
800 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000801 setTargetDAGCombine(ISD::STORE);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000802 if (Subtarget->is64Bit())
803 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000804
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000805 computeRegisterProperties();
806
Evan Cheng87ed7162006-02-14 08:25:08 +0000807 // FIXME: These should be based on subtarget info. Plus, the values should
808 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000809 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
810 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
811 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000812 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000813 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000814 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000815}
816
Scott Michel5b8f82e2008-03-10 15:42:14 +0000817
Duncan Sands5480c042009-01-01 15:52:00 +0000818MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000819 return MVT::i8;
820}
821
822
Evan Cheng29286502008-01-23 23:17:41 +0000823/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
824/// the desired ByVal argument alignment.
825static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
826 if (MaxAlign == 16)
827 return;
828 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
829 if (VTy->getBitWidth() == 128)
830 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000831 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
832 unsigned EltAlign = 0;
833 getMaxByValAlign(ATy->getElementType(), EltAlign);
834 if (EltAlign > MaxAlign)
835 MaxAlign = EltAlign;
836 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
837 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
838 unsigned EltAlign = 0;
839 getMaxByValAlign(STy->getElementType(i), EltAlign);
840 if (EltAlign > MaxAlign)
841 MaxAlign = EltAlign;
842 if (MaxAlign == 16)
843 break;
844 }
845 }
846 return;
847}
848
849/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
850/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000851/// that contain SSE vectors are placed at 16-byte boundaries while the rest
852/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000853unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000854 if (Subtarget->is64Bit()) {
855 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000856 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000857 if (TyAlign > 8)
858 return TyAlign;
859 return 8;
860 }
861
Evan Cheng29286502008-01-23 23:17:41 +0000862 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000863 if (Subtarget->hasSSE1())
864 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000865 return Align;
866}
Chris Lattner2b02a442007-02-25 08:29:00 +0000867
Evan Chengf0df0312008-05-15 08:39:06 +0000868/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000869/// and store operations as a result of memset, memcpy, and memmove
870/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000871/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000872MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000873X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
874 bool isSrcConst, bool isSrcStr) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000875 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
876 // linux. This is because the stack realignment code can't handle certain
877 // cases like PR2962. This should be removed when PR2962 is fixed.
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000878 if (!NoImplicitFloat && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000879 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
880 return MVT::v4i32;
881 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
882 return MVT::v4f32;
883 }
Evan Chengf0df0312008-05-15 08:39:06 +0000884 if (Subtarget->is64Bit() && Size >= 8)
885 return MVT::i64;
886 return MVT::i32;
887}
888
Evan Chengcc415862007-11-09 01:32:10 +0000889/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
890/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000891SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000892 SelectionDAG &DAG) const {
893 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000894 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000895 if (!Subtarget->isPICStyleRIPRel())
Dale Johannesenb300d2a2009-02-07 00:55:49 +0000896 // This doesn't have DebugLoc associated with it, but is not really the
897 // same as a Register.
898 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
899 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +0000900 return Table;
901}
902
Chris Lattner2b02a442007-02-25 08:29:00 +0000903//===----------------------------------------------------------------------===//
904// Return Value Calling Convention Implementation
905//===----------------------------------------------------------------------===//
906
Chris Lattner59ed56b2007-02-28 04:55:35 +0000907#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000908
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000909/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000910SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +0000911 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000912 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
Scott Michelfdc40a02009-02-17 22:15:04 +0000913
Chris Lattner9774c912007-02-27 05:28:59 +0000914 SmallVector<CCValAssign, 16> RVLocs;
915 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000916 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
917 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000918 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +0000919
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000920 // If this is the first return lowered for this function, add the regs to the
921 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000922 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000923 for (unsigned i = 0; i != RVLocs.size(); ++i)
924 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000925 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000926 }
Dan Gohman475871a2008-07-27 21:46:04 +0000927 SDValue Chain = Op.getOperand(0);
Scott Michelfdc40a02009-02-17 22:15:04 +0000928
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000929 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000930 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000931 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000932 SDValue TailCall = Chain;
933 SDValue TargetAddress = TailCall.getOperand(1);
934 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000935 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000936 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000937 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000938 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Scott Michelfdc40a02009-02-17 22:15:04 +0000939 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000940 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000941 assert(StackAdjustment.getOpcode() == ISD::Constant &&
942 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000943
Dan Gohman475871a2008-07-27 21:46:04 +0000944 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000945 Operands.push_back(Chain.getOperand(0));
946 Operands.push_back(TargetAddress);
947 Operands.push_back(StackAdjustment);
948 // Copy registers used by the call. Last operand is a flag so it is not
949 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000950 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000951 Operands.push_back(Chain.getOperand(i));
952 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000953 return DAG.getNode(X86ISD::TC_RETURN, dl, MVT::Other, &Operands[0],
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000954 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000955 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000956
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000957 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000958 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000959
Dan Gohman475871a2008-07-27 21:46:04 +0000960 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000961 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
962 // Operand #1 = Bytes To Pop
963 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +0000964
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000965 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000966 for (unsigned i = 0; i != RVLocs.size(); ++i) {
967 CCValAssign &VA = RVLocs[i];
968 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000969 SDValue ValToCopy = Op.getOperand(i*2+1);
Scott Michelfdc40a02009-02-17 22:15:04 +0000970
Chris Lattner447ff682008-03-11 03:23:40 +0000971 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
972 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +0000973 if (VA.getLocReg() == X86::ST0 ||
974 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +0000975 // If this is a copy from an xmm register to ST(0), use an FPExtend to
976 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +0000977 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +0000978 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +0000979 RetOps.push_back(ValToCopy);
980 // Don't emit a copytoreg.
981 continue;
982 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000983
Evan Cheng242b38b2009-02-23 09:03:22 +0000984 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
985 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +0000986 if (Subtarget->is64Bit()) {
987 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +0000988 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +0000989 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +0000990 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
991 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
992 }
Evan Cheng6140a8b2009-02-22 08:05:12 +0000993 }
994
Dale Johannesendd64c412009-02-04 00:33:20 +0000995 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000996 Flag = Chain.getValue(1);
997 }
Dan Gohman61a92132008-04-21 23:59:07 +0000998
999 // The x86-64 ABI for returning structs by value requires that we copy
1000 // the sret argument into %rax for the return. We saved the argument into
1001 // a virtual register in the entry block, so now we copy the value out
1002 // and into %rax.
1003 if (Subtarget->is64Bit() &&
1004 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1005 MachineFunction &MF = DAG.getMachineFunction();
1006 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1007 unsigned Reg = FuncInfo->getSRetReturnReg();
1008 if (!Reg) {
1009 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1010 FuncInfo->setSRetReturnReg(Reg);
1011 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001012 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001013
Dale Johannesendd64c412009-02-04 00:33:20 +00001014 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001015 Flag = Chain.getValue(1);
1016 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001017
Chris Lattner447ff682008-03-11 03:23:40 +00001018 RetOps[0] = Chain; // Update chain.
1019
1020 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001021 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001022 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001023
1024 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001025 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001026}
1027
1028
Chris Lattner3085e152007-02-25 08:59:22 +00001029/// LowerCallResult - Lower the result values of an ISD::CALL into the
1030/// appropriate copies out of appropriate physical registers. This assumes that
1031/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
1032/// being lowered. The returns a SDNode with the same number of values as the
1033/// ISD::CALL.
1034SDNode *X86TargetLowering::
Scott Michelfdc40a02009-02-17 22:15:04 +00001035LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001036 unsigned CallingConv, SelectionDAG &DAG) {
Dale Johannesenace16102009-02-03 19:33:06 +00001037
Scott Michelfdc40a02009-02-17 22:15:04 +00001038 DebugLoc dl = TheCall->getDebugLoc();
Chris Lattnere32bbf62007-02-28 07:09:55 +00001039 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001040 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001041 bool isVarArg = TheCall->isVarArg();
Torok Edwin3f142c32009-02-01 18:15:56 +00001042 bool Is64Bit = Subtarget->is64Bit();
Chris Lattner52387be2007-06-19 00:13:10 +00001043 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001044 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1045
Dan Gohman475871a2008-07-27 21:46:04 +00001046 SmallVector<SDValue, 8> ResultVals;
Scott Michelfdc40a02009-02-17 22:15:04 +00001047
Chris Lattner3085e152007-02-25 08:59:22 +00001048 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001049 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001050 CCValAssign &VA = RVLocs[i];
1051 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001052
Torok Edwin3f142c32009-02-01 18:15:56 +00001053 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001054 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001055 ((Is64Bit || TheCall->isInreg()) && !Subtarget->hasSSE1())) {
1056 cerr << "SSE register return with SSE disabled\n";
1057 exit(1);
1058 }
1059
Chris Lattner8e6da152008-03-10 21:08:41 +00001060 // If this is a call to a function that returns an fp value on the floating
1061 // point stack, but where we prefer to use the value in xmm registers, copy
1062 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Dan Gohman37eed792009-02-04 17:28:58 +00001063 if ((VA.getLocReg() == X86::ST0 ||
1064 VA.getLocReg() == X86::ST1) &&
1065 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001066 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001067 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001068
Evan Cheng79fb3b42009-02-20 20:43:02 +00001069 SDValue Val;
1070 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001071 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1072 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1073 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1074 MVT::v2i64, InFlag).getValue(1);
1075 Val = Chain.getValue(0);
1076 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1077 Val, DAG.getConstant(0, MVT::i64));
1078 } else {
1079 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1080 MVT::i64, InFlag).getValue(1);
1081 Val = Chain.getValue(0);
1082 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001083 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1084 } else {
1085 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1086 CopyVT, InFlag).getValue(1);
1087 Val = Chain.getValue(0);
1088 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001089 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001090
Dan Gohman37eed792009-02-04 17:28:58 +00001091 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001092 // Round the F80 the right size, which also moves to the appropriate xmm
1093 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001094 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001095 // This truncation won't change the value.
1096 DAG.getIntPtrConstant(1));
1097 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001098
Chris Lattner8e6da152008-03-10 21:08:41 +00001099 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001100 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001101
Chris Lattner3085e152007-02-25 08:59:22 +00001102 // Merge everything together with a MERGE_VALUES node.
1103 ResultVals.push_back(Chain);
Dale Johannesenace16102009-02-03 19:33:06 +00001104 return DAG.getNode(ISD::MERGE_VALUES, dl, TheCall->getVTList(),
1105 &ResultVals[0], ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001106}
1107
1108
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001109//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001110// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001111//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001112// StdCall calling convention seems to be standard for many Windows' API
1113// routines and around. It differs from C calling convention just a little:
1114// callee should clean up the stack, not caller. Symbols should be also
1115// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001116// For info on fast calling convention see Fast Calling Convention (tail call)
1117// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001118
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001119/// CallIsStructReturn - Determines whether a CALL node uses struct return
1120/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001121static bool CallIsStructReturn(CallSDNode *TheCall) {
1122 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001123 if (!NumOps)
1124 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001125
Dan Gohman095cc292008-09-13 01:54:27 +00001126 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001127}
1128
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001129/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1130/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001131static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001132 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001133 if (!NumArgs)
1134 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001135
1136 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001137}
1138
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001139/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1140/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001141/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001142bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001143 if (IsVarArg)
1144 return false;
1145
Dan Gohman095cc292008-09-13 01:54:27 +00001146 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001147 default:
1148 return false;
1149 case CallingConv::X86_StdCall:
1150 return !Subtarget->is64Bit();
1151 case CallingConv::X86_FastCall:
1152 return !Subtarget->is64Bit();
1153 case CallingConv::Fast:
1154 return PerformTailCallOpt;
1155 }
1156}
1157
Dan Gohman095cc292008-09-13 01:54:27 +00001158/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1159/// given CallingConvention value.
1160CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001161 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001162 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001163 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001164 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1165 return CC_X86_64_TailCall;
1166 else
1167 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001168 }
1169
Gordon Henriksen86737662008-01-05 16:56:59 +00001170 if (CC == CallingConv::X86_FastCall)
1171 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001172 else if (CC == CallingConv::Fast)
1173 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001174 else
1175 return CC_X86_32_C;
1176}
1177
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001178/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1179/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001180NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001181X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001182 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001183 if (CC == CallingConv::X86_FastCall)
1184 return FastCall;
1185 else if (CC == CallingConv::X86_StdCall)
1186 return StdCall;
1187 return None;
1188}
1189
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001190
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001191/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1192/// in a register before calling.
1193bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1194 return !IsTailCall && !Is64Bit &&
1195 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1196 Subtarget->isPICStyleGOT();
1197}
1198
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001199/// CallRequiresFnAddressInReg - Check whether the call requires the function
1200/// address to be loaded in a register.
Scott Michelfdc40a02009-02-17 22:15:04 +00001201bool
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001202X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001203 return !Is64Bit && IsTailCall &&
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001204 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1205 Subtarget->isPICStyleGOT();
1206}
1207
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001208/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1209/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001210/// the specific parameter attribute. The copy will be passed as a byval
1211/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001212static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001213CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001214 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1215 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001216 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001217 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001218 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001219}
1220
Dan Gohman475871a2008-07-27 21:46:04 +00001221SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001222 const CCValAssign &VA,
1223 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001224 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001225 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001226 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001227 ISD::ArgFlagsTy Flags =
1228 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001229 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001230 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001231
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001232 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001233 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001234 // In case of tail call optimization mark all arguments mutable. Since they
1235 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001236 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001237 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001238 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001239 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001240 return FIN;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001241 return DAG.getLoad(VA.getValVT(), Op.getDebugLoc(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001242 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001243}
1244
Dan Gohman475871a2008-07-27 21:46:04 +00001245SDValue
1246X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001247 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001248 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00001249 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00001250
Gordon Henriksen86737662008-01-05 16:56:59 +00001251 const Function* Fn = MF.getFunction();
1252 if (Fn->hasExternalLinkage() &&
1253 Subtarget->isTargetCygMing() &&
1254 Fn->getName() == "main")
1255 FuncInfo->setForceFramePointer(true);
1256
1257 // Decorate the function name.
1258 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
Scott Michelfdc40a02009-02-17 22:15:04 +00001259
Evan Cheng1bc78042006-04-26 01:20:17 +00001260 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001261 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001262 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001263 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001264 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001265 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001266
1267 assert(!(isVarArg && CC == CallingConv::Fast) &&
1268 "Var args not supported with calling convention fastcc");
1269
Chris Lattner638402b2007-02-28 07:00:42 +00001270 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001271 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001272 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001273 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001274
Dan Gohman475871a2008-07-27 21:46:04 +00001275 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001276 unsigned LastVal = ~0U;
1277 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1278 CCValAssign &VA = ArgLocs[i];
1279 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1280 // places.
1281 assert(VA.getValNo() != LastVal &&
1282 "Don't support value assigned to multiple locs yet");
1283 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001284
Chris Lattnerf39f7712007-02-28 05:46:49 +00001285 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001286 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001287 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001288 if (RegVT == MVT::i32)
1289 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001290 else if (Is64Bit && RegVT == MVT::i64)
1291 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001292 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001293 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001294 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001295 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001296 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001297 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001298 else if (RegVT.isVector()) {
1299 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001300 if (!Is64Bit)
1301 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1302 else {
1303 // Darwin calling convention passes MMX values in either GPRs or
1304 // XMMs in x86-64. Other targets pass them in memory.
1305 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1306 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1307 RegVT = MVT::v2i64;
1308 } else {
1309 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1310 RegVT = MVT::i64;
1311 }
1312 }
1313 } else {
1314 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001315 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001316
Bob Wilson998e1252009-04-20 18:36:57 +00001317 unsigned Reg = DAG.getMachineFunction().addLiveIn(VA.getLocReg(), RC);
Dale Johannesendd64c412009-02-04 00:33:20 +00001318 SDValue ArgValue = DAG.getCopyFromReg(Root, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001319
Chris Lattnerf39f7712007-02-28 05:46:49 +00001320 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1321 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1322 // right size.
1323 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001324 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001325 DAG.getValueType(VA.getValVT()));
1326 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001327 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001328 DAG.getValueType(VA.getValVT()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001329
Chris Lattnerf39f7712007-02-28 05:46:49 +00001330 if (VA.getLocInfo() != CCValAssign::Full)
Dale Johannesenace16102009-02-03 19:33:06 +00001331 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001332
Gordon Henriksen86737662008-01-05 16:56:59 +00001333 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001334 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001335 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Dale Johannesenace16102009-02-03 19:33:06 +00001336 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001337 else if (RC == X86::VR128RegisterClass) {
Dale Johannesenace16102009-02-03 19:33:06 +00001338 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1339 ArgValue, DAG.getConstant(0, MVT::i64));
1340 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getLocVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001341 }
1342 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001343
Chris Lattnerf39f7712007-02-28 05:46:49 +00001344 ArgValues.push_back(ArgValue);
1345 } else {
1346 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001347 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001348 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001349 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001350
Dan Gohman61a92132008-04-21 23:59:07 +00001351 // The x86-64 ABI for returning structs by value requires that we copy
1352 // the sret argument into %rax for the return. Save the argument into
1353 // a virtual register so that we can access it from the return points.
1354 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1355 MachineFunction &MF = DAG.getMachineFunction();
1356 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1357 unsigned Reg = FuncInfo->getSRetReturnReg();
1358 if (!Reg) {
1359 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1360 FuncInfo->setSRetReturnReg(Reg);
1361 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001362 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, ArgValues[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00001363 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Root);
Dan Gohman61a92132008-04-21 23:59:07 +00001364 }
1365
Chris Lattnerf39f7712007-02-28 05:46:49 +00001366 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001367 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001368 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001369 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001370
Evan Cheng1bc78042006-04-26 01:20:17 +00001371 // If the function takes variable number of arguments, make a frame index for
1372 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001373 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001374 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1375 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1376 }
1377 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001378 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1379
1380 // FIXME: We should really autogenerate these arrays
1381 static const unsigned GPR64ArgRegsWin64[] = {
1382 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001383 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001384 static const unsigned XMMArgRegsWin64[] = {
1385 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1386 };
1387 static const unsigned GPR64ArgRegs64Bit[] = {
1388 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1389 };
1390 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001391 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1392 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1393 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001394 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1395
1396 if (IsWin64) {
1397 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1398 GPR64ArgRegs = GPR64ArgRegsWin64;
1399 XMMArgRegs = XMMArgRegsWin64;
1400 } else {
1401 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1402 GPR64ArgRegs = GPR64ArgRegs64Bit;
1403 XMMArgRegs = XMMArgRegs64Bit;
1404 }
1405 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1406 TotalNumIntRegs);
1407 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1408 TotalNumXMMRegs);
1409
Evan Chengc7ce29b2009-02-13 22:36:38 +00001410 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001411 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001412 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloat) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001413 "SSE register cannot be used when SSE is disabled!");
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001414 if (UseSoftFloat || NoImplicitFloat || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001415 // Kernel mode asks for SSE to be disabled, so don't push them
1416 // on the stack.
1417 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001418
Gordon Henriksen86737662008-01-05 16:56:59 +00001419 // For X86-64, if there are vararg parameters that are passed via
1420 // registers, then we must store them to their spots on the stack so they
1421 // may be loaded by deferencing the result of va_next.
1422 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001423 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1424 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1425 TotalNumXMMRegs * 16, 16);
1426
Gordon Henriksen86737662008-01-05 16:56:59 +00001427 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001428 SmallVector<SDValue, 8> MemOps;
1429 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001430 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001431 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001432 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001433 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1434 X86::GR64RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001435 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001436 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001437 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001438 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001439 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001440 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001441 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001442 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001443
Gordon Henriksen86737662008-01-05 16:56:59 +00001444 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001445 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001446 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001447 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001448 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1449 X86::VR128RegisterClass);
Dale Johannesendd64c412009-02-04 00:33:20 +00001450 SDValue Val = DAG.getCopyFromReg(Root, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001451 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001452 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001453 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001454 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001455 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001456 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001457 }
1458 if (!MemOps.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001459 Root = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001460 &MemOps[0], MemOps.size());
1461 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001462 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001463
Gordon Henriksenae636f82008-01-03 16:47:34 +00001464 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001465
Gordon Henriksen86737662008-01-05 16:56:59 +00001466 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001467 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001468 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001469 BytesCallerReserves = 0;
1470 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001471 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001472 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001473 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Scott Michelfdc40a02009-02-17 22:15:04 +00001474 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001475 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001476 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001477
Gordon Henriksen86737662008-01-05 16:56:59 +00001478 if (!Is64Bit) {
1479 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1480 if (CC == CallingConv::X86_FastCall)
1481 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1482 }
Evan Cheng25caf632006-05-23 21:06:34 +00001483
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001484 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001485
Evan Cheng25caf632006-05-23 21:06:34 +00001486 // Return the new list of results.
Dale Johannesenace16102009-02-03 19:33:06 +00001487 return DAG.getNode(ISD::MERGE_VALUES, dl, Op.getNode()->getVTList(),
Duncan Sandsaaffa052008-12-01 11:41:29 +00001488 &ArgValues[0], ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001489}
1490
Dan Gohman475871a2008-07-27 21:46:04 +00001491SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001492X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001493 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001494 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001495 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001496 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dale Johannesenace16102009-02-03 19:33:06 +00001497 DebugLoc dl = TheCall->getDebugLoc();
Dan Gohman4fdad172008-02-07 16:28:05 +00001498 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001499 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001500 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001501 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001502 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001503 }
Dale Johannesenace16102009-02-03 19:33:06 +00001504 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001505 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001506}
1507
Bill Wendling64e87322009-01-16 19:25:27 +00001508/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001509/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001510SDValue
1511X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001512 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001513 SDValue Chain,
1514 bool IsTailCall,
1515 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001516 int FPDiff,
1517 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001518 if (!IsTailCall || FPDiff==0) return Chain;
1519
1520 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001521 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001522 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001523
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001524 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001525 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001526 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001527}
1528
1529/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1530/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001531static SDValue
1532EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001533 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001534 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001535 // Store the return address to the appropriate stack slot.
1536 if (!FPDiff) return Chain;
1537 // Calculate the new stack slot for the return address.
1538 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001539 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001540 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001541 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001542 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001543 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001544 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001545 return Chain;
1546}
1547
Dan Gohman475871a2008-07-27 21:46:04 +00001548SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001549 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001550 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1551 SDValue Chain = TheCall->getChain();
1552 unsigned CC = TheCall->getCallingConv();
1553 bool isVarArg = TheCall->isVarArg();
1554 bool IsTailCall = TheCall->isTailCall() &&
1555 CC == CallingConv::Fast && PerformTailCallOpt;
1556 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001557 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001558 bool IsStructRet = CallIsStructReturn(TheCall);
Dale Johannesenace16102009-02-03 19:33:06 +00001559 DebugLoc dl = TheCall->getDebugLoc();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001560
1561 assert(!(isVarArg && CC == CallingConv::Fast) &&
1562 "Var args not supported with calling convention fastcc");
1563
Chris Lattner638402b2007-02-28 07:00:42 +00001564 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001565 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001566 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001567 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Scott Michelfdc40a02009-02-17 22:15:04 +00001568
Chris Lattner423c5f42007-02-28 05:31:48 +00001569 // Get a count of how many bytes are to be pushed on the stack.
1570 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001571 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001572 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001573
Gordon Henriksen86737662008-01-05 16:56:59 +00001574 int FPDiff = 0;
1575 if (IsTailCall) {
1576 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001577 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001578 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1579 FPDiff = NumBytesCallerPushed - NumBytes;
1580
1581 // Set the delta of movement of the returnaddr stackslot.
1582 // But only set if delta is greater than previous delta.
1583 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1584 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1585 }
1586
Chris Lattnere563bbc2008-10-11 22:08:30 +00001587 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001588
Dan Gohman475871a2008-07-27 21:46:04 +00001589 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001590 // Load return adress for tail calls.
1591 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001592 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001593
Dan Gohman475871a2008-07-27 21:46:04 +00001594 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1595 SmallVector<SDValue, 8> MemOpChains;
1596 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001597
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001598 // Walk the register/memloc assignments, inserting copies/loads. In the case
1599 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001600 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1601 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001602 SDValue Arg = TheCall->getArg(i);
1603 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1604 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001605
Chris Lattner423c5f42007-02-28 05:31:48 +00001606 // Promote the value if needed.
1607 switch (VA.getLocInfo()) {
1608 default: assert(0 && "Unknown loc info!");
1609 case CCValAssign::Full: break;
1610 case CCValAssign::SExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001611 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001612 break;
1613 case CCValAssign::ZExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001614 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001615 break;
1616 case CCValAssign::AExt:
Dale Johannesenace16102009-02-03 19:33:06 +00001617 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, VA.getLocVT(), Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001618 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001619 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001620
Chris Lattner423c5f42007-02-28 05:31:48 +00001621 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001622 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001623 MVT RegVT = VA.getLocVT();
1624 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001625 switch (VA.getLocReg()) {
1626 default:
1627 break;
1628 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1629 case X86::R8: {
1630 // Special case: passing MMX values in GPR registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001631 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001632 break;
1633 }
1634 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1635 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1636 // Special case: passing MMX values in XMM registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001637 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1638 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
Nate Begeman9008ca62009-04-27 18:41:29 +00001639 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
Evan Cheng10e86422008-04-25 19:11:04 +00001640 break;
1641 }
1642 }
1643 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001644 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1645 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001646 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001647 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001648 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001649 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001650
Dan Gohman095cc292008-09-13 01:54:27 +00001651 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1652 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001653 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001654 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001655 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001656
Evan Cheng32fe1032006-05-25 00:59:30 +00001657 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001658 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001659 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001660
Evan Cheng347d5f72006-04-28 21:29:37 +00001661 // Build a sequence of copy-to-reg nodes chained together with token chain
1662 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001663 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001664 // Tail call byval lowering might overwrite argument registers so in case of
1665 // tail call optimization the copies to registers are lowered later.
1666 if (!IsTailCall)
1667 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001668 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001669 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001670 InFlag = Chain.getValue(1);
1671 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001672
Evan Chengf4684712007-02-21 21:18:14 +00001673 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Scott Michelfdc40a02009-02-17 22:15:04 +00001674 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001675 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001676 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
Scott Michelfdc40a02009-02-17 22:15:04 +00001677 DAG.getNode(X86ISD::GlobalBaseReg,
1678 DebugLoc::getUnknownLoc(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001679 getPointerTy()),
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001680 InFlag);
1681 InFlag = Chain.getValue(1);
1682 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001683 // If we are tail calling and generating PIC/GOT style code load the address
1684 // of the callee into ecx. The value in ecx is used as target of the tail
1685 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1686 // calls on PIC/GOT architectures. Normally we would just put the address of
1687 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1688 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001689 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001690 // Note: The actual moving to ecx is done further down.
1691 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001692 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001693 !G->getGlobal()->hasProtectedVisibility())
1694 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001695 else if (isa<ExternalSymbolSDNode>(Callee))
1696 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001697 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001698
Gordon Henriksen86737662008-01-05 16:56:59 +00001699 if (Is64Bit && isVarArg) {
1700 // From AMD64 ABI document:
1701 // For calls that may call functions that use varargs or stdargs
1702 // (prototype-less calls or calls to functions containing ellipsis (...) in
1703 // the declaration) %al is used as hidden argument to specify the number
1704 // of SSE registers used. The contents of %al do not need to match exactly
1705 // the number of registers, but must be an ubound on the number of SSE
1706 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001707
1708 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 // Count the number of XMM registers allocated.
1710 static const unsigned XMMArgRegs[] = {
1711 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1712 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1713 };
1714 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001715 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001716 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001717
Dale Johannesendd64c412009-02-04 00:33:20 +00001718 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001719 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1720 InFlag = Chain.getValue(1);
1721 }
1722
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001723
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001724 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001725 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001726 SmallVector<SDValue, 8> MemOpChains2;
1727 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001728 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001729 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001730 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001731 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1732 CCValAssign &VA = ArgLocs[i];
1733 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001734 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001735 SDValue Arg = TheCall->getArg(i);
1736 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001737 // Create frame index.
1738 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001739 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001740 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001741 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001742
Duncan Sands276dcbd2008-03-21 09:14:45 +00001743 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001744 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001745 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001746 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001747 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001748 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001749 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001750
1751 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001752 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001753 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001754 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001755 MemOpChains2.push_back(
Dale Johannesenace16102009-02-03 19:33:06 +00001756 DAG.getStore(Chain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001757 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001758 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001759 }
1760 }
1761
1762 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001763 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001764 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001765
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001766 // Copy arguments to their registers.
1767 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001768 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001769 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001770 InFlag = Chain.getValue(1);
1771 }
Dan Gohman475871a2008-07-27 21:46:04 +00001772 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001773
Gordon Henriksen86737662008-01-05 16:56:59 +00001774 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001775 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001776 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001777 }
1778
Evan Cheng32fe1032006-05-25 00:59:30 +00001779 // If the callee is a GlobalAddress node (quite common, every direct call is)
1780 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001781 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001782 // We should use extra load for direct calls to dllimported functions in
1783 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001784 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1785 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001786 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1787 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001788 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1789 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001790 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001791 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001792
Dale Johannesendd64c412009-02-04 00:33:20 +00001793 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001794 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001795 Callee,InFlag);
1796 Callee = DAG.getRegister(Opc, getPointerTy());
1797 // Add register as live out.
1798 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001799 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001800
Chris Lattnerd96d0722007-02-25 06:40:16 +00001801 // Returns a chain & a flag for retval copy to use.
1802 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001803 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001804
1805 if (IsTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001806 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1807 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001808 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00001809
Gordon Henriksen86737662008-01-05 16:56:59 +00001810 // Returns a chain & a flag for retval copy to use.
1811 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1812 Ops.clear();
1813 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001814
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001815 Ops.push_back(Chain);
1816 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001817
Gordon Henriksen86737662008-01-05 16:56:59 +00001818 if (IsTailCall)
1819 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001820
Gordon Henriksen86737662008-01-05 16:56:59 +00001821 // Add argument registers to the end of the list so that they are known live
1822 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001823 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1824 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1825 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001826
Evan Cheng586ccac2008-03-18 23:36:35 +00001827 // Add an implicit use GOT pointer in EBX.
1828 if (!IsTailCall && !Is64Bit &&
1829 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1830 Subtarget->isPICStyleGOT())
1831 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1832
1833 // Add an implicit use of AL for x86 vararg functions.
1834 if (Is64Bit && isVarArg)
1835 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1836
Gabor Greifba36cb52008-08-28 21:40:38 +00001837 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001838 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001839
Gordon Henriksen86737662008-01-05 16:56:59 +00001840 if (IsTailCall) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001841 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001842 "Flag must be set. Depend on flag being set in LowerRET");
Dale Johannesenace16102009-02-03 19:33:06 +00001843 Chain = DAG.getNode(X86ISD::TAILCALL, dl,
Dan Gohman095cc292008-09-13 01:54:27 +00001844 TheCall->getVTList(), &Ops[0], Ops.size());
Scott Michelfdc40a02009-02-17 22:15:04 +00001845
Gabor Greifba36cb52008-08-28 21:40:38 +00001846 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001847 }
1848
Dale Johannesenace16102009-02-03 19:33:06 +00001849 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001850 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001851
Chris Lattner2d297092006-05-23 18:50:38 +00001852 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001853 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001854 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001855 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001856 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001857 // If this is is a call to a struct-return function, the callee
1858 // pops the hidden struct pointer, so we have to push it back.
1859 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001860 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001861 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001862 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00001863
Gordon Henriksenae636f82008-01-03 16:47:34 +00001864 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001865 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001866 DAG.getIntPtrConstant(NumBytes, true),
1867 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1868 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001869 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001870 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001871
Chris Lattner3085e152007-02-25 08:59:22 +00001872 // Handle result values, copying them out of physregs into vregs that we
1873 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001874 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001875 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001876}
1877
Evan Cheng25ab6902006-09-08 06:48:29 +00001878
1879//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001880// Fast Calling Convention (tail call) implementation
1881//===----------------------------------------------------------------------===//
1882
1883// Like std call, callee cleans arguments, convention except that ECX is
1884// reserved for storing the tail called function address. Only 2 registers are
1885// free for argument passing (inreg). Tail call optimization is performed
1886// provided:
1887// * tailcallopt is enabled
1888// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001889// On X86_64 architecture with GOT-style position independent code only local
1890// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001891// To keep the stack aligned according to platform abi the function
1892// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1893// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001894// If a tail called function callee has more arguments than the caller the
1895// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001896// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001897// original REtADDR, but before the saved framepointer or the spilled registers
1898// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1899// stack layout:
1900// arg1
1901// arg2
1902// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00001903// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001904// move area ]
1905// (possible EBP)
1906// ESI
1907// EDI
1908// local1 ..
1909
1910/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1911/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00001912unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001913 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001914 MachineFunction &MF = DAG.getMachineFunction();
1915 const TargetMachine &TM = MF.getTarget();
1916 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1917 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00001918 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001919 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001920 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001921 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1922 // Number smaller than 12 so just add the difference.
1923 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1924 } else {
1925 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00001926 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00001927 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001928 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001929 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001930}
1931
1932/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001933/// following the call is a return. A function is eligible if caller/callee
1934/// calling conventions match, currently only fastcc supports tail calls, and
1935/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001936bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001937 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001938 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001939 if (!PerformTailCallOpt)
1940 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001941
Dan Gohman095cc292008-09-13 01:54:27 +00001942 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001943 MachineFunction &MF = DAG.getMachineFunction();
1944 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001945 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001946 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001947 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001948 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001949 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001950 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001951 return true;
1952
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001953 // Can only do local tail calls (in same module, hidden or protected) on
1954 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001955 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1956 return G->getGlobal()->hasHiddenVisibility()
1957 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001958 }
1959 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001960
1961 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001962}
1963
Dan Gohman3df24e62008-09-03 23:12:08 +00001964FastISel *
1965X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001966 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00001967 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00001968 DenseMap<const Value *, unsigned> &vm,
1969 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001970 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001971 DenseMap<const AllocaInst *, int> &am
1972#ifndef NDEBUG
1973 , SmallSet<Instruction*, 8> &cil
1974#endif
1975 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00001976 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001977#ifndef NDEBUG
1978 , cil
1979#endif
1980 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001981}
1982
1983
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001984//===----------------------------------------------------------------------===//
1985// Other Lowering Hooks
1986//===----------------------------------------------------------------------===//
1987
1988
Dan Gohman475871a2008-07-27 21:46:04 +00001989SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001990 MachineFunction &MF = DAG.getMachineFunction();
1991 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1992 int ReturnAddrIndex = FuncInfo->getRAIndex();
1993
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001994 if (ReturnAddrIndex == 0) {
1995 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00001996 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001997 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001998 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001999 }
2000
Evan Cheng25ab6902006-09-08 06:48:29 +00002001 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002002}
2003
2004
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002005/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2006/// specific condition code, returning the condition code and the LHS/RHS of the
2007/// comparison to make.
2008static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2009 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002010 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002011 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2012 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2013 // X > -1 -> X == 0, jump !sign.
2014 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002015 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002016 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2017 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002018 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002019 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002020 // X < 1 -> X <= 0
2021 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002022 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002023 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002024 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002025
Evan Chengd9558e02006-01-06 00:43:03 +00002026 switch (SetCCOpcode) {
Chris Lattner4c78e022008-12-23 23:42:27 +00002027 default: assert(0 && "Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002028 case ISD::SETEQ: return X86::COND_E;
2029 case ISD::SETGT: return X86::COND_G;
2030 case ISD::SETGE: return X86::COND_GE;
2031 case ISD::SETLT: return X86::COND_L;
2032 case ISD::SETLE: return X86::COND_LE;
2033 case ISD::SETNE: return X86::COND_NE;
2034 case ISD::SETULT: return X86::COND_B;
2035 case ISD::SETUGT: return X86::COND_A;
2036 case ISD::SETULE: return X86::COND_BE;
2037 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002038 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002039 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002040
Chris Lattner4c78e022008-12-23 23:42:27 +00002041 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002042
Chris Lattner4c78e022008-12-23 23:42:27 +00002043 // If LHS is a foldable load, but RHS is not, flip the condition.
2044 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2045 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2046 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2047 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002048 }
2049
Chris Lattner4c78e022008-12-23 23:42:27 +00002050 switch (SetCCOpcode) {
2051 default: break;
2052 case ISD::SETOLT:
2053 case ISD::SETOLE:
2054 case ISD::SETUGT:
2055 case ISD::SETUGE:
2056 std::swap(LHS, RHS);
2057 break;
2058 }
2059
2060 // On a floating point condition, the flags are set as follows:
2061 // ZF PF CF op
2062 // 0 | 0 | 0 | X > Y
2063 // 0 | 0 | 1 | X < Y
2064 // 1 | 0 | 0 | X == Y
2065 // 1 | 1 | 1 | unordered
2066 switch (SetCCOpcode) {
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002067 default: assert(0 && "Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002068 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002069 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002070 case ISD::SETOLT: // flipped
2071 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002072 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002073 case ISD::SETOLE: // flipped
2074 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002075 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002076 case ISD::SETUGT: // flipped
2077 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002078 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002079 case ISD::SETUGE: // flipped
2080 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002081 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002082 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002083 case ISD::SETNE: return X86::COND_NE;
2084 case ISD::SETUO: return X86::COND_P;
2085 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002086 }
Evan Chengd9558e02006-01-06 00:43:03 +00002087}
2088
Evan Cheng4a460802006-01-11 00:33:36 +00002089/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2090/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002091/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002092static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002093 switch (X86CC) {
2094 default:
2095 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002096 case X86::COND_B:
2097 case X86::COND_BE:
2098 case X86::COND_E:
2099 case X86::COND_P:
2100 case X86::COND_A:
2101 case X86::COND_AE:
2102 case X86::COND_NE:
2103 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002104 return true;
2105 }
2106}
2107
Nate Begeman9008ca62009-04-27 18:41:29 +00002108/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2109/// the specified range (L, H].
2110static bool isUndefOrInRange(int Val, int Low, int Hi) {
2111 return (Val < 0) || (Val >= Low && Val < Hi);
2112}
2113
2114/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2115/// specified value.
2116static bool isUndefOrEqual(int Val, int CmpVal) {
2117 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002118 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002119 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002120}
2121
Nate Begeman9008ca62009-04-27 18:41:29 +00002122/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2123/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2124/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002125static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002126 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2127 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2128 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2129 return (Mask[0] < 2 && Mask[1] < 2);
2130 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002131}
2132
Nate Begeman9008ca62009-04-27 18:41:29 +00002133bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2134 SmallVector<int, 8> M;
2135 N->getMask(M);
2136 return ::isPSHUFDMask(M, N->getValueType(0));
2137}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002138
Nate Begeman9008ca62009-04-27 18:41:29 +00002139/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2140/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002141static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002142 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002143 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002144
2145 // Lower quadword copied in order or undef.
2146 for (int i = 0; i != 4; ++i)
2147 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002148 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002149
Evan Cheng506d3df2006-03-29 23:07:14 +00002150 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002151 for (int i = 4; i != 8; ++i)
2152 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002153 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002154
Evan Cheng506d3df2006-03-29 23:07:14 +00002155 return true;
2156}
2157
Nate Begeman9008ca62009-04-27 18:41:29 +00002158bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2159 SmallVector<int, 8> M;
2160 N->getMask(M);
2161 return ::isPSHUFHWMask(M, N->getValueType(0));
2162}
Evan Cheng506d3df2006-03-29 23:07:14 +00002163
Nate Begeman9008ca62009-04-27 18:41:29 +00002164/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2165/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002166static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002167 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002168 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002169
Rafael Espindola15684b22009-04-24 12:40:33 +00002170 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002171 for (int i = 4; i != 8; ++i)
2172 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002173 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002174
Rafael Espindola15684b22009-04-24 12:40:33 +00002175 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002176 for (int i = 0; i != 4; ++i)
2177 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002178 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002179
Rafael Espindola15684b22009-04-24 12:40:33 +00002180 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002181}
2182
Nate Begeman9008ca62009-04-27 18:41:29 +00002183bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2184 SmallVector<int, 8> M;
2185 N->getMask(M);
2186 return ::isPSHUFLWMask(M, N->getValueType(0));
2187}
2188
Evan Cheng14aed5e2006-03-24 01:18:28 +00002189/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2190/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002191static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002192 int NumElems = VT.getVectorNumElements();
2193 if (NumElems != 2 && NumElems != 4)
2194 return false;
2195
2196 int Half = NumElems / 2;
2197 for (int i = 0; i < Half; ++i)
2198 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002199 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002200 for (int i = Half; i < NumElems; ++i)
2201 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002202 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002203
Evan Cheng14aed5e2006-03-24 01:18:28 +00002204 return true;
2205}
2206
Nate Begeman9008ca62009-04-27 18:41:29 +00002207bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2208 SmallVector<int, 8> M;
2209 N->getMask(M);
2210 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002211}
2212
Evan Cheng213d2cf2007-05-17 18:45:50 +00002213/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002214/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2215/// half elements to come from vector 1 (which would equal the dest.) and
2216/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002217static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002218 int NumElems = VT.getVectorNumElements();
2219
2220 if (NumElems != 2 && NumElems != 4)
2221 return false;
2222
2223 int Half = NumElems / 2;
2224 for (int i = 0; i < Half; ++i)
2225 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002226 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002227 for (int i = Half; i < NumElems; ++i)
2228 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002229 return false;
2230 return true;
2231}
2232
Nate Begeman9008ca62009-04-27 18:41:29 +00002233static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2234 SmallVector<int, 8> M;
2235 N->getMask(M);
2236 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002237}
2238
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002239/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2240/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002241bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2242 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002243 return false;
2244
Evan Cheng2064a2b2006-03-28 06:50:32 +00002245 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002246 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2247 isUndefOrEqual(N->getMaskElt(1), 7) &&
2248 isUndefOrEqual(N->getMaskElt(2), 2) &&
2249 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002250}
2251
Evan Cheng5ced1d82006-04-06 23:23:56 +00002252/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2253/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002254bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2255 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002256
Evan Cheng5ced1d82006-04-06 23:23:56 +00002257 if (NumElems != 2 && NumElems != 4)
2258 return false;
2259
Evan Chengc5cdff22006-04-07 21:53:05 +00002260 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002261 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002262 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002263
Evan Chengc5cdff22006-04-07 21:53:05 +00002264 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002265 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002266 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002267
2268 return true;
2269}
2270
2271/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002272/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2273/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002274bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2275 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002276
Evan Cheng5ced1d82006-04-06 23:23:56 +00002277 if (NumElems != 2 && NumElems != 4)
2278 return false;
2279
Evan Chengc5cdff22006-04-07 21:53:05 +00002280 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002281 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002282 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002283
Nate Begeman9008ca62009-04-27 18:41:29 +00002284 for (unsigned i = 0; i < NumElems/2; ++i)
2285 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002286 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002287
2288 return true;
2289}
2290
Nate Begeman9008ca62009-04-27 18:41:29 +00002291/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2292/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2293/// <2, 3, 2, 3>
2294bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2295 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2296
2297 if (NumElems != 4)
2298 return false;
2299
2300 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2301 isUndefOrEqual(N->getMaskElt(1), 3) &&
2302 isUndefOrEqual(N->getMaskElt(2), 2) &&
2303 isUndefOrEqual(N->getMaskElt(3), 3);
2304}
2305
Evan Cheng0038e592006-03-28 00:39:58 +00002306/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2307/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002308static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002309 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002310 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002311 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002312 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002313
2314 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2315 int BitI = Mask[i];
2316 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002317 if (!isUndefOrEqual(BitI, j))
2318 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002319 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002320 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002321 return false;
2322 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002323 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002324 return false;
2325 }
Evan Cheng0038e592006-03-28 00:39:58 +00002326 }
Evan Cheng0038e592006-03-28 00:39:58 +00002327 return true;
2328}
2329
Nate Begeman9008ca62009-04-27 18:41:29 +00002330bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2331 SmallVector<int, 8> M;
2332 N->getMask(M);
2333 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002334}
2335
Evan Cheng4fcb9222006-03-28 02:43:26 +00002336/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2337/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002338static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002339 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002340 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002341 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002342 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002343
2344 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2345 int BitI = Mask[i];
2346 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002347 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002348 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002349 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002350 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002351 return false;
2352 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002353 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002354 return false;
2355 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002356 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002357 return true;
2358}
2359
Nate Begeman9008ca62009-04-27 18:41:29 +00002360bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2361 SmallVector<int, 8> M;
2362 N->getMask(M);
2363 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002364}
2365
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002366/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2367/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2368/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002369static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002370 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002371 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002372 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002373
2374 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2375 int BitI = Mask[i];
2376 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002377 if (!isUndefOrEqual(BitI, j))
2378 return false;
2379 if (!isUndefOrEqual(BitI1, j))
2380 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002381 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002382 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002383}
2384
Nate Begeman9008ca62009-04-27 18:41:29 +00002385bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2386 SmallVector<int, 8> M;
2387 N->getMask(M);
2388 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2389}
2390
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002391/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2392/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2393/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002394static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002395 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002396 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2397 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002398
2399 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2400 int BitI = Mask[i];
2401 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002402 if (!isUndefOrEqual(BitI, j))
2403 return false;
2404 if (!isUndefOrEqual(BitI1, j))
2405 return false;
2406 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002407 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002408}
2409
Nate Begeman9008ca62009-04-27 18:41:29 +00002410bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2411 SmallVector<int, 8> M;
2412 N->getMask(M);
2413 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2414}
2415
Evan Cheng017dcc62006-04-21 01:05:10 +00002416/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2417/// specifies a shuffle of elements that is suitable for input to MOVSS,
2418/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002419static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002420 int NumElts = VT.getVectorNumElements();
Evan Cheng10762102007-12-06 22:14:22 +00002421 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002422 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002423
2424 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002425 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002426
2427 for (int i = 1; i < NumElts; ++i)
2428 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002429 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002430
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002431 return true;
2432}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002433
Nate Begeman9008ca62009-04-27 18:41:29 +00002434bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2435 SmallVector<int, 8> M;
2436 N->getMask(M);
2437 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002438}
2439
Evan Cheng017dcc62006-04-21 01:05:10 +00002440/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2441/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002442/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002443static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002444 bool V2IsSplat = false, bool V2IsUndef = false) {
2445 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002446 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002447 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002448
2449 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002450 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002451
2452 for (int i = 1; i < NumOps; ++i)
2453 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2454 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2455 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002456 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002457
Evan Cheng39623da2006-04-20 08:58:49 +00002458 return true;
2459}
2460
Nate Begeman9008ca62009-04-27 18:41:29 +00002461static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002462 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002463 SmallVector<int, 8> M;
2464 N->getMask(M);
2465 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002466}
2467
Evan Chengd9539472006-04-14 21:59:03 +00002468/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2469/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002470bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2471 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002472 return false;
2473
2474 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002475 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002476 int Elt = N->getMaskElt(i);
2477 if (Elt >= 0 && Elt != 1)
2478 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002479 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002480
2481 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002482 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002483 int Elt = N->getMaskElt(i);
2484 if (Elt >= 0 && Elt != 3)
2485 return false;
2486 if (Elt == 3)
2487 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002488 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002489 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002490 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002491 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002492}
2493
2494/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2495/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002496bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2497 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002498 return false;
2499
2500 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002501 for (unsigned i = 0; i < 2; ++i)
2502 if (N->getMaskElt(i) > 0)
2503 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002504
2505 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002506 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002507 int Elt = N->getMaskElt(i);
2508 if (Elt >= 0 && Elt != 2)
2509 return false;
2510 if (Elt == 2)
2511 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002512 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002513 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002514 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002515}
2516
Evan Cheng0b457f02008-09-25 20:50:48 +00002517/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2518/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002519bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2520 int e = N->getValueType(0).getVectorNumElements() / 2;
2521
2522 for (int i = 0; i < e; ++i)
2523 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002524 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002525 for (int i = 0; i < e; ++i)
2526 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002527 return false;
2528 return true;
2529}
2530
Evan Cheng63d33002006-03-22 08:01:21 +00002531/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2532/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2533/// instructions.
2534unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002535 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2536 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2537
Evan Chengb9df0ca2006-03-22 02:53:00 +00002538 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2539 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002540 for (int i = 0; i < NumOperands; ++i) {
2541 int Val = SVOp->getMaskElt(NumOperands-i-1);
2542 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002543 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002544 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002545 if (i != NumOperands - 1)
2546 Mask <<= Shift;
2547 }
Evan Cheng63d33002006-03-22 08:01:21 +00002548 return Mask;
2549}
2550
Evan Cheng506d3df2006-03-29 23:07:14 +00002551/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2552/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2553/// instructions.
2554unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002555 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002556 unsigned Mask = 0;
2557 // 8 nodes, but we only care about the last 4.
2558 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002559 int Val = SVOp->getMaskElt(i);
2560 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002561 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002562 if (i != 4)
2563 Mask <<= 2;
2564 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002565 return Mask;
2566}
2567
2568/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2569/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2570/// instructions.
2571unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002572 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002573 unsigned Mask = 0;
2574 // 8 nodes, but we only care about the first 4.
2575 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002576 int Val = SVOp->getMaskElt(i);
2577 if (Val >= 0)
2578 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002579 if (i != 0)
2580 Mask <<= 2;
2581 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002582 return Mask;
2583}
2584
Nate Begeman9008ca62009-04-27 18:41:29 +00002585/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2586/// their permute mask.
2587static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2588 SelectionDAG &DAG) {
2589 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002590 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002591 SmallVector<int, 8> MaskVec;
2592
Nate Begeman5a5ca152009-04-29 05:20:52 +00002593 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002594 int idx = SVOp->getMaskElt(i);
2595 if (idx < 0)
2596 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002597 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002598 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002599 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002600 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002601 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002602 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2603 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002604}
2605
Evan Cheng779ccea2007-12-07 21:30:01 +00002606/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2607/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002608static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002609 unsigned NumElems = VT.getVectorNumElements();
2610 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002611 int idx = Mask[i];
2612 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002613 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002614 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002615 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002616 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002617 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002618 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002619}
2620
Evan Cheng533a0aa2006-04-19 20:35:22 +00002621/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2622/// match movhlps. The lower half elements should come from upper half of
2623/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002624/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002625static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2626 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002627 return false;
2628 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002629 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002630 return false;
2631 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002633 return false;
2634 return true;
2635}
2636
Evan Cheng5ced1d82006-04-06 23:23:56 +00002637/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002638/// is promoted to a vector. It also returns the LoadSDNode by reference if
2639/// required.
2640static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002641 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2642 return false;
2643 N = N->getOperand(0).getNode();
2644 if (!ISD::isNON_EXTLoad(N))
2645 return false;
2646 if (LD)
2647 *LD = cast<LoadSDNode>(N);
2648 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002649}
2650
Evan Cheng533a0aa2006-04-19 20:35:22 +00002651/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2652/// match movlp{s|d}. The lower half elements should come from lower half of
2653/// V1 (and in order), and the upper half elements should come from the upper
2654/// half of V2 (and in order). And since V1 will become the source of the
2655/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002656static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2657 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002658 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002659 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002660 // Is V2 is a vector load, don't do this transformation. We will try to use
2661 // load folding shufps op.
2662 if (ISD::isNON_EXTLoad(V2))
2663 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002664
Nate Begeman5a5ca152009-04-29 05:20:52 +00002665 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002666
Evan Cheng533a0aa2006-04-19 20:35:22 +00002667 if (NumElems != 2 && NumElems != 4)
2668 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002669 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002670 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002671 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002672 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002673 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002674 return false;
2675 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002676}
2677
Evan Cheng39623da2006-04-20 08:58:49 +00002678/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2679/// all the same.
2680static bool isSplatVector(SDNode *N) {
2681 if (N->getOpcode() != ISD::BUILD_VECTOR)
2682 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002683
Dan Gohman475871a2008-07-27 21:46:04 +00002684 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002685 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2686 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002687 return false;
2688 return true;
2689}
2690
Evan Cheng213d2cf2007-05-17 18:45:50 +00002691/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2692/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002693static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002694 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002695 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002696 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002697 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002698}
2699
2700/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002701/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002702/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002703static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002704 SDValue V1 = N->getOperand(0);
2705 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002706 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2707 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002708 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002709 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002710 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002711 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2712 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002713 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V2.getOperand(Idx-NumElems)))
2714 return false;
2715 } else if (Idx >= 0) {
2716 unsigned Opc = V1.getOpcode();
2717 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2718 continue;
2719 if (Opc != ISD::BUILD_VECTOR || !isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002720 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002721 }
2722 }
2723 return true;
2724}
2725
2726/// getZeroVector - Returns a vector of specified type with all zero elements.
2727///
Dale Johannesenace16102009-02-03 19:33:06 +00002728static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2729 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002730 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002731
Chris Lattner8a594482007-11-25 00:24:49 +00002732 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2733 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002734 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002735 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002736 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002737 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002738 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002739 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002740 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002741 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002742 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002743 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002744 }
Dale Johannesenace16102009-02-03 19:33:06 +00002745 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002746}
2747
Chris Lattner8a594482007-11-25 00:24:49 +00002748/// getOnesVector - Returns a vector of specified type with all bits set.
2749///
Dale Johannesenace16102009-02-03 19:33:06 +00002750static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002751 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002752
Chris Lattner8a594482007-11-25 00:24:49 +00002753 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2754 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002755 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2756 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002757 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002758 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002759 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002760 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002761 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002762}
2763
2764
Evan Cheng39623da2006-04-20 08:58:49 +00002765/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2766/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002767static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2768 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002769 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002770
Evan Cheng39623da2006-04-20 08:58:49 +00002771 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002772 SmallVector<int, 8> MaskVec;
2773 SVOp->getMask(MaskVec);
2774
Nate Begeman5a5ca152009-04-29 05:20:52 +00002775 for (unsigned i = 0; i != NumElems; ++i) {
2776 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002777 MaskVec[i] = NumElems;
2778 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002779 }
Evan Cheng39623da2006-04-20 08:58:49 +00002780 }
Evan Cheng39623da2006-04-20 08:58:49 +00002781 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002782 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2783 SVOp->getOperand(1), &MaskVec[0]);
2784 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002785}
2786
Evan Cheng017dcc62006-04-21 01:05:10 +00002787/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2788/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002789static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2790 SDValue V2) {
2791 unsigned NumElems = VT.getVectorNumElements();
2792 SmallVector<int, 8> Mask;
2793 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002794 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002795 Mask.push_back(i);
2796 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002797}
2798
Nate Begeman9008ca62009-04-27 18:41:29 +00002799/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2800static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2801 SDValue V2) {
2802 unsigned NumElems = VT.getVectorNumElements();
2803 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002804 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002805 Mask.push_back(i);
2806 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002807 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002808 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002809}
2810
Nate Begeman9008ca62009-04-27 18:41:29 +00002811/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2812static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2813 SDValue V2) {
2814 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002815 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002816 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002817 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002818 Mask.push_back(i + Half);
2819 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002820 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002821 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002822}
2823
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002824/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002825static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2826 bool HasSSE2) {
2827 if (SV->getValueType(0).getVectorNumElements() <= 4)
2828 return SDValue(SV, 0);
2829
2830 MVT PVT = MVT::v4f32;
2831 MVT VT = SV->getValueType(0);
2832 DebugLoc dl = SV->getDebugLoc();
2833 SDValue V1 = SV->getOperand(0);
2834 int NumElems = VT.getVectorNumElements();
2835 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002836
Nate Begeman9008ca62009-04-27 18:41:29 +00002837 // unpack elements to the correct location
2838 while (NumElems > 4) {
2839 if (EltNo < NumElems/2) {
2840 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2841 } else {
2842 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2843 EltNo -= NumElems/2;
2844 }
2845 NumElems >>= 1;
2846 }
2847
2848 // Perform the splat.
2849 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002850 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002851 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2852 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002853}
2854
Evan Chengba05f722006-04-21 23:03:30 +00002855/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002856/// vector of zero or undef vector. This produces a shuffle where the low
2857/// element of V2 is swizzled into the zero/undef vector, landing at element
2858/// 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 +00002859static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002860 bool isZero, bool HasSSE2,
2861 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002862 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002863 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002864 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2865 unsigned NumElems = VT.getVectorNumElements();
2866 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002867 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002868 // If this is the insertion idx, put the low elt of V2 here.
2869 MaskVec.push_back(i == Idx ? NumElems : i);
2870 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002871}
2872
Evan Chengf26ffe92008-05-29 08:22:04 +00002873/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2874/// a shuffle that is zero.
2875static
Nate Begeman9008ca62009-04-27 18:41:29 +00002876unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
2877 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002878 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002879 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002880 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00002881 int Idx = SVOp->getMaskElt(Index);
2882 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002883 ++NumZeros;
2884 continue;
2885 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002886 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Gabor Greifba36cb52008-08-28 21:40:38 +00002887 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00002888 ++NumZeros;
2889 else
2890 break;
2891 }
2892 return NumZeros;
2893}
2894
2895/// isVectorShift - Returns true if the shuffle can be implemented as a
2896/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002897/// FIXME: split into pslldqi, psrldqi, palignr variants.
2898static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00002899 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002900 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00002901
2902 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002903 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002904 if (!NumZeros) {
2905 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002906 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00002907 if (!NumZeros)
2908 return false;
2909 }
Evan Chengf26ffe92008-05-29 08:22:04 +00002910 bool SeenV1 = false;
2911 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002912 for (int i = NumZeros; i < NumElems; ++i) {
2913 int Val = isLeft ? (i - NumZeros) : i;
2914 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
2915 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00002916 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00002917 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00002918 SeenV1 = true;
2919 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00002920 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00002921 SeenV2 = true;
2922 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002923 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00002924 return false;
2925 }
2926 if (SeenV1 && SeenV2)
2927 return false;
2928
Nate Begeman9008ca62009-04-27 18:41:29 +00002929 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00002930 ShAmt = NumZeros;
2931 return true;
2932}
2933
2934
Evan Chengc78d3b42006-04-24 18:01:45 +00002935/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
2936///
Dan Gohman475871a2008-07-27 21:46:04 +00002937static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002938 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002939 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002940 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00002941 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002942
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002943 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002944 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002945 bool First = true;
2946 for (unsigned i = 0; i < 16; ++i) {
2947 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
2948 if (ThisIsNonZero && First) {
2949 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002950 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002951 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002952 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00002953 First = false;
2954 }
2955
2956 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00002957 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002958 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
2959 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00002960 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00002961 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00002962 }
2963 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00002964 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
2965 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00002966 ThisElt, DAG.getConstant(8, MVT::i8));
2967 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002968 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00002969 } else
2970 ThisElt = LastElt;
2971
Gabor Greifba36cb52008-08-28 21:40:38 +00002972 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00002973 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00002974 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00002975 }
2976 }
2977
Dale Johannesenace16102009-02-03 19:33:06 +00002978 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00002979}
2980
Bill Wendlinga348c562007-03-22 18:42:45 +00002981/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00002982///
Dan Gohman475871a2008-07-27 21:46:04 +00002983static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00002984 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00002985 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00002986 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00002987 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00002988
Dale Johannesen6f38cb62009-02-07 19:59:05 +00002989 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00002991 bool First = true;
2992 for (unsigned i = 0; i < 8; ++i) {
2993 bool isNonZero = (NonZeros & (1 << i)) != 0;
2994 if (isNonZero) {
2995 if (First) {
2996 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00002997 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00002998 else
Dale Johannesene8d72302009-02-06 23:05:02 +00002999 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003000 First = false;
3001 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003002 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003003 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003004 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003005 }
3006 }
3007
3008 return V;
3009}
3010
Evan Chengf26ffe92008-05-29 08:22:04 +00003011/// getVShift - Return a vector logical shift node.
3012///
Dan Gohman475871a2008-07-27 21:46:04 +00003013static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003014 unsigned NumBits, SelectionDAG &DAG,
3015 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003016 bool isMMX = VT.getSizeInBits() == 64;
3017 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003018 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003019 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3020 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3021 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003022 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003023}
3024
Dan Gohman475871a2008-07-27 21:46:04 +00003025SDValue
3026X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003027 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003028 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003029 if (ISD::isBuildVectorAllZeros(Op.getNode())
3030 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003031 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3032 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3033 // eliminated on x86-32 hosts.
3034 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3035 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003036
Gabor Greifba36cb52008-08-28 21:40:38 +00003037 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003038 return getOnesVector(Op.getValueType(), DAG, dl);
3039 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003040 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003041
Duncan Sands83ec4b62008-06-06 12:08:01 +00003042 MVT VT = Op.getValueType();
3043 MVT EVT = VT.getVectorElementType();
3044 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003045
3046 unsigned NumElems = Op.getNumOperands();
3047 unsigned NumZero = 0;
3048 unsigned NumNonZero = 0;
3049 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003050 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003051 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003052 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003053 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003054 if (Elt.getOpcode() == ISD::UNDEF)
3055 continue;
3056 Values.insert(Elt);
3057 if (Elt.getOpcode() != ISD::Constant &&
3058 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003059 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003060 if (isZeroNode(Elt))
3061 NumZero++;
3062 else {
3063 NonZeros |= (1 << i);
3064 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003065 }
3066 }
3067
Dan Gohman7f321562007-06-25 16:23:39 +00003068 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003069 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003070 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003071 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003072
Chris Lattner67f453a2008-03-09 05:42:06 +00003073 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003074 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003075 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003076 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003077
Chris Lattner62098042008-03-09 01:05:04 +00003078 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3079 // the value are obviously zero, truncate the value to i32 and do the
3080 // insertion that way. Only do this if the value is non-constant or if the
3081 // value is a constant being inserted into element 0. It is cheaper to do
3082 // a constant pool load than it is to do a movd + shuffle.
3083 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3084 (!IsAllConstants || Idx == 0)) {
3085 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3086 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003087 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3088 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003089
Chris Lattner62098042008-03-09 01:05:04 +00003090 // Truncate the value (which may itself be a constant) to i32, and
3091 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003092 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3093 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003094 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3095 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003096
Chris Lattner62098042008-03-09 01:05:04 +00003097 // Now we have our 32-bit value zero extended in the low element of
3098 // a vector. If Idx != 0, swizzle it into place.
3099 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003100 SmallVector<int, 4> Mask;
3101 Mask.push_back(Idx);
3102 for (unsigned i = 1; i != VecElts; ++i)
3103 Mask.push_back(i);
3104 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3105 DAG.getUNDEF(Item.getValueType()),
3106 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003107 }
Dale Johannesenace16102009-02-03 19:33:06 +00003108 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003109 }
3110 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003111
Chris Lattner19f79692008-03-08 22:59:52 +00003112 // If we have a constant or non-constant insertion into the low element of
3113 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3114 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3115 // depending on what the source datatype is. Because we can only get here
3116 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3117 if (Idx == 0 &&
3118 // Don't do this for i64 values on x86-32.
3119 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Dale Johannesenace16102009-02-03 19:33:06 +00003120 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003121 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003122 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3123 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003124 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003125
3126 // Is it a vector logical left shift?
3127 if (NumElems == 2 && Idx == 1 &&
3128 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003129 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003130 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003131 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003132 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003133 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003134 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003135
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003136 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003137 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003138
Chris Lattner19f79692008-03-08 22:59:52 +00003139 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3140 // is a non-constant being inserted into an element other than the low one,
3141 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3142 // movd/movss) to move this into the low element, then shuffle it into
3143 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003144 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003145 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003146
Evan Cheng0db9fe62006-04-25 20:13:52 +00003147 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003148 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3149 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003150 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003151 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003152 MaskVec.push_back(i == Idx ? 0 : 1);
3153 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003154 }
3155 }
3156
Chris Lattner67f453a2008-03-09 05:42:06 +00003157 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3158 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003159 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003160
Dan Gohmana3941172007-07-24 22:55:08 +00003161 // A vector full of immediates; various special cases are already
3162 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003163 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003164 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003165
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003166 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003167 if (EVTBits == 64) {
3168 if (NumNonZero == 1) {
3169 // One half is zero or undef.
3170 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003171 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003172 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003173 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3174 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003175 }
Dan Gohman475871a2008-07-27 21:46:04 +00003176 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003177 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003178
3179 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003180 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003181 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003182 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003183 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003184 }
3185
Bill Wendling826f36f2007-03-28 00:57:11 +00003186 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003187 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003188 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003189 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003190 }
3191
3192 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003193 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003194 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003195 if (NumElems == 4 && NumZero > 0) {
3196 for (unsigned i = 0; i < 4; ++i) {
3197 bool isZero = !(NonZeros & (1 << i));
3198 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003199 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003200 else
Dale Johannesenace16102009-02-03 19:33:06 +00003201 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003202 }
3203
3204 for (unsigned i = 0; i < 2; ++i) {
3205 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3206 default: break;
3207 case 0:
3208 V[i] = V[i*2]; // Must be a zero vector.
3209 break;
3210 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003211 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003212 break;
3213 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003214 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003215 break;
3216 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003217 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003218 break;
3219 }
3220 }
3221
Nate Begeman9008ca62009-04-27 18:41:29 +00003222 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003223 bool Reverse = (NonZeros & 0x3) == 2;
3224 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003225 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003226 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3227 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003228 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3229 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003230 }
3231
3232 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003233 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3234 // values to be inserted is equal to the number of elements, in which case
3235 // use the unpack code below in the hopes of matching the consecutive elts
3236 // load merge pattern for shuffles.
3237 // FIXME: We could probably just check that here directly.
3238 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3239 getSubtarget()->hasSSE41()) {
3240 V[0] = DAG.getUNDEF(VT);
3241 for (unsigned i = 0; i < NumElems; ++i)
3242 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3243 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3244 Op.getOperand(i), DAG.getIntPtrConstant(i));
3245 return V[0];
3246 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003247 // Expand into a number of unpckl*.
3248 // e.g. for v4f32
3249 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3250 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3251 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003252 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003253 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003254 NumElems >>= 1;
3255 while (NumElems != 0) {
3256 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003257 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003258 NumElems >>= 1;
3259 }
3260 return V[0];
3261 }
3262
Dan Gohman475871a2008-07-27 21:46:04 +00003263 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003264}
3265
Nate Begemanb9a47b82009-02-23 08:49:38 +00003266// v8i16 shuffles - Prefer shuffles in the following order:
3267// 1. [all] pshuflw, pshufhw, optional move
3268// 2. [ssse3] 1 x pshufb
3269// 3. [ssse3] 2 x pshufb + 1 x por
3270// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003271static
Nate Begeman9008ca62009-04-27 18:41:29 +00003272SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3273 SelectionDAG &DAG, X86TargetLowering &TLI) {
3274 SDValue V1 = SVOp->getOperand(0);
3275 SDValue V2 = SVOp->getOperand(1);
3276 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003277 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003278
Nate Begemanb9a47b82009-02-23 08:49:38 +00003279 // Determine if more than 1 of the words in each of the low and high quadwords
3280 // of the result come from the same quadword of one of the two inputs. Undef
3281 // mask values count as coming from any quadword, for better codegen.
3282 SmallVector<unsigned, 4> LoQuad(4);
3283 SmallVector<unsigned, 4> HiQuad(4);
3284 BitVector InputQuads(4);
3285 for (unsigned i = 0; i < 8; ++i) {
3286 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003288 MaskVals.push_back(EltIdx);
3289 if (EltIdx < 0) {
3290 ++Quad[0];
3291 ++Quad[1];
3292 ++Quad[2];
3293 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003294 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003295 }
3296 ++Quad[EltIdx / 4];
3297 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003298 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003299
Nate Begemanb9a47b82009-02-23 08:49:38 +00003300 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003301 unsigned MaxQuad = 1;
3302 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003303 if (LoQuad[i] > MaxQuad) {
3304 BestLoQuad = i;
3305 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003306 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003307 }
3308
Nate Begemanb9a47b82009-02-23 08:49:38 +00003309 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003310 MaxQuad = 1;
3311 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003312 if (HiQuad[i] > MaxQuad) {
3313 BestHiQuad = i;
3314 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003315 }
3316 }
3317
Nate Begemanb9a47b82009-02-23 08:49:38 +00003318 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3319 // of the two input vectors, shuffle them into one input vector so only a
3320 // single pshufb instruction is necessary. If There are more than 2 input
3321 // quads, disable the next transformation since it does not help SSSE3.
3322 bool V1Used = InputQuads[0] || InputQuads[1];
3323 bool V2Used = InputQuads[2] || InputQuads[3];
3324 if (TLI.getSubtarget()->hasSSSE3()) {
3325 if (InputQuads.count() == 2 && V1Used && V2Used) {
3326 BestLoQuad = InputQuads.find_first();
3327 BestHiQuad = InputQuads.find_next(BestLoQuad);
3328 }
3329 if (InputQuads.count() > 2) {
3330 BestLoQuad = -1;
3331 BestHiQuad = -1;
3332 }
3333 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003334
Nate Begemanb9a47b82009-02-23 08:49:38 +00003335 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3336 // the shuffle mask. If a quad is scored as -1, that means that it contains
3337 // words from all 4 input quadwords.
3338 SDValue NewV;
3339 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003340 SmallVector<int, 8> MaskV;
3341 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3342 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3343 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3344 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3345 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003346 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003347
Nate Begemanb9a47b82009-02-23 08:49:38 +00003348 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3349 // source words for the shuffle, to aid later transformations.
3350 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003351 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003352 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003353 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003354 if (idx != (int)i)
3355 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003356 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003357 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003358 AllWordsInNewV = false;
3359 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003360 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003361
Nate Begemanb9a47b82009-02-23 08:49:38 +00003362 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3363 if (AllWordsInNewV) {
3364 for (int i = 0; i != 8; ++i) {
3365 int idx = MaskVals[i];
3366 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003367 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003368 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3369 if ((idx != i) && idx < 4)
3370 pshufhw = false;
3371 if ((idx != i) && idx > 3)
3372 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003373 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003374 V1 = NewV;
3375 V2Used = false;
3376 BestLoQuad = 0;
3377 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003378 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003379
Nate Begemanb9a47b82009-02-23 08:49:38 +00003380 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3381 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003382 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003383 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3384 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003385 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003386 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003387
3388 // If we have SSSE3, and all words of the result are from 1 input vector,
3389 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3390 // is present, fall back to case 4.
3391 if (TLI.getSubtarget()->hasSSSE3()) {
3392 SmallVector<SDValue,16> pshufbMask;
3393
3394 // If we have elements from both input vectors, set the high bit of the
3395 // shuffle mask element to zero out elements that come from V2 in the V1
3396 // mask, and elements that come from V1 in the V2 mask, so that the two
3397 // results can be OR'd together.
3398 bool TwoInputs = V1Used && V2Used;
3399 for (unsigned i = 0; i != 8; ++i) {
3400 int EltIdx = MaskVals[i] * 2;
3401 if (TwoInputs && (EltIdx >= 16)) {
3402 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3403 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3404 continue;
3405 }
3406 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3407 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3408 }
3409 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3410 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003411 DAG.getNode(ISD::BUILD_VECTOR, dl,
3412 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003413 if (!TwoInputs)
3414 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3415
3416 // Calculate the shuffle mask for the second input, shuffle it, and
3417 // OR it with the first shuffled input.
3418 pshufbMask.clear();
3419 for (unsigned i = 0; i != 8; ++i) {
3420 int EltIdx = MaskVals[i] * 2;
3421 if (EltIdx < 16) {
3422 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3423 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3424 continue;
3425 }
3426 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3427 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3428 }
3429 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3430 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003431 DAG.getNode(ISD::BUILD_VECTOR, dl,
3432 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003433 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3434 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3435 }
3436
3437 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3438 // and update MaskVals with new element order.
3439 BitVector InOrder(8);
3440 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003441 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003442 for (int i = 0; i != 4; ++i) {
3443 int idx = MaskVals[i];
3444 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003445 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003446 InOrder.set(i);
3447 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003448 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003449 InOrder.set(i);
3450 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003451 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003452 }
3453 }
3454 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003455 MaskV.push_back(i);
3456 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3457 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003458 }
3459
3460 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3461 // and update MaskVals with the new element order.
3462 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003463 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003464 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003465 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003466 for (unsigned i = 4; i != 8; ++i) {
3467 int idx = MaskVals[i];
3468 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003469 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003470 InOrder.set(i);
3471 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003472 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003473 InOrder.set(i);
3474 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003475 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003476 }
3477 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003478 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3479 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003480 }
3481
3482 // In case BestHi & BestLo were both -1, which means each quadword has a word
3483 // from each of the four input quadwords, calculate the InOrder bitvector now
3484 // before falling through to the insert/extract cleanup.
3485 if (BestLoQuad == -1 && BestHiQuad == -1) {
3486 NewV = V1;
3487 for (int i = 0; i != 8; ++i)
3488 if (MaskVals[i] < 0 || MaskVals[i] == i)
3489 InOrder.set(i);
3490 }
3491
3492 // The other elements are put in the right place using pextrw and pinsrw.
3493 for (unsigned i = 0; i != 8; ++i) {
3494 if (InOrder[i])
3495 continue;
3496 int EltIdx = MaskVals[i];
3497 if (EltIdx < 0)
3498 continue;
3499 SDValue ExtOp = (EltIdx < 8)
3500 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3501 DAG.getIntPtrConstant(EltIdx))
3502 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3503 DAG.getIntPtrConstant(EltIdx - 8));
3504 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3505 DAG.getIntPtrConstant(i));
3506 }
3507 return NewV;
3508}
3509
3510// v16i8 shuffles - Prefer shuffles in the following order:
3511// 1. [ssse3] 1 x pshufb
3512// 2. [ssse3] 2 x pshufb + 1 x por
3513// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3514static
Nate Begeman9008ca62009-04-27 18:41:29 +00003515SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3516 SelectionDAG &DAG, X86TargetLowering &TLI) {
3517 SDValue V1 = SVOp->getOperand(0);
3518 SDValue V2 = SVOp->getOperand(1);
3519 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003520 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003521 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003522
3523 // If we have SSSE3, case 1 is generated when all result bytes come from
3524 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3525 // present, fall back to case 3.
3526 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3527 bool V1Only = true;
3528 bool V2Only = true;
3529 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003530 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003531 if (EltIdx < 0)
3532 continue;
3533 if (EltIdx < 16)
3534 V2Only = false;
3535 else
3536 V1Only = false;
3537 }
3538
3539 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3540 if (TLI.getSubtarget()->hasSSSE3()) {
3541 SmallVector<SDValue,16> pshufbMask;
3542
3543 // If all result elements are from one input vector, then only translate
3544 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3545 //
3546 // Otherwise, we have elements from both input vectors, and must zero out
3547 // elements that come from V2 in the first mask, and V1 in the second mask
3548 // so that we can OR them together.
3549 bool TwoInputs = !(V1Only || V2Only);
3550 for (unsigned i = 0; i != 16; ++i) {
3551 int EltIdx = MaskVals[i];
3552 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3553 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3554 continue;
3555 }
3556 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3557 }
3558 // If all the elements are from V2, assign it to V1 and return after
3559 // building the first pshufb.
3560 if (V2Only)
3561 V1 = V2;
3562 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003563 DAG.getNode(ISD::BUILD_VECTOR, dl,
3564 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003565 if (!TwoInputs)
3566 return V1;
3567
3568 // Calculate the shuffle mask for the second input, shuffle it, and
3569 // OR it with the first shuffled input.
3570 pshufbMask.clear();
3571 for (unsigned i = 0; i != 16; ++i) {
3572 int EltIdx = MaskVals[i];
3573 if (EltIdx < 16) {
3574 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3575 continue;
3576 }
3577 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3578 }
3579 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003580 DAG.getNode(ISD::BUILD_VECTOR, dl,
3581 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003582 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3583 }
3584
3585 // No SSSE3 - Calculate in place words and then fix all out of place words
3586 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3587 // the 16 different words that comprise the two doublequadword input vectors.
3588 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3589 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3590 SDValue NewV = V2Only ? V2 : V1;
3591 for (int i = 0; i != 8; ++i) {
3592 int Elt0 = MaskVals[i*2];
3593 int Elt1 = MaskVals[i*2+1];
3594
3595 // This word of the result is all undef, skip it.
3596 if (Elt0 < 0 && Elt1 < 0)
3597 continue;
3598
3599 // This word of the result is already in the correct place, skip it.
3600 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3601 continue;
3602 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3603 continue;
3604
3605 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3606 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3607 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003608
3609 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3610 // using a single extract together, load it and store it.
3611 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3612 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3613 DAG.getIntPtrConstant(Elt1 / 2));
3614 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3615 DAG.getIntPtrConstant(i));
3616 continue;
3617 }
3618
Nate Begemanb9a47b82009-02-23 08:49:38 +00003619 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003620 // source byte is not also odd, shift the extracted word left 8 bits
3621 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003622 if (Elt1 >= 0) {
3623 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3624 DAG.getIntPtrConstant(Elt1 / 2));
3625 if ((Elt1 & 1) == 0)
3626 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3627 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003628 else if (Elt0 >= 0)
3629 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3630 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003631 }
3632 // If Elt0 is defined, extract it from the appropriate source. If the
3633 // source byte is not also even, shift the extracted word right 8 bits. If
3634 // Elt1 was also defined, OR the extracted values together before
3635 // inserting them in the result.
3636 if (Elt0 >= 0) {
3637 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3638 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3639 if ((Elt0 & 1) != 0)
3640 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3641 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003642 else if (Elt1 >= 0)
3643 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3644 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003645 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3646 : InsElt0;
3647 }
3648 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3649 DAG.getIntPtrConstant(i));
3650 }
3651 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003652}
3653
Evan Cheng7a831ce2007-12-15 03:00:47 +00003654/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3655/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3656/// done when every pair / quad of shuffle mask elements point to elements in
3657/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003658/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3659static
Nate Begeman9008ca62009-04-27 18:41:29 +00003660SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3661 SelectionDAG &DAG,
3662 TargetLowering &TLI, DebugLoc dl) {
3663 MVT VT = SVOp->getValueType(0);
3664 SDValue V1 = SVOp->getOperand(0);
3665 SDValue V2 = SVOp->getOperand(1);
3666 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003667 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003668 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003669 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003670 MVT NewVT = MaskVT;
3671 switch (VT.getSimpleVT()) {
3672 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003673 case MVT::v4f32: NewVT = MVT::v2f64; break;
3674 case MVT::v4i32: NewVT = MVT::v2i64; break;
3675 case MVT::v8i16: NewVT = MVT::v4i32; break;
3676 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003677 }
3678
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003679 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003680 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003681 NewVT = MVT::v2i64;
3682 else
3683 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003684 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003685 int Scale = NumElems / NewWidth;
3686 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003687 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003688 int StartIdx = -1;
3689 for (int j = 0; j < Scale; ++j) {
3690 int EltIdx = SVOp->getMaskElt(i+j);
3691 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003692 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003693 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003694 StartIdx = EltIdx - (EltIdx % Scale);
3695 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003696 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003697 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003698 if (StartIdx == -1)
3699 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003700 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003701 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003702 }
3703
Dale Johannesenace16102009-02-03 19:33:06 +00003704 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3705 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003706 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003707}
3708
Evan Chengd880b972008-05-09 21:53:03 +00003709/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003710///
Dan Gohman475871a2008-07-27 21:46:04 +00003711static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003712 SDValue SrcOp, SelectionDAG &DAG,
3713 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003714 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3715 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003716 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003717 LD = dyn_cast<LoadSDNode>(SrcOp);
3718 if (!LD) {
3719 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3720 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003721 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003722 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3723 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3724 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3725 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3726 // PR2108
3727 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003728 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3729 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3730 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3731 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003732 SrcOp.getOperand(0)
3733 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003734 }
3735 }
3736 }
3737
Dale Johannesenace16102009-02-03 19:33:06 +00003738 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3739 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003740 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003741 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003742}
3743
Evan Chengace3c172008-07-22 21:13:36 +00003744/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3745/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003746static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003747LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3748 SDValue V1 = SVOp->getOperand(0);
3749 SDValue V2 = SVOp->getOperand(1);
3750 DebugLoc dl = SVOp->getDebugLoc();
3751 MVT VT = SVOp->getValueType(0);
3752
Evan Chengace3c172008-07-22 21:13:36 +00003753 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003754 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003755 SmallVector<int, 8> Mask1(4U, -1);
3756 SmallVector<int, 8> PermMask;
3757 SVOp->getMask(PermMask);
3758
Evan Chengace3c172008-07-22 21:13:36 +00003759 unsigned NumHi = 0;
3760 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003761 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003762 int Idx = PermMask[i];
3763 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003764 Locs[i] = std::make_pair(-1, -1);
3765 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003766 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3767 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003768 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003769 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003770 NumLo++;
3771 } else {
3772 Locs[i] = std::make_pair(1, NumHi);
3773 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003774 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003775 NumHi++;
3776 }
3777 }
3778 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003779
Evan Chengace3c172008-07-22 21:13:36 +00003780 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003781 // If no more than two elements come from either vector. This can be
3782 // implemented with two shuffles. First shuffle gather the elements.
3783 // The second shuffle, which takes the first shuffle as both of its
3784 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003785 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003786
Nate Begeman9008ca62009-04-27 18:41:29 +00003787 SmallVector<int, 8> Mask2(4U, -1);
3788
Evan Chengace3c172008-07-22 21:13:36 +00003789 for (unsigned i = 0; i != 4; ++i) {
3790 if (Locs[i].first == -1)
3791 continue;
3792 else {
3793 unsigned Idx = (i < 2) ? 0 : 4;
3794 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003795 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003796 }
3797 }
3798
Nate Begeman9008ca62009-04-27 18:41:29 +00003799 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003800 } else if (NumLo == 3 || NumHi == 3) {
3801 // Otherwise, we must have three elements from one vector, call it X, and
3802 // one element from the other, call it Y. First, use a shufps to build an
3803 // intermediate vector with the one element from Y and the element from X
3804 // that will be in the same half in the final destination (the indexes don't
3805 // matter). Then, use a shufps to build the final vector, taking the half
3806 // containing the element from Y from the intermediate, and the other half
3807 // from X.
3808 if (NumHi == 3) {
3809 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003810 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003811 std::swap(V1, V2);
3812 }
3813
3814 // Find the element from V2.
3815 unsigned HiIndex;
3816 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003817 int Val = PermMask[HiIndex];
3818 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003819 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003820 if (Val >= 4)
3821 break;
3822 }
3823
Nate Begeman9008ca62009-04-27 18:41:29 +00003824 Mask1[0] = PermMask[HiIndex];
3825 Mask1[1] = -1;
3826 Mask1[2] = PermMask[HiIndex^1];
3827 Mask1[3] = -1;
3828 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003829
3830 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003831 Mask1[0] = PermMask[0];
3832 Mask1[1] = PermMask[1];
3833 Mask1[2] = HiIndex & 1 ? 6 : 4;
3834 Mask1[3] = HiIndex & 1 ? 4 : 6;
3835 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003836 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003837 Mask1[0] = HiIndex & 1 ? 2 : 0;
3838 Mask1[1] = HiIndex & 1 ? 0 : 2;
3839 Mask1[2] = PermMask[2];
3840 Mask1[3] = PermMask[3];
3841 if (Mask1[2] >= 0)
3842 Mask1[2] += 4;
3843 if (Mask1[3] >= 0)
3844 Mask1[3] += 4;
3845 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003846 }
Evan Chengace3c172008-07-22 21:13:36 +00003847 }
3848
3849 // Break it into (shuffle shuffle_hi, shuffle_lo).
3850 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003851 SmallVector<int,8> LoMask(4U, -1);
3852 SmallVector<int,8> HiMask(4U, -1);
3853
3854 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003855 unsigned MaskIdx = 0;
3856 unsigned LoIdx = 0;
3857 unsigned HiIdx = 2;
3858 for (unsigned i = 0; i != 4; ++i) {
3859 if (i == 2) {
3860 MaskPtr = &HiMask;
3861 MaskIdx = 1;
3862 LoIdx = 0;
3863 HiIdx = 2;
3864 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003865 int Idx = PermMask[i];
3866 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003867 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00003868 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003869 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003870 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003871 LoIdx++;
3872 } else {
3873 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00003874 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003875 HiIdx++;
3876 }
3877 }
3878
Nate Begeman9008ca62009-04-27 18:41:29 +00003879 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
3880 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
3881 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003882 for (unsigned i = 0; i != 4; ++i) {
3883 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003884 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00003885 } else {
3886 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003887 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00003888 }
3889 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003890 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00003891}
3892
Dan Gohman475871a2008-07-27 21:46:04 +00003893SDValue
3894X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003895 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00003896 SDValue V1 = Op.getOperand(0);
3897 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003898 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003899 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00003900 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003901 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003902 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3903 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003904 bool V1IsSplat = false;
3905 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003906
Nate Begeman9008ca62009-04-27 18:41:29 +00003907 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00003908 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003909
Nate Begeman9008ca62009-04-27 18:41:29 +00003910 // Promote splats to v4f32.
3911 if (SVOp->isSplat()) {
3912 if (isMMX || NumElems < 4)
3913 return Op;
3914 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003915 }
3916
Evan Cheng7a831ce2007-12-15 03:00:47 +00003917 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3918 // do it!
3919 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003920 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003921 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00003922 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00003923 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00003924 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3925 // FIXME: Figure out a cleaner way to do this.
3926 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00003927 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003928 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00003929 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003930 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
3931 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
3932 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003933 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003934 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003935 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
3936 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00003937 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00003938 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003939 }
3940 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003941
3942 if (X86::isPSHUFDMask(SVOp))
3943 return Op;
3944
Evan Chengf26ffe92008-05-29 08:22:04 +00003945 // Check if this can be converted into a logical shift.
3946 bool isLeft = false;
3947 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003948 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00003949 bool isShift = getSubtarget()->hasSSE2() &&
3950 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00003951 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003952 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00003953 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003954 MVT EVT = VT.getVectorElementType();
3955 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003956 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003957 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003958
3959 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003960 if (V1IsUndef)
3961 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00003962 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003963 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003964 if (!isMMX)
3965 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003966 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003967
3968 // FIXME: fold these into legal mask.
3969 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
3970 X86::isMOVSLDUPMask(SVOp) ||
3971 X86::isMOVHLPSMask(SVOp) ||
3972 X86::isMOVHPMask(SVOp) ||
3973 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003974 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003975
Nate Begeman9008ca62009-04-27 18:41:29 +00003976 if (ShouldXformToMOVHLPS(SVOp) ||
3977 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
3978 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003979
Evan Chengf26ffe92008-05-29 08:22:04 +00003980 if (isShift) {
3981 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003982 MVT EVT = VT.getVectorElementType();
3983 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00003984 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003985 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003986
Evan Cheng9eca5e82006-10-25 21:49:50 +00003987 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003988 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3989 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00003990 V1IsSplat = isSplatVector(V1.getNode());
3991 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00003992
Chris Lattner8a594482007-11-25 00:24:49 +00003993 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003994 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003995 Op = CommuteVectorShuffle(SVOp, DAG);
3996 SVOp = cast<ShuffleVectorSDNode>(Op);
3997 V1 = SVOp->getOperand(0);
3998 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003999 std::swap(V1IsSplat, V2IsSplat);
4000 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004001 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004002 }
4003
Nate Begeman9008ca62009-04-27 18:41:29 +00004004 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4005 // Shuffling low element of v1 into undef, just return v1.
4006 if (V2IsUndef)
4007 return V1;
4008 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4009 // the instruction selector will not match, so get a canonical MOVL with
4010 // swapped operands to undo the commute.
4011 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004012 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004013
Nate Begeman9008ca62009-04-27 18:41:29 +00004014 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4015 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4016 X86::isUNPCKLMask(SVOp) ||
4017 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004018 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004019
Evan Cheng9bbbb982006-10-25 20:48:19 +00004020 if (V2IsSplat) {
4021 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004022 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004023 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004024 SDValue NewMask = NormalizeMask(SVOp, DAG);
4025 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4026 if (NSVOp != SVOp) {
4027 if (X86::isUNPCKLMask(NSVOp, true)) {
4028 return NewMask;
4029 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4030 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004031 }
4032 }
4033 }
4034
Evan Cheng9eca5e82006-10-25 21:49:50 +00004035 if (Commuted) {
4036 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 // FIXME: this seems wrong.
4038 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4039 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4040 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4041 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4042 X86::isUNPCKLMask(NewSVOp) ||
4043 X86::isUNPCKHMask(NewSVOp))
4044 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004045 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004046
Nate Begemanb9a47b82009-02-23 08:49:38 +00004047 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004048
4049 // Normalize the node to match x86 shuffle ops if needed
4050 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4051 return CommuteVectorShuffle(SVOp, DAG);
4052
4053 // Check for legal shuffle and return?
4054 SmallVector<int, 16> PermMask;
4055 SVOp->getMask(PermMask);
4056 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004057 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004058
Evan Cheng14b32e12007-12-11 01:46:18 +00004059 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4060 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004061 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004062 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004063 return NewOp;
4064 }
4065
Nate Begemanb9a47b82009-02-23 08:49:38 +00004066 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004068 if (NewOp.getNode())
4069 return NewOp;
4070 }
4071
Evan Chengace3c172008-07-22 21:13:36 +00004072 // Handle all 4 wide cases with a number of shuffles except for MMX.
4073 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004074 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004075
Dan Gohman475871a2008-07-27 21:46:04 +00004076 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004077}
4078
Dan Gohman475871a2008-07-27 21:46:04 +00004079SDValue
4080X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004081 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004082 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004083 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004084 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004085 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004086 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004087 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004088 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004089 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004090 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004091 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4092 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4093 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004094 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4095 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4096 DAG.getNode(ISD::BIT_CONVERT, dl,
4097 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004098 Op.getOperand(0)),
4099 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004100 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004101 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004102 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004103 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004104 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004105 } else if (VT == MVT::f32) {
4106 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4107 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004108 // result has a single use which is a store or a bitcast to i32. And in
4109 // the case of a store, it's not worth it if the index is a constant 0,
4110 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004111 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004112 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004113 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004114 if ((User->getOpcode() != ISD::STORE ||
4115 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4116 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004117 (User->getOpcode() != ISD::BIT_CONVERT ||
4118 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004119 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004120 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004121 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004122 Op.getOperand(0)),
4123 Op.getOperand(1));
4124 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004125 } else if (VT == MVT::i32) {
4126 // ExtractPS works with constant index.
4127 if (isa<ConstantSDNode>(Op.getOperand(1)))
4128 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004129 }
Dan Gohman475871a2008-07-27 21:46:04 +00004130 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004131}
4132
4133
Dan Gohman475871a2008-07-27 21:46:04 +00004134SDValue
4135X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004136 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004137 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004138
Evan Cheng62a3f152008-03-24 21:52:23 +00004139 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004140 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004141 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004142 return Res;
4143 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004144
Duncan Sands83ec4b62008-06-06 12:08:01 +00004145 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004146 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004147 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004148 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004149 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004150 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004151 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004152 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4153 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004154 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004155 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004156 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004157 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004158 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004159 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004160 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004161 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004162 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004163 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004164 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004165 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004166 if (Idx == 0)
4167 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004168
Evan Cheng0db9fe62006-04-25 20:13:52 +00004169 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004170 int Mask[4] = { Idx, -1, -1, -1 };
4171 MVT VVT = Op.getOperand(0).getValueType();
4172 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4173 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004174 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004175 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004176 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004177 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4178 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4179 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004180 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004181 if (Idx == 0)
4182 return Op;
4183
4184 // UNPCKHPD the element to the lowest double word, then movsd.
4185 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4186 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004187 int Mask[2] = { 1, -1 };
4188 MVT VVT = Op.getOperand(0).getValueType();
4189 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4190 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004191 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004192 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004193 }
4194
Dan Gohman475871a2008-07-27 21:46:04 +00004195 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004196}
4197
Dan Gohman475871a2008-07-27 21:46:04 +00004198SDValue
4199X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004200 MVT VT = Op.getValueType();
4201 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004202 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004203
Dan Gohman475871a2008-07-27 21:46:04 +00004204 SDValue N0 = Op.getOperand(0);
4205 SDValue N1 = Op.getOperand(1);
4206 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004207
Dan Gohmanef521f12008-08-14 22:53:18 +00004208 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4209 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004210 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004211 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004212 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4213 // argument.
4214 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004215 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004216 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004217 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004218 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004219 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004220 // Bits [7:6] of the constant are the source select. This will always be
4221 // zero here. The DAG Combiner may combine an extract_elt index into these
4222 // bits. For example (insert (extract, 3), 2) could be matched by putting
4223 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004224 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004225 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004226 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004227 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004228 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Dale Johannesenace16102009-02-03 19:33:06 +00004229 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004230 } else if (EVT == MVT::i32) {
4231 // InsertPS works with constant index.
4232 if (isa<ConstantSDNode>(N2))
4233 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004234 }
Dan Gohman475871a2008-07-27 21:46:04 +00004235 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004236}
4237
Dan Gohman475871a2008-07-27 21:46:04 +00004238SDValue
4239X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004240 MVT VT = Op.getValueType();
4241 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004242
4243 if (Subtarget->hasSSE41())
4244 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4245
Evan Cheng794405e2007-12-12 07:55:34 +00004246 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004247 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004248
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004249 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004250 SDValue N0 = Op.getOperand(0);
4251 SDValue N1 = Op.getOperand(1);
4252 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004253
Duncan Sands83ec4b62008-06-06 12:08:01 +00004254 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004255 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4256 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004257 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004258 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004259 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004260 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004261 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004262 }
Dan Gohman475871a2008-07-27 21:46:04 +00004263 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004264}
4265
Dan Gohman475871a2008-07-27 21:46:04 +00004266SDValue
4267X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004268 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004269 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004270 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4271 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4272 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004273 Op.getOperand(0))));
4274
Dale Johannesenace16102009-02-03 19:33:06 +00004275 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004276 MVT VT = MVT::v2i32;
4277 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004278 default: break;
4279 case MVT::v16i8:
4280 case MVT::v8i16:
4281 VT = MVT::v4i32;
4282 break;
4283 }
Dale Johannesenace16102009-02-03 19:33:06 +00004284 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4285 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004286}
4287
Bill Wendling056292f2008-09-16 21:48:12 +00004288// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4289// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4290// one of the above mentioned nodes. It has to be wrapped because otherwise
4291// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4292// be used to form addressing mode. These wrapped nodes will be selected
4293// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004294SDValue
4295X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004296 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004297 // FIXME there isn't really any debug info here, should come from the parent
4298 DebugLoc dl = CP->getDebugLoc();
Evan Cheng1606e8e2009-03-13 07:51:59 +00004299 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
4300 CP->getAlignment());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004301 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004302 // With PIC, the address is actually $g + Offset.
4303 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4304 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004305 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004306 DAG.getNode(X86ISD::GlobalBaseReg,
4307 DebugLoc::getUnknownLoc(),
4308 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004309 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004310 }
4311
4312 return Result;
4313}
4314
Dan Gohman475871a2008-07-27 21:46:04 +00004315SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004316X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004317 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004318 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004319 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4320 bool ExtraLoadRequired =
4321 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4322
4323 // Create the TargetGlobalAddress node, folding in the constant
4324 // offset if it is legal.
4325 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004326 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004327 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4328 Offset = 0;
4329 } else
4330 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004331 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004332
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004333 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004334 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004335 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4336 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004337 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004338 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004339
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004340 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4341 // load the value at address GV, not the value of GV itself. This means that
4342 // the GlobalAddress must be in the base or index register of the address, not
4343 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004344 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004345 if (ExtraLoadRequired)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004346 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004347 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004348
Dan Gohman6520e202008-10-18 02:06:02 +00004349 // If there was a non-zero offset that we didn't fold, create an explicit
4350 // addition for it.
4351 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004352 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004353 DAG.getConstant(Offset, getPointerTy()));
4354
Evan Cheng0db9fe62006-04-25 20:13:52 +00004355 return Result;
4356}
4357
Evan Chengda43bcf2008-09-24 00:05:32 +00004358SDValue
4359X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4360 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004361 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004362 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004363}
4364
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004365static SDValue
4366GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Rafael Espindola15f1b662009-04-24 12:59:40 +00004367 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004368 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4369 DebugLoc dl = GA->getDebugLoc();
4370 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4371 GA->getValueType(0),
4372 GA->getOffset());
4373 if (InFlag) {
4374 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004375 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004376 } else {
4377 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004378 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004379 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004380 SDValue Flag = Chain.getValue(1);
4381 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004382}
4383
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004384// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004385static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004386LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004387 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004388 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004389 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4390 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004391 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004392 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004393 PtrVT), InFlag);
4394 InFlag = Chain.getValue(1);
4395
Rafael Espindola15f1b662009-04-24 12:59:40 +00004396 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004397}
4398
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004399// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004400static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004401LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004402 const MVT PtrVT) {
Rafael Espindola15f1b662009-04-24 12:59:40 +00004403 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004404}
4405
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004406// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4407// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004408static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004409 const MVT PtrVT, TLSModel::Model model,
4410 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004411 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004412 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004413 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4414 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004415 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4416 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004417
4418 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4419 NULL, 0);
4420
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004421 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4422 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004423 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004424 GA->getValueType(0),
4425 GA->getOffset());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004426 SDValue Offset = DAG.getNode(X86ISD::Wrapper, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004427
Rafael Espindola9a580232009-02-27 13:37:18 +00004428 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004429 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004430 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004431
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004432 // The address of the thread local variable is the add of the thread
4433 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004434 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004435}
4436
Dan Gohman475871a2008-07-27 21:46:04 +00004437SDValue
4438X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004439 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004440 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004441 assert(Subtarget->isTargetELF() &&
4442 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004443 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Rafael Espindola9a580232009-02-27 13:37:18 +00004444 GlobalValue *GV = GA->getGlobal();
4445 TLSModel::Model model =
4446 getTLSModel (GV, getTargetMachine().getRelocationModel());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004447 if (Subtarget->is64Bit()) {
Rafael Espindola9a580232009-02-27 13:37:18 +00004448 switch (model) {
4449 case TLSModel::GeneralDynamic:
4450 case TLSModel::LocalDynamic: // not implemented
Rafael Espindola9a580232009-02-27 13:37:18 +00004451 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004452
4453 case TLSModel::InitialExec:
4454 case TLSModel::LocalExec:
4455 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, true);
Rafael Espindola9a580232009-02-27 13:37:18 +00004456 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004457 } else {
Rafael Espindola9a580232009-02-27 13:37:18 +00004458 switch (model) {
4459 case TLSModel::GeneralDynamic:
4460 case TLSModel::LocalDynamic: // not implemented
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004461 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
Rafael Espindola9a580232009-02-27 13:37:18 +00004462
4463 case TLSModel::InitialExec:
4464 case TLSModel::LocalExec:
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004465 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model, false);
Rafael Espindola9a580232009-02-27 13:37:18 +00004466 }
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004467 }
Chris Lattner5867de12009-04-01 22:14:45 +00004468 assert(0 && "Unreachable");
4469 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004470}
4471
Dan Gohman475871a2008-07-27 21:46:04 +00004472SDValue
4473X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Dale Johannesende064702009-02-06 21:50:26 +00004474 // FIXME there isn't really any debug info here
4475 DebugLoc dl = Op.getDebugLoc();
Bill Wendling056292f2008-09-16 21:48:12 +00004476 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4477 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004478 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004479 // With PIC, the address is actually $g + Offset.
4480 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4481 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004482 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Scott Michelfdc40a02009-02-17 22:15:04 +00004483 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004484 DebugLoc::getUnknownLoc(),
4485 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004486 Result);
4487 }
4488
4489 return Result;
4490}
4491
Dan Gohman475871a2008-07-27 21:46:04 +00004492SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004493 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dale Johannesende064702009-02-06 21:50:26 +00004494 // FIXME there isn't really any debug into here
4495 DebugLoc dl = JT->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004496 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004497 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004498 // With PIC, the address is actually $g + Offset.
4499 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4500 !Subtarget->isPICStyleRIPRel()) {
Dale Johannesende064702009-02-06 21:50:26 +00004501 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004502 DAG.getNode(X86ISD::GlobalBaseReg,
4503 DebugLoc::getUnknownLoc(),
4504 getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004505 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004506 }
4507
4508 return Result;
4509}
4510
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004511/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004512/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004513SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004514 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004515 MVT VT = Op.getValueType();
4516 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004517 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004518 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004519 SDValue ShOpLo = Op.getOperand(0);
4520 SDValue ShOpHi = Op.getOperand(1);
4521 SDValue ShAmt = Op.getOperand(2);
4522 SDValue Tmp1 = isSRA ?
Scott Michelfdc40a02009-02-17 22:15:04 +00004523 DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
Dale Johannesenace16102009-02-03 19:33:06 +00004524 DAG.getConstant(VTBits - 1, MVT::i8)) :
Dan Gohman4c1fa612008-03-03 22:22:09 +00004525 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004526
Dan Gohman475871a2008-07-27 21:46:04 +00004527 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004528 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004529 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4530 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004531 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004532 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4533 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004534 }
Evan Chenge3413162006-01-09 18:33:28 +00004535
Dale Johannesenace16102009-02-03 19:33:06 +00004536 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004537 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004538 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004539 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004540
Dan Gohman475871a2008-07-27 21:46:04 +00004541 SDValue Hi, Lo;
4542 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4543 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4544 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004545
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004546 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004547 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4548 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004549 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004550 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4551 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004552 }
4553
Dan Gohman475871a2008-07-27 21:46:04 +00004554 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004555 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004556}
Evan Chenga3195e82006-01-12 22:54:21 +00004557
Dan Gohman475871a2008-07-27 21:46:04 +00004558SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004559 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004560 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004561 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004562
Eli Friedman36df4992009-05-27 00:47:34 +00004563 // These are really Legal; return the operand so the caller accepts it as
4564 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004565 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004566 return Op;
4567 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4568 Subtarget->is64Bit()) {
4569 return Op;
4570 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004571
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004572 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004573 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004574 MachineFunction &MF = DAG.getMachineFunction();
4575 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004576 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004577 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004578 StackSlot,
4579 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004580 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4581}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004582
Eli Friedman948e95a2009-05-23 09:59:16 +00004583SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4584 SDValue StackSlot,
4585 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004586 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004587 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004588 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004589 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004590 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004591 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4592 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004593 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004594 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004595 Ops.push_back(Chain);
4596 Ops.push_back(StackSlot);
4597 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004598 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004599 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004600
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004601 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004602 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004604
4605 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4606 // shouldn't be necessary except that RFP cannot be live across
4607 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004608 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004609 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004610 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004611 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004612 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004613 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004614 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004615 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004616 Ops.push_back(DAG.getValueType(Op.getValueType()));
4617 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004618 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4619 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004620 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004621 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004622
Evan Cheng0db9fe62006-04-25 20:13:52 +00004623 return Result;
4624}
4625
Bill Wendling8b8a6362009-01-17 03:56:04 +00004626// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4627SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4628 // This algorithm is not obvious. Here it is in C code, more or less:
4629 /*
4630 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4631 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4632 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004633
Bill Wendling8b8a6362009-01-17 03:56:04 +00004634 // Copy ints to xmm registers.
4635 __m128i xh = _mm_cvtsi32_si128( hi );
4636 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004637
Bill Wendling8b8a6362009-01-17 03:56:04 +00004638 // Combine into low half of a single xmm register.
4639 __m128i x = _mm_unpacklo_epi32( xh, xl );
4640 __m128d d;
4641 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004642
Bill Wendling8b8a6362009-01-17 03:56:04 +00004643 // Merge in appropriate exponents to give the integer bits the right
4644 // magnitude.
4645 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004646
Bill Wendling8b8a6362009-01-17 03:56:04 +00004647 // Subtract away the biases to deal with the IEEE-754 double precision
4648 // implicit 1.
4649 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004650
Bill Wendling8b8a6362009-01-17 03:56:04 +00004651 // All conversions up to here are exact. The correctly rounded result is
4652 // calculated using the current rounding mode using the following
4653 // horizontal add.
4654 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4655 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4656 // store doesn't really need to be here (except
4657 // maybe to zero the other double)
4658 return sd;
4659 }
4660 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004661
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004662 DebugLoc dl = Op.getDebugLoc();
Dale Johannesenace16102009-02-03 19:33:06 +00004663
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004664 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004665 std::vector<Constant*> CV0;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004666 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4667 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4668 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4669 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4670 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004671 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004672
Bill Wendling8b8a6362009-01-17 03:56:04 +00004673 std::vector<Constant*> CV1;
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004674 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4675 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4676 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004677 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004678
Dale Johannesenace16102009-02-03 19:33:06 +00004679 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4680 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004681 Op.getOperand(0),
4682 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004683 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4684 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004685 Op.getOperand(0),
4686 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004687 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004688 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004689 PseudoSourceValue::getConstantPool(), 0,
4690 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004691 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004692 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4693 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004694 PseudoSourceValue::getConstantPool(), 0,
4695 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004696 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004697
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004698 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004699 int ShufMask[2] = { 1, -1 };
4700 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4701 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004702 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4703 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004704 DAG.getIntPtrConstant(0));
4705}
4706
Bill Wendling8b8a6362009-01-17 03:56:04 +00004707// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4708SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004709 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004710 // FP constant to bias correct the final result.
4711 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4712 MVT::f64);
4713
4714 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004715 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4716 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004717 Op.getOperand(0),
4718 DAG.getIntPtrConstant(0)));
4719
Dale Johannesenace16102009-02-03 19:33:06 +00004720 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4721 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004722 DAG.getIntPtrConstant(0));
4723
4724 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004725 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4726 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4727 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004728 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004729 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4730 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004731 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004732 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4733 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004734 DAG.getIntPtrConstant(0));
4735
4736 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004737 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004738
4739 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004740 MVT DestVT = Op.getValueType();
4741
4742 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004743 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004744 DAG.getIntPtrConstant(0));
4745 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004746 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004747 }
4748
4749 // Handle final rounding.
4750 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004751}
4752
4753SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004754 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004755 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004756
Evan Chenga06ec9e2009-01-19 08:08:22 +00004757 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4758 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4759 // the optimization here.
4760 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004761 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004762
4763 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004764 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004765 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004766 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004767 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004768
Bill Wendling8b8a6362009-01-17 03:56:04 +00004769 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004770 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004771 return LowerUINT_TO_FP_i32(Op, DAG);
4772 }
4773
Eli Friedman948e95a2009-05-23 09:59:16 +00004774 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4775
4776 // Make a 64-bit buffer, and use it to build an FILD.
4777 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4778 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4779 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4780 getPointerTy(), StackSlot, WordOff);
4781 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4782 StackSlot, NULL, 0);
4783 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4784 OffsetSlot, NULL, 0);
4785 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004786}
4787
Dan Gohman475871a2008-07-27 21:46:04 +00004788std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004789FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004790 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00004791
4792 MVT DstTy = Op.getValueType();
4793
4794 if (!IsSigned) {
4795 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
4796 DstTy = MVT::i64;
4797 }
4798
4799 assert(DstTy.getSimpleVT() <= MVT::i64 &&
4800 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004802
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004803 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00004804 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004805 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004806 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004807 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00004808 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00004809 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004810 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004811
Evan Cheng87c89352007-10-15 20:11:21 +00004812 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4813 // stack slot.
4814 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00004815 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004816 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00004818
Evan Cheng0db9fe62006-04-25 20:13:52 +00004819 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00004820 switch (DstTy.getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004821 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4822 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4823 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4824 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004825 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004826
Dan Gohman475871a2008-07-27 21:46:04 +00004827 SDValue Chain = DAG.getEntryNode();
4828 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004829 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004830 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00004831 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004832 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004833 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004834 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004835 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4836 };
Dale Johannesenace16102009-02-03 19:33:06 +00004837 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004838 Chain = Value.getValue(1);
4839 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4840 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4841 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004842
Evan Cheng0db9fe62006-04-25 20:13:52 +00004843 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004844 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00004845 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004846
Chris Lattner27a6c732007-11-24 07:07:01 +00004847 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004848}
4849
Dan Gohman475871a2008-07-27 21:46:04 +00004850SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman948e95a2009-05-23 09:59:16 +00004851 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00004852 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00004853 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
4854 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00004855
Chris Lattner27a6c732007-11-24 07:07:01 +00004856 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004857 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00004858 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004859}
4860
Eli Friedman948e95a2009-05-23 09:59:16 +00004861SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
4862 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
4863 SDValue FIST = Vals.first, StackSlot = Vals.second;
4864 assert(FIST.getNode() && "Unexpected failure");
4865
4866 // Load the result.
4867 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
4868 FIST, StackSlot, NULL, 0);
4869}
4870
Dan Gohman475871a2008-07-27 21:46:04 +00004871SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004872 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004873 MVT VT = Op.getValueType();
4874 MVT EltVT = VT;
4875 if (VT.isVector())
4876 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004877 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004878 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004879 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004880 CV.push_back(C);
4881 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004882 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004883 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004884 CV.push_back(C);
4885 CV.push_back(C);
4886 CV.push_back(C);
4887 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004888 }
Dan Gohmand3006222007-07-27 17:16:43 +00004889 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004890 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004891 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004892 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004893 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004894 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004895}
4896
Dan Gohman475871a2008-07-27 21:46:04 +00004897SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004898 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004899 MVT VT = Op.getValueType();
4900 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004901 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004902 if (VT.isVector()) {
4903 EltVT = VT.getVectorElementType();
4904 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004905 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004906 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004907 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004908 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004909 CV.push_back(C);
4910 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004911 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004912 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004913 CV.push_back(C);
4914 CV.push_back(C);
4915 CV.push_back(C);
4916 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004917 }
Dan Gohmand3006222007-07-27 17:16:43 +00004918 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004919 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004920 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004921 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004922 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004923 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00004924 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
4925 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00004926 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00004927 Op.getOperand(0)),
4928 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00004929 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004930 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00004931 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004932}
4933
Dan Gohman475871a2008-07-27 21:46:04 +00004934SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4935 SDValue Op0 = Op.getOperand(0);
4936 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004937 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004938 MVT VT = Op.getValueType();
4939 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004940
4941 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004942 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004943 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004944 SrcVT = VT;
4945 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004946 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004947 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004948 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004949 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004950 }
4951
4952 // At this point the operands and the result should have the same
4953 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004954
Evan Cheng68c47cb2007-01-05 07:55:56 +00004955 // First get the sign bit of second operand.
4956 std::vector<Constant*> CV;
4957 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004958 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4959 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004960 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004961 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4962 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4963 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4964 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004965 }
Dan Gohmand3006222007-07-27 17:16:43 +00004966 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004967 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004968 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004969 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004970 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004971 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004972
4973 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004974 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004975 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00004976 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
4977 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00004978 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00004979 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
4980 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004981 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004982 }
4983
Evan Cheng73d6cf12007-01-05 21:37:56 +00004984 // Clear first operand sign bit.
4985 CV.clear();
4986 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004987 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4988 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004989 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004990 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4991 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4992 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4993 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004994 }
Dan Gohmand3006222007-07-27 17:16:43 +00004995 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004996 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004997 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004998 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004999 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005000 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005001
5002 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005003 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005004}
5005
Dan Gohman076aee32009-03-04 19:44:21 +00005006/// Emit nodes that will be selected as "test Op0,Op0", or something
5007/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005008SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5009 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005010 DebugLoc dl = Op.getDebugLoc();
5011
Dan Gohman31125812009-03-07 01:58:32 +00005012 // CF and OF aren't always set the way we want. Determine which
5013 // of these we need.
5014 bool NeedCF = false;
5015 bool NeedOF = false;
5016 switch (X86CC) {
5017 case X86::COND_A: case X86::COND_AE:
5018 case X86::COND_B: case X86::COND_BE:
5019 NeedCF = true;
5020 break;
5021 case X86::COND_G: case X86::COND_GE:
5022 case X86::COND_L: case X86::COND_LE:
5023 case X86::COND_O: case X86::COND_NO:
5024 NeedOF = true;
5025 break;
5026 default: break;
5027 }
5028
Dan Gohman076aee32009-03-04 19:44:21 +00005029 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005030 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5031 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5032 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005033 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005034 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005035 switch (Op.getNode()->getOpcode()) {
5036 case ISD::ADD:
5037 // Due to an isel shortcoming, be conservative if this add is likely to
5038 // be selected as part of a load-modify-store instruction. When the root
5039 // node in a match is a store, isel doesn't know how to remap non-chain
5040 // non-flag uses of other nodes in the match, such as the ADD in this
5041 // case. This leads to the ADD being left around and reselected, with
5042 // the result being two adds in the output.
5043 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5044 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5045 if (UI->getOpcode() == ISD::STORE)
5046 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005047 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005048 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5049 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005050 if (C->getAPIntValue() == 1) {
5051 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005052 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005053 break;
5054 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005055 // An add of negative one (subtract of one) will be selected as a DEC.
5056 if (C->getAPIntValue().isAllOnesValue()) {
5057 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005058 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005059 break;
5060 }
5061 }
Dan Gohman076aee32009-03-04 19:44:21 +00005062 // Otherwise use a regular EFLAGS-setting add.
5063 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005064 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005065 break;
5066 case ISD::SUB:
5067 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5068 // likely to be selected as part of a load-modify-store instruction.
5069 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5070 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5071 if (UI->getOpcode() == ISD::STORE)
5072 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005073 // Otherwise use a regular EFLAGS-setting sub.
5074 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005075 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005076 break;
5077 case X86ISD::ADD:
5078 case X86ISD::SUB:
5079 case X86ISD::INC:
5080 case X86ISD::DEC:
5081 return SDValue(Op.getNode(), 1);
5082 default:
5083 default_case:
5084 break;
5085 }
5086 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005087 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005088 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005089 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005090 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005091 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005092 DAG.ReplaceAllUsesWith(Op, New);
5093 return SDValue(New.getNode(), 1);
5094 }
5095 }
5096
5097 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5098 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5099 DAG.getConstant(0, Op.getValueType()));
5100}
5101
5102/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5103/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005104SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5105 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005106 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5107 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005108 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005109
5110 DebugLoc dl = Op0.getDebugLoc();
5111 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5112}
5113
Dan Gohman475871a2008-07-27 21:46:04 +00005114SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005115 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005116 SDValue Op0 = Op.getOperand(0);
5117 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005118 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005119 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005120
Dan Gohmane5af2d32009-01-29 01:59:02 +00005121 // Lower (X & (1 << N)) == 0 to BT(X, N).
5122 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5123 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005124 if (Op0.getOpcode() == ISD::AND &&
5125 Op0.hasOneUse() &&
5126 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005127 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005128 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005129 SDValue LHS, RHS;
5130 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5131 if (ConstantSDNode *Op010C =
5132 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5133 if (Op010C->getZExtValue() == 1) {
5134 LHS = Op0.getOperand(0);
5135 RHS = Op0.getOperand(1).getOperand(1);
5136 }
5137 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5138 if (ConstantSDNode *Op000C =
5139 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5140 if (Op000C->getZExtValue() == 1) {
5141 LHS = Op0.getOperand(1);
5142 RHS = Op0.getOperand(0).getOperand(1);
5143 }
5144 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5145 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5146 SDValue AndLHS = Op0.getOperand(0);
5147 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5148 LHS = AndLHS.getOperand(0);
5149 RHS = AndLHS.getOperand(1);
5150 }
5151 }
Evan Cheng0488db92007-09-25 01:57:46 +00005152
Dan Gohmane5af2d32009-01-29 01:59:02 +00005153 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005154 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5155 // instruction. Since the shift amount is in-range-or-undefined, we know
5156 // that doing a bittest on the i16 value is ok. We extend to i32 because
5157 // the encoding for the i16 version is larger than the i32 version.
5158 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005159 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005160
5161 // If the operand types disagree, extend the shift amount to match. Since
5162 // BT ignores high bits (like shifts) we can use anyextend.
5163 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005164 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005165
Dale Johannesenace16102009-02-03 19:33:06 +00005166 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005167 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005168 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005169 DAG.getConstant(Cond, MVT::i8), BT);
5170 }
5171 }
5172
5173 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5174 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005175
Dan Gohman31125812009-03-07 01:58:32 +00005176 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005177 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005178 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005179}
5180
Dan Gohman475871a2008-07-27 21:46:04 +00005181SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5182 SDValue Cond;
5183 SDValue Op0 = Op.getOperand(0);
5184 SDValue Op1 = Op.getOperand(1);
5185 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005186 MVT VT = Op.getValueType();
5187 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5188 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005189 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005190
5191 if (isFP) {
5192 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005193 MVT VT0 = Op0.getValueType();
5194 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5195 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005196 bool Swap = false;
5197
5198 switch (SetCCOpcode) {
5199 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005200 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005201 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005202 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005203 case ISD::SETGT: Swap = true; // Fallthrough
5204 case ISD::SETLT:
5205 case ISD::SETOLT: SSECC = 1; break;
5206 case ISD::SETOGE:
5207 case ISD::SETGE: Swap = true; // Fallthrough
5208 case ISD::SETLE:
5209 case ISD::SETOLE: SSECC = 2; break;
5210 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005211 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005212 case ISD::SETNE: SSECC = 4; break;
5213 case ISD::SETULE: Swap = true;
5214 case ISD::SETUGE: SSECC = 5; break;
5215 case ISD::SETULT: Swap = true;
5216 case ISD::SETUGT: SSECC = 6; break;
5217 case ISD::SETO: SSECC = 7; break;
5218 }
5219 if (Swap)
5220 std::swap(Op0, Op1);
5221
Nate Begemanfb8ead02008-07-25 19:05:58 +00005222 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005223 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005224 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005225 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005226 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5227 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5228 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005229 }
5230 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005231 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005232 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5233 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5234 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005235 }
5236 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005237 }
5238 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005239 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005240 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005241
Nate Begeman30a0de92008-07-17 16:51:19 +00005242 // We are handling one of the integer comparisons here. Since SSE only has
5243 // GT and EQ comparisons for integer, swapping operands and multiple
5244 // operations may be required for some comparisons.
5245 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5246 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005247
Nate Begeman30a0de92008-07-17 16:51:19 +00005248 switch (VT.getSimpleVT()) {
5249 default: break;
5250 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5251 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5252 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5253 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5254 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005255
Nate Begeman30a0de92008-07-17 16:51:19 +00005256 switch (SetCCOpcode) {
5257 default: break;
5258 case ISD::SETNE: Invert = true;
5259 case ISD::SETEQ: Opc = EQOpc; break;
5260 case ISD::SETLT: Swap = true;
5261 case ISD::SETGT: Opc = GTOpc; break;
5262 case ISD::SETGE: Swap = true;
5263 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5264 case ISD::SETULT: Swap = true;
5265 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5266 case ISD::SETUGE: Swap = true;
5267 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5268 }
5269 if (Swap)
5270 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005271
Nate Begeman30a0de92008-07-17 16:51:19 +00005272 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5273 // bits of the inputs before performing those operations.
5274 if (FlipSigns) {
5275 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005276 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5277 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005278 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005279 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5280 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005281 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5282 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005283 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005284
Dale Johannesenace16102009-02-03 19:33:06 +00005285 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005286
5287 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005288 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005289 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005290
Nate Begeman30a0de92008-07-17 16:51:19 +00005291 return Result;
5292}
Evan Cheng0488db92007-09-25 01:57:46 +00005293
Evan Cheng370e5342008-12-03 08:38:43 +00005294// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005295static bool isX86LogicalCmp(SDValue Op) {
5296 unsigned Opc = Op.getNode()->getOpcode();
5297 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5298 return true;
5299 if (Op.getResNo() == 1 &&
5300 (Opc == X86ISD::ADD ||
5301 Opc == X86ISD::SUB ||
5302 Opc == X86ISD::SMUL ||
5303 Opc == X86ISD::UMUL ||
5304 Opc == X86ISD::INC ||
5305 Opc == X86ISD::DEC))
5306 return true;
5307
5308 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005309}
5310
Dan Gohman475871a2008-07-27 21:46:04 +00005311SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005312 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005313 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005314 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005315 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005316
Evan Cheng734503b2006-09-11 02:19:56 +00005317 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005318 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005319
Evan Cheng3f41d662007-10-08 22:16:29 +00005320 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5321 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005322 if (Cond.getOpcode() == X86ISD::SETCC) {
5323 CC = Cond.getOperand(0);
5324
Dan Gohman475871a2008-07-27 21:46:04 +00005325 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005326 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005327 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005328
Evan Cheng3f41d662007-10-08 22:16:29 +00005329 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005330 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005331 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005332 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005333
Chris Lattnerd1980a52009-03-12 06:52:53 +00005334 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5335 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005336 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005337 addTest = false;
5338 }
5339 }
5340
5341 if (addTest) {
5342 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005343 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005344 }
5345
Dan Gohmanfc166572009-04-09 23:54:40 +00005346 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005347 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005348 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5349 // condition is true.
5350 Ops.push_back(Op.getOperand(2));
5351 Ops.push_back(Op.getOperand(1));
5352 Ops.push_back(CC);
5353 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005354 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005355}
5356
Evan Cheng370e5342008-12-03 08:38:43 +00005357// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5358// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5359// from the AND / OR.
5360static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5361 Opc = Op.getOpcode();
5362 if (Opc != ISD::OR && Opc != ISD::AND)
5363 return false;
5364 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5365 Op.getOperand(0).hasOneUse() &&
5366 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5367 Op.getOperand(1).hasOneUse());
5368}
5369
Evan Cheng961d6d42009-02-02 08:19:07 +00005370// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5371// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005372static bool isXor1OfSetCC(SDValue Op) {
5373 if (Op.getOpcode() != ISD::XOR)
5374 return false;
5375 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5376 if (N1C && N1C->getAPIntValue() == 1) {
5377 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5378 Op.getOperand(0).hasOneUse();
5379 }
5380 return false;
5381}
5382
Dan Gohman475871a2008-07-27 21:46:04 +00005383SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005384 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005385 SDValue Chain = Op.getOperand(0);
5386 SDValue Cond = Op.getOperand(1);
5387 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005388 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005389 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005390
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005392 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005393#if 0
5394 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005395 else if (Cond.getOpcode() == X86ISD::ADD ||
5396 Cond.getOpcode() == X86ISD::SUB ||
5397 Cond.getOpcode() == X86ISD::SMUL ||
5398 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005399 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005400#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005401
Evan Cheng3f41d662007-10-08 22:16:29 +00005402 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5403 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005404 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005405 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005406
Dan Gohman475871a2008-07-27 21:46:04 +00005407 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005408 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005409 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005410 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005411 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005412 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005413 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005414 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005415 default: break;
5416 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005417 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005418 // These can only come from an arithmetic instruction with overflow,
5419 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005420 Cond = Cond.getNode()->getOperand(1);
5421 addTest = false;
5422 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005423 }
Evan Cheng0488db92007-09-25 01:57:46 +00005424 }
Evan Cheng370e5342008-12-03 08:38:43 +00005425 } else {
5426 unsigned CondOpc;
5427 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5428 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005429 if (CondOpc == ISD::OR) {
5430 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5431 // two branches instead of an explicit OR instruction with a
5432 // separate test.
5433 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005434 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005435 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005436 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005437 Chain, Dest, CC, Cmp);
5438 CC = Cond.getOperand(1).getOperand(0);
5439 Cond = Cmp;
5440 addTest = false;
5441 }
5442 } else { // ISD::AND
5443 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5444 // two branches instead of an explicit AND instruction with a
5445 // separate test. However, we only do this if this block doesn't
5446 // have a fall-through edge, because this requires an explicit
5447 // jmp when the condition is false.
5448 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005449 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005450 Op.getNode()->hasOneUse()) {
5451 X86::CondCode CCode =
5452 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5453 CCode = X86::GetOppositeBranchCondition(CCode);
5454 CC = DAG.getConstant(CCode, MVT::i8);
5455 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5456 // Look for an unconditional branch following this conditional branch.
5457 // We need this because we need to reverse the successors in order
5458 // to implement FCMP_OEQ.
5459 if (User.getOpcode() == ISD::BR) {
5460 SDValue FalseBB = User.getOperand(1);
5461 SDValue NewBR =
5462 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5463 assert(NewBR == User);
5464 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005465
Dale Johannesene4d209d2009-02-03 20:21:25 +00005466 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005467 Chain, Dest, CC, Cmp);
5468 X86::CondCode CCode =
5469 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5470 CCode = X86::GetOppositeBranchCondition(CCode);
5471 CC = DAG.getConstant(CCode, MVT::i8);
5472 Cond = Cmp;
5473 addTest = false;
5474 }
5475 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005476 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005477 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5478 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5479 // It should be transformed during dag combiner except when the condition
5480 // is set by a arithmetics with overflow node.
5481 X86::CondCode CCode =
5482 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5483 CCode = X86::GetOppositeBranchCondition(CCode);
5484 CC = DAG.getConstant(CCode, MVT::i8);
5485 Cond = Cond.getOperand(0).getOperand(1);
5486 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005487 }
Evan Cheng0488db92007-09-25 01:57:46 +00005488 }
5489
5490 if (addTest) {
5491 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005492 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005493 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005494 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005495 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005496}
5497
Anton Korobeynikove060b532007-04-17 19:34:00 +00005498
5499// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5500// Calls to _alloca is needed to probe the stack when allocating more than 4k
5501// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5502// that the guard pages used by the OS virtual memory manager are allocated in
5503// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005504SDValue
5505X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005506 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005507 assert(Subtarget->isTargetCygMing() &&
5508 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005509 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005510
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005511 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005512 SDValue Chain = Op.getOperand(0);
5513 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005514 // FIXME: Ensure alignment here
5515
Dan Gohman475871a2008-07-27 21:46:04 +00005516 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005517
Duncan Sands83ec4b62008-06-06 12:08:01 +00005518 MVT IntPtr = getPointerTy();
5519 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005520
Chris Lattnere563bbc2008-10-11 22:08:30 +00005521 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005522
Dale Johannesendd64c412009-02-04 00:33:20 +00005523 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005524 Flag = Chain.getValue(1);
5525
5526 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005527 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005528 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005529 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005530 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005531 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005532 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005533 Flag = Chain.getValue(1);
5534
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005535 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005536 DAG.getIntPtrConstant(0, true),
5537 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005538 Flag);
5539
Dale Johannesendd64c412009-02-04 00:33:20 +00005540 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005541
Dan Gohman475871a2008-07-27 21:46:04 +00005542 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005543 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005544}
5545
Dan Gohman475871a2008-07-27 21:46:04 +00005546SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005547X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005548 SDValue Chain,
5549 SDValue Dst, SDValue Src,
5550 SDValue Size, unsigned Align,
5551 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005552 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005553 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005554
Bill Wendling6f287b22008-09-30 21:22:07 +00005555 // If not DWORD aligned or size is more than the threshold, call the library.
5556 // The libc version is likely to be faster for these cases. It can use the
5557 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005558 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005559 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005560 ConstantSize->getZExtValue() >
5561 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005562 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005563
5564 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005565 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005566
Bill Wendling6158d842008-10-01 00:59:58 +00005567 if (const char *bzeroEntry = V &&
5568 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5569 MVT IntPtr = getPointerTy();
5570 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005571 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005572 TargetLowering::ArgListEntry Entry;
5573 Entry.Node = Dst;
5574 Entry.Ty = IntPtrTy;
5575 Args.push_back(Entry);
5576 Entry.Node = Size;
5577 Args.push_back(Entry);
5578 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005579 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5580 CallingConv::C, false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005581 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005582 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005583 }
5584
Dan Gohman707e0182008-04-12 04:36:06 +00005585 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005586 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005587 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005588
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005589 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005590 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005591 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005592 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005593 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005594 unsigned BytesLeft = 0;
5595 bool TwoRepStos = false;
5596 if (ValC) {
5597 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005598 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005599
Evan Cheng0db9fe62006-04-25 20:13:52 +00005600 // If the value is a constant, then we can potentially use larger sets.
5601 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005602 case 2: // WORD aligned
5603 AVT = MVT::i16;
5604 ValReg = X86::AX;
5605 Val = (Val << 8) | Val;
5606 break;
5607 case 0: // DWORD aligned
5608 AVT = MVT::i32;
5609 ValReg = X86::EAX;
5610 Val = (Val << 8) | Val;
5611 Val = (Val << 16) | Val;
5612 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5613 AVT = MVT::i64;
5614 ValReg = X86::RAX;
5615 Val = (Val << 32) | Val;
5616 }
5617 break;
5618 default: // Byte aligned
5619 AVT = MVT::i8;
5620 ValReg = X86::AL;
5621 Count = DAG.getIntPtrConstant(SizeVal);
5622 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005623 }
5624
Duncan Sands8e4eb092008-06-08 20:54:56 +00005625 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005626 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005627 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5628 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005629 }
5630
Dale Johannesen0f502f62009-02-03 22:26:09 +00005631 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005632 InFlag);
5633 InFlag = Chain.getValue(1);
5634 } else {
5635 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005636 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005637 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005638 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005639 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005640
Scott Michelfdc40a02009-02-17 22:15:04 +00005641 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005642 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005643 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005644 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005645 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005646 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005647 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005648 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005649
Chris Lattnerd96d0722007-02-25 06:40:16 +00005650 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005651 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005652 Ops.push_back(Chain);
5653 Ops.push_back(DAG.getValueType(AVT));
5654 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005655 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005656
Evan Cheng0db9fe62006-04-25 20:13:52 +00005657 if (TwoRepStos) {
5658 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005659 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005660 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005661 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005662 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005663 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005664 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005665 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005666 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005667 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005668 Ops.clear();
5669 Ops.push_back(Chain);
5670 Ops.push_back(DAG.getValueType(MVT::i8));
5671 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005672 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005673 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005674 // Handle the last 1 - 7 bytes.
5675 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005676 MVT AddrVT = Dst.getValueType();
5677 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005678
Dale Johannesen0f502f62009-02-03 22:26:09 +00005679 Chain = DAG.getMemset(Chain, dl,
5680 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005681 DAG.getConstant(Offset, AddrVT)),
5682 Src,
5683 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005684 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005685 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005686
Dan Gohman707e0182008-04-12 04:36:06 +00005687 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005688 return Chain;
5689}
Evan Cheng11e15b32006-04-03 20:53:28 +00005690
Dan Gohman475871a2008-07-27 21:46:04 +00005691SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005692X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005693 SDValue Chain, SDValue Dst, SDValue Src,
5694 SDValue Size, unsigned Align,
5695 bool AlwaysInline,
5696 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005697 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005698 // This requires the copy size to be a constant, preferrably
5699 // within a subtarget-specific limit.
5700 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5701 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005702 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005703 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005704 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005705 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005706
Evan Cheng1887c1c2008-08-21 21:00:15 +00005707 /// If not DWORD aligned, call the library.
5708 if ((Align & 3) != 0)
5709 return SDValue();
5710
5711 // DWORD aligned
5712 MVT AVT = MVT::i32;
5713 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005714 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005715
Duncan Sands83ec4b62008-06-06 12:08:01 +00005716 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005717 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005718 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005719 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005720
Dan Gohman475871a2008-07-27 21:46:04 +00005721 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005722 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005723 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005724 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005725 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005726 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005727 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005728 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005729 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005730 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005731 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005732 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005733 InFlag = Chain.getValue(1);
5734
Chris Lattnerd96d0722007-02-25 06:40:16 +00005735 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005736 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005737 Ops.push_back(Chain);
5738 Ops.push_back(DAG.getValueType(AVT));
5739 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005740 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005741
Dan Gohman475871a2008-07-27 21:46:04 +00005742 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005743 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005744 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005745 // Handle the last 1 - 7 bytes.
5746 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005747 MVT DstVT = Dst.getValueType();
5748 MVT SrcVT = Src.getValueType();
5749 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005750 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005751 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005752 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005753 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005754 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005755 DAG.getConstant(BytesLeft, SizeVT),
5756 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005757 DstSV, DstSVOff + Offset,
5758 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005759 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005760
Scott Michelfdc40a02009-02-17 22:15:04 +00005761 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005762 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005763}
5764
Dan Gohman475871a2008-07-27 21:46:04 +00005765SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005766 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005767 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005768
Evan Cheng25ab6902006-09-08 06:48:29 +00005769 if (!Subtarget->is64Bit()) {
5770 // vastart just stores the address of the VarArgsFrameIndex slot into the
5771 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005772 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005773 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005774 }
5775
5776 // __va_list_tag:
5777 // gp_offset (0 - 6 * 8)
5778 // fp_offset (48 - 48 + 8 * 16)
5779 // overflow_arg_area (point to parameters coming in memory).
5780 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005781 SmallVector<SDValue, 8> MemOps;
5782 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005783 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00005784 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005785 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005786 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005787 MemOps.push_back(Store);
5788
5789 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00005790 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005791 FIN, DAG.getIntPtrConstant(4));
5792 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00005793 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005794 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005795 MemOps.push_back(Store);
5796
5797 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00005798 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005799 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005800 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005801 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005802 MemOps.push_back(Store);
5803
5804 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00005805 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00005806 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005807 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005808 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005809 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00005810 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00005811 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005812}
5813
Dan Gohman475871a2008-07-27 21:46:04 +00005814SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005815 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5816 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005817 SDValue Chain = Op.getOperand(0);
5818 SDValue SrcPtr = Op.getOperand(1);
5819 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005820
5821 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5822 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005823 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005824}
5825
Dan Gohman475871a2008-07-27 21:46:04 +00005826SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005827 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005828 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005829 SDValue Chain = Op.getOperand(0);
5830 SDValue DstPtr = Op.getOperand(1);
5831 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005832 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5833 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005834 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00005835
Dale Johannesendd64c412009-02-04 00:33:20 +00005836 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00005837 DAG.getIntPtrConstant(24), 8, false,
5838 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005839}
5840
Dan Gohman475871a2008-07-27 21:46:04 +00005841SDValue
5842X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005843 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005844 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005845 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005846 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005847 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005848 case Intrinsic::x86_sse_comieq_ss:
5849 case Intrinsic::x86_sse_comilt_ss:
5850 case Intrinsic::x86_sse_comile_ss:
5851 case Intrinsic::x86_sse_comigt_ss:
5852 case Intrinsic::x86_sse_comige_ss:
5853 case Intrinsic::x86_sse_comineq_ss:
5854 case Intrinsic::x86_sse_ucomieq_ss:
5855 case Intrinsic::x86_sse_ucomilt_ss:
5856 case Intrinsic::x86_sse_ucomile_ss:
5857 case Intrinsic::x86_sse_ucomigt_ss:
5858 case Intrinsic::x86_sse_ucomige_ss:
5859 case Intrinsic::x86_sse_ucomineq_ss:
5860 case Intrinsic::x86_sse2_comieq_sd:
5861 case Intrinsic::x86_sse2_comilt_sd:
5862 case Intrinsic::x86_sse2_comile_sd:
5863 case Intrinsic::x86_sse2_comigt_sd:
5864 case Intrinsic::x86_sse2_comige_sd:
5865 case Intrinsic::x86_sse2_comineq_sd:
5866 case Intrinsic::x86_sse2_ucomieq_sd:
5867 case Intrinsic::x86_sse2_ucomilt_sd:
5868 case Intrinsic::x86_sse2_ucomile_sd:
5869 case Intrinsic::x86_sse2_ucomigt_sd:
5870 case Intrinsic::x86_sse2_ucomige_sd:
5871 case Intrinsic::x86_sse2_ucomineq_sd: {
5872 unsigned Opc = 0;
5873 ISD::CondCode CC = ISD::SETCC_INVALID;
5874 switch (IntNo) {
5875 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005876 case Intrinsic::x86_sse_comieq_ss:
5877 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005878 Opc = X86ISD::COMI;
5879 CC = ISD::SETEQ;
5880 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005881 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005882 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005883 Opc = X86ISD::COMI;
5884 CC = ISD::SETLT;
5885 break;
5886 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005887 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005888 Opc = X86ISD::COMI;
5889 CC = ISD::SETLE;
5890 break;
5891 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005892 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005893 Opc = X86ISD::COMI;
5894 CC = ISD::SETGT;
5895 break;
5896 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005897 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005898 Opc = X86ISD::COMI;
5899 CC = ISD::SETGE;
5900 break;
5901 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005902 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005903 Opc = X86ISD::COMI;
5904 CC = ISD::SETNE;
5905 break;
5906 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005907 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005908 Opc = X86ISD::UCOMI;
5909 CC = ISD::SETEQ;
5910 break;
5911 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005912 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913 Opc = X86ISD::UCOMI;
5914 CC = ISD::SETLT;
5915 break;
5916 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005917 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005918 Opc = X86ISD::UCOMI;
5919 CC = ISD::SETLE;
5920 break;
5921 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005922 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005923 Opc = X86ISD::UCOMI;
5924 CC = ISD::SETGT;
5925 break;
5926 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005927 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005928 Opc = X86ISD::UCOMI;
5929 CC = ISD::SETGE;
5930 break;
5931 case Intrinsic::x86_sse_ucomineq_ss:
5932 case Intrinsic::x86_sse2_ucomineq_sd:
5933 Opc = X86ISD::UCOMI;
5934 CC = ISD::SETNE;
5935 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005936 }
Evan Cheng734503b2006-09-11 02:19:56 +00005937
Dan Gohman475871a2008-07-27 21:46:04 +00005938 SDValue LHS = Op.getOperand(1);
5939 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00005940 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005941 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
5942 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005943 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005944 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005945 }
Evan Cheng5759f972008-05-04 09:15:50 +00005946
5947 // Fix vector shift instructions where the last operand is a non-immediate
5948 // i32 value.
5949 case Intrinsic::x86_sse2_pslli_w:
5950 case Intrinsic::x86_sse2_pslli_d:
5951 case Intrinsic::x86_sse2_pslli_q:
5952 case Intrinsic::x86_sse2_psrli_w:
5953 case Intrinsic::x86_sse2_psrli_d:
5954 case Intrinsic::x86_sse2_psrli_q:
5955 case Intrinsic::x86_sse2_psrai_w:
5956 case Intrinsic::x86_sse2_psrai_d:
5957 case Intrinsic::x86_mmx_pslli_w:
5958 case Intrinsic::x86_mmx_pslli_d:
5959 case Intrinsic::x86_mmx_pslli_q:
5960 case Intrinsic::x86_mmx_psrli_w:
5961 case Intrinsic::x86_mmx_psrli_d:
5962 case Intrinsic::x86_mmx_psrli_q:
5963 case Intrinsic::x86_mmx_psrai_w:
5964 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005965 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005966 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005967 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005968
5969 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005970 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005971 switch (IntNo) {
5972 case Intrinsic::x86_sse2_pslli_w:
5973 NewIntNo = Intrinsic::x86_sse2_psll_w;
5974 break;
5975 case Intrinsic::x86_sse2_pslli_d:
5976 NewIntNo = Intrinsic::x86_sse2_psll_d;
5977 break;
5978 case Intrinsic::x86_sse2_pslli_q:
5979 NewIntNo = Intrinsic::x86_sse2_psll_q;
5980 break;
5981 case Intrinsic::x86_sse2_psrli_w:
5982 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5983 break;
5984 case Intrinsic::x86_sse2_psrli_d:
5985 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5986 break;
5987 case Intrinsic::x86_sse2_psrli_q:
5988 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5989 break;
5990 case Intrinsic::x86_sse2_psrai_w:
5991 NewIntNo = Intrinsic::x86_sse2_psra_w;
5992 break;
5993 case Intrinsic::x86_sse2_psrai_d:
5994 NewIntNo = Intrinsic::x86_sse2_psra_d;
5995 break;
5996 default: {
5997 ShAmtVT = MVT::v2i32;
5998 switch (IntNo) {
5999 case Intrinsic::x86_mmx_pslli_w:
6000 NewIntNo = Intrinsic::x86_mmx_psll_w;
6001 break;
6002 case Intrinsic::x86_mmx_pslli_d:
6003 NewIntNo = Intrinsic::x86_mmx_psll_d;
6004 break;
6005 case Intrinsic::x86_mmx_pslli_q:
6006 NewIntNo = Intrinsic::x86_mmx_psll_q;
6007 break;
6008 case Intrinsic::x86_mmx_psrli_w:
6009 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6010 break;
6011 case Intrinsic::x86_mmx_psrli_d:
6012 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6013 break;
6014 case Intrinsic::x86_mmx_psrli_q:
6015 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6016 break;
6017 case Intrinsic::x86_mmx_psrai_w:
6018 NewIntNo = Intrinsic::x86_mmx_psra_w;
6019 break;
6020 case Intrinsic::x86_mmx_psrai_d:
6021 NewIntNo = Intrinsic::x86_mmx_psra_d;
6022 break;
6023 default: abort(); // Can't reach here.
6024 }
6025 break;
6026 }
6027 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006028 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006029 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6030 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6031 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006032 DAG.getConstant(NewIntNo, MVT::i32),
6033 Op.getOperand(1), ShAmt);
6034 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006035 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006036}
Evan Cheng72261582005-12-20 06:22:03 +00006037
Dan Gohman475871a2008-07-27 21:46:04 +00006038SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006039 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006040 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006041
6042 if (Depth > 0) {
6043 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6044 SDValue Offset =
6045 DAG.getConstant(TD->getPointerSize(),
6046 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006047 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006048 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006049 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006050 NULL, 0);
6051 }
6052
6053 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006054 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006055 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006056 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006057}
6058
Dan Gohman475871a2008-07-27 21:46:04 +00006059SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006060 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6061 MFI->setFrameAddressIsTaken(true);
6062 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006063 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006064 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6065 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006066 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006067 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006068 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006069 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006070}
6071
Dan Gohman475871a2008-07-27 21:46:04 +00006072SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006073 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006074 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006075}
6076
Dan Gohman475871a2008-07-27 21:46:04 +00006077SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006078{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006079 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006080 SDValue Chain = Op.getOperand(0);
6081 SDValue Offset = Op.getOperand(1);
6082 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006083 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006084
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006085 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6086 getPointerTy());
6087 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006088
Dale Johannesene4d209d2009-02-03 20:21:25 +00006089 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006090 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006091 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6092 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006093 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006094 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006095
Dale Johannesene4d209d2009-02-03 20:21:25 +00006096 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006097 MVT::Other,
6098 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006099}
6100
Dan Gohman475871a2008-07-27 21:46:04 +00006101SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006102 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006103 SDValue Root = Op.getOperand(0);
6104 SDValue Trmp = Op.getOperand(1); // trampoline
6105 SDValue FPtr = Op.getOperand(2); // nested function
6106 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006107 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006108
Dan Gohman69de1932008-02-06 22:27:42 +00006109 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006110
Duncan Sands339e14f2008-01-16 22:55:25 +00006111 const X86InstrInfo *TII =
6112 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6113
Duncan Sandsb116fac2007-07-27 20:02:49 +00006114 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006115 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006116
6117 // Large code-model.
6118
6119 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6120 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6121
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006122 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6123 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006124
6125 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6126
6127 // Load the pointer to the nested function into R11.
6128 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006129 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006130 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6131 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006132
Scott Michelfdc40a02009-02-17 22:15:04 +00006133 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006134 DAG.getConstant(2, MVT::i64));
6135 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006136
6137 // Load the 'nest' parameter value into R10.
6138 // R10 is specified in X86CallingConv.td
6139 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006140 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006141 DAG.getConstant(10, MVT::i64));
6142 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6143 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006144
Scott Michelfdc40a02009-02-17 22:15:04 +00006145 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006146 DAG.getConstant(12, MVT::i64));
6147 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006148
6149 // Jump to the nested function.
6150 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006151 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006152 DAG.getConstant(20, MVT::i64));
6153 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6154 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006155
6156 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006157 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006158 DAG.getConstant(22, MVT::i64));
6159 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006160 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006161
Dan Gohman475871a2008-07-27 21:46:04 +00006162 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006163 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6164 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006165 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006166 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006167 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6168 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006169 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006170
6171 switch (CC) {
6172 default:
6173 assert(0 && "Unsupported calling convention");
6174 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006175 case CallingConv::X86_StdCall: {
6176 // Pass 'nest' parameter in ECX.
6177 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006178 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006179
6180 // Check that ECX wasn't needed by an 'inreg' parameter.
6181 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006182 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006183
Chris Lattner58d74912008-03-12 17:45:29 +00006184 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006185 unsigned InRegCount = 0;
6186 unsigned Idx = 1;
6187
6188 for (FunctionType::param_iterator I = FTy->param_begin(),
6189 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006190 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006191 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006192 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006193
6194 if (InRegCount > 2) {
6195 cerr << "Nest register in use - reduce number of inreg parameters!\n";
6196 abort();
6197 }
6198 }
6199 break;
6200 }
6201 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006202 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006203 // Pass 'nest' parameter in EAX.
6204 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006205 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006206 break;
6207 }
6208
Dan Gohman475871a2008-07-27 21:46:04 +00006209 SDValue OutChains[4];
6210 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006211
Scott Michelfdc40a02009-02-17 22:15:04 +00006212 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006213 DAG.getConstant(10, MVT::i32));
6214 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006215
Duncan Sands339e14f2008-01-16 22:55:25 +00006216 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006217 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006218 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006219 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006220 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006221
Scott Michelfdc40a02009-02-17 22:15:04 +00006222 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006223 DAG.getConstant(1, MVT::i32));
6224 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006225
Duncan Sands339e14f2008-01-16 22:55:25 +00006226 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006227 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006228 DAG.getConstant(5, MVT::i32));
6229 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006230 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006231
Scott Michelfdc40a02009-02-17 22:15:04 +00006232 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006233 DAG.getConstant(6, MVT::i32));
6234 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006235
Dan Gohman475871a2008-07-27 21:46:04 +00006236 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006237 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6238 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006239 }
6240}
6241
Dan Gohman475871a2008-07-27 21:46:04 +00006242SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006243 /*
6244 The rounding mode is in bits 11:10 of FPSR, and has the following
6245 settings:
6246 00 Round to nearest
6247 01 Round to -inf
6248 10 Round to +inf
6249 11 Round to 0
6250
6251 FLT_ROUNDS, on the other hand, expects the following:
6252 -1 Undefined
6253 0 Round to 0
6254 1 Round to nearest
6255 2 Round to +inf
6256 3 Round to -inf
6257
6258 To perform the conversion, we do:
6259 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6260 */
6261
6262 MachineFunction &MF = DAG.getMachineFunction();
6263 const TargetMachine &TM = MF.getTarget();
6264 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6265 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006266 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006267 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006268
6269 // Save FP Control Word to stack slot
6270 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006271 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006272
Dale Johannesene4d209d2009-02-03 20:21:25 +00006273 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006274 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006275
6276 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006277 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006278
6279 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006280 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006281 DAG.getNode(ISD::SRL, dl, MVT::i16,
6282 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006283 CWD, DAG.getConstant(0x800, MVT::i16)),
6284 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006285 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006286 DAG.getNode(ISD::SRL, dl, MVT::i16,
6287 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006288 CWD, DAG.getConstant(0x400, MVT::i16)),
6289 DAG.getConstant(9, MVT::i8));
6290
Dan Gohman475871a2008-07-27 21:46:04 +00006291 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006292 DAG.getNode(ISD::AND, dl, MVT::i16,
6293 DAG.getNode(ISD::ADD, dl, MVT::i16,
6294 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006295 DAG.getConstant(1, MVT::i16)),
6296 DAG.getConstant(3, MVT::i16));
6297
6298
Duncan Sands83ec4b62008-06-06 12:08:01 +00006299 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006300 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006301}
6302
Dan Gohman475871a2008-07-27 21:46:04 +00006303SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006304 MVT VT = Op.getValueType();
6305 MVT OpVT = VT;
6306 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006307 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006308
6309 Op = Op.getOperand(0);
6310 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006311 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006312 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006313 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006314 }
Evan Cheng18efe262007-12-14 02:13:44 +00006315
Evan Cheng152804e2007-12-14 08:30:15 +00006316 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6317 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006318 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006319
6320 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006321 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006322 Ops.push_back(Op);
6323 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6324 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6325 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006326 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006327
6328 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006329 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006330
Evan Cheng18efe262007-12-14 02:13:44 +00006331 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006332 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006333 return Op;
6334}
6335
Dan Gohman475871a2008-07-27 21:46:04 +00006336SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006337 MVT VT = Op.getValueType();
6338 MVT OpVT = VT;
6339 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006340 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006341
6342 Op = Op.getOperand(0);
6343 if (VT == MVT::i8) {
6344 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006345 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006346 }
Evan Cheng152804e2007-12-14 08:30:15 +00006347
6348 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6349 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006350 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006351
6352 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006353 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006354 Ops.push_back(Op);
6355 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6356 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6357 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006358 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006359
Evan Cheng18efe262007-12-14 02:13:44 +00006360 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006361 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006362 return Op;
6363}
6364
Mon P Wangaf9b9522008-12-18 21:42:19 +00006365SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6366 MVT VT = Op.getValueType();
6367 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006368 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006369
Mon P Wangaf9b9522008-12-18 21:42:19 +00006370 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6371 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6372 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6373 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6374 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6375 //
6376 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6377 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6378 // return AloBlo + AloBhi + AhiBlo;
6379
6380 SDValue A = Op.getOperand(0);
6381 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006382
Dale Johannesene4d209d2009-02-03 20:21:25 +00006383 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006384 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6385 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006386 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006387 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6388 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006389 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006390 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6391 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006392 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006393 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6394 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006395 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006396 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6397 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006398 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006399 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6400 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006401 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006402 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6403 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006404 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6405 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006406 return Res;
6407}
6408
6409
Bill Wendling74c37652008-12-09 22:08:41 +00006410SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6411 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6412 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006413 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6414 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006415 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006416 SDValue LHS = N->getOperand(0);
6417 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006418 unsigned BaseOp = 0;
6419 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006420 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006421
6422 switch (Op.getOpcode()) {
6423 default: assert(0 && "Unknown ovf instruction!");
6424 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006425 // A subtract of one will be selected as a INC. Note that INC doesn't
6426 // set CF, so we can't do this for UADDO.
6427 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6428 if (C->getAPIntValue() == 1) {
6429 BaseOp = X86ISD::INC;
6430 Cond = X86::COND_O;
6431 break;
6432 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006433 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006434 Cond = X86::COND_O;
6435 break;
6436 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006437 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006438 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006439 break;
6440 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006441 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6442 // set CF, so we can't do this for USUBO.
6443 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6444 if (C->getAPIntValue() == 1) {
6445 BaseOp = X86ISD::DEC;
6446 Cond = X86::COND_O;
6447 break;
6448 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006449 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006450 Cond = X86::COND_O;
6451 break;
6452 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006453 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006454 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006455 break;
6456 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006457 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006458 Cond = X86::COND_O;
6459 break;
6460 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006461 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006462 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006463 break;
6464 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006465
Bill Wendling61edeb52008-12-02 01:06:39 +00006466 // Also sets EFLAGS.
6467 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006468 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006469
Bill Wendling61edeb52008-12-02 01:06:39 +00006470 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006471 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006472 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006473
Bill Wendling61edeb52008-12-02 01:06:39 +00006474 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6475 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006476}
6477
Dan Gohman475871a2008-07-27 21:46:04 +00006478SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006479 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006480 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006481 unsigned Reg = 0;
6482 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006483 switch(T.getSimpleVT()) {
6484 default:
6485 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006486 case MVT::i8: Reg = X86::AL; size = 1; break;
6487 case MVT::i16: Reg = X86::AX; size = 2; break;
6488 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006489 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006490 assert(Subtarget->is64Bit() && "Node not type legal!");
6491 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006492 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006493 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006494 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006495 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006496 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006497 Op.getOperand(1),
6498 Op.getOperand(3),
6499 DAG.getTargetConstant(size, MVT::i8),
6500 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006501 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006502 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006503 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006504 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006505 return cpOut;
6506}
6507
Duncan Sands1607f052008-12-01 11:39:25 +00006508SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006509 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006510 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006511 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006512 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006513 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006514 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006515 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6516 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006517 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006518 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006519 DAG.getConstant(32, MVT::i8));
6520 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006521 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006522 rdx.getValue(1)
6523 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006524 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006525}
6526
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006527SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6528 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006529 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006530 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006531 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006532 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006533 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006534 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006535 Node->getOperand(0),
6536 Node->getOperand(1), negOp,
6537 cast<AtomicSDNode>(Node)->getSrcValue(),
6538 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006539}
6540
Evan Cheng0db9fe62006-04-25 20:13:52 +00006541/// LowerOperation - Provide custom lowering hooks for some operations.
6542///
Dan Gohman475871a2008-07-27 21:46:04 +00006543SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006544 switch (Op.getOpcode()) {
6545 default: assert(0 && "Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006546 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6547 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006548 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6549 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6550 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6551 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6552 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6553 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6554 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006555 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006556 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006557 case ISD::SHL_PARTS:
6558 case ISD::SRA_PARTS:
6559 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6560 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006561 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006562 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006563 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006564 case ISD::FABS: return LowerFABS(Op, DAG);
6565 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006566 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006567 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006568 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006569 case ISD::SELECT: return LowerSELECT(Op, DAG);
6570 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006571 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006572 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006573 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006574 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006575 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006576 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006577 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006578 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006579 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6580 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006581 case ISD::FRAME_TO_ARGS_OFFSET:
6582 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006583 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006584 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006585 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006586 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006587 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6588 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006589 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006590 case ISD::SADDO:
6591 case ISD::UADDO:
6592 case ISD::SSUBO:
6593 case ISD::USUBO:
6594 case ISD::SMULO:
6595 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006596 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006597 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006598}
6599
Duncan Sands1607f052008-12-01 11:39:25 +00006600void X86TargetLowering::
6601ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6602 SelectionDAG &DAG, unsigned NewOp) {
6603 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006604 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006605 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6606
6607 SDValue Chain = Node->getOperand(0);
6608 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006609 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006610 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006611 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006612 Node->getOperand(2), DAG.getIntPtrConstant(1));
6613 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6614 // have a MemOperand. Pass the info through as a normal operand.
6615 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6616 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6617 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006618 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006619 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006620 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006621 Results.push_back(Result.getValue(2));
6622}
6623
Duncan Sands126d9072008-07-04 11:47:58 +00006624/// ReplaceNodeResults - Replace a node with an illegal result type
6625/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006626void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6627 SmallVectorImpl<SDValue>&Results,
6628 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006629 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006630 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006631 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006632 assert(false && "Do not know how to custom type legalize this operation!");
6633 return;
6634 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006635 std::pair<SDValue,SDValue> Vals =
6636 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006637 SDValue FIST = Vals.first, StackSlot = Vals.second;
6638 if (FIST.getNode() != 0) {
6639 MVT VT = N->getValueType(0);
6640 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006641 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006642 }
6643 return;
6644 }
6645 case ISD::READCYCLECOUNTER: {
6646 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6647 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006648 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006649 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006650 rd.getValue(1));
6651 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006652 eax.getValue(2));
6653 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6654 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006655 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006656 Results.push_back(edx.getValue(1));
6657 return;
6658 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006659 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006660 MVT T = N->getValueType(0);
6661 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6662 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006663 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006664 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006665 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006666 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006667 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6668 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006669 cpInL.getValue(1));
6670 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006671 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006672 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006673 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006674 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006675 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006676 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006677 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006678 swapInL.getValue(1));
6679 SDValue Ops[] = { swapInH.getValue(0),
6680 N->getOperand(1),
6681 swapInH.getValue(1) };
6682 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006683 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006684 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6685 MVT::i32, Result.getValue(1));
6686 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6687 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006688 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006689 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006690 Results.push_back(cpOutH.getValue(1));
6691 return;
6692 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006693 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006694 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6695 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006696 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006697 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6698 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006699 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006700 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6701 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006702 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006703 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6704 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006705 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006706 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6707 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006708 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006709 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6710 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006711 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006712 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6713 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006714 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006715}
6716
Evan Cheng72261582005-12-20 06:22:03 +00006717const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6718 switch (Opcode) {
6719 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006720 case X86ISD::BSF: return "X86ISD::BSF";
6721 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006722 case X86ISD::SHLD: return "X86ISD::SHLD";
6723 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006724 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006725 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006726 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006727 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006728 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006729 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006730 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6731 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6732 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006733 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006734 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006735 case X86ISD::CALL: return "X86ISD::CALL";
6736 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6737 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006738 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006739 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006740 case X86ISD::COMI: return "X86ISD::COMI";
6741 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006742 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006743 case X86ISD::CMOV: return "X86ISD::CMOV";
6744 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006745 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006746 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6747 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006748 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006749 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006750 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006751 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006752 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6753 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006754 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00006755 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00006756 case X86ISD::FMAX: return "X86ISD::FMAX";
6757 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006758 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6759 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006760 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00006761 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006762 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006763 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006764 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006765 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6766 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006767 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6768 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6769 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6770 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6771 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6772 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006773 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6774 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006775 case X86ISD::VSHL: return "X86ISD::VSHL";
6776 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006777 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6778 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6779 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6780 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6781 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6782 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6783 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6784 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6785 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6786 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006787 case X86ISD::ADD: return "X86ISD::ADD";
6788 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00006789 case X86ISD::SMUL: return "X86ISD::SMUL";
6790 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00006791 case X86ISD::INC: return "X86ISD::INC";
6792 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00006793 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Evan Cheng72261582005-12-20 06:22:03 +00006794 }
6795}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006796
Chris Lattnerc9addb72007-03-30 23:15:24 +00006797// isLegalAddressingMode - Return true if the addressing mode represented
6798// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00006799bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00006800 const Type *Ty) const {
6801 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00006802
Chris Lattnerc9addb72007-03-30 23:15:24 +00006803 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6804 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6805 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00006806
Chris Lattnerc9addb72007-03-30 23:15:24 +00006807 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006808 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006809 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6810 return false;
Dale Johannesen203af582008-12-05 21:47:27 +00006811 // If BaseGV requires a register, we cannot also have a BaseReg.
6812 if (Subtarget->GVRequiresRegister(AM.BaseGV, getTargetMachine(), false) &&
6813 AM.HasBaseReg)
6814 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006815
6816 // X86-64 only supports addr of globals in small code model.
6817 if (Subtarget->is64Bit()) {
6818 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6819 return false;
6820 // If lower 4G is not available, then we must use rip-relative addressing.
6821 if (AM.BaseOffs || AM.Scale > 1)
6822 return false;
6823 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006824 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006825
Chris Lattnerc9addb72007-03-30 23:15:24 +00006826 switch (AM.Scale) {
6827 case 0:
6828 case 1:
6829 case 2:
6830 case 4:
6831 case 8:
6832 // These scales always work.
6833 break;
6834 case 3:
6835 case 5:
6836 case 9:
6837 // These scales are formed with basereg+scalereg. Only accept if there is
6838 // no basereg yet.
6839 if (AM.HasBaseReg)
6840 return false;
6841 break;
6842 default: // Other stuff never works.
6843 return false;
6844 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006845
Chris Lattnerc9addb72007-03-30 23:15:24 +00006846 return true;
6847}
6848
6849
Evan Cheng2bd122c2007-10-26 01:56:11 +00006850bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6851 if (!Ty1->isInteger() || !Ty2->isInteger())
6852 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006853 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6854 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006855 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006856 return false;
6857 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006858}
6859
Duncan Sands83ec4b62008-06-06 12:08:01 +00006860bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6861 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006862 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006863 unsigned NumBits1 = VT1.getSizeInBits();
6864 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006865 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006866 return false;
6867 return Subtarget->is64Bit() || NumBits1 < 64;
6868}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006869
Dan Gohman97121ba2009-04-08 00:15:30 +00006870bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006871 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006872 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
6873}
6874
6875bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00006876 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00006877 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
6878}
6879
Evan Cheng60c07e12006-07-05 22:17:51 +00006880/// isShuffleMaskLegal - Targets can use this to indicate that they only
6881/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6882/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6883/// are assumed to be legal.
6884bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006885X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
6886 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006887 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00006888 if (VT.getSizeInBits() == 64)
6889 return false;
6890
6891 // FIXME: pshufb, blends, palignr, shifts.
6892 return (VT.getVectorNumElements() == 2 ||
6893 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
6894 isMOVLMask(M, VT) ||
6895 isSHUFPMask(M, VT) ||
6896 isPSHUFDMask(M, VT) ||
6897 isPSHUFHWMask(M, VT) ||
6898 isPSHUFLWMask(M, VT) ||
6899 isUNPCKLMask(M, VT) ||
6900 isUNPCKHMask(M, VT) ||
6901 isUNPCKL_v_undef_Mask(M, VT) ||
6902 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006903}
6904
Dan Gohman7d8143f2008-04-09 20:09:42 +00006905bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00006906X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00006907 MVT VT) const {
6908 unsigned NumElts = VT.getVectorNumElements();
6909 // FIXME: This collection of masks seems suspect.
6910 if (NumElts == 2)
6911 return true;
6912 if (NumElts == 4 && VT.getSizeInBits() == 128) {
6913 return (isMOVLMask(Mask, VT) ||
6914 isCommutedMOVLMask(Mask, VT, true) ||
6915 isSHUFPMask(Mask, VT) ||
6916 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00006917 }
6918 return false;
6919}
6920
6921//===----------------------------------------------------------------------===//
6922// X86 Scheduler Hooks
6923//===----------------------------------------------------------------------===//
6924
Mon P Wang63307c32008-05-05 19:05:59 +00006925// private utility function
6926MachineBasicBlock *
6927X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6928 MachineBasicBlock *MBB,
6929 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006930 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006931 unsigned LoadOpc,
6932 unsigned CXchgOpc,
6933 unsigned copyOpc,
6934 unsigned notOpc,
6935 unsigned EAXreg,
6936 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00006937 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00006938 // For the atomic bitwise operator, we generate
6939 // thisMBB:
6940 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006941 // ld t1 = [bitinstr.addr]
6942 // op t2 = t1, [bitinstr.val]
6943 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006944 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6945 // bz newMBB
6946 // fallthrough -->nextMBB
6947 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6948 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006949 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006950 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00006951
Mon P Wang63307c32008-05-05 19:05:59 +00006952 /// First build the CFG
6953 MachineFunction *F = MBB->getParent();
6954 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006955 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6956 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6957 F->insert(MBBIter, newMBB);
6958 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006959
Mon P Wang63307c32008-05-05 19:05:59 +00006960 // Move all successors to thisMBB to nextMBB
6961 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006962
Mon P Wang63307c32008-05-05 19:05:59 +00006963 // Update thisMBB to fall through to newMBB
6964 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006965
Mon P Wang63307c32008-05-05 19:05:59 +00006966 // newMBB jumps to itself and fall through to nextMBB
6967 newMBB->addSuccessor(nextMBB);
6968 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00006969
Mon P Wang63307c32008-05-05 19:05:59 +00006970 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006971 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
6972 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00006973 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00006974 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006975 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00006976 int numArgs = bInstr->getNumOperands() - 1;
6977 for (int i=0; i < numArgs; ++i)
6978 argOpers[i] = &bInstr->getOperand(i+1);
6979
6980 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00006981 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
6982 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00006983
Dale Johannesen140be2d2008-08-19 18:47:28 +00006984 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006985 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006986 for (int i=0; i <= lastAddrIndx; ++i)
6987 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006988
Dale Johannesen140be2d2008-08-19 18:47:28 +00006989 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006990 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006991 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006992 }
Scott Michelfdc40a02009-02-17 22:15:04 +00006993 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006994 tt = t1;
6995
Dale Johannesen140be2d2008-08-19 18:47:28 +00006996 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00006997 assert((argOpers[valArgIndx]->isReg() ||
6998 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00006999 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007000 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007001 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007002 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007003 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007004 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007005 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007006
Dale Johannesene4d209d2009-02-03 20:21:25 +00007007 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007008 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007009
Dale Johannesene4d209d2009-02-03 20:21:25 +00007010 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007011 for (int i=0; i <= lastAddrIndx; ++i)
7012 (*MIB).addOperand(*argOpers[i]);
7013 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007014 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7015 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7016
Dale Johannesene4d209d2009-02-03 20:21:25 +00007017 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007018 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007019
Mon P Wang63307c32008-05-05 19:05:59 +00007020 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007021 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007022
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007023 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007024 return nextMBB;
7025}
7026
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007027// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007028MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007029X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7030 MachineBasicBlock *MBB,
7031 unsigned regOpcL,
7032 unsigned regOpcH,
7033 unsigned immOpcL,
7034 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007035 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007036 // For the atomic bitwise operator, we generate
7037 // thisMBB (instructions are in pairs, except cmpxchg8b)
7038 // ld t1,t2 = [bitinstr.addr]
7039 // newMBB:
7040 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7041 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007042 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007043 // mov ECX, EBX <- t5, t6
7044 // mov EAX, EDX <- t1, t2
7045 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7046 // mov t3, t4 <- EAX, EDX
7047 // bz newMBB
7048 // result in out1, out2
7049 // fallthrough -->nextMBB
7050
7051 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7052 const unsigned LoadOpc = X86::MOV32rm;
7053 const unsigned copyOpc = X86::MOV32rr;
7054 const unsigned NotOpc = X86::NOT32r;
7055 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7056 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7057 MachineFunction::iterator MBBIter = MBB;
7058 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007059
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007060 /// First build the CFG
7061 MachineFunction *F = MBB->getParent();
7062 MachineBasicBlock *thisMBB = MBB;
7063 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7064 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7065 F->insert(MBBIter, newMBB);
7066 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007067
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007068 // Move all successors to thisMBB to nextMBB
7069 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007070
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007071 // Update thisMBB to fall through to newMBB
7072 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007073
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007074 // newMBB jumps to itself and fall through to nextMBB
7075 newMBB->addSuccessor(nextMBB);
7076 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007077
Dale Johannesene4d209d2009-02-03 20:21:25 +00007078 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007079 // Insert instructions into newMBB based on incoming instruction
7080 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007081 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
7082 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007083 MachineOperand& dest1Oper = bInstr->getOperand(0);
7084 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007085 MachineOperand* argOpers[2 + X86AddrNumOperands];
7086 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007087 argOpers[i] = &bInstr->getOperand(i+2);
7088
7089 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007090 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007091
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007092 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007093 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007094 for (int i=0; i <= lastAddrIndx; ++i)
7095 (*MIB).addOperand(*argOpers[i]);
7096 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007097 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007098 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007099 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007100 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007101 MachineOperand newOp3 = *(argOpers[3]);
7102 if (newOp3.isImm())
7103 newOp3.setImm(newOp3.getImm()+4);
7104 else
7105 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007106 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007107 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007108
7109 // t3/4 are defined later, at the bottom of the loop
7110 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7111 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007112 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007113 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007114 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007115 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7116
7117 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7118 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007119 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007120 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7121 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007122 } else {
7123 tt1 = t1;
7124 tt2 = t2;
7125 }
7126
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007127 int valArgIndx = lastAddrIndx + 1;
7128 assert((argOpers[valArgIndx]->isReg() ||
7129 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007130 "invalid operand");
7131 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7132 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007133 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007134 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007135 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007136 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007137 if (regOpcL != X86::MOV32rr)
7138 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007139 (*MIB).addOperand(*argOpers[valArgIndx]);
7140 assert(argOpers[valArgIndx + 1]->isReg() ==
7141 argOpers[valArgIndx]->isReg());
7142 assert(argOpers[valArgIndx + 1]->isImm() ==
7143 argOpers[valArgIndx]->isImm());
7144 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007145 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007146 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007147 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007148 if (regOpcH != X86::MOV32rr)
7149 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007150 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007151
Dale Johannesene4d209d2009-02-03 20:21:25 +00007152 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007153 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007154 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007155 MIB.addReg(t2);
7156
Dale Johannesene4d209d2009-02-03 20:21:25 +00007157 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007158 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007159 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007160 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007161
Dale Johannesene4d209d2009-02-03 20:21:25 +00007162 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007163 for (int i=0; i <= lastAddrIndx; ++i)
7164 (*MIB).addOperand(*argOpers[i]);
7165
7166 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7167 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7168
Dale Johannesene4d209d2009-02-03 20:21:25 +00007169 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007170 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007171 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007172 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007173
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007174 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007175 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007176
7177 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7178 return nextMBB;
7179}
7180
7181// private utility function
7182MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007183X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7184 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007185 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007186 // For the atomic min/max operator, we generate
7187 // thisMBB:
7188 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007189 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007190 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007191 // cmp t1, t2
7192 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007193 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007194 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7195 // bz newMBB
7196 // fallthrough -->nextMBB
7197 //
7198 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7199 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007200 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007201 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007202
Mon P Wang63307c32008-05-05 19:05:59 +00007203 /// First build the CFG
7204 MachineFunction *F = MBB->getParent();
7205 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007206 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7207 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7208 F->insert(MBBIter, newMBB);
7209 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007210
Mon P Wang63307c32008-05-05 19:05:59 +00007211 // Move all successors to thisMBB to nextMBB
7212 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007213
Mon P Wang63307c32008-05-05 19:05:59 +00007214 // Update thisMBB to fall through to newMBB
7215 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007216
Mon P Wang63307c32008-05-05 19:05:59 +00007217 // newMBB jumps to newMBB and fall through to nextMBB
7218 newMBB->addSuccessor(nextMBB);
7219 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007220
Dale Johannesene4d209d2009-02-03 20:21:25 +00007221 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007222 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007223 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
7224 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007225 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007226 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007227 int numArgs = mInstr->getNumOperands() - 1;
7228 for (int i=0; i < numArgs; ++i)
7229 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007230
Mon P Wang63307c32008-05-05 19:05:59 +00007231 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007232 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7233 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007234
Mon P Wangab3e7472008-05-05 22:56:23 +00007235 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007236 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007237 for (int i=0; i <= lastAddrIndx; ++i)
7238 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007239
Mon P Wang63307c32008-05-05 19:05:59 +00007240 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007241 assert((argOpers[valArgIndx]->isReg() ||
7242 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007243 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007244
7245 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007246 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007247 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007248 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007249 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007250 (*MIB).addOperand(*argOpers[valArgIndx]);
7251
Dale Johannesene4d209d2009-02-03 20:21:25 +00007252 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007253 MIB.addReg(t1);
7254
Dale Johannesene4d209d2009-02-03 20:21:25 +00007255 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007256 MIB.addReg(t1);
7257 MIB.addReg(t2);
7258
7259 // Generate movc
7260 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007261 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007262 MIB.addReg(t2);
7263 MIB.addReg(t1);
7264
7265 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007266 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007267 for (int i=0; i <= lastAddrIndx; ++i)
7268 (*MIB).addOperand(*argOpers[i]);
7269 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007270 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7271 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007272
Dale Johannesene4d209d2009-02-03 20:21:25 +00007273 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007274 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007275
Mon P Wang63307c32008-05-05 19:05:59 +00007276 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007277 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007278
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007279 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007280 return nextMBB;
7281}
7282
7283
Evan Cheng60c07e12006-07-05 22:17:51 +00007284MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007285X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007286 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007287 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007288 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007289 switch (MI->getOpcode()) {
7290 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007291 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007292 case X86::CMOV_FR32:
7293 case X86::CMOV_FR64:
7294 case X86::CMOV_V4F32:
7295 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007296 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007297 // To "insert" a SELECT_CC instruction, we actually have to insert the
7298 // diamond control-flow pattern. The incoming instruction knows the
7299 // destination vreg to set, the condition code register to branch on, the
7300 // true/false values to select between, and a branch opcode to use.
7301 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007302 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007303 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007304
Evan Cheng60c07e12006-07-05 22:17:51 +00007305 // thisMBB:
7306 // ...
7307 // TrueVal = ...
7308 // cmpTY ccX, r1, r2
7309 // bCC copy1MBB
7310 // fallthrough --> copy0MBB
7311 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007312 MachineFunction *F = BB->getParent();
7313 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7314 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007315 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007316 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007317 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007318 F->insert(It, copy0MBB);
7319 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007320 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007321 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007322 sinkMBB->transferSuccessors(BB);
7323
7324 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007325 BB->addSuccessor(copy0MBB);
7326 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007327
Evan Cheng60c07e12006-07-05 22:17:51 +00007328 // copy0MBB:
7329 // %FalseValue = ...
7330 // # fallthrough to sinkMBB
7331 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007332
Evan Cheng60c07e12006-07-05 22:17:51 +00007333 // Update machine-CFG edges
7334 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007335
Evan Cheng60c07e12006-07-05 22:17:51 +00007336 // sinkMBB:
7337 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7338 // ...
7339 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007340 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007341 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7342 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7343
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007344 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007345 return BB;
7346 }
7347
Dale Johannesen849f2142007-07-03 00:53:03 +00007348 case X86::FP32_TO_INT16_IN_MEM:
7349 case X86::FP32_TO_INT32_IN_MEM:
7350 case X86::FP32_TO_INT64_IN_MEM:
7351 case X86::FP64_TO_INT16_IN_MEM:
7352 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007353 case X86::FP64_TO_INT64_IN_MEM:
7354 case X86::FP80_TO_INT16_IN_MEM:
7355 case X86::FP80_TO_INT32_IN_MEM:
7356 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007357 // Change the floating point control register to use "round towards zero"
7358 // mode when truncating to an integer value.
7359 MachineFunction *F = BB->getParent();
7360 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007361 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007362
7363 // Load the old value of the high byte of the control word...
7364 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007365 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007366 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007367 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007368
7369 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007370 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007371 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007372
7373 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007374 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007375
7376 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007377 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007378 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007379
7380 // Get the X86 opcode to use.
7381 unsigned Opc;
7382 switch (MI->getOpcode()) {
7383 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007384 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7385 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7386 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7387 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7388 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7389 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007390 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7391 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7392 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007393 }
7394
7395 X86AddressMode AM;
7396 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007397 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007398 AM.BaseType = X86AddressMode::RegBase;
7399 AM.Base.Reg = Op.getReg();
7400 } else {
7401 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007402 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007403 }
7404 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007405 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007406 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007407 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007408 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007409 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007410 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007411 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007412 AM.GV = Op.getGlobal();
7413 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007414 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007415 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007416 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007417 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007418
7419 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007420 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007421
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007422 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007423 return BB;
7424 }
Mon P Wang63307c32008-05-05 19:05:59 +00007425 case X86::ATOMAND32:
7426 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007427 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007428 X86::LCMPXCHG32, X86::MOV32rr,
7429 X86::NOT32r, X86::EAX,
7430 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007431 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007432 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7433 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007434 X86::LCMPXCHG32, X86::MOV32rr,
7435 X86::NOT32r, X86::EAX,
7436 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007437 case X86::ATOMXOR32:
7438 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007439 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007440 X86::LCMPXCHG32, X86::MOV32rr,
7441 X86::NOT32r, X86::EAX,
7442 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007443 case X86::ATOMNAND32:
7444 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007445 X86::AND32ri, X86::MOV32rm,
7446 X86::LCMPXCHG32, X86::MOV32rr,
7447 X86::NOT32r, X86::EAX,
7448 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007449 case X86::ATOMMIN32:
7450 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7451 case X86::ATOMMAX32:
7452 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7453 case X86::ATOMUMIN32:
7454 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7455 case X86::ATOMUMAX32:
7456 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007457
7458 case X86::ATOMAND16:
7459 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7460 X86::AND16ri, X86::MOV16rm,
7461 X86::LCMPXCHG16, X86::MOV16rr,
7462 X86::NOT16r, X86::AX,
7463 X86::GR16RegisterClass);
7464 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007465 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007466 X86::OR16ri, X86::MOV16rm,
7467 X86::LCMPXCHG16, X86::MOV16rr,
7468 X86::NOT16r, X86::AX,
7469 X86::GR16RegisterClass);
7470 case X86::ATOMXOR16:
7471 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7472 X86::XOR16ri, X86::MOV16rm,
7473 X86::LCMPXCHG16, X86::MOV16rr,
7474 X86::NOT16r, X86::AX,
7475 X86::GR16RegisterClass);
7476 case X86::ATOMNAND16:
7477 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7478 X86::AND16ri, X86::MOV16rm,
7479 X86::LCMPXCHG16, X86::MOV16rr,
7480 X86::NOT16r, X86::AX,
7481 X86::GR16RegisterClass, true);
7482 case X86::ATOMMIN16:
7483 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7484 case X86::ATOMMAX16:
7485 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7486 case X86::ATOMUMIN16:
7487 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7488 case X86::ATOMUMAX16:
7489 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7490
7491 case X86::ATOMAND8:
7492 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7493 X86::AND8ri, X86::MOV8rm,
7494 X86::LCMPXCHG8, X86::MOV8rr,
7495 X86::NOT8r, X86::AL,
7496 X86::GR8RegisterClass);
7497 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007498 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007499 X86::OR8ri, X86::MOV8rm,
7500 X86::LCMPXCHG8, X86::MOV8rr,
7501 X86::NOT8r, X86::AL,
7502 X86::GR8RegisterClass);
7503 case X86::ATOMXOR8:
7504 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7505 X86::XOR8ri, X86::MOV8rm,
7506 X86::LCMPXCHG8, X86::MOV8rr,
7507 X86::NOT8r, X86::AL,
7508 X86::GR8RegisterClass);
7509 case X86::ATOMNAND8:
7510 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7511 X86::AND8ri, X86::MOV8rm,
7512 X86::LCMPXCHG8, X86::MOV8rr,
7513 X86::NOT8r, X86::AL,
7514 X86::GR8RegisterClass, true);
7515 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007516 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007517 case X86::ATOMAND64:
7518 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007519 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007520 X86::LCMPXCHG64, X86::MOV64rr,
7521 X86::NOT64r, X86::RAX,
7522 X86::GR64RegisterClass);
7523 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007524 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7525 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007526 X86::LCMPXCHG64, X86::MOV64rr,
7527 X86::NOT64r, X86::RAX,
7528 X86::GR64RegisterClass);
7529 case X86::ATOMXOR64:
7530 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007531 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007532 X86::LCMPXCHG64, X86::MOV64rr,
7533 X86::NOT64r, X86::RAX,
7534 X86::GR64RegisterClass);
7535 case X86::ATOMNAND64:
7536 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7537 X86::AND64ri32, X86::MOV64rm,
7538 X86::LCMPXCHG64, X86::MOV64rr,
7539 X86::NOT64r, X86::RAX,
7540 X86::GR64RegisterClass, true);
7541 case X86::ATOMMIN64:
7542 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7543 case X86::ATOMMAX64:
7544 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7545 case X86::ATOMUMIN64:
7546 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7547 case X86::ATOMUMAX64:
7548 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007549
7550 // This group does 64-bit operations on a 32-bit host.
7551 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007552 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007553 X86::AND32rr, X86::AND32rr,
7554 X86::AND32ri, X86::AND32ri,
7555 false);
7556 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007557 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007558 X86::OR32rr, X86::OR32rr,
7559 X86::OR32ri, X86::OR32ri,
7560 false);
7561 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007562 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007563 X86::XOR32rr, X86::XOR32rr,
7564 X86::XOR32ri, X86::XOR32ri,
7565 false);
7566 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007567 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007568 X86::AND32rr, X86::AND32rr,
7569 X86::AND32ri, X86::AND32ri,
7570 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007571 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007572 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007573 X86::ADD32rr, X86::ADC32rr,
7574 X86::ADD32ri, X86::ADC32ri,
7575 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007576 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007577 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007578 X86::SUB32rr, X86::SBB32rr,
7579 X86::SUB32ri, X86::SBB32ri,
7580 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007581 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007582 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007583 X86::MOV32rr, X86::MOV32rr,
7584 X86::MOV32ri, X86::MOV32ri,
7585 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007586 }
7587}
7588
7589//===----------------------------------------------------------------------===//
7590// X86 Optimization Hooks
7591//===----------------------------------------------------------------------===//
7592
Dan Gohman475871a2008-07-27 21:46:04 +00007593void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007594 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007595 APInt &KnownZero,
7596 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007597 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007598 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007599 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007600 assert((Opc >= ISD::BUILTIN_OP_END ||
7601 Opc == ISD::INTRINSIC_WO_CHAIN ||
7602 Opc == ISD::INTRINSIC_W_CHAIN ||
7603 Opc == ISD::INTRINSIC_VOID) &&
7604 "Should use MaskedValueIsZero if you don't know whether Op"
7605 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007606
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007607 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007608 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007609 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007610 case X86ISD::ADD:
7611 case X86ISD::SUB:
7612 case X86ISD::SMUL:
7613 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007614 case X86ISD::INC:
7615 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007616 // These nodes' second result is a boolean.
7617 if (Op.getResNo() == 0)
7618 break;
7619 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007620 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007621 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7622 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007623 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007624 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007625}
Chris Lattner259e97c2006-01-31 19:43:35 +00007626
Evan Cheng206ee9d2006-07-07 08:33:52 +00007627/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007628/// node is a GlobalAddress + offset.
7629bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7630 GlobalValue* &GA, int64_t &Offset) const{
7631 if (N->getOpcode() == X86ISD::Wrapper) {
7632 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007633 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007634 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007635 return true;
7636 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007637 }
Evan Chengad4196b2008-05-12 19:56:52 +00007638 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007639}
7640
Evan Chengad4196b2008-05-12 19:56:52 +00007641static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7642 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007643 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007644 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007645 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007646 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007647 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007648 return false;
7649}
7650
Nate Begeman9008ca62009-04-27 18:41:29 +00007651static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
7652 MVT EVT, SDNode *&Base,
Evan Chengad4196b2008-05-12 19:56:52 +00007653 SelectionDAG &DAG, MachineFrameInfo *MFI,
7654 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007655 Base = NULL;
7656 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007657 if (N->getMaskElt(i) < 0) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007658 if (!Base)
7659 return false;
7660 continue;
7661 }
7662
Dan Gohman475871a2008-07-27 21:46:04 +00007663 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007664 if (!Elt.getNode() ||
7665 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007666 return false;
7667 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007668 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007669 if (Base->getOpcode() == ISD::UNDEF)
7670 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007671 continue;
7672 }
7673 if (Elt.getOpcode() == ISD::UNDEF)
7674 continue;
7675
Gabor Greifba36cb52008-08-28 21:40:38 +00007676 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007677 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007678 return false;
7679 }
7680 return true;
7681}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007682
7683/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7684/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7685/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007686/// order. In the case of v2i64, it will see if it can rewrite the
7687/// shuffle to be an appropriate build vector so it can take advantage of
7688// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007689static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007690 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007691 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007692 MVT VT = N->getValueType(0);
7693 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007694 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7695 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007696
7697 // For x86-32 machines, if we see an insert and then a shuffle in a v2i64
7698 // where the upper half is 0, it is advantageous to rewrite it as a build
7699 // vector of (0, val) so it can use movq.
7700 if (VT == MVT::v2i64) {
7701 SDValue In[2];
7702 In[0] = N->getOperand(0);
7703 In[1] = N->getOperand(1);
Nate Begeman9008ca62009-04-27 18:41:29 +00007704 int Idx0 = SVN->getMaskElt(0);
7705 int Idx1 = SVN->getMaskElt(1);
7706 // FIXME: can we take advantage of undef index?
7707 if (Idx0 >= 0 && Idx1 >= 0 &&
Mon P Wang1e955802009-04-03 02:43:30 +00007708 In[Idx0/2].getOpcode() == ISD::INSERT_VECTOR_ELT &&
7709 In[Idx1/2].getOpcode() == ISD::BUILD_VECTOR) {
7710 ConstantSDNode* InsertVecIdx =
7711 dyn_cast<ConstantSDNode>(In[Idx0/2].getOperand(2));
7712 if (InsertVecIdx &&
Nate Begeman9008ca62009-04-27 18:41:29 +00007713 InsertVecIdx->getZExtValue() == (unsigned)(Idx0 % 2) &&
Mon P Wang1e955802009-04-03 02:43:30 +00007714 isZeroNode(In[Idx1/2].getOperand(Idx1 % 2))) {
7715 return DAG.getNode(ISD::BUILD_VECTOR, dl, VT,
7716 In[Idx0/2].getOperand(1),
7717 In[Idx1/2].getOperand(Idx1 % 2));
7718 }
7719 }
7720 }
7721
7722 // Try to combine a vector_shuffle into a 128-bit load.
7723 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007724 SDNode *Base = NULL;
Nate Begeman9008ca62009-04-27 18:41:29 +00007725 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, Base, DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007726 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007727
Dan Gohmand3006222007-07-27 17:16:43 +00007728 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007729 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Dale Johannesene4d209d2009-02-03 20:21:25 +00007730 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007731 LD->getSrcValue(), LD->getSrcValueOffset(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007732 LD->isVolatile());
7733 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7734 LD->getSrcValue(), LD->getSrcValueOffset(),
7735 LD->isVolatile(), LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007736}
7737
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007738/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007739static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Dan Gohmane5af2d32009-01-29 01:59:02 +00007740 TargetLowering::DAGCombinerInfo &DCI,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007741 const X86Subtarget *Subtarget,
7742 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007743 unsigned NumOps = N->getNumOperands();
Dale Johannesene4d209d2009-02-03 20:21:25 +00007744 DebugLoc dl = N->getDebugLoc();
Evan Chengf26ffe92008-05-29 08:22:04 +00007745
Evan Chengd880b972008-05-09 21:53:03 +00007746 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007747 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007748 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007749
Duncan Sands83ec4b62008-06-06 12:08:01 +00007750 MVT VT = N->getValueType(0);
7751 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00007752 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7753 // We are looking for load i64 and zero extend. We want to transform
7754 // it before legalizer has a chance to expand it. Also look for i64
7755 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00007756 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007757 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00007758 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00007759 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007760 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007761
7762 // Value must be a load.
Gabor Greifba36cb52008-08-28 21:40:38 +00007763 SDNode *Base = N->getOperand(0).getNode();
Evan Chengd880b972008-05-09 21:53:03 +00007764 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007765 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00007766 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007767 Base = Base->getOperand(0).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007768 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00007769 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007770 }
Evan Chengd880b972008-05-09 21:53:03 +00007771
7772 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007773 LoadSDNode *LD = cast<LoadSDNode>(Base);
Scott Michelfdc40a02009-02-17 22:15:04 +00007774
Nate Begemanf7333bf2008-05-28 00:24:25 +00007775 // Load must not be an extload.
7776 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00007777 return SDValue();
Mon P Wang7ad9b512009-01-30 07:07:40 +00007778
7779 // Load type should legal type so we don't have to legalize it.
7780 if (!TLI.isTypeLegal(VT))
7781 return SDValue();
7782
Evan Cheng8a186ae2008-09-24 23:26:36 +00007783 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7784 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
Dale Johannesene4d209d2009-02-03 20:21:25 +00007785 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Dan Gohmane5af2d32009-01-29 01:59:02 +00007786 TargetLowering::TargetLoweringOpt TLO(DAG);
7787 TLO.CombineTo(SDValue(Base, 1), ResNode.getValue(1));
7788 DCI.CommitTargetLoweringOpt(TLO);
Evan Cheng8a186ae2008-09-24 23:26:36 +00007789 return ResNode;
Scott Michelfdc40a02009-02-17 22:15:04 +00007790}
Evan Chengd880b972008-05-09 21:53:03 +00007791
Chris Lattner83e6c992006-10-04 06:57:07 +00007792/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007793static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007794 const X86Subtarget *Subtarget) {
7795 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007796 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007797 // Get the LHS/RHS of the select.
7798 SDValue LHS = N->getOperand(1);
7799 SDValue RHS = N->getOperand(2);
7800
Chris Lattner83e6c992006-10-04 06:57:07 +00007801 // If we have SSE[12] support, try to form min/max nodes.
7802 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007803 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
7804 Cond.getOpcode() == ISD::SETCC) {
7805 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007806
Chris Lattner47b4ce82009-03-11 05:48:52 +00007807 unsigned Opcode = 0;
7808 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
7809 switch (CC) {
7810 default: break;
7811 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7812 case ISD::SETULE:
7813 case ISD::SETLE:
7814 if (!UnsafeFPMath) break;
7815 // FALL THROUGH.
7816 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7817 case ISD::SETLT:
7818 Opcode = X86ISD::FMIN;
7819 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007820
Chris Lattner47b4ce82009-03-11 05:48:52 +00007821 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7822 case ISD::SETUGT:
7823 case ISD::SETGT:
7824 if (!UnsafeFPMath) break;
7825 // FALL THROUGH.
7826 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7827 case ISD::SETGE:
7828 Opcode = X86ISD::FMAX;
7829 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00007830 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007831 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
7832 switch (CC) {
7833 default: break;
7834 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7835 case ISD::SETUGT:
7836 case ISD::SETGT:
7837 if (!UnsafeFPMath) break;
7838 // FALL THROUGH.
7839 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7840 case ISD::SETGE:
7841 Opcode = X86ISD::FMIN;
7842 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007843
Chris Lattner47b4ce82009-03-11 05:48:52 +00007844 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7845 case ISD::SETULE:
7846 case ISD::SETLE:
7847 if (!UnsafeFPMath) break;
7848 // FALL THROUGH.
7849 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7850 case ISD::SETLT:
7851 Opcode = X86ISD::FMAX;
7852 break;
7853 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007854 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007855
Chris Lattner47b4ce82009-03-11 05:48:52 +00007856 if (Opcode)
7857 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007858 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00007859
Chris Lattnerd1980a52009-03-12 06:52:53 +00007860 // If this is a select between two integer constants, try to do some
7861 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00007862 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
7863 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00007864 // Don't do this for crazy integer types.
7865 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
7866 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00007867 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007868 bool NeedsCondInvert = false;
7869
Chris Lattnercee56e72009-03-13 05:53:31 +00007870 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00007871 // Efficiently invertible.
7872 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
7873 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
7874 isa<ConstantSDNode>(Cond.getOperand(1))))) {
7875 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00007876 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00007877 }
7878
7879 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007880 if (FalseC->getAPIntValue() == 0 &&
7881 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00007882 if (NeedsCondInvert) // Invert the condition if needed.
7883 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7884 DAG.getConstant(1, Cond.getValueType()));
7885
7886 // Zero extend the condition if needed.
7887 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
7888
Chris Lattnercee56e72009-03-13 05:53:31 +00007889 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00007890 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
7891 DAG.getConstant(ShAmt, MVT::i8));
7892 }
Chris Lattner97a29a52009-03-13 05:22:11 +00007893
7894 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00007895 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00007896 if (NeedsCondInvert) // Invert the condition if needed.
7897 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7898 DAG.getConstant(1, Cond.getValueType()));
7899
7900 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00007901 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
7902 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00007903 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00007904 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00007905 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007906
7907 // Optimize cases that will turn into an LEA instruction. This requires
7908 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
7909 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
7910 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
7911 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
7912
7913 bool isFastMultiplier = false;
7914 if (Diff < 10) {
7915 switch ((unsigned char)Diff) {
7916 default: break;
7917 case 1: // result = add base, cond
7918 case 2: // result = lea base( , cond*2)
7919 case 3: // result = lea base(cond, cond*2)
7920 case 4: // result = lea base( , cond*4)
7921 case 5: // result = lea base(cond, cond*4)
7922 case 8: // result = lea base( , cond*8)
7923 case 9: // result = lea base(cond, cond*8)
7924 isFastMultiplier = true;
7925 break;
7926 }
7927 }
7928
7929 if (isFastMultiplier) {
7930 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
7931 if (NeedsCondInvert) // Invert the condition if needed.
7932 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
7933 DAG.getConstant(1, Cond.getValueType()));
7934
7935 // Zero extend the condition if needed.
7936 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
7937 Cond);
7938 // Scale the condition by the difference.
7939 if (Diff != 1)
7940 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
7941 DAG.getConstant(Diff, Cond.getValueType()));
7942
7943 // Add the base if non-zero.
7944 if (FalseC->getAPIntValue() != 0)
7945 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
7946 SDValue(FalseC, 0));
7947 return Cond;
7948 }
7949 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00007950 }
7951 }
7952
Dan Gohman475871a2008-07-27 21:46:04 +00007953 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007954}
7955
Chris Lattnerd1980a52009-03-12 06:52:53 +00007956/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
7957static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
7958 TargetLowering::DAGCombinerInfo &DCI) {
7959 DebugLoc DL = N->getDebugLoc();
7960
7961 // If the flag operand isn't dead, don't touch this CMOV.
7962 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
7963 return SDValue();
7964
7965 // If this is a select between two integer constants, try to do some
7966 // optimizations. Note that the operands are ordered the opposite of SELECT
7967 // operands.
7968 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
7969 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
7970 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
7971 // larger than FalseC (the false value).
7972 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
7973
7974 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
7975 CC = X86::GetOppositeBranchCondition(CC);
7976 std::swap(TrueC, FalseC);
7977 }
7978
7979 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00007980 // This is efficient for any integer data type (including i8/i16) and
7981 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00007982 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
7983 SDValue Cond = N->getOperand(3);
7984 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
7985 DAG.getConstant(CC, MVT::i8), Cond);
7986
7987 // Zero extend the condition if needed.
7988 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
7989
7990 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
7991 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
7992 DAG.getConstant(ShAmt, MVT::i8));
7993 if (N->getNumValues() == 2) // Dead flag value?
7994 return DCI.CombineTo(N, Cond, SDValue());
7995 return Cond;
7996 }
Chris Lattnercee56e72009-03-13 05:53:31 +00007997
7998 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
7999 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008000 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8001 SDValue Cond = N->getOperand(3);
8002 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8003 DAG.getConstant(CC, MVT::i8), Cond);
8004
8005 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008006 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8007 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008008 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8009 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008010
Chris Lattner97a29a52009-03-13 05:22:11 +00008011 if (N->getNumValues() == 2) // Dead flag value?
8012 return DCI.CombineTo(N, Cond, SDValue());
8013 return Cond;
8014 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008015
8016 // Optimize cases that will turn into an LEA instruction. This requires
8017 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8018 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8019 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8020 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8021
8022 bool isFastMultiplier = false;
8023 if (Diff < 10) {
8024 switch ((unsigned char)Diff) {
8025 default: break;
8026 case 1: // result = add base, cond
8027 case 2: // result = lea base( , cond*2)
8028 case 3: // result = lea base(cond, cond*2)
8029 case 4: // result = lea base( , cond*4)
8030 case 5: // result = lea base(cond, cond*4)
8031 case 8: // result = lea base( , cond*8)
8032 case 9: // result = lea base(cond, cond*8)
8033 isFastMultiplier = true;
8034 break;
8035 }
8036 }
8037
8038 if (isFastMultiplier) {
8039 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8040 SDValue Cond = N->getOperand(3);
8041 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8042 DAG.getConstant(CC, MVT::i8), Cond);
8043 // Zero extend the condition if needed.
8044 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8045 Cond);
8046 // Scale the condition by the difference.
8047 if (Diff != 1)
8048 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8049 DAG.getConstant(Diff, Cond.getValueType()));
8050
8051 // Add the base if non-zero.
8052 if (FalseC->getAPIntValue() != 0)
8053 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8054 SDValue(FalseC, 0));
8055 if (N->getNumValues() == 2) // Dead flag value?
8056 return DCI.CombineTo(N, Cond, SDValue());
8057 return Cond;
8058 }
8059 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008060 }
8061 }
8062 return SDValue();
8063}
8064
8065
Evan Cheng0b0cd912009-03-28 05:57:29 +00008066/// PerformMulCombine - Optimize a single multiply with constant into two
8067/// in order to implement it with two cheaper instructions, e.g.
8068/// LEA + SHL, LEA + LEA.
8069static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8070 TargetLowering::DAGCombinerInfo &DCI) {
8071 if (DAG.getMachineFunction().
8072 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8073 return SDValue();
8074
8075 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8076 return SDValue();
8077
8078 MVT VT = N->getValueType(0);
8079 if (VT != MVT::i64)
8080 return SDValue();
8081
8082 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8083 if (!C)
8084 return SDValue();
8085 uint64_t MulAmt = C->getZExtValue();
8086 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8087 return SDValue();
8088
8089 uint64_t MulAmt1 = 0;
8090 uint64_t MulAmt2 = 0;
8091 if ((MulAmt % 9) == 0) {
8092 MulAmt1 = 9;
8093 MulAmt2 = MulAmt / 9;
8094 } else if ((MulAmt % 5) == 0) {
8095 MulAmt1 = 5;
8096 MulAmt2 = MulAmt / 5;
8097 } else if ((MulAmt % 3) == 0) {
8098 MulAmt1 = 3;
8099 MulAmt2 = MulAmt / 3;
8100 }
8101 if (MulAmt2 &&
8102 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8103 DebugLoc DL = N->getDebugLoc();
8104
8105 if (isPowerOf2_64(MulAmt2) &&
8106 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8107 // If second multiplifer is pow2, issue it first. We want the multiply by
8108 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8109 // is an add.
8110 std::swap(MulAmt1, MulAmt2);
8111
8112 SDValue NewMul;
8113 if (isPowerOf2_64(MulAmt1))
8114 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8115 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8116 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008117 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008118 DAG.getConstant(MulAmt1, VT));
8119
8120 if (isPowerOf2_64(MulAmt2))
8121 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8122 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8123 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008124 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008125 DAG.getConstant(MulAmt2, VT));
8126
8127 // Do not add new nodes to DAG combiner worklist.
8128 DCI.CombineTo(N, NewMul, false);
8129 }
8130 return SDValue();
8131}
8132
8133
Nate Begeman740ab032009-01-26 00:52:55 +00008134/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8135/// when possible.
8136static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8137 const X86Subtarget *Subtarget) {
8138 // On X86 with SSE2 support, we can transform this to a vector shift if
8139 // all elements are shifted by the same amount. We can't do this in legalize
8140 // because the a constant vector is typically transformed to a constant pool
8141 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008142 if (!Subtarget->hasSSE2())
8143 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008144
Nate Begeman740ab032009-01-26 00:52:55 +00008145 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008146 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8147 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008148
Mon P Wang3becd092009-01-28 08:12:05 +00008149 SDValue ShAmtOp = N->getOperand(1);
8150 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008151 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008152 SDValue BaseShAmt;
8153 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8154 unsigned NumElts = VT.getVectorNumElements();
8155 unsigned i = 0;
8156 for (; i != NumElts; ++i) {
8157 SDValue Arg = ShAmtOp.getOperand(i);
8158 if (Arg.getOpcode() == ISD::UNDEF) continue;
8159 BaseShAmt = Arg;
8160 break;
8161 }
8162 for (; i != NumElts; ++i) {
8163 SDValue Arg = ShAmtOp.getOperand(i);
8164 if (Arg.getOpcode() == ISD::UNDEF) continue;
8165 if (Arg != BaseShAmt) {
8166 return SDValue();
8167 }
8168 }
8169 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008170 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8171 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8172 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008173 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008174 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008175
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008176 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008177 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008178 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008179 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008180
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008181 // The shift amount is identical so we can do a vector shift.
8182 SDValue ValOp = N->getOperand(0);
8183 switch (N->getOpcode()) {
8184 default:
8185 assert(0 && "Unknown shift opcode!");
8186 break;
8187 case ISD::SHL:
8188 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008189 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008190 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8191 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008192 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008193 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008194 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8195 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008196 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008197 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008198 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8199 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008200 break;
8201 case ISD::SRA:
8202 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008203 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008204 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8205 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008206 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008207 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008208 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8209 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008210 break;
8211 case ISD::SRL:
8212 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008213 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008214 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8215 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008216 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008217 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008218 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8219 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008220 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008221 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008222 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8223 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008224 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008225 }
8226 return SDValue();
8227}
8228
Chris Lattner149a4e52008-02-22 02:09:43 +00008229/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008230static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008231 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008232 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8233 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008234 // A preferable solution to the general problem is to figure out the right
8235 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008236
8237 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008238 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008239 MVT VT = St->getValue().getValueType();
8240 if (VT.getSizeInBits() != 64)
8241 return SDValue();
8242
8243 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloat && Subtarget->hasSSE2();
8244 if ((VT.isVector() ||
8245 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008246 isa<LoadSDNode>(St->getValue()) &&
8247 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8248 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008249 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008250 LoadSDNode *Ld = 0;
8251 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008252 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008253 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008254 // Must be a store of a load. We currently handle two cases: the load
8255 // is a direct child, and it's under an intervening TokenFactor. It is
8256 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008257 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008258 Ld = cast<LoadSDNode>(St->getChain());
8259 else if (St->getValue().hasOneUse() &&
8260 ChainVal->getOpcode() == ISD::TokenFactor) {
8261 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008262 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008263 TokenFactorIndex = i;
8264 Ld = cast<LoadSDNode>(St->getValue());
8265 } else
8266 Ops.push_back(ChainVal->getOperand(i));
8267 }
8268 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008269
Evan Cheng536e6672009-03-12 05:59:15 +00008270 if (!Ld || !ISD::isNormalLoad(Ld))
8271 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008272
Evan Cheng536e6672009-03-12 05:59:15 +00008273 // If this is not the MMX case, i.e. we are just turning i64 load/store
8274 // into f64 load/store, avoid the transformation if there are multiple
8275 // uses of the loaded value.
8276 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8277 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008278
Evan Cheng536e6672009-03-12 05:59:15 +00008279 DebugLoc LdDL = Ld->getDebugLoc();
8280 DebugLoc StDL = N->getDebugLoc();
8281 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8282 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8283 // pair instead.
8284 if (Subtarget->is64Bit() || F64IsLegal) {
8285 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8286 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8287 Ld->getBasePtr(), Ld->getSrcValue(),
8288 Ld->getSrcValueOffset(), Ld->isVolatile(),
8289 Ld->getAlignment());
8290 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008291 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008292 Ops.push_back(NewChain);
8293 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008294 Ops.size());
8295 }
Evan Cheng536e6672009-03-12 05:59:15 +00008296 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008297 St->getSrcValue(), St->getSrcValueOffset(),
8298 St->isVolatile(), St->getAlignment());
8299 }
Evan Cheng536e6672009-03-12 05:59:15 +00008300
8301 // Otherwise, lower to two pairs of 32-bit loads / stores.
8302 SDValue LoAddr = Ld->getBasePtr();
8303 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8304 DAG.getConstant(4, MVT::i32));
8305
8306 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8307 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8308 Ld->isVolatile(), Ld->getAlignment());
8309 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8310 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8311 Ld->isVolatile(),
8312 MinAlign(Ld->getAlignment(), 4));
8313
8314 SDValue NewChain = LoLd.getValue(1);
8315 if (TokenFactorIndex != -1) {
8316 Ops.push_back(LoLd);
8317 Ops.push_back(HiLd);
8318 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8319 Ops.size());
8320 }
8321
8322 LoAddr = St->getBasePtr();
8323 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8324 DAG.getConstant(4, MVT::i32));
8325
8326 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8327 St->getSrcValue(), St->getSrcValueOffset(),
8328 St->isVolatile(), St->getAlignment());
8329 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8330 St->getSrcValue(),
8331 St->getSrcValueOffset() + 4,
8332 St->isVolatile(),
8333 MinAlign(St->getAlignment(), 4));
8334 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008335 }
Dan Gohman475871a2008-07-27 21:46:04 +00008336 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008337}
8338
Chris Lattner6cf73262008-01-25 06:14:17 +00008339/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8340/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008341static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008342 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8343 // F[X]OR(0.0, x) -> x
8344 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008345 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8346 if (C->getValueAPF().isPosZero())
8347 return N->getOperand(1);
8348 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8349 if (C->getValueAPF().isPosZero())
8350 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008351 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008352}
8353
8354/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008355static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008356 // FAND(0.0, x) -> 0.0
8357 // FAND(x, 0.0) -> 0.0
8358 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8359 if (C->getValueAPF().isPosZero())
8360 return N->getOperand(0);
8361 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8362 if (C->getValueAPF().isPosZero())
8363 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008364 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008365}
8366
Dan Gohmane5af2d32009-01-29 01:59:02 +00008367static SDValue PerformBTCombine(SDNode *N,
8368 SelectionDAG &DAG,
8369 TargetLowering::DAGCombinerInfo &DCI) {
8370 // BT ignores high bits in the bit index operand.
8371 SDValue Op1 = N->getOperand(1);
8372 if (Op1.hasOneUse()) {
8373 unsigned BitWidth = Op1.getValueSizeInBits();
8374 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8375 APInt KnownZero, KnownOne;
8376 TargetLowering::TargetLoweringOpt TLO(DAG);
8377 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8378 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8379 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8380 DCI.CommitTargetLoweringOpt(TLO);
8381 }
8382 return SDValue();
8383}
Chris Lattner83e6c992006-10-04 06:57:07 +00008384
Dan Gohman475871a2008-07-27 21:46:04 +00008385SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008386 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008387 SelectionDAG &DAG = DCI.DAG;
8388 switch (N->getOpcode()) {
8389 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008390 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
8391 case ISD::BUILD_VECTOR:
Dan Gohmane5af2d32009-01-29 01:59:02 +00008392 return PerformBuildVectorCombine(N, DAG, DCI, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008393 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008394 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008395 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008396 case ISD::SHL:
8397 case ISD::SRA:
8398 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008399 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008400 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008401 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8402 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008403 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008404 }
8405
Dan Gohman475871a2008-07-27 21:46:04 +00008406 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008407}
8408
Evan Cheng60c07e12006-07-05 22:17:51 +00008409//===----------------------------------------------------------------------===//
8410// X86 Inline Assembly Support
8411//===----------------------------------------------------------------------===//
8412
Chris Lattnerf4dff842006-07-11 02:54:03 +00008413/// getConstraintType - Given a constraint letter, return the type of
8414/// constraint it is for this target.
8415X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008416X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8417 if (Constraint.size() == 1) {
8418 switch (Constraint[0]) {
8419 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008420 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008421 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008422 case 'r':
8423 case 'R':
8424 case 'l':
8425 case 'q':
8426 case 'Q':
8427 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008428 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008429 case 'Y':
8430 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008431 case 'e':
8432 case 'Z':
8433 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008434 default:
8435 break;
8436 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008437 }
Chris Lattner4234f572007-03-25 02:14:49 +00008438 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008439}
8440
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008441/// LowerXConstraint - try to replace an X constraint, which matches anything,
8442/// with another that has more specific requirements based on the type of the
8443/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008444const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008445LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008446 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8447 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008448 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008449 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008450 return "Y";
8451 if (Subtarget->hasSSE1())
8452 return "x";
8453 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008454
Chris Lattner5e764232008-04-26 23:02:14 +00008455 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008456}
8457
Chris Lattner48884cd2007-08-25 00:47:38 +00008458/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8459/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008460void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008461 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008462 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008463 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008464 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008465 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008466
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008467 switch (Constraint) {
8468 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008469 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008470 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008471 if (C->getZExtValue() <= 31) {
8472 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008473 break;
8474 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008475 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008476 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008477 case 'J':
8478 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8479 if (C->getZExtValue() <= 63) {
8480 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8481 break;
8482 }
8483 }
8484 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008485 case 'N':
8486 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008487 if (C->getZExtValue() <= 255) {
8488 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008489 break;
8490 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008491 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008492 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008493 case 'e': {
8494 // 32-bit signed value
8495 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8496 const ConstantInt *CI = C->getConstantIntValue();
8497 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8498 // Widen to 64 bits here to get it sign extended.
8499 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8500 break;
8501 }
8502 // FIXME gcc accepts some relocatable values here too, but only in certain
8503 // memory models; it's complicated.
8504 }
8505 return;
8506 }
8507 case 'Z': {
8508 // 32-bit unsigned value
8509 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8510 const ConstantInt *CI = C->getConstantIntValue();
8511 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8512 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8513 break;
8514 }
8515 }
8516 // FIXME gcc accepts some relocatable values here too, but only in certain
8517 // memory models; it's complicated.
8518 return;
8519 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008520 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008521 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008522 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008523 // Widen to 64 bits here to get it sign extended.
8524 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008525 break;
8526 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008527
Chris Lattnerdc43a882007-05-03 16:52:29 +00008528 // If we are in non-pic codegen mode, we allow the address of a global (with
8529 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008530 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008531 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008532
Chris Lattner49921962009-05-08 18:23:14 +00008533 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8534 while (1) {
8535 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8536 Offset += GA->getOffset();
8537 break;
8538 } else if (Op.getOpcode() == ISD::ADD) {
8539 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8540 Offset += C->getZExtValue();
8541 Op = Op.getOperand(0);
8542 continue;
8543 }
8544 } else if (Op.getOpcode() == ISD::SUB) {
8545 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8546 Offset += -C->getZExtValue();
8547 Op = Op.getOperand(0);
8548 continue;
8549 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008550 }
Chris Lattner49921962009-05-08 18:23:14 +00008551
8552 // Otherwise, this isn't something we can handle, reject it.
8553 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008554 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008555
Chris Lattner49921962009-05-08 18:23:14 +00008556 if (hasMemory)
8557 Op = LowerGlobalAddress(GA->getGlobal(), Op.getDebugLoc(), Offset, DAG);
8558 else
8559 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
8560 Offset);
8561 Result = Op;
8562 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008563 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008564 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008565
Gabor Greifba36cb52008-08-28 21:40:38 +00008566 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008567 Ops.push_back(Result);
8568 return;
8569 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008570 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8571 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008572}
8573
Chris Lattner259e97c2006-01-31 19:43:35 +00008574std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008575getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008576 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008577 if (Constraint.size() == 1) {
8578 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008579 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008580 default: break; // Unknown constraint letter
Chris Lattner259e97c2006-01-31 19:43:35 +00008581 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
8582 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008583 if (VT == MVT::i32)
8584 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8585 else if (VT == MVT::i16)
8586 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8587 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008588 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008589 else if (VT == MVT::i64)
8590 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8591 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008592 }
8593 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008594
Chris Lattner1efa40f2006-02-22 00:56:39 +00008595 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008596}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008597
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008598std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00008599X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008600 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00008601 // First, see if this is a constraint that directly corresponds to an LLVM
8602 // register class.
8603 if (Constraint.size() == 1) {
8604 // GCC Constraint Letters
8605 switch (Constraint[0]) {
8606 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00008607 case 'r': // GENERAL_REGS
8608 case 'R': // LEGACY_REGS
8609 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00008610 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00008611 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008612 if (VT == MVT::i16)
8613 return std::make_pair(0U, X86::GR16RegisterClass);
8614 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00008615 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00008616 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008617 case 'f': // FP Stack registers.
8618 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
8619 // value to the correct fpstack register class.
8620 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
8621 return std::make_pair(0U, X86::RFP32RegisterClass);
8622 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
8623 return std::make_pair(0U, X86::RFP64RegisterClass);
8624 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00008625 case 'y': // MMX_REGS if MMX allowed.
8626 if (!Subtarget->hasMMX()) break;
8627 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008628 case 'Y': // SSE_REGS if SSE2 allowed
8629 if (!Subtarget->hasSSE2()) break;
8630 // FALL THROUGH.
8631 case 'x': // SSE_REGS if SSE1 allowed
8632 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00008633
8634 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00008635 default: break;
8636 // Scalar SSE types.
8637 case MVT::f32:
8638 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00008639 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008640 case MVT::f64:
8641 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00008642 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00008643 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00008644 case MVT::v16i8:
8645 case MVT::v8i16:
8646 case MVT::v4i32:
8647 case MVT::v2i64:
8648 case MVT::v4f32:
8649 case MVT::v2f64:
8650 return std::make_pair(0U, X86::VR128RegisterClass);
8651 }
Chris Lattnerad043e82007-04-09 05:11:28 +00008652 break;
8653 }
8654 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008655
Chris Lattnerf76d1802006-07-31 23:26:50 +00008656 // Use the default implementation in TargetLowering to convert the register
8657 // constraint into a member of a register class.
8658 std::pair<unsigned, const TargetRegisterClass*> Res;
8659 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00008660
8661 // Not found as a standard register?
8662 if (Res.second == 0) {
8663 // GCC calls "st(0)" just plain "st".
8664 if (StringsEqualNoCase("{st}", Constraint)) {
8665 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00008666 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00008667 }
Dale Johannesen330169f2008-11-13 21:52:36 +00008668 // 'A' means EAX + EDX.
8669 if (Constraint == "A") {
8670 Res.first = X86::EAX;
8671 Res.second = X86::GRADRegisterClass;
8672 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00008673 return Res;
8674 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008675
Chris Lattnerf76d1802006-07-31 23:26:50 +00008676 // Otherwise, check to see if this is a register class of the wrong value
8677 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
8678 // turn into {ax},{dx}.
8679 if (Res.second->hasType(VT))
8680 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008681
Chris Lattnerf76d1802006-07-31 23:26:50 +00008682 // All of the single-register GCC register classes map their values onto
8683 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
8684 // really want an 8-bit or 32-bit register, map to the appropriate register
8685 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00008686 if (Res.second == X86::GR16RegisterClass) {
8687 if (VT == MVT::i8) {
8688 unsigned DestReg = 0;
8689 switch (Res.first) {
8690 default: break;
8691 case X86::AX: DestReg = X86::AL; break;
8692 case X86::DX: DestReg = X86::DL; break;
8693 case X86::CX: DestReg = X86::CL; break;
8694 case X86::BX: DestReg = X86::BL; break;
8695 }
8696 if (DestReg) {
8697 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008698 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008699 }
8700 } else if (VT == MVT::i32) {
8701 unsigned DestReg = 0;
8702 switch (Res.first) {
8703 default: break;
8704 case X86::AX: DestReg = X86::EAX; break;
8705 case X86::DX: DestReg = X86::EDX; break;
8706 case X86::CX: DestReg = X86::ECX; break;
8707 case X86::BX: DestReg = X86::EBX; break;
8708 case X86::SI: DestReg = X86::ESI; break;
8709 case X86::DI: DestReg = X86::EDI; break;
8710 case X86::BP: DestReg = X86::EBP; break;
8711 case X86::SP: DestReg = X86::ESP; break;
8712 }
8713 if (DestReg) {
8714 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008715 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008716 }
8717 } else if (VT == MVT::i64) {
8718 unsigned DestReg = 0;
8719 switch (Res.first) {
8720 default: break;
8721 case X86::AX: DestReg = X86::RAX; break;
8722 case X86::DX: DestReg = X86::RDX; break;
8723 case X86::CX: DestReg = X86::RCX; break;
8724 case X86::BX: DestReg = X86::RBX; break;
8725 case X86::SI: DestReg = X86::RSI; break;
8726 case X86::DI: DestReg = X86::RDI; break;
8727 case X86::BP: DestReg = X86::RBP; break;
8728 case X86::SP: DestReg = X86::RSP; break;
8729 }
8730 if (DestReg) {
8731 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00008732 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00008733 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00008734 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00008735 } else if (Res.second == X86::FR32RegisterClass ||
8736 Res.second == X86::FR64RegisterClass ||
8737 Res.second == X86::VR128RegisterClass) {
8738 // Handle references to XMM physical registers that got mapped into the
8739 // wrong class. This can happen with constraints like {xmm0} where the
8740 // target independent register mapper will just pick the first match it can
8741 // find, ignoring the required type.
8742 if (VT == MVT::f32)
8743 Res.second = X86::FR32RegisterClass;
8744 else if (VT == MVT::f64)
8745 Res.second = X86::FR64RegisterClass;
8746 else if (X86::VR128RegisterClass->hasType(VT))
8747 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00008748 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008749
Chris Lattnerf76d1802006-07-31 23:26:50 +00008750 return Res;
8751}
Mon P Wang0c397192008-10-30 08:01:45 +00008752
8753//===----------------------------------------------------------------------===//
8754// X86 Widen vector type
8755//===----------------------------------------------------------------------===//
8756
8757/// getWidenVectorType: given a vector type, returns the type to widen
8758/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
8759/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00008760/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00008761/// scalarizing vs using the wider vector type.
8762
Dan Gohmanc13cf132009-01-15 17:34:08 +00008763MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00008764 assert(VT.isVector());
8765 if (isTypeLegal(VT))
8766 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008767
Mon P Wang0c397192008-10-30 08:01:45 +00008768 // TODO: In computeRegisterProperty, we can compute the list of legal vector
8769 // type based on element type. This would speed up our search (though
8770 // it may not be worth it since the size of the list is relatively
8771 // small).
8772 MVT EltVT = VT.getVectorElementType();
8773 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00008774
Mon P Wang0c397192008-10-30 08:01:45 +00008775 // On X86, it make sense to widen any vector wider than 1
8776 if (NElts <= 1)
8777 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00008778
8779 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00008780 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
8781 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00008782
8783 if (isTypeLegal(SVT) &&
8784 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00008785 SVT.getVectorNumElements() > NElts)
8786 return SVT;
8787 }
8788 return MVT::Other;
8789}