blob: cdf167078137ebb318de46ae1f9825433982bf8c [file] [log] [blame]
Arnold Schwaighofer92226dd2007-10-12 21:53:12 +00001//===-- X86ISelLowering.cpp - X86 DAG Lowering Implementation -------------===//
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the interfaces that X86 uses to lower LLVM code into a
11// selection DAG.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
Evan Cheng0cc39452006-01-16 21:21:29 +000016#include "X86InstrBuilder.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000017#include "X86ISelLowering.h"
Evan Chenge8bd0a32006-06-06 23:30:24 +000018#include "X86MachineFunctionInfo.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000019#include "X86TargetMachine.h"
20#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000021#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000022#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000023#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000024#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000025#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000026#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000027#include "llvm/ADT/VectorExtras.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000028#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000029#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000030#include "llvm/CodeGen/MachineFunction.h"
31#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000032#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000033#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000034#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000035#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000036#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000037#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000038#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000039#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000040#include "llvm/ADT/StringExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000041using namespace llvm;
42
Evan Cheng10e86422008-04-25 19:11:04 +000043// Forward declarations.
Dan Gohman475871a2008-07-27 21:46:04 +000044static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng10e86422008-04-25 19:11:04 +000045
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000046X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000047 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000048 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000049 X86ScalarSSEf64 = Subtarget->hasSSE2();
50 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000051 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000052
Chris Lattnerd43d00c2008-01-24 08:07:48 +000053 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000054
Anton Korobeynikov2365f512007-07-14 14:06:15 +000055 RegInfo = TM.getRegisterInfo();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +000056 TD = getTargetData();
Anton Korobeynikov2365f512007-07-14 14:06:15 +000057
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000058 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000063 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000065 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000066
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000067 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000068 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000071 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
79
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000081 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
Evan Cheng03294662008-10-14 21:26:46 +000087 setLoadExtAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000088
Chris Lattnerddf89562008-01-17 19:59:44 +000089 // We don't accept any truncstore of integer registers.
90 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
Evan Cheng7f042682008-10-15 02:05:31 +000095 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
97 // SETOEQ and SETUNE require checking two conditions.
98 setCondCodeAction(ISD::SETOEQ, MVT::f32, Expand);
99 setCondCodeAction(ISD::SETOEQ, MVT::f64, Expand);
100 setCondCodeAction(ISD::SETOEQ, MVT::f80, Expand);
101 setCondCodeAction(ISD::SETUNE, MVT::f32, Expand);
102 setCondCodeAction(ISD::SETUNE, MVT::f64, Expand);
103 setCondCodeAction(ISD::SETUNE, MVT::f80, Expand);
Chris Lattnerddf89562008-01-17 19:59:44 +0000104
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000105 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
106 // operation.
107 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
108 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
109 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000110
Evan Cheng25ab6902006-09-08 06:48:29 +0000111 if (Subtarget->is64Bit()) {
112 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +0000113 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000114 } else {
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000115 if (X86ScalarSSEf64) {
116 // We have an impenetrably clever algorithm for ui64->double only.
117 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000118 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
119 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
Dale Johannesen1c15bf52008-10-21 20:50:01 +0000120 } else
Evan Cheng25ab6902006-09-08 06:48:29 +0000121 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
122 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000123
124 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
125 // this operation.
126 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
127 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000128 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000129 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000130 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000131 // f32 and f64 cases are Legal, f80 case is not
132 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
133 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000134 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
135 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
136 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000137
Dale Johannesen73328d12007-09-19 23:55:34 +0000138 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
139 // are Legal, f80 is custom lowered.
140 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
141 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000142
Evan Cheng02568ff2006-01-30 22:13:22 +0000143 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
144 // this operation.
145 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
146 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
147
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000148 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000149 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000150 // f32 and f64 cases are Legal, f80 case is not
151 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000152 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000153 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000154 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155 }
156
157 // Handle FP_TO_UINT by promoting the destination to a larger signed
158 // conversion.
159 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
160 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
161 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
162
Evan Cheng25ab6902006-09-08 06:48:29 +0000163 if (Subtarget->is64Bit()) {
164 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000165 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000166 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000167 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000168 // Expand FP_TO_UINT into a select.
169 // FIXME: We would like to use a Custom expander here eventually to do
170 // the optimal thing for SSE vs. the default expansion in the legalizer.
171 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
172 else
173 // With SSE3 we can use fisttpll to convert to a signed i64.
174 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
175 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000176
Chris Lattner399610a2006-12-05 18:22:22 +0000177 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000178 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000179 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
180 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
181 }
Chris Lattner21f66852005-12-23 05:15:23 +0000182
Dan Gohmanb00ee212008-02-18 19:34:53 +0000183 // Scalar integer divide and remainder are lowered to use operations that
184 // produce two results, to match the available instructions. This exposes
185 // the two-result form to trivial CSE, which is able to combine x/y and x%y
186 // into a single instruction.
187 //
188 // Scalar integer multiply-high is also lowered to use two-result
189 // operations, to match the available instructions. However, plain multiply
190 // (low) operations are left as Legal, as there are single-result
191 // instructions for this in x86. Using the two-result multiply instructions
192 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000193 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
194 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
195 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
196 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
197 setOperationAction(ISD::SREM , MVT::i8 , Expand);
198 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000199 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
200 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
201 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
202 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
203 setOperationAction(ISD::SREM , MVT::i16 , Expand);
204 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000205 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
206 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
207 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
208 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
209 setOperationAction(ISD::SREM , MVT::i32 , Expand);
210 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000211 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
212 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
213 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
214 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
215 setOperationAction(ISD::SREM , MVT::i64 , Expand);
216 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000217
Evan Chengc35497f2006-10-30 08:02:39 +0000218 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000219 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000220 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
221 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000222 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000223 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
224 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
225 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
227 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000228 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000230 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000231 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000232
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000233 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000236 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000237 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
238 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000239 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000240 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
241 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000242 if (Subtarget->is64Bit()) {
243 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000244 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
245 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000246 }
247
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000248 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000249 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000250
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000251 // These should be promoted to a larger select which is supported.
252 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
253 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000254 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000255 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
256 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
257 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
258 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000259 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000260 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
261 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
262 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
263 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
264 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000265 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000266 if (Subtarget->is64Bit()) {
267 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
268 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
269 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000270 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000271 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000272 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000273
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000274 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000275 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000276 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000277 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000278 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000279 if (Subtarget->is64Bit())
280 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000281 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000282 if (Subtarget->is64Bit()) {
283 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
284 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
285 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
Bill Wendling056292f2008-09-16 21:48:12 +0000286 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000287 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000288 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000289 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
290 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
291 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000292 if (Subtarget->is64Bit()) {
293 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
294 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
295 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
296 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000297
Evan Chengd2cde682008-03-10 19:38:10 +0000298 if (Subtarget->hasSSE1())
299 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000300
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000301 if (!Subtarget->hasSSE2())
302 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
303
Mon P Wang63307c32008-05-05 19:05:59 +0000304 // Expand certain atomics
Dale Johannesene00a8a22008-08-28 02:44:49 +0000305 setOperationAction(ISD::ATOMIC_CMP_SWAP_8 , MVT::i8, Custom);
306 setOperationAction(ISD::ATOMIC_CMP_SWAP_16, MVT::i16, Custom);
307 setOperationAction(ISD::ATOMIC_CMP_SWAP_32, MVT::i32, Custom);
308 setOperationAction(ISD::ATOMIC_CMP_SWAP_64, MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000309
Dale Johannesen71d1bf52008-09-29 22:25:26 +0000310 setOperationAction(ISD::ATOMIC_LOAD_SUB_8 , MVT::i8, Custom);
311 setOperationAction(ISD::ATOMIC_LOAD_SUB_16, MVT::i16, Custom);
312 setOperationAction(ISD::ATOMIC_LOAD_SUB_32, MVT::i32, Custom);
313 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000314
Dale Johannesen48c1bc22008-10-02 18:53:47 +0000315 if (!Subtarget->is64Bit()) {
316 setOperationAction(ISD::ATOMIC_LOAD_ADD_64, MVT::i64, Custom);
317 setOperationAction(ISD::ATOMIC_LOAD_SUB_64, MVT::i64, Custom);
318 setOperationAction(ISD::ATOMIC_LOAD_AND_64, MVT::i64, Custom);
319 setOperationAction(ISD::ATOMIC_LOAD_OR_64, MVT::i64, Custom);
320 setOperationAction(ISD::ATOMIC_LOAD_XOR_64, MVT::i64, Custom);
321 setOperationAction(ISD::ATOMIC_LOAD_NAND_64, MVT::i64, Custom);
322 setOperationAction(ISD::ATOMIC_SWAP_64, MVT::i64, Custom);
323 }
324
Dan Gohman7f460202008-06-30 20:59:49 +0000325 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
326 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000327 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000328 if (!Subtarget->isTargetDarwin() &&
329 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000330 !Subtarget->isTargetCygMing()) {
331 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
332 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
333 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000334
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000335 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
336 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
337 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
338 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
339 if (Subtarget->is64Bit()) {
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000340 setExceptionPointerRegister(X86::RAX);
341 setExceptionSelectorRegister(X86::RDX);
342 } else {
343 setExceptionPointerRegister(X86::EAX);
344 setExceptionSelectorRegister(X86::EDX);
345 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000346 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikov260a6b82008-09-08 21:12:11 +0000347 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i64, Custom);
348
Duncan Sandsf7331b32007-09-11 14:10:23 +0000349 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000350
Chris Lattnerda68d302008-01-15 21:58:22 +0000351 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000352
Nate Begemanacc398c2006-01-25 18:21:52 +0000353 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
354 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000355 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000356 if (Subtarget->is64Bit()) {
357 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000358 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000359 } else {
360 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000361 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000362 }
Evan Chengae642192007-03-02 23:16:35 +0000363
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000364 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000365 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000366 if (Subtarget->is64Bit())
367 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000368 if (Subtarget->isTargetCygMing())
369 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
370 else
371 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000372
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000373 if (X86ScalarSSEf64) {
374 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000375 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000376 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
377 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000378
Evan Cheng223547a2006-01-31 22:28:30 +0000379 // Use ANDPD to simulate FABS.
380 setOperationAction(ISD::FABS , MVT::f64, Custom);
381 setOperationAction(ISD::FABS , MVT::f32, Custom);
382
383 // Use XORP to simulate FNEG.
384 setOperationAction(ISD::FNEG , MVT::f64, Custom);
385 setOperationAction(ISD::FNEG , MVT::f32, Custom);
386
Evan Cheng68c47cb2007-01-05 07:55:56 +0000387 // Use ANDPD and ORPD to simulate FCOPYSIGN.
388 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
389 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
390
Evan Chengd25e9e82006-02-02 00:28:23 +0000391 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000392 setOperationAction(ISD::FSIN , MVT::f64, Expand);
393 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000394 setOperationAction(ISD::FSIN , MVT::f32, Expand);
395 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000396
Chris Lattnera54aa942006-01-29 06:26:08 +0000397 // Expand FP immediates into loads from the stack, except for the special
398 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000399 addLegalFPImmediate(APFloat(+0.0)); // xorpd
400 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000401
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000402 // Floating truncations from f80 and extensions to f80 go through memory.
403 // If optimizing, we lie about this though and handle it in
404 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
405 if (Fast) {
406 setConvertAction(MVT::f32, MVT::f80, Expand);
407 setConvertAction(MVT::f64, MVT::f80, Expand);
408 setConvertAction(MVT::f80, MVT::f32, Expand);
409 setConvertAction(MVT::f80, MVT::f64, Expand);
410 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000411 } else if (X86ScalarSSEf32) {
412 // Use SSE for f32, x87 for f64.
413 // Set up the FP register classes.
414 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
415 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
416
417 // Use ANDPS to simulate FABS.
418 setOperationAction(ISD::FABS , MVT::f32, Custom);
419
420 // Use XORP to simulate FNEG.
421 setOperationAction(ISD::FNEG , MVT::f32, Custom);
422
423 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
424
425 // Use ANDPS and ORPS to simulate FCOPYSIGN.
426 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
427 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
428
429 // We don't support sin/cos/fmod
430 setOperationAction(ISD::FSIN , MVT::f32, Expand);
431 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000432
Nate Begemane1795842008-02-14 08:57:00 +0000433 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000434 addLegalFPImmediate(APFloat(+0.0f)); // xorps
435 addLegalFPImmediate(APFloat(+0.0)); // FLD0
436 addLegalFPImmediate(APFloat(+1.0)); // FLD1
437 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
438 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
439
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000440 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
441 // this though and handle it in InstructionSelectPreprocess so that
442 // dagcombine2 can hack on these.
443 if (Fast) {
444 setConvertAction(MVT::f32, MVT::f64, Expand);
445 setConvertAction(MVT::f32, MVT::f80, Expand);
446 setConvertAction(MVT::f80, MVT::f32, Expand);
447 setConvertAction(MVT::f64, MVT::f32, Expand);
448 // And x87->x87 truncations also.
449 setConvertAction(MVT::f80, MVT::f64, Expand);
450 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000451
452 if (!UnsafeFPMath) {
453 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
454 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
455 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000456 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000457 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000458 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000459 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
460 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000461
Evan Cheng68c47cb2007-01-05 07:55:56 +0000462 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000463 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000464 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
465 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000466
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000467 // Floating truncations go through memory. If optimizing, we lie about
468 // this though and handle it in InstructionSelectPreprocess so that
469 // dagcombine2 can hack on these.
470 if (Fast) {
471 setConvertAction(MVT::f80, MVT::f32, Expand);
472 setConvertAction(MVT::f64, MVT::f32, Expand);
473 setConvertAction(MVT::f80, MVT::f64, Expand);
474 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000475
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000476 if (!UnsafeFPMath) {
477 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
478 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
479 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000480 addLegalFPImmediate(APFloat(+0.0)); // FLD0
481 addLegalFPImmediate(APFloat(+1.0)); // FLD1
482 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
483 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000484 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
485 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
486 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
487 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000488 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000489
Dale Johannesen59a58732007-08-05 18:49:15 +0000490 // Long double always uses X87.
491 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000492 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
493 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000494 {
Dale Johannesen23a98552008-10-09 23:00:39 +0000495 bool ignored;
Chris Lattner71d07a02008-01-27 06:19:31 +0000496 APFloat TmpFlt(+0.0);
Dale Johannesen23a98552008-10-09 23:00:39 +0000497 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
498 &ignored);
Chris Lattner71d07a02008-01-27 06:19:31 +0000499 addLegalFPImmediate(TmpFlt); // FLD0
500 TmpFlt.changeSign();
501 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
502 APFloat TmpFlt2(+1.0);
Dale Johannesen23a98552008-10-09 23:00:39 +0000503 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven,
504 &ignored);
Chris Lattner71d07a02008-01-27 06:19:31 +0000505 addLegalFPImmediate(TmpFlt2); // FLD1
506 TmpFlt2.changeSign();
507 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
508 }
509
Dale Johannesen2f429012007-09-26 21:10:55 +0000510 if (!UnsafeFPMath) {
511 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
512 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
513 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000514
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000515 // Always use a library call for pow.
516 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
517 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
518 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
519
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000520 setOperationAction(ISD::FLOG, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000521 setOperationAction(ISD::FLOG2, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000522 setOperationAction(ISD::FLOG10, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000523 setOperationAction(ISD::FEXP, MVT::f80, Expand);
Dale Johannesen7794f2a2008-09-04 00:47:13 +0000524 setOperationAction(ISD::FEXP2, MVT::f80, Expand);
525
Mon P Wang0c397192008-10-30 08:01:45 +0000526 // First set operation action for all vector types to either to promote
527 // (for widening) or expand (for scalarization). Then we will selectively
528 // turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000529 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
530 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000531 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
540 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
541 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
542 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
543 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
Gabor Greif327ef032008-08-28 23:19:51 +0000544 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
545 setOperationAction(ISD::EXTRACT_VECTOR_ELT,(MVT::SimpleValueType)VT,Expand);
546 setOperationAction(ISD::INSERT_VECTOR_ELT,(MVT::SimpleValueType)VT, Expand);
Mon P Wang0c397192008-10-30 08:01:45 +0000547 setOperationAction(ISD::EXTRACT_SUBVECTOR,(MVT::SimpleValueType)VT, Expand);
548 setOperationAction(ISD::CONCAT_VECTORS,(MVT::SimpleValueType)VT, Expand);
Duncan Sands83ec4b62008-06-06 12:08:01 +0000549 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
550 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
551 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
552 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
553 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
554 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
555 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
556 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
557 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
558 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
559 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
560 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
561 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
562 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
563 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
564 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
565 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
566 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
567 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
568 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
569 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
570 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Dale Johannesenfb0e1322008-09-10 17:31:40 +0000571 setOperationAction(ISD::FLOG, (MVT::SimpleValueType)VT, Expand);
572 setOperationAction(ISD::FLOG2, (MVT::SimpleValueType)VT, Expand);
573 setOperationAction(ISD::FLOG10, (MVT::SimpleValueType)VT, Expand);
574 setOperationAction(ISD::FEXP, (MVT::SimpleValueType)VT, Expand);
575 setOperationAction(ISD::FEXP2, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000576 }
577
Evan Chenga88973f2006-03-22 19:22:18 +0000578 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000579 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
580 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
581 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000582 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000583 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000584
Evan Chengd30bf012006-03-01 01:11:20 +0000585 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000586
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000587 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
588 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
589 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000590 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000591
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000592 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
593 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
594 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000595 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000596
Bill Wendling74027e92007-03-15 21:24:36 +0000597 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
598 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
599
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000600 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000601 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000602 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000603 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
604 setOperationAction(ISD::AND, MVT::v2i32, Promote);
605 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
606 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000607
608 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000609 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000610 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000611 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
612 setOperationAction(ISD::OR, MVT::v2i32, Promote);
613 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
614 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000615
616 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000617 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000618 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000619 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
620 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
621 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
622 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000623
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000624 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000625 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000626 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000627 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
628 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
629 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000630 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
631 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000632 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000633
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000634 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
635 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
636 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000637 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000638 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000639
640 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
641 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
642 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000643 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000644
Evan Cheng52672b82008-07-22 18:39:19 +0000645 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000646 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
647 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000648 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000649
650 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000651 }
652
Evan Chenga88973f2006-03-22 19:22:18 +0000653 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000654 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
655
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000656 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
657 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
658 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
659 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000660 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
661 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000662 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
663 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
664 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000665 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000666 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000667 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000668 }
669
Evan Chenga88973f2006-03-22 19:22:18 +0000670 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000671 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
672 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
673 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
674 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
675 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
676
Evan Chengf7c378e2006-04-10 07:23:14 +0000677 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
678 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
679 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000680 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000681 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
682 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
683 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000684 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000685 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000686 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
687 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
688 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
689 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000690 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
691 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000692
Nate Begeman30a0de92008-07-17 16:51:19 +0000693 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
694 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
695 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
696 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000697
Evan Chengf7c378e2006-04-10 07:23:14 +0000698 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
699 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000700 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000701 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000702 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000703
Evan Cheng2c3ae372006-04-12 21:21:57 +0000704 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000705 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
706 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000707 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000708 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000709 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000710 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
711 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
712 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000713 }
714 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
715 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
716 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
717 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000718 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000719 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000720 if (Subtarget->is64Bit()) {
721 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000722 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000723 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000724
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000725 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000726 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000727 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
728 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
729 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
730 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
731 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
732 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
733 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
734 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
735 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
736 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000737 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000738
Chris Lattnerddf89562008-01-17 19:59:44 +0000739 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000740
Evan Cheng2c3ae372006-04-12 21:21:57 +0000741 // Custom lower v2i64 and v2f64 selects.
742 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000743 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000744 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000745 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000746
Evan Cheng470a6ad2006-02-22 02:26:30 +0000747 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000748
749 if (Subtarget->hasSSE41()) {
750 // FIXME: Do we need to handle scalar-to-vector here?
751 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohman0b924dc2008-05-23 17:49:40 +0000752 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000753
754 // i8 and i16 vectors are custom , because the source register and source
755 // source memory operand types are not the same width. f32 vectors are
756 // custom since the immediate controlling the insert encodes additional
757 // information.
758 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
759 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
760 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
761 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
762
763 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
764 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
765 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng62a3f152008-03-24 21:52:23 +0000766 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000767
768 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000769 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
770 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000771 }
772 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000773
Nate Begeman30a0de92008-07-17 16:51:19 +0000774 if (Subtarget->hasSSE42()) {
775 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
776 }
777
Evan Cheng6be2c582006-04-05 23:38:46 +0000778 // We want to custom lower some of our intrinsics.
779 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
780
Evan Cheng206ee9d2006-07-07 08:33:52 +0000781 // We have target-specific dag combine patterns for the following nodes:
782 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000783 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000784 setTargetDAGCombine(ISD::SELECT);
Chris Lattner149a4e52008-02-22 02:09:43 +0000785 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000786
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000787 computeRegisterProperties();
788
Evan Cheng87ed7162006-02-14 08:25:08 +0000789 // FIXME: These should be based on subtarget info. Plus, the values should
790 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000791 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
792 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
793 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000794 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000795 setPrefLoopAlignment(16);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000796}
797
Scott Michel5b8f82e2008-03-10 15:42:14 +0000798
Dan Gohman475871a2008-07-27 21:46:04 +0000799MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000800 return MVT::i8;
801}
802
803
Evan Cheng29286502008-01-23 23:17:41 +0000804/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
805/// the desired ByVal argument alignment.
806static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
807 if (MaxAlign == 16)
808 return;
809 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
810 if (VTy->getBitWidth() == 128)
811 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000812 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
813 unsigned EltAlign = 0;
814 getMaxByValAlign(ATy->getElementType(), EltAlign);
815 if (EltAlign > MaxAlign)
816 MaxAlign = EltAlign;
817 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
818 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
819 unsigned EltAlign = 0;
820 getMaxByValAlign(STy->getElementType(i), EltAlign);
821 if (EltAlign > MaxAlign)
822 MaxAlign = EltAlign;
823 if (MaxAlign == 16)
824 break;
825 }
826 }
827 return;
828}
829
830/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
831/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000832/// that contain SSE vectors are placed at 16-byte boundaries while the rest
833/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000834unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000835 if (Subtarget->is64Bit()) {
836 // Max of 8 and alignment of type.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +0000837 unsigned TyAlign = TD->getABITypeAlignment(Ty);
Evan Cheng1887c1c2008-08-21 21:00:15 +0000838 if (TyAlign > 8)
839 return TyAlign;
840 return 8;
841 }
842
Evan Cheng29286502008-01-23 23:17:41 +0000843 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000844 if (Subtarget->hasSSE1())
845 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000846 return Align;
847}
Chris Lattner2b02a442007-02-25 08:29:00 +0000848
Evan Chengf0df0312008-05-15 08:39:06 +0000849/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000850/// and store operations as a result of memset, memcpy, and memmove
851/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000852/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000853MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000854X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
855 bool isSrcConst, bool isSrcStr) const {
Chris Lattner4002a1b2008-10-28 05:49:35 +0000856 // FIXME: This turns off use of xmm stores for memset/memcpy on targets like
857 // linux. This is because the stack realignment code can't handle certain
858 // cases like PR2962. This should be removed when PR2962 is fixed.
859 if (Subtarget->getStackAlignment() >= 16) {
860 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
861 return MVT::v4i32;
862 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
863 return MVT::v4f32;
864 }
Evan Chengf0df0312008-05-15 08:39:06 +0000865 if (Subtarget->is64Bit() && Size >= 8)
866 return MVT::i64;
867 return MVT::i32;
868}
869
870
Evan Chengcc415862007-11-09 01:32:10 +0000871/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
872/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000873SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000874 SelectionDAG &DAG) const {
875 if (usesGlobalOffsetTable())
876 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
877 if (!Subtarget->isPICStyleRIPRel())
878 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
879 return Table;
880}
881
Chris Lattner2b02a442007-02-25 08:29:00 +0000882//===----------------------------------------------------------------------===//
883// Return Value Calling Convention Implementation
884//===----------------------------------------------------------------------===//
885
Chris Lattner59ed56b2007-02-28 04:55:35 +0000886#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000887
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000888/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000889SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000890 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
891
Chris Lattner9774c912007-02-27 05:28:59 +0000892 SmallVector<CCValAssign, 16> RVLocs;
893 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000894 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
895 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Gabor Greifba36cb52008-08-28 21:40:38 +0000896 CCInfo.AnalyzeReturn(Op.getNode(), RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000897
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000898 // If this is the first return lowered for this function, add the regs to the
899 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000900 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000901 for (unsigned i = 0; i != RVLocs.size(); ++i)
902 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000903 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000904 }
Dan Gohman475871a2008-07-27 21:46:04 +0000905 SDValue Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000906
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000907 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000908 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000909 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000910 SDValue TailCall = Chain;
911 SDValue TargetAddress = TailCall.getOperand(1);
912 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000913 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighofer290ae032008-09-22 14:50:07 +0000914 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::EAX ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000915 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
Bill Wendling056292f2008-09-16 21:48:12 +0000916 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000917 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
918 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000919 assert(StackAdjustment.getOpcode() == ISD::Constant &&
920 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000921
Dan Gohman475871a2008-07-27 21:46:04 +0000922 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000923 Operands.push_back(Chain.getOperand(0));
924 Operands.push_back(TargetAddress);
925 Operands.push_back(StackAdjustment);
926 // Copy registers used by the call. Last operand is a flag so it is not
927 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000928 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000929 Operands.push_back(Chain.getOperand(i));
930 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000931 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
932 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000933 }
934
935 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000936 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000937
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000939 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
940 // Operand #1 = Bytes To Pop
941 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
942
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000943 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000944 for (unsigned i = 0; i != RVLocs.size(); ++i) {
945 CCValAssign &VA = RVLocs[i];
946 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000947 SDValue ValToCopy = Op.getOperand(i*2+1);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000948
Chris Lattner447ff682008-03-11 03:23:40 +0000949 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
950 // the RET instruction and handled by the FP Stackifier.
951 if (RVLocs[i].getLocReg() == X86::ST0 ||
952 RVLocs[i].getLocReg() == X86::ST1) {
953 // If this is a copy from an xmm register to ST(0), use an FPExtend to
954 // change the value to the FP stack register class.
955 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
956 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
957 RetOps.push_back(ValToCopy);
958 // Don't emit a copytoreg.
959 continue;
960 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000961
Chris Lattner8e6da152008-03-10 21:08:41 +0000962 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000963 Flag = Chain.getValue(1);
964 }
Dan Gohman61a92132008-04-21 23:59:07 +0000965
966 // The x86-64 ABI for returning structs by value requires that we copy
967 // the sret argument into %rax for the return. We saved the argument into
968 // a virtual register in the entry block, so now we copy the value out
969 // and into %rax.
970 if (Subtarget->is64Bit() &&
971 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
972 MachineFunction &MF = DAG.getMachineFunction();
973 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
974 unsigned Reg = FuncInfo->getSRetReturnReg();
975 if (!Reg) {
976 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
977 FuncInfo->setSRetReturnReg(Reg);
978 }
Dan Gohman475871a2008-07-27 21:46:04 +0000979 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +0000980
981 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
982 Flag = Chain.getValue(1);
983 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000984
Chris Lattner447ff682008-03-11 03:23:40 +0000985 RetOps[0] = Chain; // Update chain.
986
987 // Add the flag if we have it.
Gabor Greifba36cb52008-08-28 21:40:38 +0000988 if (Flag.getNode())
Chris Lattner447ff682008-03-11 03:23:40 +0000989 RetOps.push_back(Flag);
990
991 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000992}
993
994
Chris Lattner3085e152007-02-25 08:59:22 +0000995/// LowerCallResult - Lower the result values of an ISD::CALL into the
996/// appropriate copies out of appropriate physical registers. This assumes that
997/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
998/// being lowered. The returns a SDNode with the same number of values as the
999/// ISD::CALL.
1000SDNode *X86TargetLowering::
Dan Gohman095cc292008-09-13 01:54:27 +00001001LowerCallResult(SDValue Chain, SDValue InFlag, CallSDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +00001002 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +00001003
1004 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +00001005 SmallVector<CCValAssign, 16> RVLocs;
Dan Gohman095cc292008-09-13 01:54:27 +00001006 bool isVarArg = TheCall->isVarArg();
Chris Lattner52387be2007-06-19 00:13:10 +00001007 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +00001008 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
1009
Dan Gohman475871a2008-07-27 21:46:04 +00001010 SmallVector<SDValue, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +00001011
1012 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +00001013 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001014 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattner8e6da152008-03-10 21:08:41 +00001015
1016 // If this is a call to a function that returns an fp value on the floating
1017 // point stack, but where we prefer to use the value in xmm registers, copy
1018 // it out as F80 and use a truncate to move it from fp stack reg to xmm reg.
Mon P Wang7e665102008-08-21 19:54:16 +00001019 if ((RVLocs[i].getLocReg() == X86::ST0 ||
1020 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattner8e6da152008-03-10 21:08:41 +00001021 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
1022 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +00001023 }
Chris Lattner3085e152007-02-25 08:59:22 +00001024
Chris Lattner8e6da152008-03-10 21:08:41 +00001025 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
1026 CopyVT, InFlag).getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00001027 SDValue Val = Chain.getValue(0);
Chris Lattner8e6da152008-03-10 21:08:41 +00001028 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +00001029
Chris Lattner8e6da152008-03-10 21:08:41 +00001030 if (CopyVT != RVLocs[i].getValVT()) {
1031 // Round the F80 the right size, which also moves to the appropriate xmm
1032 // register.
1033 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
1034 // This truncation won't change the value.
1035 DAG.getIntPtrConstant(1));
1036 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +00001037
Chris Lattner8e6da152008-03-10 21:08:41 +00001038 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +00001039 }
Duncan Sands4bdcb612008-07-02 17:40:58 +00001040
Chris Lattner3085e152007-02-25 08:59:22 +00001041 // Merge everything together with a MERGE_VALUES node.
1042 ResultVals.push_back(Chain);
Duncan Sandsf9516202008-06-30 10:19:09 +00001043 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
Gabor Greifba36cb52008-08-28 21:40:38 +00001044 ResultVals.size()).getNode();
Chris Lattner2b02a442007-02-25 08:29:00 +00001045}
1046
1047
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001048//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001049// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001050//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001051// StdCall calling convention seems to be standard for many Windows' API
1052// routines and around. It differs from C calling convention just a little:
1053// callee should clean up the stack, not caller. Symbols should be also
1054// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001055// For info on fast calling convention see Fast Calling Convention (tail call)
1056// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001057
Evan Cheng85e38002006-04-27 05:35:28 +00001058/// AddLiveIn - This helper function adds the specified physical register to the
1059/// MachineFunction as a live in value. It also creates a corresponding virtual
1060/// register for it.
1061static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001062 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +00001063 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +00001064 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1065 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +00001066 return VReg;
1067}
1068
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001069/// CallIsStructReturn - Determines whether a CALL node uses struct return
1070/// semantics.
Dan Gohman095cc292008-09-13 01:54:27 +00001071static bool CallIsStructReturn(CallSDNode *TheCall) {
1072 unsigned NumOps = TheCall->getNumArgs();
Gordon Henriksen86737662008-01-05 16:56:59 +00001073 if (!NumOps)
1074 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001075
Dan Gohman095cc292008-09-13 01:54:27 +00001076 return TheCall->getArgFlags(0).isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001077}
1078
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001079/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1080/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001081static bool ArgsAreStructReturn(SDValue Op) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001082 unsigned NumArgs = Op.getNode()->getNumValues() - 1;
Gordon Henriksen86737662008-01-05 16:56:59 +00001083 if (!NumArgs)
1084 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001085
1086 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001087}
1088
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001089/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1090/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001091/// calls.
Dan Gohman095cc292008-09-13 01:54:27 +00001092bool X86TargetLowering::IsCalleePop(bool IsVarArg, unsigned CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001093 if (IsVarArg)
1094 return false;
1095
Dan Gohman095cc292008-09-13 01:54:27 +00001096 switch (CallingConv) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001097 default:
1098 return false;
1099 case CallingConv::X86_StdCall:
1100 return !Subtarget->is64Bit();
1101 case CallingConv::X86_FastCall:
1102 return !Subtarget->is64Bit();
1103 case CallingConv::Fast:
1104 return PerformTailCallOpt;
1105 }
1106}
1107
Dan Gohman095cc292008-09-13 01:54:27 +00001108/// CCAssignFnForNode - Selects the correct CCAssignFn for a the
1109/// given CallingConvention value.
1110CCAssignFn *X86TargetLowering::CCAssignFnForNode(unsigned CC) const {
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001111 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001112 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001113 return CC_X86_Win64_C;
Evan Chenge9ac9e62008-09-07 09:07:23 +00001114 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1115 return CC_X86_64_TailCall;
1116 else
1117 return CC_X86_64_C;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001118 }
1119
Gordon Henriksen86737662008-01-05 16:56:59 +00001120 if (CC == CallingConv::X86_FastCall)
1121 return CC_X86_32_FastCall;
Evan Chengb188dd92008-09-10 18:25:29 +00001122 else if (CC == CallingConv::Fast)
1123 return CC_X86_32_FastCC;
Gordon Henriksen86737662008-01-05 16:56:59 +00001124 else
1125 return CC_X86_32_C;
1126}
1127
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001128/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1129/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001130NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001131X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001132 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001133 if (CC == CallingConv::X86_FastCall)
1134 return FastCall;
1135 else if (CC == CallingConv::X86_StdCall)
1136 return StdCall;
1137 return None;
1138}
1139
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001140
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001141/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1142/// in a register before calling.
1143bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1144 return !IsTailCall && !Is64Bit &&
1145 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1146 Subtarget->isPICStyleGOT();
1147}
1148
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001149/// CallRequiresFnAddressInReg - Check whether the call requires the function
1150/// address to be loaded in a register.
1151bool
1152X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1153 return !Is64Bit && IsTailCall &&
1154 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1155 Subtarget->isPICStyleGOT();
1156}
1157
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001158/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1159/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001160/// the specific parameter attribute. The copy will be passed as a byval
1161/// function parameter.
Dan Gohman475871a2008-07-27 21:46:04 +00001162static SDValue
1163CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001164 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001165 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohman707e0182008-04-12 04:36:06 +00001166 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001167 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001168}
1169
Dan Gohman475871a2008-07-27 21:46:04 +00001170SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001171 const CCValAssign &VA,
1172 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001173 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001174 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001175 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001176 ISD::ArgFlagsTy Flags =
1177 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001178 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001179 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001180
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001181 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1182 // changed with more analysis.
1183 // In case of tail call optimization mark all arguments mutable. Since they
1184 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001185 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001186 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001187 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001188 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001189 return FIN;
Dan Gohman69de1932008-02-06 22:27:42 +00001190 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001191 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001192}
1193
Dan Gohman475871a2008-07-27 21:46:04 +00001194SDValue
1195X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001196 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001197 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1198
1199 const Function* Fn = MF.getFunction();
1200 if (Fn->hasExternalLinkage() &&
1201 Subtarget->isTargetCygMing() &&
1202 Fn->getName() == "main")
1203 FuncInfo->setForceFramePointer(true);
1204
1205 // Decorate the function name.
1206 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1207
Evan Cheng1bc78042006-04-26 01:20:17 +00001208 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001209 SDValue Root = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001210 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getZExtValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001211 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001212 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001213 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001214
1215 assert(!(isVarArg && CC == CallingConv::Fast) &&
1216 "Var args not supported with calling convention fastcc");
1217
Chris Lattner638402b2007-02-28 07:00:42 +00001218 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001219 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001220 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001221 CCInfo.AnalyzeFormalArguments(Op.getNode(), CCAssignFnForNode(CC));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001222
Dan Gohman475871a2008-07-27 21:46:04 +00001223 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001224 unsigned LastVal = ~0U;
1225 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1226 CCValAssign &VA = ArgLocs[i];
1227 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1228 // places.
1229 assert(VA.getValNo() != LastVal &&
1230 "Don't support value assigned to multiple locs yet");
1231 LastVal = VA.getValNo();
1232
1233 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001234 MVT RegVT = VA.getLocVT();
Chris Lattnerf39f7712007-02-28 05:46:49 +00001235 TargetRegisterClass *RC;
1236 if (RegVT == MVT::i32)
1237 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001238 else if (Is64Bit && RegVT == MVT::i64)
1239 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001240 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001241 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001242 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001243 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001244 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001245 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001246 else if (RegVT.isVector()) {
1247 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001248 if (!Is64Bit)
1249 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1250 else {
1251 // Darwin calling convention passes MMX values in either GPRs or
1252 // XMMs in x86-64. Other targets pass them in memory.
1253 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1254 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1255 RegVT = MVT::v2i64;
1256 } else {
1257 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1258 RegVT = MVT::i64;
1259 }
1260 }
1261 } else {
1262 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001263 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001264
Chris Lattner82932a52007-03-02 05:12:29 +00001265 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman475871a2008-07-27 21:46:04 +00001266 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001267
1268 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1269 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1270 // right size.
1271 if (VA.getLocInfo() == CCValAssign::SExt)
1272 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1273 DAG.getValueType(VA.getValVT()));
1274 else if (VA.getLocInfo() == CCValAssign::ZExt)
1275 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1276 DAG.getValueType(VA.getValVT()));
1277
1278 if (VA.getLocInfo() != CCValAssign::Full)
1279 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1280
Gordon Henriksen86737662008-01-05 16:56:59 +00001281 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001282 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001283 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Cheng44c0fd12008-04-25 20:13:28 +00001284 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1285 else if (RC == X86::VR128RegisterClass) {
1286 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1287 DAG.getConstant(0, MVT::i64));
1288 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1289 }
1290 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001291
Chris Lattnerf39f7712007-02-28 05:46:49 +00001292 ArgValues.push_back(ArgValue);
1293 } else {
1294 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001295 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001296 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001297 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001298
Dan Gohman61a92132008-04-21 23:59:07 +00001299 // The x86-64 ABI for returning structs by value requires that we copy
1300 // the sret argument into %rax for the return. Save the argument into
1301 // a virtual register so that we can access it from the return points.
1302 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1303 MachineFunction &MF = DAG.getMachineFunction();
1304 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1305 unsigned Reg = FuncInfo->getSRetReturnReg();
1306 if (!Reg) {
1307 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1308 FuncInfo->setSRetReturnReg(Reg);
1309 }
Dan Gohman475871a2008-07-27 21:46:04 +00001310 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohman61a92132008-04-21 23:59:07 +00001311 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1312 }
1313
Chris Lattnerf39f7712007-02-28 05:46:49 +00001314 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001315 // align stack specially for tail calls
Evan Chenge9ac9e62008-09-07 09:07:23 +00001316 if (PerformTailCallOpt && CC == CallingConv::Fast)
Gordon Henriksenae636f82008-01-03 16:47:34 +00001317 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001318
Evan Cheng1bc78042006-04-26 01:20:17 +00001319 // If the function takes variable number of arguments, make a frame index for
1320 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001321 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001322 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1323 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1324 }
1325 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001326 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1327
1328 // FIXME: We should really autogenerate these arrays
1329 static const unsigned GPR64ArgRegsWin64[] = {
1330 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001331 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001332 static const unsigned XMMArgRegsWin64[] = {
1333 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1334 };
1335 static const unsigned GPR64ArgRegs64Bit[] = {
1336 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1337 };
1338 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001339 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1340 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1341 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001342 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1343
1344 if (IsWin64) {
1345 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1346 GPR64ArgRegs = GPR64ArgRegsWin64;
1347 XMMArgRegs = XMMArgRegsWin64;
1348 } else {
1349 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1350 GPR64ArgRegs = GPR64ArgRegs64Bit;
1351 XMMArgRegs = XMMArgRegs64Bit;
1352 }
1353 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1354 TotalNumIntRegs);
1355 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1356 TotalNumXMMRegs);
1357
Gordon Henriksen86737662008-01-05 16:56:59 +00001358 // For X86-64, if there are vararg parameters that are passed via
1359 // registers, then we must store them to their spots on the stack so they
1360 // may be loaded by deferencing the result of va_next.
1361 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001362 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1363 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1364 TotalNumXMMRegs * 16, 16);
1365
Gordon Henriksen86737662008-01-05 16:56:59 +00001366 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001367 SmallVector<SDValue, 8> MemOps;
1368 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1369 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001370 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001371 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001372 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1373 X86::GR64RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001374 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1375 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001376 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001377 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001378 MemOps.push_back(Store);
1379 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001380 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001381 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001382
Gordon Henriksen86737662008-01-05 16:56:59 +00001383 // Now store the XMM (fp + vector) parameter registers.
1384 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001385 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001386 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001387 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1388 X86::VR128RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001389 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1390 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001391 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001392 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001393 MemOps.push_back(Store);
1394 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001395 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001396 }
1397 if (!MemOps.empty())
1398 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1399 &MemOps[0], MemOps.size());
1400 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001401 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001402
Gordon Henriksenae636f82008-01-03 16:47:34 +00001403 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001404
Gordon Henriksen86737662008-01-05 16:56:59 +00001405 // Some CCs need callee pop.
Dan Gohman095cc292008-09-13 01:54:27 +00001406 if (IsCalleePop(isVarArg, CC)) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001407 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001408 BytesCallerReserves = 0;
1409 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001410 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001411 // If this is an sret function, the return should pop the hidden pointer.
Evan Chengb188dd92008-09-10 18:25:29 +00001412 if (!Is64Bit && CC != CallingConv::Fast && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001413 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001414 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001415 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001416
Gordon Henriksen86737662008-01-05 16:56:59 +00001417 if (!Is64Bit) {
1418 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1419 if (CC == CallingConv::X86_FastCall)
1420 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1421 }
Evan Cheng25caf632006-05-23 21:06:34 +00001422
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001423 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001424
Evan Cheng25caf632006-05-23 21:06:34 +00001425 // Return the new list of results.
Gabor Greifba36cb52008-08-28 21:40:38 +00001426 return DAG.getMergeValues(Op.getNode()->getVTList(), &ArgValues[0],
Gabor Greif99a6cb92008-08-26 22:36:50 +00001427 ArgValues.size()).getValue(Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001428}
1429
Dan Gohman475871a2008-07-27 21:46:04 +00001430SDValue
Dan Gohman095cc292008-09-13 01:54:27 +00001431X86TargetLowering::LowerMemOpCallTo(CallSDNode *TheCall, SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001432 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001433 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001434 SDValue Chain,
Dan Gohman095cc292008-09-13 01:54:27 +00001435 SDValue Arg, ISD::ArgFlagsTy Flags) {
Dan Gohman4fdad172008-02-07 16:28:05 +00001436 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001437 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001438 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001439 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001440 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001441 }
Dan Gohman4fdad172008-02-07 16:28:05 +00001442 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001443 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001444}
1445
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001446/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1447/// optimization is performed and it is required.
Dan Gohman475871a2008-07-27 21:46:04 +00001448SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001449X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001450 SDValue &OutRetAddr,
1451 SDValue Chain,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001452 bool IsTailCall,
1453 bool Is64Bit,
1454 int FPDiff) {
1455 if (!IsTailCall || FPDiff==0) return Chain;
1456
1457 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001458 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001459 OutRetAddr = getReturnAddressFrameIndex(DAG);
1460 // Load the "old" Return address.
1461 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Gabor Greifba36cb52008-08-28 21:40:38 +00001462 return SDValue(OutRetAddr.getNode(), 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001463}
1464
1465/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1466/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman475871a2008-07-27 21:46:04 +00001467static SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001468EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001469 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001470 bool Is64Bit, int FPDiff) {
1471 // Store the return address to the appropriate stack slot.
1472 if (!FPDiff) return Chain;
1473 // Calculate the new stack slot for the return address.
1474 int SlotSize = Is64Bit ? 8 : 4;
1475 int NewReturnAddrFI =
1476 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001477 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001478 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001479 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001480 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001481 return Chain;
1482}
1483
Dan Gohman475871a2008-07-27 21:46:04 +00001484SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001485 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman095cc292008-09-13 01:54:27 +00001486 CallSDNode *TheCall = cast<CallSDNode>(Op.getNode());
1487 SDValue Chain = TheCall->getChain();
1488 unsigned CC = TheCall->getCallingConv();
1489 bool isVarArg = TheCall->isVarArg();
1490 bool IsTailCall = TheCall->isTailCall() &&
1491 CC == CallingConv::Fast && PerformTailCallOpt;
1492 SDValue Callee = TheCall->getCallee();
Gordon Henriksen86737662008-01-05 16:56:59 +00001493 bool Is64Bit = Subtarget->is64Bit();
Dan Gohman095cc292008-09-13 01:54:27 +00001494 bool IsStructRet = CallIsStructReturn(TheCall);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001495
1496 assert(!(isVarArg && CC == CallingConv::Fast) &&
1497 "Var args not supported with calling convention fastcc");
1498
Chris Lattner638402b2007-02-28 07:00:42 +00001499 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001500 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001501 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Dan Gohman095cc292008-09-13 01:54:27 +00001502 CCInfo.AnalyzeCallOperands(TheCall, CCAssignFnForNode(CC));
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001503
Chris Lattner423c5f42007-02-28 05:31:48 +00001504 // Get a count of how many bytes are to be pushed on the stack.
1505 unsigned NumBytes = CCInfo.getNextStackOffset();
Arnold Schwaighofer1fdc40f2008-09-11 20:28:43 +00001506 if (PerformTailCallOpt && CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001507 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001508
Gordon Henriksen86737662008-01-05 16:56:59 +00001509 int FPDiff = 0;
1510 if (IsTailCall) {
1511 // Lower arguments at fp - stackoffset + fpdiff.
1512 unsigned NumBytesCallerPushed =
1513 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1514 FPDiff = NumBytesCallerPushed - NumBytes;
1515
1516 // Set the delta of movement of the returnaddr stackslot.
1517 // But only set if delta is greater than previous delta.
1518 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1519 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1520 }
1521
Chris Lattnere563bbc2008-10-11 22:08:30 +00001522 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes, true));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001523
Dan Gohman475871a2008-07-27 21:46:04 +00001524 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001525 // Load return adress for tail calls.
1526 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1527 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001528
Dan Gohman475871a2008-07-27 21:46:04 +00001529 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1530 SmallVector<SDValue, 8> MemOpChains;
1531 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001532
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001533 // Walk the register/memloc assignments, inserting copies/loads. In the case
1534 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001535 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1536 CCValAssign &VA = ArgLocs[i];
Dan Gohman095cc292008-09-13 01:54:27 +00001537 SDValue Arg = TheCall->getArg(i);
1538 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
1539 bool isByVal = Flags.isByVal();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001540
Chris Lattner423c5f42007-02-28 05:31:48 +00001541 // Promote the value if needed.
1542 switch (VA.getLocInfo()) {
1543 default: assert(0 && "Unknown loc info!");
1544 case CCValAssign::Full: break;
1545 case CCValAssign::SExt:
1546 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1547 break;
1548 case CCValAssign::ZExt:
1549 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1550 break;
1551 case CCValAssign::AExt:
1552 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1553 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001554 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001555
1556 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001557 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001558 MVT RegVT = VA.getLocVT();
1559 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001560 switch (VA.getLocReg()) {
1561 default:
1562 break;
1563 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1564 case X86::R8: {
1565 // Special case: passing MMX values in GPR registers.
1566 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1567 break;
1568 }
1569 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1570 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1571 // Special case: passing MMX values in XMM registers.
1572 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1573 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1574 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1575 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1576 getMOVLMask(2, DAG));
1577 break;
1578 }
1579 }
1580 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001581 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1582 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001583 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001584 assert(VA.isMemLoc());
Gabor Greifba36cb52008-08-28 21:40:38 +00001585 if (StackPtr.getNode() == 0)
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001586 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1587
Dan Gohman095cc292008-09-13 01:54:27 +00001588 MemOpChains.push_back(LowerMemOpCallTo(TheCall, DAG, StackPtr, VA,
1589 Chain, Arg, Flags));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001590 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001591 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001592 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001593
Evan Cheng32fe1032006-05-25 00:59:30 +00001594 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001595 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1596 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001597
Evan Cheng347d5f72006-04-28 21:29:37 +00001598 // Build a sequence of copy-to-reg nodes chained together with token chain
1599 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001600 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001601 // Tail call byval lowering might overwrite argument registers so in case of
1602 // tail call optimization the copies to registers are lowered later.
1603 if (!IsTailCall)
1604 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1605 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1606 InFlag);
1607 InFlag = Chain.getValue(1);
1608 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001609
Evan Chengf4684712007-02-21 21:18:14 +00001610 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001611 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001612 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1613 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1614 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1615 InFlag);
1616 InFlag = Chain.getValue(1);
1617 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001618 // If we are tail calling and generating PIC/GOT style code load the address
1619 // of the callee into ecx. The value in ecx is used as target of the tail
1620 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1621 // calls on PIC/GOT architectures. Normally we would just put the address of
1622 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1623 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001624 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001625 // Note: The actual moving to ecx is done further down.
1626 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
Evan Chengda43bcf2008-09-24 00:05:32 +00001627 if (G && !G->getGlobal()->hasHiddenVisibility() &&
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001628 !G->getGlobal()->hasProtectedVisibility())
1629 Callee = LowerGlobalAddress(Callee, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00001630 else if (isa<ExternalSymbolSDNode>(Callee))
1631 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001632 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001633
Gordon Henriksen86737662008-01-05 16:56:59 +00001634 if (Is64Bit && isVarArg) {
1635 // From AMD64 ABI document:
1636 // For calls that may call functions that use varargs or stdargs
1637 // (prototype-less calls or calls to functions containing ellipsis (...) in
1638 // the declaration) %al is used as hidden argument to specify the number
1639 // of SSE registers used. The contents of %al do not need to match exactly
1640 // the number of registers, but must be an ubound on the number of SSE
1641 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001642
1643 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001644 // Count the number of XMM registers allocated.
1645 static const unsigned XMMArgRegs[] = {
1646 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1647 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1648 };
1649 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1650
1651 Chain = DAG.getCopyToReg(Chain, X86::AL,
1652 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1653 InFlag = Chain.getValue(1);
1654 }
1655
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001656
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001657 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001658 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001659 SmallVector<SDValue, 8> MemOpChains2;
1660 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001661 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001662 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001663 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001664 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1665 CCValAssign &VA = ArgLocs[i];
1666 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001667 assert(VA.isMemLoc());
Dan Gohman095cc292008-09-13 01:54:27 +00001668 SDValue Arg = TheCall->getArg(i);
1669 ISD::ArgFlagsTy Flags = TheCall->getArgFlags(i);
Gordon Henriksen86737662008-01-05 16:56:59 +00001670 // Create frame index.
1671 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001672 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001673 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001674 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001675
Duncan Sands276dcbd2008-03-21 09:14:45 +00001676 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001677 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001678 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Gabor Greifba36cb52008-08-28 21:40:38 +00001679 if (StackPtr.getNode() == 0)
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001680 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1681 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1682
1683 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng8e5712b2008-01-12 01:08:07 +00001684 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001685 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001686 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001687 MemOpChains2.push_back(
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001688 DAG.getStore(Chain, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001689 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001690 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001691 }
1692 }
1693
1694 if (!MemOpChains2.empty())
1695 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001696 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001697
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001698 // Copy arguments to their registers.
1699 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1700 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1701 InFlag);
1702 InFlag = Chain.getValue(1);
1703 }
Dan Gohman475871a2008-07-27 21:46:04 +00001704 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001705
Gordon Henriksen86737662008-01-05 16:56:59 +00001706 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001707 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1708 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001709 }
1710
Evan Cheng32fe1032006-05-25 00:59:30 +00001711 // If the callee is a GlobalAddress node (quite common, every direct call is)
1712 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001713 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001714 // We should use extra load for direct calls to dllimported functions in
1715 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001716 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1717 getTargetMachine(), true))
Dan Gohman6520e202008-10-18 02:06:02 +00001718 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy(),
1719 G->getOffset());
Bill Wendling056292f2008-09-16 21:48:12 +00001720 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
1721 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 } else if (IsTailCall) {
Arnold Schwaighofer290ae032008-09-22 14:50:07 +00001723 unsigned Opc = Is64Bit ? X86::R9 : X86::EAX;
Gordon Henriksen86737662008-01-05 16:56:59 +00001724
1725 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001726 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001727 Callee,InFlag);
1728 Callee = DAG.getRegister(Opc, getPointerTy());
1729 // Add register as live out.
1730 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001731 }
1732
Chris Lattnerd96d0722007-02-25 06:40:16 +00001733 // Returns a chain & a flag for retval copy to use.
1734 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001735 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001736
1737 if (IsTailCall) {
1738 Ops.push_back(Chain);
Chris Lattnere563bbc2008-10-11 22:08:30 +00001739 Ops.push_back(DAG.getIntPtrConstant(NumBytes, true));
1740 Ops.push_back(DAG.getIntPtrConstant(0, true));
Gabor Greifba36cb52008-08-28 21:40:38 +00001741 if (InFlag.getNode())
Gordon Henriksen86737662008-01-05 16:56:59 +00001742 Ops.push_back(InFlag);
1743 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1744 InFlag = Chain.getValue(1);
1745
1746 // Returns a chain & a flag for retval copy to use.
1747 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1748 Ops.clear();
1749 }
1750
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001751 Ops.push_back(Chain);
1752 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001753
Gordon Henriksen86737662008-01-05 16:56:59 +00001754 if (IsTailCall)
1755 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001756
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 // Add argument registers to the end of the list so that they are known live
1758 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001759 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1760 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1761 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001762
Evan Cheng586ccac2008-03-18 23:36:35 +00001763 // Add an implicit use GOT pointer in EBX.
1764 if (!IsTailCall && !Is64Bit &&
1765 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1766 Subtarget->isPICStyleGOT())
1767 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1768
1769 // Add an implicit use of AL for x86 vararg functions.
1770 if (Is64Bit && isVarArg)
1771 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1772
Gabor Greifba36cb52008-08-28 21:40:38 +00001773 if (InFlag.getNode())
Evan Cheng347d5f72006-04-28 21:29:37 +00001774 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001775
Gordon Henriksen86737662008-01-05 16:56:59 +00001776 if (IsTailCall) {
Gabor Greifba36cb52008-08-28 21:40:38 +00001777 assert(InFlag.getNode() &&
Gordon Henriksen86737662008-01-05 16:56:59 +00001778 "Flag must be set. Depend on flag being set in LowerRET");
1779 Chain = DAG.getNode(X86ISD::TAILCALL,
Dan Gohman095cc292008-09-13 01:54:27 +00001780 TheCall->getVTList(), &Ops[0], Ops.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001781
Gabor Greifba36cb52008-08-28 21:40:38 +00001782 return SDValue(Chain.getNode(), Op.getResNo());
Gordon Henriksen86737662008-01-05 16:56:59 +00001783 }
1784
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001785 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001786 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001787
Chris Lattner2d297092006-05-23 18:50:38 +00001788 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001789 unsigned NumBytesForCalleeToPush;
Dan Gohman095cc292008-09-13 01:54:27 +00001790 if (IsCalleePop(isVarArg, CC))
Gordon Henriksen86737662008-01-05 16:56:59 +00001791 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Chengb188dd92008-09-10 18:25:29 +00001792 else if (!Is64Bit && CC != CallingConv::Fast && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001793 // If this is is a call to a struct-return function, the callee
1794 // pops the hidden struct pointer, so we have to push it back.
1795 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001796 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001797 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001798 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001799
Gordon Henriksenae636f82008-01-03 16:47:34 +00001800 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001801 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00001802 DAG.getIntPtrConstant(NumBytes, true),
1803 DAG.getIntPtrConstant(NumBytesForCalleeToPush,
1804 true),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001805 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001806 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001807
Chris Lattner3085e152007-02-25 08:59:22 +00001808 // Handle result values, copying them out of physregs into vregs that we
1809 // return.
Dan Gohman095cc292008-09-13 01:54:27 +00001810 return SDValue(LowerCallResult(Chain, InFlag, TheCall, CC, DAG),
Gabor Greif327ef032008-08-28 23:19:51 +00001811 Op.getResNo());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001812}
1813
Evan Cheng25ab6902006-09-08 06:48:29 +00001814
1815//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001816// Fast Calling Convention (tail call) implementation
1817//===----------------------------------------------------------------------===//
1818
1819// Like std call, callee cleans arguments, convention except that ECX is
1820// reserved for storing the tail called function address. Only 2 registers are
1821// free for argument passing (inreg). Tail call optimization is performed
1822// provided:
1823// * tailcallopt is enabled
1824// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001825// On X86_64 architecture with GOT-style position independent code only local
1826// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001827// To keep the stack aligned according to platform abi the function
1828// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1829// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001830// If a tail called function callee has more arguments than the caller the
1831// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001832// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001833// original REtADDR, but before the saved framepointer or the spilled registers
1834// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1835// stack layout:
1836// arg1
1837// arg2
1838// RETADDR
1839// [ new RETADDR
1840// move area ]
1841// (possible EBP)
1842// ESI
1843// EDI
1844// local1 ..
1845
1846/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1847/// for a 16 byte align requirement.
1848unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1849 SelectionDAG& DAG) {
Evan Chenge9ac9e62008-09-07 09:07:23 +00001850 MachineFunction &MF = DAG.getMachineFunction();
1851 const TargetMachine &TM = MF.getTarget();
1852 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1853 unsigned StackAlignment = TFI.getStackAlignment();
1854 uint64_t AlignMask = StackAlignment - 1;
1855 int64_t Offset = StackSize;
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001856 uint64_t SlotSize = TD->getPointerSize();
Evan Chenge9ac9e62008-09-07 09:07:23 +00001857 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1858 // Number smaller than 12 so just add the difference.
1859 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1860 } else {
1861 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1862 Offset = ((~AlignMask) & Offset) + StackAlignment +
1863 (StackAlignment-SlotSize);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001864 }
Evan Chenge9ac9e62008-09-07 09:07:23 +00001865 return Offset;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001866}
1867
1868/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001869/// following the call is a return. A function is eligible if caller/callee
1870/// calling conventions match, currently only fastcc supports tail calls, and
1871/// the function CALL is immediatly followed by a RET.
Dan Gohman095cc292008-09-13 01:54:27 +00001872bool X86TargetLowering::IsEligibleForTailCallOptimization(CallSDNode *TheCall,
Dan Gohman475871a2008-07-27 21:46:04 +00001873 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001874 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001875 if (!PerformTailCallOpt)
1876 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001877
Dan Gohman095cc292008-09-13 01:54:27 +00001878 if (CheckTailCallReturnConstraints(TheCall, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001879 MachineFunction &MF = DAG.getMachineFunction();
1880 unsigned CallerCC = MF.getFunction()->getCallingConv();
Dan Gohman095cc292008-09-13 01:54:27 +00001881 unsigned CalleeCC= TheCall->getCallingConv();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001882 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman095cc292008-09-13 01:54:27 +00001883 SDValue Callee = TheCall->getCallee();
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001884 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001885 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001886 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001887 return true;
1888
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001889 // Can only do local tail calls (in same module, hidden or protected) on
1890 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001891 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1892 return G->getGlobal()->hasHiddenVisibility()
1893 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001894 }
1895 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001896
1897 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001898}
1899
Dan Gohman3df24e62008-09-03 23:12:08 +00001900FastISel *
1901X86TargetLowering::createFastISel(MachineFunction &mf,
Dan Gohmand57dd5f2008-09-23 21:53:34 +00001902 MachineModuleInfo *mmo,
Dan Gohman3df24e62008-09-03 23:12:08 +00001903 DenseMap<const Value *, unsigned> &vm,
1904 DenseMap<const BasicBlock *,
Dan Gohman0586d912008-09-10 20:11:02 +00001905 MachineBasicBlock *> &bm,
Dan Gohmandd5b58a2008-10-14 23:54:11 +00001906 DenseMap<const AllocaInst *, int> &am
1907#ifndef NDEBUG
1908 , SmallSet<Instruction*, 8> &cil
1909#endif
1910 ) {
1911 return X86::createFastISel(mf, mmo, vm, bm, am
1912#ifndef NDEBUG
1913 , cil
1914#endif
1915 );
Dan Gohmand9f3c482008-08-19 21:32:53 +00001916}
1917
1918
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001919//===----------------------------------------------------------------------===//
1920// Other Lowering Hooks
1921//===----------------------------------------------------------------------===//
1922
1923
Dan Gohman475871a2008-07-27 21:46:04 +00001924SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001925 MachineFunction &MF = DAG.getMachineFunction();
1926 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1927 int ReturnAddrIndex = FuncInfo->getRAIndex();
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001928 uint64_t SlotSize = TD->getPointerSize();
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001929
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001930 if (ReturnAddrIndex == 0) {
1931 // Set up a frame object for the return address.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00001932 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(SlotSize, -SlotSize);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001933 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001934 }
1935
Evan Cheng25ab6902006-09-08 06:48:29 +00001936 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001937}
1938
1939
Evan Cheng6dfa9992006-01-30 23:41:35 +00001940/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1941/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001942/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1943/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001944static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman475871a2008-07-27 21:46:04 +00001945 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Chris Lattnerf9570512006-09-13 03:22:10 +00001946 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001947 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001948 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001949 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1950 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1951 // X > -1 -> X == 0, jump !sign.
1952 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001953 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001954 return true;
1955 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1956 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001957 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001958 return true;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00001959 } else if (SetCCOpcode == ISD::SETLT && RHSC->getZExtValue() == 1) {
Dan Gohman5f6913c2007-09-17 14:49:27 +00001960 // X < 1 -> X <= 0
1961 RHS = DAG.getConstant(0, RHS.getValueType());
1962 X86CC = X86::COND_LE;
1963 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001964 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001965 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001966
Evan Chengd9558e02006-01-06 00:43:03 +00001967 switch (SetCCOpcode) {
1968 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001969 case ISD::SETEQ: X86CC = X86::COND_E; break;
1970 case ISD::SETGT: X86CC = X86::COND_G; break;
1971 case ISD::SETGE: X86CC = X86::COND_GE; break;
1972 case ISD::SETLT: X86CC = X86::COND_L; break;
1973 case ISD::SETLE: X86CC = X86::COND_LE; break;
1974 case ISD::SETNE: X86CC = X86::COND_NE; break;
1975 case ISD::SETULT: X86CC = X86::COND_B; break;
1976 case ISD::SETUGT: X86CC = X86::COND_A; break;
1977 case ISD::SETULE: X86CC = X86::COND_BE; break;
1978 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001979 }
1980 } else {
Duncan Sands4047f4a2008-10-24 13:03:10 +00001981 // First determine if it is required or is profitable to flip the operands.
1982
1983 // If LHS is a foldable load, but RHS is not, flip the condition.
1984 if ((ISD::isNON_EXTLoad(LHS.getNode()) && LHS.hasOneUse()) &&
1985 !(ISD::isNON_EXTLoad(RHS.getNode()) && RHS.hasOneUse())) {
1986 SetCCOpcode = getSetCCSwappedOperands(SetCCOpcode);
1987 std::swap(LHS, RHS);
1988 }
1989
Evan Cheng45670442008-08-29 23:22:12 +00001990 switch (SetCCOpcode) {
1991 default: break;
1992 case ISD::SETOLT:
1993 case ISD::SETOLE:
1994 case ISD::SETUGT:
1995 case ISD::SETUGE:
Duncan Sands4047f4a2008-10-24 13:03:10 +00001996 std::swap(LHS, RHS);
Evan Cheng45670442008-08-29 23:22:12 +00001997 break;
1998 }
1999
Evan Chengd9558e02006-01-06 00:43:03 +00002000 // On a floating point condition, the flags are set as follows:
2001 // ZF PF CF op
2002 // 0 | 0 | 0 | X > Y
2003 // 0 | 0 | 1 | X < Y
2004 // 1 | 0 | 0 | X == Y
2005 // 1 | 1 | 1 | unordered
2006 switch (SetCCOpcode) {
2007 default: break;
2008 case ISD::SETUEQ:
Evan Cheng45670442008-08-29 23:22:12 +00002009 case ISD::SETEQ:
2010 X86CC = X86::COND_E;
2011 break;
2012 case ISD::SETOLT: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00002013 case ISD::SETOGT:
Evan Cheng45670442008-08-29 23:22:12 +00002014 case ISD::SETGT:
2015 X86CC = X86::COND_A;
2016 break;
2017 case ISD::SETOLE: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00002018 case ISD::SETOGE:
Evan Cheng45670442008-08-29 23:22:12 +00002019 case ISD::SETGE:
2020 X86CC = X86::COND_AE;
2021 break;
2022 case ISD::SETUGT: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00002023 case ISD::SETULT:
Evan Cheng45670442008-08-29 23:22:12 +00002024 case ISD::SETLT:
2025 X86CC = X86::COND_B;
2026 break;
2027 case ISD::SETUGE: // flipped
Evan Chengd9558e02006-01-06 00:43:03 +00002028 case ISD::SETULE:
Evan Cheng45670442008-08-29 23:22:12 +00002029 case ISD::SETLE:
2030 X86CC = X86::COND_BE;
2031 break;
Evan Chengd9558e02006-01-06 00:43:03 +00002032 case ISD::SETONE:
Evan Cheng45670442008-08-29 23:22:12 +00002033 case ISD::SETNE:
2034 X86CC = X86::COND_NE;
2035 break;
2036 case ISD::SETUO:
2037 X86CC = X86::COND_P;
2038 break;
2039 case ISD::SETO:
2040 X86CC = X86::COND_NP;
2041 break;
Evan Chengd9558e02006-01-06 00:43:03 +00002042 }
Evan Cheng4d46d0a2008-08-28 23:48:31 +00002043 }
2044
Evan Cheng94a50da2008-08-29 22:13:21 +00002045 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00002046}
2047
Evan Cheng4a460802006-01-11 00:33:36 +00002048/// hasFPCMov - is there a floating point cmov for the specific X86 condition
2049/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00002050/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00002051static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00002052 switch (X86CC) {
2053 default:
2054 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00002055 case X86::COND_B:
2056 case X86::COND_BE:
2057 case X86::COND_E:
2058 case X86::COND_P:
2059 case X86::COND_A:
2060 case X86::COND_AE:
2061 case X86::COND_NE:
2062 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002063 return true;
2064 }
2065}
2066
Evan Cheng5ced1d82006-04-06 23:23:56 +00002067/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002068/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman475871a2008-07-27 21:46:04 +00002069static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002070 if (Op.getOpcode() == ISD::UNDEF)
2071 return true;
2072
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002073 unsigned Val = cast<ConstantSDNode>(Op)->getZExtValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002074 return (Val >= Low && Val < Hi);
2075}
2076
2077/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2078/// true if Op is undef or if its value equal to the specified value.
Dan Gohman475871a2008-07-27 21:46:04 +00002079static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Evan Chengc5cdff22006-04-07 21:53:05 +00002080 if (Op.getOpcode() == ISD::UNDEF)
2081 return true;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002082 return cast<ConstantSDNode>(Op)->getZExtValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002083}
2084
Evan Cheng0188ecb2006-03-22 18:59:22 +00002085/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2086/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2087bool X86::isPSHUFDMask(SDNode *N) {
2088 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2089
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002090 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002091 return false;
2092
2093 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002094 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002095 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002096 if (Arg.getOpcode() == ISD::UNDEF) continue;
2097 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002098 if (cast<ConstantSDNode>(Arg)->getZExtValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002099 return false;
2100 }
2101
2102 return true;
2103}
2104
2105/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002106/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002107bool X86::isPSHUFHWMask(SDNode *N) {
2108 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2109
2110 if (N->getNumOperands() != 8)
2111 return false;
2112
2113 // Lower quadword copied in order.
2114 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002115 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002116 if (Arg.getOpcode() == ISD::UNDEF) continue;
2117 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002118 if (cast<ConstantSDNode>(Arg)->getZExtValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002119 return false;
2120 }
2121
2122 // Upper quadword shuffled.
2123 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002124 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002125 if (Arg.getOpcode() == ISD::UNDEF) continue;
2126 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002127 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002128 if (Val < 4 || Val > 7)
2129 return false;
2130 }
2131
2132 return true;
2133}
2134
2135/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002136/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002137bool X86::isPSHUFLWMask(SDNode *N) {
2138 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2139
2140 if (N->getNumOperands() != 8)
2141 return false;
2142
2143 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002144 for (unsigned i = 4; i != 8; ++i)
2145 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002146 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002147
2148 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002149 for (unsigned i = 0; i != 4; ++i)
2150 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002151 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002152
2153 return true;
2154}
2155
Evan Cheng14aed5e2006-03-24 01:18:28 +00002156/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2157/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002158static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002159 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002160
Evan Cheng39623da2006-04-20 08:58:49 +00002161 unsigned Half = NumElems / 2;
2162 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002163 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002164 return false;
2165 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002166 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002167 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002168
2169 return true;
2170}
2171
Evan Cheng39623da2006-04-20 08:58:49 +00002172bool X86::isSHUFPMask(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002174 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002175}
2176
Evan Cheng213d2cf2007-05-17 18:45:50 +00002177/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002178/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2179/// half elements to come from vector 1 (which would equal the dest.) and
2180/// the upper half to come from vector 2.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002181static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002182 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002183
Chris Lattner5a88b832007-02-25 07:10:00 +00002184 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002185 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002186 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002187 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002188 for (unsigned i = Half; i < NumOps; ++i)
2189 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002190 return false;
2191 return true;
2192}
2193
2194static bool isCommutedSHUFP(SDNode *N) {
2195 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002196 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002197}
2198
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002199/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2200/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2201bool X86::isMOVHLPSMask(SDNode *N) {
2202 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2203
Evan Cheng2064a2b2006-03-28 06:50:32 +00002204 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002205 return false;
2206
Evan Cheng2064a2b2006-03-28 06:50:32 +00002207 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002208 return isUndefOrEqual(N->getOperand(0), 6) &&
2209 isUndefOrEqual(N->getOperand(1), 7) &&
2210 isUndefOrEqual(N->getOperand(2), 2) &&
2211 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002212}
2213
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002214/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2215/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2216/// <2, 3, 2, 3>
2217bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2218 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2219
2220 if (N->getNumOperands() != 4)
2221 return false;
2222
2223 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2224 return isUndefOrEqual(N->getOperand(0), 2) &&
2225 isUndefOrEqual(N->getOperand(1), 3) &&
2226 isUndefOrEqual(N->getOperand(2), 2) &&
2227 isUndefOrEqual(N->getOperand(3), 3);
2228}
2229
Evan Cheng5ced1d82006-04-06 23:23:56 +00002230/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2231/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2232bool X86::isMOVLPMask(SDNode *N) {
2233 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2234
2235 unsigned NumElems = N->getNumOperands();
2236 if (NumElems != 2 && NumElems != 4)
2237 return false;
2238
Evan Chengc5cdff22006-04-07 21:53:05 +00002239 for (unsigned i = 0; i < NumElems/2; ++i)
2240 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2241 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002242
Evan Chengc5cdff22006-04-07 21:53:05 +00002243 for (unsigned i = NumElems/2; i < NumElems; ++i)
2244 if (!isUndefOrEqual(N->getOperand(i), i))
2245 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002246
2247 return true;
2248}
2249
2250/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002251/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2252/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002253bool X86::isMOVHPMask(SDNode *N) {
2254 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2255
2256 unsigned NumElems = N->getNumOperands();
2257 if (NumElems != 2 && NumElems != 4)
2258 return false;
2259
Evan Chengc5cdff22006-04-07 21:53:05 +00002260 for (unsigned i = 0; i < NumElems/2; ++i)
2261 if (!isUndefOrEqual(N->getOperand(i), i))
2262 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002263
2264 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002265 SDValue Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002266 if (!isUndefOrEqual(Arg, i + NumElems))
2267 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002268 }
2269
2270 return true;
2271}
2272
Evan Cheng0038e592006-03-28 00:39:58 +00002273/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2274/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002275bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002276 bool V2IsSplat = false) {
2277 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002278 return false;
2279
Chris Lattner5a88b832007-02-25 07:10:00 +00002280 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002281 SDValue BitI = Elts[i];
2282 SDValue BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002283 if (!isUndefOrEqual(BitI, j))
2284 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002285 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002286 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002287 return false;
2288 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002289 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002290 return false;
2291 }
Evan Cheng0038e592006-03-28 00:39:58 +00002292 }
2293
2294 return true;
2295}
2296
Evan Cheng39623da2006-04-20 08:58:49 +00002297bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002299 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002300}
2301
Evan Cheng4fcb9222006-03-28 02:43:26 +00002302/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2303/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002304bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002305 bool V2IsSplat = false) {
2306 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002307 return false;
2308
Chris Lattner5a88b832007-02-25 07:10:00 +00002309 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002310 SDValue BitI = Elts[i];
2311 SDValue BitI1 = Elts[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002312 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002313 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002314 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002315 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002316 return false;
2317 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002318 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002319 return false;
2320 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002321 }
2322
2323 return true;
2324}
2325
Evan Cheng39623da2006-04-20 08:58:49 +00002326bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2327 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002328 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002329}
2330
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002331/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2332/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2333/// <0, 0, 1, 1>
2334bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2335 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2336
2337 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002338 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002339 return false;
2340
2341 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002342 SDValue BitI = N->getOperand(i);
2343 SDValue BitI1 = N->getOperand(i+1);
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002344
Evan Chengc5cdff22006-04-07 21:53:05 +00002345 if (!isUndefOrEqual(BitI, j))
2346 return false;
2347 if (!isUndefOrEqual(BitI1, j))
2348 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002349 }
2350
2351 return true;
2352}
2353
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002354/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2355/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2356/// <2, 2, 3, 3>
2357bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2358 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2359
2360 unsigned NumElems = N->getNumOperands();
2361 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2362 return false;
2363
2364 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002365 SDValue BitI = N->getOperand(i);
2366 SDValue BitI1 = N->getOperand(i + 1);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002367
2368 if (!isUndefOrEqual(BitI, j))
2369 return false;
2370 if (!isUndefOrEqual(BitI1, j))
2371 return false;
2372 }
2373
2374 return true;
2375}
2376
Evan Cheng017dcc62006-04-21 01:05:10 +00002377/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2378/// specifies a shuffle of elements that is suitable for input to MOVSS,
2379/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002380static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002381 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002382 return false;
2383
Chris Lattner5a88b832007-02-25 07:10:00 +00002384 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002385 return false;
2386
Chris Lattner5a88b832007-02-25 07:10:00 +00002387 for (unsigned i = 1; i < NumElts; ++i) {
2388 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002389 return false;
2390 }
2391
2392 return true;
2393}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002394
Evan Cheng017dcc62006-04-21 01:05:10 +00002395bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002396 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002397 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002398}
2399
Evan Cheng017dcc62006-04-21 01:05:10 +00002400/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2401/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002402/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002403static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002404 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002405 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002406 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002407 return false;
2408
2409 if (!isUndefOrEqual(Ops[0], 0))
2410 return false;
2411
Chris Lattner5a88b832007-02-25 07:10:00 +00002412 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002413 SDValue Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002414 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2415 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2416 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002417 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002418 }
2419
2420 return true;
2421}
2422
Evan Cheng8cf723d2006-09-08 01:50:06 +00002423static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2424 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002425 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002426 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2427 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002428}
2429
Evan Chengd9539472006-04-14 21:59:03 +00002430/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2431/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2432bool X86::isMOVSHDUPMask(SDNode *N) {
2433 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2434
2435 if (N->getNumOperands() != 4)
2436 return false;
2437
2438 // Expect 1, 1, 3, 3
2439 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002440 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002441 if (Arg.getOpcode() == ISD::UNDEF) continue;
2442 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002443 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002444 if (Val != 1) return false;
2445 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002446
2447 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002448 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002449 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002450 if (Arg.getOpcode() == ISD::UNDEF) continue;
2451 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002452 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002453 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002454 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002455 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002456
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002457 // Don't use movshdup if it can be done with a shufps.
2458 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002459}
2460
2461/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2462/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2463bool X86::isMOVSLDUPMask(SDNode *N) {
2464 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2465
2466 if (N->getNumOperands() != 4)
2467 return false;
2468
2469 // Expect 0, 0, 2, 2
2470 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002471 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002472 if (Arg.getOpcode() == ISD::UNDEF) continue;
2473 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002474 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002475 if (Val != 0) return false;
2476 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002477
2478 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002479 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002480 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002481 if (Arg.getOpcode() == ISD::UNDEF) continue;
2482 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002483 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengd9539472006-04-14 21:59:03 +00002484 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002485 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002486 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002487
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002488 // Don't use movshdup if it can be done with a shufps.
2489 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002490}
2491
Evan Cheng49892af2007-06-19 00:02:56 +00002492/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2493/// specifies a identity operation on the LHS or RHS.
2494static bool isIdentityMask(SDNode *N, bool RHS = false) {
2495 unsigned NumElems = N->getNumOperands();
2496 for (unsigned i = 0; i < NumElems; ++i)
2497 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2498 return false;
2499 return true;
2500}
2501
Evan Chengb9df0ca2006-03-22 02:53:00 +00002502/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2503/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002504static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002505 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2506
Evan Chengb9df0ca2006-03-22 02:53:00 +00002507 // This is a splat operation if each element of the permute is the same, and
2508 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002509 unsigned NumElems = N->getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002510 SDValue ElementBase;
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002511 unsigned i = 0;
2512 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002513 SDValue Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002514 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002515 ElementBase = Elt;
2516 break;
2517 }
2518 }
2519
Gabor Greifba36cb52008-08-28 21:40:38 +00002520 if (!ElementBase.getNode())
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002521 return false;
2522
2523 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002524 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002525 if (Arg.getOpcode() == ISD::UNDEF) continue;
2526 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002527 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002528 }
2529
2530 // Make sure it is a splat of the first vector operand.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002531 return cast<ConstantSDNode>(ElementBase)->getZExtValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002532}
2533
Evan Chengc575ca22006-04-17 20:43:08 +00002534/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2535/// a splat of a single element and it's a 2 or 4 element mask.
2536bool X86::isSplatMask(SDNode *N) {
2537 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2538
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002539 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002540 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2541 return false;
2542 return ::isSplatMask(N);
2543}
2544
Evan Chengf686d9b2006-10-27 21:08:32 +00002545/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2546/// specifies a splat of zero element.
2547bool X86::isSplatLoMask(SDNode *N) {
2548 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2549
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002550 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002551 if (!isUndefOrEqual(N->getOperand(i), 0))
2552 return false;
2553 return true;
2554}
2555
Evan Cheng0b457f02008-09-25 20:50:48 +00002556/// isMOVDDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2557/// specifies a shuffle of elements that is suitable for input to MOVDDUP.
2558bool X86::isMOVDDUPMask(SDNode *N) {
2559 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2560
2561 unsigned e = N->getNumOperands() / 2;
2562 for (unsigned i = 0; i < e; ++i)
2563 if (!isUndefOrEqual(N->getOperand(i), i))
2564 return false;
2565 for (unsigned i = 0; i < e; ++i)
2566 if (!isUndefOrEqual(N->getOperand(e+i), i))
2567 return false;
2568 return true;
2569}
2570
Evan Cheng63d33002006-03-22 08:01:21 +00002571/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2572/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2573/// instructions.
2574unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002575 unsigned NumOperands = N->getNumOperands();
2576 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2577 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002578 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002579 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002580 SDValue Arg = N->getOperand(NumOperands-i-1);
Evan Chengef698ca2006-03-31 00:30:29 +00002581 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002582 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002583 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002584 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002585 if (i != NumOperands - 1)
2586 Mask <<= Shift;
2587 }
Evan Cheng63d33002006-03-22 08:01:21 +00002588
2589 return Mask;
2590}
2591
Evan Cheng506d3df2006-03-29 23:07:14 +00002592/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2593/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2594/// instructions.
2595unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2596 unsigned Mask = 0;
2597 // 8 nodes, but we only care about the last 4.
2598 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002599 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002600 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002601 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002602 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002603 Mask |= (Val - 4);
2604 if (i != 4)
2605 Mask <<= 2;
2606 }
2607
2608 return Mask;
2609}
2610
2611/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2612/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2613/// instructions.
2614unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2615 unsigned Mask = 0;
2616 // 8 nodes, but we only care about the first 4.
2617 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002618 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002619 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002620 if (Arg.getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002621 Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002622 Mask |= Val;
2623 if (i != 0)
2624 Mask <<= 2;
2625 }
2626
2627 return Mask;
2628}
2629
Evan Chengc21a0532006-04-05 01:47:37 +00002630/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2631/// specifies a 8 element shuffle that can be broken into a pair of
2632/// PSHUFHW and PSHUFLW.
2633static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2634 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2635
2636 if (N->getNumOperands() != 8)
2637 return false;
2638
2639 // Lower quadword shuffled.
2640 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002641 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002642 if (Arg.getOpcode() == ISD::UNDEF) continue;
2643 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002644 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002645 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002646 return false;
2647 }
2648
2649 // Upper quadword shuffled.
2650 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002651 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002652 if (Arg.getOpcode() == ISD::UNDEF) continue;
2653 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002654 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Chengc21a0532006-04-05 01:47:37 +00002655 if (Val < 4 || Val > 7)
2656 return false;
2657 }
2658
2659 return true;
2660}
2661
Chris Lattner8a594482007-11-25 00:24:49 +00002662/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002663/// values in ther permute mask.
Dan Gohman475871a2008-07-27 21:46:04 +00002664static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2665 SDValue &V2, SDValue &Mask,
Evan Cheng9eca5e82006-10-25 21:49:50 +00002666 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002667 MVT VT = Op.getValueType();
2668 MVT MaskVT = Mask.getValueType();
2669 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002670 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002671 SmallVector<SDValue, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002672
2673 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002674 SDValue Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002675 if (Arg.getOpcode() == ISD::UNDEF) {
2676 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2677 continue;
2678 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002679 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002680 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002681 if (Val < NumElems)
2682 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2683 else
2684 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2685 }
2686
Evan Cheng9eca5e82006-10-25 21:49:50 +00002687 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002688 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002689 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002690}
2691
Evan Cheng779ccea2007-12-07 21:30:01 +00002692/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2693/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002694static
Dan Gohman475871a2008-07-27 21:46:04 +00002695SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002696 MVT MaskVT = Mask.getValueType();
2697 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002698 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002699 SmallVector<SDValue, 8> MaskVec;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002700 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002701 SDValue Arg = Mask.getOperand(i);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002702 if (Arg.getOpcode() == ISD::UNDEF) {
2703 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2704 continue;
2705 }
2706 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002707 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002708 if (Val < NumElems)
2709 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2710 else
2711 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2712 }
2713 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2714}
2715
2716
Evan Cheng533a0aa2006-04-19 20:35:22 +00002717/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2718/// match movhlps. The lower half elements should come from upper half of
2719/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002720/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002721static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2722 unsigned NumElems = Mask->getNumOperands();
2723 if (NumElems != 4)
2724 return false;
2725 for (unsigned i = 0, e = 2; i != e; ++i)
2726 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2727 return false;
2728 for (unsigned i = 2; i != 4; ++i)
2729 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2730 return false;
2731 return true;
2732}
2733
Evan Cheng5ced1d82006-04-06 23:23:56 +00002734/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002735/// is promoted to a vector. It also returns the LoadSDNode by reference if
2736/// required.
2737static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng0b457f02008-09-25 20:50:48 +00002738 if (N->getOpcode() != ISD::SCALAR_TO_VECTOR)
2739 return false;
2740 N = N->getOperand(0).getNode();
2741 if (!ISD::isNON_EXTLoad(N))
2742 return false;
2743 if (LD)
2744 *LD = cast<LoadSDNode>(N);
2745 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002746}
2747
Evan Cheng533a0aa2006-04-19 20:35:22 +00002748/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2749/// match movlp{s|d}. The lower half elements should come from lower half of
2750/// V1 (and in order), and the upper half elements should come from the upper
2751/// half of V2 (and in order). And since V1 will become the source of the
2752/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002753static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002754 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002755 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002756 // Is V2 is a vector load, don't do this transformation. We will try to use
2757 // load folding shufps op.
2758 if (ISD::isNON_EXTLoad(V2))
2759 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002760
Evan Cheng533a0aa2006-04-19 20:35:22 +00002761 unsigned NumElems = Mask->getNumOperands();
2762 if (NumElems != 2 && NumElems != 4)
2763 return false;
2764 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2765 if (!isUndefOrEqual(Mask->getOperand(i), i))
2766 return false;
2767 for (unsigned i = NumElems/2; i != NumElems; ++i)
2768 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2769 return false;
2770 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002771}
2772
Evan Cheng39623da2006-04-20 08:58:49 +00002773/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2774/// all the same.
2775static bool isSplatVector(SDNode *N) {
2776 if (N->getOpcode() != ISD::BUILD_VECTOR)
2777 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002778
Dan Gohman475871a2008-07-27 21:46:04 +00002779 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002780 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2781 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002782 return false;
2783 return true;
2784}
2785
Evan Cheng8cf723d2006-09-08 01:50:06 +00002786/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2787/// to an undef.
2788static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002789 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002790 return false;
2791
Dan Gohman475871a2008-07-27 21:46:04 +00002792 SDValue V1 = N->getOperand(0);
2793 SDValue V2 = N->getOperand(1);
2794 SDValue Mask = N->getOperand(2);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002795 unsigned NumElems = Mask.getNumOperands();
2796 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002797 SDValue Arg = Mask.getOperand(i);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002798 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002799 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng8cf723d2006-09-08 01:50:06 +00002800 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2801 return false;
2802 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2803 return false;
2804 }
2805 }
2806 return true;
2807}
2808
Evan Cheng213d2cf2007-05-17 18:45:50 +00002809/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2810/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002811static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002812 return ((isa<ConstantSDNode>(Elt) &&
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002813 cast<ConstantSDNode>(Elt)->getZExtValue() == 0) ||
Evan Cheng213d2cf2007-05-17 18:45:50 +00002814 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002815 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002816}
2817
2818/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2819/// to an zero vector.
2820static bool isZeroShuffle(SDNode *N) {
2821 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2822 return false;
2823
Dan Gohman475871a2008-07-27 21:46:04 +00002824 SDValue V1 = N->getOperand(0);
2825 SDValue V2 = N->getOperand(1);
2826 SDValue Mask = N->getOperand(2);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002827 unsigned NumElems = Mask.getNumOperands();
2828 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002829 SDValue Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002830 if (Arg.getOpcode() == ISD::UNDEF)
2831 continue;
2832
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002833 unsigned Idx = cast<ConstantSDNode>(Arg)->getZExtValue();
Chris Lattner8a594482007-11-25 00:24:49 +00002834 if (Idx < NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002835 unsigned Opc = V1.getNode()->getOpcode();
2836 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002837 continue;
2838 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002839 !isZeroNode(V1.getNode()->getOperand(Idx)))
Chris Lattner8a594482007-11-25 00:24:49 +00002840 return false;
2841 } else if (Idx >= NumElems) {
Gabor Greifba36cb52008-08-28 21:40:38 +00002842 unsigned Opc = V2.getNode()->getOpcode();
2843 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00002844 continue;
2845 if (Opc != ISD::BUILD_VECTOR ||
Gabor Greifba36cb52008-08-28 21:40:38 +00002846 !isZeroNode(V2.getNode()->getOperand(Idx - NumElems)))
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///
Dan Gohman475871a2008-07-27 21:46:04 +00002855static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002856 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002857
2858 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2859 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002860 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002861 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002862 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002863 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002864 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002865 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002866 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002867 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002868 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chengf0df0312008-05-15 08:39:06 +00002869 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2870 }
Chris Lattner8a594482007-11-25 00:24:49 +00002871 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002872}
2873
Chris Lattner8a594482007-11-25 00:24:49 +00002874/// getOnesVector - Returns a vector of specified type with all bits set.
2875///
Dan Gohman475871a2008-07-27 21:46:04 +00002876static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002877 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002878
2879 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2880 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002881 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2882 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002883 if (VT.getSizeInBits() == 64) // MMX
Chris Lattner8a594482007-11-25 00:24:49 +00002884 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2885 else // SSE
2886 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2887 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2888}
2889
2890
Evan Cheng39623da2006-04-20 08:58:49 +00002891/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2892/// that point to V2 points to its first element.
Dan Gohman475871a2008-07-27 21:46:04 +00002893static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002894 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2895
2896 bool Changed = false;
Dan Gohman475871a2008-07-27 21:46:04 +00002897 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002898 unsigned NumElems = Mask.getNumOperands();
2899 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002900 SDValue Arg = Mask.getOperand(i);
Evan Cheng39623da2006-04-20 08:58:49 +00002901 if (Arg.getOpcode() != ISD::UNDEF) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00002902 unsigned Val = cast<ConstantSDNode>(Arg)->getZExtValue();
Evan Cheng39623da2006-04-20 08:58:49 +00002903 if (Val > NumElems) {
2904 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2905 Changed = true;
2906 }
2907 }
2908 MaskVec.push_back(Arg);
2909 }
2910
2911 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002912 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2913 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002914 return Mask;
2915}
2916
Evan Cheng017dcc62006-04-21 01:05:10 +00002917/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2918/// operation of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002919static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002920 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2921 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002922
Dan Gohman475871a2008-07-27 21:46:04 +00002923 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002924 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2925 for (unsigned i = 1; i != NumElems; ++i)
2926 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002927 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002928}
2929
Evan Chengc575ca22006-04-17 20:43:08 +00002930/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2931/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002932static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002933 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2934 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002935 SmallVector<SDValue, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002936 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2937 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2938 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2939 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002940 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002941}
2942
Evan Cheng39623da2006-04-20 08:58:49 +00002943/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2944/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002945static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002946 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2947 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002948 unsigned Half = NumElems/2;
Dan Gohman475871a2008-07-27 21:46:04 +00002949 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002950 for (unsigned i = 0; i != Half; ++i) {
2951 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2952 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2953 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002954 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002955}
2956
Chris Lattner62098042008-03-09 01:05:04 +00002957/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2958/// element #0 of a vector with the specified index, leaving the rest of the
2959/// elements in place.
Dan Gohman475871a2008-07-27 21:46:04 +00002960static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner62098042008-03-09 01:05:04 +00002961 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002962 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2963 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002964 SmallVector<SDValue, 8> MaskVec;
Chris Lattner62098042008-03-09 01:05:04 +00002965 // Element #0 of the result gets the elt we are replacing.
2966 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2967 for (unsigned i = 1; i != NumElems; ++i)
2968 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2969 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2970}
2971
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002972/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman475871a2008-07-27 21:46:04 +00002973static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002974 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2975 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002976 if (PVT == VT)
2977 return Op;
Dan Gohman475871a2008-07-27 21:46:04 +00002978 SDValue V1 = Op.getOperand(0);
2979 SDValue Mask = Op.getOperand(2);
Evan Cheng017dcc62006-04-21 01:05:10 +00002980 unsigned NumElems = Mask.getNumOperands();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002981 // Special handling of v4f32 -> v4i32.
2982 if (VT != MVT::v4f32) {
2983 Mask = getUnpacklMask(NumElems, DAG);
2984 while (NumElems > 4) {
2985 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2986 NumElems >>= 1;
2987 }
Evan Chengf0df0312008-05-15 08:39:06 +00002988 Mask = getZeroVector(MVT::v4i32, true, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002989 }
Evan Chengc575ca22006-04-17 20:43:08 +00002990
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002991 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman475871a2008-07-27 21:46:04 +00002992 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002993 DAG.getNode(ISD::UNDEF, PVT), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002994 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2995}
2996
Evan Cheng0b457f02008-09-25 20:50:48 +00002997/// isVectorLoad - Returns true if the node is a vector load, a scalar
2998/// load that's promoted to vector, or a load bitcasted.
2999static bool isVectorLoad(SDValue Op) {
3000 assert(Op.getValueType().isVector() && "Expected a vector type");
3001 if (Op.getOpcode() == ISD::SCALAR_TO_VECTOR ||
3002 Op.getOpcode() == ISD::BIT_CONVERT) {
3003 return isa<LoadSDNode>(Op.getOperand(0));
3004 }
3005 return isa<LoadSDNode>(Op);
3006}
3007
3008
3009/// CanonicalizeMovddup - Cannonicalize movddup shuffle to v2f64.
3010///
3011static SDValue CanonicalizeMovddup(SDValue Op, SDValue V1, SDValue Mask,
3012 SelectionDAG &DAG, bool HasSSE3) {
3013 // If we have sse3 and shuffle has more than one use or input is a load, then
3014 // use movddup. Otherwise, use movlhps.
3015 bool UseMovddup = HasSSE3 && (!Op.hasOneUse() || isVectorLoad(V1));
3016 MVT PVT = UseMovddup ? MVT::v2f64 : MVT::v4f32;
3017 MVT VT = Op.getValueType();
3018 if (VT == PVT)
3019 return Op;
3020 unsigned NumElems = PVT.getVectorNumElements();
3021 if (NumElems == 2) {
3022 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
3023 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
3024 } else {
3025 assert(NumElems == 4);
3026 SDValue Cst0 = DAG.getTargetConstant(0, MVT::i32);
3027 SDValue Cst1 = DAG.getTargetConstant(1, MVT::i32);
3028 Mask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst0, Cst1, Cst0, Cst1);
3029 }
3030
3031 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
3032 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
3033 DAG.getNode(ISD::UNDEF, PVT), Mask);
3034 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
3035}
3036
Evan Chengba05f722006-04-21 23:03:30 +00003037/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00003038/// vector of zero or undef vector. This produces a shuffle where the low
3039/// element of V2 is swizzled into the zero/undef vector, landing at element
3040/// 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 +00003041static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00003042 bool isZero, bool HasSSE2,
3043 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003044 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00003045 SDValue V1 = isZero
Evan Chengf0df0312008-05-15 08:39:06 +00003046 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003047 unsigned NumElems = V2.getValueType().getVectorNumElements();
3048 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3049 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003050 SmallVector<SDValue, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00003051 for (unsigned i = 0; i != NumElems; ++i)
3052 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
3053 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
3054 else
3055 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003056 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003057 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00003058 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00003059}
3060
Evan Chengf26ffe92008-05-29 08:22:04 +00003061/// getNumOfConsecutiveZeros - Return the number of elements in a result of
3062/// a shuffle that is zero.
3063static
Dan Gohman475871a2008-07-27 21:46:04 +00003064unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengf26ffe92008-05-29 08:22:04 +00003065 unsigned NumElems, bool Low,
3066 SelectionDAG &DAG) {
3067 unsigned NumZeros = 0;
3068 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00003069 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman475871a2008-07-27 21:46:04 +00003070 SDValue Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00003071 if (Idx.getOpcode() == ISD::UNDEF) {
3072 ++NumZeros;
3073 continue;
3074 }
Gabor Greifba36cb52008-08-28 21:40:38 +00003075 SDValue Elt = DAG.getShuffleScalarElt(Op.getNode(), Index);
3076 if (Elt.getNode() && isZeroNode(Elt))
Evan Chengf26ffe92008-05-29 08:22:04 +00003077 ++NumZeros;
3078 else
3079 break;
3080 }
3081 return NumZeros;
3082}
3083
3084/// isVectorShift - Returns true if the shuffle can be implemented as a
3085/// logical left or right shift of a vector.
Dan Gohman475871a2008-07-27 21:46:04 +00003086static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
3087 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengf26ffe92008-05-29 08:22:04 +00003088 unsigned NumElems = Mask.getNumOperands();
3089
3090 isLeft = true;
3091 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
3092 if (!NumZeros) {
3093 isLeft = false;
3094 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
3095 if (!NumZeros)
3096 return false;
3097 }
3098
3099 bool SeenV1 = false;
3100 bool SeenV2 = false;
3101 for (unsigned i = NumZeros; i < NumElems; ++i) {
3102 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman475871a2008-07-27 21:46:04 +00003103 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengf26ffe92008-05-29 08:22:04 +00003104 if (Idx.getOpcode() == ISD::UNDEF)
3105 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003106 unsigned Index = cast<ConstantSDNode>(Idx)->getZExtValue();
Evan Chengf26ffe92008-05-29 08:22:04 +00003107 if (Index < NumElems)
3108 SeenV1 = true;
3109 else {
3110 Index -= NumElems;
3111 SeenV2 = true;
3112 }
3113 if (Index != Val)
3114 return false;
3115 }
3116 if (SeenV1 && SeenV2)
3117 return false;
3118
3119 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3120 ShAmt = NumZeros;
3121 return true;
3122}
3123
3124
Evan Chengc78d3b42006-04-24 18:01:45 +00003125/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3126///
Dan Gohman475871a2008-07-27 21:46:04 +00003127static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003128 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003129 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003130 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003131 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003132
Dan Gohman475871a2008-07-27 21:46:04 +00003133 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003134 bool First = true;
3135 for (unsigned i = 0; i < 16; ++i) {
3136 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3137 if (ThisIsNonZero && First) {
3138 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003139 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003140 else
3141 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3142 First = false;
3143 }
3144
3145 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003146 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003147 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3148 if (LastIsNonZero) {
3149 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3150 }
3151 if (ThisIsNonZero) {
3152 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3153 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3154 ThisElt, DAG.getConstant(8, MVT::i8));
3155 if (LastIsNonZero)
3156 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3157 } else
3158 ThisElt = LastElt;
3159
Gabor Greifba36cb52008-08-28 21:40:38 +00003160 if (ThisElt.getNode())
Evan Chengc78d3b42006-04-24 18:01:45 +00003161 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003162 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003163 }
3164 }
3165
3166 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3167}
3168
Bill Wendlinga348c562007-03-22 18:42:45 +00003169/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003170///
Dan Gohman475871a2008-07-27 21:46:04 +00003171static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003172 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003173 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003174 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003175 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003176
Dan Gohman475871a2008-07-27 21:46:04 +00003177 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003178 bool First = true;
3179 for (unsigned i = 0; i < 8; ++i) {
3180 bool isNonZero = (NonZeros & (1 << i)) != 0;
3181 if (isNonZero) {
3182 if (First) {
3183 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003184 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003185 else
3186 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3187 First = false;
3188 }
3189 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003190 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003191 }
3192 }
3193
3194 return V;
3195}
3196
Evan Chengf26ffe92008-05-29 08:22:04 +00003197/// getVShift - Return a vector logical shift node.
3198///
Dan Gohman475871a2008-07-27 21:46:04 +00003199static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003200 unsigned NumBits, SelectionDAG &DAG,
3201 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003202 bool isMMX = VT.getSizeInBits() == 64;
3203 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003204 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3205 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3206 return DAG.getNode(ISD::BIT_CONVERT, VT,
3207 DAG.getNode(Opc, ShVT, SrcOp,
Gabor Greif327ef032008-08-28 23:19:51 +00003208 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
Evan Chengf26ffe92008-05-29 08:22:04 +00003209}
3210
Dan Gohman475871a2008-07-27 21:46:04 +00003211SDValue
3212X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003213 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
Gabor Greif327ef032008-08-28 23:19:51 +00003214 if (ISD::isBuildVectorAllZeros(Op.getNode())
3215 || ISD::isBuildVectorAllOnes(Op.getNode())) {
Chris Lattner8a594482007-11-25 00:24:49 +00003216 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3217 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3218 // eliminated on x86-32 hosts.
3219 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3220 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003221
Gabor Greifba36cb52008-08-28 21:40:38 +00003222 if (ISD::isBuildVectorAllOnes(Op.getNode()))
Chris Lattner8a594482007-11-25 00:24:49 +00003223 return getOnesVector(Op.getValueType(), DAG);
Evan Chengf0df0312008-05-15 08:39:06 +00003224 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattner8a594482007-11-25 00:24:49 +00003225 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003226
Duncan Sands83ec4b62008-06-06 12:08:01 +00003227 MVT VT = Op.getValueType();
3228 MVT EVT = VT.getVectorElementType();
3229 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003230
3231 unsigned NumElems = Op.getNumOperands();
3232 unsigned NumZero = 0;
3233 unsigned NumNonZero = 0;
3234 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003235 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003236 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003237 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003238 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003239 if (Elt.getOpcode() == ISD::UNDEF)
3240 continue;
3241 Values.insert(Elt);
3242 if (Elt.getOpcode() != ISD::Constant &&
3243 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003244 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003245 if (isZeroNode(Elt))
3246 NumZero++;
3247 else {
3248 NonZeros |= (1 << i);
3249 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003250 }
3251 }
3252
Dan Gohman7f321562007-06-25 16:23:39 +00003253 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003254 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3255 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003256 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003257
Chris Lattner67f453a2008-03-09 05:42:06 +00003258 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003259 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003260 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003261 SDValue Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003262
Chris Lattner62098042008-03-09 01:05:04 +00003263 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3264 // the value are obviously zero, truncate the value to i32 and do the
3265 // insertion that way. Only do this if the value is non-constant or if the
3266 // value is a constant being inserted into element 0. It is cheaper to do
3267 // a constant pool load than it is to do a movd + shuffle.
3268 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3269 (!IsAllConstants || Idx == 0)) {
3270 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3271 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003272 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3273 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003274
3275 // Truncate the value (which may itself be a constant) to i32, and
3276 // convert it to a vector with movd (S2V+shuffle to zero extend).
3277 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3278 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003279 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3280 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003281
3282 // Now we have our 32-bit value zero extended in the low element of
3283 // a vector. If Idx != 0, swizzle it into place.
3284 if (Idx != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003285 SDValue Ops[] = {
Chris Lattner62098042008-03-09 01:05:04 +00003286 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3287 getSwapEltZeroMask(VecElts, Idx, DAG)
3288 };
3289 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3290 }
3291 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3292 }
3293 }
3294
Chris Lattner19f79692008-03-08 22:59:52 +00003295 // If we have a constant or non-constant insertion into the low element of
3296 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3297 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3298 // depending on what the source datatype is. Because we can only get here
3299 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3300 if (Idx == 0 &&
3301 // Don't do this for i64 values on x86-32.
3302 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003303 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003304 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003305 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3306 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003307 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003308
3309 // Is it a vector logical left shift?
3310 if (NumElems == 2 && Idx == 1 &&
3311 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003312 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003313 return getVShift(true, VT,
3314 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3315 NumBits/2, DAG, *this);
3316 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003317
3318 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003319 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003320
Chris Lattner19f79692008-03-08 22:59:52 +00003321 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3322 // is a non-constant being inserted into an element other than the low one,
3323 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3324 // movd/movss) to move this into the low element, then shuffle it into
3325 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003326 if (EVTBits == 32) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003327 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3328
Evan Cheng0db9fe62006-04-25 20:13:52 +00003329 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003330 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3331 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003332 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3333 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003334 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003335 for (unsigned i = 0; i < NumElems; i++)
3336 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003337 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003338 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003339 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3340 DAG.getNode(ISD::UNDEF, VT), Mask);
3341 }
3342 }
3343
Chris Lattner67f453a2008-03-09 05:42:06 +00003344 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3345 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003346 return SDValue();
Chris Lattner67f453a2008-03-09 05:42:06 +00003347
Dan Gohmana3941172007-07-24 22:55:08 +00003348 // A vector full of immediates; various special cases are already
3349 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003350 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003351 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003352
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003353 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003354 if (EVTBits == 64) {
3355 if (NumNonZero == 1) {
3356 // One half is zero or undef.
3357 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003358 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003359 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003360 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3361 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003362 }
Dan Gohman475871a2008-07-27 21:46:04 +00003363 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003364 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003365
3366 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003367 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003368 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003369 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003370 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003371 }
3372
Bill Wendling826f36f2007-03-28 00:57:11 +00003373 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003374 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003375 *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003376 if (V.getNode()) return V;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003377 }
3378
3379 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003380 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003381 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003382 if (NumElems == 4 && NumZero > 0) {
3383 for (unsigned i = 0; i < 4; ++i) {
3384 bool isZero = !(NonZeros & (1 << i));
3385 if (isZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003386 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003387 else
3388 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3389 }
3390
3391 for (unsigned i = 0; i < 2; ++i) {
3392 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3393 default: break;
3394 case 0:
3395 V[i] = V[i*2]; // Must be a zero vector.
3396 break;
3397 case 1:
3398 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3399 getMOVLMask(NumElems, DAG));
3400 break;
3401 case 2:
3402 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3403 getMOVLMask(NumElems, DAG));
3404 break;
3405 case 3:
3406 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3407 getUnpacklMask(NumElems, DAG));
3408 break;
3409 }
3410 }
3411
Duncan Sands83ec4b62008-06-06 12:08:01 +00003412 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3413 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003414 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003415 bool Reverse = (NonZeros & 0x3) == 2;
3416 for (unsigned i = 0; i < 2; ++i)
3417 if (Reverse)
3418 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3419 else
3420 MaskVec.push_back(DAG.getConstant(i, EVT));
3421 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3422 for (unsigned i = 0; i < 2; ++i)
3423 if (Reverse)
3424 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3425 else
3426 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003427 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00003428 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003429 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3430 }
3431
3432 if (Values.size() > 2) {
3433 // Expand into a number of unpckl*.
3434 // e.g. for v4f32
3435 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3436 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3437 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman475871a2008-07-27 21:46:04 +00003438 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003439 for (unsigned i = 0; i < NumElems; ++i)
3440 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3441 NumElems >>= 1;
3442 while (NumElems != 0) {
3443 for (unsigned i = 0; i < NumElems; ++i)
3444 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3445 UnpckMask);
3446 NumElems >>= 1;
3447 }
3448 return V[0];
3449 }
3450
Dan Gohman475871a2008-07-27 21:46:04 +00003451 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003452}
3453
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003454static
Dan Gohman475871a2008-07-27 21:46:04 +00003455SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
Bill Wendlinge85dc492008-08-21 22:35:37 +00003456 SDValue PermMask, SelectionDAG &DAG,
3457 TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +00003458 SDValue NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003459 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3460 MVT MaskEVT = MaskVT.getVectorElementType();
3461 MVT PtrVT = TLI.getPointerTy();
Gabor Greifba36cb52008-08-28 21:40:38 +00003462 SmallVector<SDValue, 8> MaskElts(PermMask.getNode()->op_begin(),
3463 PermMask.getNode()->op_end());
Evan Cheng14b32e12007-12-11 01:46:18 +00003464
3465 // First record which half of which vector the low elements come from.
3466 SmallVector<unsigned, 4> LowQuad(4);
3467 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003468 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003469 if (Elt.getOpcode() == ISD::UNDEF)
3470 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003471 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003472 int QuadIdx = EltIdx / 4;
3473 ++LowQuad[QuadIdx];
3474 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003475
Evan Cheng14b32e12007-12-11 01:46:18 +00003476 int BestLowQuad = -1;
3477 unsigned MaxQuad = 1;
3478 for (unsigned i = 0; i < 4; ++i) {
3479 if (LowQuad[i] > MaxQuad) {
3480 BestLowQuad = i;
3481 MaxQuad = LowQuad[i];
3482 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003483 }
3484
Evan Cheng14b32e12007-12-11 01:46:18 +00003485 // Record which half of which vector the high elements come from.
3486 SmallVector<unsigned, 4> HighQuad(4);
3487 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003488 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003489 if (Elt.getOpcode() == ISD::UNDEF)
3490 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003491 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003492 int QuadIdx = EltIdx / 4;
3493 ++HighQuad[QuadIdx];
3494 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003495
Evan Cheng14b32e12007-12-11 01:46:18 +00003496 int BestHighQuad = -1;
3497 MaxQuad = 1;
3498 for (unsigned i = 0; i < 4; ++i) {
3499 if (HighQuad[i] > MaxQuad) {
3500 BestHighQuad = i;
3501 MaxQuad = HighQuad[i];
3502 }
3503 }
3504
3505 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3506 if (BestLowQuad != -1 || BestHighQuad != -1) {
3507 // First sort the 4 chunks in order using shufpd.
Dan Gohman475871a2008-07-27 21:46:04 +00003508 SmallVector<SDValue, 8> MaskVec;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003509
Evan Cheng14b32e12007-12-11 01:46:18 +00003510 if (BestLowQuad != -1)
3511 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3512 else
3513 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003514
Evan Cheng14b32e12007-12-11 01:46:18 +00003515 if (BestHighQuad != -1)
3516 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3517 else
3518 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003519
Dan Gohman475871a2008-07-27 21:46:04 +00003520 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng14b32e12007-12-11 01:46:18 +00003521 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3522 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3523 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3524 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3525
3526 // Now sort high and low parts separately.
3527 BitVector InOrder(8);
3528 if (BestLowQuad != -1) {
3529 // Sort lower half in order using PSHUFLW.
3530 MaskVec.clear();
3531 bool AnyOutOrder = false;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003532
Evan Cheng14b32e12007-12-11 01:46:18 +00003533 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003534 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003535 if (Elt.getOpcode() == ISD::UNDEF) {
3536 MaskVec.push_back(Elt);
3537 InOrder.set(i);
3538 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003539 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003540 if (EltIdx != i)
3541 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003542
Evan Cheng14b32e12007-12-11 01:46:18 +00003543 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003544
Evan Cheng14b32e12007-12-11 01:46:18 +00003545 // If this element is in the right place after this shuffle, then
3546 // remember it.
3547 if ((int)(EltIdx / 4) == BestLowQuad)
3548 InOrder.set(i);
3549 }
3550 }
3551 if (AnyOutOrder) {
3552 for (unsigned i = 4; i != 8; ++i)
3553 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003554 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003555 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3556 }
3557 }
3558
3559 if (BestHighQuad != -1) {
3560 // Sort high half in order using PSHUFHW if possible.
3561 MaskVec.clear();
Bill Wendlinge85dc492008-08-21 22:35:37 +00003562
Evan Cheng14b32e12007-12-11 01:46:18 +00003563 for (unsigned i = 0; i != 4; ++i)
3564 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003565
Evan Cheng14b32e12007-12-11 01:46:18 +00003566 bool AnyOutOrder = false;
3567 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003568 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003569 if (Elt.getOpcode() == ISD::UNDEF) {
3570 MaskVec.push_back(Elt);
3571 InOrder.set(i);
3572 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003573 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003574 if (EltIdx != i)
3575 AnyOutOrder = true;
Bill Wendlinge85dc492008-08-21 22:35:37 +00003576
Evan Cheng14b32e12007-12-11 01:46:18 +00003577 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
Bill Wendlinge85dc492008-08-21 22:35:37 +00003578
Evan Cheng14b32e12007-12-11 01:46:18 +00003579 // If this element is in the right place after this shuffle, then
3580 // remember it.
3581 if ((int)(EltIdx / 4) == BestHighQuad)
3582 InOrder.set(i);
3583 }
3584 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003585
Evan Cheng14b32e12007-12-11 01:46:18 +00003586 if (AnyOutOrder) {
Dan Gohman475871a2008-07-27 21:46:04 +00003587 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003588 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3589 }
3590 }
3591
3592 // The other elements are put in the right place using pextrw and pinsrw.
3593 for (unsigned i = 0; i != 8; ++i) {
3594 if (InOrder[i])
3595 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003596 SDValue Elt = MaskElts[i];
Bill Wendlingae0218c2008-08-21 22:36:36 +00003597 if (Elt.getOpcode() == ISD::UNDEF)
3598 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003599 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003600 SDValue ExtOp = (EltIdx < 8)
Evan Cheng14b32e12007-12-11 01:46:18 +00003601 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3602 DAG.getConstant(EltIdx, PtrVT))
3603 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3604 DAG.getConstant(EltIdx - 8, PtrVT));
3605 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3606 DAG.getConstant(i, PtrVT));
3607 }
Bill Wendlinge85dc492008-08-21 22:35:37 +00003608
Evan Cheng14b32e12007-12-11 01:46:18 +00003609 return NewV;
3610 }
3611
Bill Wendlinge85dc492008-08-21 22:35:37 +00003612 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use as
3613 // few as possible. First, let's find out how many elements are already in the
3614 // right order.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003615 unsigned V1InOrder = 0;
3616 unsigned V1FromV1 = 0;
3617 unsigned V2InOrder = 0;
3618 unsigned V2FromV2 = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003619 SmallVector<SDValue, 8> V1Elts;
3620 SmallVector<SDValue, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003621 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003622 SDValue Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003623 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003624 V1Elts.push_back(Elt);
3625 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003626 ++V1InOrder;
3627 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003628 continue;
3629 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003630 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003631 if (EltIdx == i) {
3632 V1Elts.push_back(Elt);
3633 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3634 ++V1InOrder;
3635 } else if (EltIdx == i+8) {
3636 V1Elts.push_back(Elt);
3637 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3638 ++V2InOrder;
3639 } else if (EltIdx < 8) {
3640 V1Elts.push_back(Elt);
3641 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003642 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003643 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3644 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003645 }
3646 }
3647
3648 if (V2InOrder > V1InOrder) {
3649 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3650 std::swap(V1, V2);
3651 std::swap(V1Elts, V2Elts);
3652 std::swap(V1FromV1, V2FromV2);
3653 }
3654
Evan Cheng14b32e12007-12-11 01:46:18 +00003655 if ((V1FromV1 + V1InOrder) != 8) {
3656 // Some elements are from V2.
3657 if (V1FromV1) {
3658 // If there are elements that are from V1 but out of place,
3659 // then first sort them in place
Dan Gohman475871a2008-07-27 21:46:04 +00003660 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003661 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003662 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003663 if (Elt.getOpcode() == ISD::UNDEF) {
3664 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3665 continue;
3666 }
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003667 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003668 if (EltIdx >= 8)
3669 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3670 else
3671 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3672 }
Dan Gohman475871a2008-07-27 21:46:04 +00003673 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003674 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003675 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003676
3677 NewV = V1;
3678 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003679 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003680 if (Elt.getOpcode() == ISD::UNDEF)
3681 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003682 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003683 if (EltIdx < 8)
3684 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003685 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003686 DAG.getConstant(EltIdx - 8, PtrVT));
3687 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3688 DAG.getConstant(i, PtrVT));
3689 }
3690 return NewV;
3691 } else {
3692 // All elements are from V1.
3693 NewV = V1;
3694 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003695 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003696 if (Elt.getOpcode() == ISD::UNDEF)
3697 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003698 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003699 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003700 DAG.getConstant(EltIdx, PtrVT));
3701 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3702 DAG.getConstant(i, PtrVT));
3703 }
3704 return NewV;
3705 }
3706}
3707
Evan Cheng7a831ce2007-12-15 03:00:47 +00003708/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3709/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3710/// done when every pair / quad of shuffle mask elements point to elements in
3711/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003712/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3713static
Dan Gohman475871a2008-07-27 21:46:04 +00003714SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003715 MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00003716 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng14b32e12007-12-11 01:46:18 +00003717 TargetLowering &TLI) {
3718 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003719 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003720 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003721 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003722 MVT NewVT = MaskVT;
3723 switch (VT.getSimpleVT()) {
3724 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003725 case MVT::v4f32: NewVT = MVT::v2f64; break;
3726 case MVT::v4i32: NewVT = MVT::v2i64; break;
3727 case MVT::v8i16: NewVT = MVT::v4i32; break;
3728 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003729 }
3730
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003731 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003732 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003733 NewVT = MVT::v2i64;
3734 else
3735 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003736 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003737 unsigned Scale = NumElems / NewWidth;
Dan Gohman475871a2008-07-27 21:46:04 +00003738 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003739 for (unsigned i = 0; i < NumElems; i += Scale) {
3740 unsigned StartIdx = ~0U;
3741 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003742 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng14b32e12007-12-11 01:46:18 +00003743 if (Elt.getOpcode() == ISD::UNDEF)
3744 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003745 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003746 if (StartIdx == ~0U)
3747 StartIdx = EltIdx - (EltIdx % Scale);
3748 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003749 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003750 }
3751 if (StartIdx == ~0U)
Duncan Sandsd038e042008-07-21 10:20:31 +00003752 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003753 else
Duncan Sandsd038e042008-07-21 10:20:31 +00003754 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003755 }
3756
Evan Cheng7a831ce2007-12-15 03:00:47 +00003757 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3758 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3759 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3760 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3761 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003762}
3763
Evan Chengd880b972008-05-09 21:53:03 +00003764/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003765///
Dan Gohman475871a2008-07-27 21:46:04 +00003766static SDValue getVZextMovL(MVT VT, MVT OpVT,
3767 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003768 const X86Subtarget *Subtarget) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003769 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3770 LoadSDNode *LD = NULL;
Gabor Greifba36cb52008-08-28 21:40:38 +00003771 if (!isScalarLoadToVector(SrcOp.getNode(), &LD))
Evan Cheng7e2ff772008-05-08 00:57:18 +00003772 LD = dyn_cast<LoadSDNode>(SrcOp);
3773 if (!LD) {
3774 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3775 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003776 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003777 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3778 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3779 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3780 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3781 // PR2108
3782 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3783 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003784 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003785 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
Gabor Greif327ef032008-08-28 23:19:51 +00003786 SrcOp.getOperand(0)
3787 .getOperand(0))));
Evan Cheng7e2ff772008-05-08 00:57:18 +00003788 }
3789 }
3790 }
3791
3792 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003793 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003794 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3795}
3796
Evan Chengace3c172008-07-22 21:13:36 +00003797/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3798/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003799static SDValue
3800LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3801 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengace3c172008-07-22 21:13:36 +00003802 MVT MaskVT = PermMask.getValueType();
3803 MVT MaskEVT = MaskVT.getVectorElementType();
3804 SmallVector<std::pair<int, int>, 8> Locs;
Rafael Espindola833a9902008-08-28 18:32:53 +00003805 Locs.resize(4);
Dan Gohman475871a2008-07-27 21:46:04 +00003806 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003807 unsigned NumHi = 0;
3808 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003809 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003810 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003811 if (Elt.getOpcode() == ISD::UNDEF) {
3812 Locs[i] = std::make_pair(-1, -1);
3813 } else {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003814 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Dan Gohmand0859942008-08-04 23:09:15 +00003815 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengace3c172008-07-22 21:13:36 +00003816 if (Val < 4) {
3817 Locs[i] = std::make_pair(0, NumLo);
3818 Mask1[NumLo] = Elt;
3819 NumLo++;
3820 } else {
3821 Locs[i] = std::make_pair(1, NumHi);
3822 if (2+NumHi < 4)
3823 Mask1[2+NumHi] = Elt;
3824 NumHi++;
3825 }
3826 }
3827 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003828
Evan Chengace3c172008-07-22 21:13:36 +00003829 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003830 // If no more than two elements come from either vector. This can be
3831 // implemented with two shuffles. First shuffle gather the elements.
3832 // The second shuffle, which takes the first shuffle as both of its
3833 // vector operands, put the elements into the right order.
Evan Chengace3c172008-07-22 21:13:36 +00003834 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3835 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3836 &Mask1[0], Mask1.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003837
Dan Gohman475871a2008-07-27 21:46:04 +00003838 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003839 for (unsigned i = 0; i != 4; ++i) {
3840 if (Locs[i].first == -1)
3841 continue;
3842 else {
3843 unsigned Idx = (i < 2) ? 0 : 4;
3844 Idx += Locs[i].first * 2 + Locs[i].second;
3845 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3846 }
3847 }
3848
3849 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3850 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3851 &Mask2[0], Mask2.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003852 } else if (NumLo == 3 || NumHi == 3) {
3853 // Otherwise, we must have three elements from one vector, call it X, and
3854 // one element from the other, call it Y. First, use a shufps to build an
3855 // intermediate vector with the one element from Y and the element from X
3856 // that will be in the same half in the final destination (the indexes don't
3857 // matter). Then, use a shufps to build the final vector, taking the half
3858 // containing the element from Y from the intermediate, and the other half
3859 // from X.
3860 if (NumHi == 3) {
3861 // Normalize it so the 3 elements come from V1.
3862 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3863 std::swap(V1, V2);
3864 }
3865
3866 // Find the element from V2.
3867 unsigned HiIndex;
3868 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman475871a2008-07-27 21:46:04 +00003869 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003870 if (Elt.getOpcode() == ISD::UNDEF)
3871 continue;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003872 unsigned Val = cast<ConstantSDNode>(Elt)->getZExtValue();
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003873 if (Val >= 4)
3874 break;
3875 }
3876
3877 Mask1[0] = PermMask.getOperand(HiIndex);
3878 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3879 Mask1[2] = PermMask.getOperand(HiIndex^1);
3880 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3881 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3882 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3883
3884 if (HiIndex >= 2) {
3885 Mask1[0] = PermMask.getOperand(0);
3886 Mask1[1] = PermMask.getOperand(1);
3887 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3888 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3889 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3890 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3891 } else {
3892 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3893 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3894 Mask1[2] = PermMask.getOperand(2);
3895 Mask1[3] = PermMask.getOperand(3);
3896 if (Mask1[2].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003897 Mask1[2] =
3898 DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getZExtValue()+4,
3899 MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003900 if (Mask1[3].getOpcode() != ISD::UNDEF)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003901 Mask1[3] =
3902 DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getZExtValue()+4,
3903 MaskEVT);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003904 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3905 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3906 }
Evan Chengace3c172008-07-22 21:13:36 +00003907 }
3908
3909 // Break it into (shuffle shuffle_hi, shuffle_lo).
3910 Locs.clear();
Dan Gohman475871a2008-07-27 21:46:04 +00003911 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3912 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3913 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003914 unsigned MaskIdx = 0;
3915 unsigned LoIdx = 0;
3916 unsigned HiIdx = 2;
3917 for (unsigned i = 0; i != 4; ++i) {
3918 if (i == 2) {
3919 MaskPtr = &HiMask;
3920 MaskIdx = 1;
3921 LoIdx = 0;
3922 HiIdx = 2;
3923 }
Dan Gohman475871a2008-07-27 21:46:04 +00003924 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003925 if (Elt.getOpcode() == ISD::UNDEF) {
3926 Locs[i] = std::make_pair(-1, -1);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00003927 } else if (cast<ConstantSDNode>(Elt)->getZExtValue() < 4) {
Evan Chengace3c172008-07-22 21:13:36 +00003928 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3929 (*MaskPtr)[LoIdx] = Elt;
3930 LoIdx++;
3931 } else {
3932 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3933 (*MaskPtr)[HiIdx] = Elt;
3934 HiIdx++;
3935 }
3936 }
3937
Dan Gohman475871a2008-07-27 21:46:04 +00003938 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003939 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3940 &LoMask[0], LoMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003941 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003942 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3943 &HiMask[0], HiMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003944 SmallVector<SDValue, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003945 for (unsigned i = 0; i != 4; ++i) {
3946 if (Locs[i].first == -1) {
3947 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3948 } else {
3949 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3950 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3951 }
3952 }
3953 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3954 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3955 &MaskOps[0], MaskOps.size()));
3956}
3957
Dan Gohman475871a2008-07-27 21:46:04 +00003958SDValue
3959X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3960 SDValue V1 = Op.getOperand(0);
3961 SDValue V2 = Op.getOperand(1);
3962 SDValue PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003963 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003964 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003965 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003966 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3967 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003968 bool V1IsSplat = false;
3969 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003970
Gabor Greifba36cb52008-08-28 21:40:38 +00003971 if (isUndefShuffle(Op.getNode()))
Evan Cheng8cf723d2006-09-08 01:50:06 +00003972 return DAG.getNode(ISD::UNDEF, VT);
3973
Gabor Greifba36cb52008-08-28 21:40:38 +00003974 if (isZeroShuffle(Op.getNode()))
Evan Chengf0df0312008-05-15 08:39:06 +00003975 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003976
Gabor Greifba36cb52008-08-28 21:40:38 +00003977 if (isIdentityMask(PermMask.getNode()))
Evan Cheng49892af2007-06-19 00:02:56 +00003978 return V1;
Gabor Greifba36cb52008-08-28 21:40:38 +00003979 else if (isIdentityMask(PermMask.getNode(), true))
Evan Cheng49892af2007-06-19 00:02:56 +00003980 return V2;
3981
Evan Cheng4dcc8a32008-09-25 23:35:16 +00003982 // Canonicalize movddup shuffles.
3983 if (V2IsUndef && Subtarget->hasSSE2() &&
Evan Cheng882cdfd2008-10-06 21:13:08 +00003984 VT.getSizeInBits() == 128 &&
Evan Cheng4dcc8a32008-09-25 23:35:16 +00003985 X86::isMOVDDUPMask(PermMask.getNode()))
3986 return CanonicalizeMovddup(Op, V1, PermMask, DAG, Subtarget->hasSSE3());
3987
Gabor Greifba36cb52008-08-28 21:40:38 +00003988 if (isSplatMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003989 if (isMMX || NumElems < 4) return Op;
3990 // Promote it to a v4{if}32 splat.
3991 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003992 }
3993
Evan Cheng7a831ce2007-12-15 03:00:47 +00003994 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3995 // do it!
3996 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003997 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00003998 if (NewOp.getNode())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003999 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
4000 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
4001 // FIXME: Figure out a cleaner way to do this.
4002 // Try to make use of movq to zero out the top part.
Gabor Greifba36cb52008-08-28 21:40:38 +00004003 if (ISD::isBuildVectorAllZeros(V2.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004004 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004005 DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004006 if (NewOp.getNode()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004007 SDValue NewV1 = NewOp.getOperand(0);
4008 SDValue NewV2 = NewOp.getOperand(1);
4009 SDValue NewMask = NewOp.getOperand(2);
Gabor Greifba36cb52008-08-28 21:40:38 +00004010 if (isCommutedMOVL(NewMask.getNode(), true, false)) {
Evan Cheng7a831ce2007-12-15 03:00:47 +00004011 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chengd880b972008-05-09 21:53:03 +00004012 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004013 }
4014 }
Gabor Greifba36cb52008-08-28 21:40:38 +00004015 } else if (ISD::isBuildVectorAllZeros(V1.getNode())) {
Dan Gohman475871a2008-07-27 21:46:04 +00004016 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00004017 DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004018 if (NewOp.getNode() && X86::isMOVLMask(NewOp.getOperand(2).getNode()))
Evan Chengd880b972008-05-09 21:53:03 +00004019 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng7e2ff772008-05-08 00:57:18 +00004020 DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00004021 }
4022 }
4023
Evan Chengf26ffe92008-05-29 08:22:04 +00004024 // Check if this can be converted into a logical shift.
4025 bool isLeft = false;
4026 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00004027 SDValue ShVal;
Evan Chengf26ffe92008-05-29 08:22:04 +00004028 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
4029 if (isShift && ShVal.hasOneUse()) {
4030 // If the shifted value has multiple uses, it may be cheaper to use
4031 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004032 MVT EVT = VT.getVectorElementType();
4033 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004034 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4035 }
4036
Gabor Greifba36cb52008-08-28 21:40:38 +00004037 if (X86::isMOVLMask(PermMask.getNode())) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00004038 if (V1IsUndef)
4039 return V2;
Gabor Greifba36cb52008-08-28 21:40:38 +00004040 if (ISD::isBuildVectorAllZeros(V1.getNode()))
Evan Chengd880b972008-05-09 21:53:03 +00004041 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begemanfb8ead02008-07-25 19:05:58 +00004042 if (!isMMX)
4043 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00004044 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004045
Gabor Greifba36cb52008-08-28 21:40:38 +00004046 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.getNode()) ||
4047 X86::isMOVSLDUPMask(PermMask.getNode()) ||
4048 X86::isMOVHLPSMask(PermMask.getNode()) ||
4049 X86::isMOVHPMask(PermMask.getNode()) ||
4050 X86::isMOVLPMask(PermMask.getNode())))
Evan Cheng9bbbb982006-10-25 20:48:19 +00004051 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004052
Gabor Greifba36cb52008-08-28 21:40:38 +00004053 if (ShouldXformToMOVHLPS(PermMask.getNode()) ||
4054 ShouldXformToMOVLP(V1.getNode(), V2.getNode(), PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004055 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004056
Evan Chengf26ffe92008-05-29 08:22:04 +00004057 if (isShift) {
4058 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004059 MVT EVT = VT.getVectorElementType();
4060 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00004061 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
4062 }
4063
Evan Cheng9eca5e82006-10-25 21:49:50 +00004064 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00004065 // FIXME: This should also accept a bitcast of a splat? Be careful, not
4066 // 1,1,1,1 -> v8i16 though.
Gabor Greifba36cb52008-08-28 21:40:38 +00004067 V1IsSplat = isSplatVector(V1.getNode());
4068 V2IsSplat = isSplatVector(V2.getNode());
Chris Lattner8a594482007-11-25 00:24:49 +00004069
4070 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00004071 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00004072 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004073 std::swap(V1IsSplat, V2IsSplat);
4074 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00004075 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00004076 }
4077
Evan Cheng7a831ce2007-12-15 03:00:47 +00004078 // FIXME: Figure out a cleaner way to do this.
Gabor Greifba36cb52008-08-28 21:40:38 +00004079 if (isCommutedMOVL(PermMask.getNode(), V2IsSplat, V2IsUndef)) {
Evan Cheng9bbbb982006-10-25 20:48:19 +00004080 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00004081 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004082 if (V2IsSplat) {
4083 // V2 is a splat, so the mask may be malformed. That is, it may point
4084 // to any V2 element. The instruction selectior won't like this. Get
4085 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman475871a2008-07-27 21:46:04 +00004086 SDValue NewMask = getMOVLMask(NumElems, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004087 if (NewMask.getNode() != PermMask.getNode())
Evan Cheng9bbbb982006-10-25 20:48:19 +00004088 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004089 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00004090 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00004091 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004092
Gabor Greifba36cb52008-08-28 21:40:38 +00004093 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4094 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4095 X86::isUNPCKLMask(PermMask.getNode()) ||
4096 X86::isUNPCKHMask(PermMask.getNode()))
Evan Chengd9b8e402006-10-16 06:36:00 +00004097 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00004098
Evan Cheng9bbbb982006-10-25 20:48:19 +00004099 if (V2IsSplat) {
4100 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004101 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00004102 // new vector_shuffle with the corrected mask.
Dan Gohman475871a2008-07-27 21:46:04 +00004103 SDValue NewMask = NormalizeMask(PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004104 if (NewMask.getNode() != PermMask.getNode()) {
4105 if (X86::isUNPCKLMask(PermMask.getNode(), true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004106 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004107 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Gabor Greifba36cb52008-08-28 21:40:38 +00004108 } else if (X86::isUNPCKHMask(PermMask.getNode(), true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00004109 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00004110 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004111 }
4112 }
4113 }
4114
4115 // Normalize the node to match x86 shuffle ops if needed
Gabor Greifba36cb52008-08-28 21:40:38 +00004116 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004117 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
4118
4119 if (Commuted) {
4120 // Commute is back and try unpck* again.
4121 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004122 if (X86::isUNPCKL_v_undef_Mask(PermMask.getNode()) ||
4123 X86::isUNPCKH_v_undef_Mask(PermMask.getNode()) ||
4124 X86::isUNPCKLMask(PermMask.getNode()) ||
4125 X86::isUNPCKHMask(PermMask.getNode()))
Evan Cheng9eca5e82006-10-25 21:49:50 +00004126 return Op;
4127 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004128
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004129 // Try PSHUF* first, then SHUFP*.
4130 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
4131 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
Gabor Greifba36cb52008-08-28 21:40:38 +00004132 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.getNode())) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004133 if (V2.getOpcode() != ISD::UNDEF)
4134 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
4135 DAG.getNode(ISD::UNDEF, VT), PermMask);
4136 return Op;
4137 }
4138
4139 if (!isMMX) {
4140 if (Subtarget->hasSSE2() &&
Gabor Greifba36cb52008-08-28 21:40:38 +00004141 (X86::isPSHUFDMask(PermMask.getNode()) ||
4142 X86::isPSHUFHWMask(PermMask.getNode()) ||
4143 X86::isPSHUFLWMask(PermMask.getNode()))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004144 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004145 if (VT == MVT::v4f32) {
4146 RVT = MVT::v4i32;
4147 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4148 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4149 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4150 } else if (V2.getOpcode() != ISD::UNDEF)
4151 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4152 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4153 if (RVT != VT)
4154 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004155 return Op;
4156 }
4157
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004158 // Binary or unary shufps.
Gabor Greifba36cb52008-08-28 21:40:38 +00004159 if (X86::isSHUFPMask(PermMask.getNode()) ||
4160 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.getNode())))
Evan Cheng0db9fe62006-04-25 20:13:52 +00004161 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004162 }
4163
Evan Cheng14b32e12007-12-11 01:46:18 +00004164 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4165 if (VT == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004166 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Gabor Greifba36cb52008-08-28 21:40:38 +00004167 if (NewOp.getNode())
Evan Cheng14b32e12007-12-11 01:46:18 +00004168 return NewOp;
4169 }
4170
Evan Chengace3c172008-07-22 21:13:36 +00004171 // Handle all 4 wide cases with a number of shuffles except for MMX.
4172 if (NumElems == 4 && !isMMX)
4173 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004174
Dan Gohman475871a2008-07-27 21:46:04 +00004175 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004176}
4177
Dan Gohman475871a2008-07-27 21:46:04 +00004178SDValue
4179X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004180 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004181 MVT VT = Op.getValueType();
4182 if (VT.getSizeInBits() == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004183 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004184 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004185 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004186 DAG.getValueType(VT));
4187 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004188 } else if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004189 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004190 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004191 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004192 DAG.getValueType(VT));
4193 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004194 } else if (VT == MVT::f32) {
4195 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4196 // the result back to FR32 register. It's only worth matching if the
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004197 // result has a single use which is a store or a bitcast to i32. And in
4198 // the case of a store, it's not worth it if the index is a constant 0,
4199 // because a MOVSSmr can be used instead, which is smaller and faster.
Evan Cheng62a3f152008-03-24 21:52:23 +00004200 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004201 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00004202 SDNode *User = *Op.getNode()->use_begin();
Dan Gohmand17cfbe2008-10-31 00:57:24 +00004203 if ((User->getOpcode() != ISD::STORE ||
4204 (isa<ConstantSDNode>(Op.getOperand(1)) &&
4205 cast<ConstantSDNode>(Op.getOperand(1))->isNullValue())) &&
Dan Gohman171c11e2008-04-16 02:32:24 +00004206 (User->getOpcode() != ISD::BIT_CONVERT ||
4207 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004208 return SDValue();
4209 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng62a3f152008-03-24 21:52:23 +00004210 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4211 Op.getOperand(1));
4212 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004213 }
Dan Gohman475871a2008-07-27 21:46:04 +00004214 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004215}
4216
4217
Dan Gohman475871a2008-07-27 21:46:04 +00004218SDValue
4219X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004220 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004221 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004222
Evan Cheng62a3f152008-03-24 21:52:23 +00004223 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004224 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Gabor Greifba36cb52008-08-28 21:40:38 +00004225 if (Res.getNode())
Evan Cheng62a3f152008-03-24 21:52:23 +00004226 return Res;
4227 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004228
Duncan Sands83ec4b62008-06-06 12:08:01 +00004229 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004230 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004231 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004232 SDValue Vec = Op.getOperand(0);
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004233 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00004234 if (Idx == 0)
4235 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4236 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4237 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4238 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004239 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004240 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman475871a2008-07-27 21:46:04 +00004241 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004242 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004243 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004244 DAG.getValueType(VT));
4245 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004246 } else if (VT.getSizeInBits() == 32) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004247 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004248 if (Idx == 0)
4249 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004250 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004251 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman475871a2008-07-27 21:46:04 +00004252 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004253 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004254 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004255 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004256 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004257 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004258 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004259 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004260 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004261 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004262 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004263 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004264 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00004265 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004266 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004267 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004268 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004269 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4270 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4271 // to match extract_elt for f64.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004272 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004273 if (Idx == 0)
4274 return Op;
4275
4276 // UNPCKHPD the element to the lowest double word, then movsd.
4277 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4278 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd038e042008-07-21 10:20:31 +00004279 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman475871a2008-07-27 21:46:04 +00004280 SmallVector<SDValue, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004281 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004282 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004283 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004284 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004285 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004286 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004287 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4288 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4289 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004290 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004291 }
4292
Dan Gohman475871a2008-07-27 21:46:04 +00004293 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004294}
4295
Dan Gohman475871a2008-07-27 21:46:04 +00004296SDValue
4297X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004298 MVT VT = Op.getValueType();
4299 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004300
Dan Gohman475871a2008-07-27 21:46:04 +00004301 SDValue N0 = Op.getOperand(0);
4302 SDValue N1 = Op.getOperand(1);
4303 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004304
Dan Gohmanef521f12008-08-14 22:53:18 +00004305 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4306 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004307 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004308 : X86ISD::PINSRW;
4309 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4310 // argument.
4311 if (N1.getValueType() != MVT::i32)
4312 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4313 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004314 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Nate Begeman14d12ca2008-02-11 04:19:36 +00004315 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004316 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004317 // Bits [7:6] of the constant are the source select. This will always be
4318 // zero here. The DAG Combiner may combine an extract_elt index into these
4319 // bits. For example (insert (extract, 3), 2) could be matched by putting
4320 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4321 // Bits [5:4] of the constant are the destination select. This is the
4322 // value of the incoming immediate.
4323 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4324 // combine either bitwise AND or insert of float 0.0 to set these bits.
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004325 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue() << 4);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004326 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4327 }
Dan Gohman475871a2008-07-27 21:46:04 +00004328 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004329}
4330
Dan Gohman475871a2008-07-27 21:46:04 +00004331SDValue
4332X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004333 MVT VT = Op.getValueType();
4334 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004335
4336 if (Subtarget->hasSSE41())
4337 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4338
Evan Cheng794405e2007-12-12 07:55:34 +00004339 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004340 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004341
Dan Gohman475871a2008-07-27 21:46:04 +00004342 SDValue N0 = Op.getOperand(0);
4343 SDValue N1 = Op.getOperand(1);
4344 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004345
Duncan Sands83ec4b62008-06-06 12:08:01 +00004346 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004347 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4348 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004349 if (N1.getValueType() != MVT::i32)
4350 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4351 if (N2.getValueType() != MVT::i32)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00004352 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getZExtValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004353 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004354 }
Dan Gohman475871a2008-07-27 21:46:04 +00004355 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004356}
4357
Dan Gohman475871a2008-07-27 21:46:04 +00004358SDValue
4359X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng52672b82008-07-22 18:39:19 +00004360 if (Op.getValueType() == MVT::v2f32)
4361 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4362 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4363 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4364 Op.getOperand(0))));
4365
Dan Gohman475871a2008-07-27 21:46:04 +00004366 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004367 MVT VT = MVT::v2i32;
4368 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004369 default: break;
4370 case MVT::v16i8:
4371 case MVT::v8i16:
4372 VT = MVT::v4i32;
4373 break;
4374 }
4375 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4376 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004377}
4378
Bill Wendling056292f2008-09-16 21:48:12 +00004379// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
4380// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4381// one of the above mentioned nodes. It has to be wrapped because otherwise
4382// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4383// be used to form addressing mode. These wrapped nodes will be selected
4384// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004385SDValue
4386X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004387 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004388 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Evan Chengd0ff02c2006-11-29 23:19:46 +00004389 getPointerTy(),
4390 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004391 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004392 // With PIC, the address is actually $g + Offset.
4393 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4394 !Subtarget->isPICStyleRIPRel()) {
4395 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4396 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4397 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004398 }
4399
4400 return Result;
4401}
4402
Dan Gohman475871a2008-07-27 21:46:04 +00004403SDValue
Evan Chengda43bcf2008-09-24 00:05:32 +00004404X86TargetLowering::LowerGlobalAddress(const GlobalValue *GV,
Dan Gohman6520e202008-10-18 02:06:02 +00004405 int64_t Offset,
Evan Chengda43bcf2008-09-24 00:05:32 +00004406 SelectionDAG &DAG) const {
Dan Gohman6520e202008-10-18 02:06:02 +00004407 bool IsPic = getTargetMachine().getRelocationModel() == Reloc::PIC_;
4408 bool ExtraLoadRequired =
4409 Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false);
4410
4411 // Create the TargetGlobalAddress node, folding in the constant
4412 // offset if it is legal.
4413 SDValue Result;
Dan Gohman44013612008-10-21 03:38:42 +00004414 if (!IsPic && !ExtraLoadRequired && isInt32(Offset)) {
Dan Gohman6520e202008-10-18 02:06:02 +00004415 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), Offset);
4416 Offset = 0;
4417 } else
4418 Result = DAG.getTargetGlobalAddress(GV, getPointerTy(), 0);
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004419 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Dan Gohman6520e202008-10-18 02:06:02 +00004420
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004421 // With PIC, the address is actually $g + Offset.
Dan Gohman6520e202008-10-18 02:06:02 +00004422 if (IsPic && !Subtarget->isPICStyleRIPRel()) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004423 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4424 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4425 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004426 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004427
4428 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4429 // load the value at address GV, not the value of GV itself. This means that
4430 // the GlobalAddress must be in the base or index register of the address, not
4431 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004432 // The same applies for external symbols during PIC codegen
Dan Gohman6520e202008-10-18 02:06:02 +00004433 if (ExtraLoadRequired)
Dan Gohman69de1932008-02-06 22:27:42 +00004434 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004435 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004436
Dan Gohman6520e202008-10-18 02:06:02 +00004437 // If there was a non-zero offset that we didn't fold, create an explicit
4438 // addition for it.
4439 if (Offset != 0)
4440 Result = DAG.getNode(ISD::ADD, getPointerTy(), Result,
4441 DAG.getConstant(Offset, getPointerTy()));
4442
Evan Cheng0db9fe62006-04-25 20:13:52 +00004443 return Result;
4444}
4445
Evan Chengda43bcf2008-09-24 00:05:32 +00004446SDValue
4447X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
4448 const GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00004449 int64_t Offset = cast<GlobalAddressSDNode>(Op)->getOffset();
4450 return LowerGlobalAddress(GV, Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00004451}
4452
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004453// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004454static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004455LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004456 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004457 SDValue InFlag;
4458 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004459 DAG.getNode(X86ISD::GlobalBaseReg,
4460 PtrVT), InFlag);
4461 InFlag = Chain.getValue(1);
4462
4463 // emit leal symbol@TLSGD(,%ebx,1), %eax
4464 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004465 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004466 GA->getValueType(0),
4467 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004468 SDValue Ops[] = { Chain, TGA, InFlag };
4469 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004470 InFlag = Result.getValue(2);
4471 Chain = Result.getValue(1);
4472
4473 // call ___tls_get_addr. This function receives its argument in
4474 // the register EAX.
4475 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4476 InFlag = Chain.getValue(1);
4477
4478 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004479 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004480 DAG.getTargetExternalSymbol("___tls_get_addr",
4481 PtrVT),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004482 DAG.getRegister(X86::EAX, PtrVT),
4483 DAG.getRegister(X86::EBX, PtrVT),
4484 InFlag };
4485 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4486 InFlag = Chain.getValue(1);
4487
4488 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4489}
4490
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004491// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004492static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004493LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004494 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004495 SDValue InFlag, Chain;
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004496
4497 // emit leaq symbol@TLSGD(%rip), %rdi
4498 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004499 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004500 GA->getValueType(0),
4501 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004502 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4503 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004504 Chain = Result.getValue(1);
4505 InFlag = Result.getValue(2);
4506
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004507 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004508 // the register RDI.
4509 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4510 InFlag = Chain.getValue(1);
4511
4512 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004513 SDValue Ops1[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00004514 DAG.getTargetExternalSymbol("__tls_get_addr",
4515 PtrVT),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004516 DAG.getRegister(X86::RDI, PtrVT),
4517 InFlag };
4518 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4519 InFlag = Chain.getValue(1);
4520
4521 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4522}
4523
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004524// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4525// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004526static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004527 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004528 // Get the Thread Pointer
Dan Gohman475871a2008-07-27 21:46:04 +00004529 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004530 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4531 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004532 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004533 GA->getValueType(0),
4534 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004535 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004536
4537 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman69de1932008-02-06 22:27:42 +00004538 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004539 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004540
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004541 // The address of the thread local variable is the add of the thread
4542 // pointer with the offset of the variable.
4543 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4544}
4545
Dan Gohman475871a2008-07-27 21:46:04 +00004546SDValue
4547X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004548 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004549 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004550 assert(Subtarget->isTargetELF() &&
4551 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004552 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4553 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4554 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004555 if (Subtarget->is64Bit()) {
4556 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4557 } else {
4558 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4559 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4560 else
4561 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4562 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004563}
4564
Dan Gohman475871a2008-07-27 21:46:04 +00004565SDValue
4566X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Bill Wendling056292f2008-09-16 21:48:12 +00004567 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
4568 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004569 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004570 // With PIC, the address is actually $g + Offset.
4571 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4572 !Subtarget->isPICStyleRIPRel()) {
4573 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4574 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4575 Result);
4576 }
4577
4578 return Result;
4579}
4580
Dan Gohman475871a2008-07-27 21:46:04 +00004581SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004582 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004583 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004584 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4585 // With PIC, the address is actually $g + Offset.
4586 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4587 !Subtarget->isPICStyleRIPRel()) {
4588 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4589 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4590 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004591 }
4592
4593 return Result;
4594}
4595
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004596/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4597/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004598SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004599 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004600 MVT VT = Op.getValueType();
4601 unsigned VTBits = VT.getSizeInBits();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004602 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004603 SDValue ShOpLo = Op.getOperand(0);
4604 SDValue ShOpHi = Op.getOperand(1);
4605 SDValue ShAmt = Op.getOperand(2);
4606 SDValue Tmp1 = isSRA ?
Dan Gohman4c1fa612008-03-03 22:22:09 +00004607 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4608 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004609
Dan Gohman475871a2008-07-27 21:46:04 +00004610 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004611 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004612 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4613 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004614 } else {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004615 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4616 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004617 }
Evan Chenge3413162006-01-09 18:33:28 +00004618
Dan Gohman475871a2008-07-27 21:46:04 +00004619 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004620 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00004621 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004622 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004623
Dan Gohman475871a2008-07-27 21:46:04 +00004624 SDValue Hi, Lo;
4625 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4626 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4627 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004628
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004629 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf9516202008-06-30 10:19:09 +00004630 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4631 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004632 } else {
Duncan Sandsf9516202008-06-30 10:19:09 +00004633 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4634 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004635 }
4636
Dan Gohman475871a2008-07-27 21:46:04 +00004637 SDValue Ops[2] = { Lo, Hi };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004638 return DAG.getMergeValues(Ops, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004639}
Evan Chenga3195e82006-01-12 22:54:21 +00004640
Dan Gohman475871a2008-07-27 21:46:04 +00004641SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004642 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004643 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004644 "Unknown SINT_TO_FP to lower!");
4645
4646 // These are really Legal; caller falls through into that case.
4647 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004648 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004649 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4650 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004651 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004652
Duncan Sands83ec4b62008-06-06 12:08:01 +00004653 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004654 MachineFunction &MF = DAG.getMachineFunction();
4655 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004656 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4657 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004658 StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004659 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004660
4661 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004662 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004663 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004664 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004665 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4666 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004667 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004668 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004669 Ops.push_back(Chain);
4670 Ops.push_back(StackSlot);
4671 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman475871a2008-07-27 21:46:04 +00004672 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004673 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004674
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004675 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004676 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004677 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004678
4679 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4680 // shouldn't be necessary except that RFP cannot be live across
4681 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004682 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004683 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004684 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004685 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004686 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004687 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004688 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004689 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004690 Ops.push_back(DAG.getValueType(Op.getValueType()));
4691 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004692 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman69de1932008-02-06 22:27:42 +00004693 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004694 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004695 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004696
Evan Cheng0db9fe62006-04-25 20:13:52 +00004697 return Result;
4698}
4699
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004700SDValue X86TargetLowering::LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
4701 MVT SrcVT = Op.getOperand(0).getValueType();
4702 assert(SrcVT.getSimpleVT() == MVT::i64 && "Unknown UINT_TO_FP to lower!");
4703
4704 // We only handle SSE2 f64 target here; caller can handle the rest.
4705 if (Op.getValueType() != MVT::f64 || !X86ScalarSSEf64)
4706 return SDValue();
4707
Dale Johannesen040225f2008-10-21 23:07:49 +00004708 // This algorithm is not obvious. Here it is in C code, more or less:
4709/*
4710 double uint64_to_double( uint32_t hi, uint32_t lo )
4711 {
4712 static const __m128i exp = { 0x4330000045300000ULL, 0 };
4713 static const __m128d bias = { 0x1.0p84, 0x1.0p52 };
4714
Dale Johannesen9ae0d6d2008-10-22 00:02:32 +00004715 // copy ints to xmm registers
Dale Johannesen040225f2008-10-21 23:07:49 +00004716 __m128i xh = _mm_cvtsi32_si128( hi );
4717 __m128i xl = _mm_cvtsi32_si128( lo );
4718
Dale Johannesen9ae0d6d2008-10-22 00:02:32 +00004719 // combine into low half of a single xmm register
Dale Johannesen040225f2008-10-21 23:07:49 +00004720 __m128i x = _mm_unpacklo_epi32( xh, xl );
4721 __m128d d;
4722 double sd;
4723
Dale Johannesen9ae0d6d2008-10-22 00:02:32 +00004724 // merge in appropriate exponents to give the integer bits the
Dale Johannesen040225f2008-10-21 23:07:49 +00004725 // right magnitude
4726 x = _mm_unpacklo_epi32( x, exp );
4727
Dale Johannesen9ae0d6d2008-10-22 00:02:32 +00004728 // subtract away the biases to deal with the IEEE-754 double precision
4729 // implicit 1
Dale Johannesen040225f2008-10-21 23:07:49 +00004730 d = _mm_sub_pd( (__m128d) x, bias );
4731
Dale Johannesen9ae0d6d2008-10-22 00:02:32 +00004732 // All conversions up to here are exact. The correctly rounded result is
Dale Johannesen040225f2008-10-21 23:07:49 +00004733 // calculated using the
Dale Johannesen9ae0d6d2008-10-22 00:02:32 +00004734 // current rounding mode using the following horizontal add.
Dale Johannesen040225f2008-10-21 23:07:49 +00004735 d = _mm_add_sd( d, _mm_unpackhi_pd( d, d ) );
4736 _mm_store_sd( &sd, d ); //since we are returning doubles in XMM, this
Dale Johannesen9ae0d6d2008-10-22 00:02:32 +00004737 // store doesn't really need to be here (except maybe to zero the other
4738 // double)
Dale Johannesen040225f2008-10-21 23:07:49 +00004739 return sd;
4740 }
4741*/
4742
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004743 // Build some magic constants.
4744 std::vector<Constant*>CV0;
4745 CV0.push_back(ConstantInt::get(APInt(32, 0x45300000)));
4746 CV0.push_back(ConstantInt::get(APInt(32, 0x43300000)));
4747 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4748 CV0.push_back(ConstantInt::get(APInt(32, 0)));
4749 Constant *C0 = ConstantVector::get(CV0);
4750 SDValue CPIdx0 = DAG.getConstantPool(C0, getPointerTy(), 4);
4751
4752 std::vector<Constant*>CV1;
4753 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4530000000000000ULL))));
4754 CV1.push_back(ConstantFP::get(APFloat(APInt(64, 0x4330000000000000ULL))));
4755 Constant *C1 = ConstantVector::get(CV1);
4756 SDValue CPIdx1 = DAG.getConstantPool(C1, getPointerTy(), 4);
4757
4758 SmallVector<SDValue, 4> MaskVec;
4759 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
4760 MaskVec.push_back(DAG.getConstant(4, MVT::i32));
4761 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
4762 MaskVec.push_back(DAG.getConstant(5, MVT::i32));
4763 SDValue UnpcklMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, &MaskVec[0],
4764 MaskVec.size());
4765 SmallVector<SDValue, 4> MaskVec2;
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004766 MaskVec2.push_back(DAG.getConstant(1, MVT::i32));
4767 MaskVec2.push_back(DAG.getConstant(0, MVT::i32));
4768 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec2[0],
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004769 MaskVec2.size());
4770
4771 SDValue XR1 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004772 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4773 Op.getOperand(0),
4774 DAG.getIntPtrConstant(1)));
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004775 SDValue XR2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v4i32,
Duncan Sands6b6aeb32008-10-22 11:24:12 +00004776 DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
4777 Op.getOperand(0),
4778 DAG.getIntPtrConstant(0)));
Dale Johannesen1c15bf52008-10-21 20:50:01 +00004779 SDValue Unpck1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4780 XR1, XR2, UnpcklMask);
4781 SDValue CLod0 = DAG.getLoad(MVT::v4i32, DAG.getEntryNode(), CPIdx0,
4782 PseudoSourceValue::getConstantPool(), 0, false, 16);
4783 SDValue Unpck2 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v4i32,
4784 Unpck1, CLod0, UnpcklMask);
4785 SDValue XR2F = DAG.getNode(ISD::BIT_CONVERT, MVT::v2f64, Unpck2);
4786 SDValue CLod1 = DAG.getLoad(MVT::v2f64, CLod0.getValue(1), CPIdx1,
4787 PseudoSourceValue::getConstantPool(), 0, false, 16);
4788 SDValue Sub = DAG.getNode(ISD::FSUB, MVT::v2f64, XR2F, CLod1);
4789 // Add the halves; easiest way is to swap them into another reg first.
4790 SDValue Shuf = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2f64,
4791 Sub, Sub, ShufMask);
4792 SDValue Add = DAG.getNode(ISD::FADD, MVT::v2f64, Shuf, Sub);
4793 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f64, Add,
4794 DAG.getIntPtrConstant(0));
4795}
4796
Dan Gohman475871a2008-07-27 21:46:04 +00004797std::pair<SDValue,SDValue> X86TargetLowering::
4798FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004799 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4800 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004801 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004802
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004803 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004804 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004805 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004806 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004807 if (Subtarget->is64Bit() &&
4808 Op.getValueType() == MVT::i64 &&
4809 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00004810 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004811
Evan Cheng87c89352007-10-15 20:11:21 +00004812 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4813 // stack slot.
4814 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004815 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004816 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004817 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004818 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004819 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004820 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4821 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4822 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4823 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004824 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004825
Dan Gohman475871a2008-07-27 21:46:04 +00004826 SDValue Chain = DAG.getEntryNode();
4827 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004828 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004829 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman69de1932008-02-06 22:27:42 +00004830 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004831 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004832 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004833 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004834 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4835 };
4836 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004837 Chain = Value.getValue(1);
4838 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4839 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4840 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004841
Evan Cheng0db9fe62006-04-25 20:13:52 +00004842 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004843 SDValue Ops[] = { Chain, Value, StackSlot };
4844 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004845
Chris Lattner27a6c732007-11-24 07:07:01 +00004846 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004847}
4848
Dan Gohman475871a2008-07-27 21:46:04 +00004849SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4850 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4851 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00004852 if (FIST.getNode() == 0) return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00004853
4854 // Load the result.
4855 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4856}
4857
4858SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00004859 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4860 SDValue FIST = Vals.first, StackSlot = Vals.second;
Gabor Greifba36cb52008-08-28 21:40:38 +00004861 if (FIST.getNode() == 0) return 0;
Duncan Sandsf9516202008-06-30 10:19:09 +00004862
4863 MVT VT = N->getValueType(0);
4864
4865 // Return a load from the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004866 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004867
Duncan Sands4bdcb612008-07-02 17:40:58 +00004868 // Use MERGE_VALUES to drop the chain result value and get a node with one
4869 // result. This requires turning off getMergeValues simplification, since
4870 // otherwise it will give us Res back.
Gabor Greifba36cb52008-08-28 21:40:38 +00004871 return DAG.getMergeValues(&Res, 1, false).getNode();
Duncan Sandsf9516202008-06-30 10:19:09 +00004872}
Chris Lattner27a6c732007-11-24 07:07:01 +00004873
Dan Gohman475871a2008-07-27 21:46:04 +00004874SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004875 MVT VT = Op.getValueType();
4876 MVT EltVT = VT;
4877 if (VT.isVector())
4878 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004879 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004880 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004881 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004882 CV.push_back(C);
4883 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004884 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004885 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004886 CV.push_back(C);
4887 CV.push_back(C);
4888 CV.push_back(C);
4889 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004890 }
Dan Gohmand3006222007-07-27 17:16:43 +00004891 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004892 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4893 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004894 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004895 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004896 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4897}
4898
Dan Gohman475871a2008-07-27 21:46:04 +00004899SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004900 MVT VT = Op.getValueType();
4901 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004902 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004903 if (VT.isVector()) {
4904 EltVT = VT.getVectorElementType();
4905 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004906 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004907 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004908 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004909 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004910 CV.push_back(C);
4911 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004912 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004913 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004914 CV.push_back(C);
4915 CV.push_back(C);
4916 CV.push_back(C);
4917 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004918 }
Dan Gohmand3006222007-07-27 17:16:43 +00004919 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004920 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4921 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004922 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004923 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004924 if (VT.isVector()) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004925 return DAG.getNode(ISD::BIT_CONVERT, VT,
4926 DAG.getNode(ISD::XOR, MVT::v2i64,
4927 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4928 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4929 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004930 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4931 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004932}
4933
Dan Gohman475871a2008-07-27 21:46:04 +00004934SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4935 SDValue Op0 = Op.getOperand(0);
4936 SDValue Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004937 MVT VT = Op.getValueType();
4938 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004939
4940 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004941 if (SrcVT.bitsLT(VT)) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004942 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4943 SrcVT = VT;
4944 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004945 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004946 if (SrcVT.bitsGT(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004947 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004948 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004949 }
4950
4951 // At this point the operands and the result should have the same
4952 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004953
Evan Cheng68c47cb2007-01-05 07:55:56 +00004954 // First get the sign bit of second operand.
4955 std::vector<Constant*> CV;
4956 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004957 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4958 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004959 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004960 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4961 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4962 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4963 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004964 }
Dan Gohmand3006222007-07-27 17:16:43 +00004965 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004966 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4967 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004968 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004969 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004970 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004971
4972 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004973 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004974 // Op0 is MVT::f32, Op1 is MVT::f64.
4975 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4976 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4977 DAG.getConstant(32, MVT::i32));
4978 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4979 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004980 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004981 }
4982
Evan Cheng73d6cf12007-01-05 21:37:56 +00004983 // Clear first operand sign bit.
4984 CV.clear();
4985 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004986 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4987 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004988 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004989 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4990 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4991 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4992 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004993 }
Dan Gohmand3006222007-07-27 17:16:43 +00004994 C = ConstantVector::get(CV);
4995 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman475871a2008-07-27 21:46:04 +00004996 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004997 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004998 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004999 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00005000
5001 // Or the value with the sign bit.
5002 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005003}
5004
Dan Gohman475871a2008-07-27 21:46:04 +00005005SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00005006 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00005007 SDValue Cond;
5008 SDValue Op0 = Op.getOperand(0);
5009 SDValue Op1 = Op.getOperand(1);
5010 SDValue CC = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005011 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng0488db92007-09-25 01:57:46 +00005012 unsigned X86CC;
5013
Evan Cheng0488db92007-09-25 01:57:46 +00005014 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00005015 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00005016 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
5017 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00005018 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00005019 }
Evan Cheng0488db92007-09-25 01:57:46 +00005020
Evan Cheng7f042682008-10-15 02:05:31 +00005021 assert(0 && "Illegal SetCC!");
5022 return SDValue();
Evan Cheng0488db92007-09-25 01:57:46 +00005023}
5024
Dan Gohman475871a2008-07-27 21:46:04 +00005025SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
5026 SDValue Cond;
5027 SDValue Op0 = Op.getOperand(0);
5028 SDValue Op1 = Op.getOperand(1);
5029 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00005030 MVT VT = Op.getValueType();
5031 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
5032 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
5033
5034 if (isFP) {
5035 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00005036 MVT VT0 = Op0.getValueType();
5037 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
5038 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00005039 bool Swap = false;
5040
5041 switch (SetCCOpcode) {
5042 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005043 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00005044 case ISD::SETEQ: SSECC = 0; break;
5045 case ISD::SETOGT:
5046 case ISD::SETGT: Swap = true; // Fallthrough
5047 case ISD::SETLT:
5048 case ISD::SETOLT: SSECC = 1; break;
5049 case ISD::SETOGE:
5050 case ISD::SETGE: Swap = true; // Fallthrough
5051 case ISD::SETLE:
5052 case ISD::SETOLE: SSECC = 2; break;
5053 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005054 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00005055 case ISD::SETNE: SSECC = 4; break;
5056 case ISD::SETULE: Swap = true;
5057 case ISD::SETUGE: SSECC = 5; break;
5058 case ISD::SETULT: Swap = true;
5059 case ISD::SETUGT: SSECC = 6; break;
5060 case ISD::SETO: SSECC = 7; break;
5061 }
5062 if (Swap)
5063 std::swap(Op0, Op1);
5064
Nate Begemanfb8ead02008-07-25 19:05:58 +00005065 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00005066 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00005067 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00005068 SDValue UNORD, EQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005069 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
5070 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
5071 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
5072 }
5073 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00005074 SDValue ORD, NEQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00005075 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
5076 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
5077 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
5078 }
5079 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00005080 }
5081 // Handle all other FP comparisons here.
5082 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
5083 }
5084
5085 // We are handling one of the integer comparisons here. Since SSE only has
5086 // GT and EQ comparisons for integer, swapping operands and multiple
5087 // operations may be required for some comparisons.
5088 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
5089 bool Swap = false, Invert = false, FlipSigns = false;
5090
5091 switch (VT.getSimpleVT()) {
5092 default: break;
5093 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
5094 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
5095 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
5096 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
5097 }
5098
5099 switch (SetCCOpcode) {
5100 default: break;
5101 case ISD::SETNE: Invert = true;
5102 case ISD::SETEQ: Opc = EQOpc; break;
5103 case ISD::SETLT: Swap = true;
5104 case ISD::SETGT: Opc = GTOpc; break;
5105 case ISD::SETGE: Swap = true;
5106 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
5107 case ISD::SETULT: Swap = true;
5108 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
5109 case ISD::SETUGE: Swap = true;
5110 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
5111 }
5112 if (Swap)
5113 std::swap(Op0, Op1);
5114
5115 // Since SSE has no unsigned integer comparisons, we need to flip the sign
5116 // bits of the inputs before performing those operations.
5117 if (FlipSigns) {
5118 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005119 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
5120 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
5121 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00005122 SignBits.size());
5123 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
5124 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
5125 }
5126
Dan Gohman475871a2008-07-27 21:46:04 +00005127 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00005128
5129 // If the logical-not of the result is required, perform that now.
5130 if (Invert) {
5131 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00005132 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
5133 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
5134 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00005135 NegOnes.size());
5136 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
5137 }
5138 return Result;
5139}
Evan Cheng0488db92007-09-25 01:57:46 +00005140
Dan Gohman475871a2008-07-27 21:46:04 +00005141SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005142 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005143 SDValue Cond = Op.getOperand(0);
5144 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00005145
Evan Cheng734503b2006-09-11 02:19:56 +00005146 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005147 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005148
Evan Cheng3f41d662007-10-08 22:16:29 +00005149 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5150 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00005151 if (Cond.getOpcode() == X86ISD::SETCC) {
5152 CC = Cond.getOperand(0);
5153
Dan Gohman475871a2008-07-27 21:46:04 +00005154 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005155 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005156 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00005157
Evan Cheng3f41d662007-10-08 22:16:29 +00005158 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005159 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00005160 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Dan Gohman7810bfe2008-09-26 21:54:37 +00005161 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSExtValue());
Chris Lattner1956d152008-01-16 06:19:45 +00005162
Evan Chenge5f62042007-09-29 00:00:36 +00005163 if ((Opc == X86ISD::CMP ||
5164 Opc == X86ISD::COMI ||
5165 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005166 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005167 addTest = false;
5168 }
5169 }
5170
5171 if (addTest) {
5172 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00005173 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00005174 }
5175
Duncan Sands83ec4b62008-06-06 12:08:01 +00005176 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00005177 MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005178 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00005179 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
5180 // condition is true.
5181 Ops.push_back(Op.getOperand(2));
5182 Ops.push_back(Op.getOperand(1));
5183 Ops.push_back(CC);
5184 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00005185 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00005186}
5187
Dan Gohman475871a2008-07-27 21:46:04 +00005188SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00005189 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00005190 SDValue Chain = Op.getOperand(0);
5191 SDValue Cond = Op.getOperand(1);
5192 SDValue Dest = Op.getOperand(2);
5193 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00005194
Evan Cheng0db9fe62006-04-25 20:13:52 +00005195 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00005196 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005197
Evan Cheng3f41d662007-10-08 22:16:29 +00005198 // If condition flag is set by a X86ISD::CMP, then use it as the condition
5199 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005200 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00005201 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005202
Dan Gohman475871a2008-07-27 21:46:04 +00005203 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00005204 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00005205 if (Opc == X86ISD::CMP ||
5206 Opc == X86ISD::COMI ||
5207 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00005208 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00005209 addTest = false;
5210 }
Dan Gohman279c22e2008-10-21 03:29:32 +00005211 // Also, recognize the pattern generated by an FCMP_UNE. We can emit
5212 // two branches instead of an explicit OR instruction with a
5213 // separate test.
5214 } else if (Cond.getOpcode() == ISD::OR &&
5215 Cond.hasOneUse() &&
5216 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5217 Cond.getOperand(0).hasOneUse() &&
5218 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5219 Cond.getOperand(1).hasOneUse()) {
5220 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5221 unsigned Opc = Cmp.getOpcode();
5222 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5223 (Opc == X86ISD::CMP ||
5224 Opc == X86ISD::COMI ||
5225 Opc == X86ISD::UCOMI)) {
5226 CC = Cond.getOperand(0).getOperand(0);
5227 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5228 Chain, Dest, CC, Cmp);
5229 CC = Cond.getOperand(1).getOperand(0);
5230 Cond = Cmp;
5231 addTest = false;
5232 }
5233 // Also, recognize the pattern generated by an FCMP_OEQ. We can emit
5234 // two branches instead of an explicit AND instruction with a
5235 // separate test. However, we only do this if this block doesn't
5236 // have a fall-through edge, because this requires an explicit
5237 // jmp when the condition is false.
5238 } else if (Cond.getOpcode() == ISD::AND &&
5239 Cond.hasOneUse() &&
5240 Cond.getOperand(0).getOpcode() == X86ISD::SETCC &&
5241 Cond.getOperand(0).hasOneUse() &&
5242 Cond.getOperand(1).getOpcode() == X86ISD::SETCC &&
5243 Cond.getOperand(1).hasOneUse()) {
5244 SDValue Cmp = Cond.getOperand(0).getOperand(1);
5245 unsigned Opc = Cmp.getOpcode();
5246 if (Cmp == Cond.getOperand(1).getOperand(1) &&
5247 (Opc == X86ISD::CMP ||
5248 Opc == X86ISD::COMI ||
5249 Opc == X86ISD::UCOMI) &&
5250 Op.getNode()->hasOneUse()) {
5251 X86::CondCode CCode =
5252 (X86::CondCode)Cond.getOperand(0).getConstantOperandVal(0);
5253 CCode = X86::GetOppositeBranchCondition(CCode);
5254 CC = DAG.getConstant(CCode, MVT::i8);
5255 SDValue User = SDValue(*Op.getNode()->use_begin(), 0);
5256 // Look for an unconditional branch following this conditional branch.
5257 // We need this because we need to reverse the successors in order
5258 // to implement FCMP_OEQ.
5259 if (User.getOpcode() == ISD::BR) {
5260 SDValue FalseBB = User.getOperand(1);
5261 SDValue NewBR =
5262 DAG.UpdateNodeOperands(User, User.getOperand(0), Dest);
5263 assert(NewBR == User);
5264 Dest = FalseBB;
5265
5266 Chain = DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
5267 Chain, Dest, CC, Cmp);
5268 X86::CondCode CCode =
5269 (X86::CondCode)Cond.getOperand(1).getConstantOperandVal(0);
5270 CCode = X86::GetOppositeBranchCondition(CCode);
5271 CC = DAG.getConstant(CCode, MVT::i8);
5272 Cond = Cmp;
5273 addTest = false;
5274 }
5275 }
Evan Cheng0488db92007-09-25 01:57:46 +00005276 }
5277
5278 if (addTest) {
5279 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00005280 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00005281 }
Evan Chenge5f62042007-09-29 00:00:36 +00005282 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Dan Gohman279c22e2008-10-21 03:29:32 +00005283 Chain, Dest, CC, Cond);
Evan Cheng0488db92007-09-25 01:57:46 +00005284}
5285
Anton Korobeynikove060b532007-04-17 19:34:00 +00005286
5287// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
5288// Calls to _alloca is needed to probe the stack when allocating more than 4k
5289// bytes in one go. Touching the stack at 4K increments is necessary to ensure
5290// that the guard pages used by the OS virtual memory manager are allocated in
5291// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00005292SDValue
5293X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005294 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00005295 assert(Subtarget->isTargetCygMing() &&
5296 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005297
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005298 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00005299 SDValue Chain = Op.getOperand(0);
5300 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005301 // FIXME: Ensure alignment here
5302
Dan Gohman475871a2008-07-27 21:46:04 +00005303 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005304
Duncan Sands83ec4b62008-06-06 12:08:01 +00005305 MVT IntPtr = getPointerTy();
5306 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005307
Chris Lattnere563bbc2008-10-11 22:08:30 +00005308 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0, true));
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005309
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005310 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
5311 Flag = Chain.getValue(1);
5312
5313 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005314 SDValue Ops[] = { Chain,
Bill Wendling056292f2008-09-16 21:48:12 +00005315 DAG.getTargetExternalSymbol("_alloca", IntPtr),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005316 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005317 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005318 Flag };
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005319 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005320 Flag = Chain.getValue(1);
5321
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005322 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattnere563bbc2008-10-11 22:08:30 +00005323 DAG.getIntPtrConstant(0, true),
5324 DAG.getIntPtrConstant(0, true),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005325 Flag);
5326
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005327 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005328
Dan Gohman475871a2008-07-27 21:46:04 +00005329 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005330 return DAG.getMergeValues(Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005331}
5332
Dan Gohman475871a2008-07-27 21:46:04 +00005333SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005334X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Bill Wendling6f287b22008-09-30 21:22:07 +00005335 SDValue Chain,
5336 SDValue Dst, SDValue Src,
5337 SDValue Size, unsigned Align,
5338 const Value *DstSV,
Bill Wendling6158d842008-10-01 00:59:58 +00005339 uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005340 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005341
Bill Wendling6f287b22008-09-30 21:22:07 +00005342 // If not DWORD aligned or size is more than the threshold, call the library.
5343 // The libc version is likely to be faster for these cases. It can use the
5344 // address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005345 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005346 !ConstantSize ||
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005347 ConstantSize->getZExtValue() >
5348 getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005349 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005350
5351 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005352 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
Bill Wendling6f287b22008-09-30 21:22:07 +00005353
Bill Wendling6158d842008-10-01 00:59:58 +00005354 if (const char *bzeroEntry = V &&
5355 V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
5356 MVT IntPtr = getPointerTy();
5357 const Type *IntPtrTy = TD->getIntPtrType();
5358 TargetLowering::ArgListTy Args;
5359 TargetLowering::ArgListEntry Entry;
5360 Entry.Node = Dst;
5361 Entry.Ty = IntPtrTy;
5362 Args.push_back(Entry);
5363 Entry.Node = Size;
5364 Args.push_back(Entry);
5365 std::pair<SDValue,SDValue> CallResult =
5366 LowerCallTo(Chain, Type::VoidTy, false, false, false, false,
5367 CallingConv::C, false,
5368 DAG.getExternalSymbol(bzeroEntry, IntPtr), Args, DAG);
5369 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005370 }
5371
Dan Gohman707e0182008-04-12 04:36:06 +00005372 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005373 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005374 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005375
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005376 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005377 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005378 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005379 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005380 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005381 unsigned BytesLeft = 0;
5382 bool TwoRepStos = false;
5383 if (ValC) {
5384 unsigned ValReg;
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005385 uint64_t Val = ValC->getZExtValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005386
Evan Cheng0db9fe62006-04-25 20:13:52 +00005387 // If the value is a constant, then we can potentially use larger sets.
5388 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005389 case 2: // WORD aligned
5390 AVT = MVT::i16;
5391 ValReg = X86::AX;
5392 Val = (Val << 8) | Val;
5393 break;
5394 case 0: // DWORD aligned
5395 AVT = MVT::i32;
5396 ValReg = X86::EAX;
5397 Val = (Val << 8) | Val;
5398 Val = (Val << 16) | Val;
5399 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5400 AVT = MVT::i64;
5401 ValReg = X86::RAX;
5402 Val = (Val << 32) | Val;
5403 }
5404 break;
5405 default: // Byte aligned
5406 AVT = MVT::i8;
5407 ValReg = X86::AL;
5408 Count = DAG.getIntPtrConstant(SizeVal);
5409 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005410 }
5411
Duncan Sands8e4eb092008-06-08 20:54:56 +00005412 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005413 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005414 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5415 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005416 }
5417
Evan Cheng0db9fe62006-04-25 20:13:52 +00005418 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5419 InFlag);
5420 InFlag = Chain.getValue(1);
5421 } else {
5422 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005423 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohman707e0182008-04-12 04:36:06 +00005424 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005425 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005426 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005427
Evan Cheng25ab6902006-09-08 06:48:29 +00005428 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5429 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005430 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005431 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005432 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005433 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005434
Chris Lattnerd96d0722007-02-25 06:40:16 +00005435 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005436 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005437 Ops.push_back(Chain);
5438 Ops.push_back(DAG.getValueType(AVT));
5439 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005440 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005441
Evan Cheng0db9fe62006-04-25 20:13:52 +00005442 if (TwoRepStos) {
5443 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005444 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005445 MVT CVT = Count.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005446 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005447 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5448 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5449 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005450 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005451 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005452 Ops.clear();
5453 Ops.push_back(Chain);
5454 Ops.push_back(DAG.getValueType(MVT::i8));
5455 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005456 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005457 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005458 // Handle the last 1 - 7 bytes.
5459 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005460 MVT AddrVT = Dst.getValueType();
5461 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005462
5463 Chain = DAG.getMemset(Chain,
5464 DAG.getNode(ISD::ADD, AddrVT, Dst,
5465 DAG.getConstant(Offset, AddrVT)),
5466 Src,
5467 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005468 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005469 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005470
Dan Gohman707e0182008-04-12 04:36:06 +00005471 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005472 return Chain;
5473}
Evan Cheng11e15b32006-04-03 20:53:28 +00005474
Dan Gohman475871a2008-07-27 21:46:04 +00005475SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005476X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005477 SDValue Chain, SDValue Dst, SDValue Src,
5478 SDValue Size, unsigned Align,
5479 bool AlwaysInline,
5480 const Value *DstSV, uint64_t DstSVOff,
5481 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005482 // This requires the copy size to be a constant, preferrably
5483 // within a subtarget-specific limit.
5484 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5485 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005486 return SDValue();
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005487 uint64_t SizeVal = ConstantSize->getZExtValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005488 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005489 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005490
Evan Cheng1887c1c2008-08-21 21:00:15 +00005491 /// If not DWORD aligned, call the library.
5492 if ((Align & 3) != 0)
5493 return SDValue();
5494
5495 // DWORD aligned
5496 MVT AVT = MVT::i32;
5497 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005498 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005499
Duncan Sands83ec4b62008-06-06 12:08:01 +00005500 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005501 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005502 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005503 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005504
Dan Gohman475871a2008-07-27 21:46:04 +00005505 SDValue InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005506 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5507 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005508 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005509 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005510 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005511 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005512 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005513 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005514 InFlag = Chain.getValue(1);
5515
Chris Lattnerd96d0722007-02-25 06:40:16 +00005516 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005517 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005518 Ops.push_back(Chain);
5519 Ops.push_back(DAG.getValueType(AVT));
5520 Ops.push_back(InFlag);
Dan Gohman475871a2008-07-27 21:46:04 +00005521 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005522
Dan Gohman475871a2008-07-27 21:46:04 +00005523 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005524 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005525 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005526 // Handle the last 1 - 7 bytes.
5527 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005528 MVT DstVT = Dst.getValueType();
5529 MVT SrcVT = Src.getValueType();
5530 MVT SizeVT = Size.getValueType();
Evan Cheng2749c722008-04-25 00:26:43 +00005531 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00005532 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005533 DAG.getConstant(Offset, DstVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005534 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005535 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005536 DAG.getConstant(BytesLeft, SizeVT),
5537 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005538 DstSV, DstSVOff + Offset,
5539 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005540 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005541
Dan Gohman707e0182008-04-12 04:36:06 +00005542 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005543}
5544
Chris Lattner27a6c732007-11-24 07:07:01 +00005545/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5546SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00005547 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005548 SDValue TheChain = N->getOperand(0);
5549 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005550 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005551 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5552 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005553 MVT::i64, rax.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00005554 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005555 DAG.getConstant(32, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00005556 SDValue Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00005557 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00005558 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00005559
Gabor Greifba36cb52008-08-28 21:40:38 +00005560 return DAG.getMergeValues(Ops, 2).getNode();
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005561 }
Chris Lattner5a88b832007-02-25 07:10:00 +00005562
Dan Gohman475871a2008-07-27 21:46:04 +00005563 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5564 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005565 MVT::i32, eax.getValue(2));
5566 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman475871a2008-07-27 21:46:04 +00005567 SDValue Ops[] = { eax, edx };
Chris Lattner27a6c732007-11-24 07:07:01 +00005568 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5569
5570 // Use a MERGE_VALUES to return the value and chain.
5571 Ops[1] = edx.getValue(1);
Gabor Greifba36cb52008-08-28 21:40:38 +00005572 return DAG.getMergeValues(Ops, 2).getNode();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005573}
5574
Dan Gohman475871a2008-07-27 21:46:04 +00005575SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005576 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005577
Evan Cheng25ab6902006-09-08 06:48:29 +00005578 if (!Subtarget->is64Bit()) {
5579 // vastart just stores the address of the VarArgsFrameIndex slot into the
5580 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005581 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005582 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005583 }
5584
5585 // __va_list_tag:
5586 // gp_offset (0 - 6 * 8)
5587 // fp_offset (48 - 48 + 8 * 16)
5588 // overflow_arg_area (point to parameters coming in memory).
5589 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005590 SmallVector<SDValue, 8> MemOps;
5591 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005592 // Store gp_offset
Dan Gohman475871a2008-07-27 21:46:04 +00005593 SDValue Store = DAG.getStore(Op.getOperand(0),
Evan Cheng786225a2006-10-05 23:01:46 +00005594 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005595 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005596 MemOps.push_back(Store);
5597
5598 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00005599 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00005600 Store = DAG.getStore(Op.getOperand(0),
5601 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005602 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005603 MemOps.push_back(Store);
5604
5605 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00005606 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005607 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005608 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005609 MemOps.push_back(Store);
5610
5611 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00005612 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005613 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005614 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005615 MemOps.push_back(Store);
5616 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005617}
5618
Dan Gohman475871a2008-07-27 21:46:04 +00005619SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005620 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5621 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005622 SDValue Chain = Op.getOperand(0);
5623 SDValue SrcPtr = Op.getOperand(1);
5624 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005625
5626 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5627 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005628 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005629}
5630
Dan Gohman475871a2008-07-27 21:46:04 +00005631SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005632 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005633 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005634 SDValue Chain = Op.getOperand(0);
5635 SDValue DstPtr = Op.getOperand(1);
5636 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005637 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5638 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengae642192007-03-02 23:16:35 +00005639
Dan Gohman28269132008-04-18 20:55:41 +00005640 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5641 DAG.getIntPtrConstant(24), 8, false,
5642 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005643}
5644
Dan Gohman475871a2008-07-27 21:46:04 +00005645SDValue
5646X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005647 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00005648 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005649 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005650 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005651 case Intrinsic::x86_sse_comieq_ss:
5652 case Intrinsic::x86_sse_comilt_ss:
5653 case Intrinsic::x86_sse_comile_ss:
5654 case Intrinsic::x86_sse_comigt_ss:
5655 case Intrinsic::x86_sse_comige_ss:
5656 case Intrinsic::x86_sse_comineq_ss:
5657 case Intrinsic::x86_sse_ucomieq_ss:
5658 case Intrinsic::x86_sse_ucomilt_ss:
5659 case Intrinsic::x86_sse_ucomile_ss:
5660 case Intrinsic::x86_sse_ucomigt_ss:
5661 case Intrinsic::x86_sse_ucomige_ss:
5662 case Intrinsic::x86_sse_ucomineq_ss:
5663 case Intrinsic::x86_sse2_comieq_sd:
5664 case Intrinsic::x86_sse2_comilt_sd:
5665 case Intrinsic::x86_sse2_comile_sd:
5666 case Intrinsic::x86_sse2_comigt_sd:
5667 case Intrinsic::x86_sse2_comige_sd:
5668 case Intrinsic::x86_sse2_comineq_sd:
5669 case Intrinsic::x86_sse2_ucomieq_sd:
5670 case Intrinsic::x86_sse2_ucomilt_sd:
5671 case Intrinsic::x86_sse2_ucomile_sd:
5672 case Intrinsic::x86_sse2_ucomigt_sd:
5673 case Intrinsic::x86_sse2_ucomige_sd:
5674 case Intrinsic::x86_sse2_ucomineq_sd: {
5675 unsigned Opc = 0;
5676 ISD::CondCode CC = ISD::SETCC_INVALID;
5677 switch (IntNo) {
5678 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005679 case Intrinsic::x86_sse_comieq_ss:
5680 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005681 Opc = X86ISD::COMI;
5682 CC = ISD::SETEQ;
5683 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005684 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005685 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005686 Opc = X86ISD::COMI;
5687 CC = ISD::SETLT;
5688 break;
5689 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005690 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005691 Opc = X86ISD::COMI;
5692 CC = ISD::SETLE;
5693 break;
5694 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005695 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005696 Opc = X86ISD::COMI;
5697 CC = ISD::SETGT;
5698 break;
5699 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005700 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005701 Opc = X86ISD::COMI;
5702 CC = ISD::SETGE;
5703 break;
5704 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005705 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005706 Opc = X86ISD::COMI;
5707 CC = ISD::SETNE;
5708 break;
5709 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005710 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005711 Opc = X86ISD::UCOMI;
5712 CC = ISD::SETEQ;
5713 break;
5714 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005715 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005716 Opc = X86ISD::UCOMI;
5717 CC = ISD::SETLT;
5718 break;
5719 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005720 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005721 Opc = X86ISD::UCOMI;
5722 CC = ISD::SETLE;
5723 break;
5724 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005725 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005726 Opc = X86ISD::UCOMI;
5727 CC = ISD::SETGT;
5728 break;
5729 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005730 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005731 Opc = X86ISD::UCOMI;
5732 CC = ISD::SETGE;
5733 break;
5734 case Intrinsic::x86_sse_ucomineq_ss:
5735 case Intrinsic::x86_sse2_ucomineq_sd:
5736 Opc = X86ISD::UCOMI;
5737 CC = ISD::SETNE;
5738 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005739 }
Evan Cheng734503b2006-09-11 02:19:56 +00005740
Evan Cheng0db9fe62006-04-25 20:13:52 +00005741 unsigned X86CC;
Dan Gohman475871a2008-07-27 21:46:04 +00005742 SDValue LHS = Op.getOperand(1);
5743 SDValue RHS = Op.getOperand(2);
Chris Lattnerf9570512006-09-13 03:22:10 +00005744 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005745
Dan Gohman475871a2008-07-27 21:46:04 +00005746 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5747 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005748 DAG.getConstant(X86CC, MVT::i8), Cond);
5749 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005750 }
Evan Cheng5759f972008-05-04 09:15:50 +00005751
5752 // Fix vector shift instructions where the last operand is a non-immediate
5753 // i32 value.
5754 case Intrinsic::x86_sse2_pslli_w:
5755 case Intrinsic::x86_sse2_pslli_d:
5756 case Intrinsic::x86_sse2_pslli_q:
5757 case Intrinsic::x86_sse2_psrli_w:
5758 case Intrinsic::x86_sse2_psrli_d:
5759 case Intrinsic::x86_sse2_psrli_q:
5760 case Intrinsic::x86_sse2_psrai_w:
5761 case Intrinsic::x86_sse2_psrai_d:
5762 case Intrinsic::x86_mmx_pslli_w:
5763 case Intrinsic::x86_mmx_pslli_d:
5764 case Intrinsic::x86_mmx_pslli_q:
5765 case Intrinsic::x86_mmx_psrli_w:
5766 case Intrinsic::x86_mmx_psrli_d:
5767 case Intrinsic::x86_mmx_psrli_q:
5768 case Intrinsic::x86_mmx_psrai_w:
5769 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005770 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005771 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005772 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005773
5774 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005775 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005776 switch (IntNo) {
5777 case Intrinsic::x86_sse2_pslli_w:
5778 NewIntNo = Intrinsic::x86_sse2_psll_w;
5779 break;
5780 case Intrinsic::x86_sse2_pslli_d:
5781 NewIntNo = Intrinsic::x86_sse2_psll_d;
5782 break;
5783 case Intrinsic::x86_sse2_pslli_q:
5784 NewIntNo = Intrinsic::x86_sse2_psll_q;
5785 break;
5786 case Intrinsic::x86_sse2_psrli_w:
5787 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5788 break;
5789 case Intrinsic::x86_sse2_psrli_d:
5790 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5791 break;
5792 case Intrinsic::x86_sse2_psrli_q:
5793 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5794 break;
5795 case Intrinsic::x86_sse2_psrai_w:
5796 NewIntNo = Intrinsic::x86_sse2_psra_w;
5797 break;
5798 case Intrinsic::x86_sse2_psrai_d:
5799 NewIntNo = Intrinsic::x86_sse2_psra_d;
5800 break;
5801 default: {
5802 ShAmtVT = MVT::v2i32;
5803 switch (IntNo) {
5804 case Intrinsic::x86_mmx_pslli_w:
5805 NewIntNo = Intrinsic::x86_mmx_psll_w;
5806 break;
5807 case Intrinsic::x86_mmx_pslli_d:
5808 NewIntNo = Intrinsic::x86_mmx_psll_d;
5809 break;
5810 case Intrinsic::x86_mmx_pslli_q:
5811 NewIntNo = Intrinsic::x86_mmx_psll_q;
5812 break;
5813 case Intrinsic::x86_mmx_psrli_w:
5814 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5815 break;
5816 case Intrinsic::x86_mmx_psrli_d:
5817 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5818 break;
5819 case Intrinsic::x86_mmx_psrli_q:
5820 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5821 break;
5822 case Intrinsic::x86_mmx_psrai_w:
5823 NewIntNo = Intrinsic::x86_mmx_psra_w;
5824 break;
5825 case Intrinsic::x86_mmx_psrai_d:
5826 NewIntNo = Intrinsic::x86_mmx_psra_d;
5827 break;
5828 default: abort(); // Can't reach here.
5829 }
5830 break;
5831 }
5832 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00005833 MVT VT = Op.getValueType();
Evan Cheng5759f972008-05-04 09:15:50 +00005834 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5835 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5836 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5837 DAG.getConstant(NewIntNo, MVT::i32),
5838 Op.getOperand(1), ShAmt);
5839 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005840 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005841}
Evan Cheng72261582005-12-20 06:22:03 +00005842
Dan Gohman475871a2008-07-27 21:46:04 +00005843SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Nate Begemanbcc5f362007-01-29 22:58:52 +00005844 // Depths > 0 not supported yet!
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00005845 if (cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005846 return SDValue();
Nate Begemanbcc5f362007-01-29 22:58:52 +00005847
5848 // Just load the return address
Dan Gohman475871a2008-07-27 21:46:04 +00005849 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005850 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5851}
5852
Dan Gohman475871a2008-07-27 21:46:04 +00005853SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng184793f2008-09-27 01:56:22 +00005854 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
5855 MFI->setFrameAddressIsTaken(true);
5856 MVT VT = Op.getValueType();
5857 unsigned Depth = cast<ConstantSDNode>(Op.getOperand(0))->getZExtValue();
5858 unsigned FrameReg = Subtarget->is64Bit() ? X86::RBP : X86::EBP;
5859 SDValue FrameAddr = DAG.getCopyFromReg(DAG.getEntryNode(), FrameReg, VT);
5860 while (Depth--)
5861 FrameAddr = DAG.getLoad(VT, DAG.getEntryNode(), FrameAddr, NULL, 0);
5862 return FrameAddr;
Nate Begemanbcc5f362007-01-29 22:58:52 +00005863}
5864
Dan Gohman475871a2008-07-27 21:46:04 +00005865SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov260a6b82008-09-08 21:12:11 +00005866 SelectionDAG &DAG) {
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005867 return DAG.getIntPtrConstant(2*TD->getPointerSize());
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005868}
5869
Dan Gohman475871a2008-07-27 21:46:04 +00005870SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005871{
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005872 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00005873 SDValue Chain = Op.getOperand(0);
5874 SDValue Offset = Op.getOperand(1);
5875 SDValue Handler = Op.getOperand(2);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005876
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00005877 SDValue Frame = DAG.getRegister(Subtarget->is64Bit() ? X86::RBP : X86::EBP,
5878 getPointerTy());
5879 unsigned StoreAddrReg = (Subtarget->is64Bit() ? X86::RCX : X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005880
Dan Gohman475871a2008-07-27 21:46:04 +00005881 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005882 DAG.getIntPtrConstant(-TD->getPointerSize()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005883 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5884 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00005885 Chain = DAG.getCopyToReg(Chain, StoreAddrReg, StoreAddr);
5886 MF.getRegInfo().addLiveOut(StoreAddrReg);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005887
Anton Korobeynikovb84c1672008-09-08 21:12:47 +00005888 return DAG.getNode(X86ISD::EH_RETURN,
5889 MVT::Other,
5890 Chain, DAG.getRegister(StoreAddrReg, getPointerTy()));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005891}
5892
Dan Gohman475871a2008-07-27 21:46:04 +00005893SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00005894 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00005895 SDValue Root = Op.getOperand(0);
5896 SDValue Trmp = Op.getOperand(1); // trampoline
5897 SDValue FPtr = Op.getOperand(2); // nested function
5898 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsb116fac2007-07-27 20:02:49 +00005899
Dan Gohman69de1932008-02-06 22:27:42 +00005900 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005901
Duncan Sands339e14f2008-01-16 22:55:25 +00005902 const X86InstrInfo *TII =
5903 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5904
Duncan Sandsb116fac2007-07-27 20:02:49 +00005905 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005906 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00005907
5908 // Large code-model.
5909
5910 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5911 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5912
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005913 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5914 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00005915
5916 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5917
5918 // Load the pointer to the nested function into R11.
5919 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00005920 SDValue Addr = Trmp;
Duncan Sands339e14f2008-01-16 22:55:25 +00005921 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005922 TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00005923
5924 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005925 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005926
5927 // Load the 'nest' parameter value into R10.
5928 // R10 is specified in X86CallingConv.td
5929 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5930 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5931 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005932 TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005933
5934 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005935 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005936
5937 // Jump to the nested function.
5938 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5939 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5940 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005941 TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00005942
5943 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5944 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5945 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005946 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00005947
Dan Gohman475871a2008-07-27 21:46:04 +00005948 SDValue Ops[] =
Duncan Sands339e14f2008-01-16 22:55:25 +00005949 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005950 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005951 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00005952 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00005953 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5954 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005955 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005956
5957 switch (CC) {
5958 default:
5959 assert(0 && "Unsupported calling convention");
5960 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005961 case CallingConv::X86_StdCall: {
5962 // Pass 'nest' parameter in ECX.
5963 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005964 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005965
5966 // Check that ECX wasn't needed by an 'inreg' parameter.
5967 const FunctionType *FTy = Func->getFunctionType();
Devang Patel05988662008-09-25 21:00:45 +00005968 const AttrListPtr &Attrs = Func->getAttributes();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005969
Chris Lattner58d74912008-03-12 17:45:29 +00005970 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00005971 unsigned InRegCount = 0;
5972 unsigned Idx = 1;
5973
5974 for (FunctionType::param_iterator I = FTy->param_begin(),
5975 E = FTy->param_end(); I != E; ++I, ++Idx)
Devang Patel05988662008-09-25 21:00:45 +00005976 if (Attrs.paramHasAttr(Idx, Attribute::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00005977 // FIXME: should only count parameters that are lowered to integers.
Anton Korobeynikovbff66b02008-09-09 18:22:57 +00005978 InRegCount += (TD->getTypeSizeInBits(*I) + 31) / 32;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005979
5980 if (InRegCount > 2) {
5981 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5982 abort();
5983 }
5984 }
5985 break;
5986 }
5987 case CallingConv::X86_FastCall:
Duncan Sandsbf53c292008-09-10 13:22:10 +00005988 case CallingConv::Fast:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005989 // Pass 'nest' parameter in EAX.
5990 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005991 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005992 break;
5993 }
5994
Dan Gohman475871a2008-07-27 21:46:04 +00005995 SDValue OutChains[4];
5996 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005997
5998 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5999 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
6000
Duncan Sands339e14f2008-01-16 22:55:25 +00006001 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00006002 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00006003 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00006004 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006005
6006 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00006007 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006008
Duncan Sands339e14f2008-01-16 22:55:25 +00006009 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006010 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
6011 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00006012 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006013
6014 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00006015 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006016
Dan Gohman475871a2008-07-27 21:46:04 +00006017 SDValue Ops[] =
Duncan Sandsf7331b32007-09-11 14:10:23 +00006018 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00006019 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006020 }
6021}
6022
Dan Gohman475871a2008-07-27 21:46:04 +00006023SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006024 /*
6025 The rounding mode is in bits 11:10 of FPSR, and has the following
6026 settings:
6027 00 Round to nearest
6028 01 Round to -inf
6029 10 Round to +inf
6030 11 Round to 0
6031
6032 FLT_ROUNDS, on the other hand, expects the following:
6033 -1 Undefined
6034 0 Round to 0
6035 1 Round to nearest
6036 2 Round to +inf
6037 3 Round to -inf
6038
6039 To perform the conversion, we do:
6040 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
6041 */
6042
6043 MachineFunction &MF = DAG.getMachineFunction();
6044 const TargetMachine &TM = MF.getTarget();
6045 const TargetFrameInfo &TFI = *TM.getFrameInfo();
6046 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006047 MVT VT = Op.getValueType();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006048
6049 // Save FP Control Word to stack slot
6050 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00006051 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006052
Dan Gohman475871a2008-07-27 21:46:04 +00006053 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Evan Cheng8a186ae2008-09-24 23:26:36 +00006054 DAG.getEntryNode(), StackSlot);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006055
6056 // Load FP Control Word from stack slot
Dan Gohman475871a2008-07-27 21:46:04 +00006057 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006058
6059 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00006060 SDValue CWD1 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006061 DAG.getNode(ISD::SRL, MVT::i16,
6062 DAG.getNode(ISD::AND, MVT::i16,
6063 CWD, DAG.getConstant(0x800, MVT::i16)),
6064 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00006065 SDValue CWD2 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006066 DAG.getNode(ISD::SRL, MVT::i16,
6067 DAG.getNode(ISD::AND, MVT::i16,
6068 CWD, DAG.getConstant(0x400, MVT::i16)),
6069 DAG.getConstant(9, MVT::i8));
6070
Dan Gohman475871a2008-07-27 21:46:04 +00006071 SDValue RetVal =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006072 DAG.getNode(ISD::AND, MVT::i16,
6073 DAG.getNode(ISD::ADD, MVT::i16,
6074 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
6075 DAG.getConstant(1, MVT::i16)),
6076 DAG.getConstant(3, MVT::i16));
6077
6078
Duncan Sands83ec4b62008-06-06 12:08:01 +00006079 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006080 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
6081}
6082
Dan Gohman475871a2008-07-27 21:46:04 +00006083SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006084 MVT VT = Op.getValueType();
6085 MVT OpVT = VT;
6086 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00006087
6088 Op = Op.getOperand(0);
6089 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00006090 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00006091 OpVT = MVT::i32;
6092 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6093 }
Evan Cheng18efe262007-12-14 02:13:44 +00006094
Evan Cheng152804e2007-12-14 08:30:15 +00006095 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
6096 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6097 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
6098
6099 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006100 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006101 Ops.push_back(Op);
6102 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
6103 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6104 Ops.push_back(Op.getValue(1));
6105 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6106
6107 // Finally xor with NumBits-1.
6108 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
6109
Evan Cheng18efe262007-12-14 02:13:44 +00006110 if (VT == MVT::i8)
6111 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6112 return Op;
6113}
6114
Dan Gohman475871a2008-07-27 21:46:04 +00006115SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00006116 MVT VT = Op.getValueType();
6117 MVT OpVT = VT;
6118 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00006119
6120 Op = Op.getOperand(0);
6121 if (VT == MVT::i8) {
6122 OpVT = MVT::i32;
6123 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
6124 }
Evan Cheng152804e2007-12-14 08:30:15 +00006125
6126 // Issue a bsf (scan bits forward) which also sets EFLAGS.
6127 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
6128 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
6129
6130 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00006131 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00006132 Ops.push_back(Op);
6133 Ops.push_back(DAG.getConstant(NumBits, OpVT));
6134 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
6135 Ops.push_back(Op.getValue(1));
6136 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
6137
Evan Cheng18efe262007-12-14 02:13:44 +00006138 if (VT == MVT::i8)
6139 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
6140 return Op;
6141}
6142
Dan Gohman475871a2008-07-27 21:46:04 +00006143SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006144 MVT T = Op.getValueType();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00006145 unsigned Reg = 0;
6146 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006147 switch(T.getSimpleVT()) {
6148 default:
6149 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006150 case MVT::i8: Reg = X86::AL; size = 1; break;
6151 case MVT::i16: Reg = X86::AX; size = 2; break;
6152 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006153 case MVT::i64:
6154 if (Subtarget->is64Bit()) {
6155 Reg = X86::RAX; size = 8;
Duncan Sandsed294c42008-10-20 15:56:33 +00006156 } else //Should go away when LegalizeType stuff lands
Gabor Greifba36cb52008-08-28 21:40:38 +00006157 return SDValue(ExpandATOMIC_CMP_SWAP(Op.getNode(), DAG), 0);
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006158 break;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006159 };
Dan Gohman475871a2008-07-27 21:46:04 +00006160 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
Dale Johannesend18a4622008-09-11 03:12:59 +00006161 Op.getOperand(2), SDValue());
Dan Gohman475871a2008-07-27 21:46:04 +00006162 SDValue Ops[] = { cpIn.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006163 Op.getOperand(1),
6164 Op.getOperand(3),
6165 DAG.getTargetConstant(size, MVT::i8),
6166 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006167 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006168 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
6169 SDValue cpOut =
Andrew Lenharth26ed8692008-03-01 21:52:34 +00006170 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
6171 return cpOut;
6172}
6173
Gabor Greif327ef032008-08-28 23:19:51 +00006174SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op,
6175 SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00006176 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00006177 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman475871a2008-07-27 21:46:04 +00006178 SDValue cpInL, cpInH;
Dale Johannesend18a4622008-09-11 03:12:59 +00006179 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006180 DAG.getConstant(0, MVT::i32));
Dale Johannesend18a4622008-09-11 03:12:59 +00006181 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006182 DAG.getConstant(1, MVT::i32));
6183 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman475871a2008-07-27 21:46:04 +00006184 cpInL, SDValue());
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006185 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
6186 cpInH, cpInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006187 SDValue swapInL, swapInH;
Dale Johannesend18a4622008-09-11 03:12:59 +00006188 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006189 DAG.getConstant(0, MVT::i32));
Dale Johannesend18a4622008-09-11 03:12:59 +00006190 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006191 DAG.getConstant(1, MVT::i32));
6192 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
6193 swapInL, cpInH.getValue(1));
6194 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
6195 swapInH, swapInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006196 SDValue Ops[] = { swapInH.getValue(0),
Evan Cheng8a186ae2008-09-24 23:26:36 +00006197 Op->getOperand(1),
6198 swapInH.getValue(1) };
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006199 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00006200 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
6201 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006202 Result.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00006203 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006204 cpOutL.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00006205 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
6206 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6207 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Gabor Greifba36cb52008-08-28 21:40:38 +00006208 return DAG.getMergeValues(Vals, 2).getNode();
Andrew Lenharthd19189e2008-03-05 01:15:49 +00006209}
6210
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006211SDValue X86TargetLowering::LowerATOMIC_BINARY_64(SDValue Op,
6212 SelectionDAG &DAG,
6213 unsigned NewOp) {
6214 SDNode *Node = Op.getNode();
6215 MVT T = Node->getValueType(0);
6216 assert (T == MVT::i64 && "Only know how to expand i64 atomics");
6217
6218 SDValue Chain = Node->getOperand(0);
6219 SDValue In1 = Node->getOperand(1);
Duncan Sandsed294c42008-10-20 15:56:33 +00006220 SDValue In2L = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6221 Node->getOperand(2), DAG.getIntPtrConstant(0));
6222 SDValue In2H = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32,
6223 Node->getOperand(2), DAG.getIntPtrConstant(1));
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006224 // This is a generalized SDNode, not an AtomicSDNode, so it doesn't
6225 // have a MemOperand. Pass the info through as a normal operand.
6226 SDValue LSI = DAG.getMemOperand(cast<MemSDNode>(Node)->getMemOperand());
6227 SDValue Ops[] = { Chain, In1, In2L, In2H, LSI };
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006228 SDVTList Tys = DAG.getVTList(MVT::i32, MVT::i32, MVT::Other);
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006229 SDValue Result = DAG.getNode(NewOp, Tys, Ops, 5);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006230 SDValue OpsF[] = { Result.getValue(0), Result.getValue(1)};
6231 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
6232 SDValue Vals[2] = { ResultVal, Result.getValue(2) };
6233 return SDValue(DAG.getMergeValues(Vals, 2).getNode(), 0);
6234}
6235
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006236SDValue X86TargetLowering::LowerLOAD_SUB(SDValue Op, SelectionDAG &DAG) {
6237 SDNode *Node = Op.getNode();
6238 MVT T = Node->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00006239 SDValue negOp = DAG.getNode(ISD::SUB, T,
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006240 DAG.getConstant(0, T), Node->getOperand(2));
6241 return DAG.getAtomic((Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_8 ?
6242 ISD::ATOMIC_LOAD_ADD_8 :
6243 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_16 ?
6244 ISD::ATOMIC_LOAD_ADD_16 :
6245 Op.getOpcode()==ISD::ATOMIC_LOAD_SUB_32 ?
6246 ISD::ATOMIC_LOAD_ADD_32 :
6247 ISD::ATOMIC_LOAD_ADD_64),
6248 Node->getOperand(0),
6249 Node->getOperand(1), negOp,
6250 cast<AtomicSDNode>(Node)->getSrcValue(),
6251 cast<AtomicSDNode>(Node)->getAlignment());
Mon P Wang63307c32008-05-05 19:05:59 +00006252}
6253
Evan Cheng0db9fe62006-04-25 20:13:52 +00006254/// LowerOperation - Provide custom lowering hooks for some operations.
6255///
Dan Gohman475871a2008-07-27 21:46:04 +00006256SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00006257 switch (Op.getOpcode()) {
6258 default: assert(0 && "Should not custom lower this!");
Duncan Sandsed294c42008-10-20 15:56:33 +00006259 case ISD::ATOMIC_CMP_SWAP_8:
6260 case ISD::ATOMIC_CMP_SWAP_16:
6261 case ISD::ATOMIC_CMP_SWAP_32:
Dale Johannesene00a8a22008-08-28 02:44:49 +00006262 case ISD::ATOMIC_CMP_SWAP_64: return LowerCMP_SWAP(Op,DAG);
Duncan Sandsed294c42008-10-20 15:56:33 +00006263 case ISD::ATOMIC_LOAD_SUB_8:
6264 case ISD::ATOMIC_LOAD_SUB_16:
Dale Johannesen71d1bf52008-09-29 22:25:26 +00006265 case ISD::ATOMIC_LOAD_SUB_32: return LowerLOAD_SUB(Op,DAG);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006266 case ISD::ATOMIC_LOAD_SUB_64: return (Subtarget->is64Bit()) ?
Dale Johannesen880ae362008-10-03 22:25:52 +00006267 LowerLOAD_SUB(Op,DAG) :
6268 LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006269 X86ISD::ATOMSUB64_DAG);
6270 case ISD::ATOMIC_LOAD_AND_64: return LowerATOMIC_BINARY_64(Op,DAG,
6271 X86ISD::ATOMAND64_DAG);
Dale Johannesen880ae362008-10-03 22:25:52 +00006272 case ISD::ATOMIC_LOAD_OR_64: return LowerATOMIC_BINARY_64(Op, DAG,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006273 X86ISD::ATOMOR64_DAG);
6274 case ISD::ATOMIC_LOAD_XOR_64: return LowerATOMIC_BINARY_64(Op,DAG,
6275 X86ISD::ATOMXOR64_DAG);
Dale Johannesen880ae362008-10-03 22:25:52 +00006276 case ISD::ATOMIC_LOAD_NAND_64:return LowerATOMIC_BINARY_64(Op,DAG,
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006277 X86ISD::ATOMNAND64_DAG);
6278 case ISD::ATOMIC_LOAD_ADD_64: return LowerATOMIC_BINARY_64(Op,DAG,
6279 X86ISD::ATOMADD64_DAG);
Dale Johannesen880ae362008-10-03 22:25:52 +00006280 case ISD::ATOMIC_SWAP_64: return LowerATOMIC_BINARY_64(Op,DAG,
6281 X86ISD::ATOMSWAP64_DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006282 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
6283 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
6284 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
6285 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
6286 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
6287 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
6288 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006289 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Bill Wendling056292f2008-09-16 21:48:12 +00006290 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006291 case ISD::SHL_PARTS:
6292 case ISD::SRA_PARTS:
6293 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
6294 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
Dale Johannesen1c15bf52008-10-21 20:50:01 +00006295 case ISD::UINT_TO_FP: return LowerUINT_TO_FP(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006296 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
6297 case ISD::FABS: return LowerFABS(Op, DAG);
6298 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00006299 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006300 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00006301 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00006302 case ISD::SELECT: return LowerSELECT(Op, DAG);
6303 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006304 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00006305 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006306 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00006307 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006308 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00006309 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00006310 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006311 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00006312 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
6313 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006314 case ISD::FRAME_TO_ARGS_OFFSET:
6315 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00006316 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006317 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00006318 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00006319 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00006320 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
6321 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00006322
6323 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
6324 case ISD::READCYCLECOUNTER:
Gabor Greifba36cb52008-08-28 21:40:38 +00006325 return SDValue(ExpandREADCYCLECOUNTER(Op.getNode(), DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00006326 }
Chris Lattner27a6c732007-11-24 07:07:01 +00006327}
6328
Duncan Sands126d9072008-07-04 11:47:58 +00006329/// ReplaceNodeResults - Replace a node with an illegal result type
6330/// with a new node built out of custom code.
6331SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00006332 switch (N->getOpcode()) {
Duncan Sandsed294c42008-10-20 15:56:33 +00006333 default:
6334 return X86TargetLowering::LowerOperation(SDValue (N, 0), DAG).getNode();
Chris Lattner27a6c732007-11-24 07:07:01 +00006335 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
6336 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Dale Johannesene00a8a22008-08-28 02:44:49 +00006337 case ISD::ATOMIC_CMP_SWAP_64: return ExpandATOMIC_CMP_SWAP(N, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00006338 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00006339}
6340
Evan Cheng72261582005-12-20 06:22:03 +00006341const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
6342 switch (Opcode) {
6343 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00006344 case X86ISD::BSF: return "X86ISD::BSF";
6345 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00006346 case X86ISD::SHLD: return "X86ISD::SHLD";
6347 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00006348 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006349 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00006350 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00006351 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00006352 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00006353 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00006354 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
6355 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
6356 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00006357 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00006358 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00006359 case X86ISD::CALL: return "X86ISD::CALL";
6360 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
6361 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
6362 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00006363 case X86ISD::COMI: return "X86ISD::COMI";
6364 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00006365 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00006366 case X86ISD::CMOV: return "X86ISD::CMOV";
6367 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00006368 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00006369 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6370 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006371 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006372 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006373 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006374 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006375 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6376 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006377 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00006378 case X86ISD::FMAX: return "X86ISD::FMAX";
6379 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006380 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6381 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006382 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6383 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006384 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006385 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006386 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006387 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6388 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006389 case X86ISD::ATOMADD64_DAG: return "X86ISD::ATOMADD64_DAG";
6390 case X86ISD::ATOMSUB64_DAG: return "X86ISD::ATOMSUB64_DAG";
6391 case X86ISD::ATOMOR64_DAG: return "X86ISD::ATOMOR64_DAG";
6392 case X86ISD::ATOMXOR64_DAG: return "X86ISD::ATOMXOR64_DAG";
6393 case X86ISD::ATOMAND64_DAG: return "X86ISD::ATOMAND64_DAG";
6394 case X86ISD::ATOMNAND64_DAG: return "X86ISD::ATOMNAND64_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006395 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6396 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006397 case X86ISD::VSHL: return "X86ISD::VSHL";
6398 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006399 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6400 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6401 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6402 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6403 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6404 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6405 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6406 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6407 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6408 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Evan Cheng72261582005-12-20 06:22:03 +00006409 }
6410}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006411
Chris Lattnerc9addb72007-03-30 23:15:24 +00006412// isLegalAddressingMode - Return true if the addressing mode represented
6413// by AM is legal for this target, for a load/store of the specified type.
6414bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6415 const Type *Ty) const {
6416 // X86 supports extremely general addressing modes.
6417
6418 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6419 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6420 return false;
6421
6422 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006423 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006424 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6425 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006426
6427 // X86-64 only supports addr of globals in small code model.
6428 if (Subtarget->is64Bit()) {
6429 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6430 return false;
6431 // If lower 4G is not available, then we must use rip-relative addressing.
6432 if (AM.BaseOffs || AM.Scale > 1)
6433 return false;
6434 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006435 }
6436
6437 switch (AM.Scale) {
6438 case 0:
6439 case 1:
6440 case 2:
6441 case 4:
6442 case 8:
6443 // These scales always work.
6444 break;
6445 case 3:
6446 case 5:
6447 case 9:
6448 // These scales are formed with basereg+scalereg. Only accept if there is
6449 // no basereg yet.
6450 if (AM.HasBaseReg)
6451 return false;
6452 break;
6453 default: // Other stuff never works.
6454 return false;
6455 }
6456
6457 return true;
6458}
6459
6460
Evan Cheng2bd122c2007-10-26 01:56:11 +00006461bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6462 if (!Ty1->isInteger() || !Ty2->isInteger())
6463 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006464 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6465 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006466 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006467 return false;
6468 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006469}
6470
Duncan Sands83ec4b62008-06-06 12:08:01 +00006471bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6472 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006473 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006474 unsigned NumBits1 = VT1.getSizeInBits();
6475 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006476 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006477 return false;
6478 return Subtarget->is64Bit() || NumBits1 < 64;
6479}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006480
Evan Cheng60c07e12006-07-05 22:17:51 +00006481/// isShuffleMaskLegal - Targets can use this to indicate that they only
6482/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6483/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6484/// are assumed to be legal.
6485bool
Dan Gohman475871a2008-07-27 21:46:04 +00006486X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006487 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006488 if (VT.getSizeInBits() == 64) return false;
Gabor Greifba36cb52008-08-28 21:40:38 +00006489 return (Mask.getNode()->getNumOperands() <= 4 ||
6490 isIdentityMask(Mask.getNode()) ||
6491 isIdentityMask(Mask.getNode(), true) ||
6492 isSplatMask(Mask.getNode()) ||
6493 isPSHUFHW_PSHUFLWMask(Mask.getNode()) ||
6494 X86::isUNPCKLMask(Mask.getNode()) ||
6495 X86::isUNPCKHMask(Mask.getNode()) ||
6496 X86::isUNPCKL_v_undef_Mask(Mask.getNode()) ||
6497 X86::isUNPCKH_v_undef_Mask(Mask.getNode()));
Evan Cheng60c07e12006-07-05 22:17:51 +00006498}
6499
Dan Gohman7d8143f2008-04-09 20:09:42 +00006500bool
Dan Gohman475871a2008-07-27 21:46:04 +00006501X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006502 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006503 unsigned NumElts = BVOps.size();
6504 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006505 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006506 if (NumElts == 2) return true;
6507 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00006508 return (isMOVLMask(&BVOps[0], 4) ||
6509 isCommutedMOVL(&BVOps[0], 4, true) ||
6510 isSHUFPMask(&BVOps[0], 4) ||
6511 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00006512 }
6513 return false;
6514}
6515
6516//===----------------------------------------------------------------------===//
6517// X86 Scheduler Hooks
6518//===----------------------------------------------------------------------===//
6519
Mon P Wang63307c32008-05-05 19:05:59 +00006520// private utility function
6521MachineBasicBlock *
6522X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6523 MachineBasicBlock *MBB,
6524 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006525 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006526 unsigned LoadOpc,
6527 unsigned CXchgOpc,
6528 unsigned copyOpc,
6529 unsigned notOpc,
6530 unsigned EAXreg,
6531 TargetRegisterClass *RC,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006532 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00006533 // For the atomic bitwise operator, we generate
6534 // thisMBB:
6535 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006536 // ld t1 = [bitinstr.addr]
6537 // op t2 = t1, [bitinstr.val]
6538 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006539 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6540 // bz newMBB
6541 // fallthrough -->nextMBB
6542 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6543 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006544 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006545 ++MBBIter;
6546
6547 /// First build the CFG
6548 MachineFunction *F = MBB->getParent();
6549 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006550 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6551 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6552 F->insert(MBBIter, newMBB);
6553 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006554
6555 // Move all successors to thisMBB to nextMBB
6556 nextMBB->transferSuccessors(thisMBB);
6557
6558 // Update thisMBB to fall through to newMBB
6559 thisMBB->addSuccessor(newMBB);
6560
6561 // newMBB jumps to itself and fall through to nextMBB
6562 newMBB->addSuccessor(nextMBB);
6563 newMBB->addSuccessor(newMBB);
6564
6565 // Insert instructions into newMBB based on incoming instruction
6566 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6567 MachineOperand& destOper = bInstr->getOperand(0);
6568 MachineOperand* argOpers[6];
6569 int numArgs = bInstr->getNumOperands() - 1;
6570 for (int i=0; i < numArgs; ++i)
6571 argOpers[i] = &bInstr->getOperand(i+1);
6572
6573 // x86 address has 4 operands: base, index, scale, and displacement
6574 int lastAddrIndx = 3; // [0,3]
6575 int valArgIndx = 4;
6576
Dale Johannesen140be2d2008-08-19 18:47:28 +00006577 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6578 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006579 for (int i=0; i <= lastAddrIndx; ++i)
6580 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006581
Dale Johannesen140be2d2008-08-19 18:47:28 +00006582 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006583 if (invSrc) {
Dale Johannesen140be2d2008-08-19 18:47:28 +00006584 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006585 }
6586 else
6587 tt = t1;
6588
Dale Johannesen140be2d2008-08-19 18:47:28 +00006589 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00006590 assert((argOpers[valArgIndx]->isReg() ||
6591 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00006592 "invalid operand");
Dan Gohmand735b802008-10-03 15:45:36 +00006593 if (argOpers[valArgIndx]->isReg())
Mon P Wang63307c32008-05-05 19:05:59 +00006594 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6595 else
6596 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006597 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00006598 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006599
Dale Johannesen140be2d2008-08-19 18:47:28 +00006600 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00006601 MIB.addReg(t1);
6602
Dale Johannesen140be2d2008-08-19 18:47:28 +00006603 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00006604 for (int i=0; i <= lastAddrIndx; ++i)
6605 (*MIB).addOperand(*argOpers[i]);
6606 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00006607 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6608 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6609
Dale Johannesen140be2d2008-08-19 18:47:28 +00006610 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6611 MIB.addReg(EAXreg);
Mon P Wang63307c32008-05-05 19:05:59 +00006612
6613 // insert branch
6614 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6615
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006616 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006617 return nextMBB;
6618}
6619
Dale Johannesen1b54c7f2008-10-03 19:41:08 +00006620// private utility function: 64 bit atomics on 32 bit host.
Mon P Wang63307c32008-05-05 19:05:59 +00006621MachineBasicBlock *
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006622X86TargetLowering::EmitAtomicBit6432WithCustomInserter(MachineInstr *bInstr,
6623 MachineBasicBlock *MBB,
6624 unsigned regOpcL,
6625 unsigned regOpcH,
6626 unsigned immOpcL,
6627 unsigned immOpcH,
6628 bool invSrc) {
6629 // For the atomic bitwise operator, we generate
6630 // thisMBB (instructions are in pairs, except cmpxchg8b)
6631 // ld t1,t2 = [bitinstr.addr]
6632 // newMBB:
6633 // out1, out2 = phi (thisMBB, t1/t2) (newMBB, t3/t4)
6634 // op t5, t6 <- out1, out2, [bitinstr.val]
Dale Johannesen880ae362008-10-03 22:25:52 +00006635 // (for SWAP, substitute: mov t5, t6 <- [bitinstr.val])
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006636 // mov ECX, EBX <- t5, t6
6637 // mov EAX, EDX <- t1, t2
6638 // cmpxchg8b [bitinstr.addr] [EAX, EDX, EBX, ECX implicit]
6639 // mov t3, t4 <- EAX, EDX
6640 // bz newMBB
6641 // result in out1, out2
6642 // fallthrough -->nextMBB
6643
6644 const TargetRegisterClass *RC = X86::GR32RegisterClass;
6645 const unsigned LoadOpc = X86::MOV32rm;
6646 const unsigned copyOpc = X86::MOV32rr;
6647 const unsigned NotOpc = X86::NOT32r;
6648 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6649 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
6650 MachineFunction::iterator MBBIter = MBB;
6651 ++MBBIter;
6652
6653 /// First build the CFG
6654 MachineFunction *F = MBB->getParent();
6655 MachineBasicBlock *thisMBB = MBB;
6656 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6657 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6658 F->insert(MBBIter, newMBB);
6659 F->insert(MBBIter, nextMBB);
6660
6661 // Move all successors to thisMBB to nextMBB
6662 nextMBB->transferSuccessors(thisMBB);
6663
6664 // Update thisMBB to fall through to newMBB
6665 thisMBB->addSuccessor(newMBB);
6666
6667 // newMBB jumps to itself and fall through to nextMBB
6668 newMBB->addSuccessor(nextMBB);
6669 newMBB->addSuccessor(newMBB);
6670
6671 // Insert instructions into newMBB based on incoming instruction
6672 // There are 8 "real" operands plus 9 implicit def/uses, ignored here.
6673 assert(bInstr->getNumOperands() < 18 && "unexpected number of operands");
6674 MachineOperand& dest1Oper = bInstr->getOperand(0);
6675 MachineOperand& dest2Oper = bInstr->getOperand(1);
6676 MachineOperand* argOpers[6];
6677 for (int i=0; i < 6; ++i)
6678 argOpers[i] = &bInstr->getOperand(i+2);
6679
6680 // x86 address has 4 operands: base, index, scale, and displacement
6681 int lastAddrIndx = 3; // [0,3]
6682
6683 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6684 MachineInstrBuilder MIB = BuildMI(thisMBB, TII->get(LoadOpc), t1);
6685 for (int i=0; i <= lastAddrIndx; ++i)
6686 (*MIB).addOperand(*argOpers[i]);
6687 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
6688 MIB = BuildMI(thisMBB, TII->get(LoadOpc), t2);
Dale Johannesen880ae362008-10-03 22:25:52 +00006689 // add 4 to displacement.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006690 for (int i=0; i <= lastAddrIndx-1; ++i)
6691 (*MIB).addOperand(*argOpers[i]);
Dale Johannesen880ae362008-10-03 22:25:52 +00006692 MachineOperand newOp3 = *(argOpers[3]);
6693 if (newOp3.isImm())
6694 newOp3.setImm(newOp3.getImm()+4);
6695 else
6696 newOp3.setOffset(newOp3.getOffset()+4);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006697 (*MIB).addOperand(newOp3);
6698
6699 // t3/4 are defined later, at the bottom of the loop
6700 unsigned t3 = F->getRegInfo().createVirtualRegister(RC);
6701 unsigned t4 = F->getRegInfo().createVirtualRegister(RC);
6702 BuildMI(newMBB, TII->get(X86::PHI), dest1Oper.getReg())
6703 .addReg(t1).addMBB(thisMBB).addReg(t3).addMBB(newMBB);
6704 BuildMI(newMBB, TII->get(X86::PHI), dest2Oper.getReg())
6705 .addReg(t2).addMBB(thisMBB).addReg(t4).addMBB(newMBB);
6706
6707 unsigned tt1 = F->getRegInfo().createVirtualRegister(RC);
6708 unsigned tt2 = F->getRegInfo().createVirtualRegister(RC);
6709 if (invSrc) {
6710 MIB = BuildMI(newMBB, TII->get(NotOpc), tt1).addReg(t1);
6711 MIB = BuildMI(newMBB, TII->get(NotOpc), tt2).addReg(t2);
6712 } else {
6713 tt1 = t1;
6714 tt2 = t2;
6715 }
6716
Dan Gohmand735b802008-10-03 15:45:36 +00006717 assert((argOpers[4]->isReg() || argOpers[4]->isImm()) &&
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006718 "invalid operand");
6719 unsigned t5 = F->getRegInfo().createVirtualRegister(RC);
6720 unsigned t6 = F->getRegInfo().createVirtualRegister(RC);
Dan Gohmand735b802008-10-03 15:45:36 +00006721 if (argOpers[4]->isReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006722 MIB = BuildMI(newMBB, TII->get(regOpcL), t5);
6723 else
6724 MIB = BuildMI(newMBB, TII->get(immOpcL), t5);
Dale Johannesen880ae362008-10-03 22:25:52 +00006725 if (regOpcL != X86::MOV32rr)
6726 MIB.addReg(tt1);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006727 (*MIB).addOperand(*argOpers[4]);
Dan Gohmand735b802008-10-03 15:45:36 +00006728 assert(argOpers[5]->isReg() == argOpers[4]->isReg());
6729 assert(argOpers[5]->isImm() == argOpers[4]->isImm());
6730 if (argOpers[5]->isReg())
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006731 MIB = BuildMI(newMBB, TII->get(regOpcH), t6);
6732 else
6733 MIB = BuildMI(newMBB, TII->get(immOpcH), t6);
Dale Johannesen880ae362008-10-03 22:25:52 +00006734 if (regOpcH != X86::MOV32rr)
6735 MIB.addReg(tt2);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00006736 (*MIB).addOperand(*argOpers[5]);
6737
6738 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EAX);
6739 MIB.addReg(t1);
6740 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EDX);
6741 MIB.addReg(t2);
6742
6743 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::EBX);
6744 MIB.addReg(t5);
6745 MIB = BuildMI(newMBB, TII->get(copyOpc), X86::ECX);
6746 MIB.addReg(t6);
6747
6748 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG8B));
6749 for (int i=0; i <= lastAddrIndx; ++i)
6750 (*MIB).addOperand(*argOpers[i]);
6751
6752 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6753 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6754
6755 MIB = BuildMI(newMBB, TII->get(copyOpc), t3);
6756 MIB.addReg(X86::EAX);
6757 MIB = BuildMI(newMBB, TII->get(copyOpc), t4);
6758 MIB.addReg(X86::EDX);
6759
6760 // insert branch
6761 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6762
6763 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
6764 return nextMBB;
6765}
6766
6767// private utility function
6768MachineBasicBlock *
Mon P Wang63307c32008-05-05 19:05:59 +00006769X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6770 MachineBasicBlock *MBB,
6771 unsigned cmovOpc) {
6772 // For the atomic min/max operator, we generate
6773 // thisMBB:
6774 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006775 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00006776 // mov t2 = [min/max.val]
6777 // cmp t1, t2
6778 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00006779 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006780 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6781 // bz newMBB
6782 // fallthrough -->nextMBB
6783 //
6784 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6785 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006786 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006787 ++MBBIter;
6788
6789 /// First build the CFG
6790 MachineFunction *F = MBB->getParent();
6791 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006792 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6793 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6794 F->insert(MBBIter, newMBB);
6795 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006796
6797 // Move all successors to thisMBB to nextMBB
6798 nextMBB->transferSuccessors(thisMBB);
6799
6800 // Update thisMBB to fall through to newMBB
6801 thisMBB->addSuccessor(newMBB);
6802
6803 // newMBB jumps to newMBB and fall through to nextMBB
6804 newMBB->addSuccessor(nextMBB);
6805 newMBB->addSuccessor(newMBB);
6806
6807 // Insert instructions into newMBB based on incoming instruction
6808 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6809 MachineOperand& destOper = mInstr->getOperand(0);
6810 MachineOperand* argOpers[6];
6811 int numArgs = mInstr->getNumOperands() - 1;
6812 for (int i=0; i < numArgs; ++i)
6813 argOpers[i] = &mInstr->getOperand(i+1);
6814
6815 // x86 address has 4 operands: base, index, scale, and displacement
6816 int lastAddrIndx = 3; // [0,3]
6817 int valArgIndx = 4;
6818
Mon P Wangab3e7472008-05-05 22:56:23 +00006819 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6820 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006821 for (int i=0; i <= lastAddrIndx; ++i)
6822 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00006823
Mon P Wang63307c32008-05-05 19:05:59 +00006824 // We only support register and immediate values
Dan Gohmand735b802008-10-03 15:45:36 +00006825 assert((argOpers[valArgIndx]->isReg() ||
6826 argOpers[valArgIndx]->isImm()) &&
Dan Gohman014278e2008-09-13 17:58:21 +00006827 "invalid operand");
Mon P Wang63307c32008-05-05 19:05:59 +00006828
6829 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
Dan Gohmand735b802008-10-03 15:45:36 +00006830 if (argOpers[valArgIndx]->isReg())
Mon P Wang63307c32008-05-05 19:05:59 +00006831 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6832 else
6833 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6834 (*MIB).addOperand(*argOpers[valArgIndx]);
6835
Mon P Wangab3e7472008-05-05 22:56:23 +00006836 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6837 MIB.addReg(t1);
6838
Mon P Wang63307c32008-05-05 19:05:59 +00006839 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6840 MIB.addReg(t1);
6841 MIB.addReg(t2);
6842
6843 // Generate movc
6844 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6845 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6846 MIB.addReg(t2);
6847 MIB.addReg(t1);
6848
6849 // Cmp and exchange if none has modified the memory location
6850 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6851 for (int i=0; i <= lastAddrIndx; ++i)
6852 (*MIB).addOperand(*argOpers[i]);
6853 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00006854 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6855 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang63307c32008-05-05 19:05:59 +00006856
6857 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6858 MIB.addReg(X86::EAX);
6859
6860 // insert branch
6861 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6862
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006863 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006864 return nextMBB;
6865}
6866
6867
Evan Cheng60c07e12006-07-05 22:17:51 +00006868MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006869X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6870 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00006871 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00006872 switch (MI->getOpcode()) {
6873 default: assert(false && "Unexpected instr type to insert");
6874 case X86::CMOV_FR32:
6875 case X86::CMOV_FR64:
6876 case X86::CMOV_V4F32:
6877 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00006878 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006879 // To "insert" a SELECT_CC instruction, we actually have to insert the
6880 // diamond control-flow pattern. The incoming instruction knows the
6881 // destination vreg to set, the condition code register to branch on, the
6882 // true/false values to select between, and a branch opcode to use.
6883 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006884 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00006885 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006886
Evan Cheng60c07e12006-07-05 22:17:51 +00006887 // thisMBB:
6888 // ...
6889 // TrueVal = ...
6890 // cmpTY ccX, r1, r2
6891 // bCC copy1MBB
6892 // fallthrough --> copy0MBB
6893 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006894 MachineFunction *F = BB->getParent();
6895 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6896 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006897 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00006898 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00006899 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006900 F->insert(It, copy0MBB);
6901 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006902 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00006903 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00006904 sinkMBB->transferSuccessors(BB);
6905
6906 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00006907 BB->addSuccessor(copy0MBB);
6908 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006909
Evan Cheng60c07e12006-07-05 22:17:51 +00006910 // copy0MBB:
6911 // %FalseValue = ...
6912 // # fallthrough to sinkMBB
6913 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006914
Evan Cheng60c07e12006-07-05 22:17:51 +00006915 // Update machine-CFG edges
6916 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006917
Evan Cheng60c07e12006-07-05 22:17:51 +00006918 // sinkMBB:
6919 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6920 // ...
6921 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00006922 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00006923 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6924 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6925
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006926 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006927 return BB;
6928 }
6929
Dale Johannesen849f2142007-07-03 00:53:03 +00006930 case X86::FP32_TO_INT16_IN_MEM:
6931 case X86::FP32_TO_INT32_IN_MEM:
6932 case X86::FP32_TO_INT64_IN_MEM:
6933 case X86::FP64_TO_INT16_IN_MEM:
6934 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00006935 case X86::FP64_TO_INT64_IN_MEM:
6936 case X86::FP80_TO_INT16_IN_MEM:
6937 case X86::FP80_TO_INT32_IN_MEM:
6938 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006939 // Change the floating point control register to use "round towards zero"
6940 // mode when truncating to an integer value.
6941 MachineFunction *F = BB->getParent();
6942 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006943 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006944
6945 // Load the old value of the high byte of the control word...
6946 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00006947 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006948 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006949
6950 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006951 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6952 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00006953
6954 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006955 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006956
6957 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00006958 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6959 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00006960
6961 // Get the X86 opcode to use.
6962 unsigned Opc;
6963 switch (MI->getOpcode()) {
6964 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00006965 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6966 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6967 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6968 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6969 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6970 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00006971 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6972 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6973 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00006974 }
6975
6976 X86AddressMode AM;
6977 MachineOperand &Op = MI->getOperand(0);
Dan Gohmand735b802008-10-03 15:45:36 +00006978 if (Op.isReg()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00006979 AM.BaseType = X86AddressMode::RegBase;
6980 AM.Base.Reg = Op.getReg();
6981 } else {
6982 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00006983 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00006984 }
6985 Op = MI->getOperand(1);
Dan Gohmand735b802008-10-03 15:45:36 +00006986 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006987 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006988 Op = MI->getOperand(2);
Dan Gohmand735b802008-10-03 15:45:36 +00006989 if (Op.isImm())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006990 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006991 Op = MI->getOperand(3);
Dan Gohmand735b802008-10-03 15:45:36 +00006992 if (Op.isGlobal()) {
Evan Cheng60c07e12006-07-05 22:17:51 +00006993 AM.GV = Op.getGlobal();
6994 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00006995 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006996 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00006997 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6998 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00006999
7000 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00007001 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00007002
Dan Gohman8e5f2c62008-07-07 23:14:23 +00007003 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00007004 return BB;
7005 }
Mon P Wang63307c32008-05-05 19:05:59 +00007006 case X86::ATOMAND32:
7007 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007008 X86::AND32ri, X86::MOV32rm,
7009 X86::LCMPXCHG32, X86::MOV32rr,
7010 X86::NOT32r, X86::EAX,
7011 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007012 case X86::ATOMOR32:
7013 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007014 X86::OR32ri, X86::MOV32rm,
7015 X86::LCMPXCHG32, X86::MOV32rr,
7016 X86::NOT32r, X86::EAX,
7017 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00007018 case X86::ATOMXOR32:
7019 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007020 X86::XOR32ri, X86::MOV32rm,
7021 X86::LCMPXCHG32, X86::MOV32rr,
7022 X86::NOT32r, X86::EAX,
7023 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00007024 case X86::ATOMNAND32:
7025 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00007026 X86::AND32ri, X86::MOV32rm,
7027 X86::LCMPXCHG32, X86::MOV32rr,
7028 X86::NOT32r, X86::EAX,
7029 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00007030 case X86::ATOMMIN32:
7031 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
7032 case X86::ATOMMAX32:
7033 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
7034 case X86::ATOMUMIN32:
7035 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
7036 case X86::ATOMUMAX32:
7037 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00007038
7039 case X86::ATOMAND16:
7040 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7041 X86::AND16ri, X86::MOV16rm,
7042 X86::LCMPXCHG16, X86::MOV16rr,
7043 X86::NOT16r, X86::AX,
7044 X86::GR16RegisterClass);
7045 case X86::ATOMOR16:
7046 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
7047 X86::OR16ri, X86::MOV16rm,
7048 X86::LCMPXCHG16, X86::MOV16rr,
7049 X86::NOT16r, X86::AX,
7050 X86::GR16RegisterClass);
7051 case X86::ATOMXOR16:
7052 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
7053 X86::XOR16ri, X86::MOV16rm,
7054 X86::LCMPXCHG16, X86::MOV16rr,
7055 X86::NOT16r, X86::AX,
7056 X86::GR16RegisterClass);
7057 case X86::ATOMNAND16:
7058 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
7059 X86::AND16ri, X86::MOV16rm,
7060 X86::LCMPXCHG16, X86::MOV16rr,
7061 X86::NOT16r, X86::AX,
7062 X86::GR16RegisterClass, true);
7063 case X86::ATOMMIN16:
7064 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
7065 case X86::ATOMMAX16:
7066 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
7067 case X86::ATOMUMIN16:
7068 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
7069 case X86::ATOMUMAX16:
7070 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
7071
7072 case X86::ATOMAND8:
7073 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7074 X86::AND8ri, X86::MOV8rm,
7075 X86::LCMPXCHG8, X86::MOV8rr,
7076 X86::NOT8r, X86::AL,
7077 X86::GR8RegisterClass);
7078 case X86::ATOMOR8:
7079 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
7080 X86::OR8ri, X86::MOV8rm,
7081 X86::LCMPXCHG8, X86::MOV8rr,
7082 X86::NOT8r, X86::AL,
7083 X86::GR8RegisterClass);
7084 case X86::ATOMXOR8:
7085 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
7086 X86::XOR8ri, X86::MOV8rm,
7087 X86::LCMPXCHG8, X86::MOV8rr,
7088 X86::NOT8r, X86::AL,
7089 X86::GR8RegisterClass);
7090 case X86::ATOMNAND8:
7091 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
7092 X86::AND8ri, X86::MOV8rm,
7093 X86::LCMPXCHG8, X86::MOV8rr,
7094 X86::NOT8r, X86::AL,
7095 X86::GR8RegisterClass, true);
7096 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007097 // This group is for 64-bit host.
Dale Johannesena99e3842008-08-20 00:48:50 +00007098 case X86::ATOMAND64:
7099 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7100 X86::AND64ri32, X86::MOV64rm,
7101 X86::LCMPXCHG64, X86::MOV64rr,
7102 X86::NOT64r, X86::RAX,
7103 X86::GR64RegisterClass);
7104 case X86::ATOMOR64:
7105 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
7106 X86::OR64ri32, X86::MOV64rm,
7107 X86::LCMPXCHG64, X86::MOV64rr,
7108 X86::NOT64r, X86::RAX,
7109 X86::GR64RegisterClass);
7110 case X86::ATOMXOR64:
7111 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
7112 X86::XOR64ri32, X86::MOV64rm,
7113 X86::LCMPXCHG64, X86::MOV64rr,
7114 X86::NOT64r, X86::RAX,
7115 X86::GR64RegisterClass);
7116 case X86::ATOMNAND64:
7117 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
7118 X86::AND64ri32, X86::MOV64rm,
7119 X86::LCMPXCHG64, X86::MOV64rr,
7120 X86::NOT64r, X86::RAX,
7121 X86::GR64RegisterClass, true);
7122 case X86::ATOMMIN64:
7123 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
7124 case X86::ATOMMAX64:
7125 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
7126 case X86::ATOMUMIN64:
7127 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
7128 case X86::ATOMUMAX64:
7129 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007130
7131 // This group does 64-bit operations on a 32-bit host.
7132 case X86::ATOMAND6432:
7133 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7134 X86::AND32rr, X86::AND32rr,
7135 X86::AND32ri, X86::AND32ri,
7136 false);
7137 case X86::ATOMOR6432:
7138 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7139 X86::OR32rr, X86::OR32rr,
7140 X86::OR32ri, X86::OR32ri,
7141 false);
7142 case X86::ATOMXOR6432:
7143 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7144 X86::XOR32rr, X86::XOR32rr,
7145 X86::XOR32ri, X86::XOR32ri,
7146 false);
7147 case X86::ATOMNAND6432:
7148 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7149 X86::AND32rr, X86::AND32rr,
7150 X86::AND32ri, X86::AND32ri,
7151 true);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007152 case X86::ATOMADD6432:
7153 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7154 X86::ADD32rr, X86::ADC32rr,
7155 X86::ADD32ri, X86::ADC32ri,
7156 false);
Dale Johannesen48c1bc22008-10-02 18:53:47 +00007157 case X86::ATOMSUB6432:
7158 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7159 X86::SUB32rr, X86::SBB32rr,
7160 X86::SUB32ri, X86::SBB32ri,
7161 false);
Dale Johannesen880ae362008-10-03 22:25:52 +00007162 case X86::ATOMSWAP6432:
7163 return EmitAtomicBit6432WithCustomInserter(MI, BB,
7164 X86::MOV32rr, X86::MOV32rr,
7165 X86::MOV32ri, X86::MOV32ri,
7166 false);
Evan Cheng60c07e12006-07-05 22:17:51 +00007167 }
7168}
7169
7170//===----------------------------------------------------------------------===//
7171// X86 Optimization Hooks
7172//===----------------------------------------------------------------------===//
7173
Dan Gohman475871a2008-07-27 21:46:04 +00007174void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00007175 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007176 APInt &KnownZero,
7177 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00007178 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00007179 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007180 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00007181 assert((Opc >= ISD::BUILTIN_OP_END ||
7182 Opc == ISD::INTRINSIC_WO_CHAIN ||
7183 Opc == ISD::INTRINSIC_W_CHAIN ||
7184 Opc == ISD::INTRINSIC_VOID) &&
7185 "Should use MaskedValueIsZero if you don't know whether Op"
7186 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007187
Dan Gohmanf4f92f52008-02-13 23:07:24 +00007188 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007189 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00007190 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007191 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00007192 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
7193 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00007194 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007195 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00007196}
Chris Lattner259e97c2006-01-31 19:43:35 +00007197
Evan Cheng206ee9d2006-07-07 08:33:52 +00007198/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00007199/// node is a GlobalAddress + offset.
7200bool X86TargetLowering::isGAPlusOffset(SDNode *N,
7201 GlobalValue* &GA, int64_t &Offset) const{
7202 if (N->getOpcode() == X86ISD::Wrapper) {
7203 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007204 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
Dan Gohman6520e202008-10-18 02:06:02 +00007205 Offset = cast<GlobalAddressSDNode>(N->getOperand(0))->getOffset();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007206 return true;
7207 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00007208 }
Evan Chengad4196b2008-05-12 19:56:52 +00007209 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007210}
7211
Evan Chengad4196b2008-05-12 19:56:52 +00007212static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
7213 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007214 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00007215 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00007216 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007217 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00007218 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00007219 return false;
7220}
7221
Dan Gohman475871a2008-07-27 21:46:04 +00007222static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007223 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00007224 SDNode *&Base,
7225 SelectionDAG &DAG, MachineFrameInfo *MFI,
7226 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007227 Base = NULL;
7228 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00007229 SDValue Idx = PermMask.getOperand(i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00007230 if (Idx.getOpcode() == ISD::UNDEF) {
7231 if (!Base)
7232 return false;
7233 continue;
7234 }
7235
Dan Gohman475871a2008-07-27 21:46:04 +00007236 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Gabor Greifba36cb52008-08-28 21:40:38 +00007237 if (!Elt.getNode() ||
7238 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.getNode())))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007239 return false;
7240 if (!Base) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007241 Base = Elt.getNode();
Evan Cheng50d9e722008-05-10 06:46:49 +00007242 if (Base->getOpcode() == ISD::UNDEF)
7243 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00007244 continue;
7245 }
7246 if (Elt.getOpcode() == ISD::UNDEF)
7247 continue;
7248
Gabor Greifba36cb52008-08-28 21:40:38 +00007249 if (!TLI.isConsecutiveLoad(Elt.getNode(), Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007250 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00007251 return false;
7252 }
7253 return true;
7254}
Evan Cheng206ee9d2006-07-07 08:33:52 +00007255
7256/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
7257/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
7258/// if the load addresses are consecutive, non-overlapping, and in the right
7259/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00007260static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00007261 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00007262 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00007263 MVT VT = N->getValueType(0);
7264 MVT EVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00007265 SDValue PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00007266 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007267 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00007268 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
7269 DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00007270 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007271
Dan Gohmand3006222007-07-27 17:16:43 +00007272 LoadSDNode *LD = cast<LoadSDNode>(Base);
Gabor Greifba36cb52008-08-28 21:40:38 +00007273 if (isBaseAlignmentOfN(16, Base->getOperand(1).getNode(), TLI))
Evan Cheng466685d2006-10-09 20:57:25 +00007274 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00007275 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng71f489d2008-05-05 22:12:23 +00007276 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
7277 LD->getSrcValueOffset(), LD->isVolatile(),
7278 LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00007279}
7280
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007281/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00007282static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Cheng8a186ae2008-09-24 23:26:36 +00007283 const X86Subtarget *Subtarget,
7284 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00007285 unsigned NumOps = N->getNumOperands();
7286
Evan Chengd880b972008-05-09 21:53:03 +00007287 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00007288 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00007289 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007290
Duncan Sands83ec4b62008-06-06 12:08:01 +00007291 MVT VT = N->getValueType(0);
7292 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00007293 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
7294 // We are looking for load i64 and zero extend. We want to transform
7295 // it before legalizer has a chance to expand it. Also look for i64
7296 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00007297 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007298 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00007299 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00007300 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00007301 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007302
7303 // Value must be a load.
Gabor Greifba36cb52008-08-28 21:40:38 +00007304 SDNode *Base = N->getOperand(0).getNode();
Evan Chengd880b972008-05-09 21:53:03 +00007305 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007306 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00007307 return SDValue();
Gabor Greifba36cb52008-08-28 21:40:38 +00007308 Base = Base->getOperand(0).getNode();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007309 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00007310 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00007311 }
Evan Chengd880b972008-05-09 21:53:03 +00007312
7313 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00007314 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00007315
7316 // Load must not be an extload.
7317 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00007318 return SDValue();
Nate Begemanf7333bf2008-05-28 00:24:25 +00007319
Evan Cheng8a186ae2008-09-24 23:26:36 +00007320 SDVTList Tys = DAG.getVTList(VT, MVT::Other);
7321 SDValue Ops[] = { LD->getChain(), LD->getBasePtr() };
7322 SDValue ResNode = DAG.getNode(X86ISD::VZEXT_LOAD, Tys, Ops, 2);
7323 DAG.ReplaceAllUsesOfValueWith(SDValue(Base, 1), ResNode.getValue(1));
7324 return ResNode;
Evan Chengd880b972008-05-09 21:53:03 +00007325}
7326
Chris Lattner83e6c992006-10-04 06:57:07 +00007327/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007328static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner83e6c992006-10-04 06:57:07 +00007329 const X86Subtarget *Subtarget) {
Dan Gohman475871a2008-07-27 21:46:04 +00007330 SDValue Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007331
Chris Lattner83e6c992006-10-04 06:57:07 +00007332 // If we have SSE[12] support, try to form min/max nodes.
7333 if (Subtarget->hasSSE2() &&
7334 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
7335 if (Cond.getOpcode() == ISD::SETCC) {
7336 // Get the LHS/RHS of the select.
Dan Gohman475871a2008-07-27 21:46:04 +00007337 SDValue LHS = N->getOperand(1);
7338 SDValue RHS = N->getOperand(2);
Chris Lattner83e6c992006-10-04 06:57:07 +00007339 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007340
Evan Cheng8ca29322006-11-10 21:43:37 +00007341 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00007342 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00007343 switch (CC) {
7344 default: break;
7345 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
7346 case ISD::SETULE:
7347 case ISD::SETLE:
7348 if (!UnsafeFPMath) break;
7349 // FALL THROUGH.
7350 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
7351 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00007352 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007353 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007354
Chris Lattner1907a7b2006-10-05 04:11:26 +00007355 case ISD::SETOGT: // (X > Y) ? X : Y -> max
7356 case ISD::SETUGT:
7357 case ISD::SETGT:
7358 if (!UnsafeFPMath) break;
7359 // FALL THROUGH.
7360 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
7361 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00007362 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007363 break;
7364 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007365 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00007366 switch (CC) {
7367 default: break;
7368 case ISD::SETOGT: // (X > Y) ? Y : X -> min
7369 case ISD::SETUGT:
7370 case ISD::SETGT:
7371 if (!UnsafeFPMath) break;
7372 // FALL THROUGH.
7373 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
7374 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00007375 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007376 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007377
Chris Lattner1907a7b2006-10-05 04:11:26 +00007378 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
7379 case ISD::SETULE:
7380 case ISD::SETLE:
7381 if (!UnsafeFPMath) break;
7382 // FALL THROUGH.
7383 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
7384 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00007385 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00007386 break;
7387 }
Chris Lattner83e6c992006-10-04 06:57:07 +00007388 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007389
Evan Cheng8ca29322006-11-10 21:43:37 +00007390 if (Opcode)
7391 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00007392 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007393
Chris Lattner83e6c992006-10-04 06:57:07 +00007394 }
7395
Dan Gohman475871a2008-07-27 21:46:04 +00007396 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00007397}
7398
Chris Lattner149a4e52008-02-22 02:09:43 +00007399/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007400static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00007401 const X86Subtarget *Subtarget) {
7402 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
7403 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00007404 // A preferable solution to the general problem is to figure out the right
7405 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00007406 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00007407 if (St->getValue().getValueType().isVector() &&
7408 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00007409 isa<LoadSDNode>(St->getValue()) &&
7410 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
7411 St->getChain().hasOneUse() && !St->isVolatile()) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007412 SDNode* LdVal = St->getValue().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007413 LoadSDNode *Ld = 0;
7414 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00007415 SmallVector<SDValue, 8> Ops;
Gabor Greifba36cb52008-08-28 21:40:38 +00007416 SDNode* ChainVal = St->getChain().getNode();
Dale Johannesen079f2a62008-02-25 19:20:14 +00007417 // Must be a store of a load. We currently handle two cases: the load
7418 // is a direct child, and it's under an intervening TokenFactor. It is
7419 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00007420 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00007421 Ld = cast<LoadSDNode>(St->getChain());
7422 else if (St->getValue().hasOneUse() &&
7423 ChainVal->getOpcode() == ISD::TokenFactor) {
7424 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Gabor Greifba36cb52008-08-28 21:40:38 +00007425 if (ChainVal->getOperand(i).getNode() == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00007426 TokenFactorIndex = i;
7427 Ld = cast<LoadSDNode>(St->getValue());
7428 } else
7429 Ops.push_back(ChainVal->getOperand(i));
7430 }
7431 }
7432 if (Ld) {
7433 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
7434 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00007435 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00007436 Ld->getBasePtr(), Ld->getSrcValue(),
7437 Ld->getSrcValueOffset(), Ld->isVolatile(),
7438 Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00007439 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00007440 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00007441 Ops.push_back(NewChain);
Dale Johannesen079f2a62008-02-25 19:20:14 +00007442 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7443 Ops.size());
7444 }
7445 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
7446 St->getSrcValue(), St->getSrcValueOffset(),
7447 St->isVolatile(), St->getAlignment());
7448 }
7449
7450 // Otherwise, lower to two 32-bit copies.
Dan Gohman475871a2008-07-27 21:46:04 +00007451 SDValue LoAddr = Ld->getBasePtr();
7452 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007453 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00007454
Dan Gohman475871a2008-07-27 21:46:04 +00007455 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00007456 Ld->getSrcValue(), Ld->getSrcValueOffset(),
7457 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00007458 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00007459 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
7460 Ld->isVolatile(),
7461 MinAlign(Ld->getAlignment(), 4));
7462
Dan Gohman475871a2008-07-27 21:46:04 +00007463 SDValue NewChain = LoLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00007464 if (TokenFactorIndex != -1) {
7465 Ops.push_back(LoLd);
7466 Ops.push_back(HiLd);
7467 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
7468 Ops.size());
7469 }
7470
7471 LoAddr = St->getBasePtr();
7472 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007473 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00007474
Dan Gohman475871a2008-07-27 21:46:04 +00007475 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00007476 St->getSrcValue(), St->getSrcValueOffset(),
7477 St->isVolatile(), St->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00007478 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Gabor Greif327ef032008-08-28 23:19:51 +00007479 St->getSrcValue(),
7480 St->getSrcValueOffset() + 4,
Dale Johannesen079f2a62008-02-25 19:20:14 +00007481 St->isVolatile(),
7482 MinAlign(St->getAlignment(), 4));
7483 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00007484 }
Chris Lattner149a4e52008-02-22 02:09:43 +00007485 }
Dan Gohman475871a2008-07-27 21:46:04 +00007486 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00007487}
7488
Chris Lattner6cf73262008-01-25 06:14:17 +00007489/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
7490/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007491static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00007492 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
7493 // F[X]OR(0.0, x) -> x
7494 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00007495 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7496 if (C->getValueAPF().isPosZero())
7497 return N->getOperand(1);
7498 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7499 if (C->getValueAPF().isPosZero())
7500 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00007501 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00007502}
7503
7504/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00007505static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00007506 // FAND(0.0, x) -> 0.0
7507 // FAND(x, 0.0) -> 0.0
7508 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
7509 if (C->getValueAPF().isPosZero())
7510 return N->getOperand(0);
7511 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
7512 if (C->getValueAPF().isPosZero())
7513 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00007514 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00007515}
7516
Chris Lattner83e6c992006-10-04 06:57:07 +00007517
Dan Gohman475871a2008-07-27 21:46:04 +00007518SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00007519 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00007520 SelectionDAG &DAG = DCI.DAG;
7521 switch (N->getOpcode()) {
7522 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00007523 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
7524 case ISD::BUILD_VECTOR:
7525 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00007526 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00007527 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00007528 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00007529 case X86ISD::FOR: return PerformFORCombine(N, DAG);
7530 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00007531 }
7532
Dan Gohman475871a2008-07-27 21:46:04 +00007533 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00007534}
7535
Evan Cheng60c07e12006-07-05 22:17:51 +00007536//===----------------------------------------------------------------------===//
7537// X86 Inline Assembly Support
7538//===----------------------------------------------------------------------===//
7539
Chris Lattnerf4dff842006-07-11 02:54:03 +00007540/// getConstraintType - Given a constraint letter, return the type of
7541/// constraint it is for this target.
7542X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00007543X86TargetLowering::getConstraintType(const std::string &Constraint) const {
7544 if (Constraint.size() == 1) {
7545 switch (Constraint[0]) {
7546 case 'A':
Chris Lattnerfce84ac2008-03-11 19:06:29 +00007547 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00007548 case 'r':
7549 case 'R':
7550 case 'l':
7551 case 'q':
7552 case 'Q':
7553 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00007554 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00007555 case 'Y':
7556 return C_RegisterClass;
7557 default:
7558 break;
7559 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00007560 }
Chris Lattner4234f572007-03-25 02:14:49 +00007561 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00007562}
7563
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007564/// LowerXConstraint - try to replace an X constraint, which matches anything,
7565/// with another that has more specific requirements based on the type of the
7566/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00007567const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00007568LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00007569 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7570 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007571 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007572 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00007573 return "Y";
7574 if (Subtarget->hasSSE1())
7575 return "x";
7576 }
7577
7578 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007579}
7580
Chris Lattner48884cd2007-08-25 00:47:38 +00007581/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7582/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00007583void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00007584 char Constraint,
Evan Chengda43bcf2008-09-24 00:05:32 +00007585 bool hasMemory,
Dan Gohman475871a2008-07-27 21:46:04 +00007586 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00007587 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007588 SDValue Result(0, 0);
Chris Lattner48884cd2007-08-25 00:47:38 +00007589
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007590 switch (Constraint) {
7591 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00007592 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00007593 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007594 if (C->getZExtValue() <= 31) {
7595 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00007596 break;
7597 }
Devang Patel84f7fd22007-03-17 00:13:28 +00007598 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007599 return;
Evan Cheng364091e2008-09-22 23:57:37 +00007600 case 'J':
7601 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
7602 if (C->getZExtValue() <= 63) {
7603 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
7604 break;
7605 }
7606 }
7607 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00007608 case 'N':
7609 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007610 if (C->getZExtValue() <= 255) {
7611 Result = DAG.getTargetConstant(C->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00007612 break;
7613 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00007614 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007615 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00007616 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007617 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00007618 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007619 Result = DAG.getTargetConstant(CST->getZExtValue(), Op.getValueType());
Chris Lattner48884cd2007-08-25 00:47:38 +00007620 break;
7621 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007622
Chris Lattnerdc43a882007-05-03 16:52:29 +00007623 // If we are in non-pic codegen mode, we allow the address of a global (with
7624 // an optional displacement) to be used with 'i'.
7625 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7626 int64_t Offset = 0;
7627
7628 // Match either (GA) or (GA+C)
7629 if (GA) {
7630 Offset = GA->getOffset();
7631 } else if (Op.getOpcode() == ISD::ADD) {
7632 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7633 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7634 if (C && GA) {
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007635 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00007636 } else {
7637 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7638 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7639 if (C && GA)
Dan Gohmanf5aeb1a2008-09-12 16:56:44 +00007640 Offset = GA->getOffset()+C->getZExtValue();
Chris Lattnerdc43a882007-05-03 16:52:29 +00007641 else
7642 C = 0, GA = 0;
7643 }
7644 }
7645
7646 if (GA) {
Evan Chengda43bcf2008-09-24 00:05:32 +00007647 if (hasMemory)
Dan Gohman6520e202008-10-18 02:06:02 +00007648 Op = LowerGlobalAddress(GA->getGlobal(), Offset, DAG);
Evan Chengda43bcf2008-09-24 00:05:32 +00007649 else
7650 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7651 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00007652 Result = Op;
7653 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007654 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007655
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007656 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00007657 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007658 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00007659 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007660
Gabor Greifba36cb52008-08-28 21:40:38 +00007661 if (Result.getNode()) {
Chris Lattner48884cd2007-08-25 00:47:38 +00007662 Ops.push_back(Result);
7663 return;
7664 }
Evan Chengda43bcf2008-09-24 00:05:32 +00007665 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, hasMemory,
7666 Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007667}
7668
Chris Lattner259e97c2006-01-31 19:43:35 +00007669std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00007670getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007671 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00007672 if (Constraint.size() == 1) {
7673 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00007674 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00007675 default: break; // Unknown constraint letter
7676 case 'A': // EAX/EDX
7677 if (VT == MVT::i32 || VT == MVT::i64)
7678 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7679 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00007680 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7681 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00007682 if (VT == MVT::i32)
7683 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7684 else if (VT == MVT::i16)
7685 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7686 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00007687 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00007688 else if (VT == MVT::i64)
7689 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7690 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00007691 }
7692 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007693
Chris Lattner1efa40f2006-02-22 00:56:39 +00007694 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00007695}
Chris Lattnerf76d1802006-07-31 23:26:50 +00007696
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007697std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00007698X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007699 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00007700 // First, see if this is a constraint that directly corresponds to an LLVM
7701 // register class.
7702 if (Constraint.size() == 1) {
7703 // GCC Constraint Letters
7704 switch (Constraint[0]) {
7705 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00007706 case 'r': // GENERAL_REGS
7707 case 'R': // LEGACY_REGS
7708 case 'l': // INDEX_REGS
Chris Lattner1fa71982008-10-17 18:15:05 +00007709 if (VT == MVT::i8)
Chris Lattner0f65cad2007-04-09 05:49:22 +00007710 return std::make_pair(0U, X86::GR8RegisterClass);
Chris Lattner1fa71982008-10-17 18:15:05 +00007711 if (VT == MVT::i16)
7712 return std::make_pair(0U, X86::GR16RegisterClass);
7713 if (VT == MVT::i32 || !Subtarget->is64Bit())
7714 return std::make_pair(0U, X86::GR32RegisterClass);
7715 return std::make_pair(0U, X86::GR64RegisterClass);
Chris Lattnerfce84ac2008-03-11 19:06:29 +00007716 case 'f': // FP Stack registers.
7717 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7718 // value to the correct fpstack register class.
7719 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7720 return std::make_pair(0U, X86::RFP32RegisterClass);
7721 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7722 return std::make_pair(0U, X86::RFP64RegisterClass);
7723 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00007724 case 'y': // MMX_REGS if MMX allowed.
7725 if (!Subtarget->hasMMX()) break;
7726 return std::make_pair(0U, X86::VR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007727 case 'Y': // SSE_REGS if SSE2 allowed
7728 if (!Subtarget->hasSSE2()) break;
7729 // FALL THROUGH.
7730 case 'x': // SSE_REGS if SSE1 allowed
7731 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007732
7733 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00007734 default: break;
7735 // Scalar SSE types.
7736 case MVT::f32:
7737 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00007738 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007739 case MVT::f64:
7740 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00007741 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007742 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00007743 case MVT::v16i8:
7744 case MVT::v8i16:
7745 case MVT::v4i32:
7746 case MVT::v2i64:
7747 case MVT::v4f32:
7748 case MVT::v2f64:
7749 return std::make_pair(0U, X86::VR128RegisterClass);
7750 }
Chris Lattnerad043e82007-04-09 05:11:28 +00007751 break;
7752 }
7753 }
7754
Chris Lattnerf76d1802006-07-31 23:26:50 +00007755 // Use the default implementation in TargetLowering to convert the register
7756 // constraint into a member of a register class.
7757 std::pair<unsigned, const TargetRegisterClass*> Res;
7758 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00007759
7760 // Not found as a standard register?
7761 if (Res.second == 0) {
7762 // GCC calls "st(0)" just plain "st".
7763 if (StringsEqualNoCase("{st}", Constraint)) {
7764 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00007765 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00007766 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007767
Chris Lattner1a60aa72006-10-31 19:42:44 +00007768 return Res;
7769 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007770
Chris Lattnerf76d1802006-07-31 23:26:50 +00007771 // Otherwise, check to see if this is a register class of the wrong value
7772 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7773 // turn into {ax},{dx}.
7774 if (Res.second->hasType(VT))
7775 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007776
Chris Lattnerf76d1802006-07-31 23:26:50 +00007777 // All of the single-register GCC register classes map their values onto
7778 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7779 // really want an 8-bit or 32-bit register, map to the appropriate register
7780 // class and return the appropriate register.
Chris Lattner6ba50a92008-08-26 06:19:02 +00007781 if (Res.second == X86::GR16RegisterClass) {
7782 if (VT == MVT::i8) {
7783 unsigned DestReg = 0;
7784 switch (Res.first) {
7785 default: break;
7786 case X86::AX: DestReg = X86::AL; break;
7787 case X86::DX: DestReg = X86::DL; break;
7788 case X86::CX: DestReg = X86::CL; break;
7789 case X86::BX: DestReg = X86::BL; break;
7790 }
7791 if (DestReg) {
7792 Res.first = DestReg;
7793 Res.second = Res.second = X86::GR8RegisterClass;
7794 }
7795 } else if (VT == MVT::i32) {
7796 unsigned DestReg = 0;
7797 switch (Res.first) {
7798 default: break;
7799 case X86::AX: DestReg = X86::EAX; break;
7800 case X86::DX: DestReg = X86::EDX; break;
7801 case X86::CX: DestReg = X86::ECX; break;
7802 case X86::BX: DestReg = X86::EBX; break;
7803 case X86::SI: DestReg = X86::ESI; break;
7804 case X86::DI: DestReg = X86::EDI; break;
7805 case X86::BP: DestReg = X86::EBP; break;
7806 case X86::SP: DestReg = X86::ESP; break;
7807 }
7808 if (DestReg) {
7809 Res.first = DestReg;
7810 Res.second = Res.second = X86::GR32RegisterClass;
7811 }
7812 } else if (VT == MVT::i64) {
7813 unsigned DestReg = 0;
7814 switch (Res.first) {
7815 default: break;
7816 case X86::AX: DestReg = X86::RAX; break;
7817 case X86::DX: DestReg = X86::RDX; break;
7818 case X86::CX: DestReg = X86::RCX; break;
7819 case X86::BX: DestReg = X86::RBX; break;
7820 case X86::SI: DestReg = X86::RSI; break;
7821 case X86::DI: DestReg = X86::RDI; break;
7822 case X86::BP: DestReg = X86::RBP; break;
7823 case X86::SP: DestReg = X86::RSP; break;
7824 }
7825 if (DestReg) {
7826 Res.first = DestReg;
7827 Res.second = Res.second = X86::GR64RegisterClass;
7828 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00007829 }
Chris Lattner6ba50a92008-08-26 06:19:02 +00007830 } else if (Res.second == X86::FR32RegisterClass ||
7831 Res.second == X86::FR64RegisterClass ||
7832 Res.second == X86::VR128RegisterClass) {
7833 // Handle references to XMM physical registers that got mapped into the
7834 // wrong class. This can happen with constraints like {xmm0} where the
7835 // target independent register mapper will just pick the first match it can
7836 // find, ignoring the required type.
7837 if (VT == MVT::f32)
7838 Res.second = X86::FR32RegisterClass;
7839 else if (VT == MVT::f64)
7840 Res.second = X86::FR64RegisterClass;
7841 else if (X86::VR128RegisterClass->hasType(VT))
7842 Res.second = X86::VR128RegisterClass;
Chris Lattnerf76d1802006-07-31 23:26:50 +00007843 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007844
Chris Lattnerf76d1802006-07-31 23:26:50 +00007845 return Res;
7846}
Mon P Wang0c397192008-10-30 08:01:45 +00007847
7848//===----------------------------------------------------------------------===//
7849// X86 Widen vector type
7850//===----------------------------------------------------------------------===//
7851
7852/// getWidenVectorType: given a vector type, returns the type to widen
7853/// to (e.g., v7i8 to v8i8). If the vector type is legal, it returns itself.
7854/// If there is no vector type that we want to widen to, returns MVT::Other
7855/// When and were to widen is target dependent based on the cost of
7856/// scalarizing vs using the wider vector type.
7857
7858MVT X86TargetLowering::getWidenVectorType(MVT VT) {
7859 assert(VT.isVector());
7860 if (isTypeLegal(VT))
7861 return VT;
7862
7863 // TODO: In computeRegisterProperty, we can compute the list of legal vector
7864 // type based on element type. This would speed up our search (though
7865 // it may not be worth it since the size of the list is relatively
7866 // small).
7867 MVT EltVT = VT.getVectorElementType();
7868 unsigned NElts = VT.getVectorNumElements();
7869
7870 // On X86, it make sense to widen any vector wider than 1
7871 if (NElts <= 1)
7872 return MVT::Other;
7873
7874 for (unsigned nVT = MVT::FIRST_VECTOR_VALUETYPE;
7875 nVT <= MVT::LAST_VECTOR_VALUETYPE; ++nVT) {
7876 MVT SVT = (MVT::SimpleValueType)nVT;
7877
7878 if (isTypeLegal(SVT) &&
7879 SVT.getVectorElementType() == EltVT &&
7880 SVT.getVectorNumElements() > NElts)
7881 return SVT;
7882 }
7883 return MVT::Other;
7884}