blob: 74db8efe27a5c27db068e33800326e84e91b868a [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"
18#include "X86TargetMachine.h"
19#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000020#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000021#include "llvm/DerivedTypes.h"
Chris Lattnerb903bed2009-06-26 21:20:29 +000022#include "llvm/GlobalAlias.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"
Chris Lattnerb8105652009-07-20 17:51:36 +000025#include "llvm/Instructions.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000026#include "llvm/Intrinsics.h"
Owen Andersona90b3dc2009-07-15 21:51:10 +000027#include "llvm/LLVMContext.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000028#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000029#include "llvm/ADT/VectorExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.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"
Torok Edwinab7c09b2009-07-08 18:01:40 +000038#include "llvm/Support/ErrorHandling.h"
Chris Lattnerf0144122009-07-28 03:13:23 +000039#include "llvm/Target/TargetLoweringObjectFile.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000040#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000041#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000042#include "llvm/ADT/StringExtras.h"
Mon P Wang3c81d352008-11-23 04:37:22 +000043#include "llvm/Support/CommandLine.h"
Torok Edwindac237e2009-07-08 20:53:28 +000044#include "llvm/Support/raw_ostream.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000045using namespace llvm;
46
Mon P Wang3c81d352008-11-23 04:37:22 +000047static cl::opt<bool>
Mon P Wang9f22a4a2008-11-24 02:10:43 +000048DisableMMX("disable-mmx", cl::Hidden, cl::desc("Disable use of MMX"));
Mon P Wang3c81d352008-11-23 04:37:22 +000049
Evan Cheng10e86422008-04-25 19:11:04 +000050// Forward declarations.
Nate Begeman9008ca62009-04-27 18:41:29 +000051static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
52 SDValue V2);
Evan Cheng10e86422008-04-25 19:11:04 +000053
Chris Lattnerf0144122009-07-28 03:13:23 +000054static TargetLoweringObjectFile *createTLOF(X86TargetMachine &TM) {
55 switch (TM.getSubtarget<X86Subtarget>().TargetType) {
56 default: llvm_unreachable("unknown subtarget type");
57 case X86Subtarget::isDarwin:
Chris Lattnerf26e03b2009-07-31 17:42:42 +000058 return new TargetLoweringObjectFileMachO();
Chris Lattnerf0144122009-07-28 03:13:23 +000059 case X86Subtarget::isELF:
60 return new TargetLoweringObjectFileELF();
61 case X86Subtarget::isMingw:
62 case X86Subtarget::isCygwin:
63 case X86Subtarget::isWindows:
64 return new TargetLoweringObjectFileCOFF();
65 }
66
67}
68
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000069X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerf0144122009-07-28 03:13:23 +000070 : TargetLowering(TM, createTLOF(TM)) {
Evan Cheng559806f2006-01-27 08:10:46 +000071 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000072 X86ScalarSSEf64 = Subtarget->hasSSE2();
73 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000074 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000075
Anton Korobeynikov2365f512007-07-14 14:06:15 +000076 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000077 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000078
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000079 // Set up the TargetLowering object.
80
81 // X86 is weird, it always uses i8 for shift amounts and setcc results.
82 setShiftAmountType(MVT::i8);
Duncan Sands03228082008-11-23 15:47:28 +000083 setBooleanContents(ZeroOrOneBooleanContent);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000084 setSchedulingPreference(SchedulingForRegPressure);
Evan Cheng25ab6902006-09-08 06:48:29 +000085 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000086
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000087 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000088 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000089 setUseUnderscoreSetJmp(false);
90 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000091 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000092 // MS runtime is weird: it exports _setjmp, but longjmp!
93 setUseUnderscoreSetJmp(true);
94 setUseUnderscoreLongJmp(false);
95 } else {
96 setUseUnderscoreSetJmp(true);
97 setUseUnderscoreLongJmp(true);
98 }
Scott Michelfdc40a02009-02-17 22:15:04 +000099
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000100 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +0000101 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
102 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
103 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +0000104 if (Subtarget->is64Bit())
105 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000106
Evan Cheng03294662008-10-14 21:26:46 +0000107 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +0000108
Scott Michelfdc40a02009-02-17 22:15:04 +0000109 // We don't accept any truncstore of integer registers.
Chris Lattnerddf89562008-01-17 19:59:44 +0000110 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
111 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
112 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
113 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
114 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +0000115 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
116
117 // SETOEQ and SETUNE require checking two conditions.
118 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
119 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
120 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
121 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
122 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
123 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000124
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000125 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
126 // operation.
127 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
128 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
129 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000130
Evan Cheng25ab6902006-09-08 06:48:29 +0000131 if (Subtarget->is64Bit()) {
Evan Cheng6892f282006-01-17 02:32:49 +0000132 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000133 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Eli Friedman948e95a2009-05-23 09:59:16 +0000134 } else if (!UseSoftFloat) {
135 if (X86ScalarSSEf64) {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000136 // We have an impenetrably clever algorithm for ui64->double only.
137 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Bill Wendling105be5a2009-03-13 08:41:47 +0000138 }
Eli Friedman948e95a2009-05-23 09:59:16 +0000139 // We have an algorithm for SSE2, and we turn this into a 64-bit
140 // FILD for other targets.
141 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000142 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143
144 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
145 // this operation.
146 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
147 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Bill Wendling105be5a2009-03-13 08:41:47 +0000148
Devang Patel6a784892009-06-05 18:48:29 +0000149 if (!UseSoftFloat) {
Bill Wendling105be5a2009-03-13 08:41:47 +0000150 // SSE has no i16 to fp conversion, only i32
151 if (X86ScalarSSEf32) {
152 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
153 // f32 and f64 cases are Legal, f80 case is not
154 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
155 } else {
156 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
157 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
158 }
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000159 } else {
Bill Wendling105be5a2009-03-13 08:41:47 +0000160 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
161 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Promote);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000162 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000163
Dale Johannesen73328d12007-09-19 23:55:34 +0000164 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
165 // are Legal, f80 is custom lowered.
166 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
167 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000168
Evan Cheng02568ff2006-01-30 22:13:22 +0000169 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
170 // this operation.
171 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
172 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
173
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000174 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000175 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000176 // f32 and f64 cases are Legal, f80 case is not
177 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000178 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000179 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000180 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000181 }
182
183 // Handle FP_TO_UINT by promoting the destination to a larger signed
184 // conversion.
185 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
186 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
187 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
188
Evan Cheng25ab6902006-09-08 06:48:29 +0000189 if (Subtarget->is64Bit()) {
190 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000191 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Eli Friedman948e95a2009-05-23 09:59:16 +0000192 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000193 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000194 // Expand FP_TO_UINT into a select.
195 // FIXME: We would like to use a Custom expander here eventually to do
196 // the optimal thing for SSE vs. the default expansion in the legalizer.
197 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
198 else
Eli Friedman948e95a2009-05-23 09:59:16 +0000199 // With SSE3 we can use fisttpll to convert to a signed i64; without
200 // SSE, we're stuck with a fistpll.
201 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000202 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000203
Chris Lattner399610a2006-12-05 18:22:22 +0000204 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000205 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000206 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
207 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
208 }
Chris Lattner21f66852005-12-23 05:15:23 +0000209
Dan Gohmanb00ee212008-02-18 19:34:53 +0000210 // Scalar integer divide and remainder are lowered to use operations that
211 // produce two results, to match the available instructions. This exposes
212 // the two-result form to trivial CSE, which is able to combine x/y and x%y
213 // into a single instruction.
214 //
215 // Scalar integer multiply-high is also lowered to use two-result
216 // operations, to match the available instructions. However, plain multiply
217 // (low) operations are left as Legal, as there are single-result
218 // instructions for this in x86. Using the two-result multiply instructions
219 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000220 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
221 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
222 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
223 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
224 setOperationAction(ISD::SREM , MVT::i8 , Expand);
225 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000226 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
227 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
228 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
229 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
230 setOperationAction(ISD::SREM , MVT::i16 , Expand);
231 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000232 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
233 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
234 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
235 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
236 setOperationAction(ISD::SREM , MVT::i32 , Expand);
237 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000238 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
239 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
240 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
241 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
242 setOperationAction(ISD::SREM , MVT::i64 , Expand);
243 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000244
Evan Chengc35497f2006-10-30 08:02:39 +0000245 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000246 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000247 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
248 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000249 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000250 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
251 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
252 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000253 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
254 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000255 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000256 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000257 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000258 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000259
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000260 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000261 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
262 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000263 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000264 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
265 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000266 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000267 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
268 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000269 if (Subtarget->is64Bit()) {
270 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000271 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
272 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 }
274
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000275 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000276 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000277
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000278 // These should be promoted to a larger select which is supported.
279 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
280 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000281 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000282 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
283 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
284 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
285 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000286 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000287 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
288 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
289 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
290 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
291 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000292 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000293 if (Subtarget->is64Bit()) {
294 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
295 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
296 }
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000297 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000298
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000299 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000300 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000301 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000302 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000303 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000304 if (Subtarget->is64Bit())
305 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000306 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000307 if (Subtarget->is64Bit()) {
308 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
309 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
310 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000311 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000312 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000313 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000314 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
315 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
316 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000317 if (Subtarget->is64Bit()) {
318 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
319 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
320 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
321 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000322
Evan Chengd2cde682008-03-10 19:38:10 +0000323 if (Subtarget->hasSSE1())
324 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000325
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000326 if (!Subtarget->hasSSE2())
327 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
328
Mon P Wang63307c32008-05-05 19:05:59 +0000329 // Expand certain atomics
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000330 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i8, Custom);
331 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i16, Custom);
332 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);
333 setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000334
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000335 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i8, Custom);
336 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i16, Custom);
337 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i32, Custom);
338 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000339
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000340 if (!Subtarget->is64Bit()) {
Dan Gohman0b1d4a72008-12-23 21:37:04 +0000341 setOperationAction(ISD::ATOMIC_LOAD_ADD, MVT::i64, Custom);
342 setOperationAction(ISD::ATOMIC_LOAD_SUB, MVT::i64, Custom);
343 setOperationAction(ISD::ATOMIC_LOAD_AND, MVT::i64, Custom);
344 setOperationAction(ISD::ATOMIC_LOAD_OR, MVT::i64, Custom);
345 setOperationAction(ISD::ATOMIC_LOAD_XOR, MVT::i64, Custom);
346 setOperationAction(ISD::ATOMIC_LOAD_NAND, MVT::i64, Custom);
347 setOperationAction(ISD::ATOMIC_SWAP, MVT::i64, Custom);
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000348 }
349
Dan Gohman7f460202008-06-30 20:59:49 +0000350 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
351 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000352 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000353 if (!Subtarget->isTargetDarwin() &&
354 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000355 !Subtarget->isTargetCygMing()) {
356 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
357 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
358 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000359
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000360 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
361 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
362 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
363 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
364 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000365 setExceptionPointerRegister(X86::RAX);
366 setExceptionSelectorRegister(X86::RDX);
367 } else {
368 setExceptionPointerRegister(X86::EAX);
369 setExceptionSelectorRegister(X86::EDX);
370 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000371 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000372 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
373
Duncan Sandsf7331b32007-09-11 14:10:23 +0000374 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000375
Chris Lattnerda68d302008-01-15 21:58:22 +0000376 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000377
Nate Begemanacc398c2006-01-25 18:21:52 +0000378 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
379 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000380 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000381 if (Subtarget->is64Bit()) {
382 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000383 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000384 } else {
385 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000386 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000387 }
Evan Chengae642192007-03-02 23:16:35 +0000388
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000389 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000390 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000391 if (Subtarget->is64Bit())
392 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000393 if (Subtarget->isTargetCygMing())
394 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
395 else
396 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000397
Evan Chengc7ce29b2009-02-13 22:36:38 +0000398 if (!UseSoftFloat && X86ScalarSSEf64) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000399 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000400 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000401 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
402 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000403
Evan Cheng223547a2006-01-31 22:28:30 +0000404 // Use ANDPD to simulate FABS.
405 setOperationAction(ISD::FABS , MVT::f64, Custom);
406 setOperationAction(ISD::FABS , MVT::f32, Custom);
407
408 // Use XORP to simulate FNEG.
409 setOperationAction(ISD::FNEG , MVT::f64, Custom);
410 setOperationAction(ISD::FNEG , MVT::f32, Custom);
411
Evan Cheng68c47cb2007-01-05 07:55:56 +0000412 // Use ANDPD and ORPD to simulate FCOPYSIGN.
413 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
414 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
415
Evan Chengd25e9e82006-02-02 00:28:23 +0000416 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000417 setOperationAction(ISD::FSIN , MVT::f64, Expand);
418 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000419 setOperationAction(ISD::FSIN , MVT::f32, Expand);
420 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000421
Chris Lattnera54aa942006-01-29 06:26:08 +0000422 // Expand FP immediates into loads from the stack, except for the special
423 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000424 addLegalFPImmediate(APFloat(+0.0)); // xorpd
425 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Evan Chengc7ce29b2009-02-13 22:36:38 +0000426 } else if (!UseSoftFloat && X86ScalarSSEf32) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000427 // Use SSE for f32, x87 for f64.
428 // Set up the FP register classes.
429 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
430 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
431
432 // Use ANDPS to simulate FABS.
433 setOperationAction(ISD::FABS , MVT::f32, Custom);
434
435 // Use XORP to simulate FNEG.
436 setOperationAction(ISD::FNEG , MVT::f32, Custom);
437
438 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
439
440 // Use ANDPS and ORPS to simulate FCOPYSIGN.
441 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
442 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
443
444 // We don't support sin/cos/fmod
445 setOperationAction(ISD::FSIN , MVT::f32, Expand);
446 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000447
Nate Begemane1795842008-02-14 08:57:00 +0000448 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000449 addLegalFPImmediate(APFloat(+0.0f)); // xorps
450 addLegalFPImmediate(APFloat(+0.0)); // FLD0
451 addLegalFPImmediate(APFloat(+1.0)); // FLD1
452 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
453 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
454
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000455 if (!UnsafeFPMath) {
456 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
457 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
458 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000459 } else if (!UseSoftFloat) {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000460 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000461 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000462 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
463 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000464
Evan Cheng68c47cb2007-01-05 07:55:56 +0000465 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000466 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000467 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
468 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000469
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000470 if (!UnsafeFPMath) {
471 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
472 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
473 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000474 addLegalFPImmediate(APFloat(+0.0)); // FLD0
475 addLegalFPImmediate(APFloat(+1.0)); // FLD1
476 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
477 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000478 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
479 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
480 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
481 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000482 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000483
Dale Johannesen59a58732007-08-05 18:49:15 +0000484 // Long double always uses X87.
Evan Cheng92722532009-03-26 23:06:32 +0000485 if (!UseSoftFloat) {
Evan Chengc7ce29b2009-02-13 22:36:38 +0000486 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
487 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
488 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
489 {
490 bool ignored;
491 APFloat TmpFlt(+0.0);
492 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
493 &ignored);
494 addLegalFPImmediate(TmpFlt); // FLD0
495 TmpFlt.changeSign();
496 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
497 APFloat TmpFlt2(+1.0);
498 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
499 &ignored);
500 addLegalFPImmediate(TmpFlt2); // FLD1
501 TmpFlt2.changeSign();
502 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
503 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000504
Evan Chengc7ce29b2009-02-13 22:36:38 +0000505 if (!UnsafeFPMath) {
506 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
507 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
508 }
Dale Johannesen2f429012007-09-26 21:10:55 +0000509 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000510
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000511 // Always use a library call for pow.
512 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
513 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
514 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
515
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000516 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000517 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000518 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000519 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000520 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
521
Mon P Wangf007a8b2008-11-06 05:31:54 +0000522 // First set operation action for all vector types to either promote
Mon P Wang0c397192008-10-30 08:01:45 +0000523 // (for widening) or expand (for scalarization). Then we will selectively
524 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000525 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
526 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000527 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000540 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
Eli Friedman108b5192009-05-23 22:44:52 +0000542 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT,Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000543 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000544 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
546 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
547 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
549 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000566 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Eli Friedman23ef1052009-06-06 03:57:58 +0000571 setOperationAction(ISD::FP_TO_UINT, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FP_TO_SINT, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::UINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::SINT_TO_FP, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000575 }
576
Evan Chengc7ce29b2009-02-13 22:36:38 +0000577 // FIXME: In order to prevent SSE instructions being expanded to MMX ones
578 // with -msoft-float, disable use of MMX as well.
Evan Cheng92722532009-03-26 23:06:32 +0000579 if (!UseSoftFloat && !DisableMMX && Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000580 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
581 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
582 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000583 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000584 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000585
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000586 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
587 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
588 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000589 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000590
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000591 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
592 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
593 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000594 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000595
Bill Wendling74027e92007-03-15 21:24:36 +0000596 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
597 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
598
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000599 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000600 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000601 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000602 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
603 setOperationAction(ISD::AND, MVT::v2i32, Promote);
604 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
605 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000606
607 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000608 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000609 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000610 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
611 setOperationAction(ISD::OR, MVT::v2i32, Promote);
612 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
613 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000614
615 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000616 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000617 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000618 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
619 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
620 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
621 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000622
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000623 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000624 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000625 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000626 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
627 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
628 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000629 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
630 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000631 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000632
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000633 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
634 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
635 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000636 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000637 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000638
639 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
640 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
641 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000643
Evan Cheng52672b82008-07-22 18:39:19 +0000644 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000648
649 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000650
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000651 setTruncStoreAction(MVT::v8i16, MVT::v8i8, Expand);
Mon P Wang9e5ecb82008-12-12 01:25:51 +0000652 setOperationAction(ISD::TRUNCATE, MVT::v8i8, Expand);
653 setOperationAction(ISD::SELECT, MVT::v8i8, Promote);
654 setOperationAction(ISD::SELECT, MVT::v4i16, Promote);
655 setOperationAction(ISD::SELECT, MVT::v2i32, Promote);
656 setOperationAction(ISD::SELECT, MVT::v1i64, Custom);
Eli Friedman3dae2842009-07-22 01:06:52 +0000657 setOperationAction(ISD::VSETCC, MVT::v8i8, Custom);
658 setOperationAction(ISD::VSETCC, MVT::v4i16, Custom);
659 setOperationAction(ISD::VSETCC, MVT::v2i32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000660 }
661
Evan Cheng92722532009-03-26 23:06:32 +0000662 if (!UseSoftFloat && Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000663 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
664
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000665 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
666 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
667 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
668 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000669 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
670 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000671 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
672 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
673 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000674 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000675 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000676 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000677 }
678
Evan Cheng92722532009-03-26 23:06:32 +0000679 if (!UseSoftFloat && Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000680 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
Evan Chengc7ce29b2009-02-13 22:36:38 +0000681
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000682 // FIXME: Unfortunately -soft-float and -no-implicit-float means XMM
683 // registers cannot be used even for integer operations.
Evan Cheng470a6ad2006-02-22 02:26:30 +0000684 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
685 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
686 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
687 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
688
Evan Chengf7c378e2006-04-10 07:23:14 +0000689 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
690 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
691 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000692 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Mon P Wangaf9b9522008-12-18 21:42:19 +0000693 setOperationAction(ISD::MUL, MVT::v2i64, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000694 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
695 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
696 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000697 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000698 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000699 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
700 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
701 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
702 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000703 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
704 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000705
Nate Begeman30a0de92008-07-17 16:51:19 +0000706 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
707 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
708 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
709 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000710
Evan Chengf7c378e2006-04-10 07:23:14 +0000711 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
712 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000713 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000714 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000715 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000716
Evan Cheng2c3ae372006-04-12 21:21:57 +0000717 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000718 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
719 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000720 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000721 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000722 continue;
David Greene9b9838d2009-06-29 16:47:10 +0000723 // Do not attempt to custom lower non-128-bit vectors
724 if (!VT.is128BitVector())
725 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000726 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
727 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
728 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000729 }
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000730
Evan Cheng2c3ae372006-04-12 21:21:57 +0000731 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
732 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
733 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
734 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000735 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000736 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Bill Wendlingf9abd7e2009-03-11 22:30:01 +0000737
Nate Begemancdd1eec2008-02-12 22:51:28 +0000738 if (Subtarget->is64Bit()) {
739 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000740 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000741 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000742
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000743 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
David Greene9b9838d2009-06-29 16:47:10 +0000744 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; i++) {
745 MVT VT = (MVT::SimpleValueType)i;
746
747 // Do not attempt to promote non-128-bit vectors
748 if (!VT.is128BitVector()) {
749 continue;
750 }
751 setOperationAction(ISD::AND, VT, Promote);
752 AddPromotedToType (ISD::AND, VT, MVT::v2i64);
753 setOperationAction(ISD::OR, VT, Promote);
754 AddPromotedToType (ISD::OR, VT, MVT::v2i64);
755 setOperationAction(ISD::XOR, VT, Promote);
756 AddPromotedToType (ISD::XOR, VT, MVT::v2i64);
757 setOperationAction(ISD::LOAD, VT, Promote);
758 AddPromotedToType (ISD::LOAD, VT, MVT::v2i64);
759 setOperationAction(ISD::SELECT, VT, Promote);
760 AddPromotedToType (ISD::SELECT, VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000761 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000762
Chris Lattnerddf89562008-01-17 19:59:44 +0000763 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000764
Evan Cheng2c3ae372006-04-12 21:21:57 +0000765 // Custom lower v2i64 and v2f64 selects.
766 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000767 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000768 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000769 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Scott Michelfdc40a02009-02-17 22:15:04 +0000770
Eli Friedman23ef1052009-06-06 03:57:58 +0000771 setOperationAction(ISD::FP_TO_SINT, MVT::v4i32, Legal);
772 setOperationAction(ISD::SINT_TO_FP, MVT::v4i32, Legal);
773 if (!DisableMMX && Subtarget->hasMMX()) {
774 setOperationAction(ISD::FP_TO_SINT, MVT::v2i32, Custom);
775 setOperationAction(ISD::SINT_TO_FP, MVT::v2i32, Custom);
776 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000777 }
Evan Chengc7ce29b2009-02-13 22:36:38 +0000778
Nate Begeman14d12ca2008-02-11 04:19:36 +0000779 if (Subtarget->hasSSE41()) {
780 // FIXME: Do we need to handle scalar-to-vector here?
781 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
782
783 // i8 and i16 vectors are custom , because the source register and source
784 // source memory operand types are not the same width. f32 vectors are
785 // custom since the immediate controlling the insert encodes additional
786 // information.
787 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
788 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000789 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000790 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
791
792 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
793 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
Mon P Wangf0fcdd82009-01-15 21:10:20 +0000794 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng62a3f152008-03-24 21:52:23 +0000795 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000796
797 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000798 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
799 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000800 }
801 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000802
Nate Begeman30a0de92008-07-17 16:51:19 +0000803 if (Subtarget->hasSSE42()) {
804 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
805 }
Scott Michelfdc40a02009-02-17 22:15:04 +0000806
David Greene9b9838d2009-06-29 16:47:10 +0000807 if (!UseSoftFloat && Subtarget->hasAVX()) {
David Greened94c1012009-06-29 22:50:51 +0000808 addRegisterClass(MVT::v8f32, X86::VR256RegisterClass);
809 addRegisterClass(MVT::v4f64, X86::VR256RegisterClass);
810 addRegisterClass(MVT::v8i32, X86::VR256RegisterClass);
811 addRegisterClass(MVT::v4i64, X86::VR256RegisterClass);
812
David Greene9b9838d2009-06-29 16:47:10 +0000813 setOperationAction(ISD::LOAD, MVT::v8f32, Legal);
814 setOperationAction(ISD::LOAD, MVT::v8i32, Legal);
815 setOperationAction(ISD::LOAD, MVT::v4f64, Legal);
816 setOperationAction(ISD::LOAD, MVT::v4i64, Legal);
817 setOperationAction(ISD::FADD, MVT::v8f32, Legal);
818 setOperationAction(ISD::FSUB, MVT::v8f32, Legal);
819 setOperationAction(ISD::FMUL, MVT::v8f32, Legal);
820 setOperationAction(ISD::FDIV, MVT::v8f32, Legal);
821 setOperationAction(ISD::FSQRT, MVT::v8f32, Legal);
822 setOperationAction(ISD::FNEG, MVT::v8f32, Custom);
823 //setOperationAction(ISD::BUILD_VECTOR, MVT::v8f32, Custom);
824 //setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8f32, Custom);
825 //setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8f32, Custom);
826 //setOperationAction(ISD::SELECT, MVT::v8f32, Custom);
827 //setOperationAction(ISD::VSETCC, MVT::v8f32, Custom);
828
829 // Operations to consider commented out -v16i16 v32i8
830 //setOperationAction(ISD::ADD, MVT::v16i16, Legal);
831 setOperationAction(ISD::ADD, MVT::v8i32, Custom);
832 setOperationAction(ISD::ADD, MVT::v4i64, Custom);
833 //setOperationAction(ISD::SUB, MVT::v32i8, Legal);
834 //setOperationAction(ISD::SUB, MVT::v16i16, Legal);
835 setOperationAction(ISD::SUB, MVT::v8i32, Custom);
836 setOperationAction(ISD::SUB, MVT::v4i64, Custom);
837 //setOperationAction(ISD::MUL, MVT::v16i16, Legal);
838 setOperationAction(ISD::FADD, MVT::v4f64, Legal);
839 setOperationAction(ISD::FSUB, MVT::v4f64, Legal);
840 setOperationAction(ISD::FMUL, MVT::v4f64, Legal);
841 setOperationAction(ISD::FDIV, MVT::v4f64, Legal);
842 setOperationAction(ISD::FSQRT, MVT::v4f64, Legal);
843 setOperationAction(ISD::FNEG, MVT::v4f64, Custom);
844
845 setOperationAction(ISD::VSETCC, MVT::v4f64, Custom);
846 // setOperationAction(ISD::VSETCC, MVT::v32i8, Custom);
847 // setOperationAction(ISD::VSETCC, MVT::v16i16, Custom);
848 setOperationAction(ISD::VSETCC, MVT::v8i32, Custom);
849
850 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v32i8, Custom);
851 // setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i16, Custom);
852 // setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i16, Custom);
853 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i32, Custom);
854 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8f32, Custom);
855
856 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f64, Custom);
857 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i64, Custom);
858 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f64, Custom);
859 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i64, Custom);
860 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f64, Custom);
861 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f64, Custom);
862
863#if 0
864 // Not sure we want to do this since there are no 256-bit integer
865 // operations in AVX
866
867 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
868 // This includes 256-bit vectors
869 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; ++i) {
870 MVT VT = (MVT::SimpleValueType)i;
871
872 // Do not attempt to custom lower non-power-of-2 vectors
873 if (!isPowerOf2_32(VT.getVectorNumElements()))
874 continue;
875
876 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
877 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
878 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
879 }
880
881 if (Subtarget->is64Bit()) {
882 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i64, Custom);
883 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i64, Custom);
884 }
885#endif
886
887#if 0
888 // Not sure we want to do this since there are no 256-bit integer
889 // operations in AVX
890
891 // Promote v32i8, v16i16, v8i32 load, select, and, or, xor to v4i64.
892 // Including 256-bit vectors
893 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v4i64; i++) {
894 MVT VT = (MVT::SimpleValueType)i;
895
896 if (!VT.is256BitVector()) {
897 continue;
898 }
899 setOperationAction(ISD::AND, VT, Promote);
900 AddPromotedToType (ISD::AND, VT, MVT::v4i64);
901 setOperationAction(ISD::OR, VT, Promote);
902 AddPromotedToType (ISD::OR, VT, MVT::v4i64);
903 setOperationAction(ISD::XOR, VT, Promote);
904 AddPromotedToType (ISD::XOR, VT, MVT::v4i64);
905 setOperationAction(ISD::LOAD, VT, Promote);
906 AddPromotedToType (ISD::LOAD, VT, MVT::v4i64);
907 setOperationAction(ISD::SELECT, VT, Promote);
908 AddPromotedToType (ISD::SELECT, VT, MVT::v4i64);
909 }
910
911 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
912#endif
913 }
914
Evan Cheng6be2c582006-04-05 23:38:46 +0000915 // We want to custom lower some of our intrinsics.
916 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
917
Bill Wendling74c37652008-12-09 22:08:41 +0000918 // Add/Sub/Mul with overflow operations are custom lowered.
Bill Wendling41ea7e72008-11-24 19:21:46 +0000919 setOperationAction(ISD::SADDO, MVT::i32, Custom);
920 setOperationAction(ISD::SADDO, MVT::i64, Custom);
921 setOperationAction(ISD::UADDO, MVT::i32, Custom);
922 setOperationAction(ISD::UADDO, MVT::i64, Custom);
Bill Wendling74c37652008-12-09 22:08:41 +0000923 setOperationAction(ISD::SSUBO, MVT::i32, Custom);
924 setOperationAction(ISD::SSUBO, MVT::i64, Custom);
925 setOperationAction(ISD::USUBO, MVT::i32, Custom);
926 setOperationAction(ISD::USUBO, MVT::i64, Custom);
927 setOperationAction(ISD::SMULO, MVT::i32, Custom);
928 setOperationAction(ISD::SMULO, MVT::i64, Custom);
Bill Wendling41ea7e72008-11-24 19:21:46 +0000929
Evan Chengd54f2d52009-03-31 19:38:51 +0000930 if (!Subtarget->is64Bit()) {
931 // These libcalls are not available in 32-bit.
932 setLibcallName(RTLIB::SHL_I128, 0);
933 setLibcallName(RTLIB::SRL_I128, 0);
934 setLibcallName(RTLIB::SRA_I128, 0);
935 }
936
Evan Cheng206ee9d2006-07-07 08:33:52 +0000937 // We have target-specific dag combine patterns for the following nodes:
938 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000939 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000940 setTargetDAGCombine(ISD::SELECT);
Nate Begeman740ab032009-01-26 00:52:55 +0000941 setTargetDAGCombine(ISD::SHL);
942 setTargetDAGCombine(ISD::SRA);
943 setTargetDAGCombine(ISD::SRL);
Chris Lattner149a4e52008-02-22 02:09:43 +0000944 setTargetDAGCombine(ISD::STORE);
Owen Anderson99177002009-06-29 18:04:45 +0000945 setTargetDAGCombine(ISD::MEMBARRIER);
Evan Cheng0b0cd912009-03-28 05:57:29 +0000946 if (Subtarget->is64Bit())
947 setTargetDAGCombine(ISD::MUL);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000948
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000949 computeRegisterProperties();
950
Evan Cheng87ed7162006-02-14 08:25:08 +0000951 // FIXME: These should be based on subtarget info. Plus, the values should
952 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000953 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
954 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
955 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000956 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000957 setPrefLoopAlignment(16);
Evan Cheng6ebf7bc2009-05-13 21:42:09 +0000958 benefitFromCodePlacementOpt = true;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000959}
960
Scott Michel5b8f82e2008-03-10 15:42:14 +0000961
Duncan Sands5480c042009-01-01 15:52:00 +0000962MVT X86TargetLowering::getSetCCResultType(MVT VT) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000963 return MVT::i8;
964}
965
966
Evan Cheng29286502008-01-23 23:17:41 +0000967/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
968/// the desired ByVal argument alignment.
969static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
970 if (MaxAlign == 16)
971 return;
972 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
973 if (VTy->getBitWidth() == 128)
974 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000975 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
976 unsigned EltAlign = 0;
977 getMaxByValAlign(ATy->getElementType(), EltAlign);
978 if (EltAlign > MaxAlign)
979 MaxAlign = EltAlign;
980 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
981 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
982 unsigned EltAlign = 0;
983 getMaxByValAlign(STy->getElementType(i), EltAlign);
984 if (EltAlign > MaxAlign)
985 MaxAlign = EltAlign;
986 if (MaxAlign == 16)
987 break;
988 }
989 }
990 return;
991}
992
993/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
994/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000995/// that contain SSE vectors are placed at 16-byte boundaries while the rest
996/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000997unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000998 if (Subtarget->is64Bit()) {
999 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001000 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +00001001 if (TyAlign > 8)
1002 return TyAlign;
1003 return 8;
1004 }
1005
Evan Cheng29286502008-01-23 23:17:41 +00001006 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +00001007 if (Subtarget->hasSSE1())
1008 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +00001009 return Align;
1010}
Chris Lattner2b02a442007-02-25 08:29:00 +00001011
Evan Chengf0df0312008-05-15 08:39:06 +00001012/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +00001013/// and store operations as a result of memset, memcpy, and memmove
1014/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +00001015/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001016MVT
Evan Chengf0df0312008-05-15 08:39:06 +00001017X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
Devang Patel578efa92009-06-05 21:57:13 +00001018 bool isSrcConst, bool isSrcStr,
1019 SelectionDAG &DAG) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001020 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
1021 // linux. This is because the stack realignment code can't handle certain
1022 // cases like PR2962. This should be removed when PR2962 is fixed.
Devang Patel578efa92009-06-05 21:57:13 +00001023 const Function *F = DAG.getMachineFunction().getFunction();
1024 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
1025 if (!NoImplicitFloatOps && Subtarget->getStackAlignment() >= 16) {
Chris Lattner4002a1b2008-10-28 05:49:35 +00001026 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
1027 return MVT::v4i32;
1028 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
1029 return MVT::v4f32;
1030 }
Evan Chengf0df0312008-05-15 08:39:06 +00001031 if (Subtarget->is64Bit() && Size >= 8)
1032 return MVT::i64;
1033 return MVT::i32;
1034}
1035
Evan Chengcc415862007-11-09 01:32:10 +00001036/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
1037/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +00001038SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +00001039 SelectionDAG &DAG) const {
1040 if (usesGlobalOffsetTable())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001041 return DAG.getGLOBAL_OFFSET_TABLE(getPointerTy());
Chris Lattnere4df7562009-07-09 03:15:51 +00001042 if (!Subtarget->is64Bit())
Dale Johannesenb300d2a2009-02-07 00:55:49 +00001043 // This doesn't have DebugLoc associated with it, but is not really the
1044 // same as a Register.
1045 return DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc::getUnknownLoc(),
1046 getPointerTy());
Evan Chengcc415862007-11-09 01:32:10 +00001047 return Table;
1048}
1049
Bill Wendlingb4202b82009-07-01 18:50:55 +00001050/// getFunctionAlignment - Return the Log2 alignment of this function.
Bill Wendling20c568f2009-06-30 22:38:32 +00001051unsigned X86TargetLowering::getFunctionAlignment(const Function *F) const {
1052 return F->hasFnAttr(Attribute::OptimizeForSize) ? 1 : 4;
1053}
1054
Chris Lattner2b02a442007-02-25 08:29:00 +00001055//===----------------------------------------------------------------------===//
1056// Return Value Calling Convention Implementation
1057//===----------------------------------------------------------------------===//
1058
Chris Lattner59ed56b2007-02-28 04:55:35 +00001059#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001060
Dan Gohman98ca4f22009-08-05 01:29:28 +00001061SDValue
1062X86TargetLowering::LowerReturn(SDValue Chain,
1063 unsigned CallConv, bool isVarArg,
1064 const SmallVectorImpl<ISD::OutputArg> &Outs,
1065 DebugLoc dl, SelectionDAG &DAG) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001066
Chris Lattner9774c912007-02-27 05:28:59 +00001067 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001068 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1069 RVLocs, *DAG.getContext());
1070 CCInfo.AnalyzeReturn(Outs, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001071
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001072 // If this is the first return lowered for this function, add the regs to the
1073 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +00001074 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +00001075 for (unsigned i = 0; i != RVLocs.size(); ++i)
1076 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +00001077 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001079
Dan Gohman475871a2008-07-27 21:46:04 +00001080 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001081
Dan Gohman475871a2008-07-27 21:46:04 +00001082 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +00001083 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
1084 // Operand #1 = Bytes To Pop
1085 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
Scott Michelfdc40a02009-02-17 22:15:04 +00001086
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001087 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +00001088 for (unsigned i = 0; i != RVLocs.size(); ++i) {
1089 CCValAssign &VA = RVLocs[i];
1090 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman98ca4f22009-08-05 01:29:28 +00001091 SDValue ValToCopy = Outs[i].Val;
Scott Michelfdc40a02009-02-17 22:15:04 +00001092
Chris Lattner447ff682008-03-11 03:23:40 +00001093 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
1094 // the RET instruction and handled by the FP Stackifier.
Dan Gohman37eed792009-02-04 17:28:58 +00001095 if (VA.getLocReg() == X86::ST0 ||
1096 VA.getLocReg() == X86::ST1) {
Chris Lattner447ff682008-03-11 03:23:40 +00001097 // If this is a copy from an xmm register to ST(0), use an FPExtend to
1098 // change the value to the FP stack register class.
Dan Gohman37eed792009-02-04 17:28:58 +00001099 if (isScalarFPTypeInSSEReg(VA.getValVT()))
Dale Johannesenace16102009-02-03 19:33:06 +00001100 ValToCopy = DAG.getNode(ISD::FP_EXTEND, dl, MVT::f80, ValToCopy);
Chris Lattner447ff682008-03-11 03:23:40 +00001101 RetOps.push_back(ValToCopy);
1102 // Don't emit a copytoreg.
1103 continue;
1104 }
Dale Johannesena68f9012008-06-24 22:01:44 +00001105
Evan Cheng242b38b2009-02-23 09:03:22 +00001106 // 64-bit vector (MMX) values are returned in XMM0 / XMM1 except for v1i64
1107 // which is returned in RAX / RDX.
Evan Cheng6140a8b2009-02-22 08:05:12 +00001108 if (Subtarget->is64Bit()) {
1109 MVT ValVT = ValToCopy.getValueType();
Evan Cheng242b38b2009-02-23 09:03:22 +00001110 if (ValVT.isVector() && ValVT.getSizeInBits() == 64) {
Evan Cheng6140a8b2009-02-22 08:05:12 +00001111 ValToCopy = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, ValToCopy);
Evan Cheng242b38b2009-02-23 09:03:22 +00001112 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1)
1113 ValToCopy = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, ValToCopy);
1114 }
Evan Cheng6140a8b2009-02-22 08:05:12 +00001115 }
1116
Dale Johannesendd64c412009-02-04 00:33:20 +00001117 Chain = DAG.getCopyToReg(Chain, dl, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001118 Flag = Chain.getValue(1);
1119 }
Dan Gohman61a92132008-04-21 23:59:07 +00001120
1121 // The x86-64 ABI for returning structs by value requires that we copy
1122 // the sret argument into %rax for the return. We saved the argument into
1123 // a virtual register in the entry block, so now we copy the value out
1124 // and into %rax.
1125 if (Subtarget->is64Bit() &&
1126 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1127 MachineFunction &MF = DAG.getMachineFunction();
1128 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1129 unsigned Reg = FuncInfo->getSRetReturnReg();
1130 if (!Reg) {
1131 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1132 FuncInfo->setSRetReturnReg(Reg);
1133 }
Dale Johannesendd64c412009-02-04 00:33:20 +00001134 SDValue Val = DAG.getCopyFromReg(Chain, dl, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +00001135
Dale Johannesendd64c412009-02-04 00:33:20 +00001136 Chain = DAG.getCopyToReg(Chain, dl, X86::RAX, Val, Flag);
Dan Gohman61a92132008-04-21 23:59:07 +00001137 Flag = Chain.getValue(1);
1138 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001139
Chris Lattner447ff682008-03-11 03:23:40 +00001140 RetOps[0] = Chain; // Update chain.
1141
1142 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +00001143 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +00001144 RetOps.push_back(Flag);
Scott Michelfdc40a02009-02-17 22:15:04 +00001145
1146 return DAG.getNode(X86ISD::RET_FLAG, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00001147 MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001148}
1149
Dan Gohman98ca4f22009-08-05 01:29:28 +00001150/// LowerCallResult - Lower the result values of a call into the
1151/// appropriate copies out of appropriate physical registers.
1152///
1153SDValue
1154X86TargetLowering::LowerCallResult(SDValue Chain, SDValue InFlag,
1155 unsigned CallConv, bool isVarArg,
1156 const SmallVectorImpl<ISD::InputArg> &Ins,
1157 DebugLoc dl, SelectionDAG &DAG,
1158 SmallVectorImpl<SDValue> &InVals) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +00001159
Chris Lattnere32bbf62007-02-28 07:09:55 +00001160 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001161 SmallVector<CCValAssign, 16> RVLocs;
Torok Edwin3f142c32009-02-01 18:15:56 +00001162 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001163 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
Owen Andersone922c022009-07-22 00:24:57 +00001164 RVLocs, *DAG.getContext());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001165 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
Scott Michelfdc40a02009-02-17 22:15:04 +00001166
Chris Lattner3085e152007-02-25 08:59:22 +00001167 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001168 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Dan Gohman37eed792009-02-04 17:28:58 +00001169 CCValAssign &VA = RVLocs[i];
1170 MVT CopyVT = VA.getValVT();
Scott Michelfdc40a02009-02-17 22:15:04 +00001171
Torok Edwin3f142c32009-02-01 18:15:56 +00001172 // If this is x86-64, and we disabled SSE, we can't return FP values
Scott Michelfdc40a02009-02-17 22:15:04 +00001173 if ((CopyVT == MVT::f32 || CopyVT == MVT::f64) &&
Dan Gohman98ca4f22009-08-05 01:29:28 +00001174 ((Is64Bit || Ins[i].Flags.isInReg()) && !Subtarget->hasSSE1())) {
Torok Edwin804e0fe2009-07-08 19:04:27 +00001175 llvm_report_error("SSE register return with SSE disabled");
Torok Edwin3f142c32009-02-01 18:15:56 +00001176 }
1177
Chris Lattner8e6da152008-03-10 21:08:41 +00001178 // If this is a call to a function that returns an fp value on the floating
1179 // point stack, but where we prefer to use the value in xmm registers, copy
1180 // 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 +00001181 if ((VA.getLocReg() == X86::ST0 ||
1182 VA.getLocReg() == X86::ST1) &&
1183 isScalarFPTypeInSSEReg(VA.getValVT())) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001184 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001185 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001186
Evan Cheng79fb3b42009-02-20 20:43:02 +00001187 SDValue Val;
1188 if (Is64Bit && CopyVT.isVector() && CopyVT.getSizeInBits() == 64) {
Evan Cheng242b38b2009-02-23 09:03:22 +00001189 // For x86-64, MMX values are returned in XMM0 / XMM1 except for v1i64.
1190 if (VA.getLocReg() == X86::XMM0 || VA.getLocReg() == X86::XMM1) {
1191 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1192 MVT::v2i64, InFlag).getValue(1);
1193 Val = Chain.getValue(0);
1194 Val = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001195 Val, DAG.getConstant(0, MVT::i64));
Evan Cheng242b38b2009-02-23 09:03:22 +00001196 } else {
1197 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1198 MVT::i64, InFlag).getValue(1);
1199 Val = Chain.getValue(0);
1200 }
Evan Cheng79fb3b42009-02-20 20:43:02 +00001201 Val = DAG.getNode(ISD::BIT_CONVERT, dl, CopyVT, Val);
1202 } else {
1203 Chain = DAG.getCopyFromReg(Chain, dl, VA.getLocReg(),
1204 CopyVT, InFlag).getValue(1);
1205 Val = Chain.getValue(0);
1206 }
Chris Lattner8e6da152008-03-10 21:08:41 +00001207 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001208
Dan Gohman37eed792009-02-04 17:28:58 +00001209 if (CopyVT != VA.getValVT()) {
Chris Lattner8e6da152008-03-10 21:08:41 +00001210 // Round the F80 the right size, which also moves to the appropriate xmm
1211 // register.
Dan Gohman37eed792009-02-04 17:28:58 +00001212 Val = DAG.getNode(ISD::FP_ROUND, dl, VA.getValVT(), Val,
Chris Lattner8e6da152008-03-10 21:08:41 +00001213 // This truncation won't change the value.
1214 DAG.getIntPtrConstant(1));
1215 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001216
Dan Gohman98ca4f22009-08-05 01:29:28 +00001217 InVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001218 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001219
Dan Gohman98ca4f22009-08-05 01:29:28 +00001220 return Chain;
Chris Lattner2b02a442007-02-25 08:29:00 +00001221}
1222
1223
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001224//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001225// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001226//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001227// StdCall calling convention seems to be standard for many Windows' API
1228// routines and around. It differs from C calling convention just a little:
1229// callee should clean up the stack, not caller. Symbols should be also
1230// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001231// For info on fast calling convention see Fast Calling Convention (tail call)
1232// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001233
Dan Gohman98ca4f22009-08-05 01:29:28 +00001234/// CallIsStructReturn - Determines whether a call uses struct return
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001235/// semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001236static bool CallIsStructReturn(const SmallVectorImpl<ISD::OutputArg> &Outs) {
1237 if (Outs.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001238 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001239
Dan Gohman98ca4f22009-08-05 01:29:28 +00001240 return Outs[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001241}
1242
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001243/// ArgsAreStructReturn - Determines whether a function uses struct
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001244/// return semantics.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001245static bool
1246ArgsAreStructReturn(const SmallVectorImpl<ISD::InputArg> &Ins) {
1247 if (Ins.empty())
Gordon Henriksen86737662008-01-05 16:56:59 +00001248 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001249
Dan Gohman98ca4f22009-08-05 01:29:28 +00001250 return Ins[0].Flags.isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001251}
1252
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001253/// IsCalleePop - Determines whether the callee is required to pop its
1254/// own arguments. Callee pop is necessary to support tail calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001255bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001256 if (IsVarArg)
1257 return false;
1258
Dan Gohman095cc292008-09-13 01:54:27 +00001259 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001260 default:
1261 return false;
1262 case CallingConv::X86_StdCall:
1263 return !Subtarget->is64Bit();
1264 case CallingConv::X86_FastCall:
1265 return !Subtarget->is64Bit();
1266 case CallingConv::Fast:
1267 return PerformTailCallOpt;
1268 }
1269}
1270
Dan Gohman095cc292008-09-13 01:54:27 +00001271/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1272/// given CallingConvention value.
1273CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001274 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001275 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001276 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001277 else
1278 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001279 }
1280
Gordon Henriksen86737662008-01-05 16:56:59 +00001281 if (CC == CallingConv::X86_FastCall)
1282 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001283 else if (CC == CallingConv::Fast)
1284 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001285 else
1286 return CC_X86_32_C;
1287}
1288
Dan Gohman98ca4f22009-08-05 01:29:28 +00001289/// NameDecorationForCallConv - Selects the appropriate decoration to
1290/// apply to a MachineFunction containing a given calling convention.
Gordon Henriksen86737662008-01-05 16:56:59 +00001291NameDecorationStyle
Dan Gohman98ca4f22009-08-05 01:29:28 +00001292X86TargetLowering::NameDecorationForCallConv(unsigned CallConv) {
1293 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001294 return FastCall;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001295 else if (CallConv == CallingConv::X86_StdCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001296 return StdCall;
1297 return None;
1298}
1299
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001300
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001301/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1302/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001303/// the specific parameter attribute. The copy will be passed as a byval
1304/// function parameter.
Scott Michelfdc40a02009-02-17 22:15:04 +00001305static SDValue
Dan Gohman475871a2008-07-27 21:46:04 +00001306CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001307 ISD::ArgFlagsTy Flags, SelectionDAG &DAG,
1308 DebugLoc dl) {
Dan Gohman475871a2008-07-27 21:46:04 +00001309 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dale Johannesendd64c412009-02-04 00:33:20 +00001310 return DAG.getMemcpy(Chain, dl, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001311 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001312}
1313
Dan Gohman98ca4f22009-08-05 01:29:28 +00001314SDValue
1315X86TargetLowering::LowerMemArgument(SDValue Chain,
1316 unsigned CallConv,
1317 const SmallVectorImpl<ISD::InputArg> &Ins,
1318 DebugLoc dl, SelectionDAG &DAG,
1319 const CCValAssign &VA,
1320 MachineFrameInfo *MFI,
1321 unsigned i) {
1322
Rafael Espindola7effac52007-09-14 15:48:13 +00001323 // Create the nodes corresponding to a load from this parameter slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001324 ISD::ArgFlagsTy Flags = Ins[i].Flags;
1325 bool AlwaysUseMutable = (CallConv==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001326 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001327
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001328 // FIXME: For now, all byval parameter objects are marked mutable. This can be
Scott Michelfdc40a02009-02-17 22:15:04 +00001329 // changed with more analysis.
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001330 // In case of tail call optimization mark all arguments mutable. Since they
1331 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001332 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001333 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001334 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001335 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001336 return FIN;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001337 return DAG.getLoad(VA.getValVT(), dl, Chain, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001338 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001339}
1340
Dan Gohman475871a2008-07-27 21:46:04 +00001341SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001342X86TargetLowering::LowerFormalArguments(SDValue Chain,
1343 unsigned CallConv,
1344 bool isVarArg,
1345 const SmallVectorImpl<ISD::InputArg> &Ins,
1346 DebugLoc dl,
1347 SelectionDAG &DAG,
1348 SmallVectorImpl<SDValue> &InVals) {
1349
Evan Cheng1bc78042006-04-26 01:20:17 +00001350 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001351 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
Scott Michelfdc40a02009-02-17 22:15:04 +00001352
Gordon Henriksen86737662008-01-05 16:56:59 +00001353 const Function* Fn = MF.getFunction();
1354 if (Fn->hasExternalLinkage() &&
1355 Subtarget->isTargetCygMing() &&
1356 Fn->getName() == "main")
1357 FuncInfo->setForceFramePointer(true);
1358
1359 // Decorate the function name.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001360 FuncInfo->setDecorationStyle(NameDecorationForCallConv(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001361
Evan Cheng1bc78042006-04-26 01:20:17 +00001362 MachineFrameInfo *MFI = MF.getFrameInfo();
Gordon Henriksen86737662008-01-05 16:56:59 +00001363 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001364 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001365
Dan Gohman98ca4f22009-08-05 01:29:28 +00001366 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001367 "Var args not supported with calling convention fastcc");
1368
Chris Lattner638402b2007-02-28 07:00:42 +00001369 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001370 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001371 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1372 ArgLocs, *DAG.getContext());
1373 CCInfo.AnalyzeFormalArguments(Ins, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001374
Chris Lattnerf39f7712007-02-28 05:46:49 +00001375 unsigned LastVal = ~0U;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001376 SDValue ArgValue;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001377 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1378 CCValAssign &VA = ArgLocs[i];
1379 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1380 // places.
1381 assert(VA.getValNo() != LastVal &&
1382 "Don't support value assigned to multiple locs yet");
1383 LastVal = VA.getValNo();
Scott Michelfdc40a02009-02-17 22:15:04 +00001384
Chris Lattnerf39f7712007-02-28 05:46:49 +00001385 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001386 MVT RegVT = VA.getLocVT();
Devang Patel8a84e442009-01-05 17:31:22 +00001387 TargetRegisterClass *RC = NULL;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001388 if (RegVT == MVT::i32)
1389 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001390 else if (Is64Bit && RegVT == MVT::i64)
1391 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001392 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001393 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001394 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001395 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001396 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001397 RC = X86::VR128RegisterClass;
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001398 else if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
1399 RC = X86::VR64RegisterClass;
1400 else
Torok Edwinc23197a2009-07-14 16:55:14 +00001401 llvm_unreachable("Unknown argument type!");
Gordon Henriksenae636f82008-01-03 16:47:34 +00001402
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001403 unsigned Reg = MF.addLiveIn(VA.getLocReg(), RC);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001404 ArgValue = DAG.getCopyFromReg(Chain, dl, Reg, RegVT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001405
Chris Lattnerf39f7712007-02-28 05:46:49 +00001406 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1407 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1408 // right size.
1409 if (VA.getLocInfo() == CCValAssign::SExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001410 ArgValue = DAG.getNode(ISD::AssertSext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001411 DAG.getValueType(VA.getValVT()));
1412 else if (VA.getLocInfo() == CCValAssign::ZExt)
Dale Johannesenace16102009-02-03 19:33:06 +00001413 ArgValue = DAG.getNode(ISD::AssertZext, dl, RegVT, ArgValue,
Chris Lattnerf39f7712007-02-28 05:46:49 +00001414 DAG.getValueType(VA.getValVT()));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001415 else if (VA.getLocInfo() == CCValAssign::BCvt)
Anton Korobeynikov6dde14b2009-08-03 08:14:14 +00001416 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
Scott Michelfdc40a02009-02-17 22:15:04 +00001417
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001418 if (VA.isExtInLoc()) {
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001419 // Handle MMX values passed in XMM regs.
1420 if (RegVT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00001421 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i64,
1422 ArgValue, DAG.getConstant(0, MVT::i64));
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001423 ArgValue = DAG.getNode(ISD::BIT_CONVERT, dl, VA.getValVT(), ArgValue);
1424 } else
1425 ArgValue = DAG.getNode(ISD::TRUNCATE, dl, VA.getValVT(), ArgValue);
Evan Cheng44c0fd12008-04-25 20:13:28 +00001426 }
Chris Lattnerf39f7712007-02-28 05:46:49 +00001427 } else {
1428 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001429 ArgValue = LowerMemArgument(Chain, CallConv, Ins, dl, DAG, VA, MFI, i);
Evan Cheng1bc78042006-04-26 01:20:17 +00001430 }
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001431
1432 // If value is passed via pointer - do a load.
1433 if (VA.getLocInfo() == CCValAssign::Indirect)
Dan Gohman98ca4f22009-08-05 01:29:28 +00001434 ArgValue = DAG.getLoad(VA.getValVT(), dl, Chain, ArgValue, NULL, 0);
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001435
Dan Gohman98ca4f22009-08-05 01:29:28 +00001436 InVals.push_back(ArgValue);
Evan Cheng1bc78042006-04-26 01:20:17 +00001437 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001438
Dan Gohman61a92132008-04-21 23:59:07 +00001439 // The x86-64 ABI for returning structs by value requires that we copy
1440 // the sret argument into %rax for the return. Save the argument into
1441 // a virtual register so that we can access it from the return points.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001442 if (Is64Bit && MF.getFunction()->hasStructRetAttr()) {
Dan Gohman61a92132008-04-21 23:59:07 +00001443 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1444 unsigned Reg = FuncInfo->getSRetReturnReg();
1445 if (!Reg) {
1446 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1447 FuncInfo->setSRetReturnReg(Reg);
1448 }
Dan Gohman98ca4f22009-08-05 01:29:28 +00001449 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), dl, Reg, InVals[0]);
1450 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other, Copy, Chain);
Dan Gohman61a92132008-04-21 23:59:07 +00001451 }
1452
Chris Lattnerf39f7712007-02-28 05:46:49 +00001453 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001454 // align stack specially for tail calls
Dan Gohman98ca4f22009-08-05 01:29:28 +00001455 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001456 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001457
Evan Cheng1bc78042006-04-26 01:20:17 +00001458 // If the function takes variable number of arguments, make a frame index for
1459 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001460 if (isVarArg) {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001461 if (Is64Bit || CallConv != CallingConv::X86_FastCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001462 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1463 }
1464 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001465 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1466
1467 // FIXME: We should really autogenerate these arrays
1468 static const unsigned GPR64ArgRegsWin64[] = {
1469 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001470 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001471 static const unsigned XMMArgRegsWin64[] = {
1472 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1473 };
1474 static const unsigned GPR64ArgRegs64Bit[] = {
1475 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1476 };
1477 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001478 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1479 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1480 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001481 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1482
1483 if (IsWin64) {
1484 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1485 GPR64ArgRegs = GPR64ArgRegsWin64;
1486 XMMArgRegs = XMMArgRegsWin64;
1487 } else {
1488 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1489 GPR64ArgRegs = GPR64ArgRegs64Bit;
1490 XMMArgRegs = XMMArgRegs64Bit;
1491 }
1492 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1493 TotalNumIntRegs);
1494 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1495 TotalNumXMMRegs);
1496
Devang Patel578efa92009-06-05 21:57:13 +00001497 bool NoImplicitFloatOps = Fn->hasFnAttr(Attribute::NoImplicitFloat);
Evan Chengc7ce29b2009-02-13 22:36:38 +00001498 assert(!(NumXMMRegs && !Subtarget->hasSSE1()) &&
Torok Edwin3f142c32009-02-01 18:15:56 +00001499 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001500 assert(!(NumXMMRegs && UseSoftFloat && NoImplicitFloatOps) &&
Evan Chengc7ce29b2009-02-13 22:36:38 +00001501 "SSE register cannot be used when SSE is disabled!");
Devang Patel578efa92009-06-05 21:57:13 +00001502 if (UseSoftFloat || NoImplicitFloatOps || !Subtarget->hasSSE1())
Torok Edwin3f142c32009-02-01 18:15:56 +00001503 // Kernel mode asks for SSE to be disabled, so don't push them
1504 // on the stack.
1505 TotalNumXMMRegs = 0;
Bill Wendlingf9abd7e2009-03-11 22:30:01 +00001506
Gordon Henriksen86737662008-01-05 16:56:59 +00001507 // For X86-64, if there are vararg parameters that are passed via
1508 // registers, then we must store them to their spots on the stack so they
1509 // may be loaded by deferencing the result of va_next.
1510 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001511 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1512 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1513 TotalNumXMMRegs * 16, 16);
1514
Gordon Henriksen86737662008-01-05 16:56:59 +00001515 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001516 SmallVector<SDValue, 8> MemOps;
1517 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001518 SDValue FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001519 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001520 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001521 unsigned VReg = MF.addLiveIn(GPR64ArgRegs[NumIntRegs],
1522 X86::GR64RegisterClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001523 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::i64);
Dan Gohman475871a2008-07-27 21:46:04 +00001524 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001525 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001526 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001527 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001528 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001529 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001530 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001531
Gordon Henriksen86737662008-01-05 16:56:59 +00001532 // Now store the XMM (fp + vector) parameter registers.
Dale Johannesenace16102009-02-03 19:33:06 +00001533 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001534 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001535 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Bob Wilson998e1252009-04-20 18:36:57 +00001536 unsigned VReg = MF.addLiveIn(XMMArgRegs[NumXMMRegs],
1537 X86::VR128RegisterClass);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001538 SDValue Val = DAG.getCopyFromReg(Chain, dl, VReg, MVT::v4f32);
Dan Gohman475871a2008-07-27 21:46:04 +00001539 SDValue Store =
Dale Johannesenace16102009-02-03 19:33:06 +00001540 DAG.getStore(Val.getValue(1), dl, Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001541 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001542 MemOps.push_back(Store);
Dale Johannesenace16102009-02-03 19:33:06 +00001543 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001544 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001545 }
1546 if (!MemOps.empty())
Dan Gohman98ca4f22009-08-05 01:29:28 +00001547 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Gordon Henriksen86737662008-01-05 16:56:59 +00001548 &MemOps[0], MemOps.size());
1549 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001550 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001551
Gordon Henriksen86737662008-01-05 16:56:59 +00001552 // Some CCs need callee pop.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001553 if (IsCalleePop(isVarArg, CallConv)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001554 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001555 BytesCallerReserves = 0;
1556 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001557 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001558 // If this is an sret function, the return should pop the hidden pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001559 if (!Is64Bit && CallConv != CallingConv::Fast && ArgsAreStructReturn(Ins))
Scott Michelfdc40a02009-02-17 22:15:04 +00001560 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001561 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001562 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001563
Gordon Henriksen86737662008-01-05 16:56:59 +00001564 if (!Is64Bit) {
1565 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001566 if (CallConv == CallingConv::X86_FastCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001567 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1568 }
Evan Cheng25caf632006-05-23 21:06:34 +00001569
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001570 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001571
Dan Gohman98ca4f22009-08-05 01:29:28 +00001572 return Chain;
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001573}
1574
Dan Gohman475871a2008-07-27 21:46:04 +00001575SDValue
Dan Gohman98ca4f22009-08-05 01:29:28 +00001576X86TargetLowering::LowerMemOpCallTo(SDValue Chain,
1577 SDValue StackPtr, SDValue Arg,
1578 DebugLoc dl, SelectionDAG &DAG,
Evan Chengdffbd832008-01-10 00:09:10 +00001579 const CCValAssign &VA,
Dan Gohman98ca4f22009-08-05 01:29:28 +00001580 ISD::ArgFlagsTy Flags) {
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001581 const unsigned FirstStackArgOffset = (Subtarget->isTargetWin64() ? 32 : 0);
Anton Korobeynikovcf6b7392009-08-03 08:12:53 +00001582 unsigned LocMemOffset = FirstStackArgOffset + VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001583 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Dale Johannesenace16102009-02-03 19:33:06 +00001584 PtrOff = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001585 if (Flags.isByVal()) {
Dale Johannesendd64c412009-02-04 00:33:20 +00001586 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG, dl);
Evan Chengdffbd832008-01-10 00:09:10 +00001587 }
Dale Johannesenace16102009-02-03 19:33:06 +00001588 return DAG.getStore(Chain, dl, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001589 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001590}
1591
Bill Wendling64e87322009-01-16 19:25:27 +00001592/// EmitTailCallLoadRetAddr - Emit a load of return address if tail call
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001593/// optimization is performed and it is required.
Scott Michelfdc40a02009-02-17 22:15:04 +00001594SDValue
1595X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001596 SDValue &OutRetAddr,
Scott Michelfdc40a02009-02-17 22:15:04 +00001597 SDValue Chain,
1598 bool IsTailCall,
1599 bool Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001600 int FPDiff,
1601 DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001602 if (!IsTailCall || FPDiff==0) return Chain;
1603
1604 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001605 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001606 OutRetAddr = getReturnAddressFrameIndex(DAG);
Bill Wendling64e87322009-01-16 19:25:27 +00001607
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001608 // Load the "old" Return address.
Dale Johannesenace16102009-02-03 19:33:06 +00001609 OutRetAddr = DAG.getLoad(VT, dl, Chain, OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001610 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001611}
1612
1613/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1614/// optimization is performed and it is required (FPDiff!=0).
Scott Michelfdc40a02009-02-17 22:15:04 +00001615static SDValue
1616EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001617 SDValue Chain, SDValue RetAddrFrIdx,
Dale Johannesenace16102009-02-03 19:33:06 +00001618 bool Is64Bit, int FPDiff, DebugLoc dl) {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001619 // Store the return address to the appropriate stack slot.
1620 if (!FPDiff) return Chain;
1621 // Calculate the new stack slot for the return address.
1622 int SlotSize = Is64Bit ? 8 : 4;
Scott Michelfdc40a02009-02-17 22:15:04 +00001623 int NewReturnAddrFI =
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001624 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001625 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001626 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Scott Michelfdc40a02009-02-17 22:15:04 +00001627 Chain = DAG.getStore(Chain, dl, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001628 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001629 return Chain;
1630}
1631
Dan Gohman98ca4f22009-08-05 01:29:28 +00001632SDValue
1633X86TargetLowering::LowerCall(SDValue Chain, SDValue Callee,
1634 unsigned CallConv, bool isVarArg, bool isTailCall,
1635 const SmallVectorImpl<ISD::OutputArg> &Outs,
1636 const SmallVectorImpl<ISD::InputArg> &Ins,
1637 DebugLoc dl, SelectionDAG &DAG,
1638 SmallVectorImpl<SDValue> &InVals) {
Gordon Henriksenae636f82008-01-03 16:47:34 +00001639
Dan Gohman98ca4f22009-08-05 01:29:28 +00001640 MachineFunction &MF = DAG.getMachineFunction();
1641 bool Is64Bit = Subtarget->is64Bit();
1642 bool IsStructRet = CallIsStructReturn(Outs);
1643
1644 assert((!isTailCall ||
1645 (CallConv == CallingConv::Fast && PerformTailCallOpt)) &&
1646 "IsEligibleForTailCallOptimization missed a case!");
1647 assert(!(isVarArg && CallConv == CallingConv::Fast) &&
Gordon Henriksenae636f82008-01-03 16:47:34 +00001648 "Var args not supported with calling convention fastcc");
1649
Chris Lattner638402b2007-02-28 07:00:42 +00001650 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001651 SmallVector<CCValAssign, 16> ArgLocs;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001652 CCState CCInfo(CallConv, isVarArg, getTargetMachine(),
1653 ArgLocs, *DAG.getContext());
1654 CCInfo.AnalyzeCallOperands(Outs, CCAssignFnForNode(CallConv));
Scott Michelfdc40a02009-02-17 22:15:04 +00001655
Chris Lattner423c5f42007-02-28 05:31:48 +00001656 // Get a count of how many bytes are to be pushed on the stack.
1657 unsigned NumBytes = CCInfo.getNextStackOffset();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001658 if (PerformTailCallOpt && CallConv == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001659 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001660
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 int FPDiff = 0;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001662 if (isTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001663 // Lower arguments at fp - stackoffset + fpdiff.
Scott Michelfdc40a02009-02-17 22:15:04 +00001664 unsigned NumBytesCallerPushed =
Gordon Henriksen86737662008-01-05 16:56:59 +00001665 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1666 FPDiff = NumBytesCallerPushed - NumBytes;
1667
1668 // Set the delta of movement of the returnaddr stackslot.
1669 // But only set if delta is greater than previous delta.
1670 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1671 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1672 }
1673
Chris Lattnere563bbc2008-10-11 22:08:30 +00001674 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001675
Dan Gohman475871a2008-07-27 21:46:04 +00001676 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001677 // Load return adress for tail calls.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001678 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, isTailCall, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001679 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001680
Dan Gohman475871a2008-07-27 21:46:04 +00001681 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1682 SmallVector<SDValue, 8> MemOpChains;
1683 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001684
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001685 // Walk the register/memloc assignments, inserting copies/loads. In the case
1686 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001687 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1688 CCValAssign &VA = ArgLocs[i];
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001689 MVT RegVT = VA.getLocVT();
Dan Gohman98ca4f22009-08-05 01:29:28 +00001690 SDValue Arg = Outs[i].Val;
1691 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Dan Gohman095cc292008-09-13 01:54:27 +00001692 bool isByVal = Flags.isByVal();
Scott Michelfdc40a02009-02-17 22:15:04 +00001693
Chris Lattner423c5f42007-02-28 05:31:48 +00001694 // Promote the value if needed.
1695 switch (VA.getLocInfo()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00001696 default: llvm_unreachable("Unknown loc info!");
Chris Lattner423c5f42007-02-28 05:31:48 +00001697 case CCValAssign::Full: break;
1698 case CCValAssign::SExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001699 Arg = DAG.getNode(ISD::SIGN_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001700 break;
1701 case CCValAssign::ZExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001702 Arg = DAG.getNode(ISD::ZERO_EXTEND, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001703 break;
1704 case CCValAssign::AExt:
Anton Korobeynikov80cb8aa2009-08-03 08:13:24 +00001705 if (RegVT.isVector() && RegVT.getSizeInBits() == 128) {
1706 // Special case: passing MMX values in XMM registers.
1707 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i64, Arg);
1708 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i64, Arg);
1709 Arg = getMOVL(DAG, dl, MVT::v2i64, DAG.getUNDEF(MVT::v2i64), Arg);
1710 } else
1711 Arg = DAG.getNode(ISD::ANY_EXTEND, dl, RegVT, Arg);
1712 break;
1713 case CCValAssign::BCvt:
1714 Arg = DAG.getNode(ISD::BIT_CONVERT, dl, RegVT, Arg);
Chris Lattner423c5f42007-02-28 05:31:48 +00001715 break;
Anton Korobeynikov4ab15532009-08-03 08:13:56 +00001716 case CCValAssign::Indirect: {
1717 // Store the argument.
1718 SDValue SpillSlot = DAG.CreateStackTemporary(VA.getValVT());
1719 int FI = cast<FrameIndexSDNode>(SpillSlot)->getIndex();
1720 Chain = DAG.getStore(Chain, dl, Arg, SpillSlot,
1721 PseudoSourceValue::getFixedStack(FI), 0);
1722 Arg = SpillSlot;
1723 break;
1724 }
Evan Cheng6b5783d2006-05-25 18:56:34 +00001725 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001726
Chris Lattner423c5f42007-02-28 05:31:48 +00001727 if (VA.isRegLoc()) {
1728 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1729 } else {
Dan Gohman98ca4f22009-08-05 01:29:28 +00001730 if (!isTailCall || (isTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001731 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001732 if (StackPtr.getNode() == 0)
Dale Johannesendd64c412009-02-04 00:33:20 +00001733 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr, getPointerTy());
Scott Michelfdc40a02009-02-17 22:15:04 +00001734
Dan Gohman98ca4f22009-08-05 01:29:28 +00001735 MemOpChains.push_back(LowerMemOpCallTo(Chain, StackPtr, Arg,
1736 dl, DAG, VA, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001737 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001738 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001739 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001740
Evan Cheng32fe1032006-05-25 00:59:30 +00001741 if (!MemOpChains.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001742 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001743 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001744
Evan Cheng347d5f72006-04-28 21:29:37 +00001745 // Build a sequence of copy-to-reg nodes chained together with token chain
1746 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001747 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001748 // Tail call byval lowering might overwrite argument registers so in case of
1749 // tail call optimization the copies to registers are lowered later.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001750 if (!isTailCall)
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001751 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001752 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001753 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001754 InFlag = Chain.getValue(1);
1755 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001756
Chris Lattner951bf7d2009-07-09 02:44:11 +00001757
Chris Lattner88e1fd52009-07-09 04:24:46 +00001758 if (Subtarget->isPICStyleGOT()) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001759 // ELF / PIC requires GOT in the EBX register before function calls via PLT
1760 // GOT pointer.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001761 if (!isTailCall) {
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001762 Chain = DAG.getCopyToReg(Chain, dl, X86::EBX,
1763 DAG.getNode(X86ISD::GlobalBaseReg,
1764 DebugLoc::getUnknownLoc(),
1765 getPointerTy()),
1766 InFlag);
1767 InFlag = Chain.getValue(1);
1768 } else {
1769 // If we are tail calling and generating PIC/GOT style code load the
1770 // address of the callee into ECX. The value in ecx is used as target of
1771 // the tail jump. This is done to circumvent the ebx/callee-saved problem
1772 // for tail calls on PIC/GOT architectures. Normally we would just put the
1773 // address of GOT into ebx and then call target@PLT. But for tail calls
1774 // ebx would be restored (since ebx is callee saved) before jumping to the
1775 // target@PLT.
1776
1777 // Note: The actual moving to ECX is done further down.
1778 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1779 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1780 !G->getGlobal()->hasProtectedVisibility())
1781 Callee = LowerGlobalAddress(Callee, DAG);
1782 else if (isa<ExternalSymbolSDNode>(Callee))
Chris Lattner15a380a2009-07-09 04:39:06 +00001783 Callee = LowerExternalSymbol(Callee, DAG);
Chris Lattnerb133a0a2009-07-09 02:55:47 +00001784 }
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001785 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001786
Gordon Henriksen86737662008-01-05 16:56:59 +00001787 if (Is64Bit && isVarArg) {
1788 // From AMD64 ABI document:
1789 // For calls that may call functions that use varargs or stdargs
1790 // (prototype-less calls or calls to functions containing ellipsis (...) in
1791 // the declaration) %al is used as hidden argument to specify the number
1792 // of SSE registers used. The contents of %al do not need to match exactly
1793 // the number of registers, but must be an ubound on the number of SSE
1794 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001795
1796 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001797 // Count the number of XMM registers allocated.
1798 static const unsigned XMMArgRegs[] = {
1799 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1800 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1801 };
1802 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
Scott Michelfdc40a02009-02-17 22:15:04 +00001803 assert((Subtarget->hasSSE1() || !NumXMMRegs)
Torok Edwin3f142c32009-02-01 18:15:56 +00001804 && "SSE registers cannot be used when SSE is disabled");
Scott Michelfdc40a02009-02-17 22:15:04 +00001805
Dale Johannesendd64c412009-02-04 00:33:20 +00001806 Chain = DAG.getCopyToReg(Chain, dl, X86::AL,
Gordon Henriksen86737662008-01-05 16:56:59 +00001807 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1808 InFlag = Chain.getValue(1);
1809 }
1810
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001811
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001812 // For tail calls lower the arguments to the 'real' stack slot.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001813 if (isTailCall) {
1814 // Force all the incoming stack arguments to be loaded from the stack
1815 // before any new outgoing arguments are stored to the stack, because the
1816 // outgoing stack slots may alias the incoming argument stack slots, and
1817 // the alias isn't otherwise explicit. This is slightly more conservative
1818 // than necessary, because it means that each store effectively depends
1819 // on every argument instead of just those arguments it would clobber.
1820 SDValue ArgChain = DAG.getStackArgumentTokenFactor(Chain);
1821
Dan Gohman475871a2008-07-27 21:46:04 +00001822 SmallVector<SDValue, 8> MemOpChains2;
1823 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001824 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001825 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001826 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001827 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1828 CCValAssign &VA = ArgLocs[i];
1829 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001830 assert(VA.isMemLoc());
Dan Gohman98ca4f22009-08-05 01:29:28 +00001831 SDValue Arg = Outs[i].Val;
1832 ISD::ArgFlagsTy Flags = Outs[i].Flags;
Gordon Henriksen86737662008-01-05 16:56:59 +00001833 // Create frame index.
1834 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001835 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001836 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001837 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001838
Duncan Sands276dcbd2008-03-21 09:14:45 +00001839 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001840 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001841 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001842 if (StackPtr.getNode() == 0)
Scott Michelfdc40a02009-02-17 22:15:04 +00001843 StackPtr = DAG.getCopyFromReg(Chain, dl, X86StackPtr,
Dale Johannesendd64c412009-02-04 00:33:20 +00001844 getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00001845 Source = DAG.getNode(ISD::ADD, dl, getPointerTy(), StackPtr, Source);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001846
Dan Gohman98ca4f22009-08-05 01:29:28 +00001847 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN,
1848 ArgChain,
Dale Johannesendd64c412009-02-04 00:33:20 +00001849 Flags, DAG, dl));
Gordon Henriksen86737662008-01-05 16:56:59 +00001850 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001851 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001852 MemOpChains2.push_back(
Dan Gohman98ca4f22009-08-05 01:29:28 +00001853 DAG.getStore(ArgChain, dl, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001854 PseudoSourceValue::getFixedStack(FI), 0));
Scott Michelfdc40a02009-02-17 22:15:04 +00001855 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001856 }
1857 }
1858
1859 if (!MemOpChains2.empty())
Dale Johannesenace16102009-02-03 19:33:06 +00001860 Chain = DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001861 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001862
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001863 // Copy arguments to their registers.
1864 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
Scott Michelfdc40a02009-02-17 22:15:04 +00001865 Chain = DAG.getCopyToReg(Chain, dl, RegsToPass[i].first,
Dale Johannesendd64c412009-02-04 00:33:20 +00001866 RegsToPass[i].second, InFlag);
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001867 InFlag = Chain.getValue(1);
1868 }
Dan Gohman475871a2008-07-27 21:46:04 +00001869 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001870
Gordon Henriksen86737662008-01-05 16:56:59 +00001871 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001872 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
Dale Johannesenace16102009-02-03 19:33:06 +00001873 FPDiff, dl);
Gordon Henriksen86737662008-01-05 16:56:59 +00001874 }
1875
Evan Cheng32fe1032006-05-25 00:59:30 +00001876 // If the callee is a GlobalAddress node (quite common, every direct call is)
1877 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001878 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001879 // We should use extra load for direct calls to dllimported functions in
1880 // non-JIT mode.
Chris Lattner74e726e2009-07-09 05:27:35 +00001881 GlobalValue *GV = G->getGlobal();
Chris Lattner754b7652009-07-10 05:48:03 +00001882 if (!GV->hasDLLImportLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001883 unsigned char OpFlags = 0;
1884
1885 // On ELF targets, in both X86-64 and X86-32 mode, direct calls to
1886 // external symbols most go through the PLT in PIC mode. If the symbol
1887 // has hidden or protected visibility, or if it is static or local, then
1888 // we don't need to use the PLT - we can directly call it.
1889 if (Subtarget->isTargetELF() &&
1890 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001891 GV->hasDefaultVisibility() && !GV->hasLocalLinkage()) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001892 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001893 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001894 (GV->isDeclaration() || GV->isWeakForLinker()) &&
1895 Subtarget->getDarwinVers() < 9) {
1896 // PC-relative references to external symbols should go through $stub,
1897 // unless we're building with the leopard linker or later, which
1898 // automatically synthesizes these stubs.
1899 OpFlags = X86II::MO_DARWIN_STUB;
1900 }
Chris Lattner48a7d022009-07-09 05:02:21 +00001901
Chris Lattner74e726e2009-07-09 05:27:35 +00001902 Callee = DAG.getTargetGlobalAddress(GV, getPointerTy(),
Chris Lattner48a7d022009-07-09 05:02:21 +00001903 G->getOffset(), OpFlags);
1904 }
Bill Wendling056292f2008-09-16 21:48:12 +00001905 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001906 unsigned char OpFlags = 0;
1907
1908 // On ELF targets, in either X86-64 or X86-32 mode, direct calls to external
1909 // symbols should go through the PLT.
1910 if (Subtarget->isTargetELF() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001911 getTargetMachine().getRelocationModel() == Reloc::PIC_) {
Chris Lattner48a7d022009-07-09 05:02:21 +00001912 OpFlags = X86II::MO_PLT;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00001913 } else if (Subtarget->isPICStyleStubAny() &&
Chris Lattner74e726e2009-07-09 05:27:35 +00001914 Subtarget->getDarwinVers() < 9) {
1915 // PC-relative references to external symbols should go through $stub,
1916 // unless we're building with the leopard linker or later, which
1917 // automatically synthesizes these stubs.
1918 OpFlags = X86II::MO_DARWIN_STUB;
1919 }
1920
Chris Lattner48a7d022009-07-09 05:02:21 +00001921 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy(),
1922 OpFlags);
Dan Gohman98ca4f22009-08-05 01:29:28 +00001923 } else if (isTailCall) {
Arnold Schwaighoferbbd8c332009-06-12 16:26:57 +00001924 unsigned Opc = Is64Bit ? X86::R11 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001925
Dale Johannesendd64c412009-02-04 00:33:20 +00001926 Chain = DAG.getCopyToReg(Chain, dl,
Scott Michelfdc40a02009-02-17 22:15:04 +00001927 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001928 Callee,InFlag);
1929 Callee = DAG.getRegister(Opc, getPointerTy());
1930 // Add register as live out.
Dan Gohman7e77b0f2009-08-01 19:14:37 +00001931 MF.getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001932 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001933
Chris Lattnerd96d0722007-02-25 06:40:16 +00001934 // Returns a chain & a flag for retval copy to use.
1935 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001936 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001937
Dan Gohman98ca4f22009-08-05 01:29:28 +00001938 if (isTailCall) {
Dale Johannesene8d72302009-02-06 23:05:02 +00001939 Chain = DAG.getCALLSEQ_END(Chain, DAG.getIntPtrConstant(NumBytes, true),
1940 DAG.getIntPtrConstant(0, true), InFlag);
Gordon Henriksen86737662008-01-05 16:56:59 +00001941 InFlag = Chain.getValue(1);
Gordon Henriksen86737662008-01-05 16:56:59 +00001942 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001943
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001944 Ops.push_back(Chain);
1945 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001946
Dan Gohman98ca4f22009-08-05 01:29:28 +00001947 if (isTailCall)
Gordon Henriksen86737662008-01-05 16:56:59 +00001948 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001949
Gordon Henriksen86737662008-01-05 16:56:59 +00001950 // Add argument registers to the end of the list so that they are known live
1951 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001952 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1953 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1954 RegsToPass[i].second.getValueType()));
Scott Michelfdc40a02009-02-17 22:15:04 +00001955
Evan Cheng586ccac2008-03-18 23:36:35 +00001956 // Add an implicit use GOT pointer in EBX.
Dan Gohman98ca4f22009-08-05 01:29:28 +00001957 if (!isTailCall && Subtarget->isPICStyleGOT())
Evan Cheng586ccac2008-03-18 23:36:35 +00001958 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1959
1960 // Add an implicit use of AL for x86 vararg functions.
1961 if (Is64Bit && isVarArg)
1962 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1963
Gabor Greifba36cb52008-08-28 21:40:38 +00001964 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001965 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001966
Dan Gohman98ca4f22009-08-05 01:29:28 +00001967 if (isTailCall) {
1968 // If this is the first return lowered for this function, add the regs
1969 // to the liveout set for the function.
1970 if (MF.getRegInfo().liveout_empty()) {
1971 SmallVector<CCValAssign, 16> RVLocs;
1972 CCState CCInfo(CallConv, isVarArg, getTargetMachine(), RVLocs,
1973 *DAG.getContext());
1974 CCInfo.AnalyzeCallResult(Ins, RetCC_X86);
1975 for (unsigned i = 0; i != RVLocs.size(); ++i)
1976 if (RVLocs[i].isRegLoc())
1977 MF.getRegInfo().addLiveOut(RVLocs[i].getLocReg());
1978 }
Scott Michelfdc40a02009-02-17 22:15:04 +00001979
Dan Gohman98ca4f22009-08-05 01:29:28 +00001980 assert(((Callee.getOpcode() == ISD::Register &&
1981 (cast<RegisterSDNode>(Callee)->getReg() == X86::EAX ||
1982 cast<RegisterSDNode>(Callee)->getReg() == X86::R9)) ||
1983 Callee.getOpcode() == ISD::TargetExternalSymbol ||
1984 Callee.getOpcode() == ISD::TargetGlobalAddress) &&
1985 "Expecting an global address, external symbol, or register");
1986
1987 return DAG.getNode(X86ISD::TC_RETURN, dl,
1988 NodeTys, &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001989 }
1990
Dale Johannesenace16102009-02-03 19:33:06 +00001991 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001992 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001993
Chris Lattner2d297092006-05-23 18:50:38 +00001994 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001995 unsigned NumBytesForCalleeToPush;
Dan Gohman98ca4f22009-08-05 01:29:28 +00001996 if (IsCalleePop(isVarArg, CallConv))
Gordon Henriksen86737662008-01-05 16:56:59 +00001997 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Dan Gohman98ca4f22009-08-05 01:29:28 +00001998 else if (!Is64Bit && CallConv != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001999 // If this is is a call to a struct-return function, the callee
2000 // pops the hidden struct pointer, so we have to push it back.
2001 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00002002 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00002003 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00002004 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Scott Michelfdc40a02009-02-17 22:15:04 +00002005
Gordon Henriksenae636f82008-01-03 16:47:34 +00002006 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002007 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00002008 DAG.getIntPtrConstant(NumBytes, true),
2009 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
2010 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00002011 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00002012 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002013
Chris Lattner3085e152007-02-25 08:59:22 +00002014 // Handle result values, copying them out of physregs into vregs that we
2015 // return.
Dan Gohman98ca4f22009-08-05 01:29:28 +00002016 return LowerCallResult(Chain, InFlag, CallConv, isVarArg,
2017 Ins, dl, DAG, InVals);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002018}
2019
Evan Cheng25ab6902006-09-08 06:48:29 +00002020
2021//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002022// Fast Calling Convention (tail call) implementation
2023//===----------------------------------------------------------------------===//
2024
2025// Like std call, callee cleans arguments, convention except that ECX is
2026// reserved for storing the tail called function address. Only 2 registers are
2027// free for argument passing (inreg). Tail call optimization is performed
2028// provided:
2029// * tailcallopt is enabled
2030// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00002031// On X86_64 architecture with GOT-style position independent code only local
2032// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002033// To keep the stack aligned according to platform abi the function
2034// GetAlignedArgumentStackSize ensures that argument delta is always multiples
2035// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002036// If a tail called function callee has more arguments than the caller the
2037// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00002038// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002039// original REtADDR, but before the saved framepointer or the spilled registers
2040// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
2041// stack layout:
2042// arg1
2043// arg2
2044// RETADDR
Scott Michelfdc40a02009-02-17 22:15:04 +00002045// [ new RETADDR
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002046// move area ]
2047// (possible EBP)
2048// ESI
2049// EDI
2050// local1 ..
2051
2052/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
2053/// for a 16 byte align requirement.
Scott Michelfdc40a02009-02-17 22:15:04 +00002054unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002055 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00002056 MachineFunction &MF = DAG.getMachineFunction();
2057 const TargetMachine &TM = MF.getTarget();
2058 const TargetFrameInfo &TFI = *TM.getFrameInfo();
2059 unsigned StackAlignment = TFI.getStackAlignment();
Scott Michelfdc40a02009-02-17 22:15:04 +00002060 uint64_t AlignMask = StackAlignment - 1;
Evan Chenge9ac9e62008-09-07 09:07:23 +00002061 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002062 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00002063 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
2064 // Number smaller than 12 so just add the difference.
2065 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
2066 } else {
2067 // Mask out lower bits, add stackalignment once plus the 12 bytes.
Scott Michelfdc40a02009-02-17 22:15:04 +00002068 Offset = ((~AlignMask) & Offset) + StackAlignment +
Evan Chenge9ac9e62008-09-07 09:07:23 +00002069 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002070 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00002071 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002072}
2073
Dan Gohman98ca4f22009-08-05 01:29:28 +00002074/// IsEligibleForTailCallOptimization - Check whether the call is eligible
2075/// for tail call optimization. Targets which want to do tail call
2076/// optimization should implement this function.
2077bool
2078X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Callee,
2079 unsigned CalleeCC,
2080 bool isVarArg,
2081 const SmallVectorImpl<ISD::InputArg> &Ins,
2082 SelectionDAG& DAG) const {
2083 MachineFunction &MF = DAG.getMachineFunction();
2084 unsigned CallerCC = MF.getFunction()->getCallingConv();
2085 return CalleeCC == CallingConv::Fast && CallerCC == CalleeCC;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00002086}
2087
Dan Gohman3df24e62008-09-03 23:12:08 +00002088FastISel *
2089X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00002090 MachineModuleInfo *mmo,
Devang Patel83489bb2009-01-13 00:35:13 +00002091 DwarfWriter *dw,
Dan Gohman3df24e62008-09-03 23:12:08 +00002092 DenseMap<const Value *, unsigned> &vm,
2093 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00002094 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002095 DenseMap<const AllocaInst *, int> &am
2096#ifndef NDEBUG
2097 , SmallSet<Instruction*, 8> &cil
2098#endif
2099 ) {
Devang Patel83489bb2009-01-13 00:35:13 +00002100 return X86::createFastISel(mf, mmo, dw, vm, bm, am
Dan Gohmandd5b58a2008-10-14 23:54:11 +00002101#ifndef NDEBUG
2102 , cil
2103#endif
2104 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00002105}
2106
2107
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00002108//===----------------------------------------------------------------------===//
2109// Other Lowering Hooks
2110//===----------------------------------------------------------------------===//
2111
2112
Dan Gohman475871a2008-07-27 21:46:04 +00002113SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002114 MachineFunction &MF = DAG.getMachineFunction();
2115 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
2116 int ReturnAddrIndex = FuncInfo->getRAIndex();
2117
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002118 if (ReturnAddrIndex == 0) {
2119 // Set up a frame object for the return address.
Bill Wendling64e87322009-01-16 19:25:27 +00002120 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00002121 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00002122 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002123 }
2124
Evan Cheng25ab6902006-09-08 06:48:29 +00002125 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002126}
2127
2128
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002129/// TranslateX86CC - do a one to one translation of a ISD::CondCode to the X86
2130/// specific condition code, returning the condition code and the LHS/RHS of the
2131/// comparison to make.
2132static unsigned TranslateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
2133 SDValue &LHS, SDValue &RHS, SelectionDAG &DAG) {
Evan Chengd9558e02006-01-06 00:43:03 +00002134 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002135 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
2136 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
2137 // X > -1 -> X == 0, jump !sign.
2138 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002139 return X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002140 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
2141 // X < 0 -> X == 0, jump on sign.
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002142 return X86::COND_S;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002143 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00002144 // X < 1 -> X <= 0
2145 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002146 return X86::COND_LE;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00002147 }
Chris Lattnerf9570512006-09-13 03:22:10 +00002148 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002149
Evan Chengd9558e02006-01-06 00:43:03 +00002150 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002151 default: llvm_unreachable("Invalid integer condition!");
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002152 case ISD::SETEQ: return X86::COND_E;
2153 case ISD::SETGT: return X86::COND_G;
2154 case ISD::SETGE: return X86::COND_GE;
2155 case ISD::SETLT: return X86::COND_L;
2156 case ISD::SETLE: return X86::COND_LE;
2157 case ISD::SETNE: return X86::COND_NE;
2158 case ISD::SETULT: return X86::COND_B;
2159 case ISD::SETUGT: return X86::COND_A;
2160 case ISD::SETULE: return X86::COND_BE;
2161 case ISD::SETUGE: return X86::COND_AE;
Evan Chengd9558e02006-01-06 00:43:03 +00002162 }
Chris Lattner4c78e022008-12-23 23:42:27 +00002163 }
Scott Michelfdc40a02009-02-17 22:15:04 +00002164
Chris Lattner4c78e022008-12-23 23:42:27 +00002165 // First determine if it is required or is profitable to flip the operands.
Duncan Sands4047f4a2008-10-24 13:03:10 +00002166
Chris Lattner4c78e022008-12-23 23:42:27 +00002167 // If LHS is a foldable load, but RHS is not, flip the condition.
2168 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
2169 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
2170 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
2171 std::swap(LHS, RHS);
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002172 }
2173
Chris Lattner4c78e022008-12-23 23:42:27 +00002174 switch (SetCCOpcode) {
2175 default: break;
2176 case ISD::SETOLT:
2177 case ISD::SETOLE:
2178 case ISD::SETUGT:
2179 case ISD::SETUGE:
2180 std::swap(LHS, RHS);
2181 break;
2182 }
2183
2184 // On a floating point condition, the flags are set as follows:
2185 // ZF PF CF op
2186 // 0 | 0 | 0 | X > Y
2187 // 0 | 0 | 1 | X < Y
2188 // 1 | 0 | 0 | X == Y
2189 // 1 | 1 | 1 | unordered
2190 switch (SetCCOpcode) {
Torok Edwinc23197a2009-07-14 16:55:14 +00002191 default: llvm_unreachable("Condcode should be pre-legalized away");
Chris Lattner4c78e022008-12-23 23:42:27 +00002192 case ISD::SETUEQ:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002193 case ISD::SETEQ: return X86::COND_E;
Chris Lattner4c78e022008-12-23 23:42:27 +00002194 case ISD::SETOLT: // flipped
2195 case ISD::SETOGT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002196 case ISD::SETGT: return X86::COND_A;
Chris Lattner4c78e022008-12-23 23:42:27 +00002197 case ISD::SETOLE: // flipped
2198 case ISD::SETOGE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002199 case ISD::SETGE: return X86::COND_AE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002200 case ISD::SETUGT: // flipped
2201 case ISD::SETULT:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002202 case ISD::SETLT: return X86::COND_B;
Chris Lattner4c78e022008-12-23 23:42:27 +00002203 case ISD::SETUGE: // flipped
2204 case ISD::SETULE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002205 case ISD::SETLE: return X86::COND_BE;
Chris Lattner4c78e022008-12-23 23:42:27 +00002206 case ISD::SETONE:
Chris Lattner1c39d4c2008-12-24 23:53:05 +00002207 case ISD::SETNE: return X86::COND_NE;
2208 case ISD::SETUO: return X86::COND_P;
2209 case ISD::SETO: return X86::COND_NP;
Chris Lattner4c78e022008-12-23 23:42:27 +00002210 }
Evan Chengd9558e02006-01-06 00:43:03 +00002211}
2212
Evan Cheng4a460802006-01-11 00:33:36 +00002213/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2214/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002215/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002216static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002217 switch (X86CC) {
2218 default:
2219 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002220 case X86::COND_B:
2221 case X86::COND_BE:
2222 case X86::COND_E:
2223 case X86::COND_P:
2224 case X86::COND_A:
2225 case X86::COND_AE:
2226 case X86::COND_NE:
2227 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002228 return true;
2229 }
2230}
2231
Nate Begeman9008ca62009-04-27 18:41:29 +00002232/// isUndefOrInRange - Return true if Val is undef or if its value falls within
2233/// the specified range (L, H].
2234static bool isUndefOrInRange(int Val, int Low, int Hi) {
2235 return (Val < 0) || (Val >= Low && Val < Hi);
2236}
2237
2238/// isUndefOrEqual - Val is either less than zero (undef) or equal to the
2239/// specified value.
2240static bool isUndefOrEqual(int Val, int CmpVal) {
2241 if (Val < 0 || Val == CmpVal)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002242 return true;
Nate Begeman9008ca62009-04-27 18:41:29 +00002243 return false;
Evan Chengc5cdff22006-04-07 21:53:05 +00002244}
2245
Nate Begeman9008ca62009-04-27 18:41:29 +00002246/// isPSHUFDMask - Return true if the node specifies a shuffle of elements that
2247/// is suitable for input to PSHUFD or PSHUFW. That is, it doesn't reference
2248/// the second operand.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002249static bool isPSHUFDMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002250 if (VT == MVT::v4f32 || VT == MVT::v4i32 || VT == MVT::v4i16)
2251 return (Mask[0] < 4 && Mask[1] < 4 && Mask[2] < 4 && Mask[3] < 4);
2252 if (VT == MVT::v2f64 || VT == MVT::v2i64)
2253 return (Mask[0] < 2 && Mask[1] < 2);
2254 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002255}
2256
Nate Begeman9008ca62009-04-27 18:41:29 +00002257bool X86::isPSHUFDMask(ShuffleVectorSDNode *N) {
2258 SmallVector<int, 8> M;
2259 N->getMask(M);
2260 return ::isPSHUFDMask(M, N->getValueType(0));
2261}
Evan Cheng0188ecb2006-03-22 18:59:22 +00002262
Nate Begeman9008ca62009-04-27 18:41:29 +00002263/// isPSHUFHWMask - Return true if the node specifies a shuffle of elements that
2264/// is suitable for input to PSHUFHW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002265static bool isPSHUFHWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002266 if (VT != MVT::v8i16)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002267 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002268
2269 // Lower quadword copied in order or undef.
2270 for (int i = 0; i != 4; ++i)
2271 if (Mask[i] >= 0 && Mask[i] != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002272 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002273
Evan Cheng506d3df2006-03-29 23:07:14 +00002274 // Upper quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002275 for (int i = 4; i != 8; ++i)
2276 if (Mask[i] >= 0 && (Mask[i] < 4 || Mask[i] > 7))
Evan Cheng506d3df2006-03-29 23:07:14 +00002277 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002278
Evan Cheng506d3df2006-03-29 23:07:14 +00002279 return true;
2280}
2281
Nate Begeman9008ca62009-04-27 18:41:29 +00002282bool X86::isPSHUFHWMask(ShuffleVectorSDNode *N) {
2283 SmallVector<int, 8> M;
2284 N->getMask(M);
2285 return ::isPSHUFHWMask(M, N->getValueType(0));
2286}
Evan Cheng506d3df2006-03-29 23:07:14 +00002287
Nate Begeman9008ca62009-04-27 18:41:29 +00002288/// isPSHUFLWMask - Return true if the node specifies a shuffle of elements that
2289/// is suitable for input to PSHUFLW.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002290static bool isPSHUFLWMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002291 if (VT != MVT::v8i16)
Evan Cheng506d3df2006-03-29 23:07:14 +00002292 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002293
Rafael Espindola15684b22009-04-24 12:40:33 +00002294 // Upper quadword copied in order.
Nate Begeman9008ca62009-04-27 18:41:29 +00002295 for (int i = 4; i != 8; ++i)
2296 if (Mask[i] >= 0 && Mask[i] != i)
Rafael Espindola15684b22009-04-24 12:40:33 +00002297 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002298
Rafael Espindola15684b22009-04-24 12:40:33 +00002299 // Lower quadword shuffled.
Nate Begeman9008ca62009-04-27 18:41:29 +00002300 for (int i = 0; i != 4; ++i)
2301 if (Mask[i] >= 4)
Rafael Espindola15684b22009-04-24 12:40:33 +00002302 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002303
Rafael Espindola15684b22009-04-24 12:40:33 +00002304 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002305}
2306
Nate Begeman9008ca62009-04-27 18:41:29 +00002307bool X86::isPSHUFLWMask(ShuffleVectorSDNode *N) {
2308 SmallVector<int, 8> M;
2309 N->getMask(M);
2310 return ::isPSHUFLWMask(M, N->getValueType(0));
2311}
2312
Evan Cheng14aed5e2006-03-24 01:18:28 +00002313/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2314/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002315static bool isSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002316 int NumElems = VT.getVectorNumElements();
2317 if (NumElems != 2 && NumElems != 4)
2318 return false;
2319
2320 int Half = NumElems / 2;
2321 for (int i = 0; i < Half; ++i)
2322 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002323 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002324 for (int i = Half; i < NumElems; ++i)
2325 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002326 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002327
Evan Cheng14aed5e2006-03-24 01:18:28 +00002328 return true;
2329}
2330
Nate Begeman9008ca62009-04-27 18:41:29 +00002331bool X86::isSHUFPMask(ShuffleVectorSDNode *N) {
2332 SmallVector<int, 8> M;
2333 N->getMask(M);
2334 return ::isSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002335}
2336
Evan Cheng213d2cf2007-05-17 18:45:50 +00002337/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002338/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2339/// half elements to come from vector 1 (which would equal the dest.) and
2340/// the upper half to come from vector 2.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002341static bool isCommutedSHUFPMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002342 int NumElems = VT.getVectorNumElements();
2343
2344 if (NumElems != 2 && NumElems != 4)
2345 return false;
2346
2347 int Half = NumElems / 2;
2348 for (int i = 0; i < Half; ++i)
2349 if (!isUndefOrInRange(Mask[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002350 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002351 for (int i = Half; i < NumElems; ++i)
2352 if (!isUndefOrInRange(Mask[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002353 return false;
2354 return true;
2355}
2356
Nate Begeman9008ca62009-04-27 18:41:29 +00002357static bool isCommutedSHUFP(ShuffleVectorSDNode *N) {
2358 SmallVector<int, 8> M;
2359 N->getMask(M);
2360 return isCommutedSHUFPMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002361}
2362
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002363/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2364/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002365bool X86::isMOVHLPSMask(ShuffleVectorSDNode *N) {
2366 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002367 return false;
2368
Evan Cheng2064a2b2006-03-28 06:50:32 +00002369 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Nate Begeman9008ca62009-04-27 18:41:29 +00002370 return isUndefOrEqual(N->getMaskElt(0), 6) &&
2371 isUndefOrEqual(N->getMaskElt(1), 7) &&
2372 isUndefOrEqual(N->getMaskElt(2), 2) &&
2373 isUndefOrEqual(N->getMaskElt(3), 3);
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002374}
2375
Evan Cheng5ced1d82006-04-06 23:23:56 +00002376/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2377/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
Nate Begeman9008ca62009-04-27 18:41:29 +00002378bool X86::isMOVLPMask(ShuffleVectorSDNode *N) {
2379 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002380
Evan Cheng5ced1d82006-04-06 23:23:56 +00002381 if (NumElems != 2 && NumElems != 4)
2382 return false;
2383
Evan Chengc5cdff22006-04-07 21:53:05 +00002384 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002385 if (!isUndefOrEqual(N->getMaskElt(i), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002386 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002387
Evan Chengc5cdff22006-04-07 21:53:05 +00002388 for (unsigned i = NumElems/2; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002389 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002390 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002391
2392 return true;
2393}
2394
2395/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002396/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2397/// and MOVLHPS.
Nate Begeman9008ca62009-04-27 18:41:29 +00002398bool X86::isMOVHPMask(ShuffleVectorSDNode *N) {
2399 unsigned NumElems = N->getValueType(0).getVectorNumElements();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002400
Evan Cheng5ced1d82006-04-06 23:23:56 +00002401 if (NumElems != 2 && NumElems != 4)
2402 return false;
2403
Evan Chengc5cdff22006-04-07 21:53:05 +00002404 for (unsigned i = 0; i < NumElems/2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002405 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Chengc5cdff22006-04-07 21:53:05 +00002406 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002407
Nate Begeman9008ca62009-04-27 18:41:29 +00002408 for (unsigned i = 0; i < NumElems/2; ++i)
2409 if (!isUndefOrEqual(N->getMaskElt(i + NumElems/2), i + NumElems))
Evan Chengc5cdff22006-04-07 21:53:05 +00002410 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002411
2412 return true;
2413}
2414
Nate Begeman9008ca62009-04-27 18:41:29 +00002415/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2416/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2417/// <2, 3, 2, 3>
2418bool X86::isMOVHLPS_v_undef_Mask(ShuffleVectorSDNode *N) {
2419 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2420
2421 if (NumElems != 4)
2422 return false;
2423
2424 return isUndefOrEqual(N->getMaskElt(0), 2) &&
2425 isUndefOrEqual(N->getMaskElt(1), 3) &&
2426 isUndefOrEqual(N->getMaskElt(2), 2) &&
2427 isUndefOrEqual(N->getMaskElt(3), 3);
2428}
2429
Evan Cheng0038e592006-03-28 00:39:58 +00002430/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2431/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002432static bool isUNPCKLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002433 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002434 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002435 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002436 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002437
2438 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2439 int BitI = Mask[i];
2440 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002441 if (!isUndefOrEqual(BitI, j))
2442 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002443 if (V2IsSplat) {
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002444 if (!isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002445 return false;
2446 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002447 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002448 return false;
2449 }
Evan Cheng0038e592006-03-28 00:39:58 +00002450 }
Evan Cheng0038e592006-03-28 00:39:58 +00002451 return true;
2452}
2453
Nate Begeman9008ca62009-04-27 18:41:29 +00002454bool X86::isUNPCKLMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2455 SmallVector<int, 8> M;
2456 N->getMask(M);
2457 return ::isUNPCKLMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002458}
2459
Evan Cheng4fcb9222006-03-28 02:43:26 +00002460/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2461/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002462static bool isUNPCKHMask(const SmallVectorImpl<int> &Mask, MVT VT,
Rafael Espindola15684b22009-04-24 12:40:33 +00002463 bool V2IsSplat = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002464 int NumElts = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002465 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002466 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002467
2468 for (int i = 0, j = 0; i != NumElts; i += 2, ++j) {
2469 int BitI = Mask[i];
2470 int BitI1 = Mask[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002471 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002472 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002473 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002474 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002475 return false;
2476 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002477 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002478 return false;
2479 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002480 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002481 return true;
2482}
2483
Nate Begeman9008ca62009-04-27 18:41:29 +00002484bool X86::isUNPCKHMask(ShuffleVectorSDNode *N, bool V2IsSplat) {
2485 SmallVector<int, 8> M;
2486 N->getMask(M);
2487 return ::isUNPCKHMask(M, N->getValueType(0), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002488}
2489
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002490/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2491/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2492/// <0, 0, 1, 1>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002493static bool isUNPCKL_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002494 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002495 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002496 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002497
2498 for (int i = 0, j = 0; i != NumElems; i += 2, ++j) {
2499 int BitI = Mask[i];
2500 int BitI1 = Mask[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002501 if (!isUndefOrEqual(BitI, j))
2502 return false;
2503 if (!isUndefOrEqual(BitI1, j))
2504 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002505 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002506 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002507}
2508
Nate Begeman9008ca62009-04-27 18:41:29 +00002509bool X86::isUNPCKL_v_undef_Mask(ShuffleVectorSDNode *N) {
2510 SmallVector<int, 8> M;
2511 N->getMask(M);
2512 return ::isUNPCKL_v_undef_Mask(M, N->getValueType(0));
2513}
2514
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002515/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2516/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2517/// <2, 2, 3, 3>
Nate Begeman5a5ca152009-04-29 05:20:52 +00002518static bool isUNPCKH_v_undef_Mask(const SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002519 int NumElems = VT.getVectorNumElements();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002520 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2521 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002522
2523 for (int i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
2524 int BitI = Mask[i];
2525 int BitI1 = Mask[i+1];
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002526 if (!isUndefOrEqual(BitI, j))
2527 return false;
2528 if (!isUndefOrEqual(BitI1, j))
2529 return false;
2530 }
Rafael Espindola15684b22009-04-24 12:40:33 +00002531 return true;
Nate Begemanb706d292009-04-24 03:42:54 +00002532}
2533
Nate Begeman9008ca62009-04-27 18:41:29 +00002534bool X86::isUNPCKH_v_undef_Mask(ShuffleVectorSDNode *N) {
2535 SmallVector<int, 8> M;
2536 N->getMask(M);
2537 return ::isUNPCKH_v_undef_Mask(M, N->getValueType(0));
2538}
2539
Evan Cheng017dcc62006-04-21 01:05:10 +00002540/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2541/// specifies a shuffle of elements that is suitable for input to MOVSS,
2542/// MOVSD, and MOVD, i.e. setting the lowest element.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002543static bool isMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT) {
Eli Friedman10415532009-06-06 06:05:10 +00002544 if (VT.getVectorElementType().getSizeInBits() < 32)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002545 return false;
Eli Friedman10415532009-06-06 06:05:10 +00002546
2547 int NumElts = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002548
2549 if (!isUndefOrEqual(Mask[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002550 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002551
2552 for (int i = 1; i < NumElts; ++i)
2553 if (!isUndefOrEqual(Mask[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002554 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002555
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002556 return true;
2557}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002558
Nate Begeman9008ca62009-04-27 18:41:29 +00002559bool X86::isMOVLMask(ShuffleVectorSDNode *N) {
2560 SmallVector<int, 8> M;
2561 N->getMask(M);
2562 return ::isMOVLMask(M, N->getValueType(0));
Evan Cheng39623da2006-04-20 08:58:49 +00002563}
2564
Evan Cheng017dcc62006-04-21 01:05:10 +00002565/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2566/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002567/// element of vector 2 and the other elements to come from vector 1 in order.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002568static bool isCommutedMOVLMask(const SmallVectorImpl<int> &Mask, MVT VT,
Nate Begeman9008ca62009-04-27 18:41:29 +00002569 bool V2IsSplat = false, bool V2IsUndef = false) {
2570 int NumOps = VT.getVectorNumElements();
Chris Lattner5a88b832007-02-25 07:10:00 +00002571 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002572 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002573
2574 if (!isUndefOrEqual(Mask[0], 0))
Evan Cheng39623da2006-04-20 08:58:49 +00002575 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002576
2577 for (int i = 1; i < NumOps; ++i)
2578 if (!(isUndefOrEqual(Mask[i], i+NumOps) ||
2579 (V2IsUndef && isUndefOrInRange(Mask[i], NumOps, NumOps*2)) ||
2580 (V2IsSplat && isUndefOrEqual(Mask[i], NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002581 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002582
Evan Cheng39623da2006-04-20 08:58:49 +00002583 return true;
2584}
2585
Nate Begeman9008ca62009-04-27 18:41:29 +00002586static bool isCommutedMOVL(ShuffleVectorSDNode *N, bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002587 bool V2IsUndef = false) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002588 SmallVector<int, 8> M;
2589 N->getMask(M);
2590 return isCommutedMOVLMask(M, N->getValueType(0), V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002591}
2592
Evan Chengd9539472006-04-14 21:59:03 +00002593/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2594/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002595bool X86::isMOVSHDUPMask(ShuffleVectorSDNode *N) {
2596 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002597 return false;
2598
2599 // Expect 1, 1, 3, 3
Rafael Espindola15684b22009-04-24 12:40:33 +00002600 for (unsigned i = 0; i < 2; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002601 int Elt = N->getMaskElt(i);
2602 if (Elt >= 0 && Elt != 1)
2603 return false;
Rafael Espindola15684b22009-04-24 12:40:33 +00002604 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002605
2606 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002607 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002608 int Elt = N->getMaskElt(i);
2609 if (Elt >= 0 && Elt != 3)
2610 return false;
2611 if (Elt == 3)
2612 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002613 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002614 // Don't use movshdup if it can be done with a shufps.
Nate Begeman9008ca62009-04-27 18:41:29 +00002615 // FIXME: verify that matching u, u, 3, 3 is what we want.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002616 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002617}
2618
2619/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2620/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002621bool X86::isMOVSLDUPMask(ShuffleVectorSDNode *N) {
2622 if (N->getValueType(0).getVectorNumElements() != 4)
Evan Chengd9539472006-04-14 21:59:03 +00002623 return false;
2624
2625 // Expect 0, 0, 2, 2
Nate Begeman9008ca62009-04-27 18:41:29 +00002626 for (unsigned i = 0; i < 2; ++i)
2627 if (N->getMaskElt(i) > 0)
2628 return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002629
2630 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002631 for (unsigned i = 2; i < 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002632 int Elt = N->getMaskElt(i);
2633 if (Elt >= 0 && Elt != 2)
2634 return false;
2635 if (Elt == 2)
2636 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002637 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002638 // Don't use movsldup if it can be done with a shufps.
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002639 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002640}
2641
Evan Cheng0b457f02008-09-25 20:50:48 +00002642/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2643/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
Nate Begeman9008ca62009-04-27 18:41:29 +00002644bool X86::isMOVDDUPMask(ShuffleVectorSDNode *N) {
2645 int e = N->getValueType(0).getVectorNumElements() / 2;
2646
2647 for (int i = 0; i < e; ++i)
2648 if (!isUndefOrEqual(N->getMaskElt(i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002649 return false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002650 for (int i = 0; i < e; ++i)
2651 if (!isUndefOrEqual(N->getMaskElt(e+i), i))
Evan Cheng0b457f02008-09-25 20:50:48 +00002652 return false;
2653 return true;
2654}
2655
Evan Cheng63d33002006-03-22 08:01:21 +00002656/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2657/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2658/// instructions.
2659unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002660 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
2661 int NumOperands = SVOp->getValueType(0).getVectorNumElements();
2662
Evan Chengb9df0ca2006-03-22 02:53:00 +00002663 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2664 unsigned Mask = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00002665 for (int i = 0; i < NumOperands; ++i) {
2666 int Val = SVOp->getMaskElt(NumOperands-i-1);
2667 if (Val < 0) Val = 0;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002668 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002669 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002670 if (i != NumOperands - 1)
2671 Mask <<= Shift;
2672 }
Evan Cheng63d33002006-03-22 08:01:21 +00002673 return Mask;
2674}
2675
Evan Cheng506d3df2006-03-29 23:07:14 +00002676/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2677/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2678/// instructions.
2679unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002680 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002681 unsigned Mask = 0;
2682 // 8 nodes, but we only care about the last 4.
2683 for (unsigned i = 7; i >= 4; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002684 int Val = SVOp->getMaskElt(i);
2685 if (Val >= 0)
Mon P Wang7bcaefa2009-02-04 01:16:59 +00002686 Mask |= (Val - 4);
Evan Cheng506d3df2006-03-29 23:07:14 +00002687 if (i != 4)
2688 Mask <<= 2;
2689 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002690 return Mask;
2691}
2692
2693/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2694/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2695/// instructions.
2696unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002697 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(N);
Evan Cheng506d3df2006-03-29 23:07:14 +00002698 unsigned Mask = 0;
2699 // 8 nodes, but we only care about the first 4.
2700 for (int i = 3; i >= 0; --i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002701 int Val = SVOp->getMaskElt(i);
2702 if (Val >= 0)
2703 Mask |= Val;
Evan Cheng506d3df2006-03-29 23:07:14 +00002704 if (i != 0)
2705 Mask <<= 2;
2706 }
Evan Cheng506d3df2006-03-29 23:07:14 +00002707 return Mask;
2708}
2709
Evan Cheng37b73872009-07-30 08:33:02 +00002710/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2711/// constant +0.0.
2712bool X86::isZeroNode(SDValue Elt) {
2713 return ((isa<ConstantSDNode>(Elt) &&
2714 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
2715 (isa<ConstantFPSDNode>(Elt) &&
2716 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
2717}
2718
Nate Begeman9008ca62009-04-27 18:41:29 +00002719/// CommuteVectorShuffle - Swap vector_shuffle operands as well as values in
2720/// their permute mask.
2721static SDValue CommuteVectorShuffle(ShuffleVectorSDNode *SVOp,
2722 SelectionDAG &DAG) {
2723 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002724 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002725 SmallVector<int, 8> MaskVec;
2726
Nate Begeman5a5ca152009-04-29 05:20:52 +00002727 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002728 int idx = SVOp->getMaskElt(i);
2729 if (idx < 0)
2730 MaskVec.push_back(idx);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002731 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002732 MaskVec.push_back(idx + NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002733 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002734 MaskVec.push_back(idx - NumElems);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002735 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002736 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(1),
2737 SVOp->getOperand(0), &MaskVec[0]);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002738}
2739
Evan Cheng779ccea2007-12-07 21:30:01 +00002740/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2741/// the two vector operands have swapped position.
Nate Begeman9008ca62009-04-27 18:41:29 +00002742static void CommuteVectorShuffleMask(SmallVectorImpl<int> &Mask, MVT VT) {
Nate Begeman5a5ca152009-04-29 05:20:52 +00002743 unsigned NumElems = VT.getVectorNumElements();
2744 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002745 int idx = Mask[i];
2746 if (idx < 0)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002747 continue;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002748 else if (idx < (int)NumElems)
Nate Begeman9008ca62009-04-27 18:41:29 +00002749 Mask[i] = idx + NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002750 else
Nate Begeman9008ca62009-04-27 18:41:29 +00002751 Mask[i] = idx - NumElems;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002752 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002753}
2754
Evan Cheng533a0aa2006-04-19 20:35:22 +00002755/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2756/// match movhlps. The lower half elements should come from upper half of
2757/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002758/// half of V2 (and in order).
Nate Begeman9008ca62009-04-27 18:41:29 +00002759static bool ShouldXformToMOVHLPS(ShuffleVectorSDNode *Op) {
2760 if (Op->getValueType(0).getVectorNumElements() != 4)
Evan Cheng533a0aa2006-04-19 20:35:22 +00002761 return false;
2762 for (unsigned i = 0, e = 2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002763 if (!isUndefOrEqual(Op->getMaskElt(i), i+2))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002764 return false;
2765 for (unsigned i = 2; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002766 if (!isUndefOrEqual(Op->getMaskElt(i), i+4))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002767 return false;
2768 return true;
2769}
2770
Evan Cheng5ced1d82006-04-06 23:23:56 +00002771/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002772/// is promoted to a vector. It also returns the LoadSDNode by reference if
2773/// required.
2774static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002775 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2776 return false;
2777 N = N->getOperand(0).getNode();
2778 if (!ISD::isNON_EXTLoad(N))
2779 return false;
2780 if (LD)
2781 *LD = cast<LoadSDNode>(N);
2782 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002783}
2784
Evan Cheng533a0aa2006-04-19 20:35:22 +00002785/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2786/// match movlp{s|d}. The lower half elements should come from lower half of
2787/// V1 (and in order), and the upper half elements should come from the upper
2788/// half of V2 (and in order). And since V1 will become the source of the
2789/// MOVLP, it must be either a vector load or a scalar load to vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00002790static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2,
2791 ShuffleVectorSDNode *Op) {
Evan Cheng466685d2006-10-09 20:57:25 +00002792 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002793 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002794 // Is V2 is a vector load, don't do this transformation. We will try to use
2795 // load folding shufps op.
2796 if (ISD::isNON_EXTLoad(V2))
2797 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002798
Nate Begeman5a5ca152009-04-29 05:20:52 +00002799 unsigned NumElems = Op->getValueType(0).getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002800
Evan Cheng533a0aa2006-04-19 20:35:22 +00002801 if (NumElems != 2 && NumElems != 4)
2802 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002803 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002804 if (!isUndefOrEqual(Op->getMaskElt(i), i))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002805 return false;
Nate Begeman5a5ca152009-04-29 05:20:52 +00002806 for (unsigned i = NumElems/2; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002807 if (!isUndefOrEqual(Op->getMaskElt(i), i+NumElems))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002808 return false;
2809 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002810}
2811
Evan Cheng39623da2006-04-20 08:58:49 +00002812/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2813/// all the same.
2814static bool isSplatVector(SDNode *N) {
2815 if (N->getOpcode() != ISD::BUILD_VECTOR)
2816 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002817
Dan Gohman475871a2008-07-27 21:46:04 +00002818 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002819 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2820 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002821 return false;
2822 return true;
2823}
2824
Evan Cheng213d2cf2007-05-17 18:45:50 +00002825/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
Nate Begeman9008ca62009-04-27 18:41:29 +00002826/// to an zero vector.
Nate Begeman5a5ca152009-04-29 05:20:52 +00002827/// FIXME: move to dag combiner / method on ShuffleVectorSDNode
Nate Begeman9008ca62009-04-27 18:41:29 +00002828static bool isZeroShuffle(ShuffleVectorSDNode *N) {
Dan Gohman475871a2008-07-27 21:46:04 +00002829 SDValue V1 = N->getOperand(0);
2830 SDValue V2 = N->getOperand(1);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002831 unsigned NumElems = N->getValueType(0).getVectorNumElements();
2832 for (unsigned i = 0; i != NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002833 int Idx = N->getMaskElt(i);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002834 if (Idx >= (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002835 unsigned Opc = V2.getOpcode();
Rafael Espindola15684b22009-04-24 12:40:33 +00002836 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
2837 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00002838 if (Opc != ISD::BUILD_VECTOR ||
2839 !X86::isZeroNode(V2.getOperand(Idx-NumElems)))
Nate Begeman9008ca62009-04-27 18:41:29 +00002840 return false;
2841 } else if (Idx >= 0) {
2842 unsigned Opc = V1.getOpcode();
2843 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
2844 continue;
Evan Cheng37b73872009-07-30 08:33:02 +00002845 if (Opc != ISD::BUILD_VECTOR ||
2846 !X86::isZeroNode(V1.getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002847 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002848 }
2849 }
2850 return true;
2851}
2852
2853/// getZeroVector - Returns a vector of specified type with all zero elements.
2854///
Dale Johannesenace16102009-02-03 19:33:06 +00002855static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG,
2856 DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002857 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002858
Chris Lattner8a594482007-11-25 00:24:49 +00002859 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2860 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002861 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002862 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002863 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002864 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002865 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002866 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Evan Chenga87008d2009-02-25 22:49:59 +00002867 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002868 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002869 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chenga87008d2009-02-25 22:49:59 +00002870 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4f32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002871 }
Dale Johannesenace16102009-02-03 19:33:06 +00002872 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002873}
2874
Chris Lattner8a594482007-11-25 00:24:49 +00002875/// getOnesVector - Returns a vector of specified type with all bits set.
2876///
Dale Johannesenace16102009-02-03 19:33:06 +00002877static SDValue getOnesVector(MVT VT, SelectionDAG &DAG, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002878 assert(VT.isVector() && "Expected a vector type");
Scott Michelfdc40a02009-02-17 22:15:04 +00002879
Chris Lattner8a594482007-11-25 00:24:49 +00002880 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2881 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002882 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2883 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002884 if (VT.getSizeInBits() == 64) // MMX
Evan Chenga87008d2009-02-25 22:49:59 +00002885 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v2i32, Cst, Cst);
Chris Lattner8a594482007-11-25 00:24:49 +00002886 else // SSE
Evan Chenga87008d2009-02-25 22:49:59 +00002887 Vec = DAG.getNode(ISD::BUILD_VECTOR, dl, MVT::v4i32, Cst, Cst, Cst, Cst);
Dale Johannesenace16102009-02-03 19:33:06 +00002888 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Vec);
Chris Lattner8a594482007-11-25 00:24:49 +00002889}
2890
2891
Evan Cheng39623da2006-04-20 08:58:49 +00002892/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2893/// that point to V2 points to its first element.
Nate Begeman9008ca62009-04-27 18:41:29 +00002894static SDValue NormalizeMask(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
2895 MVT VT = SVOp->getValueType(0);
Nate Begeman5a5ca152009-04-29 05:20:52 +00002896 unsigned NumElems = VT.getVectorNumElements();
Nate Begeman9008ca62009-04-27 18:41:29 +00002897
Evan Cheng39623da2006-04-20 08:58:49 +00002898 bool Changed = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00002899 SmallVector<int, 8> MaskVec;
2900 SVOp->getMask(MaskVec);
2901
Nate Begeman5a5ca152009-04-29 05:20:52 +00002902 for (unsigned i = 0; i != NumElems; ++i) {
2903 if (MaskVec[i] > (int)NumElems) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002904 MaskVec[i] = NumElems;
2905 Changed = true;
Evan Cheng39623da2006-04-20 08:58:49 +00002906 }
Evan Cheng39623da2006-04-20 08:58:49 +00002907 }
Evan Cheng39623da2006-04-20 08:58:49 +00002908 if (Changed)
Nate Begeman9008ca62009-04-27 18:41:29 +00002909 return DAG.getVectorShuffle(VT, SVOp->getDebugLoc(), SVOp->getOperand(0),
2910 SVOp->getOperand(1), &MaskVec[0]);
2911 return SDValue(SVOp, 0);
Evan Cheng39623da2006-04-20 08:58:49 +00002912}
2913
Evan Cheng017dcc62006-04-21 01:05:10 +00002914/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2915/// operation of specified width.
Nate Begeman9008ca62009-04-27 18:41:29 +00002916static SDValue getMOVL(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2917 SDValue V2) {
2918 unsigned NumElems = VT.getVectorNumElements();
2919 SmallVector<int, 8> Mask;
2920 Mask.push_back(NumElems);
Evan Cheng39623da2006-04-20 08:58:49 +00002921 for (unsigned i = 1; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002922 Mask.push_back(i);
2923 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Cheng39623da2006-04-20 08:58:49 +00002924}
2925
Nate Begeman9008ca62009-04-27 18:41:29 +00002926/// getUnpackl - Returns a vector_shuffle node for an unpackl operation.
2927static SDValue getUnpackl(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2928 SDValue V2) {
2929 unsigned NumElems = VT.getVectorNumElements();
2930 SmallVector<int, 8> Mask;
Evan Chengc575ca22006-04-17 20:43:08 +00002931 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002932 Mask.push_back(i);
2933 Mask.push_back(i + NumElems);
Evan Chengc575ca22006-04-17 20:43:08 +00002934 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002935 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Evan Chengc575ca22006-04-17 20:43:08 +00002936}
2937
Nate Begeman9008ca62009-04-27 18:41:29 +00002938/// getUnpackhMask - Returns a vector_shuffle node for an unpackh operation.
2939static SDValue getUnpackh(SelectionDAG &DAG, DebugLoc dl, MVT VT, SDValue V1,
2940 SDValue V2) {
2941 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng39623da2006-04-20 08:58:49 +00002942 unsigned Half = NumElems/2;
Nate Begeman9008ca62009-04-27 18:41:29 +00002943 SmallVector<int, 8> Mask;
Evan Cheng39623da2006-04-20 08:58:49 +00002944 for (unsigned i = 0; i != Half; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00002945 Mask.push_back(i + Half);
2946 Mask.push_back(i + NumElems + Half);
Evan Cheng39623da2006-04-20 08:58:49 +00002947 }
Nate Begeman9008ca62009-04-27 18:41:29 +00002948 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00002949}
2950
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002951/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Nate Begeman9008ca62009-04-27 18:41:29 +00002952static SDValue PromoteSplat(ShuffleVectorSDNode *SV, SelectionDAG &DAG,
2953 bool HasSSE2) {
2954 if (SV->getValueType(0).getVectorNumElements() <= 4)
2955 return SDValue(SV, 0);
2956
2957 MVT PVT = MVT::v4f32;
2958 MVT VT = SV->getValueType(0);
2959 DebugLoc dl = SV->getDebugLoc();
2960 SDValue V1 = SV->getOperand(0);
2961 int NumElems = VT.getVectorNumElements();
2962 int EltNo = SV->getSplatIndex();
Rafael Espindola15684b22009-04-24 12:40:33 +00002963
Nate Begeman9008ca62009-04-27 18:41:29 +00002964 // unpack elements to the correct location
2965 while (NumElems > 4) {
2966 if (EltNo < NumElems/2) {
2967 V1 = getUnpackl(DAG, dl, VT, V1, V1);
2968 } else {
2969 V1 = getUnpackh(DAG, dl, VT, V1, V1);
2970 EltNo -= NumElems/2;
2971 }
2972 NumElems >>= 1;
2973 }
2974
2975 // Perform the splat.
2976 int SplatMask[4] = { EltNo, EltNo, EltNo, EltNo };
Dale Johannesenace16102009-02-03 19:33:06 +00002977 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, PVT, V1);
Nate Begeman9008ca62009-04-27 18:41:29 +00002978 V1 = DAG.getVectorShuffle(PVT, dl, V1, DAG.getUNDEF(PVT), &SplatMask[0]);
2979 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, V1);
Evan Chengc575ca22006-04-17 20:43:08 +00002980}
2981
Evan Chengba05f722006-04-21 23:03:30 +00002982/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002983/// vector of zero or undef vector. This produces a shuffle where the low
2984/// element of V2 is swizzled into the zero/undef vector, landing at element
2985/// 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 +00002986static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002987 bool isZero, bool HasSSE2,
2988 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002989 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002990 SDValue V1 = isZero
Nate Begeman9008ca62009-04-27 18:41:29 +00002991 ? getZeroVector(VT, HasSSE2, DAG, V2.getDebugLoc()) : DAG.getUNDEF(VT);
2992 unsigned NumElems = VT.getVectorNumElements();
2993 SmallVector<int, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002994 for (unsigned i = 0; i != NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00002995 // If this is the insertion idx, put the low elt of V2 here.
2996 MaskVec.push_back(i == Idx ? NumElems : i);
2997 return DAG.getVectorShuffle(VT, V2.getDebugLoc(), V1, V2, &MaskVec[0]);
Evan Cheng017dcc62006-04-21 01:05:10 +00002998}
2999
Evan Chengf26ffe92008-05-29 08:22:04 +00003000/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3001/// a shuffle that is zero.
3002static
Nate Begeman9008ca62009-04-27 18:41:29 +00003003unsigned getNumOfConsecutiveZeros(ShuffleVectorSDNode *SVOp, int NumElems,
3004 bool Low, SelectionDAG &DAG) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003005 unsigned NumZeros = 0;
Nate Begeman9008ca62009-04-27 18:41:29 +00003006 for (int i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003007 unsigned Index = Low ? i : NumElems-i-1;
Nate Begeman9008ca62009-04-27 18:41:29 +00003008 int Idx = SVOp->getMaskElt(Index);
3009 if (Idx < 0) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003010 ++NumZeros;
3011 continue;
3012 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003013 SDValue Elt = DAG.getShuffleScalarElt(SVOp, Index);
Evan Cheng37b73872009-07-30 08:33:02 +00003014 if (Elt.getNode() && X86::isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003015 ++NumZeros;
3016 else
3017 break;
3018 }
3019 return NumZeros;
3020}
3021
3022/// isVectorShift - Returns true if the shuffle can be implemented as a
3023/// logical left or right shift of a vector.
Nate Begeman9008ca62009-04-27 18:41:29 +00003024/// FIXME: split into pslldqi, psrldqi, palignr variants.
3025static bool isVectorShift(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00003026 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003027 int NumElems = SVOp->getValueType(0).getVectorNumElements();
Evan Chengf26ffe92008-05-29 08:22:04 +00003028
3029 isLeft = true;
Nate Begeman9008ca62009-04-27 18:41:29 +00003030 unsigned NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, true, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003031 if (!NumZeros) {
3032 isLeft = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003033 NumZeros = getNumOfConsecutiveZeros(SVOp, NumElems, false, DAG);
Evan Chengf26ffe92008-05-29 08:22:04 +00003034 if (!NumZeros)
3035 return false;
3036 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003037 bool SeenV1 = false;
3038 bool SeenV2 = false;
Nate Begeman9008ca62009-04-27 18:41:29 +00003039 for (int i = NumZeros; i < NumElems; ++i) {
3040 int Val = isLeft ? (i - NumZeros) : i;
3041 int Idx = SVOp->getMaskElt(isLeft ? i : (i - NumZeros));
3042 if (Idx < 0)
Evan Chengf26ffe92008-05-29 08:22:04 +00003043 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003044 if (Idx < NumElems)
Evan Chengf26ffe92008-05-29 08:22:04 +00003045 SeenV1 = true;
3046 else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003047 Idx -= NumElems;
Evan Chengf26ffe92008-05-29 08:22:04 +00003048 SeenV2 = true;
3049 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003050 if (Idx != Val)
Evan Chengf26ffe92008-05-29 08:22:04 +00003051 return false;
3052 }
3053 if (SeenV1 && SeenV2)
3054 return false;
3055
Nate Begeman9008ca62009-04-27 18:41:29 +00003056 ShVal = SeenV1 ? SVOp->getOperand(0) : SVOp->getOperand(1);
Evan Chengf26ffe92008-05-29 08:22:04 +00003057 ShAmt = NumZeros;
3058 return true;
3059}
3060
3061
Evan Chengc78d3b42006-04-24 18:01:45 +00003062/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3063///
Dan Gohman475871a2008-07-27 21:46:04 +00003064static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003065 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003066 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003067 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003068 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003069
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003070 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003071 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003072 bool First = true;
3073 for (unsigned i = 0; i < 16; ++i) {
3074 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3075 if (ThisIsNonZero && First) {
3076 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003077 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003078 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003079 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003080 First = false;
3081 }
3082
3083 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003084 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003085 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3086 if (LastIsNonZero) {
Scott Michelfdc40a02009-02-17 22:15:04 +00003087 LastElt = DAG.getNode(ISD::ZERO_EXTEND, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003088 MVT::i16, Op.getOperand(i-1));
Evan Chengc78d3b42006-04-24 18:01:45 +00003089 }
3090 if (ThisIsNonZero) {
Dale Johannesenace16102009-02-03 19:33:06 +00003091 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i16, Op.getOperand(i));
3092 ThisElt = DAG.getNode(ISD::SHL, dl, MVT::i16,
Evan Chengc78d3b42006-04-24 18:01:45 +00003093 ThisElt, DAG.getConstant(8, MVT::i8));
3094 if (LastIsNonZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003095 ThisElt = DAG.getNode(ISD::OR, dl, MVT::i16, ThisElt, LastElt);
Evan Chengc78d3b42006-04-24 18:01:45 +00003096 } else
3097 ThisElt = LastElt;
3098
Gabor Greifba36cb52008-08-28 21:40:38 +00003099 if (ThisElt.getNode())
Dale Johannesenace16102009-02-03 19:33:06 +00003100 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003101 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003102 }
3103 }
3104
Dale Johannesenace16102009-02-03 19:33:06 +00003105 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V);
Evan Chengc78d3b42006-04-24 18:01:45 +00003106}
3107
Bill Wendlinga348c562007-03-22 18:42:45 +00003108/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003109///
Dan Gohman475871a2008-07-27 21:46:04 +00003110static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003111 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003112 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003113 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003114 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003115
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003116 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00003117 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003118 bool First = true;
3119 for (unsigned i = 0; i < 8; ++i) {
3120 bool isNonZero = (NonZeros & (1 << i)) != 0;
3121 if (isNonZero) {
3122 if (First) {
3123 if (NumZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003124 V = getZeroVector(MVT::v8i16, true, DAG, dl);
Evan Chengc78d3b42006-04-24 18:01:45 +00003125 else
Dale Johannesene8d72302009-02-06 23:05:02 +00003126 V = DAG.getUNDEF(MVT::v8i16);
Evan Chengc78d3b42006-04-24 18:01:45 +00003127 First = false;
3128 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003129 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003130 MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003131 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003132 }
3133 }
3134
3135 return V;
3136}
3137
Evan Chengf26ffe92008-05-29 08:22:04 +00003138/// getVShift - Return a vector logical shift node.
3139///
Dan Gohman475871a2008-07-27 21:46:04 +00003140static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Nate Begeman9008ca62009-04-27 18:41:29 +00003141 unsigned NumBits, SelectionDAG &DAG,
3142 const TargetLowering &TLI, DebugLoc dl) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003143 bool isMMX = VT.getSizeInBits() == 64;
3144 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003145 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
Dale Johannesenace16102009-02-03 19:33:06 +00003146 SrcOp = DAG.getNode(ISD::BIT_CONVERT, dl, ShVT, SrcOp);
3147 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3148 DAG.getNode(Opc, dl, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003149 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003150}
3151
Dan Gohman475871a2008-07-27 21:46:04 +00003152SDValue
3153X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00003154 DebugLoc dl = Op.getDebugLoc();
Chris Lattner8a594482007-11-25 00:24:49 +00003155 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003156 if (ISD::isBuildVectorAllZeros(Op.getNode())
3157 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003158 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3159 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3160 // eliminated on x86-32 hosts.
3161 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3162 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003163
Gabor Greifba36cb52008-08-28 21:40:38 +00003164 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00003165 return getOnesVector(Op.getValueType(), DAG, dl);
3166 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG, dl);
Chris Lattner8a594482007-11-25 00:24:49 +00003167 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003168
Duncan Sands83ec4b62008-06-06 12:08:01 +00003169 MVT VT = Op.getValueType();
3170 MVT EVT = VT.getVectorElementType();
3171 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003172
3173 unsigned NumElems = Op.getNumOperands();
3174 unsigned NumZero = 0;
3175 unsigned NumNonZero = 0;
3176 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003177 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003178 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003179 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003180 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003181 if (Elt.getOpcode() == ISD::UNDEF)
3182 continue;
3183 Values.insert(Elt);
3184 if (Elt.getOpcode() != ISD::Constant &&
3185 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003186 IsAllConstants = false;
Evan Cheng37b73872009-07-30 08:33:02 +00003187 if (X86::isZeroNode(Elt))
Evan Chengdb2d5242007-12-12 06:45:40 +00003188 NumZero++;
3189 else {
3190 NonZeros |= (1 << i);
3191 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003192 }
3193 }
3194
Dan Gohman7f321562007-06-25 16:23:39 +00003195 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003196 // All undef vector. Return an UNDEF. All zero vectors were handled above.
Dale Johannesene8d72302009-02-06 23:05:02 +00003197 return DAG.getUNDEF(VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003198 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003199
Chris Lattner67f453a2008-03-09 05:42:06 +00003200 // Special case for single non-zero, non-undef, element.
Eli Friedman10415532009-06-06 06:05:10 +00003201 if (NumNonZero == 1) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003202 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003203 SDValue Item = Op.getOperand(Idx);
Scott Michelfdc40a02009-02-17 22:15:04 +00003204
Chris Lattner62098042008-03-09 01:05:04 +00003205 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3206 // the value are obviously zero, truncate the value to i32 and do the
3207 // insertion that way. Only do this if the value is non-constant or if the
3208 // value is a constant being inserted into element 0. It is cheaper to do
3209 // a constant pool load than it is to do a movd + shuffle.
3210 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3211 (!IsAllConstants || Idx == 0)) {
3212 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3213 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003214 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3215 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Scott Michelfdc40a02009-02-17 22:15:04 +00003216
Chris Lattner62098042008-03-09 01:05:04 +00003217 // Truncate the value (which may itself be a constant) to i32, and
3218 // convert it to a vector with movd (S2V+shuffle to zero extend).
Dale Johannesenace16102009-02-03 19:33:06 +00003219 Item = DAG.getNode(ISD::TRUNCATE, dl, MVT::i32, Item);
3220 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003221 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3222 Subtarget->hasSSE2(), DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00003223
Chris Lattner62098042008-03-09 01:05:04 +00003224 // Now we have our 32-bit value zero extended in the low element of
3225 // a vector. If Idx != 0, swizzle it into place.
3226 if (Idx != 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003227 SmallVector<int, 4> Mask;
3228 Mask.push_back(Idx);
3229 for (unsigned i = 1; i != VecElts; ++i)
3230 Mask.push_back(i);
3231 Item = DAG.getVectorShuffle(VecVT, dl, Item,
3232 DAG.getUNDEF(Item.getValueType()),
3233 &Mask[0]);
Chris Lattner62098042008-03-09 01:05:04 +00003234 }
Dale Johannesenace16102009-02-03 19:33:06 +00003235 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(), Item);
Chris Lattner62098042008-03-09 01:05:04 +00003236 }
3237 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003238
Chris Lattner19f79692008-03-08 22:59:52 +00003239 // If we have a constant or non-constant insertion into the low element of
3240 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3241 // the rest of the elements. This will be matched as movd/movq/movss/movsd
Eli Friedman10415532009-06-06 06:05:10 +00003242 // depending on what the source datatype is.
3243 if (Idx == 0) {
3244 if (NumZero == 0) {
3245 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3246 } else if (EVT == MVT::i32 || EVT == MVT::f32 || EVT == MVT::f64 ||
3247 (EVT == MVT::i64 && Subtarget->is64Bit())) {
3248 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
3249 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
3250 return getShuffleVectorZeroOrUndef(Item, 0, true, Subtarget->hasSSE2(),
3251 DAG);
3252 } else if (EVT == MVT::i16 || EVT == MVT::i8) {
3253 Item = DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, Item);
3254 MVT MiddleVT = VT.getSizeInBits() == 64 ? MVT::v2i32 : MVT::v4i32;
3255 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MiddleVT, Item);
3256 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3257 Subtarget->hasSSE2(), DAG);
3258 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, Item);
3259 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003260 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003261
3262 // Is it a vector logical left shift?
3263 if (NumElems == 2 && Idx == 1 &&
Evan Cheng37b73872009-07-30 08:33:02 +00003264 X86::isZeroNode(Op.getOperand(0)) &&
3265 !X86::isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003266 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003267 return getVShift(true, VT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003268 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00003269 VT, Op.getOperand(1)),
Dale Johannesenace16102009-02-03 19:33:06 +00003270 NumBits/2, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00003271 }
Scott Michelfdc40a02009-02-17 22:15:04 +00003272
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003273 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003274 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003275
Chris Lattner19f79692008-03-08 22:59:52 +00003276 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3277 // is a non-constant being inserted into an element other than the low one,
3278 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3279 // movd/movss) to move this into the low element, then shuffle it into
3280 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003281 if (EVTBits == 32) {
Dale Johannesenace16102009-02-03 19:33:06 +00003282 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Item);
Scott Michelfdc40a02009-02-17 22:15:04 +00003283
Evan Cheng0db9fe62006-04-25 20:13:52 +00003284 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003285 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3286 Subtarget->hasSSE2(), DAG);
Nate Begeman9008ca62009-04-27 18:41:29 +00003287 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003288 for (unsigned i = 0; i < NumElems; i++)
Nate Begeman9008ca62009-04-27 18:41:29 +00003289 MaskVec.push_back(i == Idx ? 0 : 1);
3290 return DAG.getVectorShuffle(VT, dl, Item, DAG.getUNDEF(VT), &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003291 }
3292 }
3293
Chris Lattner67f453a2008-03-09 05:42:06 +00003294 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3295 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003296 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00003297
Dan Gohmana3941172007-07-24 22:55:08 +00003298 // A vector full of immediates; various special cases are already
3299 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003300 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003301 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003302
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003303 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003304 if (EVTBits == 64) {
3305 if (NumNonZero == 1) {
3306 // One half is zero or undef.
3307 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dale Johannesenace16102009-02-03 19:33:06 +00003308 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003309 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003310 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3311 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003312 }
Dan Gohman475871a2008-07-27 21:46:04 +00003313 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003314 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003315
3316 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003317 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003318 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003319 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003320 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003321 }
3322
Bill Wendling826f36f2007-03-28 00:57:11 +00003323 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003324 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003325 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003326 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003327 }
3328
3329 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003330 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003331 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003332 if (NumElems == 4 && NumZero > 0) {
3333 for (unsigned i = 0; i < 4; ++i) {
3334 bool isZero = !(NonZeros & (1 << i));
3335 if (isZero)
Dale Johannesenace16102009-02-03 19:33:06 +00003336 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003337 else
Dale Johannesenace16102009-02-03 19:33:06 +00003338 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003339 }
3340
3341 for (unsigned i = 0; i < 2; ++i) {
3342 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3343 default: break;
3344 case 0:
3345 V[i] = V[i*2]; // Must be a zero vector.
3346 break;
3347 case 1:
Nate Begeman9008ca62009-04-27 18:41:29 +00003348 V[i] = getMOVL(DAG, dl, VT, V[i*2+1], V[i*2]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003349 break;
3350 case 2:
Nate Begeman9008ca62009-04-27 18:41:29 +00003351 V[i] = getMOVL(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003352 break;
3353 case 3:
Nate Begeman9008ca62009-04-27 18:41:29 +00003354 V[i] = getUnpackl(DAG, dl, VT, V[i*2], V[i*2+1]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003355 break;
3356 }
3357 }
3358
Nate Begeman9008ca62009-04-27 18:41:29 +00003359 SmallVector<int, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003360 bool Reverse = (NonZeros & 0x3) == 2;
3361 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003362 MaskVec.push_back(Reverse ? 1-i : i);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003363 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3364 for (unsigned i = 0; i < 2; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003365 MaskVec.push_back(Reverse ? 1-i+NumElems : i+NumElems);
3366 return DAG.getVectorShuffle(VT, dl, V[0], V[1], &MaskVec[0]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003367 }
3368
3369 if (Values.size() > 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003370 // If we have SSE 4.1, Expand into a number of inserts unless the number of
3371 // values to be inserted is equal to the number of elements, in which case
3372 // use the unpack code below in the hopes of matching the consecutive elts
3373 // load merge pattern for shuffles.
3374 // FIXME: We could probably just check that here directly.
3375 if (Values.size() < NumElems && VT.getSizeInBits() == 128 &&
3376 getSubtarget()->hasSSE41()) {
3377 V[0] = DAG.getUNDEF(VT);
3378 for (unsigned i = 0; i < NumElems; ++i)
3379 if (Op.getOperand(i).getOpcode() != ISD::UNDEF)
3380 V[0] = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, VT, V[0],
3381 Op.getOperand(i), DAG.getIntPtrConstant(i));
3382 return V[0];
3383 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003384 // Expand into a number of unpckl*.
3385 // e.g. for v4f32
3386 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3387 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3388 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Evan Cheng0db9fe62006-04-25 20:13:52 +00003389 for (unsigned i = 0; i < NumElems; ++i)
Dale Johannesenace16102009-02-03 19:33:06 +00003390 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, Op.getOperand(i));
Evan Cheng0db9fe62006-04-25 20:13:52 +00003391 NumElems >>= 1;
3392 while (NumElems != 0) {
3393 for (unsigned i = 0; i < NumElems; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003394 V[i] = getUnpackl(DAG, dl, VT, V[i], V[i + NumElems]);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003395 NumElems >>= 1;
3396 }
3397 return V[0];
3398 }
3399
Dan Gohman475871a2008-07-27 21:46:04 +00003400 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003401}
3402
Nate Begemanb9a47b82009-02-23 08:49:38 +00003403// v8i16 shuffles - Prefer shuffles in the following order:
3404// 1. [all] pshuflw, pshufhw, optional move
3405// 2. [ssse3] 1 x pshufb
3406// 3. [ssse3] 2 x pshufb + 1 x por
3407// 4. [all] mov + pshuflw + pshufhw + N x (pextrw + pinsrw)
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003408static
Nate Begeman9008ca62009-04-27 18:41:29 +00003409SDValue LowerVECTOR_SHUFFLEv8i16(ShuffleVectorSDNode *SVOp,
3410 SelectionDAG &DAG, X86TargetLowering &TLI) {
3411 SDValue V1 = SVOp->getOperand(0);
3412 SDValue V2 = SVOp->getOperand(1);
3413 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003414 SmallVector<int, 8> MaskVals;
Evan Cheng14b32e12007-12-11 01:46:18 +00003415
Nate Begemanb9a47b82009-02-23 08:49:38 +00003416 // Determine if more than 1 of the words in each of the low and high quadwords
3417 // of the result come from the same quadword of one of the two inputs. Undef
3418 // mask values count as coming from any quadword, for better codegen.
3419 SmallVector<unsigned, 4> LoQuad(4);
3420 SmallVector<unsigned, 4> HiQuad(4);
3421 BitVector InputQuads(4);
3422 for (unsigned i = 0; i < 8; ++i) {
3423 SmallVectorImpl<unsigned> &Quad = i < 4 ? LoQuad : HiQuad;
Nate Begeman9008ca62009-04-27 18:41:29 +00003424 int EltIdx = SVOp->getMaskElt(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003425 MaskVals.push_back(EltIdx);
3426 if (EltIdx < 0) {
3427 ++Quad[0];
3428 ++Quad[1];
3429 ++Quad[2];
3430 ++Quad[3];
Evan Cheng14b32e12007-12-11 01:46:18 +00003431 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003432 }
3433 ++Quad[EltIdx / 4];
3434 InputQuads.set(EltIdx / 4);
Evan Cheng14b32e12007-12-11 01:46:18 +00003435 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003436
Nate Begemanb9a47b82009-02-23 08:49:38 +00003437 int BestLoQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003438 unsigned MaxQuad = 1;
3439 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003440 if (LoQuad[i] > MaxQuad) {
3441 BestLoQuad = i;
3442 MaxQuad = LoQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003443 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003444 }
3445
Nate Begemanb9a47b82009-02-23 08:49:38 +00003446 int BestHiQuad = -1;
Evan Cheng14b32e12007-12-11 01:46:18 +00003447 MaxQuad = 1;
3448 for (unsigned i = 0; i < 4; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003449 if (HiQuad[i] > MaxQuad) {
3450 BestHiQuad = i;
3451 MaxQuad = HiQuad[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003452 }
3453 }
3454
Nate Begemanb9a47b82009-02-23 08:49:38 +00003455 // For SSSE3, If all 8 words of the result come from only 1 quadword of each
3456 // of the two input vectors, shuffle them into one input vector so only a
3457 // single pshufb instruction is necessary. If There are more than 2 input
3458 // quads, disable the next transformation since it does not help SSSE3.
3459 bool V1Used = InputQuads[0] || InputQuads[1];
3460 bool V2Used = InputQuads[2] || InputQuads[3];
3461 if (TLI.getSubtarget()->hasSSSE3()) {
3462 if (InputQuads.count() == 2 && V1Used && V2Used) {
3463 BestLoQuad = InputQuads.find_first();
3464 BestHiQuad = InputQuads.find_next(BestLoQuad);
3465 }
3466 if (InputQuads.count() > 2) {
3467 BestLoQuad = -1;
3468 BestHiQuad = -1;
3469 }
3470 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003471
Nate Begemanb9a47b82009-02-23 08:49:38 +00003472 // If BestLoQuad or BestHiQuad are set, shuffle the quads together and update
3473 // the shuffle mask. If a quad is scored as -1, that means that it contains
3474 // words from all 4 input quadwords.
3475 SDValue NewV;
3476 if (BestLoQuad >= 0 || BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003477 SmallVector<int, 8> MaskV;
3478 MaskV.push_back(BestLoQuad < 0 ? 0 : BestLoQuad);
3479 MaskV.push_back(BestHiQuad < 0 ? 1 : BestHiQuad);
3480 NewV = DAG.getVectorShuffle(MVT::v2i64, dl,
3481 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V1),
3482 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, V2), &MaskV[0]);
Dale Johannesenace16102009-02-03 19:33:06 +00003483 NewV = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003484
Nate Begemanb9a47b82009-02-23 08:49:38 +00003485 // Rewrite the MaskVals and assign NewV to V1 if NewV now contains all the
3486 // source words for the shuffle, to aid later transformations.
3487 bool AllWordsInNewV = true;
Mon P Wang37b9a192009-03-11 06:35:11 +00003488 bool InOrder[2] = { true, true };
Evan Cheng14b32e12007-12-11 01:46:18 +00003489 for (unsigned i = 0; i != 8; ++i) {
Nate Begemanb9a47b82009-02-23 08:49:38 +00003490 int idx = MaskVals[i];
Mon P Wang37b9a192009-03-11 06:35:11 +00003491 if (idx != (int)i)
3492 InOrder[i/4] = false;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003493 if (idx < 0 || (idx/4) == BestLoQuad || (idx/4) == BestHiQuad)
Evan Cheng14b32e12007-12-11 01:46:18 +00003494 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003495 AllWordsInNewV = false;
3496 break;
Evan Cheng14b32e12007-12-11 01:46:18 +00003497 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003498
Nate Begemanb9a47b82009-02-23 08:49:38 +00003499 bool pshuflw = AllWordsInNewV, pshufhw = AllWordsInNewV;
3500 if (AllWordsInNewV) {
3501 for (int i = 0; i != 8; ++i) {
3502 int idx = MaskVals[i];
3503 if (idx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003504 continue;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003505 idx = MaskVals[i] = (idx / 4) == BestLoQuad ? (idx & 3) : (idx & 3) + 4;
3506 if ((idx != i) && idx < 4)
3507 pshufhw = false;
3508 if ((idx != i) && idx > 3)
3509 pshuflw = false;
Evan Cheng14b32e12007-12-11 01:46:18 +00003510 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003511 V1 = NewV;
3512 V2Used = false;
3513 BestLoQuad = 0;
3514 BestHiQuad = 1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003515 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003516
Nate Begemanb9a47b82009-02-23 08:49:38 +00003517 // If we've eliminated the use of V2, and the new mask is a pshuflw or
3518 // pshufhw, that's as cheap as it gets. Return the new shuffle.
Mon P Wang37b9a192009-03-11 06:35:11 +00003519 if ((pshufhw && InOrder[0]) || (pshuflw && InOrder[1])) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003520 return DAG.getVectorShuffle(MVT::v8i16, dl, NewV,
3521 DAG.getUNDEF(MVT::v8i16), &MaskVals[0]);
Evan Cheng14b32e12007-12-11 01:46:18 +00003522 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003523 }
Nate Begemanb9a47b82009-02-23 08:49:38 +00003524
3525 // If we have SSSE3, and all words of the result are from 1 input vector,
3526 // case 2 is generated, otherwise case 3 is generated. If no SSSE3
3527 // is present, fall back to case 4.
3528 if (TLI.getSubtarget()->hasSSSE3()) {
3529 SmallVector<SDValue,16> pshufbMask;
3530
3531 // If we have elements from both input vectors, set the high bit of the
3532 // shuffle mask element to zero out elements that come from V2 in the V1
3533 // mask, and elements that come from V1 in the V2 mask, so that the two
3534 // results can be OR'd together.
3535 bool TwoInputs = V1Used && V2Used;
3536 for (unsigned i = 0; i != 8; ++i) {
3537 int EltIdx = MaskVals[i] * 2;
3538 if (TwoInputs && (EltIdx >= 16)) {
3539 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3540 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3541 continue;
3542 }
3543 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3544 pshufbMask.push_back(DAG.getConstant(EltIdx+1, MVT::i8));
3545 }
3546 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V1);
3547 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003548 DAG.getNode(ISD::BUILD_VECTOR, dl,
3549 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003550 if (!TwoInputs)
3551 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3552
3553 // Calculate the shuffle mask for the second input, shuffle it, and
3554 // OR it with the first shuffled input.
3555 pshufbMask.clear();
3556 for (unsigned i = 0; i != 8; ++i) {
3557 int EltIdx = MaskVals[i] * 2;
3558 if (EltIdx < 16) {
3559 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3560 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3561 continue;
3562 }
3563 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3564 pshufbMask.push_back(DAG.getConstant(EltIdx - 15, MVT::i8));
3565 }
3566 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, V2);
3567 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003568 DAG.getNode(ISD::BUILD_VECTOR, dl,
3569 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003570 V1 = DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3571 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3572 }
3573
3574 // If BestLoQuad >= 0, generate a pshuflw to put the low elements in order,
3575 // and update MaskVals with new element order.
3576 BitVector InOrder(8);
3577 if (BestLoQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003578 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003579 for (int i = 0; i != 4; ++i) {
3580 int idx = MaskVals[i];
3581 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003582 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003583 InOrder.set(i);
3584 } else if ((idx / 4) == BestLoQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003585 MaskV.push_back(idx & 3);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003586 InOrder.set(i);
3587 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003588 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003589 }
3590 }
3591 for (unsigned i = 4; i != 8; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003592 MaskV.push_back(i);
3593 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3594 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003595 }
3596
3597 // If BestHi >= 0, generate a pshufhw to put the high elements in order,
3598 // and update MaskVals with the new element order.
3599 if (BestHiQuad >= 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003600 SmallVector<int, 8> MaskV;
Nate Begemanb9a47b82009-02-23 08:49:38 +00003601 for (unsigned i = 0; i != 4; ++i)
Nate Begeman9008ca62009-04-27 18:41:29 +00003602 MaskV.push_back(i);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003603 for (unsigned i = 4; i != 8; ++i) {
3604 int idx = MaskVals[i];
3605 if (idx < 0) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003606 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003607 InOrder.set(i);
3608 } else if ((idx / 4) == BestHiQuad) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003609 MaskV.push_back((idx & 3) + 4);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003610 InOrder.set(i);
3611 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003612 MaskV.push_back(-1);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003613 }
3614 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003615 NewV = DAG.getVectorShuffle(MVT::v8i16, dl, NewV, DAG.getUNDEF(MVT::v8i16),
3616 &MaskV[0]);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003617 }
3618
3619 // In case BestHi & BestLo were both -1, which means each quadword has a word
3620 // from each of the four input quadwords, calculate the InOrder bitvector now
3621 // before falling through to the insert/extract cleanup.
3622 if (BestLoQuad == -1 && BestHiQuad == -1) {
3623 NewV = V1;
3624 for (int i = 0; i != 8; ++i)
3625 if (MaskVals[i] < 0 || MaskVals[i] == i)
3626 InOrder.set(i);
3627 }
3628
3629 // The other elements are put in the right place using pextrw and pinsrw.
3630 for (unsigned i = 0; i != 8; ++i) {
3631 if (InOrder[i])
3632 continue;
3633 int EltIdx = MaskVals[i];
3634 if (EltIdx < 0)
3635 continue;
3636 SDValue ExtOp = (EltIdx < 8)
3637 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V1,
3638 DAG.getIntPtrConstant(EltIdx))
3639 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, V2,
3640 DAG.getIntPtrConstant(EltIdx - 8));
3641 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, ExtOp,
3642 DAG.getIntPtrConstant(i));
3643 }
3644 return NewV;
3645}
3646
3647// v16i8 shuffles - Prefer shuffles in the following order:
3648// 1. [ssse3] 1 x pshufb
3649// 2. [ssse3] 2 x pshufb + 1 x por
3650// 3. [all] v8i16 shuffle + N x pextrw + rotate + pinsrw
3651static
Nate Begeman9008ca62009-04-27 18:41:29 +00003652SDValue LowerVECTOR_SHUFFLEv16i8(ShuffleVectorSDNode *SVOp,
3653 SelectionDAG &DAG, X86TargetLowering &TLI) {
3654 SDValue V1 = SVOp->getOperand(0);
3655 SDValue V2 = SVOp->getOperand(1);
3656 DebugLoc dl = SVOp->getDebugLoc();
Nate Begemanb9a47b82009-02-23 08:49:38 +00003657 SmallVector<int, 16> MaskVals;
Nate Begeman9008ca62009-04-27 18:41:29 +00003658 SVOp->getMask(MaskVals);
Nate Begemanb9a47b82009-02-23 08:49:38 +00003659
3660 // If we have SSSE3, case 1 is generated when all result bytes come from
3661 // one of the inputs. Otherwise, case 2 is generated. If no SSSE3 is
3662 // present, fall back to case 3.
3663 // FIXME: kill V2Only once shuffles are canonizalized by getNode.
3664 bool V1Only = true;
3665 bool V2Only = true;
3666 for (unsigned i = 0; i < 16; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003667 int EltIdx = MaskVals[i];
Nate Begemanb9a47b82009-02-23 08:49:38 +00003668 if (EltIdx < 0)
3669 continue;
3670 if (EltIdx < 16)
3671 V2Only = false;
3672 else
3673 V1Only = false;
3674 }
3675
3676 // If SSSE3, use 1 pshufb instruction per vector with elements in the result.
3677 if (TLI.getSubtarget()->hasSSSE3()) {
3678 SmallVector<SDValue,16> pshufbMask;
3679
3680 // If all result elements are from one input vector, then only translate
3681 // undef mask values to 0x80 (zero out result) in the pshufb mask.
3682 //
3683 // Otherwise, we have elements from both input vectors, and must zero out
3684 // elements that come from V2 in the first mask, and V1 in the second mask
3685 // so that we can OR them together.
3686 bool TwoInputs = !(V1Only || V2Only);
3687 for (unsigned i = 0; i != 16; ++i) {
3688 int EltIdx = MaskVals[i];
3689 if (EltIdx < 0 || (TwoInputs && EltIdx >= 16)) {
3690 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3691 continue;
3692 }
3693 pshufbMask.push_back(DAG.getConstant(EltIdx, MVT::i8));
3694 }
3695 // If all the elements are from V2, assign it to V1 and return after
3696 // building the first pshufb.
3697 if (V2Only)
3698 V1 = V2;
3699 V1 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V1,
Evan Chenga87008d2009-02-25 22:49:59 +00003700 DAG.getNode(ISD::BUILD_VECTOR, dl,
3701 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003702 if (!TwoInputs)
3703 return V1;
3704
3705 // Calculate the shuffle mask for the second input, shuffle it, and
3706 // OR it with the first shuffled input.
3707 pshufbMask.clear();
3708 for (unsigned i = 0; i != 16; ++i) {
3709 int EltIdx = MaskVals[i];
3710 if (EltIdx < 16) {
3711 pshufbMask.push_back(DAG.getConstant(0x80, MVT::i8));
3712 continue;
3713 }
3714 pshufbMask.push_back(DAG.getConstant(EltIdx - 16, MVT::i8));
3715 }
3716 V2 = DAG.getNode(X86ISD::PSHUFB, dl, MVT::v16i8, V2,
Evan Chenga87008d2009-02-25 22:49:59 +00003717 DAG.getNode(ISD::BUILD_VECTOR, dl,
3718 MVT::v16i8, &pshufbMask[0], 16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003719 return DAG.getNode(ISD::OR, dl, MVT::v16i8, V1, V2);
3720 }
3721
3722 // No SSSE3 - Calculate in place words and then fix all out of place words
3723 // With 0-16 extracts & inserts. Worst case is 16 bytes out of order from
3724 // the 16 different words that comprise the two doublequadword input vectors.
3725 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V1);
3726 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v8i16, V2);
3727 SDValue NewV = V2Only ? V2 : V1;
3728 for (int i = 0; i != 8; ++i) {
3729 int Elt0 = MaskVals[i*2];
3730 int Elt1 = MaskVals[i*2+1];
3731
3732 // This word of the result is all undef, skip it.
3733 if (Elt0 < 0 && Elt1 < 0)
3734 continue;
3735
3736 // This word of the result is already in the correct place, skip it.
3737 if (V1Only && (Elt0 == i*2) && (Elt1 == i*2+1))
3738 continue;
3739 if (V2Only && (Elt0 == i*2+16) && (Elt1 == i*2+17))
3740 continue;
3741
3742 SDValue Elt0Src = Elt0 < 16 ? V1 : V2;
3743 SDValue Elt1Src = Elt1 < 16 ? V1 : V2;
3744 SDValue InsElt;
Mon P Wang6b3ef692009-03-11 18:47:57 +00003745
3746 // If Elt0 and Elt1 are defined, are consecutive, and can be load
3747 // using a single extract together, load it and store it.
3748 if ((Elt0 >= 0) && ((Elt0 + 1) == Elt1) && ((Elt0 & 1) == 0)) {
3749 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3750 DAG.getIntPtrConstant(Elt1 / 2));
3751 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3752 DAG.getIntPtrConstant(i));
3753 continue;
3754 }
3755
Nate Begemanb9a47b82009-02-23 08:49:38 +00003756 // If Elt1 is defined, extract it from the appropriate source. If the
Mon P Wang6b3ef692009-03-11 18:47:57 +00003757 // source byte is not also odd, shift the extracted word left 8 bits
3758 // otherwise clear the bottom 8 bits if we need to do an or.
Nate Begemanb9a47b82009-02-23 08:49:38 +00003759 if (Elt1 >= 0) {
3760 InsElt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16, Elt1Src,
3761 DAG.getIntPtrConstant(Elt1 / 2));
3762 if ((Elt1 & 1) == 0)
3763 InsElt = DAG.getNode(ISD::SHL, dl, MVT::i16, InsElt,
3764 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003765 else if (Elt0 >= 0)
3766 InsElt = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt,
3767 DAG.getConstant(0xFF00, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003768 }
3769 // If Elt0 is defined, extract it from the appropriate source. If the
3770 // source byte is not also even, shift the extracted word right 8 bits. If
3771 // Elt1 was also defined, OR the extracted values together before
3772 // inserting them in the result.
3773 if (Elt0 >= 0) {
3774 SDValue InsElt0 = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i16,
3775 Elt0Src, DAG.getIntPtrConstant(Elt0 / 2));
3776 if ((Elt0 & 1) != 0)
3777 InsElt0 = DAG.getNode(ISD::SRL, dl, MVT::i16, InsElt0,
3778 DAG.getConstant(8, TLI.getShiftAmountTy()));
Mon P Wang6b3ef692009-03-11 18:47:57 +00003779 else if (Elt1 >= 0)
3780 InsElt0 = DAG.getNode(ISD::AND, dl, MVT::i16, InsElt0,
3781 DAG.getConstant(0x00FF, MVT::i16));
Nate Begemanb9a47b82009-02-23 08:49:38 +00003782 InsElt = Elt1 >= 0 ? DAG.getNode(ISD::OR, dl, MVT::i16, InsElt, InsElt0)
3783 : InsElt0;
3784 }
3785 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, dl, MVT::v8i16, NewV, InsElt,
3786 DAG.getIntPtrConstant(i));
3787 }
3788 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v16i8, NewV);
Evan Cheng14b32e12007-12-11 01:46:18 +00003789}
3790
Evan Cheng7a831ce2007-12-15 03:00:47 +00003791/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3792/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3793/// done when every pair / quad of shuffle mask elements point to elements in
3794/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003795/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3796static
Nate Begeman9008ca62009-04-27 18:41:29 +00003797SDValue RewriteAsNarrowerShuffle(ShuffleVectorSDNode *SVOp,
3798 SelectionDAG &DAG,
3799 TargetLowering &TLI, DebugLoc dl) {
3800 MVT VT = SVOp->getValueType(0);
3801 SDValue V1 = SVOp->getOperand(0);
3802 SDValue V2 = SVOp->getOperand(1);
3803 unsigned NumElems = VT.getVectorNumElements();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003804 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003805 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003806 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003807 MVT NewVT = MaskVT;
3808 switch (VT.getSimpleVT()) {
3809 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003810 case MVT::v4f32: NewVT = MVT::v2f64; break;
3811 case MVT::v4i32: NewVT = MVT::v2i64; break;
3812 case MVT::v8i16: NewVT = MVT::v4i32; break;
3813 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003814 }
3815
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003816 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003817 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003818 NewVT = MVT::v2i64;
3819 else
3820 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003821 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003822 int Scale = NumElems / NewWidth;
3823 SmallVector<int, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003824 for (unsigned i = 0; i < NumElems; i += Scale) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003825 int StartIdx = -1;
3826 for (int j = 0; j < Scale; ++j) {
3827 int EltIdx = SVOp->getMaskElt(i+j);
3828 if (EltIdx < 0)
Evan Cheng14b32e12007-12-11 01:46:18 +00003829 continue;
Nate Begeman9008ca62009-04-27 18:41:29 +00003830 if (StartIdx == -1)
Evan Cheng14b32e12007-12-11 01:46:18 +00003831 StartIdx = EltIdx - (EltIdx % Scale);
3832 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003833 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003834 }
Nate Begeman9008ca62009-04-27 18:41:29 +00003835 if (StartIdx == -1)
3836 MaskVec.push_back(-1);
Evan Cheng14b32e12007-12-11 01:46:18 +00003837 else
Nate Begeman9008ca62009-04-27 18:41:29 +00003838 MaskVec.push_back(StartIdx / Scale);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003839 }
3840
Dale Johannesenace16102009-02-03 19:33:06 +00003841 V1 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V1);
3842 V2 = DAG.getNode(ISD::BIT_CONVERT, dl, NewVT, V2);
Nate Begeman9008ca62009-04-27 18:41:29 +00003843 return DAG.getVectorShuffle(NewVT, dl, V1, V2, &MaskVec[0]);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003844}
3845
Evan Chengd880b972008-05-09 21:53:03 +00003846/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003847///
Dan Gohman475871a2008-07-27 21:46:04 +00003848static SDValue getVZextMovL(MVT VT, MVT OpVT,
Nate Begeman9008ca62009-04-27 18:41:29 +00003849 SDValue SrcOp, SelectionDAG &DAG,
3850 const X86Subtarget *Subtarget, DebugLoc dl) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003851 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3852 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003853 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003854 LD = dyn_cast<LoadSDNode>(SrcOp);
3855 if (!LD) {
3856 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3857 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003858 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003859 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3860 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3861 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3862 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3863 // PR2108
3864 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
Dale Johannesenace16102009-02-03 19:33:06 +00003865 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3866 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
3867 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
3868 OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003869 SrcOp.getOperand(0)
3870 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003871 }
3872 }
3873 }
3874
Dale Johannesenace16102009-02-03 19:33:06 +00003875 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
3876 DAG.getNode(X86ISD::VZEXT_MOVL, dl, OpVT,
Scott Michelfdc40a02009-02-17 22:15:04 +00003877 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00003878 OpVT, SrcOp)));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003879}
3880
Evan Chengace3c172008-07-22 21:13:36 +00003881/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3882/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003883static SDValue
Nate Begeman9008ca62009-04-27 18:41:29 +00003884LowerVECTOR_SHUFFLE_4wide(ShuffleVectorSDNode *SVOp, SelectionDAG &DAG) {
3885 SDValue V1 = SVOp->getOperand(0);
3886 SDValue V2 = SVOp->getOperand(1);
3887 DebugLoc dl = SVOp->getDebugLoc();
3888 MVT VT = SVOp->getValueType(0);
3889
Evan Chengace3c172008-07-22 21:13:36 +00003890 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003891 Locs.resize(4);
Nate Begeman9008ca62009-04-27 18:41:29 +00003892 SmallVector<int, 8> Mask1(4U, -1);
3893 SmallVector<int, 8> PermMask;
3894 SVOp->getMask(PermMask);
3895
Evan Chengace3c172008-07-22 21:13:36 +00003896 unsigned NumHi = 0;
3897 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003898 for (unsigned i = 0; i != 4; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003899 int Idx = PermMask[i];
3900 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00003901 Locs[i] = std::make_pair(-1, -1);
3902 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003903 assert(Idx < 8 && "Invalid VECTOR_SHUFFLE index!");
3904 if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003905 Locs[i] = std::make_pair(0, NumLo);
Nate Begeman9008ca62009-04-27 18:41:29 +00003906 Mask1[NumLo] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003907 NumLo++;
3908 } else {
3909 Locs[i] = std::make_pair(1, NumHi);
3910 if (2+NumHi < 4)
Nate Begeman9008ca62009-04-27 18:41:29 +00003911 Mask1[2+NumHi] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003912 NumHi++;
3913 }
3914 }
3915 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003916
Evan Chengace3c172008-07-22 21:13:36 +00003917 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003918 // If no more than two elements come from either vector. This can be
3919 // implemented with two shuffles. First shuffle gather the elements.
3920 // The second shuffle, which takes the first shuffle as both of its
3921 // vector operands, put the elements into the right order.
Nate Begeman9008ca62009-04-27 18:41:29 +00003922 V1 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003923
Nate Begeman9008ca62009-04-27 18:41:29 +00003924 SmallVector<int, 8> Mask2(4U, -1);
3925
Evan Chengace3c172008-07-22 21:13:36 +00003926 for (unsigned i = 0; i != 4; ++i) {
3927 if (Locs[i].first == -1)
3928 continue;
3929 else {
3930 unsigned Idx = (i < 2) ? 0 : 4;
3931 Idx += Locs[i].first * 2 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00003932 Mask2[i] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00003933 }
3934 }
3935
Nate Begeman9008ca62009-04-27 18:41:29 +00003936 return DAG.getVectorShuffle(VT, dl, V1, V1, &Mask2[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003937 } else if (NumLo == 3 || NumHi == 3) {
3938 // Otherwise, we must have three elements from one vector, call it X, and
3939 // one element from the other, call it Y. First, use a shufps to build an
3940 // intermediate vector with the one element from Y and the element from X
3941 // that will be in the same half in the final destination (the indexes don't
3942 // matter). Then, use a shufps to build the final vector, taking the half
3943 // containing the element from Y from the intermediate, and the other half
3944 // from X.
3945 if (NumHi == 3) {
3946 // Normalize it so the 3 elements come from V1.
Nate Begeman9008ca62009-04-27 18:41:29 +00003947 CommuteVectorShuffleMask(PermMask, VT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003948 std::swap(V1, V2);
3949 }
3950
3951 // Find the element from V2.
3952 unsigned HiIndex;
3953 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003954 int Val = PermMask[HiIndex];
3955 if (Val < 0)
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003956 continue;
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003957 if (Val >= 4)
3958 break;
3959 }
3960
Nate Begeman9008ca62009-04-27 18:41:29 +00003961 Mask1[0] = PermMask[HiIndex];
3962 Mask1[1] = -1;
3963 Mask1[2] = PermMask[HiIndex^1];
3964 Mask1[3] = -1;
3965 V2 = DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003966
3967 if (HiIndex >= 2) {
Nate Begeman9008ca62009-04-27 18:41:29 +00003968 Mask1[0] = PermMask[0];
3969 Mask1[1] = PermMask[1];
3970 Mask1[2] = HiIndex & 1 ? 6 : 4;
3971 Mask1[3] = HiIndex & 1 ? 4 : 6;
3972 return DAG.getVectorShuffle(VT, dl, V1, V2, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003973 } else {
Nate Begeman9008ca62009-04-27 18:41:29 +00003974 Mask1[0] = HiIndex & 1 ? 2 : 0;
3975 Mask1[1] = HiIndex & 1 ? 0 : 2;
3976 Mask1[2] = PermMask[2];
3977 Mask1[3] = PermMask[3];
3978 if (Mask1[2] >= 0)
3979 Mask1[2] += 4;
3980 if (Mask1[3] >= 0)
3981 Mask1[3] += 4;
3982 return DAG.getVectorShuffle(VT, dl, V2, V1, &Mask1[0]);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003983 }
Evan Chengace3c172008-07-22 21:13:36 +00003984 }
3985
3986 // Break it into (shuffle shuffle_hi, shuffle_lo).
3987 Locs.clear();
Nate Begeman9008ca62009-04-27 18:41:29 +00003988 SmallVector<int,8> LoMask(4U, -1);
3989 SmallVector<int,8> HiMask(4U, -1);
3990
3991 SmallVector<int,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003992 unsigned MaskIdx = 0;
3993 unsigned LoIdx = 0;
3994 unsigned HiIdx = 2;
3995 for (unsigned i = 0; i != 4; ++i) {
3996 if (i == 2) {
3997 MaskPtr = &HiMask;
3998 MaskIdx = 1;
3999 LoIdx = 0;
4000 HiIdx = 2;
4001 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004002 int Idx = PermMask[i];
4003 if (Idx < 0) {
Evan Chengace3c172008-07-22 21:13:36 +00004004 Locs[i] = std::make_pair(-1, -1);
Nate Begeman9008ca62009-04-27 18:41:29 +00004005 } else if (Idx < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00004006 Locs[i] = std::make_pair(MaskIdx, LoIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004007 (*MaskPtr)[LoIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004008 LoIdx++;
4009 } else {
4010 Locs[i] = std::make_pair(MaskIdx, HiIdx);
Nate Begeman9008ca62009-04-27 18:41:29 +00004011 (*MaskPtr)[HiIdx] = Idx;
Evan Chengace3c172008-07-22 21:13:36 +00004012 HiIdx++;
4013 }
4014 }
4015
Nate Begeman9008ca62009-04-27 18:41:29 +00004016 SDValue LoShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &LoMask[0]);
4017 SDValue HiShuffle = DAG.getVectorShuffle(VT, dl, V1, V2, &HiMask[0]);
4018 SmallVector<int, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00004019 for (unsigned i = 0; i != 4; ++i) {
4020 if (Locs[i].first == -1) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004021 MaskOps.push_back(-1);
Evan Chengace3c172008-07-22 21:13:36 +00004022 } else {
4023 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
Nate Begeman9008ca62009-04-27 18:41:29 +00004024 MaskOps.push_back(Idx);
Evan Chengace3c172008-07-22 21:13:36 +00004025 }
4026 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004027 return DAG.getVectorShuffle(VT, dl, LoShuffle, HiShuffle, &MaskOps[0]);
Evan Chengace3c172008-07-22 21:13:36 +00004028}
4029
Dan Gohman475871a2008-07-27 21:46:04 +00004030SDValue
4031X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004032 ShuffleVectorSDNode *SVOp = cast<ShuffleVectorSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004033 SDValue V1 = Op.getOperand(0);
4034 SDValue V2 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004035 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004036 DebugLoc dl = Op.getDebugLoc();
Nate Begeman9008ca62009-04-27 18:41:29 +00004037 unsigned NumElems = VT.getVectorNumElements();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004038 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004039 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
4040 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00004041 bool V1IsSplat = false;
4042 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004043
Nate Begeman9008ca62009-04-27 18:41:29 +00004044 if (isZeroShuffle(SVOp))
Dale Johannesenace16102009-02-03 19:33:06 +00004045 return getZeroVector(VT, Subtarget->hasSSE2(), DAG, dl);
Evan Cheng213d2cf2007-05-17 18:45:50 +00004046
Nate Begeman9008ca62009-04-27 18:41:29 +00004047 // Promote splats to v4f32.
4048 if (SVOp->isSplat()) {
4049 if (isMMX || NumElems < 4)
4050 return Op;
4051 return PromoteSplat(SVOp, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004052 }
4053
Evan Cheng7a831ce2007-12-15 03:00:47 +00004054 // If the shuffle can be profitably rewritten as a narrower shuffle, then
4055 // do it!
4056 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004057 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004058 if (NewOp.getNode())
Scott Michelfdc40a02009-02-17 22:15:04 +00004059 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
Dale Johannesenace16102009-02-03 19:33:06 +00004060 LowerVECTOR_SHUFFLE(NewOp, DAG));
Evan Cheng7a831ce2007-12-15 03:00:47 +00004061 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4062 // FIXME: Figure out a cleaner way to do this.
4063 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004064 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004065 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
Gabor Greifba36cb52008-08-28 21:40:38 +00004066 if (NewOp.getNode()) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004067 if (isCommutedMOVL(cast<ShuffleVectorSDNode>(NewOp), true, false))
4068 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(0),
4069 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004070 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004071 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004072 SDValue NewOp = RewriteAsNarrowerShuffle(SVOp, DAG, *this, dl);
4073 if (NewOp.getNode() && X86::isMOVLMask(cast<ShuffleVectorSDNode>(NewOp)))
Evan Chengd880b972008-05-09 21:53:03 +00004074 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Nate Begeman9008ca62009-04-27 18:41:29 +00004075 DAG, Subtarget, dl);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004076 }
4077 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004078
4079 if (X86::isPSHUFDMask(SVOp))
4080 return Op;
4081
Evan Chengf26ffe92008-05-29 08:22:04 +00004082 // Check if this can be converted into a logical shift.
4083 bool isLeft = false;
4084 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004085 SDValue ShVal;
Nate Begeman9008ca62009-04-27 18:41:29 +00004086 bool isShift = getSubtarget()->hasSSE2() &&
4087 isVectorShift(SVOp, DAG, isLeft, ShVal, ShAmt);
Evan Chengf26ffe92008-05-29 08:22:04 +00004088 if (isShift && ShVal.hasOneUse()) {
Scott Michelfdc40a02009-02-17 22:15:04 +00004089 // If the shifted value has multiple uses, it may be cheaper to use
Evan Chengf26ffe92008-05-29 08:22:04 +00004090 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004091 MVT EVT = VT.getVectorElementType();
4092 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004093 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004094 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004095
4096 if (X86::isMOVLMask(SVOp)) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004097 if (V1IsUndef)
4098 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004099 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Dale Johannesenace16102009-02-03 19:33:06 +00004100 return getVZextMovL(VT, VT, V2, DAG, Subtarget, dl);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004101 if (!isMMX)
4102 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004103 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004104
4105 // FIXME: fold these into legal mask.
4106 if (!isMMX && (X86::isMOVSHDUPMask(SVOp) ||
4107 X86::isMOVSLDUPMask(SVOp) ||
4108 X86::isMOVHLPSMask(SVOp) ||
4109 X86::isMOVHPMask(SVOp) ||
4110 X86::isMOVLPMask(SVOp)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004111 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004112
Nate Begeman9008ca62009-04-27 18:41:29 +00004113 if (ShouldXformToMOVHLPS(SVOp) ||
4114 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), SVOp))
4115 return CommuteVectorShuffle(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004116
Evan Chengf26ffe92008-05-29 08:22:04 +00004117 if (isShift) {
4118 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004119 MVT EVT = VT.getVectorElementType();
4120 ShAmt *= EVT.getSizeInBits();
Dale Johannesenace16102009-02-03 19:33:06 +00004121 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this, dl);
Evan Chengf26ffe92008-05-29 08:22:04 +00004122 }
Nate Begeman9008ca62009-04-27 18:41:29 +00004123
Evan Cheng9eca5e82006-10-25 21:49:50 +00004124 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004125 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4126 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004127 V1IsSplat = isSplatVector(V1.getNode());
4128 V2IsSplat = isSplatVector(V2.getNode());
Scott Michelfdc40a02009-02-17 22:15:04 +00004129
Chris Lattner8a594482007-11-25 00:24:49 +00004130 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004131 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004132 Op = CommuteVectorShuffle(SVOp, DAG);
4133 SVOp = cast<ShuffleVectorSDNode>(Op);
4134 V1 = SVOp->getOperand(0);
4135 V2 = SVOp->getOperand(1);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004136 std::swap(V1IsSplat, V2IsSplat);
4137 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004138 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004139 }
4140
Nate Begeman9008ca62009-04-27 18:41:29 +00004141 if (isCommutedMOVL(SVOp, V2IsSplat, V2IsUndef)) {
4142 // Shuffling low element of v1 into undef, just return v1.
4143 if (V2IsUndef)
4144 return V1;
4145 // If V2 is a splat, the mask may be malformed such as <4,3,3,3>, which
4146 // the instruction selector will not match, so get a canonical MOVL with
4147 // swapped operands to undo the commute.
4148 return getMOVL(DAG, dl, VT, V2, V1);
Evan Chengd9b8e402006-10-16 06:36:00 +00004149 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004150
Nate Begeman9008ca62009-04-27 18:41:29 +00004151 if (X86::isUNPCKL_v_undef_Mask(SVOp) ||
4152 X86::isUNPCKH_v_undef_Mask(SVOp) ||
4153 X86::isUNPCKLMask(SVOp) ||
4154 X86::isUNPCKHMask(SVOp))
Evan Chengd9b8e402006-10-16 06:36:00 +00004155 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004156
Evan Cheng9bbbb982006-10-25 20:48:19 +00004157 if (V2IsSplat) {
4158 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004159 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004160 // new vector_shuffle with the corrected mask.
Nate Begeman9008ca62009-04-27 18:41:29 +00004161 SDValue NewMask = NormalizeMask(SVOp, DAG);
4162 ShuffleVectorSDNode *NSVOp = cast<ShuffleVectorSDNode>(NewMask);
4163 if (NSVOp != SVOp) {
4164 if (X86::isUNPCKLMask(NSVOp, true)) {
4165 return NewMask;
4166 } else if (X86::isUNPCKHMask(NSVOp, true)) {
4167 return NewMask;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004168 }
4169 }
4170 }
4171
Evan Cheng9eca5e82006-10-25 21:49:50 +00004172 if (Commuted) {
4173 // Commute is back and try unpck* again.
Nate Begeman9008ca62009-04-27 18:41:29 +00004174 // FIXME: this seems wrong.
4175 SDValue NewOp = CommuteVectorShuffle(SVOp, DAG);
4176 ShuffleVectorSDNode *NewSVOp = cast<ShuffleVectorSDNode>(NewOp);
4177 if (X86::isUNPCKL_v_undef_Mask(NewSVOp) ||
4178 X86::isUNPCKH_v_undef_Mask(NewSVOp) ||
4179 X86::isUNPCKLMask(NewSVOp) ||
4180 X86::isUNPCKHMask(NewSVOp))
4181 return NewOp;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004182 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004183
Nate Begemanb9a47b82009-02-23 08:49:38 +00004184 // FIXME: for mmx, bitcast v2i32 to v4i16 for shuffle.
Nate Begeman9008ca62009-04-27 18:41:29 +00004185
4186 // Normalize the node to match x86 shuffle ops if needed
4187 if (!isMMX && V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(SVOp))
4188 return CommuteVectorShuffle(SVOp, DAG);
4189
4190 // Check for legal shuffle and return?
4191 SmallVector<int, 16> PermMask;
4192 SVOp->getMask(PermMask);
4193 if (isShuffleMaskLegal(PermMask, VT))
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004194 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004195
Evan Cheng14b32e12007-12-11 01:46:18 +00004196 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4197 if (VT == MVT::v8i16) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004198 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(SVOp, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004199 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004200 return NewOp;
4201 }
4202
Nate Begemanb9a47b82009-02-23 08:49:38 +00004203 if (VT == MVT::v16i8) {
Nate Begeman9008ca62009-04-27 18:41:29 +00004204 SDValue NewOp = LowerVECTOR_SHUFFLEv16i8(SVOp, DAG, *this);
Nate Begemanb9a47b82009-02-23 08:49:38 +00004205 if (NewOp.getNode())
4206 return NewOp;
4207 }
4208
Evan Chengace3c172008-07-22 21:13:36 +00004209 // Handle all 4 wide cases with a number of shuffles except for MMX.
4210 if (NumElems == 4 && !isMMX)
Nate Begeman9008ca62009-04-27 18:41:29 +00004211 return LowerVECTOR_SHUFFLE_4wide(SVOp, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004212
Dan Gohman475871a2008-07-27 21:46:04 +00004213 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004214}
4215
Dan Gohman475871a2008-07-27 21:46:04 +00004216SDValue
4217X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004218 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004219 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004220 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004221 if (VT.getSizeInBits() == 8) {
Dale Johannesenace16102009-02-03 19:33:06 +00004222 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004223 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004224 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004225 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004226 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004227 } else if (VT.getSizeInBits() == 16) {
Evan Cheng52ceafa2009-01-02 05:29:08 +00004228 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
4229 // If Idx is 0, it's cheaper to do a move instead of a pextrw.
4230 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004231 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4232 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
4233 DAG.getNode(ISD::BIT_CONVERT, dl,
4234 MVT::v4i32,
Evan Cheng52ceafa2009-01-02 05:29:08 +00004235 Op.getOperand(0)),
4236 Op.getOperand(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004237 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004238 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004239 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004240 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004241 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004242 } else if (VT == MVT::f32) {
4243 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4244 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004245 // result has a single use which is a store or a bitcast to i32. And in
4246 // the case of a store, it's not worth it if the index is a constant 0,
4247 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004248 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004249 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004250 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004251 if ((User->getOpcode() != ISD::STORE ||
4252 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4253 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004254 (User->getOpcode() != ISD::BIT_CONVERT ||
4255 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004256 return SDValue();
Dale Johannesenace16102009-02-03 19:33:06 +00004257 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004258 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4i32,
Dale Johannesenace16102009-02-03 19:33:06 +00004259 Op.getOperand(0)),
4260 Op.getOperand(1));
4261 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::f32, Extract);
Mon P Wangf0fcdd82009-01-15 21:10:20 +00004262 } else if (VT == MVT::i32) {
4263 // ExtractPS works with constant index.
4264 if (isa<ConstantSDNode>(Op.getOperand(1)))
4265 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004266 }
Dan Gohman475871a2008-07-27 21:46:04 +00004267 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004268}
4269
4270
Dan Gohman475871a2008-07-27 21:46:04 +00004271SDValue
4272X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004273 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004274 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004275
Evan Cheng62a3f152008-03-24 21:52:23 +00004276 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004277 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004278 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004279 return Res;
4280 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004281
Duncan Sands83ec4b62008-06-06 12:08:01 +00004282 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004283 DebugLoc dl = Op.getDebugLoc();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004284 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004285 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004286 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004287 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004288 if (Idx == 0)
Dale Johannesenace16102009-02-03 19:33:06 +00004289 return DAG.getNode(ISD::TRUNCATE, dl, MVT::i16,
4290 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::i32,
Scott Michelfdc40a02009-02-17 22:15:04 +00004291 DAG.getNode(ISD::BIT_CONVERT, dl,
Dale Johannesenace16102009-02-03 19:33:06 +00004292 MVT::v4i32, Vec),
Evan Cheng14b32e12007-12-11 01:46:18 +00004293 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004294 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004295 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dale Johannesenace16102009-02-03 19:33:06 +00004296 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, dl, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004297 Op.getOperand(0), Op.getOperand(1));
Dale Johannesenace16102009-02-03 19:33:06 +00004298 SDValue Assert = DAG.getNode(ISD::AssertZext, dl, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004299 DAG.getValueType(VT));
Dale Johannesenace16102009-02-03 19:33:06 +00004300 return DAG.getNode(ISD::TRUNCATE, dl, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004301 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004302 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004303 if (Idx == 0)
4304 return Op;
Nate Begeman9008ca62009-04-27 18:41:29 +00004305
Evan Cheng0db9fe62006-04-25 20:13:52 +00004306 // SHUFPS the element to the lowest double word, then movss.
Nate Begeman9008ca62009-04-27 18:41:29 +00004307 int Mask[4] = { Idx, -1, -1, -1 };
4308 MVT VVT = Op.getOperand(0).getValueType();
4309 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4310 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004311 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004312 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004313 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004314 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4315 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4316 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004317 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004318 if (Idx == 0)
4319 return Op;
4320
4321 // UNPCKHPD the element to the lowest double word, then movsd.
4322 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4323 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Nate Begeman9008ca62009-04-27 18:41:29 +00004324 int Mask[2] = { 1, -1 };
4325 MVT VVT = Op.getOperand(0).getValueType();
4326 SDValue Vec = DAG.getVectorShuffle(VVT, dl, Op.getOperand(0),
4327 DAG.getUNDEF(VVT), Mask);
Dale Johannesenace16102009-02-03 19:33:06 +00004328 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004329 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004330 }
4331
Dan Gohman475871a2008-07-27 21:46:04 +00004332 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004333}
4334
Dan Gohman475871a2008-07-27 21:46:04 +00004335SDValue
4336X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004337 MVT VT = Op.getValueType();
4338 MVT EVT = VT.getVectorElementType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004339 DebugLoc dl = Op.getDebugLoc();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004340
Dan Gohman475871a2008-07-27 21:46:04 +00004341 SDValue N0 = Op.getOperand(0);
4342 SDValue N1 = Op.getOperand(1);
4343 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004344
Dan Gohmanef521f12008-08-14 22:53:18 +00004345 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4346 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004347 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begemanb9a47b82009-02-23 08:49:38 +00004348 : X86ISD::PINSRW;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004349 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4350 // argument.
4351 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004352 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004353 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004354 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004355 return DAG.getNode(Opc, dl, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004356 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004357 // Bits [7:6] of the constant are the source select. This will always be
4358 // zero here. The DAG Combiner may combine an extract_elt index into these
4359 // bits. For example (insert (extract, 3), 2) could be matched by putting
4360 // the '3' into bits [7:6] of X86ISD::INSERTPS.
Scott Michelfdc40a02009-02-17 22:15:04 +00004361 // Bits [5:4] of the constant are the destination select. This is the
Nate Begeman14d12ca2008-02-11 04:19:36 +00004362 // value of the incoming immediate.
Scott Michelfdc40a02009-02-17 22:15:04 +00004363 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
Nate Begeman14d12ca2008-02-11 04:19:36 +00004364 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004365 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Eric Christopherfbd66872009-07-24 00:33:09 +00004366 // Create this as a scalar to vector..
4367 N1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4f32, N1);
Dale Johannesenace16102009-02-03 19:33:06 +00004368 return DAG.getNode(X86ISD::INSERTPS, dl, VT, N0, N1, N2);
Eric Christopherfbd66872009-07-24 00:33:09 +00004369 } else if (EVT == MVT::i32 && isa<ConstantSDNode>(N2)) {
4370 // PINSR* works with constant index.
4371 return Op;
Nate Begeman14d12ca2008-02-11 04:19:36 +00004372 }
Dan Gohman475871a2008-07-27 21:46:04 +00004373 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004374}
4375
Dan Gohman475871a2008-07-27 21:46:04 +00004376SDValue
4377X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004378 MVT VT = Op.getValueType();
4379 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004380
4381 if (Subtarget->hasSSE41())
4382 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4383
Evan Cheng794405e2007-12-12 07:55:34 +00004384 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004385 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004386
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004387 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00004388 SDValue N0 = Op.getOperand(0);
4389 SDValue N1 = Op.getOperand(1);
4390 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004391
Eli Friedman30e71eb2009-06-06 06:32:50 +00004392 if (EVT.getSizeInBits() == 16 && isa<ConstantSDNode>(N2)) {
Evan Cheng794405e2007-12-12 07:55:34 +00004393 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4394 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004395 if (N1.getValueType() != MVT::i32)
Dale Johannesenace16102009-02-03 19:33:06 +00004396 N1 = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, N1);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004397 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004398 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Dale Johannesenace16102009-02-03 19:33:06 +00004399 return DAG.getNode(X86ISD::PINSRW, dl, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004400 }
Dan Gohman475871a2008-07-27 21:46:04 +00004401 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004402}
4403
Dan Gohman475871a2008-07-27 21:46:04 +00004404SDValue
4405X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004406 DebugLoc dl = Op.getDebugLoc();
Evan Cheng52672b82008-07-22 18:39:19 +00004407 if (Op.getValueType() == MVT::v2f32)
Dale Johannesenace16102009-02-03 19:33:06 +00004408 return DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f32,
4409 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2i32,
4410 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::i32,
Evan Cheng52672b82008-07-22 18:39:19 +00004411 Op.getOperand(0))));
4412
Rafael Espindoladef390a2009-08-03 02:45:34 +00004413 if (Op.getValueType() == MVT::v1i64 && Op.getOperand(0).getValueType() == MVT::i64)
Rafael Espindolacc2b67a2009-08-03 03:00:05 +00004414 return DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v1i64, Op.getOperand(0));
Rafael Espindoladef390a2009-08-03 02:45:34 +00004415
Dale Johannesenace16102009-02-03 19:33:06 +00004416 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004417 MVT VT = MVT::v2i32;
4418 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004419 default: break;
4420 case MVT::v16i8:
4421 case MVT::v8i16:
4422 VT = MVT::v4i32;
4423 break;
4424 }
Dale Johannesenace16102009-02-03 19:33:06 +00004425 return DAG.getNode(ISD::BIT_CONVERT, dl, Op.getValueType(),
4426 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004427}
4428
Bill Wendling056292f2008-09-16 21:48:12 +00004429// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4430// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4431// one of the above mentioned nodes. It has to be wrapped because otherwise
4432// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4433// be used to form addressing mode. These wrapped nodes will be selected
4434// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004435SDValue
4436X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004437 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Chris Lattner41621a22009-06-26 19:22:52 +00004438
4439 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4440 // global base reg.
4441 unsigned char OpFlag = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004442 unsigned WrapperKind = X86ISD::Wrapper;
Chris Lattnere4df7562009-07-09 03:15:51 +00004443
Chris Lattner4f066492009-07-11 20:29:19 +00004444 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004445 getTargetMachine().getCodeModel() == CodeModel::Small)
Chris Lattnere4df7562009-07-09 03:15:51 +00004446 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004447 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004448 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004449 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004450 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner41621a22009-06-26 19:22:52 +00004451
Evan Cheng1606e8e2009-03-13 07:51:59 +00004452 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(), getPointerTy(),
Chris Lattner41621a22009-06-26 19:22:52 +00004453 CP->getAlignment(),
4454 CP->getOffset(), OpFlag);
4455 DebugLoc DL = CP->getDebugLoc();
Chris Lattner18c59872009-06-27 04:16:01 +00004456 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004457 // With PIC, the address is actually $g + Offset.
Chris Lattner41621a22009-06-26 19:22:52 +00004458 if (OpFlag) {
4459 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004460 DAG.getNode(X86ISD::GlobalBaseReg,
Chris Lattner41621a22009-06-26 19:22:52 +00004461 DebugLoc::getUnknownLoc(), getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004462 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004463 }
4464
4465 return Result;
4466}
4467
Chris Lattner18c59872009-06-27 04:16:01 +00004468SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
4469 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
4470
4471 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4472 // global base reg.
4473 unsigned char OpFlag = 0;
4474 unsigned WrapperKind = X86ISD::Wrapper;
Chris Lattnere4df7562009-07-09 03:15:51 +00004475
Chris Lattner4f066492009-07-11 20:29:19 +00004476 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004477 getTargetMachine().getCodeModel() == CodeModel::Small)
Chris Lattnere4df7562009-07-09 03:15:51 +00004478 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004479 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004480 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004481 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004482 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner18c59872009-06-27 04:16:01 +00004483
4484 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy(),
4485 OpFlag);
4486 DebugLoc DL = JT->getDebugLoc();
4487 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4488
4489 // With PIC, the address is actually $g + Offset.
4490 if (OpFlag) {
4491 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4492 DAG.getNode(X86ISD::GlobalBaseReg,
4493 DebugLoc::getUnknownLoc(), getPointerTy()),
4494 Result);
4495 }
4496
4497 return Result;
4498}
4499
4500SDValue
4501X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
4502 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4503
4504 // In PIC mode (unless we're in RIPRel PIC mode) we add an offset to the
4505 // global base reg.
4506 unsigned char OpFlag = 0;
4507 unsigned WrapperKind = X86ISD::Wrapper;
Chris Lattner4f066492009-07-11 20:29:19 +00004508 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004509 getTargetMachine().getCodeModel() == CodeModel::Small)
Chris Lattnere4df7562009-07-09 03:15:51 +00004510 WrapperKind = X86ISD::WrapperRIP;
Chris Lattner3b67e9b2009-07-10 20:47:30 +00004511 else if (Subtarget->isPICStyleGOT())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004512 OpFlag = X86II::MO_GOTOFF;
Chris Lattnere2c92082009-07-10 21:00:45 +00004513 else if (Subtarget->isPICStyleStubPIC())
Chris Lattner88e1fd52009-07-09 04:24:46 +00004514 OpFlag = X86II::MO_PIC_BASE_OFFSET;
Chris Lattner18c59872009-06-27 04:16:01 +00004515
4516 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy(), OpFlag);
4517
4518 DebugLoc DL = Op.getDebugLoc();
4519 Result = DAG.getNode(WrapperKind, DL, getPointerTy(), Result);
4520
4521
4522 // With PIC, the address is actually $g + Offset.
4523 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
Chris Lattnere4df7562009-07-09 03:15:51 +00004524 !Subtarget->is64Bit()) {
Chris Lattner18c59872009-06-27 04:16:01 +00004525 Result = DAG.getNode(ISD::ADD, DL, getPointerTy(),
4526 DAG.getNode(X86ISD::GlobalBaseReg,
4527 DebugLoc::getUnknownLoc(),
4528 getPointerTy()),
4529 Result);
4530 }
4531
4532 return Result;
4533}
4534
Dan Gohman475871a2008-07-27 21:46:04 +00004535SDValue
Dale Johannesen33c960f2009-02-04 20:06:27 +00004536X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV, DebugLoc dl,
Dan Gohman6520e202008-10-18 02:06:02 +00004537 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004538 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004539 // Create the TargetGlobalAddress node, folding in the constant
4540 // offset if it is legal.
Chris Lattnerd392bd92009-07-10 07:20:05 +00004541 unsigned char OpFlags =
4542 Subtarget->ClassifyGlobalReference(GV, getTargetMachine());
Dan Gohman6520e202008-10-18 02:06:02 +00004543 SDValue Result;
Chris Lattner36c25012009-07-10 07:34:39 +00004544 if (OpFlags == X86II::MO_NO_FLAG && isInt32(Offset)) {
Chris Lattner4aa21aa2009-07-09 00:58:53 +00004545 // A direct static reference to a global.
Dale Johannesen60b3ba02009-07-21 00:12:29 +00004546 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
Dan Gohman6520e202008-10-18 02:06:02 +00004547 Offset = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004548 } else {
Chris Lattnerb1acd682009-06-27 05:39:56 +00004549 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0, OpFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004550 }
4551
Chris Lattner4f066492009-07-11 20:29:19 +00004552 if (Subtarget->isPICStyleRIPRel() &&
Chris Lattner18c59872009-06-27 04:16:01 +00004553 getTargetMachine().getCodeModel() == CodeModel::Small)
4554 Result = DAG.getNode(X86ISD::WrapperRIP, dl, getPointerTy(), Result);
4555 else
4556 Result = DAG.getNode(X86ISD::Wrapper, dl, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004557
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004558 // With PIC, the address is actually $g + Offset.
Chris Lattner36c25012009-07-10 07:34:39 +00004559 if (isGlobalRelativeToPICBase(OpFlags)) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004560 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(),
4561 DAG.getNode(X86ISD::GlobalBaseReg, dl, getPointerTy()),
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004562 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004564
Chris Lattner36c25012009-07-10 07:34:39 +00004565 // For globals that require a load from a stub to get the address, emit the
4566 // load.
4567 if (isGlobalStubReference(OpFlags))
Dale Johannesen33c960f2009-02-04 20:06:27 +00004568 Result = DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004569 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004570
Dan Gohman6520e202008-10-18 02:06:02 +00004571 // If there was a non-zero offset that we didn't fold, create an explicit
4572 // addition for it.
4573 if (Offset != 0)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004574 Result = DAG.getNode(ISD::ADD, dl, getPointerTy(), Result,
Dan Gohman6520e202008-10-18 02:06:02 +00004575 DAG.getConstant(Offset, getPointerTy()));
4576
Evan Cheng0db9fe62006-04-25 20:13:52 +00004577 return Result;
4578}
4579
Evan Chengda43bcf2008-09-24 00:05:32 +00004580SDValue
4581X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4582 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004583 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004584 return LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004585}
4586
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004587static SDValue
4588GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
Chris Lattnerb903bed2009-06-26 21:20:29 +00004589 SDValue *InFlag, const MVT PtrVT, unsigned ReturnReg,
4590 unsigned char OperandFlags) {
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004591 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
4592 DebugLoc dl = GA->getDebugLoc();
4593 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
4594 GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004595 GA->getOffset(),
4596 OperandFlags);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004597 if (InFlag) {
4598 SDValue Ops[] = { Chain, TGA, *InFlag };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004599 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004600 } else {
4601 SDValue Ops[] = { Chain, TGA };
Rafael Espindola15f1b662009-04-24 12:59:40 +00004602 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004603 }
Rafael Espindola15f1b662009-04-24 12:59:40 +00004604 SDValue Flag = Chain.getValue(1);
4605 return DAG.getCopyFromReg(Chain, dl, ReturnReg, PtrVT, Flag);
Rafael Espindola2ee3db32009-04-17 14:35:58 +00004606}
4607
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004608// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004609static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004610LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004611 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004612 SDValue InFlag;
Dale Johannesendd64c412009-02-04 00:33:20 +00004613 DebugLoc dl = GA->getDebugLoc(); // ? function entry point might be better
4614 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004615 DAG.getNode(X86ISD::GlobalBaseReg,
Dale Johannesenb300d2a2009-02-07 00:55:49 +00004616 DebugLoc::getUnknownLoc(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004617 PtrVT), InFlag);
4618 InFlag = Chain.getValue(1);
4619
Chris Lattnerb903bed2009-06-26 21:20:29 +00004620 return GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX, X86II::MO_TLSGD);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004621}
4622
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004623// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004624static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004625LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004626 const MVT PtrVT) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004627 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
4628 X86::RAX, X86II::MO_TLSGD);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004629}
4630
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004631// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4632// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004633static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004634 const MVT PtrVT, TLSModel::Model model,
4635 bool is64Bit) {
Dale Johannesen33c960f2009-02-04 20:06:27 +00004636 DebugLoc dl = GA->getDebugLoc();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004637 // Get the Thread Pointer
Rafael Espindola094fad32009-04-08 21:14:34 +00004638 SDValue Base = DAG.getNode(X86ISD::SegmentBaseAddress,
4639 DebugLoc::getUnknownLoc(), PtrVT,
Rafael Espindola7ff5bff2009-04-13 13:02:49 +00004640 DAG.getRegister(is64Bit? X86::FS : X86::GS,
4641 MVT::i32));
Rafael Espindola094fad32009-04-08 21:14:34 +00004642
4643 SDValue ThreadPointer = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Base,
4644 NULL, 0);
4645
Chris Lattnerb903bed2009-06-26 21:20:29 +00004646 unsigned char OperandFlags = 0;
Chris Lattner18c59872009-06-27 04:16:01 +00004647 // Most TLS accesses are not RIP relative, even on x86-64. One exception is
4648 // initialexec.
4649 unsigned WrapperKind = X86ISD::Wrapper;
4650 if (model == TLSModel::LocalExec) {
Chris Lattnerb903bed2009-06-26 21:20:29 +00004651 OperandFlags = is64Bit ? X86II::MO_TPOFF : X86II::MO_NTPOFF;
Chris Lattner18c59872009-06-27 04:16:01 +00004652 } else if (is64Bit) {
4653 assert(model == TLSModel::InitialExec);
4654 OperandFlags = X86II::MO_GOTTPOFF;
4655 WrapperKind = X86ISD::WrapperRIP;
4656 } else {
4657 assert(model == TLSModel::InitialExec);
4658 OperandFlags = X86II::MO_INDNTPOFF;
Chris Lattnerb903bed2009-06-26 21:20:29 +00004659 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004660
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004661 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4662 // exec)
Chris Lattner4150c082009-06-21 02:22:34 +00004663 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
Chris Lattnerb903bed2009-06-26 21:20:29 +00004664 GA->getOffset(), OperandFlags);
Chris Lattner18c59872009-06-27 04:16:01 +00004665 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004666
Rafael Espindola9a580232009-02-27 13:37:18 +00004667 if (model == TLSModel::InitialExec)
Dale Johannesen33c960f2009-02-04 20:06:27 +00004668 Offset = DAG.getLoad(PtrVT, dl, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004669 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004670
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004671 // The address of the thread local variable is the add of the thread
4672 // pointer with the offset of the variable.
Dale Johannesen33c960f2009-02-04 20:06:27 +00004673 return DAG.getNode(ISD::ADD, dl, PtrVT, ThreadPointer, Offset);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004674}
4675
Dan Gohman475871a2008-07-27 21:46:04 +00004676SDValue
4677X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004678 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004679 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004680 assert(Subtarget->isTargetELF() &&
4681 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004682 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
Chris Lattnerb903bed2009-06-26 21:20:29 +00004683 const GlobalValue *GV = GA->getGlobal();
4684
4685 // If GV is an alias then use the aliasee for determining
4686 // thread-localness.
4687 if (const GlobalAlias *GA = dyn_cast<GlobalAlias>(GV))
4688 GV = GA->resolveAliasedGlobal(false);
4689
4690 TLSModel::Model model = getTLSModel(GV,
4691 getTargetMachine().getRelocationModel());
4692
4693 switch (model) {
4694 case TLSModel::GeneralDynamic:
4695 case TLSModel::LocalDynamic: // not implemented
4696 if (Subtarget->is64Bit())
Rafael Espindola9a580232009-02-27 13:37:18 +00004697 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
Chris Lattnerb903bed2009-06-26 21:20:29 +00004698 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4699
4700 case TLSModel::InitialExec:
4701 case TLSModel::LocalExec:
4702 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
4703 Subtarget->is64Bit());
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004704 }
Chris Lattnerb903bed2009-06-26 21:20:29 +00004705
Torok Edwinc23197a2009-07-14 16:55:14 +00004706 llvm_unreachable("Unreachable");
Chris Lattner5867de12009-04-01 22:14:45 +00004707 return SDValue();
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004708}
4709
Evan Cheng0db9fe62006-04-25 20:13:52 +00004710
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004711/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
Scott Michelfdc40a02009-02-17 22:15:04 +00004712/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004713SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004714 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004715 MVT VT = Op.getValueType();
4716 unsigned VTBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004717 DebugLoc dl = Op.getDebugLoc();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004718 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004719 SDValue ShOpLo = Op.getOperand(0);
4720 SDValue ShOpHi = Op.getOperand(1);
4721 SDValue ShAmt = Op.getOperand(2);
Chris Lattner31dcfe62009-07-29 05:48:09 +00004722 SDValue Tmp1 = isSRA ? DAG.getNode(ISD::SRA, dl, VT, ShOpHi,
4723 DAG.getConstant(VTBits - 1, MVT::i8))
4724 : DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004725
Dan Gohman475871a2008-07-27 21:46:04 +00004726 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004727 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004728 Tmp2 = DAG.getNode(X86ISD::SHLD, dl, VT, ShOpHi, ShOpLo, ShAmt);
4729 Tmp3 = DAG.getNode(ISD::SHL, dl, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004730 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004731 Tmp2 = DAG.getNode(X86ISD::SHRD, dl, VT, ShOpLo, ShOpHi, ShAmt);
4732 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, dl, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004733 }
Evan Chenge3413162006-01-09 18:33:28 +00004734
Dale Johannesenace16102009-02-03 19:33:06 +00004735 SDValue AndNode = DAG.getNode(ISD::AND, dl, MVT::i8, ShAmt,
Chris Lattner31dcfe62009-07-29 05:48:09 +00004736 DAG.getConstant(VTBits, MVT::i8));
Dale Johannesenace16102009-02-03 19:33:06 +00004737 SDValue Cond = DAG.getNode(X86ISD::CMP, dl, VT,
Chris Lattner31dcfe62009-07-29 05:48:09 +00004738 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004739
Dan Gohman475871a2008-07-27 21:46:04 +00004740 SDValue Hi, Lo;
4741 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4742 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4743 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004744
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004745 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dale Johannesenace16102009-02-03 19:33:06 +00004746 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4747 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004748 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00004749 Lo = DAG.getNode(X86ISD::CMOV, dl, VT, Ops0, 4);
4750 Hi = DAG.getNode(X86ISD::CMOV, dl, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004751 }
4752
Dan Gohman475871a2008-07-27 21:46:04 +00004753 SDValue Ops[2] = { Lo, Hi };
Dale Johannesenace16102009-02-03 19:33:06 +00004754 return DAG.getMergeValues(Ops, 2, dl);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004755}
Evan Chenga3195e82006-01-12 22:54:21 +00004756
Dan Gohman475871a2008-07-27 21:46:04 +00004757SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004758 MVT SrcVT = Op.getOperand(0).getValueType();
Eli Friedman23ef1052009-06-06 03:57:58 +00004759
4760 if (SrcVT.isVector()) {
4761 if (SrcVT == MVT::v2i32 && Op.getValueType() == MVT::v2f64) {
4762 return Op;
4763 }
4764 return SDValue();
4765 }
4766
Duncan Sands8e4eb092008-06-08 20:54:56 +00004767 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004768 "Unknown SINT_TO_FP to lower!");
Scott Michelfdc40a02009-02-17 22:15:04 +00004769
Eli Friedman36df4992009-05-27 00:47:34 +00004770 // These are really Legal; return the operand so the caller accepts it as
4771 // Legal.
Chris Lattnerb09916b2008-02-27 05:57:41 +00004772 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Eli Friedman36df4992009-05-27 00:47:34 +00004773 return Op;
4774 if (SrcVT == MVT::i64 && isScalarFPTypeInSSEReg(Op.getValueType()) &&
4775 Subtarget->is64Bit()) {
4776 return Op;
4777 }
Scott Michelfdc40a02009-02-17 22:15:04 +00004778
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004779 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004780 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004781 MachineFunction &MF = DAG.getMachineFunction();
4782 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004783 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Dale Johannesenace16102009-02-03 19:33:06 +00004784 SDValue Chain = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
Bill Wendling105be5a2009-03-13 08:41:47 +00004785 StackSlot,
4786 PseudoSourceValue::getFixedStack(SSFI), 0);
Eli Friedman948e95a2009-05-23 09:59:16 +00004787 return BuildFILD(Op, SrcVT, Chain, StackSlot, DAG);
4788}
Evan Cheng0db9fe62006-04-25 20:13:52 +00004789
Eli Friedman948e95a2009-05-23 09:59:16 +00004790SDValue X86TargetLowering::BuildFILD(SDValue Op, MVT SrcVT, SDValue Chain,
4791 SDValue StackSlot,
4792 SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004793 // Build the FILD
Eli Friedman948e95a2009-05-23 09:59:16 +00004794 DebugLoc dl = Op.getDebugLoc();
Chris Lattner5a88b832007-02-25 07:10:00 +00004795 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004796 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004797 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004798 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4799 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004800 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004801 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004802 Ops.push_back(Chain);
4803 Ops.push_back(StackSlot);
4804 Ops.push_back(DAG.getValueType(SrcVT));
Dale Johannesenace16102009-02-03 19:33:06 +00004805 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD, dl,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004806 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004807
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004808 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004809 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004810 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004811
4812 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4813 // shouldn't be necessary except that RFP cannot be live across
4814 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004815 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004816 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004818 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004819 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004820 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004821 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004822 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004823 Ops.push_back(DAG.getValueType(Op.getValueType()));
4824 Ops.push_back(InFlag);
Dale Johannesenace16102009-02-03 19:33:06 +00004825 Chain = DAG.getNode(X86ISD::FST, dl, Tys, &Ops[0], Ops.size());
4826 Result = DAG.getLoad(Op.getValueType(), dl, Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004827 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004828 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004829
Evan Cheng0db9fe62006-04-25 20:13:52 +00004830 return Result;
4831}
4832
Bill Wendling8b8a6362009-01-17 03:56:04 +00004833// LowerUINT_TO_FP_i64 - 64-bit unsigned integer to double expansion.
4834SDValue X86TargetLowering::LowerUINT_TO_FP_i64(SDValue Op, SelectionDAG &DAG) {
4835 // This algorithm is not obvious. Here it is in C code, more or less:
4836 /*
4837 double uint64_to_double( uint32_t hi, uint32_t lo ) {
4838 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4839 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
Dale Johannesen040225f2008-10-21 23:07:49 +00004840
Bill Wendling8b8a6362009-01-17 03:56:04 +00004841 // Copy ints to xmm registers.
4842 __m128i xh = _mm_cvtsi32_si128( hi );
4843 __m128i xl = _mm_cvtsi32_si128( lo );
Dale Johannesen040225f2008-10-21 23:07:49 +00004844
Bill Wendling8b8a6362009-01-17 03:56:04 +00004845 // Combine into low half of a single xmm register.
4846 __m128i x = _mm_unpacklo_epi32( xh, xl );
4847 __m128d d;
4848 double sd;
Dale Johannesen040225f2008-10-21 23:07:49 +00004849
Bill Wendling8b8a6362009-01-17 03:56:04 +00004850 // Merge in appropriate exponents to give the integer bits the right
4851 // magnitude.
4852 x = _mm_unpacklo_epi32( x, exp );
Dale Johannesen040225f2008-10-21 23:07:49 +00004853
Bill Wendling8b8a6362009-01-17 03:56:04 +00004854 // Subtract away the biases to deal with the IEEE-754 double precision
4855 // implicit 1.
4856 d = _mm_sub_pd( (__m128d) x, bias );
Dale Johannesen040225f2008-10-21 23:07:49 +00004857
Bill Wendling8b8a6362009-01-17 03:56:04 +00004858 // All conversions up to here are exact. The correctly rounded result is
4859 // calculated using the current rounding mode using the following
4860 // horizontal add.
4861 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4862 _mm_store_sd( &sd, d ); // Because we are returning doubles in XMM, this
4863 // store doesn't really need to be here (except
4864 // maybe to zero the other double)
4865 return sd;
4866 }
4867 */
Dale Johannesen040225f2008-10-21 23:07:49 +00004868
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004869 DebugLoc dl = Op.getDebugLoc();
Owen Andersona90b3dc2009-07-15 21:51:10 +00004870 LLVMContext *Context = DAG.getContext();
Dale Johannesenace16102009-02-03 19:33:06 +00004871
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004872 // Build some magic constants.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004873 std::vector<Constant*> CV0;
Owen Andersoneed707b2009-07-24 23:12:02 +00004874 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x45300000)));
4875 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0x43300000)));
4876 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
4877 CV0.push_back(ConstantInt::get(*Context, APInt(32, 0)));
Owen Andersonaf7ec972009-07-28 21:19:26 +00004878 Constant *C0 = ConstantVector::get(CV0);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004879 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004880
Bill Wendling8b8a6362009-01-17 03:56:04 +00004881 std::vector<Constant*> CV1;
Owen Andersona90b3dc2009-07-15 21:51:10 +00004882 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00004883 ConstantFP::get(*Context, APFloat(APInt(64, 0x4530000000000000ULL))));
Owen Andersona90b3dc2009-07-15 21:51:10 +00004884 CV1.push_back(
Owen Anderson6f83c9c2009-07-27 20:59:43 +00004885 ConstantFP::get(*Context, APFloat(APInt(64, 0x4330000000000000ULL))));
Owen Andersonaf7ec972009-07-28 21:19:26 +00004886 Constant *C1 = ConstantVector::get(CV1);
Evan Cheng1606e8e2009-03-13 07:51:59 +00004887 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 16);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004888
Dale Johannesenace16102009-02-03 19:33:06 +00004889 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4890 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004891 Op.getOperand(0),
4892 DAG.getIntPtrConstant(1)));
Dale Johannesenace16102009-02-03 19:33:06 +00004893 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4894 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004895 Op.getOperand(0),
4896 DAG.getIntPtrConstant(0)));
Nate Begeman9008ca62009-04-27 18:41:29 +00004897 SDValue Unpck1 = getUnpackl(DAG, dl, MVT::v4i32, XR1, XR2);
Dale Johannesenace16102009-02-03 19:33:06 +00004898 SDValue CLod0 = DAG.getLoad(MVT::v4i32, dl, DAG.getEntryNode(), CPIdx0,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004899 PseudoSourceValue::getConstantPool(), 0,
4900 false, 16);
Nate Begeman9008ca62009-04-27 18:41:29 +00004901 SDValue Unpck2 = getUnpackl(DAG, dl, MVT::v4i32, Unpck1, CLod0);
Dale Johannesenace16102009-02-03 19:33:06 +00004902 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Unpck2);
4903 SDValue CLod1 = DAG.getLoad(MVT::v2f64, dl, CLod0.getValue(1), CPIdx1,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004904 PseudoSourceValue::getConstantPool(), 0,
4905 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00004906 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::v2f64, XR2F, CLod1);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004907
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004908 // Add the halves; easiest way is to swap them into another reg first.
Nate Begeman9008ca62009-04-27 18:41:29 +00004909 int ShufMask[2] = { 1, -1 };
4910 SDValue Shuf = DAG.getVectorShuffle(MVT::v2f64, dl, Sub,
4911 DAG.getUNDEF(MVT::v2f64), ShufMask);
Dale Johannesenace16102009-02-03 19:33:06 +00004912 SDValue Add = DAG.getNode(ISD::FADD, dl, MVT::v2f64, Shuf, Sub);
4913 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64, Add,
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004914 DAG.getIntPtrConstant(0));
4915}
4916
Bill Wendling8b8a6362009-01-17 03:56:04 +00004917// LowerUINT_TO_FP_i32 - 32-bit unsigned integer to float expansion.
4918SDValue X86TargetLowering::LowerUINT_TO_FP_i32(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004919 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004920 // FP constant to bias correct the final result.
4921 SDValue Bias = DAG.getConstantFP(BitsToDouble(0x4330000000000000ULL),
4922 MVT::f64);
4923
4924 // Load the 32-bit value into an XMM register.
Dale Johannesenace16102009-02-03 19:33:06 +00004925 SDValue Load = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v4i32,
4926 DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Bill Wendling8b8a6362009-01-17 03:56:04 +00004927 Op.getOperand(0),
4928 DAG.getIntPtrConstant(0)));
4929
Dale Johannesenace16102009-02-03 19:33:06 +00004930 Load = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4931 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Load),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004932 DAG.getIntPtrConstant(0));
4933
4934 // Or the load with the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004935 SDValue Or = DAG.getNode(ISD::OR, dl, MVT::v2i64,
4936 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4937 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004938 MVT::v2f64, Load)),
Dale Johannesenace16102009-02-03 19:33:06 +00004939 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
4940 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl,
Evan Cheng50c3dfe2009-01-19 08:19:57 +00004941 MVT::v2f64, Bias)));
Dale Johannesenace16102009-02-03 19:33:06 +00004942 Or = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f64,
4943 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2f64, Or),
Bill Wendling8b8a6362009-01-17 03:56:04 +00004944 DAG.getIntPtrConstant(0));
4945
4946 // Subtract the bias.
Dale Johannesenace16102009-02-03 19:33:06 +00004947 SDValue Sub = DAG.getNode(ISD::FSUB, dl, MVT::f64, Or, Bias);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004948
4949 // Handle final rounding.
Bill Wendling030939c2009-01-17 07:40:19 +00004950 MVT DestVT = Op.getValueType();
4951
4952 if (DestVT.bitsLT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004953 return DAG.getNode(ISD::FP_ROUND, dl, DestVT, Sub,
Bill Wendling030939c2009-01-17 07:40:19 +00004954 DAG.getIntPtrConstant(0));
4955 } else if (DestVT.bitsGT(MVT::f64)) {
Dale Johannesenace16102009-02-03 19:33:06 +00004956 return DAG.getNode(ISD::FP_EXTEND, dl, DestVT, Sub);
Bill Wendling030939c2009-01-17 07:40:19 +00004957 }
4958
4959 // Handle final rounding.
4960 return Sub;
Bill Wendling8b8a6362009-01-17 03:56:04 +00004961}
4962
4963SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Evan Chenga06ec9e2009-01-19 08:08:22 +00004964 SDValue N0 = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00004965 DebugLoc dl = Op.getDebugLoc();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004966
Evan Chenga06ec9e2009-01-19 08:08:22 +00004967 // Now not UINT_TO_FP is legal (it's marked custom), dag combiner won't
4968 // optimize it to a SINT_TO_FP when the sign bit is known zero. Perform
4969 // the optimization here.
4970 if (DAG.SignBitIsZero(N0))
Dale Johannesenace16102009-02-03 19:33:06 +00004971 return DAG.getNode(ISD::SINT_TO_FP, dl, Op.getValueType(), N0);
Evan Chenga06ec9e2009-01-19 08:08:22 +00004972
4973 MVT SrcVT = N0.getValueType();
Bill Wendling8b8a6362009-01-17 03:56:04 +00004974 if (SrcVT == MVT::i64) {
Eli Friedman36df4992009-05-27 00:47:34 +00004975 // We only handle SSE2 f64 target here; caller can expand the rest.
Bill Wendling8b8a6362009-01-17 03:56:04 +00004976 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
Daniel Dunbar82205572009-05-26 21:27:02 +00004977 return SDValue();
Bill Wendling030939c2009-01-17 07:40:19 +00004978
Bill Wendling8b8a6362009-01-17 03:56:04 +00004979 return LowerUINT_TO_FP_i64(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00004980 } else if (SrcVT == MVT::i32 && X86ScalarSSEf64) {
Bill Wendling8b8a6362009-01-17 03:56:04 +00004981 return LowerUINT_TO_FP_i32(Op, DAG);
4982 }
4983
Eli Friedman948e95a2009-05-23 09:59:16 +00004984 assert(SrcVT == MVT::i32 && "Unknown UINT_TO_FP to lower!");
4985
4986 // Make a 64-bit buffer, and use it to build an FILD.
4987 SDValue StackSlot = DAG.CreateStackTemporary(MVT::i64);
4988 SDValue WordOff = DAG.getConstant(4, getPointerTy());
4989 SDValue OffsetSlot = DAG.getNode(ISD::ADD, dl,
4990 getPointerTy(), StackSlot, WordOff);
4991 SDValue Store1 = DAG.getStore(DAG.getEntryNode(), dl, Op.getOperand(0),
4992 StackSlot, NULL, 0);
4993 SDValue Store2 = DAG.getStore(Store1, dl, DAG.getConstant(0, MVT::i32),
4994 OffsetSlot, NULL, 0);
4995 return BuildFILD(Op, MVT::i64, Store2, StackSlot, DAG);
Bill Wendling8b8a6362009-01-17 03:56:04 +00004996}
4997
Dan Gohman475871a2008-07-27 21:46:04 +00004998std::pair<SDValue,SDValue> X86TargetLowering::
Eli Friedman948e95a2009-05-23 09:59:16 +00004999FP_TO_INTHelper(SDValue Op, SelectionDAG &DAG, bool IsSigned) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005000 DebugLoc dl = Op.getDebugLoc();
Eli Friedman948e95a2009-05-23 09:59:16 +00005001
5002 MVT DstTy = Op.getValueType();
5003
5004 if (!IsSigned) {
5005 assert(DstTy == MVT::i32 && "Unexpected FP_TO_UINT");
5006 DstTy = MVT::i64;
5007 }
5008
5009 assert(DstTy.getSimpleVT() <= MVT::i64 &&
5010 DstTy.getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00005011 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00005012
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005013 // These are really Legal.
Eli Friedman948e95a2009-05-23 09:59:16 +00005014 if (DstTy == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00005015 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005016 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00005017 if (Subtarget->is64Bit() &&
Eli Friedman948e95a2009-05-23 09:59:16 +00005018 DstTy == MVT::i64 &&
Eli Friedman36df4992009-05-27 00:47:34 +00005019 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00005020 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00005021
Evan Cheng87c89352007-10-15 20:11:21 +00005022 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
5023 // stack slot.
5024 MachineFunction &MF = DAG.getMachineFunction();
Eli Friedman948e95a2009-05-23 09:59:16 +00005025 unsigned MemSize = DstTy.getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00005026 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00005027 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Eli Friedman948e95a2009-05-23 09:59:16 +00005028
Evan Cheng0db9fe62006-04-25 20:13:52 +00005029 unsigned Opc;
Eli Friedman948e95a2009-05-23 09:59:16 +00005030 switch (DstTy.getSimpleVT()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00005031 default: llvm_unreachable("Invalid FP_TO_SINT to lower!");
Chris Lattner27a6c732007-11-24 07:07:01 +00005032 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
5033 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
5034 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005035 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005036
Dan Gohman475871a2008-07-27 21:46:04 +00005037 SDValue Chain = DAG.getEntryNode();
5038 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00005039 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Eli Friedman948e95a2009-05-23 09:59:16 +00005040 assert(DstTy == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dale Johannesenace16102009-02-03 19:33:06 +00005041 Chain = DAG.getStore(Chain, dl, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00005042 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00005043 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00005044 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00005045 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
5046 };
Dale Johannesenace16102009-02-03 19:33:06 +00005047 Value = DAG.getNode(X86ISD::FLD, dl, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005048 Chain = Value.getValue(1);
5049 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
5050 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
5051 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005052
Evan Cheng0db9fe62006-04-25 20:13:52 +00005053 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00005054 SDValue Ops[] = { Chain, Value, StackSlot };
Dale Johannesenace16102009-02-03 19:33:06 +00005055 SDValue FIST = DAG.getNode(Opc, dl, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00005056
Chris Lattner27a6c732007-11-24 07:07:01 +00005057 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005058}
5059
Dan Gohman475871a2008-07-27 21:46:04 +00005060SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
Eli Friedman23ef1052009-06-06 03:57:58 +00005061 if (Op.getValueType().isVector()) {
5062 if (Op.getValueType() == MVT::v2i32 &&
5063 Op.getOperand(0).getValueType() == MVT::v2f64) {
5064 return Op;
5065 }
5066 return SDValue();
5067 }
5068
Eli Friedman948e95a2009-05-23 09:59:16 +00005069 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, true);
Dan Gohman475871a2008-07-27 21:46:04 +00005070 SDValue FIST = Vals.first, StackSlot = Vals.second;
Eli Friedman36df4992009-05-27 00:47:34 +00005071 // If FP_TO_INTHelper failed, the node is actually supposed to be Legal.
5072 if (FIST.getNode() == 0) return Op;
Scott Michelfdc40a02009-02-17 22:15:04 +00005073
Chris Lattner27a6c732007-11-24 07:07:01 +00005074 // Load the result.
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005075 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
Dale Johannesenace16102009-02-03 19:33:06 +00005076 FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00005077}
5078
Eli Friedman948e95a2009-05-23 09:59:16 +00005079SDValue X86TargetLowering::LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) {
5080 std::pair<SDValue,SDValue> Vals = FP_TO_INTHelper(Op, DAG, false);
5081 SDValue FIST = Vals.first, StackSlot = Vals.second;
5082 assert(FIST.getNode() && "Unexpected failure");
5083
5084 // Load the result.
5085 return DAG.getLoad(Op.getValueType(), Op.getDebugLoc(),
5086 FIST, StackSlot, NULL, 0);
5087}
5088
Dan Gohman475871a2008-07-27 21:46:04 +00005089SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005090 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005091 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005092 MVT VT = Op.getValueType();
5093 MVT EltVT = VT;
5094 if (VT.isVector())
5095 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005096 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005097 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005098 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00005099 CV.push_back(C);
5100 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005101 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005102 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00005103 CV.push_back(C);
5104 CV.push_back(C);
5105 CV.push_back(C);
5106 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005107 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005108 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005109 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005110 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005111 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005112 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005113 return DAG.getNode(X86ISD::FAND, dl, VT, Op.getOperand(0), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005114}
5115
Dan Gohman475871a2008-07-27 21:46:04 +00005116SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005117 LLVMContext *Context = DAG.getContext();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005118 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005119 MVT VT = Op.getValueType();
5120 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00005121 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005122 if (VT.isVector()) {
5123 EltVT = VT.getVectorElementType();
5124 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00005125 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005126 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00005127 if (EltVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005128 Constant *C = ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00005129 CV.push_back(C);
5130 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005131 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005132 Constant *C = ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00005133 CV.push_back(C);
5134 CV.push_back(C);
5135 CV.push_back(C);
5136 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005137 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005138 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005139 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005140 SDValue Mask = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005141 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005142 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005143 if (VT.isVector()) {
Dale Johannesenace16102009-02-03 19:33:06 +00005144 return DAG.getNode(ISD::BIT_CONVERT, dl, VT,
5145 DAG.getNode(ISD::XOR, dl, MVT::v2i64,
Scott Michelfdc40a02009-02-17 22:15:04 +00005146 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64,
Dale Johannesenace16102009-02-03 19:33:06 +00005147 Op.getOperand(0)),
5148 DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v2i64, Mask)));
Evan Chengd4d01b72007-07-19 23:36:01 +00005149 } else {
Dale Johannesenace16102009-02-03 19:33:06 +00005150 return DAG.getNode(X86ISD::FXOR, dl, VT, Op.getOperand(0), Mask);
Evan Chengd4d01b72007-07-19 23:36:01 +00005151 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005152}
5153
Dan Gohman475871a2008-07-27 21:46:04 +00005154SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
Owen Andersona90b3dc2009-07-15 21:51:10 +00005155 LLVMContext *Context = DAG.getContext();
Dan Gohman475871a2008-07-27 21:46:04 +00005156 SDValue Op0 = Op.getOperand(0);
5157 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005158 DebugLoc dl = Op.getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005159 MVT VT = Op.getValueType();
5160 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00005161
5162 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005163 if (SrcVT.bitsLT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005164 Op1 = DAG.getNode(ISD::FP_EXTEND, dl, VT, Op1);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005165 SrcVT = VT;
5166 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005167 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005168 if (SrcVT.bitsGT(VT)) {
Dale Johannesenace16102009-02-03 19:33:06 +00005169 Op1 = DAG.getNode(ISD::FP_ROUND, dl, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005170 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00005171 }
5172
5173 // At this point the operands and the result should have the same
5174 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00005175
Evan Cheng68c47cb2007-01-05 07:55:56 +00005176 // First get the sign bit of second operand.
5177 std::vector<Constant*> CV;
5178 if (SrcVT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005179 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 1ULL << 63))));
5180 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005181 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005182 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 1U << 31))));
5183 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5184 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5185 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005186 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005187 Constant *C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005188 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005189 SDValue Mask1 = DAG.getLoad(SrcVT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005190 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005191 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005192 SDValue SignBit = DAG.getNode(X86ISD::FAND, dl, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005193
5194 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00005195 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00005196 // Op0 is MVT::f32, Op1 is MVT::f64.
Dale Johannesenace16102009-02-03 19:33:06 +00005197 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, MVT::v2f64, SignBit);
5198 SignBit = DAG.getNode(X86ISD::FSRL, dl, MVT::v2f64, SignBit,
Evan Cheng68c47cb2007-01-05 07:55:56 +00005199 DAG.getConstant(32, MVT::i32));
Dale Johannesenace16102009-02-03 19:33:06 +00005200 SignBit = DAG.getNode(ISD::BIT_CONVERT, dl, MVT::v4f32, SignBit);
5201 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, dl, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00005202 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00005203 }
5204
Evan Cheng73d6cf12007-01-05 21:37:56 +00005205 // Clear first operand sign bit.
5206 CV.clear();
5207 if (VT == MVT::f64) {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005208 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, ~(1ULL << 63)))));
5209 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005210 } else {
Owen Anderson6f83c9c2009-07-27 20:59:43 +00005211 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, ~(1U << 31)))));
5212 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5213 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
5214 CV.push_back(ConstantFP::get(*Context, APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00005215 }
Owen Andersonaf7ec972009-07-28 21:19:26 +00005216 C = ConstantVector::get(CV);
Evan Cheng1606e8e2009-03-13 07:51:59 +00005217 CPIdx = DAG.getConstantPool(C, getPointerTy(), 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005218 SDValue Mask2 = DAG.getLoad(VT, dl, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00005219 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00005220 false, 16);
Dale Johannesenace16102009-02-03 19:33:06 +00005221 SDValue Val = DAG.getNode(X86ISD::FAND, dl, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005222
5223 // Or the value with the sign bit.
Dale Johannesenace16102009-02-03 19:33:06 +00005224 return DAG.getNode(X86ISD::FOR, dl, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005225}
5226
Dan Gohman076aee32009-03-04 19:44:21 +00005227/// Emit nodes that will be selected as "test Op0,Op0", or something
5228/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005229SDValue X86TargetLowering::EmitTest(SDValue Op, unsigned X86CC,
5230 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005231 DebugLoc dl = Op.getDebugLoc();
5232
Dan Gohman31125812009-03-07 01:58:32 +00005233 // CF and OF aren't always set the way we want. Determine which
5234 // of these we need.
5235 bool NeedCF = false;
5236 bool NeedOF = false;
5237 switch (X86CC) {
5238 case X86::COND_A: case X86::COND_AE:
5239 case X86::COND_B: case X86::COND_BE:
5240 NeedCF = true;
5241 break;
5242 case X86::COND_G: case X86::COND_GE:
5243 case X86::COND_L: case X86::COND_LE:
5244 case X86::COND_O: case X86::COND_NO:
5245 NeedOF = true;
5246 break;
5247 default: break;
5248 }
5249
Dan Gohman076aee32009-03-04 19:44:21 +00005250 // See if we can use the EFLAGS value from the operand instead of
Dan Gohman31125812009-03-07 01:58:32 +00005251 // doing a separate TEST. TEST always sets OF and CF to 0, so unless
5252 // we prove that the arithmetic won't overflow, we can't use OF or CF.
5253 if (Op.getResNo() == 0 && !NeedOF && !NeedCF) {
Dan Gohman076aee32009-03-04 19:44:21 +00005254 unsigned Opcode = 0;
Dan Gohman51bb4742009-03-05 21:29:28 +00005255 unsigned NumOperands = 0;
Dan Gohman076aee32009-03-04 19:44:21 +00005256 switch (Op.getNode()->getOpcode()) {
5257 case ISD::ADD:
5258 // Due to an isel shortcoming, be conservative if this add is likely to
5259 // be selected as part of a load-modify-store instruction. When the root
5260 // node in a match is a store, isel doesn't know how to remap non-chain
5261 // non-flag uses of other nodes in the match, such as the ADD in this
5262 // case. This leads to the ADD being left around and reselected, with
5263 // the result being two adds in the output.
5264 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5265 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5266 if (UI->getOpcode() == ISD::STORE)
5267 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005268 if (ConstantSDNode *C =
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005269 dyn_cast<ConstantSDNode>(Op.getNode()->getOperand(1))) {
5270 // An add of one will be selected as an INC.
Dan Gohman076aee32009-03-04 19:44:21 +00005271 if (C->getAPIntValue() == 1) {
5272 Opcode = X86ISD::INC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005273 NumOperands = 1;
Dan Gohman076aee32009-03-04 19:44:21 +00005274 break;
5275 }
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005276 // An add of negative one (subtract of one) will be selected as a DEC.
5277 if (C->getAPIntValue().isAllOnesValue()) {
5278 Opcode = X86ISD::DEC;
Dan Gohman51bb4742009-03-05 21:29:28 +00005279 NumOperands = 1;
Dan Gohman4bfcf2a2009-03-05 19:32:48 +00005280 break;
5281 }
5282 }
Dan Gohman076aee32009-03-04 19:44:21 +00005283 // Otherwise use a regular EFLAGS-setting add.
5284 Opcode = X86ISD::ADD;
Dan Gohman51bb4742009-03-05 21:29:28 +00005285 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005286 break;
5287 case ISD::SUB:
5288 // Due to the ISEL shortcoming noted above, be conservative if this sub is
5289 // likely to be selected as part of a load-modify-store instruction.
5290 for (SDNode::use_iterator UI = Op.getNode()->use_begin(),
5291 UE = Op.getNode()->use_end(); UI != UE; ++UI)
5292 if (UI->getOpcode() == ISD::STORE)
5293 goto default_case;
Dan Gohman076aee32009-03-04 19:44:21 +00005294 // Otherwise use a regular EFLAGS-setting sub.
5295 Opcode = X86ISD::SUB;
Dan Gohman51bb4742009-03-05 21:29:28 +00005296 NumOperands = 2;
Dan Gohman076aee32009-03-04 19:44:21 +00005297 break;
5298 case X86ISD::ADD:
5299 case X86ISD::SUB:
5300 case X86ISD::INC:
5301 case X86ISD::DEC:
5302 return SDValue(Op.getNode(), 1);
5303 default:
5304 default_case:
5305 break;
5306 }
5307 if (Opcode != 0) {
Dan Gohmanfc166572009-04-09 23:54:40 +00005308 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::i32);
Dan Gohman076aee32009-03-04 19:44:21 +00005309 SmallVector<SDValue, 4> Ops;
Dan Gohman31125812009-03-07 01:58:32 +00005310 for (unsigned i = 0; i != NumOperands; ++i)
Dan Gohman076aee32009-03-04 19:44:21 +00005311 Ops.push_back(Op.getOperand(i));
Dan Gohmanfc166572009-04-09 23:54:40 +00005312 SDValue New = DAG.getNode(Opcode, dl, VTs, &Ops[0], NumOperands);
Dan Gohman076aee32009-03-04 19:44:21 +00005313 DAG.ReplaceAllUsesWith(Op, New);
5314 return SDValue(New.getNode(), 1);
5315 }
5316 }
5317
5318 // Otherwise just emit a CMP with 0, which is the TEST pattern.
5319 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op,
5320 DAG.getConstant(0, Op.getValueType()));
5321}
5322
5323/// Emit nodes that will be selected as "cmp Op0,Op1", or something
5324/// equivalent.
Dan Gohman31125812009-03-07 01:58:32 +00005325SDValue X86TargetLowering::EmitCmp(SDValue Op0, SDValue Op1, unsigned X86CC,
5326 SelectionDAG &DAG) {
Dan Gohman076aee32009-03-04 19:44:21 +00005327 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op1))
5328 if (C->getAPIntValue() == 0)
Dan Gohman31125812009-03-07 01:58:32 +00005329 return EmitTest(Op0, X86CC, DAG);
Dan Gohman076aee32009-03-04 19:44:21 +00005330
5331 DebugLoc dl = Op0.getDebugLoc();
5332 return DAG.getNode(X86ISD::CMP, dl, MVT::i32, Op0, Op1);
5333}
5334
Dan Gohman475871a2008-07-27 21:46:04 +00005335SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005336 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005337 SDValue Op0 = Op.getOperand(0);
5338 SDValue Op1 = Op.getOperand(1);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005339 DebugLoc dl = Op.getDebugLoc();
Chris Lattnere55484e2008-12-25 05:34:37 +00005340 ISD::CondCode CC = cast<CondCodeSDNode>(Op.getOperand(2))->get();
Scott Michelfdc40a02009-02-17 22:15:04 +00005341
Dan Gohmane5af2d32009-01-29 01:59:02 +00005342 // Lower (X & (1 << N)) == 0 to BT(X, N).
5343 // Lower ((X >>u N) & 1) != 0 to BT(X, N).
5344 // Lower ((X >>s N) & 1) != 0 to BT(X, N).
Dan Gohman286575c2009-01-13 23:25:30 +00005345 if (Op0.getOpcode() == ISD::AND &&
5346 Op0.hasOneUse() &&
5347 Op1.getOpcode() == ISD::Constant &&
Dan Gohmane5af2d32009-01-29 01:59:02 +00005348 cast<ConstantSDNode>(Op1)->getZExtValue() == 0 &&
Chris Lattnere55484e2008-12-25 05:34:37 +00005349 (CC == ISD::SETEQ || CC == ISD::SETNE)) {
Dan Gohmane5af2d32009-01-29 01:59:02 +00005350 SDValue LHS, RHS;
5351 if (Op0.getOperand(1).getOpcode() == ISD::SHL) {
5352 if (ConstantSDNode *Op010C =
5353 dyn_cast<ConstantSDNode>(Op0.getOperand(1).getOperand(0)))
5354 if (Op010C->getZExtValue() == 1) {
5355 LHS = Op0.getOperand(0);
5356 RHS = Op0.getOperand(1).getOperand(1);
5357 }
5358 } else if (Op0.getOperand(0).getOpcode() == ISD::SHL) {
5359 if (ConstantSDNode *Op000C =
5360 dyn_cast<ConstantSDNode>(Op0.getOperand(0).getOperand(0)))
5361 if (Op000C->getZExtValue() == 1) {
5362 LHS = Op0.getOperand(1);
5363 RHS = Op0.getOperand(0).getOperand(1);
5364 }
5365 } else if (Op0.getOperand(1).getOpcode() == ISD::Constant) {
5366 ConstantSDNode *AndRHS = cast<ConstantSDNode>(Op0.getOperand(1));
5367 SDValue AndLHS = Op0.getOperand(0);
5368 if (AndRHS->getZExtValue() == 1 && AndLHS.getOpcode() == ISD::SRL) {
5369 LHS = AndLHS.getOperand(0);
5370 RHS = AndLHS.getOperand(1);
5371 }
5372 }
Evan Cheng0488db92007-09-25 01:57:46 +00005373
Dan Gohmane5af2d32009-01-29 01:59:02 +00005374 if (LHS.getNode()) {
Chris Lattnere55484e2008-12-25 05:34:37 +00005375 // If LHS is i8, promote it to i16 with any_extend. There is no i8 BT
5376 // instruction. Since the shift amount is in-range-or-undefined, we know
5377 // that doing a bittest on the i16 value is ok. We extend to i32 because
5378 // the encoding for the i16 version is larger than the i32 version.
5379 if (LHS.getValueType() == MVT::i8)
Dale Johannesenace16102009-02-03 19:33:06 +00005380 LHS = DAG.getNode(ISD::ANY_EXTEND, dl, MVT::i32, LHS);
Chris Lattnere55484e2008-12-25 05:34:37 +00005381
5382 // If the operand types disagree, extend the shift amount to match. Since
5383 // BT ignores high bits (like shifts) we can use anyextend.
5384 if (LHS.getValueType() != RHS.getValueType())
Dale Johannesenace16102009-02-03 19:33:06 +00005385 RHS = DAG.getNode(ISD::ANY_EXTEND, dl, LHS.getValueType(), RHS);
Dan Gohmane5af2d32009-01-29 01:59:02 +00005386
Dale Johannesenace16102009-02-03 19:33:06 +00005387 SDValue BT = DAG.getNode(X86ISD::BT, dl, MVT::i32, LHS, RHS);
Dan Gohman653456c2009-01-07 00:15:08 +00005388 unsigned Cond = CC == ISD::SETEQ ? X86::COND_AE : X86::COND_B;
Dale Johannesenace16102009-02-03 19:33:06 +00005389 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattnere55484e2008-12-25 05:34:37 +00005390 DAG.getConstant(Cond, MVT::i8), BT);
5391 }
5392 }
5393
5394 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5395 unsigned X86CC = TranslateX86CC(CC, isFP, Op0, Op1, DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00005396
Dan Gohman31125812009-03-07 01:58:32 +00005397 SDValue Cond = EmitCmp(Op0, Op1, X86CC, DAG);
Dale Johannesenace16102009-02-03 19:33:06 +00005398 return DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Chris Lattner43287082008-12-24 00:11:37 +00005399 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005400}
5401
Dan Gohman475871a2008-07-27 21:46:04 +00005402SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5403 SDValue Cond;
5404 SDValue Op0 = Op.getOperand(0);
5405 SDValue Op1 = Op.getOperand(1);
5406 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005407 MVT VT = Op.getValueType();
5408 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5409 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005410 DebugLoc dl = Op.getDebugLoc();
Nate Begeman30a0de92008-07-17 16:51:19 +00005411
5412 if (isFP) {
5413 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005414 MVT VT0 = Op0.getValueType();
5415 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5416 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005417 bool Swap = false;
5418
5419 switch (SetCCOpcode) {
5420 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005421 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005422 case ISD::SETEQ: SSECC = 0; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00005423 case ISD::SETOGT:
Nate Begeman30a0de92008-07-17 16:51:19 +00005424 case ISD::SETGT: Swap = true; // Fallthrough
5425 case ISD::SETLT:
5426 case ISD::SETOLT: SSECC = 1; break;
5427 case ISD::SETOGE:
5428 case ISD::SETGE: Swap = true; // Fallthrough
5429 case ISD::SETLE:
5430 case ISD::SETOLE: SSECC = 2; break;
5431 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005432 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005433 case ISD::SETNE: SSECC = 4; break;
5434 case ISD::SETULE: Swap = true;
5435 case ISD::SETUGE: SSECC = 5; break;
5436 case ISD::SETULT: Swap = true;
5437 case ISD::SETUGT: SSECC = 6; break;
5438 case ISD::SETO: SSECC = 7; break;
5439 }
5440 if (Swap)
5441 std::swap(Op0, Op1);
5442
Nate Begemanfb8ead02008-07-25 19:05:58 +00005443 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005444 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005445 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005446 SDValue UNORD, EQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005447 UNORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5448 EQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5449 return DAG.getNode(ISD::OR, dl, VT, UNORD, EQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005450 }
5451 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005452 SDValue ORD, NEQ;
Dale Johannesenace16102009-02-03 19:33:06 +00005453 ORD = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5454 NEQ = DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5455 return DAG.getNode(ISD::AND, dl, VT, ORD, NEQ);
Nate Begemanfb8ead02008-07-25 19:05:58 +00005456 }
Torok Edwinc23197a2009-07-14 16:55:14 +00005457 llvm_unreachable("Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005458 }
5459 // Handle all other FP comparisons here.
Dale Johannesenace16102009-02-03 19:33:06 +00005460 return DAG.getNode(Opc, dl, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
Nate Begeman30a0de92008-07-17 16:51:19 +00005461 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005462
Nate Begeman30a0de92008-07-17 16:51:19 +00005463 // We are handling one of the integer comparisons here. Since SSE only has
5464 // GT and EQ comparisons for integer, swapping operands and multiple
5465 // operations may be required for some comparisons.
5466 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5467 bool Swap = false, Invert = false, FlipSigns = false;
Scott Michelfdc40a02009-02-17 22:15:04 +00005468
Nate Begeman30a0de92008-07-17 16:51:19 +00005469 switch (VT.getSimpleVT()) {
5470 default: break;
Eli Friedman3dae2842009-07-22 01:06:52 +00005471 case MVT::v8i8:
Nate Begeman30a0de92008-07-17 16:51:19 +00005472 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
Eli Friedman3dae2842009-07-22 01:06:52 +00005473 case MVT::v4i16:
Nate Begeman30a0de92008-07-17 16:51:19 +00005474 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
Eli Friedman3dae2842009-07-22 01:06:52 +00005475 case MVT::v2i32:
Nate Begeman30a0de92008-07-17 16:51:19 +00005476 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5477 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5478 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005479
Nate Begeman30a0de92008-07-17 16:51:19 +00005480 switch (SetCCOpcode) {
5481 default: break;
5482 case ISD::SETNE: Invert = true;
5483 case ISD::SETEQ: Opc = EQOpc; break;
5484 case ISD::SETLT: Swap = true;
5485 case ISD::SETGT: Opc = GTOpc; break;
5486 case ISD::SETGE: Swap = true;
5487 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5488 case ISD::SETULT: Swap = true;
5489 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5490 case ISD::SETUGE: Swap = true;
5491 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5492 }
5493 if (Swap)
5494 std::swap(Op0, Op1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005495
Nate Begeman30a0de92008-07-17 16:51:19 +00005496 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5497 // bits of the inputs before performing those operations.
5498 if (FlipSigns) {
5499 MVT EltVT = VT.getVectorElementType();
Duncan Sandsb0d5cdd2009-02-01 18:06:53 +00005500 SDValue SignBit = DAG.getConstant(APInt::getSignBit(EltVT.getSizeInBits()),
5501 EltVT);
Dan Gohman475871a2008-07-27 21:46:04 +00005502 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
Evan Chenga87008d2009-02-25 22:49:59 +00005503 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, dl, VT, &SignBits[0],
5504 SignBits.size());
Dale Johannesenace16102009-02-03 19:33:06 +00005505 Op0 = DAG.getNode(ISD::XOR, dl, VT, Op0, SignVec);
5506 Op1 = DAG.getNode(ISD::XOR, dl, VT, Op1, SignVec);
Nate Begeman30a0de92008-07-17 16:51:19 +00005507 }
Scott Michelfdc40a02009-02-17 22:15:04 +00005508
Dale Johannesenace16102009-02-03 19:33:06 +00005509 SDValue Result = DAG.getNode(Opc, dl, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005510
5511 // If the logical-not of the result is required, perform that now.
Bob Wilson4c245462009-01-22 17:39:32 +00005512 if (Invert)
Dale Johannesenace16102009-02-03 19:33:06 +00005513 Result = DAG.getNOT(dl, Result, VT);
Bob Wilson4c245462009-01-22 17:39:32 +00005514
Nate Begeman30a0de92008-07-17 16:51:19 +00005515 return Result;
5516}
Evan Cheng0488db92007-09-25 01:57:46 +00005517
Evan Cheng370e5342008-12-03 08:38:43 +00005518// isX86LogicalCmp - Return true if opcode is a X86 logical comparison.
Dan Gohman076aee32009-03-04 19:44:21 +00005519static bool isX86LogicalCmp(SDValue Op) {
5520 unsigned Opc = Op.getNode()->getOpcode();
5521 if (Opc == X86ISD::CMP || Opc == X86ISD::COMI || Opc == X86ISD::UCOMI)
5522 return true;
5523 if (Op.getResNo() == 1 &&
5524 (Opc == X86ISD::ADD ||
5525 Opc == X86ISD::SUB ||
5526 Opc == X86ISD::SMUL ||
5527 Opc == X86ISD::UMUL ||
5528 Opc == X86ISD::INC ||
5529 Opc == X86ISD::DEC))
5530 return true;
5531
5532 return false;
Evan Cheng370e5342008-12-03 08:38:43 +00005533}
5534
Dan Gohman475871a2008-07-27 21:46:04 +00005535SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005536 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005537 SDValue Cond = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005538 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005539 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005540
Evan Cheng734503b2006-09-11 02:19:56 +00005541 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005542 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005543
Evan Cheng3f41d662007-10-08 22:16:29 +00005544 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5545 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005546 if (Cond.getOpcode() == X86ISD::SETCC) {
5547 CC = Cond.getOperand(0);
5548
Dan Gohman475871a2008-07-27 21:46:04 +00005549 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005550 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005551 MVT VT = Op.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005552
Evan Cheng3f41d662007-10-08 22:16:29 +00005553 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005554 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005555 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005556 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Scott Michelfdc40a02009-02-17 22:15:04 +00005557
Chris Lattnerd1980a52009-03-12 06:52:53 +00005558 if ((isX86LogicalCmp(Cmp) && !IllegalFPCMov) ||
5559 Opc == X86ISD::BT) { // FIXME
Evan Cheng3f41d662007-10-08 22:16:29 +00005560 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005561 addTest = false;
5562 }
5563 }
5564
5565 if (addTest) {
5566 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005567 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005568 }
5569
Dan Gohmanfc166572009-04-09 23:54:40 +00005570 SDVTList VTs = DAG.getVTList(Op.getValueType(), MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005571 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005572 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5573 // condition is true.
5574 Ops.push_back(Op.getOperand(2));
5575 Ops.push_back(Op.getOperand(1));
5576 Ops.push_back(CC);
5577 Ops.push_back(Cond);
Dan Gohmanfc166572009-04-09 23:54:40 +00005578 return DAG.getNode(X86ISD::CMOV, dl, VTs, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005579}
5580
Evan Cheng370e5342008-12-03 08:38:43 +00005581// isAndOrOfSingleUseSetCCs - Return true if node is an ISD::AND or
5582// ISD::OR of two X86ISD::SETCC nodes each of which has no other use apart
5583// from the AND / OR.
5584static bool isAndOrOfSetCCs(SDValue Op, unsigned &Opc) {
5585 Opc = Op.getOpcode();
5586 if (Opc != ISD::OR && Opc != ISD::AND)
5587 return false;
5588 return (Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5589 Op.getOperand(0).hasOneUse() &&
5590 Op.getOperand(1).getOpcode() == X86ISD::SETCC &&
5591 Op.getOperand(1).hasOneUse());
5592}
5593
Evan Cheng961d6d42009-02-02 08:19:07 +00005594// isXor1OfSetCC - Return true if node is an ISD::XOR of a X86ISD::SETCC and
5595// 1 and that the SETCC node has a single use.
Evan Cheng67ad9db2009-02-02 08:07:36 +00005596static bool isXor1OfSetCC(SDValue Op) {
5597 if (Op.getOpcode() != ISD::XOR)
5598 return false;
5599 ConstantSDNode *N1C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
5600 if (N1C && N1C->getAPIntValue() == 1) {
5601 return Op.getOperand(0).getOpcode() == X86ISD::SETCC &&
5602 Op.getOperand(0).hasOneUse();
5603 }
5604 return false;
5605}
5606
Dan Gohman475871a2008-07-27 21:46:04 +00005607SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005608 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005609 SDValue Chain = Op.getOperand(0);
5610 SDValue Cond = Op.getOperand(1);
5611 SDValue Dest = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005612 DebugLoc dl = Op.getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00005613 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005614
Evan Cheng0db9fe62006-04-25 20:13:52 +00005615 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005616 Cond = LowerSETCC(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005617#if 0
5618 // FIXME: LowerXALUO doesn't handle these!!
Bill Wendlingd350e022008-12-12 21:15:41 +00005619 else if (Cond.getOpcode() == X86ISD::ADD ||
5620 Cond.getOpcode() == X86ISD::SUB ||
5621 Cond.getOpcode() == X86ISD::SMUL ||
5622 Cond.getOpcode() == X86ISD::UMUL)
Bill Wendling74c37652008-12-09 22:08:41 +00005623 Cond = LowerXALUO(Cond, DAG);
Chris Lattnere55484e2008-12-25 05:34:37 +00005624#endif
Scott Michelfdc40a02009-02-17 22:15:04 +00005625
Evan Cheng3f41d662007-10-08 22:16:29 +00005626 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5627 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005628 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005629 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005630
Dan Gohman475871a2008-07-27 21:46:04 +00005631 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005632 unsigned Opc = Cmp.getOpcode();
Chris Lattnere55484e2008-12-25 05:34:37 +00005633 // FIXME: WHY THE SPECIAL CASING OF LogicalCmp??
Dan Gohman076aee32009-03-04 19:44:21 +00005634 if (isX86LogicalCmp(Cmp) || Opc == X86ISD::BT) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005635 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005636 addTest = false;
Bill Wendling61edeb52008-12-02 01:06:39 +00005637 } else {
Evan Cheng370e5342008-12-03 08:38:43 +00005638 switch (cast<ConstantSDNode>(CC)->getZExtValue()) {
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005639 default: break;
5640 case X86::COND_O:
Dan Gohman653456c2009-01-07 00:15:08 +00005641 case X86::COND_B:
Chris Lattnere55484e2008-12-25 05:34:37 +00005642 // These can only come from an arithmetic instruction with overflow,
5643 // e.g. SADDO, UADDO.
Bill Wendling0ea25cb2008-12-03 08:32:02 +00005644 Cond = Cond.getNode()->getOperand(1);
5645 addTest = false;
5646 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00005647 }
Evan Cheng0488db92007-09-25 01:57:46 +00005648 }
Evan Cheng370e5342008-12-03 08:38:43 +00005649 } else {
5650 unsigned CondOpc;
5651 if (Cond.hasOneUse() && isAndOrOfSetCCs(Cond, CondOpc)) {
5652 SDValue Cmp = Cond.getOperand(0).getOperand(1);
Evan Cheng370e5342008-12-03 08:38:43 +00005653 if (CondOpc == ISD::OR) {
5654 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5655 // two branches instead of an explicit OR instruction with a
5656 // separate test.
5657 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005658 isX86LogicalCmp(Cmp)) {
Evan Cheng370e5342008-12-03 08:38:43 +00005659 CC = Cond.getOperand(0).getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00005660 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005661 Chain, Dest, CC, Cmp);
5662 CC = Cond.getOperand(1).getOperand(0);
5663 Cond = Cmp;
5664 addTest = false;
5665 }
5666 } else { // ISD::AND
5667 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5668 // two branches instead of an explicit AND instruction with a
5669 // separate test. However, we only do this if this block doesn't
5670 // have a fall-through edge, because this requires an explicit
5671 // jmp when the condition is false.
5672 if (Cmp == Cond.getOperand(1).getOperand(1) &&
Dan Gohman076aee32009-03-04 19:44:21 +00005673 isX86LogicalCmp(Cmp) &&
Evan Cheng370e5342008-12-03 08:38:43 +00005674 Op.getNode()->hasOneUse()) {
5675 X86::CondCode CCode =
5676 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5677 CCode = X86::GetOppositeBranchCondition(CCode);
5678 CC = DAG.getConstant(CCode, MVT::i8);
5679 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5680 // Look for an unconditional branch following this conditional branch.
5681 // We need this because we need to reverse the successors in order
5682 // to implement FCMP_OEQ.
5683 if (User.getOpcode() == ISD::BR) {
5684 SDValue FalseBB = User.getOperand(1);
5685 SDValue NewBR =
5686 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5687 assert(NewBR == User);
5688 Dest = FalseBB;
Dan Gohman279c22e2008-10-21 03:29:32 +00005689
Dale Johannesene4d209d2009-02-03 20:21:25 +00005690 Chain = DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Evan Cheng370e5342008-12-03 08:38:43 +00005691 Chain, Dest, CC, Cmp);
5692 X86::CondCode CCode =
5693 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5694 CCode = X86::GetOppositeBranchCondition(CCode);
5695 CC = DAG.getConstant(CCode, MVT::i8);
5696 Cond = Cmp;
5697 addTest = false;
5698 }
5699 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005700 }
Evan Cheng67ad9db2009-02-02 08:07:36 +00005701 } else if (Cond.hasOneUse() && isXor1OfSetCC(Cond)) {
5702 // Recognize for xorb (setcc), 1 patterns. The xor inverts the condition.
5703 // It should be transformed during dag combiner except when the condition
5704 // is set by a arithmetics with overflow node.
5705 X86::CondCode CCode =
5706 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5707 CCode = X86::GetOppositeBranchCondition(CCode);
5708 CC = DAG.getConstant(CCode, MVT::i8);
5709 Cond = Cond.getOperand(0).getOperand(1);
5710 addTest = false;
Dan Gohman279c22e2008-10-21 03:29:32 +00005711 }
Evan Cheng0488db92007-09-25 01:57:46 +00005712 }
5713
5714 if (addTest) {
5715 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Dan Gohman31125812009-03-07 01:58:32 +00005716 Cond = EmitTest(Cond, X86::COND_NE, DAG);
Evan Cheng0488db92007-09-25 01:57:46 +00005717 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00005718 return DAG.getNode(X86ISD::BRCOND, dl, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005719 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005720}
5721
Anton Korobeynikove060b532007-04-17 19:34:00 +00005722
5723// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5724// Calls to _alloca is needed to probe the stack when allocating more than 4k
5725// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5726// that the guard pages used by the OS virtual memory manager are allocated in
5727// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005728SDValue
5729X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005730 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005731 assert(Subtarget->isTargetCygMing() &&
5732 "This should be used only on Cygwin/Mingw targets");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005733 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005734
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005735 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005736 SDValue Chain = Op.getOperand(0);
5737 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005738 // FIXME: Ensure alignment here
5739
Dan Gohman475871a2008-07-27 21:46:04 +00005740 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005741
Duncan Sands83ec4b62008-06-06 12:08:01 +00005742 MVT IntPtr = getPointerTy();
5743 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005744
Chris Lattnere563bbc2008-10-11 22:08:30 +00005745 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005746
Dale Johannesendd64c412009-02-04 00:33:20 +00005747 Chain = DAG.getCopyToReg(Chain, dl, X86::EAX, Size, Flag);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005748 Flag = Chain.getValue(1);
5749
5750 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005751 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005752 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005753 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005754 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005755 Flag };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005756 Chain = DAG.getNode(X86ISD::CALL, dl, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005757 Flag = Chain.getValue(1);
5758
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005759 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005760 DAG.getIntPtrConstant(0, true),
5761 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005762 Flag);
5763
Dale Johannesendd64c412009-02-04 00:33:20 +00005764 Chain = DAG.getCopyFromReg(Chain, dl, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005765
Dan Gohman475871a2008-07-27 21:46:04 +00005766 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Dale Johannesene4d209d2009-02-03 20:21:25 +00005767 return DAG.getMergeValues(Ops1, 2, dl);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005768}
5769
Dan Gohman475871a2008-07-27 21:46:04 +00005770SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005771X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG, DebugLoc dl,
Bill Wendling6f287b22008-09-30 21:22:07 +00005772 SDValue Chain,
5773 SDValue Dst, SDValue Src,
5774 SDValue Size, unsigned Align,
5775 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005776 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005777 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005778
Bill Wendling6f287b22008-09-30 21:22:07 +00005779 // If not DWORD aligned or size is more than the threshold, call the library.
5780 // The libc version is likely to be faster for these cases. It can use the
5781 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005782 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005783 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005784 ConstantSize->getZExtValue() >
5785 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005786 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005787
5788 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005789 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005790
Bill Wendling6158d842008-10-01 00:59:58 +00005791 if (const char *bzeroEntry = V &&
5792 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5793 MVT IntPtr = getPointerTy();
5794 const Type *IntPtrTy = TD->getIntPtrType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005795 TargetLowering::ArgListTy Args;
Bill Wendling6158d842008-10-01 00:59:58 +00005796 TargetLowering::ArgListEntry Entry;
5797 Entry.Node = Dst;
5798 Entry.Ty = IntPtrTy;
5799 Args.push_back(Entry);
5800 Entry.Node = Size;
5801 Args.push_back(Entry);
5802 std::pair<SDValue,SDValue> CallResult =
Scott Michelfdc40a02009-02-17 22:15:04 +00005803 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
Dan Gohman98ca4f22009-08-05 01:29:28 +00005804 0, CallingConv::C, false, /*isReturnValueUsed=*/false,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005805 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG, dl);
Bill Wendling6158d842008-10-01 00:59:58 +00005806 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005807 }
5808
Dan Gohman707e0182008-04-12 04:36:06 +00005809 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005810 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005811 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005812
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005813 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005814 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005815 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005816 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005817 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005818 unsigned BytesLeft = 0;
5819 bool TwoRepStos = false;
5820 if (ValC) {
5821 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005822 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005823
Evan Cheng0db9fe62006-04-25 20:13:52 +00005824 // If the value is a constant, then we can potentially use larger sets.
5825 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005826 case 2: // WORD aligned
5827 AVT = MVT::i16;
5828 ValReg = X86::AX;
5829 Val = (Val << 8) | Val;
5830 break;
5831 case 0: // DWORD aligned
5832 AVT = MVT::i32;
5833 ValReg = X86::EAX;
5834 Val = (Val << 8) | Val;
5835 Val = (Val << 16) | Val;
5836 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5837 AVT = MVT::i64;
5838 ValReg = X86::RAX;
5839 Val = (Val << 32) | Val;
5840 }
5841 break;
5842 default: // Byte aligned
5843 AVT = MVT::i8;
5844 ValReg = X86::AL;
5845 Count = DAG.getIntPtrConstant(SizeVal);
5846 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005847 }
5848
Duncan Sands8e4eb092008-06-08 20:54:56 +00005849 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005850 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005851 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5852 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005853 }
5854
Dale Johannesen0f502f62009-02-03 22:26:09 +00005855 Chain = DAG.getCopyToReg(Chain, dl, ValReg, DAG.getConstant(Val, AVT),
Evan Cheng0db9fe62006-04-25 20:13:52 +00005856 InFlag);
5857 InFlag = Chain.getValue(1);
5858 } else {
5859 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005860 Count = DAG.getIntPtrConstant(SizeVal);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005861 Chain = DAG.getCopyToReg(Chain, dl, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005862 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005863 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005864
Scott Michelfdc40a02009-02-17 22:15:04 +00005865 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005866 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005867 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005868 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005869 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005870 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005871 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005872 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005873
Chris Lattnerd96d0722007-02-25 06:40:16 +00005874 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005875 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005876 Ops.push_back(Chain);
5877 Ops.push_back(DAG.getValueType(AVT));
5878 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005879 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005880
Evan Cheng0db9fe62006-04-25 20:13:52 +00005881 if (TwoRepStos) {
5882 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005883 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005884 MVT CVT = Count.getValueType();
Dale Johannesen0f502f62009-02-03 22:26:09 +00005885 SDValue Left = DAG.getNode(ISD::AND, dl, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005886 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
Scott Michelfdc40a02009-02-17 22:15:04 +00005887 Chain = DAG.getCopyToReg(Chain, dl, (CVT == MVT::i64) ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005888 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005889 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005890 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005891 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005892 Ops.clear();
5893 Ops.push_back(Chain);
5894 Ops.push_back(DAG.getValueType(MVT::i8));
5895 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005896 Chain = DAG.getNode(X86ISD::REP_STOS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005897 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005898 // Handle the last 1 - 7 bytes.
5899 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005900 MVT AddrVT = Dst.getValueType();
5901 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005902
Dale Johannesen0f502f62009-02-03 22:26:09 +00005903 Chain = DAG.getMemset(Chain, dl,
5904 DAG.getNode(ISD::ADD, dl, AddrVT, Dst,
Dan Gohman707e0182008-04-12 04:36:06 +00005905 DAG.getConstant(Offset, AddrVT)),
5906 Src,
5907 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005908 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005909 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005910
Dan Gohman707e0182008-04-12 04:36:06 +00005911 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005912 return Chain;
5913}
Evan Cheng11e15b32006-04-03 20:53:28 +00005914
Dan Gohman475871a2008-07-27 21:46:04 +00005915SDValue
Dale Johannesen0f502f62009-02-03 22:26:09 +00005916X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG, DebugLoc dl,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005917 SDValue Chain, SDValue Dst, SDValue Src,
5918 SDValue Size, unsigned Align,
5919 bool AlwaysInline,
5920 const Value *DstSV, uint64_t DstSVOff,
Scott Michelfdc40a02009-02-17 22:15:04 +00005921 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005922 // This requires the copy size to be a constant, preferrably
5923 // within a subtarget-specific limit.
5924 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5925 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005926 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005927 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005928 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005929 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005930
Evan Cheng1887c1c2008-08-21 21:00:15 +00005931 /// If not DWORD aligned, call the library.
5932 if ((Align & 3) != 0)
5933 return SDValue();
5934
5935 // DWORD aligned
5936 MVT AVT = MVT::i32;
5937 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005938 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005939
Duncan Sands83ec4b62008-06-06 12:08:01 +00005940 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005941 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005942 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005943 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005944
Dan Gohman475871a2008-07-27 21:46:04 +00005945 SDValue InFlag(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00005946 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RCX :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005947 X86::ECX,
Evan Cheng25ab6902006-09-08 06:48:29 +00005948 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005949 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005950 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RDI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005951 X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005952 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005953 InFlag = Chain.getValue(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00005954 Chain = DAG.getCopyToReg(Chain, dl, Subtarget->is64Bit() ? X86::RSI :
Dale Johannesen0f502f62009-02-03 22:26:09 +00005955 X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005956 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005957 InFlag = Chain.getValue(1);
5958
Chris Lattnerd96d0722007-02-25 06:40:16 +00005959 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005960 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005961 Ops.push_back(Chain);
5962 Ops.push_back(DAG.getValueType(AVT));
5963 Ops.push_back(InFlag);
Dale Johannesen0f502f62009-02-03 22:26:09 +00005964 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, dl, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005965
Dan Gohman475871a2008-07-27 21:46:04 +00005966 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005967 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005968 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005969 // Handle the last 1 - 7 bytes.
5970 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005971 MVT DstVT = Dst.getValueType();
5972 MVT SrcVT = Src.getValueType();
5973 MVT SizeVT = Size.getValueType();
Scott Michelfdc40a02009-02-17 22:15:04 +00005974 Results.push_back(DAG.getMemcpy(Chain, dl,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005975 DAG.getNode(ISD::ADD, dl, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005976 DAG.getConstant(Offset, DstVT)),
Dale Johannesen0f502f62009-02-03 22:26:09 +00005977 DAG.getNode(ISD::ADD, dl, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005978 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005979 DAG.getConstant(BytesLeft, SizeVT),
5980 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005981 DstSV, DstSVOff + Offset,
5982 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005983 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005984
Scott Michelfdc40a02009-02-17 22:15:04 +00005985 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesen0f502f62009-02-03 22:26:09 +00005986 &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005987}
5988
Dan Gohman475871a2008-07-27 21:46:04 +00005989SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005990 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00005991 DebugLoc dl = Op.getDebugLoc();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005992
Evan Cheng25ab6902006-09-08 06:48:29 +00005993 if (!Subtarget->is64Bit()) {
5994 // vastart just stores the address of the VarArgsFrameIndex slot into the
5995 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005996 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00005997 return DAG.getStore(Op.getOperand(0), dl, FR, Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005998 }
5999
6000 // __va_list_tag:
6001 // gp_offset (0 - 6 * 8)
6002 // fp_offset (48 - 48 + 8 * 16)
6003 // overflow_arg_area (point to parameters coming in memory).
6004 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00006005 SmallVector<SDValue, 8> MemOps;
6006 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00006007 // Store gp_offset
Dale Johannesene4d209d2009-02-03 20:21:25 +00006008 SDValue Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00006009 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006010 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006011 MemOps.push_back(Store);
6012
6013 // Store fp_offset
Scott Michelfdc40a02009-02-17 22:15:04 +00006014 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006015 FIN, DAG.getIntPtrConstant(4));
6016 Store = DAG.getStore(Op.getOperand(0), dl,
Evan Cheng786225a2006-10-05 23:01:46 +00006017 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00006018 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006019 MemOps.push_back(Store);
6020
6021 // Store ptr to overflow_arg_area
Scott Michelfdc40a02009-02-17 22:15:04 +00006022 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006023 FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00006024 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006025 Store = DAG.getStore(Op.getOperand(0), dl, OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006026 MemOps.push_back(Store);
6027
6028 // Store ptr to reg_save_area.
Scott Michelfdc40a02009-02-17 22:15:04 +00006029 FIN = DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006030 FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00006031 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dale Johannesene4d209d2009-02-03 20:21:25 +00006032 Store = DAG.getStore(Op.getOperand(0), dl, RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00006033 MemOps.push_back(Store);
Scott Michelfdc40a02009-02-17 22:15:04 +00006034 return DAG.getNode(ISD::TokenFactor, dl, MVT::Other,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006035 &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00006036}
6037
Dan Gohman475871a2008-07-27 21:46:04 +00006038SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00006039 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
6040 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00006041 SDValue Chain = Op.getOperand(0);
6042 SDValue SrcPtr = Op.getOperand(1);
6043 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00006044
Torok Edwindac237e2009-07-08 20:53:28 +00006045 llvm_report_error("VAArgInst is not yet implemented for x86-64!");
Dan Gohman475871a2008-07-27 21:46:04 +00006046 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00006047}
6048
Dan Gohman475871a2008-07-27 21:46:04 +00006049SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00006050 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00006051 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00006052 SDValue Chain = Op.getOperand(0);
6053 SDValue DstPtr = Op.getOperand(1);
6054 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00006055 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
6056 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006057 DebugLoc dl = Op.getDebugLoc();
Evan Chengae642192007-03-02 23:16:35 +00006058
Dale Johannesendd64c412009-02-04 00:33:20 +00006059 return DAG.getMemcpy(Chain, dl, DstPtr, SrcPtr,
Dan Gohman28269132008-04-18 20:55:41 +00006060 DAG.getIntPtrConstant(24), 8, false,
6061 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00006062}
6063
Dan Gohman475871a2008-07-27 21:46:04 +00006064SDValue
6065X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006066 DebugLoc dl = Op.getDebugLoc();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00006067 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00006068 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00006069 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00006070 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00006071 case Intrinsic::x86_sse_comieq_ss:
6072 case Intrinsic::x86_sse_comilt_ss:
6073 case Intrinsic::x86_sse_comile_ss:
6074 case Intrinsic::x86_sse_comigt_ss:
6075 case Intrinsic::x86_sse_comige_ss:
6076 case Intrinsic::x86_sse_comineq_ss:
6077 case Intrinsic::x86_sse_ucomieq_ss:
6078 case Intrinsic::x86_sse_ucomilt_ss:
6079 case Intrinsic::x86_sse_ucomile_ss:
6080 case Intrinsic::x86_sse_ucomigt_ss:
6081 case Intrinsic::x86_sse_ucomige_ss:
6082 case Intrinsic::x86_sse_ucomineq_ss:
6083 case Intrinsic::x86_sse2_comieq_sd:
6084 case Intrinsic::x86_sse2_comilt_sd:
6085 case Intrinsic::x86_sse2_comile_sd:
6086 case Intrinsic::x86_sse2_comigt_sd:
6087 case Intrinsic::x86_sse2_comige_sd:
6088 case Intrinsic::x86_sse2_comineq_sd:
6089 case Intrinsic::x86_sse2_ucomieq_sd:
6090 case Intrinsic::x86_sse2_ucomilt_sd:
6091 case Intrinsic::x86_sse2_ucomile_sd:
6092 case Intrinsic::x86_sse2_ucomigt_sd:
6093 case Intrinsic::x86_sse2_ucomige_sd:
6094 case Intrinsic::x86_sse2_ucomineq_sd: {
6095 unsigned Opc = 0;
6096 ISD::CondCode CC = ISD::SETCC_INVALID;
6097 switch (IntNo) {
6098 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006099 case Intrinsic::x86_sse_comieq_ss:
6100 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006101 Opc = X86ISD::COMI;
6102 CC = ISD::SETEQ;
6103 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006104 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006105 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006106 Opc = X86ISD::COMI;
6107 CC = ISD::SETLT;
6108 break;
6109 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006110 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006111 Opc = X86ISD::COMI;
6112 CC = ISD::SETLE;
6113 break;
6114 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006115 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006116 Opc = X86ISD::COMI;
6117 CC = ISD::SETGT;
6118 break;
6119 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006120 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006121 Opc = X86ISD::COMI;
6122 CC = ISD::SETGE;
6123 break;
6124 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006125 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006126 Opc = X86ISD::COMI;
6127 CC = ISD::SETNE;
6128 break;
6129 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006130 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006131 Opc = X86ISD::UCOMI;
6132 CC = ISD::SETEQ;
6133 break;
6134 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006135 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006136 Opc = X86ISD::UCOMI;
6137 CC = ISD::SETLT;
6138 break;
6139 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006140 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006141 Opc = X86ISD::UCOMI;
6142 CC = ISD::SETLE;
6143 break;
6144 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006145 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006146 Opc = X86ISD::UCOMI;
6147 CC = ISD::SETGT;
6148 break;
6149 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00006150 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00006151 Opc = X86ISD::UCOMI;
6152 CC = ISD::SETGE;
6153 break;
6154 case Intrinsic::x86_sse_ucomineq_ss:
6155 case Intrinsic::x86_sse2_ucomineq_sd:
6156 Opc = X86ISD::UCOMI;
6157 CC = ISD::SETNE;
6158 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00006159 }
Evan Cheng734503b2006-09-11 02:19:56 +00006160
Dan Gohman475871a2008-07-27 21:46:04 +00006161 SDValue LHS = Op.getOperand(1);
6162 SDValue RHS = Op.getOperand(2);
Chris Lattner1c39d4c2008-12-24 23:53:05 +00006163 unsigned X86CC = TranslateX86CC(CC, true, LHS, RHS, DAG);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006164 SDValue Cond = DAG.getNode(Opc, dl, MVT::i32, LHS, RHS);
6165 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00006166 DAG.getConstant(X86CC, MVT::i8), Cond);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006167 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00006168 }
Eric Christopher71c67532009-07-29 00:28:05 +00006169 // ptest intrinsics. The intrinsic these come from are designed to return
Eric Christopher794bfed2009-07-29 01:01:19 +00006170 // an integer value, not just an instruction so lower it to the ptest
6171 // pattern and a setcc for the result.
Eric Christopher71c67532009-07-29 00:28:05 +00006172 case Intrinsic::x86_sse41_ptestz:
6173 case Intrinsic::x86_sse41_ptestc:
6174 case Intrinsic::x86_sse41_ptestnzc:{
6175 unsigned X86CC = 0;
6176 switch (IntNo) {
Eric Christopher978dae32009-07-29 18:14:04 +00006177 default: llvm_unreachable("Bad fallthrough in Intrinsic lowering.");
Eric Christopher71c67532009-07-29 00:28:05 +00006178 case Intrinsic::x86_sse41_ptestz:
6179 // ZF = 1
6180 X86CC = X86::COND_E;
6181 break;
6182 case Intrinsic::x86_sse41_ptestc:
6183 // CF = 1
6184 X86CC = X86::COND_B;
6185 break;
6186 case Intrinsic::x86_sse41_ptestnzc:
6187 // ZF and CF = 0
6188 X86CC = X86::COND_A;
6189 break;
6190 }
6191
6192 SDValue LHS = Op.getOperand(1);
6193 SDValue RHS = Op.getOperand(2);
6194 SDValue Test = DAG.getNode(X86ISD::PTEST, dl, MVT::i32, LHS, RHS);
6195 SDValue CC = DAG.getConstant(X86CC, MVT::i8);
6196 SDValue SetCC = DAG.getNode(X86ISD::SETCC, dl, MVT::i8, CC, Test);
6197 return DAG.getNode(ISD::ZERO_EXTEND, dl, MVT::i32, SetCC);
6198 }
Evan Cheng5759f972008-05-04 09:15:50 +00006199
6200 // Fix vector shift instructions where the last operand is a non-immediate
6201 // i32 value.
6202 case Intrinsic::x86_sse2_pslli_w:
6203 case Intrinsic::x86_sse2_pslli_d:
6204 case Intrinsic::x86_sse2_pslli_q:
6205 case Intrinsic::x86_sse2_psrli_w:
6206 case Intrinsic::x86_sse2_psrli_d:
6207 case Intrinsic::x86_sse2_psrli_q:
6208 case Intrinsic::x86_sse2_psrai_w:
6209 case Intrinsic::x86_sse2_psrai_d:
6210 case Intrinsic::x86_mmx_pslli_w:
6211 case Intrinsic::x86_mmx_pslli_d:
6212 case Intrinsic::x86_mmx_pslli_q:
6213 case Intrinsic::x86_mmx_psrli_w:
6214 case Intrinsic::x86_mmx_psrli_d:
6215 case Intrinsic::x86_mmx_psrli_q:
6216 case Intrinsic::x86_mmx_psrai_w:
6217 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00006218 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00006219 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00006220 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00006221
6222 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006223 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00006224 switch (IntNo) {
6225 case Intrinsic::x86_sse2_pslli_w:
6226 NewIntNo = Intrinsic::x86_sse2_psll_w;
6227 break;
6228 case Intrinsic::x86_sse2_pslli_d:
6229 NewIntNo = Intrinsic::x86_sse2_psll_d;
6230 break;
6231 case Intrinsic::x86_sse2_pslli_q:
6232 NewIntNo = Intrinsic::x86_sse2_psll_q;
6233 break;
6234 case Intrinsic::x86_sse2_psrli_w:
6235 NewIntNo = Intrinsic::x86_sse2_psrl_w;
6236 break;
6237 case Intrinsic::x86_sse2_psrli_d:
6238 NewIntNo = Intrinsic::x86_sse2_psrl_d;
6239 break;
6240 case Intrinsic::x86_sse2_psrli_q:
6241 NewIntNo = Intrinsic::x86_sse2_psrl_q;
6242 break;
6243 case Intrinsic::x86_sse2_psrai_w:
6244 NewIntNo = Intrinsic::x86_sse2_psra_w;
6245 break;
6246 case Intrinsic::x86_sse2_psrai_d:
6247 NewIntNo = Intrinsic::x86_sse2_psra_d;
6248 break;
6249 default: {
6250 ShAmtVT = MVT::v2i32;
6251 switch (IntNo) {
6252 case Intrinsic::x86_mmx_pslli_w:
6253 NewIntNo = Intrinsic::x86_mmx_psll_w;
6254 break;
6255 case Intrinsic::x86_mmx_pslli_d:
6256 NewIntNo = Intrinsic::x86_mmx_psll_d;
6257 break;
6258 case Intrinsic::x86_mmx_pslli_q:
6259 NewIntNo = Intrinsic::x86_mmx_psll_q;
6260 break;
6261 case Intrinsic::x86_mmx_psrli_w:
6262 NewIntNo = Intrinsic::x86_mmx_psrl_w;
6263 break;
6264 case Intrinsic::x86_mmx_psrli_d:
6265 NewIntNo = Intrinsic::x86_mmx_psrl_d;
6266 break;
6267 case Intrinsic::x86_mmx_psrli_q:
6268 NewIntNo = Intrinsic::x86_mmx_psrl_q;
6269 break;
6270 case Intrinsic::x86_mmx_psrai_w:
6271 NewIntNo = Intrinsic::x86_mmx_psra_w;
6272 break;
6273 case Intrinsic::x86_mmx_psrai_d:
6274 NewIntNo = Intrinsic::x86_mmx_psra_d;
6275 break;
Torok Edwinc23197a2009-07-14 16:55:14 +00006276 default: llvm_unreachable("Impossible intrinsic"); // Can't reach here.
Evan Cheng5759f972008-05-04 09:15:50 +00006277 }
6278 break;
6279 }
6280 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00006281 MVT VT = Op.getValueType();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006282 ShAmt = DAG.getNode(ISD::BIT_CONVERT, dl, VT,
6283 DAG.getNode(ISD::SCALAR_TO_VECTOR, dl, ShAmtVT, ShAmt));
6284 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Evan Cheng5759f972008-05-04 09:15:50 +00006285 DAG.getConstant(NewIntNo, MVT::i32),
6286 Op.getOperand(1), ShAmt);
6287 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00006288 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00006289}
Evan Cheng72261582005-12-20 06:22:03 +00006290
Dan Gohman475871a2008-07-27 21:46:04 +00006291SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Bill Wendling64e87322009-01-16 19:25:27 +00006292 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006293 DebugLoc dl = Op.getDebugLoc();
Bill Wendling64e87322009-01-16 19:25:27 +00006294
6295 if (Depth > 0) {
6296 SDValue FrameAddr = LowerFRAMEADDR(Op, DAG);
6297 SDValue Offset =
6298 DAG.getConstant(TD->getPointerSize(),
6299 Subtarget->is64Bit() ? MVT::i64 : MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006300 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Scott Michelfdc40a02009-02-17 22:15:04 +00006301 DAG.getNode(ISD::ADD, dl, getPointerTy(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006302 FrameAddr, Offset),
Bill Wendling64e87322009-01-16 19:25:27 +00006303 NULL, 0);
6304 }
6305
6306 // Just load the return address.
Dan Gohman475871a2008-07-27 21:46:04 +00006307 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Scott Michelfdc40a02009-02-17 22:15:04 +00006308 return DAG.getLoad(getPointerTy(), dl, DAG.getEntryNode(),
Dale Johannesene4d209d2009-02-03 20:21:25 +00006309 RetAddrFI, NULL, 0);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006310}
6311
Dan Gohman475871a2008-07-27 21:46:04 +00006312SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00006313 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
6314 MFI->setFrameAddressIsTaken(true);
6315 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006316 DebugLoc dl = Op.getDebugLoc(); // FIXME probably not meaningful
Evan Cheng184793f2008-09-27 01:56:22 +00006317 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
6318 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
Dale Johannesendd64c412009-02-04 00:33:20 +00006319 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), dl, FrameReg, VT);
Evan Cheng184793f2008-09-27 01:56:22 +00006320 while (Depth--)
Dale Johannesendd64c412009-02-04 00:33:20 +00006321 FrameAddr = DAG.getLoad(VT, dl, DAG.getEntryNode(), FrameAddr, NULL, 0);
Evan Cheng184793f2008-09-27 01:56:22 +00006322 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00006323}
6324
Dan Gohman475871a2008-07-27 21:46:04 +00006325SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00006326 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006327 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006328}
6329
Dan Gohman475871a2008-07-27 21:46:04 +00006330SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006331{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006332 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00006333 SDValue Chain = Op.getOperand(0);
6334 SDValue Offset = Op.getOperand(1);
6335 SDValue Handler = Op.getOperand(2);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006336 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006337
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006338 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
6339 getPointerTy());
6340 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006341
Dale Johannesene4d209d2009-02-03 20:21:25 +00006342 SDValue StoreAddr = DAG.getNode(ISD::SUB, dl, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006343 DAG.getIntPtrConstant(-TD->getPointerSize()));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006344 StoreAddr = DAG.getNode(ISD::ADD, dl, getPointerTy(), StoreAddr, Offset);
6345 Chain = DAG.getStore(Chain, dl, Handler, StoreAddr, NULL, 0);
Dale Johannesendd64c412009-02-04 00:33:20 +00006346 Chain = DAG.getCopyToReg(Chain, dl, StoreAddrReg, StoreAddr);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006347 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006348
Dale Johannesene4d209d2009-02-03 20:21:25 +00006349 return DAG.getNode(X86ISD::EH_RETURN, dl,
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00006350 MVT::Other,
6351 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006352}
6353
Dan Gohman475871a2008-07-27 21:46:04 +00006354SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00006355 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00006356 SDValue Root = Op.getOperand(0);
6357 SDValue Trmp = Op.getOperand(1); // trampoline
6358 SDValue FPtr = Op.getOperand(2); // nested function
6359 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006360 DebugLoc dl = Op.getDebugLoc();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006361
Dan Gohman69de1932008-02-06 22:27:42 +00006362 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006363
Duncan Sands339e14f2008-01-16 22:55:25 +00006364 const X86InstrInfo *TII =
6365 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
6366
Duncan Sandsb116fac2007-07-27 20:02:49 +00006367 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006368 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00006369
6370 // Large code-model.
6371
6372 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
6373 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
6374
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006375 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
6376 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00006377
6378 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
6379
6380 // Load the pointer to the nested function into R11.
6381 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00006382 SDValue Addr = Trmp;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006383 OutChains[0] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6384 Addr, TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00006385
Scott Michelfdc40a02009-02-17 22:15:04 +00006386 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006387 DAG.getConstant(2, MVT::i64));
6388 OutChains[1] = DAG.getStore(Root, dl, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006389
6390 // Load the 'nest' parameter value into R10.
6391 // R10 is specified in X86CallingConv.td
6392 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
Scott Michelfdc40a02009-02-17 22:15:04 +00006393 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006394 DAG.getConstant(10, MVT::i64));
6395 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6396 Addr, TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00006397
Scott Michelfdc40a02009-02-17 22:15:04 +00006398 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006399 DAG.getConstant(12, MVT::i64));
6400 OutChains[3] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00006401
6402 // Jump to the nested function.
6403 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
Scott Michelfdc40a02009-02-17 22:15:04 +00006404 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006405 DAG.getConstant(20, MVT::i64));
6406 OutChains[4] = DAG.getStore(Root, dl, DAG.getConstant(OpCode, MVT::i16),
6407 Addr, TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00006408
6409 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
Scott Michelfdc40a02009-02-17 22:15:04 +00006410 Addr = DAG.getNode(ISD::ADD, dl, MVT::i64, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006411 DAG.getConstant(22, MVT::i64));
6412 OutChains[5] = DAG.getStore(Root, dl, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006413 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00006414
Dan Gohman475871a2008-07-27 21:46:04 +00006415 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006416 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 6) };
6417 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006418 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00006419 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00006420 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
6421 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00006422 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006423
6424 switch (CC) {
6425 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00006426 llvm_unreachable("Unsupported calling convention");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006427 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006428 case CallingConv::X86_StdCall: {
6429 // Pass 'nest' parameter in ECX.
6430 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006431 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006432
6433 // Check that ECX wasn't needed by an 'inreg' parameter.
6434 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00006435 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00006436
Chris Lattner58d74912008-03-12 17:45:29 +00006437 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00006438 unsigned InRegCount = 0;
6439 unsigned Idx = 1;
6440
6441 for (FunctionType::param_iterator I = FTy->param_begin(),
6442 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00006443 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00006444 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00006445 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006446
6447 if (InRegCount > 2) {
Torok Edwinab7c09b2009-07-08 18:01:40 +00006448 llvm_report_error("Nest register in use - reduce number of inreg parameters!");
Duncan Sandsb116fac2007-07-27 20:02:49 +00006449 }
6450 }
6451 break;
6452 }
6453 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00006454 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00006455 // Pass 'nest' parameter in EAX.
6456 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00006457 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006458 break;
6459 }
6460
Dan Gohman475871a2008-07-27 21:46:04 +00006461 SDValue OutChains[4];
6462 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00006463
Scott Michelfdc40a02009-02-17 22:15:04 +00006464 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006465 DAG.getConstant(10, MVT::i32));
6466 Disp = DAG.getNode(ISD::SUB, dl, MVT::i32, FPtr, Addr);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006467
Duncan Sands339e14f2008-01-16 22:55:25 +00006468 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006469 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Scott Michelfdc40a02009-02-17 22:15:04 +00006470 OutChains[0] = DAG.getStore(Root, dl,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006471 DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006472 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006473
Scott Michelfdc40a02009-02-17 22:15:04 +00006474 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006475 DAG.getConstant(1, MVT::i32));
6476 OutChains[1] = DAG.getStore(Root, dl, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006477
Duncan Sands339e14f2008-01-16 22:55:25 +00006478 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Scott Michelfdc40a02009-02-17 22:15:04 +00006479 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006480 DAG.getConstant(5, MVT::i32));
6481 OutChains[2] = DAG.getStore(Root, dl, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006482 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006483
Scott Michelfdc40a02009-02-17 22:15:04 +00006484 Addr = DAG.getNode(ISD::ADD, dl, MVT::i32, Trmp,
Dale Johannesene4d209d2009-02-03 20:21:25 +00006485 DAG.getConstant(6, MVT::i32));
6486 OutChains[3] = DAG.getStore(Root, dl, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006487
Dan Gohman475871a2008-07-27 21:46:04 +00006488 SDValue Ops[] =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006489 { Trmp, DAG.getNode(ISD::TokenFactor, dl, MVT::Other, OutChains, 4) };
6490 return DAG.getMergeValues(Ops, 2, dl);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006491 }
6492}
6493
Dan Gohman475871a2008-07-27 21:46:04 +00006494SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006495 /*
6496 The rounding mode is in bits 11:10 of FPSR, and has the following
6497 settings:
6498 00 Round to nearest
6499 01 Round to -inf
6500 10 Round to +inf
6501 11 Round to 0
6502
6503 FLT_ROUNDS, on the other hand, expects the following:
6504 -1 Undefined
6505 0 Round to 0
6506 1 Round to nearest
6507 2 Round to +inf
6508 3 Round to -inf
6509
6510 To perform the conversion, we do:
6511 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6512 */
6513
6514 MachineFunction &MF = DAG.getMachineFunction();
6515 const TargetMachine &TM = MF.getTarget();
6516 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6517 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006518 MVT VT = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006519 DebugLoc dl = Op.getDebugLoc();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006520
6521 // Save FP Control Word to stack slot
6522 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006523 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006524
Dale Johannesene4d209d2009-02-03 20:21:25 +00006525 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, dl, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006526 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006527
6528 // Load FP Control Word from stack slot
Dale Johannesene4d209d2009-02-03 20:21:25 +00006529 SDValue CWD = DAG.getLoad(MVT::i16, dl, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006530
6531 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006532 SDValue CWD1 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006533 DAG.getNode(ISD::SRL, dl, MVT::i16,
6534 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006535 CWD, DAG.getConstant(0x800, MVT::i16)),
6536 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006537 SDValue CWD2 =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006538 DAG.getNode(ISD::SRL, dl, MVT::i16,
6539 DAG.getNode(ISD::AND, dl, MVT::i16,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006540 CWD, DAG.getConstant(0x400, MVT::i16)),
6541 DAG.getConstant(9, MVT::i8));
6542
Dan Gohman475871a2008-07-27 21:46:04 +00006543 SDValue RetVal =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006544 DAG.getNode(ISD::AND, dl, MVT::i16,
6545 DAG.getNode(ISD::ADD, dl, MVT::i16,
6546 DAG.getNode(ISD::OR, dl, MVT::i16, CWD1, CWD2),
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006547 DAG.getConstant(1, MVT::i16)),
6548 DAG.getConstant(3, MVT::i16));
6549
6550
Duncan Sands83ec4b62008-06-06 12:08:01 +00006551 return DAG.getNode((VT.getSizeInBits() < 16 ?
Dale Johannesenb300d2a2009-02-07 00:55:49 +00006552 ISD::TRUNCATE : ISD::ZERO_EXTEND), dl, VT, RetVal);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006553}
6554
Dan Gohman475871a2008-07-27 21:46:04 +00006555SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006556 MVT VT = Op.getValueType();
6557 MVT OpVT = VT;
6558 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006559 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006560
6561 Op = Op.getOperand(0);
6562 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006563 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006564 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006565 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006566 }
Evan Cheng18efe262007-12-14 02:13:44 +00006567
Evan Cheng152804e2007-12-14 08:30:15 +00006568 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6569 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006570 Op = DAG.getNode(X86ISD::BSR, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006571
6572 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006573 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006574 Ops.push_back(Op);
6575 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6576 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6577 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006578 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006579
6580 // Finally xor with NumBits-1.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006581 Op = DAG.getNode(ISD::XOR, dl, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
Evan Cheng152804e2007-12-14 08:30:15 +00006582
Evan Cheng18efe262007-12-14 02:13:44 +00006583 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006584 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006585 return Op;
6586}
6587
Dan Gohman475871a2008-07-27 21:46:04 +00006588SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006589 MVT VT = Op.getValueType();
6590 MVT OpVT = VT;
6591 unsigned NumBits = VT.getSizeInBits();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006592 DebugLoc dl = Op.getDebugLoc();
Evan Cheng18efe262007-12-14 02:13:44 +00006593
6594 Op = Op.getOperand(0);
6595 if (VT == MVT::i8) {
6596 OpVT = MVT::i32;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006597 Op = DAG.getNode(ISD::ZERO_EXTEND, dl, OpVT, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006598 }
Evan Cheng152804e2007-12-14 08:30:15 +00006599
6600 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6601 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006602 Op = DAG.getNode(X86ISD::BSF, dl, VTs, Op);
Evan Cheng152804e2007-12-14 08:30:15 +00006603
6604 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006605 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006606 Ops.push_back(Op);
6607 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6608 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6609 Ops.push_back(Op.getValue(1));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006610 Op = DAG.getNode(X86ISD::CMOV, dl, OpVT, &Ops[0], 4);
Evan Cheng152804e2007-12-14 08:30:15 +00006611
Evan Cheng18efe262007-12-14 02:13:44 +00006612 if (VT == MVT::i8)
Dale Johannesene4d209d2009-02-03 20:21:25 +00006613 Op = DAG.getNode(ISD::TRUNCATE, dl, MVT::i8, Op);
Evan Cheng18efe262007-12-14 02:13:44 +00006614 return Op;
6615}
6616
Mon P Wangaf9b9522008-12-18 21:42:19 +00006617SDValue X86TargetLowering::LowerMUL_V2I64(SDValue Op, SelectionDAG &DAG) {
6618 MVT VT = Op.getValueType();
6619 assert(VT == MVT::v2i64 && "Only know how to lower V2I64 multiply");
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006620 DebugLoc dl = Op.getDebugLoc();
Scott Michelfdc40a02009-02-17 22:15:04 +00006621
Mon P Wangaf9b9522008-12-18 21:42:19 +00006622 // ulong2 Ahi = __builtin_ia32_psrlqi128( a, 32);
6623 // ulong2 Bhi = __builtin_ia32_psrlqi128( b, 32);
6624 // ulong2 AloBlo = __builtin_ia32_pmuludq128( a, b );
6625 // ulong2 AloBhi = __builtin_ia32_pmuludq128( a, Bhi );
6626 // ulong2 AhiBlo = __builtin_ia32_pmuludq128( Ahi, b );
6627 //
6628 // AloBhi = __builtin_ia32_psllqi128( AloBhi, 32 );
6629 // AhiBlo = __builtin_ia32_psllqi128( AhiBlo, 32 );
6630 // return AloBlo + AloBhi + AhiBlo;
6631
6632 SDValue A = Op.getOperand(0);
6633 SDValue B = Op.getOperand(1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006634
Dale Johannesene4d209d2009-02-03 20:21:25 +00006635 SDValue Ahi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006636 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6637 A, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006638 SDValue Bhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006639 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
6640 B, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006641 SDValue AloBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006642 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6643 A, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006644 SDValue AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006645 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6646 A, Bhi);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006647 SDValue AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006648 DAG.getConstant(Intrinsic::x86_sse2_pmulu_dq, MVT::i32),
6649 Ahi, B);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006650 AloBhi = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006651 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6652 AloBhi, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006653 AhiBlo = DAG.getNode(ISD::INTRINSIC_WO_CHAIN, dl, VT,
Mon P Wangaf9b9522008-12-18 21:42:19 +00006654 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
6655 AhiBlo, DAG.getConstant(32, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006656 SDValue Res = DAG.getNode(ISD::ADD, dl, VT, AloBlo, AloBhi);
6657 Res = DAG.getNode(ISD::ADD, dl, VT, Res, AhiBlo);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006658 return Res;
6659}
6660
6661
Bill Wendling74c37652008-12-09 22:08:41 +00006662SDValue X86TargetLowering::LowerXALUO(SDValue Op, SelectionDAG &DAG) {
6663 // Lower the "add/sub/mul with overflow" instruction into a regular ins plus
6664 // a "setcc" instruction that checks the overflow flag. The "brcond" lowering
Bill Wendling61edeb52008-12-02 01:06:39 +00006665 // looks for this combo and may remove the "setcc" instruction if the "setcc"
6666 // has only one use.
Bill Wendling3fafd932008-11-26 22:37:40 +00006667 SDNode *N = Op.getNode();
Bill Wendling61edeb52008-12-02 01:06:39 +00006668 SDValue LHS = N->getOperand(0);
6669 SDValue RHS = N->getOperand(1);
Bill Wendling74c37652008-12-09 22:08:41 +00006670 unsigned BaseOp = 0;
6671 unsigned Cond = 0;
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006672 DebugLoc dl = Op.getDebugLoc();
Bill Wendling74c37652008-12-09 22:08:41 +00006673
6674 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006675 default: llvm_unreachable("Unknown ovf instruction!");
Bill Wendling74c37652008-12-09 22:08:41 +00006676 case ISD::SADDO:
Dan Gohman076aee32009-03-04 19:44:21 +00006677 // A subtract of one will be selected as a INC. Note that INC doesn't
6678 // set CF, so we can't do this for UADDO.
6679 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6680 if (C->getAPIntValue() == 1) {
6681 BaseOp = X86ISD::INC;
6682 Cond = X86::COND_O;
6683 break;
6684 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006685 BaseOp = X86ISD::ADD;
Bill Wendling74c37652008-12-09 22:08:41 +00006686 Cond = X86::COND_O;
6687 break;
6688 case ISD::UADDO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006689 BaseOp = X86ISD::ADD;
Dan Gohman653456c2009-01-07 00:15:08 +00006690 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006691 break;
6692 case ISD::SSUBO:
Dan Gohman076aee32009-03-04 19:44:21 +00006693 // A subtract of one will be selected as a DEC. Note that DEC doesn't
6694 // set CF, so we can't do this for USUBO.
6695 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op))
6696 if (C->getAPIntValue() == 1) {
6697 BaseOp = X86ISD::DEC;
6698 Cond = X86::COND_O;
6699 break;
6700 }
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006701 BaseOp = X86ISD::SUB;
Bill Wendling74c37652008-12-09 22:08:41 +00006702 Cond = X86::COND_O;
6703 break;
6704 case ISD::USUBO:
Bill Wendlingab55ebd2008-12-12 00:56:36 +00006705 BaseOp = X86ISD::SUB;
Dan Gohman653456c2009-01-07 00:15:08 +00006706 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006707 break;
6708 case ISD::SMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006709 BaseOp = X86ISD::SMUL;
Bill Wendling74c37652008-12-09 22:08:41 +00006710 Cond = X86::COND_O;
6711 break;
6712 case ISD::UMULO:
Bill Wendlingd350e022008-12-12 21:15:41 +00006713 BaseOp = X86ISD::UMUL;
Dan Gohman653456c2009-01-07 00:15:08 +00006714 Cond = X86::COND_B;
Bill Wendling74c37652008-12-09 22:08:41 +00006715 break;
6716 }
Bill Wendling3fafd932008-11-26 22:37:40 +00006717
Bill Wendling61edeb52008-12-02 01:06:39 +00006718 // Also sets EFLAGS.
6719 SDVTList VTs = DAG.getVTList(N->getValueType(0), MVT::i32);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006720 SDValue Sum = DAG.getNode(BaseOp, dl, VTs, LHS, RHS);
Bill Wendling3fafd932008-11-26 22:37:40 +00006721
Bill Wendling61edeb52008-12-02 01:06:39 +00006722 SDValue SetCC =
Dale Johannesene4d209d2009-02-03 20:21:25 +00006723 DAG.getNode(X86ISD::SETCC, dl, N->getValueType(1),
Bill Wendlingbc5e15e2008-12-10 02:01:32 +00006724 DAG.getConstant(Cond, MVT::i32), SDValue(Sum.getNode(), 1));
Bill Wendling3fafd932008-11-26 22:37:40 +00006725
Bill Wendling61edeb52008-12-02 01:06:39 +00006726 DAG.ReplaceAllUsesOfValueWith(SDValue(N, 1), SetCC);
6727 return Sum;
Bill Wendling41ea7e72008-11-24 19:21:46 +00006728}
6729
Dan Gohman475871a2008-07-27 21:46:04 +00006730SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006731 MVT T = Op.getValueType();
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006732 DebugLoc dl = Op.getDebugLoc();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006733 unsigned Reg = 0;
6734 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006735 switch(T.getSimpleVT()) {
6736 default:
6737 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006738 case MVT::i8: Reg = X86::AL; size = 1; break;
6739 case MVT::i16: Reg = X86::AX; size = 2; break;
6740 case MVT::i32: Reg = X86::EAX; size = 4; break;
Scott Michelfdc40a02009-02-17 22:15:04 +00006741 case MVT::i64:
Duncan Sands1607f052008-12-01 11:39:25 +00006742 assert(Subtarget->is64Bit() && "Node not type legal!");
6743 Reg = X86::RAX; size = 8;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006744 break;
Bill Wendling61edeb52008-12-02 01:06:39 +00006745 }
Dale Johannesendd64c412009-02-04 00:33:20 +00006746 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), dl, Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006747 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006748 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006749 Op.getOperand(1),
6750 Op.getOperand(3),
6751 DAG.getTargetConstant(size, MVT::i8),
6752 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006753 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006754 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, dl, Tys, Ops, 5);
Scott Michelfdc40a02009-02-17 22:15:04 +00006755 SDValue cpOut =
Dale Johannesendd64c412009-02-04 00:33:20 +00006756 DAG.getCopyFromReg(Result.getValue(0), dl, Reg, T, Result.getValue(1));
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006757 return cpOut;
6758}
6759
Duncan Sands1607f052008-12-01 11:39:25 +00006760SDValue X86TargetLowering::LowerREADCYCLECOUNTER(SDValue Op,
Gabor Greif327ef032008-08-28 23:19:51 +00006761 SelectionDAG &DAG) {
Duncan Sands1607f052008-12-01 11:39:25 +00006762 assert(Subtarget->is64Bit() && "Result not type legalized?");
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006763 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Duncan Sands1607f052008-12-01 11:39:25 +00006764 SDValue TheChain = Op.getOperand(0);
Dale Johannesen6f38cb62009-02-07 19:59:05 +00006765 DebugLoc dl = Op.getDebugLoc();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006766 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Dale Johannesendd64c412009-02-04 00:33:20 +00006767 SDValue rax = DAG.getCopyFromReg(rd, dl, X86::RAX, MVT::i64, rd.getValue(1));
6768 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), dl, X86::RDX, MVT::i64,
Duncan Sands1607f052008-12-01 11:39:25 +00006769 rax.getValue(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006770 SDValue Tmp = DAG.getNode(ISD::SHL, dl, MVT::i64, rdx,
Duncan Sands1607f052008-12-01 11:39:25 +00006771 DAG.getConstant(32, MVT::i8));
6772 SDValue Ops[] = {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006773 DAG.getNode(ISD::OR, dl, MVT::i64, rax, Tmp),
Duncan Sands1607f052008-12-01 11:39:25 +00006774 rdx.getValue(1)
6775 };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006776 return DAG.getMergeValues(Ops, 2, dl);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006777}
6778
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006779SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6780 SDNode *Node = Op.getNode();
Dale Johannesene4d209d2009-02-03 20:21:25 +00006781 DebugLoc dl = Node->getDebugLoc();
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006782 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006783 SDValue negOp = DAG.getNode(ISD::SUB, dl, T,
Evan Cheng242b38b2009-02-23 09:03:22 +00006784 DAG.getConstant(0, T), Node->getOperand(2));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006785 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, dl,
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006786 cast<AtomicSDNode>(Node)->getMemoryVT(),
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006787 Node->getOperand(0),
6788 Node->getOperand(1), negOp,
6789 cast<AtomicSDNode>(Node)->getSrcValue(),
6790 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006791}
6792
Evan Cheng0db9fe62006-04-25 20:13:52 +00006793/// LowerOperation - Provide custom lowering hooks for some operations.
6794///
Dan Gohman475871a2008-07-27 21:46:04 +00006795SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006796 switch (Op.getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00006797 default: llvm_unreachable("Should not custom lower this!");
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006798 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
6799 case ISD::ATOMIC_LOAD_SUB: return LowerLOAD_SUB(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006800 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6801 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6802 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6803 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6804 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6805 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6806 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006807 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006808 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006809 case ISD::SHL_PARTS:
6810 case ISD::SRA_PARTS:
6811 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6812 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006813 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006814 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
Eli Friedman948e95a2009-05-23 09:59:16 +00006815 case ISD::FP_TO_UINT: return LowerFP_TO_UINT(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006816 case ISD::FABS: return LowerFABS(Op, DAG);
6817 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006818 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006819 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006820 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006821 case ISD::SELECT: return LowerSELECT(Op, DAG);
6822 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006823 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006824 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006825 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006826 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006827 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006828 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6829 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006830 case ISD::FRAME_TO_ARGS_OFFSET:
6831 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006832 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006833 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006834 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006835 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006836 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6837 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Mon P Wangaf9b9522008-12-18 21:42:19 +00006838 case ISD::MUL: return LowerMUL_V2I64(Op, DAG);
Bill Wendling74c37652008-12-09 22:08:41 +00006839 case ISD::SADDO:
6840 case ISD::UADDO:
6841 case ISD::SSUBO:
6842 case ISD::USUBO:
6843 case ISD::SMULO:
6844 case ISD::UMULO: return LowerXALUO(Op, DAG);
Duncan Sands1607f052008-12-01 11:39:25 +00006845 case ISD::READCYCLECOUNTER: return LowerREADCYCLECOUNTER(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006846 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006847}
6848
Duncan Sands1607f052008-12-01 11:39:25 +00006849void X86TargetLowering::
6850ReplaceATOMIC_BINARY_64(SDNode *Node, SmallVectorImpl<SDValue>&Results,
6851 SelectionDAG &DAG, unsigned NewOp) {
6852 MVT T = Node->getValueType(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006853 DebugLoc dl = Node->getDebugLoc();
Duncan Sands1607f052008-12-01 11:39:25 +00006854 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6855
6856 SDValue Chain = Node->getOperand(0);
6857 SDValue In1 = Node->getOperand(1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006858 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006859 Node->getOperand(2), DAG.getIntPtrConstant(0));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006860 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006861 Node->getOperand(2), DAG.getIntPtrConstant(1));
6862 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6863 // have a MemOperand. Pass the info through as a normal operand.
6864 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6865 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
6866 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006867 SDValue Result = DAG.getNode(NewOp, dl, Tys, Ops, 5);
Duncan Sands1607f052008-12-01 11:39:25 +00006868 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006869 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006870 Results.push_back(Result.getValue(2));
6871}
6872
Duncan Sands126d9072008-07-04 11:47:58 +00006873/// ReplaceNodeResults - Replace a node with an illegal result type
6874/// with a new node built out of custom code.
Duncan Sands1607f052008-12-01 11:39:25 +00006875void X86TargetLowering::ReplaceNodeResults(SDNode *N,
6876 SmallVectorImpl<SDValue>&Results,
6877 SelectionDAG &DAG) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00006878 DebugLoc dl = N->getDebugLoc();
Chris Lattner27a6c732007-11-24 07:07:01 +00006879 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006880 default:
Duncan Sands1607f052008-12-01 11:39:25 +00006881 assert(false && "Do not know how to custom type legalize this operation!");
6882 return;
6883 case ISD::FP_TO_SINT: {
Eli Friedman948e95a2009-05-23 09:59:16 +00006884 std::pair<SDValue,SDValue> Vals =
6885 FP_TO_INTHelper(SDValue(N, 0), DAG, true);
Duncan Sands1607f052008-12-01 11:39:25 +00006886 SDValue FIST = Vals.first, StackSlot = Vals.second;
6887 if (FIST.getNode() != 0) {
6888 MVT VT = N->getValueType(0);
6889 // Return a load from the stack slot.
Dale Johannesene4d209d2009-02-03 20:21:25 +00006890 Results.push_back(DAG.getLoad(VT, dl, FIST, StackSlot, NULL, 0));
Duncan Sands1607f052008-12-01 11:39:25 +00006891 }
6892 return;
6893 }
6894 case ISD::READCYCLECOUNTER: {
6895 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
6896 SDValue TheChain = N->getOperand(0);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006897 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, dl, Tys, &TheChain, 1);
Scott Michelfdc40a02009-02-17 22:15:04 +00006898 SDValue eax = DAG.getCopyFromReg(rd, dl, X86::EAX, MVT::i32,
Dale Johannesendd64c412009-02-04 00:33:20 +00006899 rd.getValue(1));
6900 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), dl, X86::EDX, MVT::i32,
Duncan Sands1607f052008-12-01 11:39:25 +00006901 eax.getValue(2));
6902 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
6903 SDValue Ops[] = { eax, edx };
Dale Johannesene4d209d2009-02-03 20:21:25 +00006904 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, Ops, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006905 Results.push_back(edx.getValue(1));
6906 return;
6907 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006908 case ISD::ATOMIC_CMP_SWAP: {
Duncan Sands1607f052008-12-01 11:39:25 +00006909 MVT T = N->getValueType(0);
6910 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
6911 SDValue cpInL, cpInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006912 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006913 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006914 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(2),
Duncan Sands1607f052008-12-01 11:39:25 +00006915 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006916 cpInL = DAG.getCopyToReg(N->getOperand(0), dl, X86::EAX, cpInL, SDValue());
6917 cpInH = DAG.getCopyToReg(cpInL.getValue(0), dl, X86::EDX, cpInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006918 cpInL.getValue(1));
6919 SDValue swapInL, swapInH;
Dale Johannesene4d209d2009-02-03 20:21:25 +00006920 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006921 DAG.getConstant(0, MVT::i32));
Dale Johannesene4d209d2009-02-03 20:21:25 +00006922 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, dl, MVT::i32, N->getOperand(3),
Duncan Sands1607f052008-12-01 11:39:25 +00006923 DAG.getConstant(1, MVT::i32));
Dale Johannesendd64c412009-02-04 00:33:20 +00006924 swapInL = DAG.getCopyToReg(cpInH.getValue(0), dl, X86::EBX, swapInL,
Duncan Sands1607f052008-12-01 11:39:25 +00006925 cpInH.getValue(1));
Dale Johannesendd64c412009-02-04 00:33:20 +00006926 swapInH = DAG.getCopyToReg(swapInL.getValue(0), dl, X86::ECX, swapInH,
Duncan Sands1607f052008-12-01 11:39:25 +00006927 swapInL.getValue(1));
6928 SDValue Ops[] = { swapInH.getValue(0),
6929 N->getOperand(1),
6930 swapInH.getValue(1) };
6931 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dale Johannesene4d209d2009-02-03 20:21:25 +00006932 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, dl, Tys, Ops, 3);
Dale Johannesendd64c412009-02-04 00:33:20 +00006933 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), dl, X86::EAX,
6934 MVT::i32, Result.getValue(1));
6935 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), dl, X86::EDX,
6936 MVT::i32, cpOutL.getValue(2));
Duncan Sands1607f052008-12-01 11:39:25 +00006937 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
Dale Johannesene4d209d2009-02-03 20:21:25 +00006938 Results.push_back(DAG.getNode(ISD::BUILD_PAIR, dl, MVT::i64, OpsF, 2));
Duncan Sands1607f052008-12-01 11:39:25 +00006939 Results.push_back(cpOutH.getValue(1));
6940 return;
6941 }
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006942 case ISD::ATOMIC_LOAD_ADD:
Duncan Sands1607f052008-12-01 11:39:25 +00006943 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMADD64_DAG);
6944 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006945 case ISD::ATOMIC_LOAD_AND:
Duncan Sands1607f052008-12-01 11:39:25 +00006946 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMAND64_DAG);
6947 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006948 case ISD::ATOMIC_LOAD_NAND:
Duncan Sands1607f052008-12-01 11:39:25 +00006949 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMNAND64_DAG);
6950 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006951 case ISD::ATOMIC_LOAD_OR:
Duncan Sands1607f052008-12-01 11:39:25 +00006952 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMOR64_DAG);
6953 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006954 case ISD::ATOMIC_LOAD_SUB:
Duncan Sands1607f052008-12-01 11:39:25 +00006955 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSUB64_DAG);
6956 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006957 case ISD::ATOMIC_LOAD_XOR:
Duncan Sands1607f052008-12-01 11:39:25 +00006958 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMXOR64_DAG);
6959 return;
Dan Gohman0b1d4a72008-12-23 21:37:04 +00006960 case ISD::ATOMIC_SWAP:
Duncan Sands1607f052008-12-01 11:39:25 +00006961 ReplaceATOMIC_BINARY_64(N, Results, DAG, X86ISD::ATOMSWAP64_DAG);
6962 return;
Chris Lattner27a6c732007-11-24 07:07:01 +00006963 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006964}
6965
Evan Cheng72261582005-12-20 06:22:03 +00006966const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6967 switch (Opcode) {
6968 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006969 case X86ISD::BSF: return "X86ISD::BSF";
6970 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006971 case X86ISD::SHLD: return "X86ISD::SHLD";
6972 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006973 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006974 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006975 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006976 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006977 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006978 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006979 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6980 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6981 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006982 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006983 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006984 case X86ISD::CALL: return "X86ISD::CALL";
Evan Cheng72261582005-12-20 06:22:03 +00006985 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
Dan Gohmanc7a37d42008-12-23 22:45:23 +00006986 case X86ISD::BT: return "X86ISD::BT";
Evan Cheng72261582005-12-20 06:22:03 +00006987 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006988 case X86ISD::COMI: return "X86ISD::COMI";
6989 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006990 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006991 case X86ISD::CMOV: return "X86ISD::CMOV";
6992 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006993 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006994 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6995 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006996 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006997 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Chris Lattner18c59872009-06-27 04:16:01 +00006998 case X86ISD::WrapperRIP: return "X86ISD::WrapperRIP";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006999 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00007000 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00007001 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
7002 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00007003 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Nate Begemanb9a47b82009-02-23 08:49:38 +00007004 case X86ISD::PSHUFB: return "X86ISD::PSHUFB";
Evan Cheng8ca29322006-11-10 21:43:37 +00007005 case X86ISD::FMAX: return "X86ISD::FMAX";
7006 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00007007 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
7008 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00007009 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
Rafael Espindola094fad32009-04-08 21:14:34 +00007010 case X86ISD::SegmentBaseAddress: return "X86ISD::SegmentBaseAddress";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00007011 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00007012 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00007013 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00007014 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
7015 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007016 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
7017 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
7018 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
7019 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
7020 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
7021 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00007022 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
7023 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00007024 case X86ISD::VSHL: return "X86ISD::VSHL";
7025 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00007026 case X86ISD::CMPPD: return "X86ISD::CMPPD";
7027 case X86ISD::CMPPS: return "X86ISD::CMPPS";
7028 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
7029 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
7030 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
7031 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
7032 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
7033 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
7034 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
7035 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Bill Wendlingab55ebd2008-12-12 00:56:36 +00007036 case X86ISD::ADD: return "X86ISD::ADD";
7037 case X86ISD::SUB: return "X86ISD::SUB";
Bill Wendlingd350e022008-12-12 21:15:41 +00007038 case X86ISD::SMUL: return "X86ISD::SMUL";
7039 case X86ISD::UMUL: return "X86ISD::UMUL";
Dan Gohman076aee32009-03-04 19:44:21 +00007040 case X86ISD::INC: return "X86ISD::INC";
7041 case X86ISD::DEC: return "X86ISD::DEC";
Evan Cheng73f24c92009-03-30 21:36:47 +00007042 case X86ISD::MUL_IMM: return "X86ISD::MUL_IMM";
Eric Christopher71c67532009-07-29 00:28:05 +00007043 case X86ISD::PTEST: return "X86ISD::PTEST";
Evan Cheng72261582005-12-20 06:22:03 +00007044 }
7045}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007046
Chris Lattnerc9addb72007-03-30 23:15:24 +00007047// isLegalAddressingMode - Return true if the addressing mode represented
7048// by AM is legal for this target, for a load/store of the specified type.
Scott Michelfdc40a02009-02-17 22:15:04 +00007049bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
Chris Lattnerc9addb72007-03-30 23:15:24 +00007050 const Type *Ty) const {
7051 // X86 supports extremely general addressing modes.
Scott Michelfdc40a02009-02-17 22:15:04 +00007052
Chris Lattnerc9addb72007-03-30 23:15:24 +00007053 // X86 allows a sign-extended 32-bit immediate field as a displacement.
7054 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
7055 return false;
Scott Michelfdc40a02009-02-17 22:15:04 +00007056
Chris Lattnerc9addb72007-03-30 23:15:24 +00007057 if (AM.BaseGV) {
Chris Lattnerdfed4132009-07-10 07:38:24 +00007058 unsigned GVFlags =
7059 Subtarget->ClassifyGlobalReference(AM.BaseGV, getTargetMachine());
7060
7061 // If a reference to this global requires an extra load, we can't fold it.
7062 if (isGlobalStubReference(GVFlags))
Chris Lattnerc9addb72007-03-30 23:15:24 +00007063 return false;
Chris Lattnerdfed4132009-07-10 07:38:24 +00007064
7065 // If BaseGV requires a register for the PIC base, we cannot also have a
7066 // BaseReg specified.
7067 if (AM.HasBaseReg && isGlobalRelativeToPICBase(GVFlags))
Dale Johannesen203af582008-12-05 21:47:27 +00007068 return false;
Evan Cheng52787842007-08-01 23:46:47 +00007069
7070 // X86-64 only supports addr of globals in small code model.
7071 if (Subtarget->is64Bit()) {
7072 if (getTargetMachine().getCodeModel() != CodeModel::Small)
7073 return false;
7074 // If lower 4G is not available, then we must use rip-relative addressing.
7075 if (AM.BaseOffs || AM.Scale > 1)
7076 return false;
7077 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00007078 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007079
Chris Lattnerc9addb72007-03-30 23:15:24 +00007080 switch (AM.Scale) {
7081 case 0:
7082 case 1:
7083 case 2:
7084 case 4:
7085 case 8:
7086 // These scales always work.
7087 break;
7088 case 3:
7089 case 5:
7090 case 9:
7091 // These scales are formed with basereg+scalereg. Only accept if there is
7092 // no basereg yet.
7093 if (AM.HasBaseReg)
7094 return false;
7095 break;
7096 default: // Other stuff never works.
7097 return false;
7098 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007099
Chris Lattnerc9addb72007-03-30 23:15:24 +00007100 return true;
7101}
7102
7103
Evan Cheng2bd122c2007-10-26 01:56:11 +00007104bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
7105 if (!Ty1->isInteger() || !Ty2->isInteger())
7106 return false;
Evan Chenge127a732007-10-29 07:57:50 +00007107 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
7108 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007109 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00007110 return false;
7111 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00007112}
7113
Duncan Sands83ec4b62008-06-06 12:08:01 +00007114bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
7115 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007116 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007117 unsigned NumBits1 = VT1.getSizeInBits();
7118 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00007119 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00007120 return false;
7121 return Subtarget->is64Bit() || NumBits1 < 64;
7122}
Evan Cheng2bd122c2007-10-26 01:56:11 +00007123
Dan Gohman97121ba2009-04-08 00:15:30 +00007124bool X86TargetLowering::isZExtFree(const Type *Ty1, const Type *Ty2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007125 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00007126 return Ty1 == Type::Int32Ty && Ty2 == Type::Int64Ty && Subtarget->is64Bit();
7127}
7128
7129bool X86TargetLowering::isZExtFree(MVT VT1, MVT VT2) const {
Dan Gohman349ba492009-04-09 02:06:09 +00007130 // x86-64 implicitly zero-extends 32-bit results in 64-bit registers.
Dan Gohman97121ba2009-04-08 00:15:30 +00007131 return VT1 == MVT::i32 && VT2 == MVT::i64 && Subtarget->is64Bit();
7132}
7133
Evan Cheng8b944d32009-05-28 00:35:15 +00007134bool X86TargetLowering::isNarrowingProfitable(MVT VT1, MVT VT2) const {
7135 // i16 instructions are longer (0x66 prefix) and potentially slower.
7136 return !(VT1 == MVT::i32 && VT2 == MVT::i16);
7137}
7138
Evan Cheng60c07e12006-07-05 22:17:51 +00007139/// isShuffleMaskLegal - Targets can use this to indicate that they only
7140/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
7141/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
7142/// are assumed to be legal.
7143bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007144X86TargetLowering::isShuffleMaskLegal(const SmallVectorImpl<int> &M,
7145 MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00007146 // Only do shuffles on 128-bit vector types for now.
Nate Begeman9008ca62009-04-27 18:41:29 +00007147 if (VT.getSizeInBits() == 64)
7148 return false;
7149
7150 // FIXME: pshufb, blends, palignr, shifts.
7151 return (VT.getVectorNumElements() == 2 ||
7152 ShuffleVectorSDNode::isSplatMask(&M[0], VT) ||
7153 isMOVLMask(M, VT) ||
7154 isSHUFPMask(M, VT) ||
7155 isPSHUFDMask(M, VT) ||
7156 isPSHUFHWMask(M, VT) ||
7157 isPSHUFLWMask(M, VT) ||
7158 isUNPCKLMask(M, VT) ||
7159 isUNPCKHMask(M, VT) ||
7160 isUNPCKL_v_undef_Mask(M, VT) ||
7161 isUNPCKH_v_undef_Mask(M, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007162}
7163
Dan Gohman7d8143f2008-04-09 20:09:42 +00007164bool
Nate Begeman5a5ca152009-04-29 05:20:52 +00007165X86TargetLowering::isVectorClearMaskLegal(const SmallVectorImpl<int> &Mask,
Nate Begeman9008ca62009-04-27 18:41:29 +00007166 MVT VT) const {
7167 unsigned NumElts = VT.getVectorNumElements();
7168 // FIXME: This collection of masks seems suspect.
7169 if (NumElts == 2)
7170 return true;
7171 if (NumElts == 4 && VT.getSizeInBits() == 128) {
7172 return (isMOVLMask(Mask, VT) ||
7173 isCommutedMOVLMask(Mask, VT, true) ||
7174 isSHUFPMask(Mask, VT) ||
7175 isCommutedSHUFPMask(Mask, VT));
Evan Cheng60c07e12006-07-05 22:17:51 +00007176 }
7177 return false;
7178}
7179
7180//===----------------------------------------------------------------------===//
7181// X86 Scheduler Hooks
7182//===----------------------------------------------------------------------===//
7183
Mon P Wang63307c32008-05-05 19:05:59 +00007184// private utility function
7185MachineBasicBlock *
7186X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
7187 MachineBasicBlock *MBB,
7188 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007189 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007190 unsigned LoadOpc,
7191 unsigned CXchgOpc,
7192 unsigned copyOpc,
7193 unsigned notOpc,
7194 unsigned EAXreg,
7195 TargetRegisterClass *RC,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007196 bool invSrc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007197 // For the atomic bitwise operator, we generate
7198 // thisMBB:
7199 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007200 // ld t1 = [bitinstr.addr]
7201 // op t2 = t1, [bitinstr.val]
7202 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007203 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7204 // bz newMBB
7205 // fallthrough -->nextMBB
7206 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7207 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007208 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007209 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007210
Mon P Wang63307c32008-05-05 19:05:59 +00007211 /// First build the CFG
7212 MachineFunction *F = MBB->getParent();
7213 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007214 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7215 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7216 F->insert(MBBIter, newMBB);
7217 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007218
Mon P Wang63307c32008-05-05 19:05:59 +00007219 // Move all successors to thisMBB to nextMBB
7220 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007221
Mon P Wang63307c32008-05-05 19:05:59 +00007222 // Update thisMBB to fall through to newMBB
7223 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007224
Mon P Wang63307c32008-05-05 19:05:59 +00007225 // newMBB jumps to itself and fall through to nextMBB
7226 newMBB->addSuccessor(nextMBB);
7227 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007228
Mon P Wang63307c32008-05-05 19:05:59 +00007229 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007230 assert(bInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007231 "unexpected number of operands");
Dale Johannesene4d209d2009-02-03 20:21:25 +00007232 DebugLoc dl = bInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007233 MachineOperand& destOper = bInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007234 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007235 int numArgs = bInstr->getNumOperands() - 1;
7236 for (int i=0; i < numArgs; ++i)
7237 argOpers[i] = &bInstr->getOperand(i+1);
7238
7239 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007240 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7241 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007242
Dale Johannesen140be2d2008-08-19 18:47:28 +00007243 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007244 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007245 for (int i=0; i <= lastAddrIndx; ++i)
7246 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007247
Dale Johannesen140be2d2008-08-19 18:47:28 +00007248 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007249 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007250 MIB = BuildMI(newMBB, dl, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007251 }
Scott Michelfdc40a02009-02-17 22:15:04 +00007252 else
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007253 tt = t1;
7254
Dale Johannesen140be2d2008-08-19 18:47:28 +00007255 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00007256 assert((argOpers[valArgIndx]->isReg() ||
7257 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007258 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00007259 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007260 MIB = BuildMI(newMBB, dl, TII->get(regOpc), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007261 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007262 MIB = BuildMI(newMBB, dl, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007263 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00007264 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007265
Dale Johannesene4d209d2009-02-03 20:21:25 +00007266 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00007267 MIB.addReg(t1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007268
Dale Johannesene4d209d2009-02-03 20:21:25 +00007269 MIB = BuildMI(newMBB, dl, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00007270 for (int i=0; i <= lastAddrIndx; ++i)
7271 (*MIB).addOperand(*argOpers[i]);
7272 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00007273 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7274 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7275
Dale Johannesene4d209d2009-02-03 20:21:25 +00007276 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), destOper.getReg());
Dale Johannesen140be2d2008-08-19 18:47:28 +00007277 MIB.addReg(EAXreg);
Scott Michelfdc40a02009-02-17 22:15:04 +00007278
Mon P Wang63307c32008-05-05 19:05:59 +00007279 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007280 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007281
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007282 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007283 return nextMBB;
7284}
7285
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00007286// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00007287MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007288X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
7289 MachineBasicBlock *MBB,
7290 unsigned regOpcL,
7291 unsigned regOpcH,
7292 unsigned immOpcL,
7293 unsigned immOpcH,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007294 bool invSrc) const {
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007295 // For the atomic bitwise operator, we generate
7296 // thisMBB (instructions are in pairs, except cmpxchg8b)
7297 // ld t1,t2 = [bitinstr.addr]
7298 // newMBB:
7299 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
7300 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00007301 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007302 // mov ECX, EBX <- t5, t6
7303 // mov EAX, EDX <- t1, t2
7304 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
7305 // mov t3, t4 <- EAX, EDX
7306 // bz newMBB
7307 // result in out1, out2
7308 // fallthrough -->nextMBB
7309
7310 const TargetRegisterClass *RC = X86::GR32RegisterClass;
7311 const unsigned LoadOpc = X86::MOV32rm;
7312 const unsigned copyOpc = X86::MOV32rr;
7313 const unsigned NotOpc = X86::NOT32r;
7314 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7315 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
7316 MachineFunction::iterator MBBIter = MBB;
7317 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007318
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007319 /// First build the CFG
7320 MachineFunction *F = MBB->getParent();
7321 MachineBasicBlock *thisMBB = MBB;
7322 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7323 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7324 F->insert(MBBIter, newMBB);
7325 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007326
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007327 // Move all successors to thisMBB to nextMBB
7328 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007329
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007330 // Update thisMBB to fall through to newMBB
7331 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007332
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007333 // newMBB jumps to itself and fall through to nextMBB
7334 newMBB->addSuccessor(nextMBB);
7335 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007336
Dale Johannesene4d209d2009-02-03 20:21:25 +00007337 DebugLoc dl = bInstr->getDebugLoc();
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007338 // Insert instructions into newMBB based on incoming instruction
7339 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007340 assert(bInstr->getNumOperands() < X86AddrNumOperands + 14 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007341 "unexpected number of operands");
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007342 MachineOperand& dest1Oper = bInstr->getOperand(0);
7343 MachineOperand& dest2Oper = bInstr->getOperand(1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007344 MachineOperand* argOpers[2 + X86AddrNumOperands];
7345 for (int i=0; i < 2 + X86AddrNumOperands; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007346 argOpers[i] = &bInstr->getOperand(i+2);
7347
7348 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007349 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
Scott Michelfdc40a02009-02-17 22:15:04 +00007350
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007351 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007352 MachineInstrBuilder MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007353 for (int i=0; i <= lastAddrIndx; ++i)
7354 (*MIB).addOperand(*argOpers[i]);
7355 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007356 MIB = BuildMI(thisMBB, dl, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00007357 // add 4 to displacement.
Rafael Espindola094fad32009-04-08 21:14:34 +00007358 for (int i=0; i <= lastAddrIndx-2; ++i)
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007359 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00007360 MachineOperand newOp3 = *(argOpers[3]);
7361 if (newOp3.isImm())
7362 newOp3.setImm(newOp3.getImm()+4);
7363 else
7364 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007365 (*MIB).addOperand(newOp3);
Rafael Espindola094fad32009-04-08 21:14:34 +00007366 (*MIB).addOperand(*argOpers[lastAddrIndx]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007367
7368 // t3/4 are defined later, at the bottom of the loop
7369 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
7370 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007371 BuildMI(newMBB, dl, TII->get(X86::PHI), dest1Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007372 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007373 BuildMI(newMBB, dl, TII->get(X86::PHI), dest2Oper.getReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007374 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
7375
7376 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
7377 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
Scott Michelfdc40a02009-02-17 22:15:04 +00007378 if (invSrc) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007379 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt1).addReg(t1);
7380 MIB = BuildMI(newMBB, dl, TII->get(NotOpc), tt2).addReg(t2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007381 } else {
7382 tt1 = t1;
7383 tt2 = t2;
7384 }
7385
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007386 int valArgIndx = lastAddrIndx + 1;
7387 assert((argOpers[valArgIndx]->isReg() ||
Bill Wendling51b16f42009-05-30 01:09:53 +00007388 argOpers[valArgIndx]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007389 "invalid operand");
7390 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
7391 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007392 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007393 MIB = BuildMI(newMBB, dl, TII->get(regOpcL), t5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007394 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007395 MIB = BuildMI(newMBB, dl, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00007396 if (regOpcL != X86::MOV32rr)
7397 MIB.addReg(tt1);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007398 (*MIB).addOperand(*argOpers[valArgIndx]);
7399 assert(argOpers[valArgIndx + 1]->isReg() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007400 argOpers[valArgIndx]->isReg());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007401 assert(argOpers[valArgIndx + 1]->isImm() ==
Bill Wendling51b16f42009-05-30 01:09:53 +00007402 argOpers[valArgIndx]->isImm());
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007403 if (argOpers[valArgIndx + 1]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007404 MIB = BuildMI(newMBB, dl, TII->get(regOpcH), t6);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007405 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007406 MIB = BuildMI(newMBB, dl, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00007407 if (regOpcH != X86::MOV32rr)
7408 MIB.addReg(tt2);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007409 (*MIB).addOperand(*argOpers[valArgIndx + 1]);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007410
Dale Johannesene4d209d2009-02-03 20:21:25 +00007411 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EAX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007412 MIB.addReg(t1);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007413 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EDX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007414 MIB.addReg(t2);
7415
Dale Johannesene4d209d2009-02-03 20:21:25 +00007416 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::EBX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007417 MIB.addReg(t5);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007418 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), X86::ECX);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007419 MIB.addReg(t6);
Scott Michelfdc40a02009-02-17 22:15:04 +00007420
Dale Johannesene4d209d2009-02-03 20:21:25 +00007421 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG8B));
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007422 for (int i=0; i <= lastAddrIndx; ++i)
7423 (*MIB).addOperand(*argOpers[i]);
7424
7425 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7426 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
7427
Dale Johannesene4d209d2009-02-03 20:21:25 +00007428 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t3);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007429 MIB.addReg(X86::EAX);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007430 MIB = BuildMI(newMBB, dl, TII->get(copyOpc), t4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007431 MIB.addReg(X86::EDX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007432
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007433 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007434 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007435
7436 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
7437 return nextMBB;
7438}
7439
7440// private utility function
7441MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00007442X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
7443 MachineBasicBlock *MBB,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007444 unsigned cmovOpc) const {
Mon P Wang63307c32008-05-05 19:05:59 +00007445 // For the atomic min/max operator, we generate
7446 // thisMBB:
7447 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00007448 // ld t1 = [min/max.addr]
Scott Michelfdc40a02009-02-17 22:15:04 +00007449 // mov t2 = [min/max.val]
Mon P Wang63307c32008-05-05 19:05:59 +00007450 // cmp t1, t2
7451 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00007452 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00007453 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
7454 // bz newMBB
7455 // fallthrough -->nextMBB
7456 //
7457 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
7458 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007459 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00007460 ++MBBIter;
Scott Michelfdc40a02009-02-17 22:15:04 +00007461
Mon P Wang63307c32008-05-05 19:05:59 +00007462 /// First build the CFG
7463 MachineFunction *F = MBB->getParent();
7464 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007465 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
7466 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
7467 F->insert(MBBIter, newMBB);
7468 F->insert(MBBIter, nextMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007469
Mon P Wang63307c32008-05-05 19:05:59 +00007470 // Move all successors to thisMBB to nextMBB
7471 nextMBB->transferSuccessors(thisMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007472
Mon P Wang63307c32008-05-05 19:05:59 +00007473 // Update thisMBB to fall through to newMBB
7474 thisMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007475
Mon P Wang63307c32008-05-05 19:05:59 +00007476 // newMBB jumps to newMBB and fall through to nextMBB
7477 newMBB->addSuccessor(nextMBB);
7478 newMBB->addSuccessor(newMBB);
Scott Michelfdc40a02009-02-17 22:15:04 +00007479
Dale Johannesene4d209d2009-02-03 20:21:25 +00007480 DebugLoc dl = mInstr->getDebugLoc();
Mon P Wang63307c32008-05-05 19:05:59 +00007481 // Insert instructions into newMBB based on incoming instruction
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007482 assert(mInstr->getNumOperands() < X86AddrNumOperands + 4 &&
Bill Wendling51b16f42009-05-30 01:09:53 +00007483 "unexpected number of operands");
Mon P Wang63307c32008-05-05 19:05:59 +00007484 MachineOperand& destOper = mInstr->getOperand(0);
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007485 MachineOperand* argOpers[2 + X86AddrNumOperands];
Mon P Wang63307c32008-05-05 19:05:59 +00007486 int numArgs = mInstr->getNumOperands() - 1;
7487 for (int i=0; i < numArgs; ++i)
7488 argOpers[i] = &mInstr->getOperand(i+1);
Scott Michelfdc40a02009-02-17 22:15:04 +00007489
Mon P Wang63307c32008-05-05 19:05:59 +00007490 // x86 address has 4 operands: base, index, scale, and displacement
Rafael Espindolaa82dfca2009-03-27 15:26:30 +00007491 int lastAddrIndx = X86AddrNumOperands - 1; // [0,3]
7492 int valArgIndx = lastAddrIndx + 1;
Scott Michelfdc40a02009-02-17 22:15:04 +00007493
Mon P Wangab3e7472008-05-05 22:56:23 +00007494 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007495 MachineInstrBuilder MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00007496 for (int i=0; i <= lastAddrIndx; ++i)
7497 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00007498
Mon P Wang63307c32008-05-05 19:05:59 +00007499 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00007500 assert((argOpers[valArgIndx]->isReg() ||
7501 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00007502 "invalid operand");
Scott Michelfdc40a02009-02-17 22:15:04 +00007503
7504 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00007505 if (argOpers[valArgIndx]->isReg())
Dale Johannesene4d209d2009-02-03 20:21:25 +00007506 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Scott Michelfdc40a02009-02-17 22:15:04 +00007507 else
Dale Johannesene4d209d2009-02-03 20:21:25 +00007508 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), t2);
Mon P Wang63307c32008-05-05 19:05:59 +00007509 (*MIB).addOperand(*argOpers[valArgIndx]);
7510
Dale Johannesene4d209d2009-02-03 20:21:25 +00007511 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), X86::EAX);
Mon P Wangab3e7472008-05-05 22:56:23 +00007512 MIB.addReg(t1);
7513
Dale Johannesene4d209d2009-02-03 20:21:25 +00007514 MIB = BuildMI(newMBB, dl, TII->get(X86::CMP32rr));
Mon P Wang63307c32008-05-05 19:05:59 +00007515 MIB.addReg(t1);
7516 MIB.addReg(t2);
7517
7518 // Generate movc
7519 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007520 MIB = BuildMI(newMBB, dl, TII->get(cmovOpc),t3);
Mon P Wang63307c32008-05-05 19:05:59 +00007521 MIB.addReg(t2);
7522 MIB.addReg(t1);
7523
7524 // Cmp and exchange if none has modified the memory location
Dale Johannesene4d209d2009-02-03 20:21:25 +00007525 MIB = BuildMI(newMBB, dl, TII->get(X86::LCMPXCHG32));
Mon P Wang63307c32008-05-05 19:05:59 +00007526 for (int i=0; i <= lastAddrIndx; ++i)
7527 (*MIB).addOperand(*argOpers[i]);
7528 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00007529 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
7530 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Scott Michelfdc40a02009-02-17 22:15:04 +00007531
Dale Johannesene4d209d2009-02-03 20:21:25 +00007532 MIB = BuildMI(newMBB, dl, TII->get(X86::MOV32rr), destOper.getReg());
Mon P Wang63307c32008-05-05 19:05:59 +00007533 MIB.addReg(X86::EAX);
Scott Michelfdc40a02009-02-17 22:15:04 +00007534
Mon P Wang63307c32008-05-05 19:05:59 +00007535 // insert branch
Dale Johannesene4d209d2009-02-03 20:21:25 +00007536 BuildMI(newMBB, dl, TII->get(X86::JNE)).addMBB(newMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007537
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007538 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00007539 return nextMBB;
7540}
7541
7542
Evan Cheng60c07e12006-07-05 22:17:51 +00007543MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00007544X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
Dan Gohman1fdbc1d2009-02-07 16:15:20 +00007545 MachineBasicBlock *BB) const {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007546 DebugLoc dl = MI->getDebugLoc();
Evan Chengc0f64ff2006-11-27 23:37:22 +00007547 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00007548 switch (MI->getOpcode()) {
7549 default: assert(false && "Unexpected instr type to insert");
Mon P Wang9e5ecb82008-12-12 01:25:51 +00007550 case X86::CMOV_V1I64:
Evan Cheng60c07e12006-07-05 22:17:51 +00007551 case X86::CMOV_FR32:
7552 case X86::CMOV_FR64:
7553 case X86::CMOV_V4F32:
7554 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00007555 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007556 // To "insert" a SELECT_CC instruction, we actually have to insert the
7557 // diamond control-flow pattern. The incoming instruction knows the
7558 // destination vreg to set, the condition code register to branch on, the
7559 // true/false values to select between, and a branch opcode to use.
7560 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007561 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00007562 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007563
Evan Cheng60c07e12006-07-05 22:17:51 +00007564 // thisMBB:
7565 // ...
7566 // TrueVal = ...
7567 // cmpTY ccX, r1, r2
7568 // bCC copy1MBB
7569 // fallthrough --> copy0MBB
7570 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007571 MachineFunction *F = BB->getParent();
7572 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
7573 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007574 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00007575 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007576 BuildMI(BB, dl, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007577 F->insert(It, copy0MBB);
7578 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00007579 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00007580 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00007581 sinkMBB->transferSuccessors(BB);
7582
7583 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00007584 BB->addSuccessor(copy0MBB);
7585 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007586
Evan Cheng60c07e12006-07-05 22:17:51 +00007587 // copy0MBB:
7588 // %FalseValue = ...
7589 // # fallthrough to sinkMBB
7590 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007591
Evan Cheng60c07e12006-07-05 22:17:51 +00007592 // Update machine-CFG edges
7593 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007594
Evan Cheng60c07e12006-07-05 22:17:51 +00007595 // sinkMBB:
7596 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
7597 // ...
7598 BB = sinkMBB;
Dale Johannesene4d209d2009-02-03 20:21:25 +00007599 BuildMI(BB, dl, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00007600 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
7601 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
7602
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007603 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007604 return BB;
7605 }
7606
Dale Johannesen849f2142007-07-03 00:53:03 +00007607 case X86::FP32_TO_INT16_IN_MEM:
7608 case X86::FP32_TO_INT32_IN_MEM:
7609 case X86::FP32_TO_INT64_IN_MEM:
7610 case X86::FP64_TO_INT16_IN_MEM:
7611 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00007612 case X86::FP64_TO_INT64_IN_MEM:
7613 case X86::FP80_TO_INT16_IN_MEM:
7614 case X86::FP80_TO_INT32_IN_MEM:
7615 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00007616 // Change the floating point control register to use "round towards zero"
7617 // mode when truncating to an integer value.
7618 MachineFunction *F = BB->getParent();
7619 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Dale Johannesene4d209d2009-02-03 20:21:25 +00007620 addFrameReference(BuildMI(BB, dl, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007621
7622 // Load the old value of the high byte of the control word...
7623 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00007624 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Scott Michelfdc40a02009-02-17 22:15:04 +00007625 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16rm), OldCW),
Dale Johannesene4d209d2009-02-03 20:21:25 +00007626 CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007627
7628 // Set the high part to be round to zero...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007629 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mi)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007630 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00007631
7632 // Reload the modified control word now...
Dale Johannesene4d209d2009-02-03 20:21:25 +00007633 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007634
7635 // Restore the memory image of control word to original value
Dale Johannesene4d209d2009-02-03 20:21:25 +00007636 addFrameReference(BuildMI(BB, dl, TII->get(X86::MOV16mr)), CWFrameIdx)
Evan Chengc0f64ff2006-11-27 23:37:22 +00007637 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00007638
7639 // Get the X86 opcode to use.
7640 unsigned Opc;
7641 switch (MI->getOpcode()) {
Torok Edwinc23197a2009-07-14 16:55:14 +00007642 default: llvm_unreachable("illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00007643 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
7644 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
7645 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
7646 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
7647 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
7648 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00007649 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
7650 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
7651 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00007652 }
7653
7654 X86AddressMode AM;
7655 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00007656 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007657 AM.BaseType = X86AddressMode::RegBase;
7658 AM.Base.Reg = Op.getReg();
7659 } else {
7660 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00007661 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00007662 }
7663 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00007664 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007665 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007666 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00007667 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00007668 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007669 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00007670 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00007671 AM.GV = Op.getGlobal();
7672 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00007673 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00007674 }
Dale Johannesene4d209d2009-02-03 20:21:25 +00007675 addFullAddress(BuildMI(BB, dl, TII->get(Opc)), AM)
Rafael Espindola8ef2b892009-04-08 08:09:33 +00007676 .addReg(MI->getOperand(X86AddrNumOperands).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00007677
7678 // Reload the original control word now.
Dale Johannesene4d209d2009-02-03 20:21:25 +00007679 addFrameReference(BuildMI(BB, dl, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007680
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007681 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007682 return BB;
7683 }
Mon P Wang63307c32008-05-05 19:05:59 +00007684 case X86::ATOMAND32:
7685 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007686 X86::AND32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007687 X86::LCMPXCHG32, X86::MOV32rr,
7688 X86::NOT32r, X86::EAX,
7689 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007690 case X86::ATOMOR32:
Scott Michelfdc40a02009-02-17 22:15:04 +00007691 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
7692 X86::OR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007693 X86::LCMPXCHG32, X86::MOV32rr,
7694 X86::NOT32r, X86::EAX,
7695 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007696 case X86::ATOMXOR32:
7697 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007698 X86::XOR32ri, X86::MOV32rm,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007699 X86::LCMPXCHG32, X86::MOV32rr,
7700 X86::NOT32r, X86::EAX,
7701 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007702 case X86::ATOMNAND32:
7703 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007704 X86::AND32ri, X86::MOV32rm,
7705 X86::LCMPXCHG32, X86::MOV32rr,
7706 X86::NOT32r, X86::EAX,
7707 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007708 case X86::ATOMMIN32:
7709 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7710 case X86::ATOMMAX32:
7711 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7712 case X86::ATOMUMIN32:
7713 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7714 case X86::ATOMUMAX32:
7715 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007716
7717 case X86::ATOMAND16:
7718 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7719 X86::AND16ri, X86::MOV16rm,
7720 X86::LCMPXCHG16, X86::MOV16rr,
7721 X86::NOT16r, X86::AX,
7722 X86::GR16RegisterClass);
7723 case X86::ATOMOR16:
Scott Michelfdc40a02009-02-17 22:15:04 +00007724 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007725 X86::OR16ri, X86::MOV16rm,
7726 X86::LCMPXCHG16, X86::MOV16rr,
7727 X86::NOT16r, X86::AX,
7728 X86::GR16RegisterClass);
7729 case X86::ATOMXOR16:
7730 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7731 X86::XOR16ri, X86::MOV16rm,
7732 X86::LCMPXCHG16, X86::MOV16rr,
7733 X86::NOT16r, X86::AX,
7734 X86::GR16RegisterClass);
7735 case X86::ATOMNAND16:
7736 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7737 X86::AND16ri, X86::MOV16rm,
7738 X86::LCMPXCHG16, X86::MOV16rr,
7739 X86::NOT16r, X86::AX,
7740 X86::GR16RegisterClass, true);
7741 case X86::ATOMMIN16:
7742 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7743 case X86::ATOMMAX16:
7744 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7745 case X86::ATOMUMIN16:
7746 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7747 case X86::ATOMUMAX16:
7748 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7749
7750 case X86::ATOMAND8:
7751 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7752 X86::AND8ri, X86::MOV8rm,
7753 X86::LCMPXCHG8, X86::MOV8rr,
7754 X86::NOT8r, X86::AL,
7755 X86::GR8RegisterClass);
7756 case X86::ATOMOR8:
Scott Michelfdc40a02009-02-17 22:15:04 +00007757 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007758 X86::OR8ri, X86::MOV8rm,
7759 X86::LCMPXCHG8, X86::MOV8rr,
7760 X86::NOT8r, X86::AL,
7761 X86::GR8RegisterClass);
7762 case X86::ATOMXOR8:
7763 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7764 X86::XOR8ri, X86::MOV8rm,
7765 X86::LCMPXCHG8, X86::MOV8rr,
7766 X86::NOT8r, X86::AL,
7767 X86::GR8RegisterClass);
7768 case X86::ATOMNAND8:
7769 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7770 X86::AND8ri, X86::MOV8rm,
7771 X86::LCMPXCHG8, X86::MOV8rr,
7772 X86::NOT8r, X86::AL,
7773 X86::GR8RegisterClass, true);
7774 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007775 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007776 case X86::ATOMAND64:
7777 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007778 X86::AND64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007779 X86::LCMPXCHG64, X86::MOV64rr,
7780 X86::NOT64r, X86::RAX,
7781 X86::GR64RegisterClass);
7782 case X86::ATOMOR64:
Scott Michelfdc40a02009-02-17 22:15:04 +00007783 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7784 X86::OR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007785 X86::LCMPXCHG64, X86::MOV64rr,
7786 X86::NOT64r, X86::RAX,
7787 X86::GR64RegisterClass);
7788 case X86::ATOMXOR64:
7789 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
Scott Michelfdc40a02009-02-17 22:15:04 +00007790 X86::XOR64ri32, X86::MOV64rm,
Dale Johannesena99e3842008-08-20 00:48:50 +00007791 X86::LCMPXCHG64, X86::MOV64rr,
7792 X86::NOT64r, X86::RAX,
7793 X86::GR64RegisterClass);
7794 case X86::ATOMNAND64:
7795 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7796 X86::AND64ri32, X86::MOV64rm,
7797 X86::LCMPXCHG64, X86::MOV64rr,
7798 X86::NOT64r, X86::RAX,
7799 X86::GR64RegisterClass, true);
7800 case X86::ATOMMIN64:
7801 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7802 case X86::ATOMMAX64:
7803 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7804 case X86::ATOMUMIN64:
7805 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7806 case X86::ATOMUMAX64:
7807 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007808
7809 // This group does 64-bit operations on a 32-bit host.
7810 case X86::ATOMAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007811 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007812 X86::AND32rr, X86::AND32rr,
7813 X86::AND32ri, X86::AND32ri,
7814 false);
7815 case X86::ATOMOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007816 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007817 X86::OR32rr, X86::OR32rr,
7818 X86::OR32ri, X86::OR32ri,
7819 false);
7820 case X86::ATOMXOR6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007821 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007822 X86::XOR32rr, X86::XOR32rr,
7823 X86::XOR32ri, X86::XOR32ri,
7824 false);
7825 case X86::ATOMNAND6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007826 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007827 X86::AND32rr, X86::AND32rr,
7828 X86::AND32ri, X86::AND32ri,
7829 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007830 case X86::ATOMADD6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007831 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007832 X86::ADD32rr, X86::ADC32rr,
7833 X86::ADD32ri, X86::ADC32ri,
7834 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007835 case X86::ATOMSUB6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007836 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007837 X86::SUB32rr, X86::SBB32rr,
7838 X86::SUB32ri, X86::SBB32ri,
7839 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007840 case X86::ATOMSWAP6432:
Scott Michelfdc40a02009-02-17 22:15:04 +00007841 return EmitAtomicBit6432WithCustomInserter(MI, BB,
Dale Johannesen880ae362008-10-03 22:25:52 +00007842 X86::MOV32rr, X86::MOV32rr,
7843 X86::MOV32ri, X86::MOV32ri,
7844 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007845 }
7846}
7847
7848//===----------------------------------------------------------------------===//
7849// X86 Optimization Hooks
7850//===----------------------------------------------------------------------===//
7851
Dan Gohman475871a2008-07-27 21:46:04 +00007852void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007853 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007854 APInt &KnownZero,
7855 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007856 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007857 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007858 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007859 assert((Opc >= ISD::BUILTIN_OP_END ||
7860 Opc == ISD::INTRINSIC_WO_CHAIN ||
7861 Opc == ISD::INTRINSIC_W_CHAIN ||
7862 Opc == ISD::INTRINSIC_VOID) &&
7863 "Should use MaskedValueIsZero if you don't know whether Op"
7864 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007865
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007866 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007867 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007868 default: break;
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007869 case X86ISD::ADD:
7870 case X86ISD::SUB:
7871 case X86ISD::SMUL:
7872 case X86ISD::UMUL:
Dan Gohman076aee32009-03-04 19:44:21 +00007873 case X86ISD::INC:
7874 case X86ISD::DEC:
Evan Cheng97d0e0e2009-02-02 09:15:04 +00007875 // These nodes' second result is a boolean.
7876 if (Op.getResNo() == 0)
7877 break;
7878 // Fallthrough
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007879 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007880 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7881 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007882 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007883 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007884}
Chris Lattner259e97c2006-01-31 19:43:35 +00007885
Evan Cheng206ee9d2006-07-07 08:33:52 +00007886/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007887/// node is a GlobalAddress + offset.
7888bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7889 GlobalValue* &GA, int64_t &Offset) const{
7890 if (N->getOpcode() == X86ISD::Wrapper) {
7891 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007892 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007893 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007894 return true;
7895 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007896 }
Evan Chengad4196b2008-05-12 19:56:52 +00007897 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007898}
7899
Evan Chengad4196b2008-05-12 19:56:52 +00007900static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7901 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007902 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007903 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007904 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007905 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007906 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007907 return false;
7908}
7909
Nate Begeman9008ca62009-04-27 18:41:29 +00007910static bool EltsFromConsecutiveLoads(ShuffleVectorSDNode *N, unsigned NumElems,
Eli Friedman7a5e5552009-06-07 06:52:44 +00007911 MVT EVT, LoadSDNode *&LDBase,
7912 unsigned &LastLoadedElt,
Evan Chengad4196b2008-05-12 19:56:52 +00007913 SelectionDAG &DAG, MachineFrameInfo *MFI,
7914 const TargetLowering &TLI) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007915 LDBase = NULL;
Anton Korobeynikovb51b6cf2009-06-09 23:00:39 +00007916 LastLoadedElt = -1U;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007917 for (unsigned i = 0; i < NumElems; ++i) {
Nate Begeman9008ca62009-04-27 18:41:29 +00007918 if (N->getMaskElt(i) < 0) {
Eli Friedman7a5e5552009-06-07 06:52:44 +00007919 if (!LDBase)
Evan Cheng7e2ff772008-05-08 00:57:18 +00007920 return false;
7921 continue;
7922 }
7923
Dan Gohman475871a2008-07-27 21:46:04 +00007924 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007925 if (!Elt.getNode() ||
7926 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007927 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007928 if (!LDBase) {
7929 if (Elt.getNode()->getOpcode() == ISD::UNDEF)
Evan Cheng50d9e722008-05-10 06:46:49 +00007930 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007931 LDBase = cast<LoadSDNode>(Elt.getNode());
7932 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007933 continue;
7934 }
7935 if (Elt.getOpcode() == ISD::UNDEF)
7936 continue;
7937
Nate Begemanabc01992009-06-05 21:37:30 +00007938 LoadSDNode *LD = cast<LoadSDNode>(Elt);
Nate Begemanabc01992009-06-05 21:37:30 +00007939 if (!TLI.isConsecutiveLoad(LD, LDBase, EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007940 return false;
Eli Friedman7a5e5552009-06-07 06:52:44 +00007941 LastLoadedElt = i;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007942 }
7943 return true;
7944}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007945
7946/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7947/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7948/// if the load addresses are consecutive, non-overlapping, and in the right
Mon P Wang1e955802009-04-03 02:43:30 +00007949/// order. In the case of v2i64, it will see if it can rewrite the
7950/// shuffle to be an appropriate build vector so it can take advantage of
7951// performBuildVectorCombine.
Dan Gohman475871a2008-07-27 21:46:04 +00007952static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Nate Begeman9008ca62009-04-27 18:41:29 +00007953 const TargetLowering &TLI) {
Dale Johannesene4d209d2009-02-03 20:21:25 +00007954 DebugLoc dl = N->getDebugLoc();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007955 MVT VT = N->getValueType(0);
7956 MVT EVT = VT.getVectorElementType();
Nate Begeman9008ca62009-04-27 18:41:29 +00007957 ShuffleVectorSDNode *SVN = cast<ShuffleVectorSDNode>(N);
7958 unsigned NumElems = VT.getVectorNumElements();
Mon P Wang1e955802009-04-03 02:43:30 +00007959
Eli Friedman7a5e5552009-06-07 06:52:44 +00007960 if (VT.getSizeInBits() != 128)
7961 return SDValue();
7962
Mon P Wang1e955802009-04-03 02:43:30 +00007963 // Try to combine a vector_shuffle into a 128-bit load.
7964 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Eli Friedman7a5e5552009-06-07 06:52:44 +00007965 LoadSDNode *LD = NULL;
7966 unsigned LastLoadedElt;
7967 if (!EltsFromConsecutiveLoads(SVN, NumElems, EVT, LD, LastLoadedElt, DAG,
7968 MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007969 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007970
Eli Friedman7a5e5552009-06-07 06:52:44 +00007971 if (LastLoadedElt == NumElems - 1) {
7972 if (isBaseAlignmentOfN(16, LD->getBasePtr().getNode(), TLI))
7973 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
7974 LD->getSrcValue(), LD->getSrcValueOffset(),
7975 LD->isVolatile());
Dale Johannesene4d209d2009-02-03 20:21:25 +00007976 return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(),
Scott Michelfdc40a02009-02-17 22:15:04 +00007977 LD->getSrcValue(), LD->getSrcValueOffset(),
Eli Friedman7a5e5552009-06-07 06:52:44 +00007978 LD->isVolatile(), LD->getAlignment());
7979 } else if (NumElems == 4 && LastLoadedElt == 1) {
7980 SDVTList Tys = DAG.getVTList(MVT::v2i64, MVT::Other);
Nate Begemanabc01992009-06-05 21:37:30 +00007981 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7982 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, dl, Tys, Ops, 2);
Nate Begemanabc01992009-06-05 21:37:30 +00007983 return DAG.getNode(ISD::BIT_CONVERT, dl, VT, ResNode);
7984 }
7985 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00007986}
Evan Chengd880b972008-05-09 21:53:03 +00007987
Chris Lattner83e6c992006-10-04 06:57:07 +00007988/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007989static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner47b4ce82009-03-11 05:48:52 +00007990 const X86Subtarget *Subtarget) {
7991 DebugLoc DL = N->getDebugLoc();
Dan Gohman475871a2008-07-27 21:46:04 +00007992 SDValue Cond = N->getOperand(0);
Chris Lattner47b4ce82009-03-11 05:48:52 +00007993 // Get the LHS/RHS of the select.
7994 SDValue LHS = N->getOperand(1);
7995 SDValue RHS = N->getOperand(2);
7996
Chris Lattner83e6c992006-10-04 06:57:07 +00007997 // If we have SSE[12] support, try to form min/max nodes.
7998 if (Subtarget->hasSSE2() &&
Chris Lattner47b4ce82009-03-11 05:48:52 +00007999 (LHS.getValueType() == MVT::f32 || LHS.getValueType() == MVT::f64) &&
8000 Cond.getOpcode() == ISD::SETCC) {
8001 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008002
Chris Lattner47b4ce82009-03-11 05:48:52 +00008003 unsigned Opcode = 0;
8004 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
8005 switch (CC) {
8006 default: break;
8007 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
8008 case ISD::SETULE:
8009 case ISD::SETLE:
8010 if (!UnsafeFPMath) break;
8011 // FALL THROUGH.
8012 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
8013 case ISD::SETLT:
8014 Opcode = X86ISD::FMIN;
8015 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008016
Chris Lattner47b4ce82009-03-11 05:48:52 +00008017 case ISD::SETOGT: // (X > Y) ? X : Y -> max
8018 case ISD::SETUGT:
8019 case ISD::SETGT:
8020 if (!UnsafeFPMath) break;
8021 // FALL THROUGH.
8022 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
8023 case ISD::SETGE:
8024 Opcode = X86ISD::FMAX;
8025 break;
Chris Lattner83e6c992006-10-04 06:57:07 +00008026 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008027 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
8028 switch (CC) {
8029 default: break;
8030 case ISD::SETOGT: // (X > Y) ? Y : X -> min
8031 case ISD::SETUGT:
8032 case ISD::SETGT:
8033 if (!UnsafeFPMath) break;
8034 // FALL THROUGH.
8035 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
8036 case ISD::SETGE:
8037 Opcode = X86ISD::FMIN;
8038 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008039
Chris Lattner47b4ce82009-03-11 05:48:52 +00008040 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
8041 case ISD::SETULE:
8042 case ISD::SETLE:
8043 if (!UnsafeFPMath) break;
8044 // FALL THROUGH.
8045 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
8046 case ISD::SETLT:
8047 Opcode = X86ISD::FMAX;
8048 break;
8049 }
Chris Lattner83e6c992006-10-04 06:57:07 +00008050 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008051
Chris Lattner47b4ce82009-03-11 05:48:52 +00008052 if (Opcode)
8053 return DAG.getNode(Opcode, DL, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00008054 }
Chris Lattner47b4ce82009-03-11 05:48:52 +00008055
Chris Lattnerd1980a52009-03-12 06:52:53 +00008056 // If this is a select between two integer constants, try to do some
8057 // optimizations.
Chris Lattnercee56e72009-03-13 05:53:31 +00008058 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(LHS)) {
8059 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(RHS))
Chris Lattnerd1980a52009-03-12 06:52:53 +00008060 // Don't do this for crazy integer types.
8061 if (DAG.getTargetLoweringInfo().isTypeLegal(LHS.getValueType())) {
8062 // If this is efficiently invertible, canonicalize the LHSC/RHSC values
Chris Lattnercee56e72009-03-13 05:53:31 +00008063 // so that TrueC (the true value) is larger than FalseC.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008064 bool NeedsCondInvert = false;
8065
Chris Lattnercee56e72009-03-13 05:53:31 +00008066 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue()) &&
Chris Lattnerd1980a52009-03-12 06:52:53 +00008067 // Efficiently invertible.
8068 (Cond.getOpcode() == ISD::SETCC || // setcc -> invertible.
8069 (Cond.getOpcode() == ISD::XOR && // xor(X, C) -> invertible.
8070 isa<ConstantSDNode>(Cond.getOperand(1))))) {
8071 NeedsCondInvert = true;
Chris Lattnercee56e72009-03-13 05:53:31 +00008072 std::swap(TrueC, FalseC);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008073 }
8074
8075 // Optimize C ? 8 : 0 -> zext(C) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008076 if (FalseC->getAPIntValue() == 0 &&
8077 TrueC->getAPIntValue().isPowerOf2()) {
Chris Lattnerd1980a52009-03-12 06:52:53 +00008078 if (NeedsCondInvert) // Invert the condition if needed.
8079 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8080 DAG.getConstant(1, Cond.getValueType()));
8081
8082 // Zero extend the condition if needed.
8083 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, LHS.getValueType(), Cond);
8084
Chris Lattnercee56e72009-03-13 05:53:31 +00008085 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
Chris Lattnerd1980a52009-03-12 06:52:53 +00008086 return DAG.getNode(ISD::SHL, DL, LHS.getValueType(), Cond,
8087 DAG.getConstant(ShAmt, MVT::i8));
8088 }
Chris Lattner97a29a52009-03-13 05:22:11 +00008089
8090 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst.
Chris Lattnercee56e72009-03-13 05:53:31 +00008091 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
Chris Lattner97a29a52009-03-13 05:22:11 +00008092 if (NeedsCondInvert) // Invert the condition if needed.
8093 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8094 DAG.getConstant(1, Cond.getValueType()));
8095
8096 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008097 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8098 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008099 return DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
Chris Lattnercee56e72009-03-13 05:53:31 +00008100 SDValue(FalseC, 0));
Chris Lattner97a29a52009-03-13 05:22:11 +00008101 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008102
8103 // Optimize cases that will turn into an LEA instruction. This requires
8104 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8105 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8106 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8107 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8108
8109 bool isFastMultiplier = false;
8110 if (Diff < 10) {
8111 switch ((unsigned char)Diff) {
8112 default: break;
8113 case 1: // result = add base, cond
8114 case 2: // result = lea base( , cond*2)
8115 case 3: // result = lea base(cond, cond*2)
8116 case 4: // result = lea base( , cond*4)
8117 case 5: // result = lea base(cond, cond*4)
8118 case 8: // result = lea base( , cond*8)
8119 case 9: // result = lea base(cond, cond*8)
8120 isFastMultiplier = true;
8121 break;
8122 }
8123 }
8124
8125 if (isFastMultiplier) {
8126 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8127 if (NeedsCondInvert) // Invert the condition if needed.
8128 Cond = DAG.getNode(ISD::XOR, DL, Cond.getValueType(), Cond,
8129 DAG.getConstant(1, Cond.getValueType()));
8130
8131 // Zero extend the condition if needed.
8132 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8133 Cond);
8134 // Scale the condition by the difference.
8135 if (Diff != 1)
8136 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8137 DAG.getConstant(Diff, Cond.getValueType()));
8138
8139 // Add the base if non-zero.
8140 if (FalseC->getAPIntValue() != 0)
8141 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8142 SDValue(FalseC, 0));
8143 return Cond;
8144 }
8145 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008146 }
8147 }
8148
Dan Gohman475871a2008-07-27 21:46:04 +00008149 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00008150}
8151
Chris Lattnerd1980a52009-03-12 06:52:53 +00008152/// Optimize X86ISD::CMOV [LHS, RHS, CONDCODE (e.g. X86::COND_NE), CONDVAL]
8153static SDValue PerformCMOVCombine(SDNode *N, SelectionDAG &DAG,
8154 TargetLowering::DAGCombinerInfo &DCI) {
8155 DebugLoc DL = N->getDebugLoc();
8156
8157 // If the flag operand isn't dead, don't touch this CMOV.
8158 if (N->getNumValues() == 2 && !SDValue(N, 1).use_empty())
8159 return SDValue();
8160
8161 // If this is a select between two integer constants, try to do some
8162 // optimizations. Note that the operands are ordered the opposite of SELECT
8163 // operands.
8164 if (ConstantSDNode *TrueC = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
8165 if (ConstantSDNode *FalseC = dyn_cast<ConstantSDNode>(N->getOperand(0))) {
8166 // Canonicalize the TrueC/FalseC values so that TrueC (the true value) is
8167 // larger than FalseC (the false value).
8168 X86::CondCode CC = (X86::CondCode)N->getConstantOperandVal(2);
8169
8170 if (TrueC->getAPIntValue().ult(FalseC->getAPIntValue())) {
8171 CC = X86::GetOppositeBranchCondition(CC);
8172 std::swap(TrueC, FalseC);
8173 }
8174
8175 // Optimize C ? 8 : 0 -> zext(setcc(C)) << 3. Likewise for any pow2/0.
Chris Lattnercee56e72009-03-13 05:53:31 +00008176 // This is efficient for any integer data type (including i8/i16) and
8177 // shift amount.
Chris Lattnerd1980a52009-03-12 06:52:53 +00008178 if (FalseC->getAPIntValue() == 0 && TrueC->getAPIntValue().isPowerOf2()) {
8179 SDValue Cond = N->getOperand(3);
8180 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8181 DAG.getConstant(CC, MVT::i8), Cond);
8182
8183 // Zero extend the condition if needed.
8184 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, TrueC->getValueType(0), Cond);
8185
8186 unsigned ShAmt = TrueC->getAPIntValue().logBase2();
8187 Cond = DAG.getNode(ISD::SHL, DL, Cond.getValueType(), Cond,
8188 DAG.getConstant(ShAmt, MVT::i8));
8189 if (N->getNumValues() == 2) // Dead flag value?
8190 return DCI.CombineTo(N, Cond, SDValue());
8191 return Cond;
8192 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008193
8194 // Optimize Cond ? cst+1 : cst -> zext(setcc(C)+cst. This is efficient
8195 // for any integer data type, including i8/i16.
Chris Lattner97a29a52009-03-13 05:22:11 +00008196 if (FalseC->getAPIntValue()+1 == TrueC->getAPIntValue()) {
8197 SDValue Cond = N->getOperand(3);
8198 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8199 DAG.getConstant(CC, MVT::i8), Cond);
8200
8201 // Zero extend the condition if needed.
Chris Lattnercee56e72009-03-13 05:53:31 +00008202 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL,
8203 FalseC->getValueType(0), Cond);
Chris Lattner97a29a52009-03-13 05:22:11 +00008204 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8205 SDValue(FalseC, 0));
Chris Lattnercee56e72009-03-13 05:53:31 +00008206
Chris Lattner97a29a52009-03-13 05:22:11 +00008207 if (N->getNumValues() == 2) // Dead flag value?
8208 return DCI.CombineTo(N, Cond, SDValue());
8209 return Cond;
8210 }
Chris Lattnercee56e72009-03-13 05:53:31 +00008211
8212 // Optimize cases that will turn into an LEA instruction. This requires
8213 // an i32 or i64 and an efficient multiplier (1, 2, 3, 4, 5, 8, 9).
8214 if (N->getValueType(0) == MVT::i32 || N->getValueType(0) == MVT::i64) {
8215 uint64_t Diff = TrueC->getZExtValue()-FalseC->getZExtValue();
8216 if (N->getValueType(0) == MVT::i32) Diff = (unsigned)Diff;
8217
8218 bool isFastMultiplier = false;
8219 if (Diff < 10) {
8220 switch ((unsigned char)Diff) {
8221 default: break;
8222 case 1: // result = add base, cond
8223 case 2: // result = lea base( , cond*2)
8224 case 3: // result = lea base(cond, cond*2)
8225 case 4: // result = lea base( , cond*4)
8226 case 5: // result = lea base(cond, cond*4)
8227 case 8: // result = lea base( , cond*8)
8228 case 9: // result = lea base(cond, cond*8)
8229 isFastMultiplier = true;
8230 break;
8231 }
8232 }
8233
8234 if (isFastMultiplier) {
8235 APInt Diff = TrueC->getAPIntValue()-FalseC->getAPIntValue();
8236 SDValue Cond = N->getOperand(3);
8237 Cond = DAG.getNode(X86ISD::SETCC, DL, MVT::i8,
8238 DAG.getConstant(CC, MVT::i8), Cond);
8239 // Zero extend the condition if needed.
8240 Cond = DAG.getNode(ISD::ZERO_EXTEND, DL, FalseC->getValueType(0),
8241 Cond);
8242 // Scale the condition by the difference.
8243 if (Diff != 1)
8244 Cond = DAG.getNode(ISD::MUL, DL, Cond.getValueType(), Cond,
8245 DAG.getConstant(Diff, Cond.getValueType()));
8246
8247 // Add the base if non-zero.
8248 if (FalseC->getAPIntValue() != 0)
8249 Cond = DAG.getNode(ISD::ADD, DL, Cond.getValueType(), Cond,
8250 SDValue(FalseC, 0));
8251 if (N->getNumValues() == 2) // Dead flag value?
8252 return DCI.CombineTo(N, Cond, SDValue());
8253 return Cond;
8254 }
8255 }
Chris Lattnerd1980a52009-03-12 06:52:53 +00008256 }
8257 }
8258 return SDValue();
8259}
8260
8261
Evan Cheng0b0cd912009-03-28 05:57:29 +00008262/// PerformMulCombine - Optimize a single multiply with constant into two
8263/// in order to implement it with two cheaper instructions, e.g.
8264/// LEA + SHL, LEA + LEA.
8265static SDValue PerformMulCombine(SDNode *N, SelectionDAG &DAG,
8266 TargetLowering::DAGCombinerInfo &DCI) {
8267 if (DAG.getMachineFunction().
8268 getFunction()->hasFnAttr(Attribute::OptimizeForSize))
8269 return SDValue();
8270
8271 if (DCI.isBeforeLegalize() || DCI.isCalledByLegalizer())
8272 return SDValue();
8273
8274 MVT VT = N->getValueType(0);
8275 if (VT != MVT::i64)
8276 return SDValue();
8277
8278 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1));
8279 if (!C)
8280 return SDValue();
8281 uint64_t MulAmt = C->getZExtValue();
8282 if (isPowerOf2_64(MulAmt) || MulAmt == 3 || MulAmt == 5 || MulAmt == 9)
8283 return SDValue();
8284
8285 uint64_t MulAmt1 = 0;
8286 uint64_t MulAmt2 = 0;
8287 if ((MulAmt % 9) == 0) {
8288 MulAmt1 = 9;
8289 MulAmt2 = MulAmt / 9;
8290 } else if ((MulAmt % 5) == 0) {
8291 MulAmt1 = 5;
8292 MulAmt2 = MulAmt / 5;
8293 } else if ((MulAmt % 3) == 0) {
8294 MulAmt1 = 3;
8295 MulAmt2 = MulAmt / 3;
8296 }
8297 if (MulAmt2 &&
8298 (isPowerOf2_64(MulAmt2) || MulAmt2 == 3 || MulAmt2 == 5 || MulAmt2 == 9)){
8299 DebugLoc DL = N->getDebugLoc();
8300
8301 if (isPowerOf2_64(MulAmt2) &&
8302 !(N->hasOneUse() && N->use_begin()->getOpcode() == ISD::ADD))
8303 // If second multiplifer is pow2, issue it first. We want the multiply by
8304 // 3, 5, or 9 to be folded into the addressing mode unless the lone use
8305 // is an add.
8306 std::swap(MulAmt1, MulAmt2);
8307
8308 SDValue NewMul;
8309 if (isPowerOf2_64(MulAmt1))
8310 NewMul = DAG.getNode(ISD::SHL, DL, VT, N->getOperand(0),
8311 DAG.getConstant(Log2_64(MulAmt1), MVT::i8));
8312 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008313 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, N->getOperand(0),
Evan Cheng0b0cd912009-03-28 05:57:29 +00008314 DAG.getConstant(MulAmt1, VT));
8315
8316 if (isPowerOf2_64(MulAmt2))
8317 NewMul = DAG.getNode(ISD::SHL, DL, VT, NewMul,
8318 DAG.getConstant(Log2_64(MulAmt2), MVT::i8));
8319 else
Evan Cheng73f24c92009-03-30 21:36:47 +00008320 NewMul = DAG.getNode(X86ISD::MUL_IMM, DL, VT, NewMul,
Evan Cheng0b0cd912009-03-28 05:57:29 +00008321 DAG.getConstant(MulAmt2, VT));
8322
8323 // Do not add new nodes to DAG combiner worklist.
8324 DCI.CombineTo(N, NewMul, false);
8325 }
8326 return SDValue();
8327}
8328
8329
Nate Begeman740ab032009-01-26 00:52:55 +00008330/// PerformShiftCombine - Transforms vector shift nodes to use vector shifts
8331/// when possible.
8332static SDValue PerformShiftCombine(SDNode* N, SelectionDAG &DAG,
8333 const X86Subtarget *Subtarget) {
8334 // On X86 with SSE2 support, we can transform this to a vector shift if
8335 // all elements are shifted by the same amount. We can't do this in legalize
8336 // because the a constant vector is typically transformed to a constant pool
8337 // so we have no knowledge of the shift amount.
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008338 if (!Subtarget->hasSSE2())
8339 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008340
Nate Begeman740ab032009-01-26 00:52:55 +00008341 MVT VT = N->getValueType(0);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008342 if (VT != MVT::v2i64 && VT != MVT::v4i32 && VT != MVT::v8i16)
8343 return SDValue();
Scott Michelfdc40a02009-02-17 22:15:04 +00008344
Mon P Wang3becd092009-01-28 08:12:05 +00008345 SDValue ShAmtOp = N->getOperand(1);
8346 MVT EltVT = VT.getVectorElementType();
Chris Lattner47b4ce82009-03-11 05:48:52 +00008347 DebugLoc DL = N->getDebugLoc();
Mon P Wang3becd092009-01-28 08:12:05 +00008348 SDValue BaseShAmt;
8349 if (ShAmtOp.getOpcode() == ISD::BUILD_VECTOR) {
8350 unsigned NumElts = VT.getVectorNumElements();
8351 unsigned i = 0;
8352 for (; i != NumElts; ++i) {
8353 SDValue Arg = ShAmtOp.getOperand(i);
8354 if (Arg.getOpcode() == ISD::UNDEF) continue;
8355 BaseShAmt = Arg;
8356 break;
8357 }
8358 for (; i != NumElts; ++i) {
8359 SDValue Arg = ShAmtOp.getOperand(i);
8360 if (Arg.getOpcode() == ISD::UNDEF) continue;
8361 if (Arg != BaseShAmt) {
8362 return SDValue();
8363 }
8364 }
8365 } else if (ShAmtOp.getOpcode() == ISD::VECTOR_SHUFFLE &&
Nate Begeman9008ca62009-04-27 18:41:29 +00008366 cast<ShuffleVectorSDNode>(ShAmtOp)->isSplat()) {
8367 BaseShAmt = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, DL, EltVT, ShAmtOp,
8368 DAG.getIntPtrConstant(0));
Mon P Wang3becd092009-01-28 08:12:05 +00008369 } else
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008370 return SDValue();
Nate Begeman740ab032009-01-26 00:52:55 +00008371
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008372 if (EltVT.bitsGT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008373 BaseShAmt = DAG.getNode(ISD::TRUNCATE, DL, MVT::i32, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008374 else if (EltVT.bitsLT(MVT::i32))
Chris Lattner47b4ce82009-03-11 05:48:52 +00008375 BaseShAmt = DAG.getNode(ISD::ANY_EXTEND, DL, MVT::i32, BaseShAmt);
Nate Begeman740ab032009-01-26 00:52:55 +00008376
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008377 // The shift amount is identical so we can do a vector shift.
8378 SDValue ValOp = N->getOperand(0);
8379 switch (N->getOpcode()) {
8380 default:
Torok Edwinc23197a2009-07-14 16:55:14 +00008381 llvm_unreachable("Unknown shift opcode!");
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008382 break;
8383 case ISD::SHL:
8384 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008385 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008386 DAG.getConstant(Intrinsic::x86_sse2_pslli_q, MVT::i32),
8387 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008388 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008389 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008390 DAG.getConstant(Intrinsic::x86_sse2_pslli_d, MVT::i32),
8391 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008392 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008393 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008394 DAG.getConstant(Intrinsic::x86_sse2_pslli_w, MVT::i32),
8395 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008396 break;
8397 case ISD::SRA:
8398 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008399 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008400 DAG.getConstant(Intrinsic::x86_sse2_psrai_d, MVT::i32),
8401 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008402 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008403 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008404 DAG.getConstant(Intrinsic::x86_sse2_psrai_w, MVT::i32),
8405 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008406 break;
8407 case ISD::SRL:
8408 if (VT == MVT::v2i64)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008409 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008410 DAG.getConstant(Intrinsic::x86_sse2_psrli_q, MVT::i32),
8411 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008412 if (VT == MVT::v4i32)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008413 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008414 DAG.getConstant(Intrinsic::x86_sse2_psrli_d, MVT::i32),
8415 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008416 if (VT == MVT::v8i16)
Chris Lattner47b4ce82009-03-11 05:48:52 +00008417 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, DL, VT,
Nate Begeman740ab032009-01-26 00:52:55 +00008418 DAG.getConstant(Intrinsic::x86_sse2_psrli_w, MVT::i32),
8419 ValOp, BaseShAmt);
Nate Begemanc2fd67f2009-01-26 03:15:31 +00008420 break;
Nate Begeman740ab032009-01-26 00:52:55 +00008421 }
8422 return SDValue();
8423}
8424
Chris Lattner149a4e52008-02-22 02:09:43 +00008425/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008426static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng536e6672009-03-12 05:59:15 +00008427 const X86Subtarget *Subtarget) {
Chris Lattner149a4e52008-02-22 02:09:43 +00008428 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
8429 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00008430 // A preferable solution to the general problem is to figure out the right
8431 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng536e6672009-03-12 05:59:15 +00008432
8433 // Similarly, turn load->store of i64 into double load/stores in 32-bit mode.
Evan Cheng7e2ff772008-05-08 00:57:18 +00008434 StoreSDNode *St = cast<StoreSDNode>(N);
Evan Cheng536e6672009-03-12 05:59:15 +00008435 MVT VT = St->getValue().getValueType();
8436 if (VT.getSizeInBits() != 64)
8437 return SDValue();
8438
Devang Patel578efa92009-06-05 21:57:13 +00008439 const Function *F = DAG.getMachineFunction().getFunction();
8440 bool NoImplicitFloatOps = F->hasFnAttr(Attribute::NoImplicitFloat);
8441 bool F64IsLegal = !UseSoftFloat && !NoImplicitFloatOps
8442 && Subtarget->hasSSE2();
Evan Cheng536e6672009-03-12 05:59:15 +00008443 if ((VT.isVector() ||
8444 (VT == MVT::i64 && F64IsLegal && !Subtarget->is64Bit())) &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00008445 isa<LoadSDNode>(St->getValue()) &&
8446 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
8447 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008448 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008449 LoadSDNode *Ld = 0;
8450 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00008451 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00008452 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008453 // Must be a store of a load. We currently handle two cases: the load
8454 // is a direct child, and it's under an intervening TokenFactor. It is
8455 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00008456 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00008457 Ld = cast<LoadSDNode>(St->getChain());
8458 else if (St->getValue().hasOneUse() &&
8459 ChainVal->getOpcode() == ISD::TokenFactor) {
8460 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00008461 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00008462 TokenFactorIndex = i;
8463 Ld = cast<LoadSDNode>(St->getValue());
8464 } else
8465 Ops.push_back(ChainVal->getOperand(i));
8466 }
8467 }
Dale Johannesen079f2a62008-02-25 19:20:14 +00008468
Evan Cheng536e6672009-03-12 05:59:15 +00008469 if (!Ld || !ISD::isNormalLoad(Ld))
8470 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008471
Evan Cheng536e6672009-03-12 05:59:15 +00008472 // If this is not the MMX case, i.e. we are just turning i64 load/store
8473 // into f64 load/store, avoid the transformation if there are multiple
8474 // uses of the loaded value.
8475 if (!VT.isVector() && !Ld->hasNUsesOfValue(1, 0))
8476 return SDValue();
Dale Johannesen079f2a62008-02-25 19:20:14 +00008477
Evan Cheng536e6672009-03-12 05:59:15 +00008478 DebugLoc LdDL = Ld->getDebugLoc();
8479 DebugLoc StDL = N->getDebugLoc();
8480 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
8481 // Otherwise, if it's legal to use f64 SSE instructions, use f64 load/store
8482 // pair instead.
8483 if (Subtarget->is64Bit() || F64IsLegal) {
8484 MVT LdVT = Subtarget->is64Bit() ? MVT::i64 : MVT::f64;
8485 SDValue NewLd = DAG.getLoad(LdVT, LdDL, Ld->getChain(),
8486 Ld->getBasePtr(), Ld->getSrcValue(),
8487 Ld->getSrcValueOffset(), Ld->isVolatile(),
8488 Ld->getAlignment());
8489 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00008490 if (TokenFactorIndex != -1) {
Evan Cheng536e6672009-03-12 05:59:15 +00008491 Ops.push_back(NewChain);
8492 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
Dale Johannesen079f2a62008-02-25 19:20:14 +00008493 Ops.size());
8494 }
Evan Cheng536e6672009-03-12 05:59:15 +00008495 return DAG.getStore(NewChain, StDL, NewLd, St->getBasePtr(),
Chris Lattner149a4e52008-02-22 02:09:43 +00008496 St->getSrcValue(), St->getSrcValueOffset(),
8497 St->isVolatile(), St->getAlignment());
8498 }
Evan Cheng536e6672009-03-12 05:59:15 +00008499
8500 // Otherwise, lower to two pairs of 32-bit loads / stores.
8501 SDValue LoAddr = Ld->getBasePtr();
8502 SDValue HiAddr = DAG.getNode(ISD::ADD, LdDL, MVT::i32, LoAddr,
8503 DAG.getConstant(4, MVT::i32));
8504
8505 SDValue LoLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), LoAddr,
8506 Ld->getSrcValue(), Ld->getSrcValueOffset(),
8507 Ld->isVolatile(), Ld->getAlignment());
8508 SDValue HiLd = DAG.getLoad(MVT::i32, LdDL, Ld->getChain(), HiAddr,
8509 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
8510 Ld->isVolatile(),
8511 MinAlign(Ld->getAlignment(), 4));
8512
8513 SDValue NewChain = LoLd.getValue(1);
8514 if (TokenFactorIndex != -1) {
8515 Ops.push_back(LoLd);
8516 Ops.push_back(HiLd);
8517 NewChain = DAG.getNode(ISD::TokenFactor, LdDL, MVT::Other, &Ops[0],
8518 Ops.size());
8519 }
8520
8521 LoAddr = St->getBasePtr();
8522 HiAddr = DAG.getNode(ISD::ADD, StDL, MVT::i32, LoAddr,
8523 DAG.getConstant(4, MVT::i32));
8524
8525 SDValue LoSt = DAG.getStore(NewChain, StDL, LoLd, LoAddr,
8526 St->getSrcValue(), St->getSrcValueOffset(),
8527 St->isVolatile(), St->getAlignment());
8528 SDValue HiSt = DAG.getStore(NewChain, StDL, HiLd, HiAddr,
8529 St->getSrcValue(),
8530 St->getSrcValueOffset() + 4,
8531 St->isVolatile(),
8532 MinAlign(St->getAlignment(), 4));
8533 return DAG.getNode(ISD::TokenFactor, StDL, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00008534 }
Dan Gohman475871a2008-07-27 21:46:04 +00008535 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00008536}
8537
Chris Lattner6cf73262008-01-25 06:14:17 +00008538/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
8539/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008540static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00008541 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
8542 // F[X]OR(0.0, x) -> x
8543 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00008544 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8545 if (C->getValueAPF().isPosZero())
8546 return N->getOperand(1);
8547 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8548 if (C->getValueAPF().isPosZero())
8549 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00008550 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008551}
8552
8553/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00008554static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00008555 // FAND(0.0, x) -> 0.0
8556 // FAND(x, 0.0) -> 0.0
8557 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
8558 if (C->getValueAPF().isPosZero())
8559 return N->getOperand(0);
8560 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
8561 if (C->getValueAPF().isPosZero())
8562 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00008563 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00008564}
8565
Dan Gohmane5af2d32009-01-29 01:59:02 +00008566static SDValue PerformBTCombine(SDNode *N,
8567 SelectionDAG &DAG,
8568 TargetLowering::DAGCombinerInfo &DCI) {
8569 // BT ignores high bits in the bit index operand.
8570 SDValue Op1 = N->getOperand(1);
8571 if (Op1.hasOneUse()) {
8572 unsigned BitWidth = Op1.getValueSizeInBits();
8573 APInt DemandedMask = APInt::getLowBitsSet(BitWidth, Log2_32(BitWidth));
8574 APInt KnownZero, KnownOne;
8575 TargetLowering::TargetLoweringOpt TLO(DAG);
8576 TargetLowering &TLI = DAG.getTargetLoweringInfo();
8577 if (TLO.ShrinkDemandedConstant(Op1, DemandedMask) ||
8578 TLI.SimplifyDemandedBits(Op1, DemandedMask, KnownZero, KnownOne, TLO))
8579 DCI.CommitTargetLoweringOpt(TLO);
8580 }
8581 return SDValue();
8582}
Chris Lattner83e6c992006-10-04 06:57:07 +00008583
Eli Friedman7a5e5552009-06-07 06:52:44 +00008584static SDValue PerformVZEXT_MOVLCombine(SDNode *N, SelectionDAG &DAG) {
8585 SDValue Op = N->getOperand(0);
8586 if (Op.getOpcode() == ISD::BIT_CONVERT)
8587 Op = Op.getOperand(0);
8588 MVT VT = N->getValueType(0), OpVT = Op.getValueType();
8589 if (Op.getOpcode() == X86ISD::VZEXT_LOAD &&
8590 VT.getVectorElementType().getSizeInBits() ==
8591 OpVT.getVectorElementType().getSizeInBits()) {
8592 return DAG.getNode(ISD::BIT_CONVERT, N->getDebugLoc(), VT, Op);
8593 }
8594 return SDValue();
8595}
8596
Owen Anderson99177002009-06-29 18:04:45 +00008597// On X86 and X86-64, atomic operations are lowered to locked instructions.
8598// Locked instructions, in turn, have implicit fence semantics (all memory
8599// operations are flushed before issuing the locked instruction, and the
8600// are not buffered), so we can fold away the common pattern of
8601// fence-atomic-fence.
8602static SDValue PerformMEMBARRIERCombine(SDNode* N, SelectionDAG &DAG) {
8603 SDValue atomic = N->getOperand(0);
8604 switch (atomic.getOpcode()) {
8605 case ISD::ATOMIC_CMP_SWAP:
8606 case ISD::ATOMIC_SWAP:
8607 case ISD::ATOMIC_LOAD_ADD:
8608 case ISD::ATOMIC_LOAD_SUB:
8609 case ISD::ATOMIC_LOAD_AND:
8610 case ISD::ATOMIC_LOAD_OR:
8611 case ISD::ATOMIC_LOAD_XOR:
8612 case ISD::ATOMIC_LOAD_NAND:
8613 case ISD::ATOMIC_LOAD_MIN:
8614 case ISD::ATOMIC_LOAD_MAX:
8615 case ISD::ATOMIC_LOAD_UMIN:
8616 case ISD::ATOMIC_LOAD_UMAX:
8617 break;
8618 default:
8619 return SDValue();
8620 }
8621
8622 SDValue fence = atomic.getOperand(0);
8623 if (fence.getOpcode() != ISD::MEMBARRIER)
8624 return SDValue();
8625
8626 switch (atomic.getOpcode()) {
8627 case ISD::ATOMIC_CMP_SWAP:
8628 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8629 atomic.getOperand(1), atomic.getOperand(2),
8630 atomic.getOperand(3));
8631 case ISD::ATOMIC_SWAP:
8632 case ISD::ATOMIC_LOAD_ADD:
8633 case ISD::ATOMIC_LOAD_SUB:
8634 case ISD::ATOMIC_LOAD_AND:
8635 case ISD::ATOMIC_LOAD_OR:
8636 case ISD::ATOMIC_LOAD_XOR:
8637 case ISD::ATOMIC_LOAD_NAND:
8638 case ISD::ATOMIC_LOAD_MIN:
8639 case ISD::ATOMIC_LOAD_MAX:
8640 case ISD::ATOMIC_LOAD_UMIN:
8641 case ISD::ATOMIC_LOAD_UMAX:
8642 return DAG.UpdateNodeOperands(atomic, fence.getOperand(0),
8643 atomic.getOperand(1), atomic.getOperand(2));
8644 default:
8645 return SDValue();
8646 }
8647}
8648
Dan Gohman475871a2008-07-27 21:46:04 +00008649SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng9dd93b32008-11-05 06:03:38 +00008650 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00008651 SelectionDAG &DAG = DCI.DAG;
8652 switch (N->getOpcode()) {
8653 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00008654 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00008655 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Chris Lattnerd1980a52009-03-12 06:52:53 +00008656 case X86ISD::CMOV: return PerformCMOVCombine(N, DAG, DCI);
Evan Cheng0b0cd912009-03-28 05:57:29 +00008657 case ISD::MUL: return PerformMulCombine(N, DAG, DCI);
Nate Begeman740ab032009-01-26 00:52:55 +00008658 case ISD::SHL:
8659 case ISD::SRA:
8660 case ISD::SRL: return PerformShiftCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00008661 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00008662 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00008663 case X86ISD::FOR: return PerformFORCombine(N, DAG);
8664 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Dan Gohmane5af2d32009-01-29 01:59:02 +00008665 case X86ISD::BT: return PerformBTCombine(N, DAG, DCI);
Eli Friedman7a5e5552009-06-07 06:52:44 +00008666 case X86ISD::VZEXT_MOVL: return PerformVZEXT_MOVLCombine(N, DAG);
Owen Anderson99177002009-06-29 18:04:45 +00008667 case ISD::MEMBARRIER: return PerformMEMBARRIERCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00008668 }
8669
Dan Gohman475871a2008-07-27 21:46:04 +00008670 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00008671}
8672
Evan Cheng60c07e12006-07-05 22:17:51 +00008673//===----------------------------------------------------------------------===//
8674// X86 Inline Assembly Support
8675//===----------------------------------------------------------------------===//
8676
Chris Lattnerb8105652009-07-20 17:51:36 +00008677static bool LowerToBSwap(CallInst *CI) {
8678 // FIXME: this should verify that we are targetting a 486 or better. If not,
8679 // we will turn this bswap into something that will be lowered to logical ops
8680 // instead of emitting the bswap asm. For now, we don't support 486 or lower
8681 // so don't worry about this.
8682
8683 // Verify this is a simple bswap.
8684 if (CI->getNumOperands() != 2 ||
8685 CI->getType() != CI->getOperand(1)->getType() ||
8686 !CI->getType()->isInteger())
8687 return false;
8688
8689 const IntegerType *Ty = dyn_cast<IntegerType>(CI->getType());
8690 if (!Ty || Ty->getBitWidth() % 16 != 0)
8691 return false;
8692
8693 // Okay, we can do this xform, do so now.
8694 const Type *Tys[] = { Ty };
8695 Module *M = CI->getParent()->getParent()->getParent();
8696 Constant *Int = Intrinsic::getDeclaration(M, Intrinsic::bswap, Tys, 1);
8697
8698 Value *Op = CI->getOperand(1);
8699 Op = CallInst::Create(Int, Op, CI->getName(), CI);
8700
8701 CI->replaceAllUsesWith(Op);
8702 CI->eraseFromParent();
8703 return true;
8704}
8705
8706bool X86TargetLowering::ExpandInlineAsm(CallInst *CI) const {
8707 InlineAsm *IA = cast<InlineAsm>(CI->getCalledValue());
8708 std::vector<InlineAsm::ConstraintInfo> Constraints = IA->ParseConstraints();
8709
8710 std::string AsmStr = IA->getAsmString();
8711
8712 // TODO: should remove alternatives from the asmstring: "foo {a|b}" -> "foo a"
8713 std::vector<std::string> AsmPieces;
8714 SplitString(AsmStr, AsmPieces, "\n"); // ; as separator?
8715
8716 switch (AsmPieces.size()) {
8717 default: return false;
8718 case 1:
8719 AsmStr = AsmPieces[0];
8720 AsmPieces.clear();
8721 SplitString(AsmStr, AsmPieces, " \t"); // Split with whitespace.
8722
8723 // bswap $0
8724 if (AsmPieces.size() == 2 &&
8725 (AsmPieces[0] == "bswap" ||
8726 AsmPieces[0] == "bswapq" ||
8727 AsmPieces[0] == "bswapl") &&
8728 (AsmPieces[1] == "$0" ||
8729 AsmPieces[1] == "${0:q}")) {
8730 // No need to check constraints, nothing other than the equivalent of
8731 // "=r,0" would be valid here.
8732 return LowerToBSwap(CI);
8733 }
8734 // rorw $$8, ${0:w} --> llvm.bswap.i16
8735 if (CI->getType() == Type::Int16Ty &&
8736 AsmPieces.size() == 3 &&
8737 AsmPieces[0] == "rorw" &&
8738 AsmPieces[1] == "$$8," &&
8739 AsmPieces[2] == "${0:w}" &&
8740 IA->getConstraintString() == "=r,0,~{dirflag},~{fpsr},~{flags},~{cc}") {
8741 return LowerToBSwap(CI);
8742 }
8743 break;
8744 case 3:
8745 if (CI->getType() == Type::Int64Ty && Constraints.size() >= 2 &&
8746 Constraints[0].Codes.size() == 1 && Constraints[0].Codes[0] == "A" &&
8747 Constraints[1].Codes.size() == 1 && Constraints[1].Codes[0] == "0") {
8748 // bswap %eax / bswap %edx / xchgl %eax, %edx -> llvm.bswap.i64
8749 std::vector<std::string> Words;
8750 SplitString(AsmPieces[0], Words, " \t");
8751 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%eax") {
8752 Words.clear();
8753 SplitString(AsmPieces[1], Words, " \t");
8754 if (Words.size() == 2 && Words[0] == "bswap" && Words[1] == "%edx") {
8755 Words.clear();
8756 SplitString(AsmPieces[2], Words, " \t,");
8757 if (Words.size() == 3 && Words[0] == "xchgl" && Words[1] == "%eax" &&
8758 Words[2] == "%edx") {
8759 return LowerToBSwap(CI);
8760 }
8761 }
8762 }
8763 }
8764 break;
8765 }
8766 return false;
8767}
8768
8769
8770
Chris Lattnerf4dff842006-07-11 02:54:03 +00008771/// getConstraintType - Given a constraint letter, return the type of
8772/// constraint it is for this target.
8773X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00008774X86TargetLowering::getConstraintType(const std::string &Constraint) const {
8775 if (Constraint.size() == 1) {
8776 switch (Constraint[0]) {
8777 case 'A':
Dale Johannesen330169f2008-11-13 21:52:36 +00008778 return C_Register;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00008779 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00008780 case 'r':
8781 case 'R':
8782 case 'l':
8783 case 'q':
8784 case 'Q':
8785 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00008786 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00008787 case 'Y':
8788 return C_RegisterClass;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008789 case 'e':
8790 case 'Z':
8791 return C_Other;
Chris Lattner4234f572007-03-25 02:14:49 +00008792 default:
8793 break;
8794 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00008795 }
Chris Lattner4234f572007-03-25 02:14:49 +00008796 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00008797}
8798
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008799/// LowerXConstraint - try to replace an X constraint, which matches anything,
8800/// with another that has more specific requirements based on the type of the
8801/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00008802const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00008803LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00008804 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
8805 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00008806 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008807 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00008808 return "Y";
8809 if (Subtarget->hasSSE1())
8810 return "x";
8811 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008812
Chris Lattner5e764232008-04-26 23:02:14 +00008813 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00008814}
8815
Chris Lattner48884cd2007-08-25 00:47:38 +00008816/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
8817/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00008818void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00008819 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00008820 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00008821 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00008822 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00008823 SDValue Result(0, 0);
Scott Michelfdc40a02009-02-17 22:15:04 +00008824
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008825 switch (Constraint) {
8826 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00008827 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00008828 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008829 if (C->getZExtValue() <= 31) {
8830 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008831 break;
8832 }
Devang Patel84f7fd22007-03-17 00:13:28 +00008833 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008834 return;
Evan Cheng364091e2008-09-22 23:57:37 +00008835 case 'J':
8836 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008837 if (C->getZExtValue() <= 63) {
Chris Lattnere4935152009-06-15 04:01:39 +00008838 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8839 break;
8840 }
8841 }
8842 return;
8843 case 'K':
8844 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner2e06dd22009-06-15 04:39:05 +00008845 if ((int8_t)C->getSExtValue() == C->getSExtValue()) {
Evan Cheng364091e2008-09-22 23:57:37 +00008846 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8847 break;
8848 }
8849 }
8850 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00008851 case 'N':
8852 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00008853 if (C->getZExtValue() <= 255) {
8854 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00008855 break;
8856 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00008857 }
Chris Lattner48884cd2007-08-25 00:47:38 +00008858 return;
Dale Johannesen78e3e522009-02-12 20:58:09 +00008859 case 'e': {
8860 // 32-bit signed value
8861 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8862 const ConstantInt *CI = C->getConstantIntValue();
8863 if (CI->isValueValidForType(Type::Int32Ty, C->getSExtValue())) {
8864 // Widen to 64 bits here to get it sign extended.
8865 Result = DAG.getTargetConstant(C->getSExtValue(), MVT::i64);
8866 break;
8867 }
8868 // FIXME gcc accepts some relocatable values here too, but only in certain
8869 // memory models; it's complicated.
8870 }
8871 return;
8872 }
8873 case 'Z': {
8874 // 32-bit unsigned value
8875 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
8876 const ConstantInt *CI = C->getConstantIntValue();
8877 if (CI->isValueValidForType(Type::Int32Ty, C->getZExtValue())) {
8878 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
8879 break;
8880 }
8881 }
8882 // FIXME gcc accepts some relocatable values here too, but only in certain
8883 // memory models; it's complicated.
8884 return;
8885 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008886 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008887 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00008888 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dale Johannesen78e3e522009-02-12 20:58:09 +00008889 // Widen to 64 bits here to get it sign extended.
8890 Result = DAG.getTargetConstant(CST->getSExtValue(), MVT::i64);
Chris Lattner48884cd2007-08-25 00:47:38 +00008891 break;
8892 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008893
Chris Lattnerdc43a882007-05-03 16:52:29 +00008894 // If we are in non-pic codegen mode, we allow the address of a global (with
8895 // an optional displacement) to be used with 'i'.
Chris Lattner49921962009-05-08 18:23:14 +00008896 GlobalAddressSDNode *GA = 0;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008897 int64_t Offset = 0;
Scott Michelfdc40a02009-02-17 22:15:04 +00008898
Chris Lattner49921962009-05-08 18:23:14 +00008899 // Match either (GA), (GA+C), (GA+C1+C2), etc.
8900 while (1) {
8901 if ((GA = dyn_cast<GlobalAddressSDNode>(Op))) {
8902 Offset += GA->getOffset();
8903 break;
8904 } else if (Op.getOpcode() == ISD::ADD) {
8905 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8906 Offset += C->getZExtValue();
8907 Op = Op.getOperand(0);
8908 continue;
8909 }
8910 } else if (Op.getOpcode() == ISD::SUB) {
8911 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1))) {
8912 Offset += -C->getZExtValue();
8913 Op = Op.getOperand(0);
8914 continue;
8915 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008916 }
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008917
Chris Lattner49921962009-05-08 18:23:14 +00008918 // Otherwise, this isn't something we can handle, reject it.
8919 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00008920 }
Chris Lattner3b6b36d2009-07-10 06:29:59 +00008921
Chris Lattner36c25012009-07-10 07:34:39 +00008922 GlobalValue *GV = GA->getGlobal();
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008923 // If we require an extra load to get this address, as in PIC mode, we
8924 // can't accept it.
Chris Lattner36c25012009-07-10 07:34:39 +00008925 if (isGlobalStubReference(Subtarget->ClassifyGlobalReference(GV,
8926 getTargetMachine())))
Dale Johannesen76a1e2e2009-07-07 00:18:49 +00008927 return;
Scott Michelfdc40a02009-02-17 22:15:04 +00008928
Dale Johannesen60b3ba02009-07-21 00:12:29 +00008929 if (hasMemory)
8930 Op = LowerGlobalAddress(GV, Op.getDebugLoc(), Offset, DAG);
8931 else
8932 Op = DAG.getTargetGlobalAddress(GV, GA->getValueType(0), Offset);
Chris Lattner49921962009-05-08 18:23:14 +00008933 Result = Op;
8934 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008935 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00008936 }
Scott Michelfdc40a02009-02-17 22:15:04 +00008937
Gabor Greifba36cb52008-08-28 21:40:38 +00008938 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00008939 Ops.push_back(Result);
8940 return;
8941 }
Evan Chengda43bcf2008-09-24 00:05:32 +00008942 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
8943 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00008944}
8945
Chris Lattner259e97c2006-01-31 19:43:35 +00008946std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00008947getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00008948 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00008949 if (Constraint.size() == 1) {
8950 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00008951 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00008952 default: break; // Unknown constraint letter
Evan Cheng47e9fab2009-07-17 22:13:25 +00008953 case 'q': // GENERAL_REGS in 64-bit mode, Q_REGS in 32-bit mode.
8954 if (Subtarget->is64Bit()) {
8955 if (VT == MVT::i32)
8956 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX,
8957 X86::ESI, X86::EDI, X86::R8D, X86::R9D,
8958 X86::R10D,X86::R11D,X86::R12D,
8959 X86::R13D,X86::R14D,X86::R15D,
8960 X86::EBP, X86::ESP, 0);
8961 else if (VT == MVT::i16)
8962 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX,
8963 X86::SI, X86::DI, X86::R8W,X86::R9W,
8964 X86::R10W,X86::R11W,X86::R12W,
8965 X86::R13W,X86::R14W,X86::R15W,
8966 X86::BP, X86::SP, 0);
8967 else if (VT == MVT::i8)
8968 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL,
8969 X86::SIL, X86::DIL, X86::R8B,X86::R9B,
8970 X86::R10B,X86::R11B,X86::R12B,
8971 X86::R13B,X86::R14B,X86::R15B,
8972 X86::BPL, X86::SPL, 0);
8973
8974 else if (VT == MVT::i64)
8975 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX,
8976 X86::RSI, X86::RDI, X86::R8, X86::R9,
8977 X86::R10, X86::R11, X86::R12,
8978 X86::R13, X86::R14, X86::R15,
8979 X86::RBP, X86::RSP, 0);
8980
8981 break;
8982 }
8983 // 32-bit fallthrough
Chris Lattner259e97c2006-01-31 19:43:35 +00008984 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00008985 if (VT == MVT::i32)
8986 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
8987 else if (VT == MVT::i16)
8988 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
8989 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00008990 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00008991 else if (VT == MVT::i64)
8992 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
8993 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00008994 }
8995 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00008996
Chris Lattner1efa40f2006-02-22 00:56:39 +00008997 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00008998}
Chris Lattnerf76d1802006-07-31 23:26:50 +00008999
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009000std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00009001X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00009002 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00009003 // First, see if this is a constraint that directly corresponds to an LLVM
9004 // register class.
9005 if (Constraint.size() == 1) {
9006 // GCC Constraint Letters
9007 switch (Constraint[0]) {
9008 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00009009 case 'r': // GENERAL_REGS
9010 case 'R': // LEGACY_REGS
9011 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00009012 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00009013 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009014 if (VT == MVT::i16)
9015 return std::make_pair(0U, X86::GR16RegisterClass);
9016 if (VT == MVT::i32 || !Subtarget->is64Bit())
Scott Michelfdc40a02009-02-17 22:15:04 +00009017 return std::make_pair(0U, X86::GR32RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00009018 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00009019 case 'f': // FP Stack registers.
9020 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
9021 // value to the correct fpstack register class.
9022 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
9023 return std::make_pair(0U, X86::RFP32RegisterClass);
9024 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
9025 return std::make_pair(0U, X86::RFP64RegisterClass);
9026 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00009027 case 'y': // MMX_REGS if MMX allowed.
9028 if (!Subtarget->hasMMX()) break;
9029 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009030 case 'Y': // SSE_REGS if SSE2 allowed
9031 if (!Subtarget->hasSSE2()) break;
9032 // FALL THROUGH.
9033 case 'x': // SSE_REGS if SSE1 allowed
9034 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00009035
9036 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00009037 default: break;
9038 // Scalar SSE types.
9039 case MVT::f32:
9040 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00009041 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009042 case MVT::f64:
9043 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00009044 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00009045 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00009046 case MVT::v16i8:
9047 case MVT::v8i16:
9048 case MVT::v4i32:
9049 case MVT::v2i64:
9050 case MVT::v4f32:
9051 case MVT::v2f64:
9052 return std::make_pair(0U, X86::VR128RegisterClass);
9053 }
Chris Lattnerad043e82007-04-09 05:11:28 +00009054 break;
9055 }
9056 }
Scott Michelfdc40a02009-02-17 22:15:04 +00009057
Chris Lattnerf76d1802006-07-31 23:26:50 +00009058 // Use the default implementation in TargetLowering to convert the register
9059 // constraint into a member of a register class.
9060 std::pair<unsigned, const TargetRegisterClass*> Res;
9061 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00009062
9063 // Not found as a standard register?
9064 if (Res.second == 0) {
9065 // GCC calls "st(0)" just plain "st".
9066 if (StringsEqualNoCase("{st}", Constraint)) {
9067 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00009068 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00009069 }
Dale Johannesen330169f2008-11-13 21:52:36 +00009070 // 'A' means EAX + EDX.
9071 if (Constraint == "A") {
9072 Res.first = X86::EAX;
Dan Gohman68a31c22009-07-30 17:02:08 +00009073 Res.second = X86::GR32_ADRegisterClass;
Dale Johannesen330169f2008-11-13 21:52:36 +00009074 }
Chris Lattner1a60aa72006-10-31 19:42:44 +00009075 return Res;
9076 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009077
Chris Lattnerf76d1802006-07-31 23:26:50 +00009078 // Otherwise, check to see if this is a register class of the wrong value
9079 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
9080 // turn into {ax},{dx}.
9081 if (Res.second->hasType(VT))
9082 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009083
Chris Lattnerf76d1802006-07-31 23:26:50 +00009084 // All of the single-register GCC register classes map their values onto
9085 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
9086 // really want an 8-bit or 32-bit register, map to the appropriate register
9087 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00009088 if (Res.second == X86::GR16RegisterClass) {
9089 if (VT == MVT::i8) {
9090 unsigned DestReg = 0;
9091 switch (Res.first) {
9092 default: break;
9093 case X86::AX: DestReg = X86::AL; break;
9094 case X86::DX: DestReg = X86::DL; break;
9095 case X86::CX: DestReg = X86::CL; break;
9096 case X86::BX: DestReg = X86::BL; break;
9097 }
9098 if (DestReg) {
9099 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009100 Res.second = X86::GR8RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009101 }
9102 } else if (VT == MVT::i32) {
9103 unsigned DestReg = 0;
9104 switch (Res.first) {
9105 default: break;
9106 case X86::AX: DestReg = X86::EAX; break;
9107 case X86::DX: DestReg = X86::EDX; break;
9108 case X86::CX: DestReg = X86::ECX; break;
9109 case X86::BX: DestReg = X86::EBX; break;
9110 case X86::SI: DestReg = X86::ESI; break;
9111 case X86::DI: DestReg = X86::EDI; break;
9112 case X86::BP: DestReg = X86::EBP; break;
9113 case X86::SP: DestReg = X86::ESP; break;
9114 }
9115 if (DestReg) {
9116 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009117 Res.second = X86::GR32RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009118 }
9119 } else if (VT == MVT::i64) {
9120 unsigned DestReg = 0;
9121 switch (Res.first) {
9122 default: break;
9123 case X86::AX: DestReg = X86::RAX; break;
9124 case X86::DX: DestReg = X86::RDX; break;
9125 case X86::CX: DestReg = X86::RCX; break;
9126 case X86::BX: DestReg = X86::RBX; break;
9127 case X86::SI: DestReg = X86::RSI; break;
9128 case X86::DI: DestReg = X86::RDI; break;
9129 case X86::BP: DestReg = X86::RBP; break;
9130 case X86::SP: DestReg = X86::RSP; break;
9131 }
9132 if (DestReg) {
9133 Res.first = DestReg;
Duncan Sands005e7982009-04-21 09:44:39 +00009134 Res.second = X86::GR64RegisterClass;
Chris Lattner6ba50a92008-08-26 06:19:02 +00009135 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00009136 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00009137 } else if (Res.second == X86::FR32RegisterClass ||
9138 Res.second == X86::FR64RegisterClass ||
9139 Res.second == X86::VR128RegisterClass) {
9140 // Handle references to XMM physical registers that got mapped into the
9141 // wrong class. This can happen with constraints like {xmm0} where the
9142 // target independent register mapper will just pick the first match it can
9143 // find, ignoring the required type.
9144 if (VT == MVT::f32)
9145 Res.second = X86::FR32RegisterClass;
9146 else if (VT == MVT::f64)
9147 Res.second = X86::FR64RegisterClass;
9148 else if (X86::VR128RegisterClass->hasType(VT))
9149 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00009150 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00009151
Chris Lattnerf76d1802006-07-31 23:26:50 +00009152 return Res;
9153}
Mon P Wang0c397192008-10-30 08:01:45 +00009154
9155//===----------------------------------------------------------------------===//
9156// X86 Widen vector type
9157//===----------------------------------------------------------------------===//
9158
9159/// getWidenVectorType: given a vector type, returns the type to widen
9160/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
9161/// If there is no vector type that we want to widen to, returns MVT::Other
Mon P Wangf007a8b2008-11-06 05:31:54 +00009162/// When and where to widen is target dependent based on the cost of
Mon P Wang0c397192008-10-30 08:01:45 +00009163/// scalarizing vs using the wider vector type.
9164
Dan Gohmanc13cf132009-01-15 17:34:08 +00009165MVT X86TargetLowering::getWidenVectorType(MVT VT) const {
Mon P Wang0c397192008-10-30 08:01:45 +00009166 assert(VT.isVector());
9167 if (isTypeLegal(VT))
9168 return VT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009169
Mon P Wang0c397192008-10-30 08:01:45 +00009170 // TODO: In computeRegisterProperty, we can compute the list of legal vector
9171 // type based on element type. This would speed up our search (though
9172 // it may not be worth it since the size of the list is relatively
9173 // small).
9174 MVT EltVT = VT.getVectorElementType();
9175 unsigned NElts = VT.getVectorNumElements();
Scott Michelfdc40a02009-02-17 22:15:04 +00009176
Mon P Wang0c397192008-10-30 08:01:45 +00009177 // On X86, it make sense to widen any vector wider than 1
9178 if (NElts <= 1)
9179 return MVT::Other;
Scott Michelfdc40a02009-02-17 22:15:04 +00009180
9181 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
Mon P Wang0c397192008-10-30 08:01:45 +00009182 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
9183 MVT SVT = (MVT::SimpleValueType)nVT;
Scott Michelfdc40a02009-02-17 22:15:04 +00009184
9185 if (isTypeLegal(SVT) &&
9186 SVT.getVectorElementType() == EltVT &&
Mon P Wang0c397192008-10-30 08:01:45 +00009187 SVT.getVectorNumElements() > NElts)
9188 return SVT;
9189 }
9190 return MVT::Other;
9191}