blob: a6bffe285f7dd35d1ba2d7c22eee7b3583ab9366 [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"
Dan Gohman1adf1b02008-08-19 21:45:35 +000020#include "X86FastISel.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000021#include "llvm/CallingConv.h"
Evan Cheng223547a2006-01-31 22:28:30 +000022#include "llvm/Constants.h"
Evan Cheng347d5f72006-04-28 21:29:37 +000023#include "llvm/DerivedTypes.h"
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +000024#include "llvm/GlobalVariable.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000025#include "llvm/Function.h"
Evan Cheng6be2c582006-04-05 23:38:46 +000026#include "llvm/Intrinsics.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000027#include "llvm/ADT/BitVector.h"
Evan Cheng30b37b52006-03-13 23:18:16 +000028#include "llvm/ADT/VectorExtras.h"
Chris Lattner362e98a2007-02-27 04:43:02 +000029#include "llvm/CodeGen/CallingConvLower.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000030#include "llvm/CodeGen/MachineFrameInfo.h"
Evan Cheng4a460802006-01-11 00:33:36 +000031#include "llvm/CodeGen/MachineFunction.h"
32#include "llvm/CodeGen/MachineInstrBuilder.h"
Evan Chenga844bde2008-02-02 04:07:54 +000033#include "llvm/CodeGen/MachineModuleInfo.h"
Chris Lattner84bc5422007-12-31 04:13:23 +000034#include "llvm/CodeGen/MachineRegisterInfo.h"
Dan Gohman69de1932008-02-06 22:27:42 +000035#include "llvm/CodeGen/PseudoSourceValue.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000036#include "llvm/CodeGen/SelectionDAG.h"
Evan Chengef6ffb12006-01-31 03:14:29 +000037#include "llvm/Support/MathExtras.h"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000038#include "llvm/Support/Debug.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000039#include "llvm/Target/TargetOptions.h"
Evan Cheng14b32e12007-12-11 01:46:18 +000040#include "llvm/ADT/SmallSet.h"
Chris Lattner1a60aa72006-10-31 19:42:44 +000041#include "llvm/ADT/StringExtras.h"
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000042using namespace llvm;
43
Evan Cheng10e86422008-04-25 19:11:04 +000044// Forward declarations.
Dan Gohman475871a2008-07-27 21:46:04 +000045static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG);
Evan Cheng10e86422008-04-25 19:11:04 +000046
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +000047X86TargetLowering::X86TargetLowering(X86TargetMachine &TM)
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000048 : TargetLowering(TM) {
Evan Cheng559806f2006-01-27 08:10:46 +000049 Subtarget = &TM.getSubtarget<X86Subtarget>();
Dale Johannesenf1fc3a82007-09-23 14:52:20 +000050 X86ScalarSSEf64 = Subtarget->hasSSE2();
51 X86ScalarSSEf32 = Subtarget->hasSSE1();
Evan Cheng25ab6902006-09-08 06:48:29 +000052 X86StackPtr = Subtarget->is64Bit() ? X86::RSP : X86::ESP;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +000053
Chris Lattnerd43d00c2008-01-24 08:07:48 +000054 bool Fast = false;
Evan Cheng559806f2006-01-27 08:10:46 +000055
Anton Korobeynikov2365f512007-07-14 14:06:15 +000056 RegInfo = TM.getRegisterInfo();
57
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000058 // Set up the TargetLowering object.
59
60 // X86 is weird, it always uses i8 for shift amounts and setcc results.
61 setShiftAmountType(MVT::i8);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000062 setSetCCResultContents(ZeroOrOneSetCCResult);
Evan Cheng0b2afbd2006-01-25 09:15:17 +000063 setSchedulingPreference(SchedulingForRegPressure);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000064 setShiftAmountFlavor(Mask); // shl X, 32 == shl X, 0
Evan Cheng25ab6902006-09-08 06:48:29 +000065 setStackPointerRegisterToSaveRestore(X86StackPtr);
Evan Cheng714554d2006-03-16 21:47:42 +000066
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000067 if (Subtarget->isTargetDarwin()) {
Evan Chengdf57fa02006-03-17 20:31:41 +000068 // Darwin should use _setjmp/_longjmp instead of setjmp/longjmp.
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000069 setUseUnderscoreSetJmp(false);
70 setUseUnderscoreLongJmp(false);
Anton Korobeynikov317848f2007-01-03 11:43:14 +000071 } else if (Subtarget->isTargetMingw()) {
Anton Korobeynikovd27a2582006-12-10 23:12:42 +000072 // MS runtime is weird: it exports _setjmp, but longjmp!
73 setUseUnderscoreSetJmp(true);
74 setUseUnderscoreLongJmp(false);
75 } else {
76 setUseUnderscoreSetJmp(true);
77 setUseUnderscoreLongJmp(true);
78 }
79
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000080 // Set up the register classes.
Evan Cheng069287d2006-05-16 07:21:53 +000081 addRegisterClass(MVT::i8, X86::GR8RegisterClass);
82 addRegisterClass(MVT::i16, X86::GR16RegisterClass);
83 addRegisterClass(MVT::i32, X86::GR32RegisterClass);
Evan Cheng25ab6902006-09-08 06:48:29 +000084 if (Subtarget->is64Bit())
85 addRegisterClass(MVT::i64, X86::GR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000086
Duncan Sandsf9c98e62008-01-23 20:39:46 +000087 setLoadXAction(ISD::SEXTLOAD, MVT::i1, Promote);
Evan Chengc5484282006-10-04 00:56:09 +000088
Chris Lattnerddf89562008-01-17 19:59:44 +000089 // We don't accept any truncstore of integer registers.
90 setTruncStoreAction(MVT::i64, MVT::i32, Expand);
91 setTruncStoreAction(MVT::i64, MVT::i16, Expand);
92 setTruncStoreAction(MVT::i64, MVT::i8 , Expand);
93 setTruncStoreAction(MVT::i32, MVT::i16, Expand);
94 setTruncStoreAction(MVT::i32, MVT::i8 , Expand);
95 setTruncStoreAction(MVT::i16, MVT::i8, Expand);
96
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +000097 // Promote all UINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have this
98 // operation.
99 setOperationAction(ISD::UINT_TO_FP , MVT::i1 , Promote);
100 setOperationAction(ISD::UINT_TO_FP , MVT::i8 , Promote);
101 setOperationAction(ISD::UINT_TO_FP , MVT::i16 , Promote);
Evan Cheng6892f282006-01-17 02:32:49 +0000102
Evan Cheng25ab6902006-09-08 06:48:29 +0000103 if (Subtarget->is64Bit()) {
104 setOperationAction(ISD::UINT_TO_FP , MVT::i64 , Expand);
Evan Cheng6892f282006-01-17 02:32:49 +0000105 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000106 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000107 if (X86ScalarSSEf64)
Evan Cheng25ab6902006-09-08 06:48:29 +0000108 // If SSE i64 SINT_TO_FP is not available, expand i32 UINT_TO_FP.
109 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Expand);
110 else
111 setOperationAction(ISD::UINT_TO_FP , MVT::i32 , Promote);
112 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000113
114 // Promote i1/i8 SINT_TO_FP to larger SINT_TO_FP's, as X86 doesn't have
115 // this operation.
116 setOperationAction(ISD::SINT_TO_FP , MVT::i1 , Promote);
117 setOperationAction(ISD::SINT_TO_FP , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000118 // SSE has no i16 to fp conversion, only i32
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000119 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000120 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000121 // f32 and f64 cases are Legal, f80 case is not
122 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
123 } else {
Evan Cheng5298bcc2006-02-17 07:01:52 +0000124 setOperationAction(ISD::SINT_TO_FP , MVT::i16 , Custom);
125 setOperationAction(ISD::SINT_TO_FP , MVT::i32 , Custom);
126 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000127
Dale Johannesen73328d12007-09-19 23:55:34 +0000128 // In 32-bit mode these are custom lowered. In 64-bit mode F32 and F64
129 // are Legal, f80 is custom lowered.
130 setOperationAction(ISD::FP_TO_SINT , MVT::i64 , Custom);
131 setOperationAction(ISD::SINT_TO_FP , MVT::i64 , Custom);
Evan Cheng6dab0532006-01-30 08:02:57 +0000132
Evan Cheng02568ff2006-01-30 22:13:22 +0000133 // Promote i1/i8 FP_TO_SINT to larger FP_TO_SINTS's, as X86 doesn't have
134 // this operation.
135 setOperationAction(ISD::FP_TO_SINT , MVT::i1 , Promote);
136 setOperationAction(ISD::FP_TO_SINT , MVT::i8 , Promote);
137
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000138 if (X86ScalarSSEf32) {
Evan Cheng02568ff2006-01-30 22:13:22 +0000139 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Promote);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000140 // f32 and f64 cases are Legal, f80 case is not
141 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000142 } else {
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000143 setOperationAction(ISD::FP_TO_SINT , MVT::i16 , Custom);
Evan Cheng02568ff2006-01-30 22:13:22 +0000144 setOperationAction(ISD::FP_TO_SINT , MVT::i32 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000145 }
146
147 // Handle FP_TO_UINT by promoting the destination to a larger signed
148 // conversion.
149 setOperationAction(ISD::FP_TO_UINT , MVT::i1 , Promote);
150 setOperationAction(ISD::FP_TO_UINT , MVT::i8 , Promote);
151 setOperationAction(ISD::FP_TO_UINT , MVT::i16 , Promote);
152
Evan Cheng25ab6902006-09-08 06:48:29 +0000153 if (Subtarget->is64Bit()) {
154 setOperationAction(ISD::FP_TO_UINT , MVT::i64 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000155 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
Evan Cheng25ab6902006-09-08 06:48:29 +0000156 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000157 if (X86ScalarSSEf32 && !Subtarget->hasSSE3())
Evan Cheng25ab6902006-09-08 06:48:29 +0000158 // Expand FP_TO_UINT into a select.
159 // FIXME: We would like to use a Custom expander here eventually to do
160 // the optimal thing for SSE vs. the default expansion in the legalizer.
161 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Expand);
162 else
163 // With SSE3 we can use fisttpll to convert to a signed i64.
164 setOperationAction(ISD::FP_TO_UINT , MVT::i32 , Promote);
165 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000166
Chris Lattner399610a2006-12-05 18:22:22 +0000167 // TODO: when we have SSE, these could be more efficient, by using movd/movq.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000168 if (!X86ScalarSSEf64) {
Chris Lattnerf3597a12006-12-05 18:45:06 +0000169 setOperationAction(ISD::BIT_CONVERT , MVT::f32 , Expand);
170 setOperationAction(ISD::BIT_CONVERT , MVT::i32 , Expand);
171 }
Chris Lattner21f66852005-12-23 05:15:23 +0000172
Dan Gohmanb00ee212008-02-18 19:34:53 +0000173 // Scalar integer divide and remainder are lowered to use operations that
174 // produce two results, to match the available instructions. This exposes
175 // the two-result form to trivial CSE, which is able to combine x/y and x%y
176 // into a single instruction.
177 //
178 // Scalar integer multiply-high is also lowered to use two-result
179 // operations, to match the available instructions. However, plain multiply
180 // (low) operations are left as Legal, as there are single-result
181 // instructions for this in x86. Using the two-result multiply instructions
182 // when both high and low results are needed must be arranged by dagcombine.
Dan Gohman525178c2007-10-08 18:33:35 +0000183 setOperationAction(ISD::MULHS , MVT::i8 , Expand);
184 setOperationAction(ISD::MULHU , MVT::i8 , Expand);
185 setOperationAction(ISD::SDIV , MVT::i8 , Expand);
186 setOperationAction(ISD::UDIV , MVT::i8 , Expand);
187 setOperationAction(ISD::SREM , MVT::i8 , Expand);
188 setOperationAction(ISD::UREM , MVT::i8 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000189 setOperationAction(ISD::MULHS , MVT::i16 , Expand);
190 setOperationAction(ISD::MULHU , MVT::i16 , Expand);
191 setOperationAction(ISD::SDIV , MVT::i16 , Expand);
192 setOperationAction(ISD::UDIV , MVT::i16 , Expand);
193 setOperationAction(ISD::SREM , MVT::i16 , Expand);
194 setOperationAction(ISD::UREM , MVT::i16 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000195 setOperationAction(ISD::MULHS , MVT::i32 , Expand);
196 setOperationAction(ISD::MULHU , MVT::i32 , Expand);
197 setOperationAction(ISD::SDIV , MVT::i32 , Expand);
198 setOperationAction(ISD::UDIV , MVT::i32 , Expand);
199 setOperationAction(ISD::SREM , MVT::i32 , Expand);
200 setOperationAction(ISD::UREM , MVT::i32 , Expand);
Dan Gohman525178c2007-10-08 18:33:35 +0000201 setOperationAction(ISD::MULHS , MVT::i64 , Expand);
202 setOperationAction(ISD::MULHU , MVT::i64 , Expand);
203 setOperationAction(ISD::SDIV , MVT::i64 , Expand);
204 setOperationAction(ISD::UDIV , MVT::i64 , Expand);
205 setOperationAction(ISD::SREM , MVT::i64 , Expand);
206 setOperationAction(ISD::UREM , MVT::i64 , Expand);
Dan Gohmana37c9f72007-09-25 18:23:27 +0000207
Evan Chengc35497f2006-10-30 08:02:39 +0000208 setOperationAction(ISD::BR_JT , MVT::Other, Expand);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000209 setOperationAction(ISD::BRCOND , MVT::Other, Custom);
Nate Begeman750ac1b2006-02-01 07:19:44 +0000210 setOperationAction(ISD::BR_CC , MVT::Other, Expand);
211 setOperationAction(ISD::SELECT_CC , MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000212 if (Subtarget->is64Bit())
Christopher Lambc59e5212007-08-10 21:48:46 +0000213 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i32, Legal);
214 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i16 , Legal);
215 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i8 , Legal);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000216 setOperationAction(ISD::SIGN_EXTEND_INREG, MVT::i1 , Expand);
217 setOperationAction(ISD::FP_ROUND_INREG , MVT::f32 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000218 setOperationAction(ISD::FREM , MVT::f32 , Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000219 setOperationAction(ISD::FREM , MVT::f64 , Expand);
Chris Lattnerd1108222008-03-07 06:36:32 +0000220 setOperationAction(ISD::FREM , MVT::f80 , Expand);
Dan Gohman1a024862008-01-31 00:41:03 +0000221 setOperationAction(ISD::FLT_ROUNDS_ , MVT::i32 , Custom);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +0000222
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000223 setOperationAction(ISD::CTPOP , MVT::i8 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000224 setOperationAction(ISD::CTTZ , MVT::i8 , Custom);
225 setOperationAction(ISD::CTLZ , MVT::i8 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000226 setOperationAction(ISD::CTPOP , MVT::i16 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000227 setOperationAction(ISD::CTTZ , MVT::i16 , Custom);
228 setOperationAction(ISD::CTLZ , MVT::i16 , Custom);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000229 setOperationAction(ISD::CTPOP , MVT::i32 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000230 setOperationAction(ISD::CTTZ , MVT::i32 , Custom);
231 setOperationAction(ISD::CTLZ , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000232 if (Subtarget->is64Bit()) {
233 setOperationAction(ISD::CTPOP , MVT::i64 , Expand);
Evan Cheng18efe262007-12-14 02:13:44 +0000234 setOperationAction(ISD::CTTZ , MVT::i64 , Custom);
235 setOperationAction(ISD::CTLZ , MVT::i64 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000236 }
237
Andrew Lenharthb873ff32005-11-20 21:41:10 +0000238 setOperationAction(ISD::READCYCLECOUNTER , MVT::i64 , Custom);
Nate Begemand88fc032006-01-14 03:14:10 +0000239 setOperationAction(ISD::BSWAP , MVT::i16 , Expand);
Nate Begeman35ef9132006-01-11 21:21:00 +0000240
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000241 // These should be promoted to a larger select which is supported.
242 setOperationAction(ISD::SELECT , MVT::i1 , Promote);
243 setOperationAction(ISD::SELECT , MVT::i8 , Promote);
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000244 // X86 wants to expand cmov itself.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000245 setOperationAction(ISD::SELECT , MVT::i16 , Custom);
246 setOperationAction(ISD::SELECT , MVT::i32 , Custom);
247 setOperationAction(ISD::SELECT , MVT::f32 , Custom);
248 setOperationAction(ISD::SELECT , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000249 setOperationAction(ISD::SELECT , MVT::f80 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000250 setOperationAction(ISD::SETCC , MVT::i8 , Custom);
251 setOperationAction(ISD::SETCC , MVT::i16 , Custom);
252 setOperationAction(ISD::SETCC , MVT::i32 , Custom);
253 setOperationAction(ISD::SETCC , MVT::f32 , Custom);
254 setOperationAction(ISD::SETCC , MVT::f64 , Custom);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000255 setOperationAction(ISD::SETCC , MVT::f80 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000256 if (Subtarget->is64Bit()) {
257 setOperationAction(ISD::SELECT , MVT::i64 , Custom);
258 setOperationAction(ISD::SETCC , MVT::i64 , Custom);
259 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000260 // X86 ret instruction may pop stack.
Evan Cheng5298bcc2006-02-17 07:01:52 +0000261 setOperationAction(ISD::RET , MVT::Other, Custom);
Anton Korobeynikov2365f512007-07-14 14:06:15 +0000262 if (!Subtarget->is64Bit())
263 setOperationAction(ISD::EH_RETURN , MVT::Other, Custom);
264
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000265 // Darwin ABI issue.
Evan Cheng7ccced62006-02-18 00:15:05 +0000266 setOperationAction(ISD::ConstantPool , MVT::i32 , Custom);
Nate Begeman37efe672006-04-22 18:53:45 +0000267 setOperationAction(ISD::JumpTable , MVT::i32 , Custom);
Evan Cheng5298bcc2006-02-17 07:01:52 +0000268 setOperationAction(ISD::GlobalAddress , MVT::i32 , Custom);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +0000269 setOperationAction(ISD::GlobalTLSAddress, MVT::i32 , Custom);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +0000270 if (Subtarget->is64Bit())
271 setOperationAction(ISD::GlobalTLSAddress, MVT::i64, Custom);
Evan Cheng020d2e82006-02-23 20:41:18 +0000272 setOperationAction(ISD::ExternalSymbol , MVT::i32 , Custom);
Evan Cheng25ab6902006-09-08 06:48:29 +0000273 if (Subtarget->is64Bit()) {
274 setOperationAction(ISD::ConstantPool , MVT::i64 , Custom);
275 setOperationAction(ISD::JumpTable , MVT::i64 , Custom);
276 setOperationAction(ISD::GlobalAddress , MVT::i64 , Custom);
277 setOperationAction(ISD::ExternalSymbol, MVT::i64 , Custom);
278 }
Nate Begeman4c5dcf52006-02-17 00:03:04 +0000279 // 64-bit addm sub, shl, sra, srl (iff 32-bit x86)
Evan Cheng5298bcc2006-02-17 07:01:52 +0000280 setOperationAction(ISD::SHL_PARTS , MVT::i32 , Custom);
281 setOperationAction(ISD::SRA_PARTS , MVT::i32 , Custom);
282 setOperationAction(ISD::SRL_PARTS , MVT::i32 , Custom);
Dan Gohman4c1fa612008-03-03 22:22:09 +0000283 if (Subtarget->is64Bit()) {
284 setOperationAction(ISD::SHL_PARTS , MVT::i64 , Custom);
285 setOperationAction(ISD::SRA_PARTS , MVT::i64 , Custom);
286 setOperationAction(ISD::SRL_PARTS , MVT::i64 , Custom);
287 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000288
Evan Chengd2cde682008-03-10 19:38:10 +0000289 if (Subtarget->hasSSE1())
290 setOperationAction(ISD::PREFETCH , MVT::Other, Legal);
Evan Cheng27b7db52008-03-08 00:58:38 +0000291
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000292 if (!Subtarget->hasSSE2())
293 setOperationAction(ISD::MEMBARRIER , MVT::Other, Expand);
294
Mon P Wang63307c32008-05-05 19:05:59 +0000295 // Expand certain atomics
Mon P Wang28873102008-06-25 08:15:39 +0000296 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i8, Custom);
297 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i16, Custom);
298 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i32, Custom);
299 setOperationAction(ISD::ATOMIC_CMP_SWAP , MVT::i64, Custom);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000300
Andrew Lenharth08ca62b2008-08-03 20:17:34 +0000301 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i8, Expand);
302 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i16, Expand);
Mon P Wang28873102008-06-25 08:15:39 +0000303 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i32, Expand);
Bill Wendling5bf1b4e2008-08-20 00:28:16 +0000304 setOperationAction(ISD::ATOMIC_LOAD_SUB , MVT::i64, Expand);
Andrew Lenharthd497d9f2008-02-16 14:46:26 +0000305
Dan Gohman7f460202008-06-30 20:59:49 +0000306 // Use the default ISD::DBG_STOPPOINT, ISD::DECLARE expansion.
307 setOperationAction(ISD::DBG_STOPPOINT, MVT::Other, Expand);
Evan Cheng3c992d22006-03-07 02:02:57 +0000308 // FIXME - use subtarget debug flags
Anton Korobeynikovab4022f2006-10-31 08:31:24 +0000309 if (!Subtarget->isTargetDarwin() &&
310 !Subtarget->isTargetELF() &&
Dan Gohman44066042008-07-01 00:05:16 +0000311 !Subtarget->isTargetCygMing()) {
312 setOperationAction(ISD::DBG_LABEL, MVT::Other, Expand);
313 setOperationAction(ISD::EH_LABEL, MVT::Other, Expand);
314 }
Chris Lattnerf73bae12005-11-29 06:16:21 +0000315
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000316 setOperationAction(ISD::EXCEPTIONADDR, MVT::i64, Expand);
317 setOperationAction(ISD::EHSELECTION, MVT::i64, Expand);
318 setOperationAction(ISD::EXCEPTIONADDR, MVT::i32, Expand);
319 setOperationAction(ISD::EHSELECTION, MVT::i32, Expand);
320 if (Subtarget->is64Bit()) {
321 // FIXME: Verify
322 setExceptionPointerRegister(X86::RAX);
323 setExceptionSelectorRegister(X86::RDX);
324 } else {
325 setExceptionPointerRegister(X86::EAX);
326 setExceptionSelectorRegister(X86::EDX);
327 }
Anton Korobeynikov38252622007-09-03 00:36:06 +0000328 setOperationAction(ISD::FRAME_TO_ARGS_OFFSET, MVT::i32, Custom);
Anton Korobeynikovce3b4652007-05-02 19:53:33 +0000329
Duncan Sandsf7331b32007-09-11 14:10:23 +0000330 setOperationAction(ISD::TRAMPOLINE, MVT::Other, Custom);
Duncan Sandsb116fac2007-07-27 20:02:49 +0000331
Chris Lattnerda68d302008-01-15 21:58:22 +0000332 setOperationAction(ISD::TRAP, MVT::Other, Legal);
Anton Korobeynikov66fac792008-01-15 07:02:33 +0000333
Nate Begemanacc398c2006-01-25 18:21:52 +0000334 // VASTART needs to be custom lowered to use the VarArgsFrameIndex
335 setOperationAction(ISD::VASTART , MVT::Other, Custom);
Nate Begemanacc398c2006-01-25 18:21:52 +0000336 setOperationAction(ISD::VAEND , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000337 if (Subtarget->is64Bit()) {
338 setOperationAction(ISD::VAARG , MVT::Other, Custom);
Evan Chengae642192007-03-02 23:16:35 +0000339 setOperationAction(ISD::VACOPY , MVT::Other, Custom);
Dan Gohman9018e832008-05-10 01:26:14 +0000340 } else {
341 setOperationAction(ISD::VAARG , MVT::Other, Expand);
Evan Chengae642192007-03-02 23:16:35 +0000342 setOperationAction(ISD::VACOPY , MVT::Other, Expand);
Dan Gohman9018e832008-05-10 01:26:14 +0000343 }
Evan Chengae642192007-03-02 23:16:35 +0000344
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000345 setOperationAction(ISD::STACKSAVE, MVT::Other, Expand);
Chris Lattnere1125522006-01-15 09:00:21 +0000346 setOperationAction(ISD::STACKRESTORE, MVT::Other, Expand);
Evan Cheng25ab6902006-09-08 06:48:29 +0000347 if (Subtarget->is64Bit())
348 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i64, Expand);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +0000349 if (Subtarget->isTargetCygMing())
350 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Custom);
351 else
352 setOperationAction(ISD::DYNAMIC_STACKALLOC, MVT::i32, Expand);
Chris Lattnerb99329e2006-01-13 02:42:53 +0000353
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000354 if (X86ScalarSSEf64) {
355 // f32 and f64 use SSE.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000356 // Set up the FP register classes.
Evan Cheng5ee4ccc2006-01-12 08:27:59 +0000357 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
358 addRegisterClass(MVT::f64, X86::FR64RegisterClass);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000359
Evan Cheng223547a2006-01-31 22:28:30 +0000360 // Use ANDPD to simulate FABS.
361 setOperationAction(ISD::FABS , MVT::f64, Custom);
362 setOperationAction(ISD::FABS , MVT::f32, Custom);
363
364 // Use XORP to simulate FNEG.
365 setOperationAction(ISD::FNEG , MVT::f64, Custom);
366 setOperationAction(ISD::FNEG , MVT::f32, Custom);
367
Evan Cheng68c47cb2007-01-05 07:55:56 +0000368 // Use ANDPD and ORPD to simulate FCOPYSIGN.
369 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Custom);
370 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
371
Evan Chengd25e9e82006-02-02 00:28:23 +0000372 // We don't support sin/cos/fmod
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000373 setOperationAction(ISD::FSIN , MVT::f64, Expand);
374 setOperationAction(ISD::FCOS , MVT::f64, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000375 setOperationAction(ISD::FSIN , MVT::f32, Expand);
376 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000377
Chris Lattnera54aa942006-01-29 06:26:08 +0000378 // Expand FP immediates into loads from the stack, except for the special
379 // cases we handle.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000380 addLegalFPImmediate(APFloat(+0.0)); // xorpd
381 addLegalFPImmediate(APFloat(+0.0f)); // xorps
Dale Johannesen5411a392007-08-09 01:04:01 +0000382
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000383 // Floating truncations from f80 and extensions to f80 go through memory.
384 // If optimizing, we lie about this though and handle it in
385 // InstructionSelectPreprocess so that dagcombine2 can hack on these.
386 if (Fast) {
387 setConvertAction(MVT::f32, MVT::f80, Expand);
388 setConvertAction(MVT::f64, MVT::f80, Expand);
389 setConvertAction(MVT::f80, MVT::f32, Expand);
390 setConvertAction(MVT::f80, MVT::f64, Expand);
391 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000392 } else if (X86ScalarSSEf32) {
393 // Use SSE for f32, x87 for f64.
394 // Set up the FP register classes.
395 addRegisterClass(MVT::f32, X86::FR32RegisterClass);
396 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
397
398 // Use ANDPS to simulate FABS.
399 setOperationAction(ISD::FABS , MVT::f32, Custom);
400
401 // Use XORP to simulate FNEG.
402 setOperationAction(ISD::FNEG , MVT::f32, Custom);
403
404 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
405
406 // Use ANDPS and ORPS to simulate FCOPYSIGN.
407 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
408 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Custom);
409
410 // We don't support sin/cos/fmod
411 setOperationAction(ISD::FSIN , MVT::f32, Expand);
412 setOperationAction(ISD::FCOS , MVT::f32, Expand);
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000413
Nate Begemane1795842008-02-14 08:57:00 +0000414 // Special cases we handle for FP constants.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000415 addLegalFPImmediate(APFloat(+0.0f)); // xorps
416 addLegalFPImmediate(APFloat(+0.0)); // FLD0
417 addLegalFPImmediate(APFloat(+1.0)); // FLD1
418 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
419 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
420
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000421 // SSE <-> X87 conversions go through memory. If optimizing, we lie about
422 // this though and handle it in InstructionSelectPreprocess so that
423 // dagcombine2 can hack on these.
424 if (Fast) {
425 setConvertAction(MVT::f32, MVT::f64, Expand);
426 setConvertAction(MVT::f32, MVT::f80, Expand);
427 setConvertAction(MVT::f80, MVT::f32, Expand);
428 setConvertAction(MVT::f64, MVT::f32, Expand);
429 // And x87->x87 truncations also.
430 setConvertAction(MVT::f80, MVT::f64, Expand);
431 }
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000432
433 if (!UnsafeFPMath) {
434 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
435 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
436 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000437 } else {
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000438 // f32 and f64 in x87.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000439 // Set up the FP register classes.
Dale Johannesen849f2142007-07-03 00:53:03 +0000440 addRegisterClass(MVT::f64, X86::RFP64RegisterClass);
441 addRegisterClass(MVT::f32, X86::RFP32RegisterClass);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000442
Evan Cheng68c47cb2007-01-05 07:55:56 +0000443 setOperationAction(ISD::UNDEF, MVT::f64, Expand);
Dale Johannesen849f2142007-07-03 00:53:03 +0000444 setOperationAction(ISD::UNDEF, MVT::f32, Expand);
Evan Cheng68c47cb2007-01-05 07:55:56 +0000445 setOperationAction(ISD::FCOPYSIGN, MVT::f64, Expand);
446 setOperationAction(ISD::FCOPYSIGN, MVT::f32, Expand);
Dale Johannesen5411a392007-08-09 01:04:01 +0000447
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000448 // Floating truncations go through memory. If optimizing, we lie about
449 // this though and handle it in InstructionSelectPreprocess so that
450 // dagcombine2 can hack on these.
451 if (Fast) {
452 setConvertAction(MVT::f80, MVT::f32, Expand);
453 setConvertAction(MVT::f64, MVT::f32, Expand);
454 setConvertAction(MVT::f80, MVT::f64, Expand);
455 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000456
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000457 if (!UnsafeFPMath) {
458 setOperationAction(ISD::FSIN , MVT::f64 , Expand);
459 setOperationAction(ISD::FCOS , MVT::f64 , Expand);
460 }
Dale Johannesenf04afdb2007-08-30 00:23:21 +0000461 addLegalFPImmediate(APFloat(+0.0)); // FLD0
462 addLegalFPImmediate(APFloat(+1.0)); // FLD1
463 addLegalFPImmediate(APFloat(-0.0)); // FLD0/FCHS
464 addLegalFPImmediate(APFloat(-1.0)); // FLD1/FCHS
Dale Johannesenf1fc3a82007-09-23 14:52:20 +0000465 addLegalFPImmediate(APFloat(+0.0f)); // FLD0
466 addLegalFPImmediate(APFloat(+1.0f)); // FLD1
467 addLegalFPImmediate(APFloat(-0.0f)); // FLD0/FCHS
468 addLegalFPImmediate(APFloat(-1.0f)); // FLD1/FCHS
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000469 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000470
Dale Johannesen59a58732007-08-05 18:49:15 +0000471 // Long double always uses X87.
472 addRegisterClass(MVT::f80, X86::RFP80RegisterClass);
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +0000473 setOperationAction(ISD::UNDEF, MVT::f80, Expand);
474 setOperationAction(ISD::FCOPYSIGN, MVT::f80, Expand);
Chris Lattner71d07a02008-01-27 06:19:31 +0000475 {
Chris Lattner71d07a02008-01-27 06:19:31 +0000476 APFloat TmpFlt(+0.0);
477 TmpFlt.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
478 addLegalFPImmediate(TmpFlt); // FLD0
479 TmpFlt.changeSign();
480 addLegalFPImmediate(TmpFlt); // FLD0/FCHS
481 APFloat TmpFlt2(+1.0);
482 TmpFlt2.convert(APFloat::x87DoubleExtended, APFloat::rmNearestTiesToEven);
483 addLegalFPImmediate(TmpFlt2); // FLD1
484 TmpFlt2.changeSign();
485 addLegalFPImmediate(TmpFlt2); // FLD1/FCHS
486 }
487
Dale Johannesen2f429012007-09-26 21:10:55 +0000488 if (!UnsafeFPMath) {
489 setOperationAction(ISD::FSIN , MVT::f80 , Expand);
490 setOperationAction(ISD::FCOS , MVT::f80 , Expand);
491 }
Dale Johannesen59a58732007-08-05 18:49:15 +0000492
Dan Gohmanf96e4de2007-10-11 23:21:31 +0000493 // Always use a library call for pow.
494 setOperationAction(ISD::FPOW , MVT::f32 , Expand);
495 setOperationAction(ISD::FPOW , MVT::f64 , Expand);
496 setOperationAction(ISD::FPOW , MVT::f80 , Expand);
497
Evan Chengd30bf012006-03-01 01:11:20 +0000498 // First set operation action for all vector types to expand. Then we
499 // will selectively turn on ones that can be effectively codegen'd.
Dan Gohmanfa0f77d2007-05-18 18:44:07 +0000500 for (unsigned VT = (unsigned)MVT::FIRST_VECTOR_VALUETYPE;
501 VT <= (unsigned)MVT::LAST_VECTOR_VALUETYPE; ++VT) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000502 setOperationAction(ISD::ADD , (MVT::SimpleValueType)VT, Expand);
503 setOperationAction(ISD::SUB , (MVT::SimpleValueType)VT, Expand);
504 setOperationAction(ISD::FADD, (MVT::SimpleValueType)VT, Expand);
505 setOperationAction(ISD::FNEG, (MVT::SimpleValueType)VT, Expand);
506 setOperationAction(ISD::FSUB, (MVT::SimpleValueType)VT, Expand);
507 setOperationAction(ISD::MUL , (MVT::SimpleValueType)VT, Expand);
508 setOperationAction(ISD::FMUL, (MVT::SimpleValueType)VT, Expand);
509 setOperationAction(ISD::SDIV, (MVT::SimpleValueType)VT, Expand);
510 setOperationAction(ISD::UDIV, (MVT::SimpleValueType)VT, Expand);
511 setOperationAction(ISD::FDIV, (MVT::SimpleValueType)VT, Expand);
512 setOperationAction(ISD::SREM, (MVT::SimpleValueType)VT, Expand);
513 setOperationAction(ISD::UREM, (MVT::SimpleValueType)VT, Expand);
514 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Expand);
515 setOperationAction(ISD::VECTOR_SHUFFLE, (MVT::SimpleValueType)VT, Expand);
516 setOperationAction(ISD::EXTRACT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
517 setOperationAction(ISD::INSERT_VECTOR_ELT, (MVT::SimpleValueType)VT, Expand);
518 setOperationAction(ISD::FABS, (MVT::SimpleValueType)VT, Expand);
519 setOperationAction(ISD::FSIN, (MVT::SimpleValueType)VT, Expand);
520 setOperationAction(ISD::FCOS, (MVT::SimpleValueType)VT, Expand);
521 setOperationAction(ISD::FREM, (MVT::SimpleValueType)VT, Expand);
522 setOperationAction(ISD::FPOWI, (MVT::SimpleValueType)VT, Expand);
523 setOperationAction(ISD::FSQRT, (MVT::SimpleValueType)VT, Expand);
524 setOperationAction(ISD::FCOPYSIGN, (MVT::SimpleValueType)VT, Expand);
525 setOperationAction(ISD::SMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
526 setOperationAction(ISD::UMUL_LOHI, (MVT::SimpleValueType)VT, Expand);
527 setOperationAction(ISD::SDIVREM, (MVT::SimpleValueType)VT, Expand);
528 setOperationAction(ISD::UDIVREM, (MVT::SimpleValueType)VT, Expand);
529 setOperationAction(ISD::FPOW, (MVT::SimpleValueType)VT, Expand);
530 setOperationAction(ISD::CTPOP, (MVT::SimpleValueType)VT, Expand);
531 setOperationAction(ISD::CTTZ, (MVT::SimpleValueType)VT, Expand);
532 setOperationAction(ISD::CTLZ, (MVT::SimpleValueType)VT, Expand);
533 setOperationAction(ISD::SHL, (MVT::SimpleValueType)VT, Expand);
534 setOperationAction(ISD::SRA, (MVT::SimpleValueType)VT, Expand);
535 setOperationAction(ISD::SRL, (MVT::SimpleValueType)VT, Expand);
536 setOperationAction(ISD::ROTL, (MVT::SimpleValueType)VT, Expand);
537 setOperationAction(ISD::ROTR, (MVT::SimpleValueType)VT, Expand);
538 setOperationAction(ISD::BSWAP, (MVT::SimpleValueType)VT, Expand);
539 setOperationAction(ISD::VSETCC, (MVT::SimpleValueType)VT, Expand);
Evan Chengd30bf012006-03-01 01:11:20 +0000540 }
541
Evan Chenga88973f2006-03-22 19:22:18 +0000542 if (Subtarget->hasMMX()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000543 addRegisterClass(MVT::v8i8, X86::VR64RegisterClass);
544 addRegisterClass(MVT::v4i16, X86::VR64RegisterClass);
545 addRegisterClass(MVT::v2i32, X86::VR64RegisterClass);
Dale Johannesena68f9012008-06-24 22:01:44 +0000546 addRegisterClass(MVT::v2f32, X86::VR64RegisterClass);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000547 addRegisterClass(MVT::v1i64, X86::VR64RegisterClass);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000548
Evan Chengd30bf012006-03-01 01:11:20 +0000549 // FIXME: add MMX packed arithmetics
Bill Wendlingbc9bffa2007-03-07 05:43:18 +0000550
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000551 setOperationAction(ISD::ADD, MVT::v8i8, Legal);
552 setOperationAction(ISD::ADD, MVT::v4i16, Legal);
553 setOperationAction(ISD::ADD, MVT::v2i32, Legal);
Chris Lattner6c284d72007-04-12 04:14:49 +0000554 setOperationAction(ISD::ADD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000555
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000556 setOperationAction(ISD::SUB, MVT::v8i8, Legal);
557 setOperationAction(ISD::SUB, MVT::v4i16, Legal);
558 setOperationAction(ISD::SUB, MVT::v2i32, Legal);
Dale Johannesen8d26e592007-10-30 01:18:38 +0000559 setOperationAction(ISD::SUB, MVT::v1i64, Legal);
Bill Wendlingc1fb0472007-03-10 09:57:05 +0000560
Bill Wendling74027e92007-03-15 21:24:36 +0000561 setOperationAction(ISD::MULHS, MVT::v4i16, Legal);
562 setOperationAction(ISD::MUL, MVT::v4i16, Legal);
563
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000564 setOperationAction(ISD::AND, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000565 AddPromotedToType (ISD::AND, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000566 setOperationAction(ISD::AND, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000567 AddPromotedToType (ISD::AND, MVT::v4i16, MVT::v1i64);
568 setOperationAction(ISD::AND, MVT::v2i32, Promote);
569 AddPromotedToType (ISD::AND, MVT::v2i32, MVT::v1i64);
570 setOperationAction(ISD::AND, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000571
572 setOperationAction(ISD::OR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000573 AddPromotedToType (ISD::OR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000574 setOperationAction(ISD::OR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000575 AddPromotedToType (ISD::OR, MVT::v4i16, MVT::v1i64);
576 setOperationAction(ISD::OR, MVT::v2i32, Promote);
577 AddPromotedToType (ISD::OR, MVT::v2i32, MVT::v1i64);
578 setOperationAction(ISD::OR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000579
580 setOperationAction(ISD::XOR, MVT::v8i8, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000581 AddPromotedToType (ISD::XOR, MVT::v8i8, MVT::v1i64);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000582 setOperationAction(ISD::XOR, MVT::v4i16, Promote);
Bill Wendlingab5b49d2007-03-26 08:03:33 +0000583 AddPromotedToType (ISD::XOR, MVT::v4i16, MVT::v1i64);
584 setOperationAction(ISD::XOR, MVT::v2i32, Promote);
585 AddPromotedToType (ISD::XOR, MVT::v2i32, MVT::v1i64);
586 setOperationAction(ISD::XOR, MVT::v1i64, Legal);
Bill Wendling1b7a81d2007-03-16 09:44:46 +0000587
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000588 setOperationAction(ISD::LOAD, MVT::v8i8, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000589 AddPromotedToType (ISD::LOAD, MVT::v8i8, MVT::v1i64);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000590 setOperationAction(ISD::LOAD, MVT::v4i16, Promote);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000591 AddPromotedToType (ISD::LOAD, MVT::v4i16, MVT::v1i64);
592 setOperationAction(ISD::LOAD, MVT::v2i32, Promote);
593 AddPromotedToType (ISD::LOAD, MVT::v2i32, MVT::v1i64);
Dale Johannesena68f9012008-06-24 22:01:44 +0000594 setOperationAction(ISD::LOAD, MVT::v2f32, Promote);
595 AddPromotedToType (ISD::LOAD, MVT::v2f32, MVT::v1i64);
Bill Wendlingeebc8a12007-03-26 07:53:08 +0000596 setOperationAction(ISD::LOAD, MVT::v1i64, Legal);
Bill Wendling2f88dcd2007-03-08 22:09:11 +0000597
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000598 setOperationAction(ISD::BUILD_VECTOR, MVT::v8i8, Custom);
599 setOperationAction(ISD::BUILD_VECTOR, MVT::v4i16, Custom);
600 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i32, Custom);
Dale Johannesena68f9012008-06-24 22:01:44 +0000601 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000602 setOperationAction(ISD::BUILD_VECTOR, MVT::v1i64, Custom);
Bill Wendlinga348c562007-03-22 18:42:45 +0000603
604 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v8i8, Custom);
605 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4i16, Custom);
606 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i32, Custom);
Bill Wendlingccc44ad2007-03-27 20:22:40 +0000607 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v1i64, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000608
Evan Cheng52672b82008-07-22 18:39:19 +0000609 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v2f32, Custom);
Bill Wendling826f36f2007-03-28 00:57:11 +0000610 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i8, Custom);
611 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v4i16, Custom);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +0000612 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v1i64, Custom);
Bill Wendling3180e202008-07-20 02:32:23 +0000613
614 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i16, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000615 }
616
Evan Chenga88973f2006-03-22 19:22:18 +0000617 if (Subtarget->hasSSE1()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000618 addRegisterClass(MVT::v4f32, X86::VR128RegisterClass);
619
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000620 setOperationAction(ISD::FADD, MVT::v4f32, Legal);
621 setOperationAction(ISD::FSUB, MVT::v4f32, Legal);
622 setOperationAction(ISD::FMUL, MVT::v4f32, Legal);
623 setOperationAction(ISD::FDIV, MVT::v4f32, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000624 setOperationAction(ISD::FSQRT, MVT::v4f32, Legal);
625 setOperationAction(ISD::FNEG, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000626 setOperationAction(ISD::LOAD, MVT::v4f32, Legal);
627 setOperationAction(ISD::BUILD_VECTOR, MVT::v4f32, Custom);
628 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v4f32, Custom);
Evan Cheng11e15b32006-04-03 20:53:28 +0000629 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000630 setOperationAction(ISD::SELECT, MVT::v4f32, Custom);
Nate Begeman30a0de92008-07-17 16:51:19 +0000631 setOperationAction(ISD::VSETCC, MVT::v4f32, Custom);
Evan Cheng470a6ad2006-02-22 02:26:30 +0000632 }
633
Evan Chenga88973f2006-03-22 19:22:18 +0000634 if (Subtarget->hasSSE2()) {
Evan Cheng470a6ad2006-02-22 02:26:30 +0000635 addRegisterClass(MVT::v2f64, X86::VR128RegisterClass);
636 addRegisterClass(MVT::v16i8, X86::VR128RegisterClass);
637 addRegisterClass(MVT::v8i16, X86::VR128RegisterClass);
638 addRegisterClass(MVT::v4i32, X86::VR128RegisterClass);
639 addRegisterClass(MVT::v2i64, X86::VR128RegisterClass);
640
Evan Chengf7c378e2006-04-10 07:23:14 +0000641 setOperationAction(ISD::ADD, MVT::v16i8, Legal);
642 setOperationAction(ISD::ADD, MVT::v8i16, Legal);
643 setOperationAction(ISD::ADD, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000644 setOperationAction(ISD::ADD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000645 setOperationAction(ISD::SUB, MVT::v16i8, Legal);
646 setOperationAction(ISD::SUB, MVT::v8i16, Legal);
647 setOperationAction(ISD::SUB, MVT::v4i32, Legal);
Evan Cheng37e88562007-03-12 22:58:52 +0000648 setOperationAction(ISD::SUB, MVT::v2i64, Legal);
Evan Chengf9989842006-04-13 05:10:25 +0000649 setOperationAction(ISD::MUL, MVT::v8i16, Legal);
Evan Cheng6bdb3f62006-10-27 18:49:08 +0000650 setOperationAction(ISD::FADD, MVT::v2f64, Legal);
651 setOperationAction(ISD::FSUB, MVT::v2f64, Legal);
652 setOperationAction(ISD::FMUL, MVT::v2f64, Legal);
653 setOperationAction(ISD::FDIV, MVT::v2f64, Legal);
Dan Gohman20382522007-07-10 00:05:58 +0000654 setOperationAction(ISD::FSQRT, MVT::v2f64, Legal);
655 setOperationAction(ISD::FNEG, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000656
Nate Begeman30a0de92008-07-17 16:51:19 +0000657 setOperationAction(ISD::VSETCC, MVT::v2f64, Custom);
658 setOperationAction(ISD::VSETCC, MVT::v16i8, Custom);
659 setOperationAction(ISD::VSETCC, MVT::v8i16, Custom);
660 setOperationAction(ISD::VSETCC, MVT::v4i32, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000661
Evan Chengf7c378e2006-04-10 07:23:14 +0000662 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v16i8, Custom);
663 setOperationAction(ISD::SCALAR_TO_VECTOR, MVT::v8i16, Custom);
Evan Chengb067a1e2006-03-31 19:22:53 +0000664 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000665 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Custom);
Evan Cheng5edb8d22006-04-17 22:04:06 +0000666 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
Evan Chengf7c378e2006-04-10 07:23:14 +0000667
Evan Cheng2c3ae372006-04-12 21:21:57 +0000668 // Custom lower build_vector, vector_shuffle, and extract_vector_elt.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000669 for (unsigned i = (unsigned)MVT::v16i8; i != (unsigned)MVT::v2i64; ++i) {
670 MVT VT = (MVT::SimpleValueType)i;
Nate Begeman844e0f92007-12-11 01:41:33 +0000671 // Do not attempt to custom lower non-power-of-2 vectors
Duncan Sands83ec4b62008-06-06 12:08:01 +0000672 if (!isPowerOf2_32(VT.getVectorNumElements()))
Nate Begeman844e0f92007-12-11 01:41:33 +0000673 continue;
Duncan Sands83ec4b62008-06-06 12:08:01 +0000674 setOperationAction(ISD::BUILD_VECTOR, VT, Custom);
675 setOperationAction(ISD::VECTOR_SHUFFLE, VT, Custom);
676 setOperationAction(ISD::EXTRACT_VECTOR_ELT, VT, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000677 }
678 setOperationAction(ISD::BUILD_VECTOR, MVT::v2f64, Custom);
679 setOperationAction(ISD::BUILD_VECTOR, MVT::v2i64, Custom);
680 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2f64, Custom);
681 setOperationAction(ISD::VECTOR_SHUFFLE, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000682 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000683 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2f64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000684 if (Subtarget->is64Bit()) {
685 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Custom);
Dale Johannesen25f1d082007-10-31 00:32:36 +0000686 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Custom);
Nate Begemancdd1eec2008-02-12 22:51:28 +0000687 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000688
Anton Korobeynikov12c49af2006-11-21 00:01:06 +0000689 // Promote v16i8, v8i16, v4i32 load, select, and, or, xor to v2i64.
Evan Cheng2c3ae372006-04-12 21:21:57 +0000690 for (unsigned VT = (unsigned)MVT::v16i8; VT != (unsigned)MVT::v2i64; VT++) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000691 setOperationAction(ISD::AND, (MVT::SimpleValueType)VT, Promote);
692 AddPromotedToType (ISD::AND, (MVT::SimpleValueType)VT, MVT::v2i64);
693 setOperationAction(ISD::OR, (MVT::SimpleValueType)VT, Promote);
694 AddPromotedToType (ISD::OR, (MVT::SimpleValueType)VT, MVT::v2i64);
695 setOperationAction(ISD::XOR, (MVT::SimpleValueType)VT, Promote);
696 AddPromotedToType (ISD::XOR, (MVT::SimpleValueType)VT, MVT::v2i64);
697 setOperationAction(ISD::LOAD, (MVT::SimpleValueType)VT, Promote);
698 AddPromotedToType (ISD::LOAD, (MVT::SimpleValueType)VT, MVT::v2i64);
699 setOperationAction(ISD::SELECT, (MVT::SimpleValueType)VT, Promote);
700 AddPromotedToType (ISD::SELECT, (MVT::SimpleValueType)VT, MVT::v2i64);
Evan Chengf7c378e2006-04-10 07:23:14 +0000701 }
Evan Cheng2c3ae372006-04-12 21:21:57 +0000702
Chris Lattnerddf89562008-01-17 19:59:44 +0000703 setTruncStoreAction(MVT::f64, MVT::f32, Expand);
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000704
Evan Cheng2c3ae372006-04-12 21:21:57 +0000705 // Custom lower v2i64 and v2f64 selects.
706 setOperationAction(ISD::LOAD, MVT::v2f64, Legal);
Evan Cheng91b740d2006-04-12 17:12:36 +0000707 setOperationAction(ISD::LOAD, MVT::v2i64, Legal);
Evan Chengf7c378e2006-04-10 07:23:14 +0000708 setOperationAction(ISD::SELECT, MVT::v2f64, Custom);
Evan Cheng2c3ae372006-04-12 21:21:57 +0000709 setOperationAction(ISD::SELECT, MVT::v2i64, Custom);
Nate Begemanc2616e42008-05-12 20:34:32 +0000710
Evan Cheng470a6ad2006-02-22 02:26:30 +0000711 }
Nate Begeman14d12ca2008-02-11 04:19:36 +0000712
713 if (Subtarget->hasSSE41()) {
714 // FIXME: Do we need to handle scalar-to-vector here?
715 setOperationAction(ISD::MUL, MVT::v4i32, Legal);
Dan Gohman0b924dc2008-05-23 17:49:40 +0000716 setOperationAction(ISD::MUL, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000717
718 // i8 and i16 vectors are custom , because the source register and source
719 // source memory operand types are not the same width. f32 vectors are
720 // custom since the immediate controlling the insert encodes additional
721 // information.
722 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v16i8, Custom);
723 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v8i16, Custom);
724 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4i32, Legal);
725 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v4f32, Custom);
726
727 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v16i8, Custom);
728 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v8i16, Custom);
729 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4i32, Legal);
Evan Cheng62a3f152008-03-24 21:52:23 +0000730 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v4f32, Custom);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000731
732 if (Subtarget->is64Bit()) {
Nate Begemancdd1eec2008-02-12 22:51:28 +0000733 setOperationAction(ISD::INSERT_VECTOR_ELT, MVT::v2i64, Legal);
734 setOperationAction(ISD::EXTRACT_VECTOR_ELT, MVT::v2i64, Legal);
Nate Begeman14d12ca2008-02-11 04:19:36 +0000735 }
736 }
Evan Cheng470a6ad2006-02-22 02:26:30 +0000737
Nate Begeman30a0de92008-07-17 16:51:19 +0000738 if (Subtarget->hasSSE42()) {
739 setOperationAction(ISD::VSETCC, MVT::v2i64, Custom);
740 }
741
Evan Cheng6be2c582006-04-05 23:38:46 +0000742 // We want to custom lower some of our intrinsics.
743 setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom);
744
Evan Cheng206ee9d2006-07-07 08:33:52 +0000745 // We have target-specific dag combine patterns for the following nodes:
746 setTargetDAGCombine(ISD::VECTOR_SHUFFLE);
Evan Chengd880b972008-05-09 21:53:03 +0000747 setTargetDAGCombine(ISD::BUILD_VECTOR);
Chris Lattner83e6c992006-10-04 06:57:07 +0000748 setTargetDAGCombine(ISD::SELECT);
Chris Lattner149a4e52008-02-22 02:09:43 +0000749 setTargetDAGCombine(ISD::STORE);
Evan Cheng206ee9d2006-07-07 08:33:52 +0000750
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000751 computeRegisterProperties();
752
Evan Cheng87ed7162006-02-14 08:25:08 +0000753 // FIXME: These should be based on subtarget info. Plus, the values should
754 // be smaller when we are in optimizing for size mode.
Dan Gohman87060f52008-06-30 21:00:56 +0000755 maxStoresPerMemset = 16; // For @llvm.memset -> sequence of stores
756 maxStoresPerMemcpy = 16; // For @llvm.memcpy -> sequence of stores
757 maxStoresPerMemmove = 3; // For @llvm.memmove -> sequence of stores
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000758 allowUnalignedMemoryAccesses = true; // x86 supports it!
Evan Chengfb8075d2008-02-28 00:43:03 +0000759 setPrefLoopAlignment(16);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +0000760}
761
Scott Michel5b8f82e2008-03-10 15:42:14 +0000762
Dan Gohman475871a2008-07-27 21:46:04 +0000763MVT X86TargetLowering::getSetCCResultType(const SDValue &) const {
Scott Michel5b8f82e2008-03-10 15:42:14 +0000764 return MVT::i8;
765}
766
767
Evan Cheng29286502008-01-23 23:17:41 +0000768/// getMaxByValAlign - Helper for getByValTypeAlignment to determine
769/// the desired ByVal argument alignment.
770static void getMaxByValAlign(const Type *Ty, unsigned &MaxAlign) {
771 if (MaxAlign == 16)
772 return;
773 if (const VectorType *VTy = dyn_cast<VectorType>(Ty)) {
774 if (VTy->getBitWidth() == 128)
775 MaxAlign = 16;
Evan Cheng29286502008-01-23 23:17:41 +0000776 } else if (const ArrayType *ATy = dyn_cast<ArrayType>(Ty)) {
777 unsigned EltAlign = 0;
778 getMaxByValAlign(ATy->getElementType(), EltAlign);
779 if (EltAlign > MaxAlign)
780 MaxAlign = EltAlign;
781 } else if (const StructType *STy = dyn_cast<StructType>(Ty)) {
782 for (unsigned i = 0, e = STy->getNumElements(); i != e; ++i) {
783 unsigned EltAlign = 0;
784 getMaxByValAlign(STy->getElementType(i), EltAlign);
785 if (EltAlign > MaxAlign)
786 MaxAlign = EltAlign;
787 if (MaxAlign == 16)
788 break;
789 }
790 }
791 return;
792}
793
794/// getByValTypeAlignment - Return the desired alignment for ByVal aggregate
795/// function arguments in the caller parameter area. For X86, aggregates
Dale Johannesen0c191872008-02-08 19:48:20 +0000796/// that contain SSE vectors are placed at 16-byte boundaries while the rest
797/// are at 4-byte boundaries.
Evan Cheng29286502008-01-23 23:17:41 +0000798unsigned X86TargetLowering::getByValTypeAlignment(const Type *Ty) const {
Evan Cheng1887c1c2008-08-21 21:00:15 +0000799 if (Subtarget->is64Bit()) {
800 // Max of 8 and alignment of type.
801 unsigned TyAlign = getTargetData()->getABITypeAlignment(Ty);
802 if (TyAlign > 8)
803 return TyAlign;
804 return 8;
805 }
806
Evan Cheng29286502008-01-23 23:17:41 +0000807 unsigned Align = 4;
Dale Johannesen0c191872008-02-08 19:48:20 +0000808 if (Subtarget->hasSSE1())
809 getMaxByValAlign(Ty, Align);
Evan Cheng29286502008-01-23 23:17:41 +0000810 return Align;
811}
Chris Lattner2b02a442007-02-25 08:29:00 +0000812
Evan Chengf0df0312008-05-15 08:39:06 +0000813/// getOptimalMemOpType - Returns the target specific optimal type for load
Evan Cheng0ef8de32008-05-15 22:13:02 +0000814/// and store operations as a result of memset, memcpy, and memmove
815/// lowering. It returns MVT::iAny if SelectionDAG should be responsible for
Evan Chengf0df0312008-05-15 08:39:06 +0000816/// determining it.
Duncan Sands83ec4b62008-06-06 12:08:01 +0000817MVT
Evan Chengf0df0312008-05-15 08:39:06 +0000818X86TargetLowering::getOptimalMemOpType(uint64_t Size, unsigned Align,
819 bool isSrcConst, bool isSrcStr) const {
820 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE2() && Size >= 16)
821 return MVT::v4i32;
822 if ((isSrcConst || isSrcStr) && Subtarget->hasSSE1() && Size >= 16)
823 return MVT::v4f32;
824 if (Subtarget->is64Bit() && Size >= 8)
825 return MVT::i64;
826 return MVT::i32;
827}
828
829
Evan Chengcc415862007-11-09 01:32:10 +0000830/// getPICJumpTableRelocaBase - Returns relocation base for the given PIC
831/// jumptable.
Dan Gohman475871a2008-07-27 21:46:04 +0000832SDValue X86TargetLowering::getPICJumpTableRelocBase(SDValue Table,
Evan Chengcc415862007-11-09 01:32:10 +0000833 SelectionDAG &DAG) const {
834 if (usesGlobalOffsetTable())
835 return DAG.getNode(ISD::GLOBAL_OFFSET_TABLE, getPointerTy());
836 if (!Subtarget->isPICStyleRIPRel())
837 return DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy());
838 return Table;
839}
840
Chris Lattner2b02a442007-02-25 08:29:00 +0000841//===----------------------------------------------------------------------===//
842// Return Value Calling Convention Implementation
843//===----------------------------------------------------------------------===//
844
Chris Lattner59ed56b2007-02-28 04:55:35 +0000845#include "X86GenCallingConv.inc"
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000846
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000847/// LowerRET - Lower an ISD::RET node.
Dan Gohman475871a2008-07-27 21:46:04 +0000848SDValue X86TargetLowering::LowerRET(SDValue Op, SelectionDAG &DAG) {
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000849 assert((Op.getNumOperands() & 1) == 1 && "ISD::RET should have odd # args");
850
Chris Lattner9774c912007-02-27 05:28:59 +0000851 SmallVector<CCValAssign, 16> RVLocs;
852 unsigned CC = DAG.getMachineFunction().getFunction()->getCallingConv();
Chris Lattner52387be2007-06-19 00:13:10 +0000853 bool isVarArg = DAG.getMachineFunction().getFunction()->isVarArg();
854 CCState CCInfo(CC, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000855 CCInfo.AnalyzeReturn(Op.Val, RetCC_X86);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000856
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000857 // If this is the first return lowered for this function, add the regs to the
858 // liveout set for the function.
Chris Lattner84bc5422007-12-31 04:13:23 +0000859 if (DAG.getMachineFunction().getRegInfo().liveout_empty()) {
Chris Lattner9774c912007-02-27 05:28:59 +0000860 for (unsigned i = 0; i != RVLocs.size(); ++i)
861 if (RVLocs[i].isRegLoc())
Chris Lattner84bc5422007-12-31 04:13:23 +0000862 DAG.getMachineFunction().getRegInfo().addLiveOut(RVLocs[i].getLocReg());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000863 }
Dan Gohman475871a2008-07-27 21:46:04 +0000864 SDValue Chain = Op.getOperand(0);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000865
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000866 // Handle tail call return.
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +0000867 Chain = GetPossiblePreceedingTailCall(Chain, X86ISD::TAILCALL);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000868 if (Chain.getOpcode() == X86ISD::TAILCALL) {
Dan Gohman475871a2008-07-27 21:46:04 +0000869 SDValue TailCall = Chain;
870 SDValue TargetAddress = TailCall.getOperand(1);
871 SDValue StackAdjustment = TailCall.getOperand(2);
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000872 assert(((TargetAddress.getOpcode() == ISD::Register &&
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000873 (cast<RegisterSDNode>(TargetAddress)->getReg() == X86::ECX ||
874 cast<RegisterSDNode>(TargetAddress)->getReg() == X86::R9)) ||
875 TargetAddress.getOpcode() == ISD::TargetExternalSymbol ||
876 TargetAddress.getOpcode() == ISD::TargetGlobalAddress) &&
877 "Expecting an global address, external symbol, or register");
Chris Lattnerb4a6eaa2008-01-16 05:52:18 +0000878 assert(StackAdjustment.getOpcode() == ISD::Constant &&
879 "Expecting a const value");
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000880
Dan Gohman475871a2008-07-27 21:46:04 +0000881 SmallVector<SDValue,8> Operands;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000882 Operands.push_back(Chain.getOperand(0));
883 Operands.push_back(TargetAddress);
884 Operands.push_back(StackAdjustment);
885 // Copy registers used by the call. Last operand is a flag so it is not
886 // copied.
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000887 for (unsigned i=3; i < TailCall.getNumOperands()-1; i++) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000888 Operands.push_back(Chain.getOperand(i));
889 }
Arnold Schwaighofer448175f2007-10-16 09:05:00 +0000890 return DAG.getNode(X86ISD::TC_RETURN, MVT::Other, &Operands[0],
891 Operands.size());
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000892 }
893
894 // Regular return.
Dan Gohman475871a2008-07-27 21:46:04 +0000895 SDValue Flag;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +0000896
Dan Gohman475871a2008-07-27 21:46:04 +0000897 SmallVector<SDValue, 6> RetOps;
Chris Lattner447ff682008-03-11 03:23:40 +0000898 RetOps.push_back(Chain); // Operand #0 = Chain (updated below)
899 // Operand #1 = Bytes To Pop
900 RetOps.push_back(DAG.getConstant(getBytesToPopOnReturn(), MVT::i16));
901
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000902 // Copy the result values into the output registers.
Chris Lattner8e6da152008-03-10 21:08:41 +0000903 for (unsigned i = 0; i != RVLocs.size(); ++i) {
904 CCValAssign &VA = RVLocs[i];
905 assert(VA.isRegLoc() && "Can only return in registers!");
Dan Gohman475871a2008-07-27 21:46:04 +0000906 SDValue ValToCopy = Op.getOperand(i*2+1);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000907
Chris Lattner447ff682008-03-11 03:23:40 +0000908 // Returns in ST0/ST1 are handled specially: these are pushed as operands to
909 // the RET instruction and handled by the FP Stackifier.
910 if (RVLocs[i].getLocReg() == X86::ST0 ||
911 RVLocs[i].getLocReg() == X86::ST1) {
912 // If this is a copy from an xmm register to ST(0), use an FPExtend to
913 // change the value to the FP stack register class.
914 if (isScalarFPTypeInSSEReg(RVLocs[i].getValVT()))
915 ValToCopy = DAG.getNode(ISD::FP_EXTEND, MVT::f80, ValToCopy);
916 RetOps.push_back(ValToCopy);
917 // Don't emit a copytoreg.
918 continue;
919 }
Dale Johannesena68f9012008-06-24 22:01:44 +0000920
Chris Lattner8e6da152008-03-10 21:08:41 +0000921 Chain = DAG.getCopyToReg(Chain, VA.getLocReg(), ValToCopy, Flag);
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000922 Flag = Chain.getValue(1);
923 }
Dan Gohman61a92132008-04-21 23:59:07 +0000924
925 // The x86-64 ABI for returning structs by value requires that we copy
926 // the sret argument into %rax for the return. We saved the argument into
927 // a virtual register in the entry block, so now we copy the value out
928 // and into %rax.
929 if (Subtarget->is64Bit() &&
930 DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
931 MachineFunction &MF = DAG.getMachineFunction();
932 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
933 unsigned Reg = FuncInfo->getSRetReturnReg();
934 if (!Reg) {
935 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
936 FuncInfo->setSRetReturnReg(Reg);
937 }
Dan Gohman475871a2008-07-27 21:46:04 +0000938 SDValue Val = DAG.getCopyFromReg(Chain, Reg, getPointerTy());
Dan Gohman61a92132008-04-21 23:59:07 +0000939
940 Chain = DAG.getCopyToReg(Chain, X86::RAX, Val, Flag);
941 Flag = Chain.getValue(1);
942 }
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000943
Chris Lattner447ff682008-03-11 03:23:40 +0000944 RetOps[0] = Chain; // Update chain.
945
946 // Add the flag if we have it.
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000947 if (Flag.Val)
Chris Lattner447ff682008-03-11 03:23:40 +0000948 RetOps.push_back(Flag);
949
950 return DAG.getNode(X86ISD::RET_FLAG, MVT::Other, &RetOps[0], RetOps.size());
Chris Lattner2a9bdd72007-02-25 09:12:39 +0000951}
952
953
Chris Lattner3085e152007-02-25 08:59:22 +0000954/// LowerCallResult - Lower the result values of an ISD::CALL into the
955/// appropriate copies out of appropriate physical registers. This assumes that
956/// Chain/InFlag are the input chain/flag to use, and that TheCall is the call
957/// being lowered. The returns a SDNode with the same number of values as the
958/// ISD::CALL.
959SDNode *X86TargetLowering::
Dan Gohman475871a2008-07-27 21:46:04 +0000960LowerCallResult(SDValue Chain, SDValue InFlag, SDNode *TheCall,
Chris Lattner3085e152007-02-25 08:59:22 +0000961 unsigned CallingConv, SelectionDAG &DAG) {
Chris Lattnere32bbf62007-02-28 07:09:55 +0000962
963 // Assign locations to each value returned by this call.
Chris Lattner9774c912007-02-27 05:28:59 +0000964 SmallVector<CCValAssign, 16> RVLocs;
Chris Lattner52387be2007-06-19 00:13:10 +0000965 bool isVarArg = cast<ConstantSDNode>(TheCall->getOperand(2))->getValue() != 0;
966 CCState CCInfo(CallingConv, isVarArg, getTargetMachine(), RVLocs);
Chris Lattnere32bbf62007-02-28 07:09:55 +0000967 CCInfo.AnalyzeCallResult(TheCall, RetCC_X86);
968
Dan Gohman475871a2008-07-27 21:46:04 +0000969 SmallVector<SDValue, 8> ResultVals;
Chris Lattner3085e152007-02-25 08:59:22 +0000970
971 // Copy all of the result registers out of their specified physreg.
Chris Lattner8e6da152008-03-10 21:08:41 +0000972 for (unsigned i = 0; i != RVLocs.size(); ++i) {
Duncan Sands83ec4b62008-06-06 12:08:01 +0000973 MVT CopyVT = RVLocs[i].getValVT();
Chris Lattner8e6da152008-03-10 21:08:41 +0000974
975 // If this is a call to a function that returns an fp value on the floating
976 // point stack, but where we prefer to use the value in xmm registers, copy
977 // 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 +0000978 if ((RVLocs[i].getLocReg() == X86::ST0 ||
979 RVLocs[i].getLocReg() == X86::ST1) &&
Chris Lattner8e6da152008-03-10 21:08:41 +0000980 isScalarFPTypeInSSEReg(RVLocs[i].getValVT())) {
981 CopyVT = MVT::f80;
Chris Lattner3085e152007-02-25 08:59:22 +0000982 }
Chris Lattner3085e152007-02-25 08:59:22 +0000983
Chris Lattner8e6da152008-03-10 21:08:41 +0000984 Chain = DAG.getCopyFromReg(Chain, RVLocs[i].getLocReg(),
985 CopyVT, InFlag).getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +0000986 SDValue Val = Chain.getValue(0);
Chris Lattner8e6da152008-03-10 21:08:41 +0000987 InFlag = Chain.getValue(2);
Chris Lattner112dedc2007-12-29 06:41:28 +0000988
Chris Lattner8e6da152008-03-10 21:08:41 +0000989 if (CopyVT != RVLocs[i].getValVT()) {
990 // Round the F80 the right size, which also moves to the appropriate xmm
991 // register.
992 Val = DAG.getNode(ISD::FP_ROUND, RVLocs[i].getValVT(), Val,
993 // This truncation won't change the value.
994 DAG.getIntPtrConstant(1));
995 }
Chris Lattnerd43d00c2008-01-24 08:07:48 +0000996
Chris Lattner8e6da152008-03-10 21:08:41 +0000997 ResultVals.push_back(Val);
Chris Lattner3085e152007-02-25 08:59:22 +0000998 }
Duncan Sands4bdcb612008-07-02 17:40:58 +0000999
Chris Lattner3085e152007-02-25 08:59:22 +00001000 // Merge everything together with a MERGE_VALUES node.
1001 ResultVals.push_back(Chain);
Duncan Sandsf9516202008-06-30 10:19:09 +00001002 return DAG.getMergeValues(TheCall->getVTList(), &ResultVals[0],
1003 ResultVals.size()).Val;
Chris Lattner2b02a442007-02-25 08:29:00 +00001004}
1005
1006
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001007//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001008// C & StdCall & Fast Calling Convention implementation
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001009//===----------------------------------------------------------------------===//
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001010// StdCall calling convention seems to be standard for many Windows' API
1011// routines and around. It differs from C calling convention just a little:
1012// callee should clean up the stack, not caller. Symbols should be also
1013// decorated in some fancy way :) It doesn't support any vector arguments.
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001014// For info on fast calling convention see Fast Calling Convention (tail call)
1015// implementation LowerX86_32FastCCCallTo.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001016
Evan Cheng85e38002006-04-27 05:35:28 +00001017/// AddLiveIn - This helper function adds the specified physical register to the
1018/// MachineFunction as a live in value. It also creates a corresponding virtual
1019/// register for it.
1020static unsigned AddLiveIn(MachineFunction &MF, unsigned PReg,
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001021 const TargetRegisterClass *RC) {
Evan Cheng85e38002006-04-27 05:35:28 +00001022 assert(RC->contains(PReg) && "Not the correct regclass!");
Chris Lattner84bc5422007-12-31 04:13:23 +00001023 unsigned VReg = MF.getRegInfo().createVirtualRegister(RC);
1024 MF.getRegInfo().addLiveIn(PReg, VReg);
Evan Cheng85e38002006-04-27 05:35:28 +00001025 return VReg;
1026}
1027
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001028/// CallIsStructReturn - Determines whether a CALL node uses struct return
1029/// semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001030static bool CallIsStructReturn(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001031 unsigned NumOps = (Op.getNumOperands() - 5) / 2;
1032 if (!NumOps)
1033 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001034
1035 return cast<ARG_FLAGSSDNode>(Op.getOperand(6))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001036}
1037
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001038/// ArgsAreStructReturn - Determines whether a FORMAL_ARGUMENTS node uses struct
1039/// return semantics.
Dan Gohman475871a2008-07-27 21:46:04 +00001040static bool ArgsAreStructReturn(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001041 unsigned NumArgs = Op.Val->getNumValues() - 1;
1042 if (!NumArgs)
1043 return false;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001044
1045 return cast<ARG_FLAGSSDNode>(Op.getOperand(3))->getArgFlags().isSRet();
Gordon Henriksen86737662008-01-05 16:56:59 +00001046}
1047
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001048/// IsCalleePop - Determines whether a CALL or FORMAL_ARGUMENTS node requires
1049/// the callee to pop its own arguments. Callee pop is necessary to support tail
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001050/// calls.
Dan Gohman475871a2008-07-27 21:46:04 +00001051bool X86TargetLowering::IsCalleePop(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001052 bool IsVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
1053 if (IsVarArg)
1054 return false;
1055
1056 switch (cast<ConstantSDNode>(Op.getOperand(1))->getValue()) {
1057 default:
1058 return false;
1059 case CallingConv::X86_StdCall:
1060 return !Subtarget->is64Bit();
1061 case CallingConv::X86_FastCall:
1062 return !Subtarget->is64Bit();
1063 case CallingConv::Fast:
1064 return PerformTailCallOpt;
1065 }
1066}
1067
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001068/// CCAssignFnForNode - Selects the correct CCAssignFn for a CALL or
1069/// FORMAL_ARGUMENTS node.
Dan Gohman475871a2008-07-27 21:46:04 +00001070CCAssignFn *X86TargetLowering::CCAssignFnForNode(SDValue Op) const {
Gordon Henriksen86737662008-01-05 16:56:59 +00001071 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1072
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001073 if (Subtarget->is64Bit()) {
Anton Korobeynikov1a979d92008-03-22 20:57:27 +00001074 if (Subtarget->isTargetWin64())
Anton Korobeynikov8f88cb02008-03-22 20:37:30 +00001075 return CC_X86_Win64_C;
1076 else {
1077 if (CC == CallingConv::Fast && PerformTailCallOpt)
1078 return CC_X86_64_TailCall;
1079 else
1080 return CC_X86_64_C;
1081 }
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00001082 }
1083
Gordon Henriksen86737662008-01-05 16:56:59 +00001084 if (CC == CallingConv::X86_FastCall)
1085 return CC_X86_32_FastCall;
1086 else if (CC == CallingConv::Fast && PerformTailCallOpt)
1087 return CC_X86_32_TailCall;
1088 else
1089 return CC_X86_32_C;
1090}
1091
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001092/// NameDecorationForFORMAL_ARGUMENTS - Selects the appropriate decoration to
1093/// apply to a MachineFunction containing a given FORMAL_ARGUMENTS node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001094NameDecorationStyle
Dan Gohman475871a2008-07-27 21:46:04 +00001095X86TargetLowering::NameDecorationForFORMAL_ARGUMENTS(SDValue Op) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001096 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
1097 if (CC == CallingConv::X86_FastCall)
1098 return FastCall;
1099 else if (CC == CallingConv::X86_StdCall)
1100 return StdCall;
1101 return None;
1102}
1103
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001104
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001105/// CallRequiresGOTInRegister - Check whether the call requires the GOT pointer
1106/// in a register before calling.
1107bool X86TargetLowering::CallRequiresGOTPtrInReg(bool Is64Bit, bool IsTailCall) {
1108 return !IsTailCall && !Is64Bit &&
1109 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1110 Subtarget->isPICStyleGOT();
1111}
1112
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001113/// CallRequiresFnAddressInReg - Check whether the call requires the function
1114/// address to be loaded in a register.
1115bool
1116X86TargetLowering::CallRequiresFnAddressInReg(bool Is64Bit, bool IsTailCall) {
1117 return !Is64Bit && IsTailCall &&
1118 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1119 Subtarget->isPICStyleGOT();
1120}
1121
Arnold Schwaighofer16a3e522008-02-26 17:50:59 +00001122/// CreateCopyOfByValArgument - Make a copy of an aggregate at address specified
1123/// by "Src" to address "Dst" with size and alignment information specified by
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001124/// the specific parameter attribute. The copy will be passed as a byval
1125/// function parameter.
Dan Gohman475871a2008-07-27 21:46:04 +00001126static SDValue
1127CreateCopyOfByValArgument(SDValue Src, SDValue Dst, SDValue Chain,
Duncan Sands276dcbd2008-03-21 09:14:45 +00001128 ISD::ArgFlagsTy Flags, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00001129 SDValue SizeNode = DAG.getConstant(Flags.getByValSize(), MVT::i32);
Dan Gohman707e0182008-04-12 04:36:06 +00001130 return DAG.getMemcpy(Chain, Dst, Src, SizeNode, Flags.getByValAlign(),
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001131 /*AlwaysInline=*/true, NULL, 0, NULL, 0);
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001132}
1133
Dan Gohman475871a2008-07-27 21:46:04 +00001134SDValue X86TargetLowering::LowerMemArgument(SDValue Op, SelectionDAG &DAG,
Rafael Espindola7effac52007-09-14 15:48:13 +00001135 const CCValAssign &VA,
1136 MachineFrameInfo *MFI,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001137 unsigned CC,
Dan Gohman475871a2008-07-27 21:46:04 +00001138 SDValue Root, unsigned i) {
Rafael Espindola7effac52007-09-14 15:48:13 +00001139 // Create the nodes corresponding to a load from this parameter slot.
Duncan Sands276dcbd2008-03-21 09:14:45 +00001140 ISD::ArgFlagsTy Flags =
1141 cast<ARG_FLAGSSDNode>(Op.getOperand(3 + i))->getArgFlags();
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001142 bool AlwaysUseMutable = (CC==CallingConv::Fast) && PerformTailCallOpt;
Duncan Sands276dcbd2008-03-21 09:14:45 +00001143 bool isImmutable = !AlwaysUseMutable && !Flags.isByVal();
Evan Chenge70bb592008-01-10 02:24:25 +00001144
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001145 // FIXME: For now, all byval parameter objects are marked mutable. This can be
1146 // changed with more analysis.
1147 // In case of tail call optimization mark all arguments mutable. Since they
1148 // could be overwritten by lowering of arguments in case of a tail call.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001149 int FI = MFI->CreateFixedObject(VA.getValVT().getSizeInBits()/8,
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001150 VA.getLocMemOffset(), isImmutable);
Dan Gohman475871a2008-07-27 21:46:04 +00001151 SDValue FIN = DAG.getFrameIndex(FI, getPointerTy());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001152 if (Flags.isByVal())
Rafael Espindola7effac52007-09-14 15:48:13 +00001153 return FIN;
Dan Gohman69de1932008-02-06 22:27:42 +00001154 return DAG.getLoad(VA.getValVT(), Root, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001155 PseudoSourceValue::getFixedStack(FI), 0);
Rafael Espindola7effac52007-09-14 15:48:13 +00001156}
1157
Dan Gohman475871a2008-07-27 21:46:04 +00001158SDValue
1159X86TargetLowering::LowerFORMAL_ARGUMENTS(SDValue Op, SelectionDAG &DAG) {
Evan Cheng1bc78042006-04-26 01:20:17 +00001160 MachineFunction &MF = DAG.getMachineFunction();
Gordon Henriksen86737662008-01-05 16:56:59 +00001161 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1162
1163 const Function* Fn = MF.getFunction();
1164 if (Fn->hasExternalLinkage() &&
1165 Subtarget->isTargetCygMing() &&
1166 Fn->getName() == "main")
1167 FuncInfo->setForceFramePointer(true);
1168
1169 // Decorate the function name.
1170 FuncInfo->setDecorationStyle(NameDecorationForFORMAL_ARGUMENTS(Op));
1171
Evan Cheng1bc78042006-04-26 01:20:17 +00001172 MachineFrameInfo *MFI = MF.getFrameInfo();
Dan Gohman475871a2008-07-27 21:46:04 +00001173 SDValue Root = Op.getOperand(0);
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001174 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001175 unsigned CC = MF.getFunction()->getCallingConv();
Gordon Henriksen86737662008-01-05 16:56:59 +00001176 bool Is64Bit = Subtarget->is64Bit();
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001177 bool IsWin64 = Subtarget->isTargetWin64();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001178
1179 assert(!(isVarArg && CC == CallingConv::Fast) &&
1180 "Var args not supported with calling convention fastcc");
1181
Chris Lattner638402b2007-02-28 07:00:42 +00001182 // Assign locations to all of the incoming arguments.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001183 SmallVector<CCValAssign, 16> ArgLocs;
Gordon Henriksenae636f82008-01-03 16:47:34 +00001184 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Gordon Henriksen86737662008-01-05 16:56:59 +00001185 CCInfo.AnalyzeFormalArguments(Op.Val, CCAssignFnForNode(Op));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001186
Dan Gohman475871a2008-07-27 21:46:04 +00001187 SmallVector<SDValue, 8> ArgValues;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001188 unsigned LastVal = ~0U;
1189 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1190 CCValAssign &VA = ArgLocs[i];
1191 // TODO: If an arg is passed in two places (e.g. reg and stack), skip later
1192 // places.
1193 assert(VA.getValNo() != LastVal &&
1194 "Don't support value assigned to multiple locs yet");
1195 LastVal = VA.getValNo();
1196
1197 if (VA.isRegLoc()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001198 MVT RegVT = VA.getLocVT();
Chris Lattnerf39f7712007-02-28 05:46:49 +00001199 TargetRegisterClass *RC;
1200 if (RegVT == MVT::i32)
1201 RC = X86::GR32RegisterClass;
Gordon Henriksen86737662008-01-05 16:56:59 +00001202 else if (Is64Bit && RegVT == MVT::i64)
1203 RC = X86::GR64RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001204 else if (RegVT == MVT::f32)
Gordon Henriksen86737662008-01-05 16:56:59 +00001205 RC = X86::FR32RegisterClass;
Dale Johannesene672af12008-02-05 20:46:33 +00001206 else if (RegVT == MVT::f64)
Gordon Henriksen86737662008-01-05 16:56:59 +00001207 RC = X86::FR64RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001208 else if (RegVT.isVector() && RegVT.getSizeInBits() == 128)
Evan Chengee472b12008-04-25 07:56:45 +00001209 RC = X86::VR128RegisterClass;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001210 else if (RegVT.isVector()) {
1211 assert(RegVT.getSizeInBits() == 64);
Evan Chengee472b12008-04-25 07:56:45 +00001212 if (!Is64Bit)
1213 RC = X86::VR64RegisterClass; // MMX values are passed in MMXs.
1214 else {
1215 // Darwin calling convention passes MMX values in either GPRs or
1216 // XMMs in x86-64. Other targets pass them in memory.
1217 if (RegVT != MVT::v1i64 && Subtarget->hasSSE2()) {
1218 RC = X86::VR128RegisterClass; // MMX values are passed in XMMs.
1219 RegVT = MVT::v2i64;
1220 } else {
1221 RC = X86::GR64RegisterClass; // v1i64 values are passed in GPRs.
1222 RegVT = MVT::i64;
1223 }
1224 }
1225 } else {
1226 assert(0 && "Unknown argument type!");
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001227 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001228
Chris Lattner82932a52007-03-02 05:12:29 +00001229 unsigned Reg = AddLiveIn(DAG.getMachineFunction(), VA.getLocReg(), RC);
Dan Gohman475871a2008-07-27 21:46:04 +00001230 SDValue ArgValue = DAG.getCopyFromReg(Root, Reg, RegVT);
Chris Lattnerf39f7712007-02-28 05:46:49 +00001231
1232 // If this is an 8 or 16-bit value, it is really passed promoted to 32
1233 // bits. Insert an assert[sz]ext to capture this, then truncate to the
1234 // right size.
1235 if (VA.getLocInfo() == CCValAssign::SExt)
1236 ArgValue = DAG.getNode(ISD::AssertSext, RegVT, ArgValue,
1237 DAG.getValueType(VA.getValVT()));
1238 else if (VA.getLocInfo() == CCValAssign::ZExt)
1239 ArgValue = DAG.getNode(ISD::AssertZext, RegVT, ArgValue,
1240 DAG.getValueType(VA.getValVT()));
1241
1242 if (VA.getLocInfo() != CCValAssign::Full)
1243 ArgValue = DAG.getNode(ISD::TRUNCATE, VA.getValVT(), ArgValue);
1244
Gordon Henriksen86737662008-01-05 16:56:59 +00001245 // Handle MMX values passed in GPRs.
Evan Cheng44c0fd12008-04-25 20:13:28 +00001246 if (Is64Bit && RegVT != VA.getLocVT()) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001247 if (RegVT.getSizeInBits() == 64 && RC == X86::GR64RegisterClass)
Evan Cheng44c0fd12008-04-25 20:13:28 +00001248 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1249 else if (RC == X86::VR128RegisterClass) {
1250 ArgValue = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i64, ArgValue,
1251 DAG.getConstant(0, MVT::i64));
1252 ArgValue = DAG.getNode(ISD::BIT_CONVERT, VA.getLocVT(), ArgValue);
1253 }
1254 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001255
Chris Lattnerf39f7712007-02-28 05:46:49 +00001256 ArgValues.push_back(ArgValue);
1257 } else {
1258 assert(VA.isMemLoc());
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001259 ArgValues.push_back(LowerMemArgument(Op, DAG, VA, MFI, CC, Root, i));
Evan Cheng1bc78042006-04-26 01:20:17 +00001260 }
Evan Cheng1bc78042006-04-26 01:20:17 +00001261 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001262
Dan Gohman61a92132008-04-21 23:59:07 +00001263 // The x86-64 ABI for returning structs by value requires that we copy
1264 // the sret argument into %rax for the return. Save the argument into
1265 // a virtual register so that we can access it from the return points.
1266 if (Is64Bit && DAG.getMachineFunction().getFunction()->hasStructRetAttr()) {
1267 MachineFunction &MF = DAG.getMachineFunction();
1268 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1269 unsigned Reg = FuncInfo->getSRetReturnReg();
1270 if (!Reg) {
1271 Reg = MF.getRegInfo().createVirtualRegister(getRegClassFor(MVT::i64));
1272 FuncInfo->setSRetReturnReg(Reg);
1273 }
Dan Gohman475871a2008-07-27 21:46:04 +00001274 SDValue Copy = DAG.getCopyToReg(DAG.getEntryNode(), Reg, ArgValues[0]);
Dan Gohman61a92132008-04-21 23:59:07 +00001275 Root = DAG.getNode(ISD::TokenFactor, MVT::Other, Copy, Root);
1276 }
1277
Chris Lattnerf39f7712007-02-28 05:46:49 +00001278 unsigned StackSize = CCInfo.getNextStackOffset();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001279 // align stack specially for tail calls
Gordon Henriksenae636f82008-01-03 16:47:34 +00001280 if (CC == CallingConv::Fast)
1281 StackSize = GetAlignedArgumentStackSize(StackSize, DAG);
Evan Cheng25caf632006-05-23 21:06:34 +00001282
Evan Cheng1bc78042006-04-26 01:20:17 +00001283 // If the function takes variable number of arguments, make a frame index for
1284 // the start of the first vararg value... for expansion of llvm.va_start.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001285 if (isVarArg) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001286 if (Is64Bit || CC != CallingConv::X86_FastCall) {
1287 VarArgsFrameIndex = MFI->CreateFixedObject(1, StackSize);
1288 }
1289 if (Is64Bit) {
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001290 unsigned TotalNumIntRegs = 0, TotalNumXMMRegs = 0;
1291
1292 // FIXME: We should really autogenerate these arrays
1293 static const unsigned GPR64ArgRegsWin64[] = {
1294 X86::RCX, X86::RDX, X86::R8, X86::R9
Gordon Henriksen86737662008-01-05 16:56:59 +00001295 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001296 static const unsigned XMMArgRegsWin64[] = {
1297 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3
1298 };
1299 static const unsigned GPR64ArgRegs64Bit[] = {
1300 X86::RDI, X86::RSI, X86::RDX, X86::RCX, X86::R8, X86::R9
1301 };
1302 static const unsigned XMMArgRegs64Bit[] = {
Gordon Henriksen86737662008-01-05 16:56:59 +00001303 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1304 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1305 };
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001306 const unsigned *GPR64ArgRegs, *XMMArgRegs;
1307
1308 if (IsWin64) {
1309 TotalNumIntRegs = 4; TotalNumXMMRegs = 4;
1310 GPR64ArgRegs = GPR64ArgRegsWin64;
1311 XMMArgRegs = XMMArgRegsWin64;
1312 } else {
1313 TotalNumIntRegs = 6; TotalNumXMMRegs = 8;
1314 GPR64ArgRegs = GPR64ArgRegs64Bit;
1315 XMMArgRegs = XMMArgRegs64Bit;
1316 }
1317 unsigned NumIntRegs = CCInfo.getFirstUnallocated(GPR64ArgRegs,
1318 TotalNumIntRegs);
1319 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs,
1320 TotalNumXMMRegs);
1321
Gordon Henriksen86737662008-01-05 16:56:59 +00001322 // For X86-64, if there are vararg parameters that are passed via
1323 // registers, then we must store them to their spots on the stack so they
1324 // may be loaded by deferencing the result of va_next.
1325 VarArgsGPOffset = NumIntRegs * 8;
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001326 VarArgsFPOffset = TotalNumIntRegs * 8 + NumXMMRegs * 16;
1327 RegSaveFrameIndex = MFI->CreateStackObject(TotalNumIntRegs * 8 +
1328 TotalNumXMMRegs * 16, 16);
1329
Gordon Henriksen86737662008-01-05 16:56:59 +00001330 // Store the integer parameter registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001331 SmallVector<SDValue, 8> MemOps;
1332 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
1333 SDValue FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001334 DAG.getIntPtrConstant(VarArgsGPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001335 for (; NumIntRegs != TotalNumIntRegs; ++NumIntRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001336 unsigned VReg = AddLiveIn(MF, GPR64ArgRegs[NumIntRegs],
1337 X86::GR64RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001338 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::i64);
1339 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001340 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001341 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001342 MemOps.push_back(Store);
1343 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001344 DAG.getIntPtrConstant(8));
Gordon Henriksen86737662008-01-05 16:56:59 +00001345 }
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001346
Gordon Henriksen86737662008-01-05 16:56:59 +00001347 // Now store the XMM (fp + vector) parameter registers.
1348 FIN = DAG.getNode(ISD::ADD, getPointerTy(), RSFIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001349 DAG.getIntPtrConstant(VarArgsFPOffset));
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001350 for (; NumXMMRegs != TotalNumXMMRegs; ++NumXMMRegs) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001351 unsigned VReg = AddLiveIn(MF, XMMArgRegs[NumXMMRegs],
1352 X86::VR128RegisterClass);
Dan Gohman475871a2008-07-27 21:46:04 +00001353 SDValue Val = DAG.getCopyFromReg(Root, VReg, MVT::v4f32);
1354 SDValue Store =
Dan Gohman69de1932008-02-06 22:27:42 +00001355 DAG.getStore(Val.getValue(1), Val, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001356 PseudoSourceValue::getFixedStack(RegSaveFrameIndex), 0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001357 MemOps.push_back(Store);
1358 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN,
Chris Lattner0bd48932008-01-17 07:00:52 +00001359 DAG.getIntPtrConstant(16));
Gordon Henriksen86737662008-01-05 16:56:59 +00001360 }
1361 if (!MemOps.empty())
1362 Root = DAG.getNode(ISD::TokenFactor, MVT::Other,
1363 &MemOps[0], MemOps.size());
1364 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001365 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001366
1367 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1368 // arguments and the arguments after the retaddr has been pushed are
1369 // aligned.
1370 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1371 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1372 (StackSize & 7) == 0)
1373 StackSize += 4;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001374
Gordon Henriksenae636f82008-01-03 16:47:34 +00001375 ArgValues.push_back(Root);
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001376
Gordon Henriksen86737662008-01-05 16:56:59 +00001377 // Some CCs need callee pop.
1378 if (IsCalleePop(Op)) {
1379 BytesToPopOnReturn = StackSize; // Callee pops everything.
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001380 BytesCallerReserves = 0;
1381 } else {
Anton Korobeynikov1d9bacc2007-03-06 08:12:33 +00001382 BytesToPopOnReturn = 0; // Callee pops nothing.
Chris Lattnerf39f7712007-02-28 05:46:49 +00001383 // If this is an sret function, the return should pop the hidden pointer.
Gordon Henriksen86737662008-01-05 16:56:59 +00001384 if (!Is64Bit && ArgsAreStructReturn(Op))
Chris Lattnerf39f7712007-02-28 05:46:49 +00001385 BytesToPopOnReturn = 4;
Chris Lattnerf39f7712007-02-28 05:46:49 +00001386 BytesCallerReserves = StackSize;
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001387 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001388
Gordon Henriksen86737662008-01-05 16:56:59 +00001389 if (!Is64Bit) {
1390 RegSaveFrameIndex = 0xAAAAAAA; // RegSaveFrameIndex is X86-64 only.
1391 if (CC == CallingConv::X86_FastCall)
1392 VarArgsFrameIndex = 0xAAAAAAA; // fastcc functions can't have varargs.
1393 }
Evan Cheng25caf632006-05-23 21:06:34 +00001394
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001395 FuncInfo->setBytesToPopOnReturn(BytesToPopOnReturn);
Evan Cheng1bc78042006-04-26 01:20:17 +00001396
Evan Cheng25caf632006-05-23 21:06:34 +00001397 // Return the new list of results.
Duncan Sandsf9516202008-06-30 10:19:09 +00001398 return DAG.getMergeValues(Op.Val->getVTList(), &ArgValues[0],
1399 ArgValues.size()).getValue(Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001400}
1401
Dan Gohman475871a2008-07-27 21:46:04 +00001402SDValue
1403X86TargetLowering::LowerMemOpCallTo(SDValue Op, SelectionDAG &DAG,
1404 const SDValue &StackPtr,
Evan Chengdffbd832008-01-10 00:09:10 +00001405 const CCValAssign &VA,
Dan Gohman475871a2008-07-27 21:46:04 +00001406 SDValue Chain,
1407 SDValue Arg) {
Dan Gohman4fdad172008-02-07 16:28:05 +00001408 unsigned LocMemOffset = VA.getLocMemOffset();
Dan Gohman475871a2008-07-27 21:46:04 +00001409 SDValue PtrOff = DAG.getIntPtrConstant(LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001410 PtrOff = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, PtrOff);
Duncan Sands276dcbd2008-03-21 09:14:45 +00001411 ISD::ArgFlagsTy Flags =
1412 cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->getArgFlags();
1413 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001414 return CreateCopyOfByValArgument(Arg, PtrOff, Chain, Flags, DAG);
Evan Chengdffbd832008-01-10 00:09:10 +00001415 }
Dan Gohman4fdad172008-02-07 16:28:05 +00001416 return DAG.getStore(Chain, Arg, PtrOff,
Dan Gohman3069b872008-02-07 18:41:25 +00001417 PseudoSourceValue::getStack(), LocMemOffset);
Evan Chengdffbd832008-01-10 00:09:10 +00001418}
1419
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001420/// EmitTailCallLoadRetAddr - Emit a load of return adress if tail call
1421/// optimization is performed and it is required.
Dan Gohman475871a2008-07-27 21:46:04 +00001422SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001423X86TargetLowering::EmitTailCallLoadRetAddr(SelectionDAG &DAG,
Dan Gohman475871a2008-07-27 21:46:04 +00001424 SDValue &OutRetAddr,
1425 SDValue Chain,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001426 bool IsTailCall,
1427 bool Is64Bit,
1428 int FPDiff) {
1429 if (!IsTailCall || FPDiff==0) return Chain;
1430
1431 // Adjust the Return address stack slot.
Duncan Sands83ec4b62008-06-06 12:08:01 +00001432 MVT VT = getPointerTy();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001433 OutRetAddr = getReturnAddressFrameIndex(DAG);
1434 // Load the "old" Return address.
1435 OutRetAddr = DAG.getLoad(VT, Chain,OutRetAddr, NULL, 0);
Dan Gohman475871a2008-07-27 21:46:04 +00001436 return SDValue(OutRetAddr.Val, 1);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001437}
1438
1439/// EmitTailCallStoreRetAddr - Emit a store of the return adress if tail call
1440/// optimization is performed and it is required (FPDiff!=0).
Dan Gohman475871a2008-07-27 21:46:04 +00001441static SDValue
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001442EmitTailCallStoreRetAddr(SelectionDAG & DAG, MachineFunction &MF,
Dan Gohman475871a2008-07-27 21:46:04 +00001443 SDValue Chain, SDValue RetAddrFrIdx,
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001444 bool Is64Bit, int FPDiff) {
1445 // Store the return address to the appropriate stack slot.
1446 if (!FPDiff) return Chain;
1447 // Calculate the new stack slot for the return address.
1448 int SlotSize = Is64Bit ? 8 : 4;
1449 int NewReturnAddrFI =
1450 MF.getFrameInfo()->CreateFixedObject(SlotSize, FPDiff-SlotSize);
Duncan Sands83ec4b62008-06-06 12:08:01 +00001451 MVT VT = Is64Bit ? MVT::i64 : MVT::i32;
Dan Gohman475871a2008-07-27 21:46:04 +00001452 SDValue NewRetAddrFrIdx = DAG.getFrameIndex(NewReturnAddrFI, VT);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001453 Chain = DAG.getStore(Chain, RetAddrFrIdx, NewRetAddrFrIdx,
Dan Gohmana54cf172008-07-11 22:44:52 +00001454 PseudoSourceValue::getFixedStack(NewReturnAddrFI), 0);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001455 return Chain;
1456}
1457
Dan Gohman475871a2008-07-27 21:46:04 +00001458SDValue X86TargetLowering::LowerCALL(SDValue Op, SelectionDAG &DAG) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001459 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00001460 SDValue Chain = Op.getOperand(0);
Gordon Henriksen86737662008-01-05 16:56:59 +00001461 unsigned CC = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001462 bool isVarArg = cast<ConstantSDNode>(Op.getOperand(2))->getValue() != 0;
Gordon Henriksen86737662008-01-05 16:56:59 +00001463 bool IsTailCall = cast<ConstantSDNode>(Op.getOperand(3))->getValue() != 0
1464 && CC == CallingConv::Fast && PerformTailCallOpt;
Dan Gohman475871a2008-07-27 21:46:04 +00001465 SDValue Callee = Op.getOperand(4);
Gordon Henriksen86737662008-01-05 16:56:59 +00001466 bool Is64Bit = Subtarget->is64Bit();
Evan Cheng0d9e9762008-01-29 19:34:22 +00001467 bool IsStructRet = CallIsStructReturn(Op);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001468
1469 assert(!(isVarArg && CC == CallingConv::Fast) &&
1470 "Var args not supported with calling convention fastcc");
1471
Chris Lattner638402b2007-02-28 07:00:42 +00001472 // Analyze operands of the call, assigning locations to each operand.
Chris Lattner423c5f42007-02-28 05:31:48 +00001473 SmallVector<CCValAssign, 16> ArgLocs;
Chris Lattner52387be2007-06-19 00:13:10 +00001474 CCState CCInfo(CC, isVarArg, getTargetMachine(), ArgLocs);
Chris Lattner920c37a2008-03-21 06:50:21 +00001475 CCInfo.AnalyzeCallOperands(Op.Val, CCAssignFnForNode(Op));
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001476
Chris Lattner423c5f42007-02-28 05:31:48 +00001477 // Get a count of how many bytes are to be pushed on the stack.
1478 unsigned NumBytes = CCInfo.getNextStackOffset();
Gordon Henriksenae636f82008-01-03 16:47:34 +00001479 if (CC == CallingConv::Fast)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001480 NumBytes = GetAlignedArgumentStackSize(NumBytes, DAG);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001481
Gordon Henriksen86737662008-01-05 16:56:59 +00001482 // Make sure the instruction takes 8n+4 bytes to make sure the start of the
1483 // arguments and the arguments after the retaddr has been pushed are aligned.
1484 if (!Is64Bit && CC == CallingConv::X86_FastCall &&
1485 !Subtarget->isTargetCygMing() && !Subtarget->isTargetWindows() &&
1486 (NumBytes & 7) == 0)
1487 NumBytes += 4;
1488
1489 int FPDiff = 0;
1490 if (IsTailCall) {
1491 // Lower arguments at fp - stackoffset + fpdiff.
1492 unsigned NumBytesCallerPushed =
1493 MF.getInfo<X86MachineFunctionInfo>()->getBytesToPopOnReturn();
1494 FPDiff = NumBytesCallerPushed - NumBytes;
1495
1496 // Set the delta of movement of the returnaddr stackslot.
1497 // But only set if delta is greater than previous delta.
1498 if (FPDiff < (MF.getInfo<X86MachineFunctionInfo>()->getTCReturnAddrDelta()))
1499 MF.getInfo<X86MachineFunctionInfo>()->setTCReturnAddrDelta(FPDiff);
1500 }
1501
Chris Lattner0bd48932008-01-17 07:00:52 +00001502 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(NumBytes));
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001503
Dan Gohman475871a2008-07-27 21:46:04 +00001504 SDValue RetAddrFrIdx;
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001505 // Load return adress for tail calls.
1506 Chain = EmitTailCallLoadRetAddr(DAG, RetAddrFrIdx, Chain, IsTailCall, Is64Bit,
1507 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001508
Dan Gohman475871a2008-07-27 21:46:04 +00001509 SmallVector<std::pair<unsigned, SDValue>, 8> RegsToPass;
1510 SmallVector<SDValue, 8> MemOpChains;
1511 SDValue StackPtr;
Chris Lattner423c5f42007-02-28 05:31:48 +00001512
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001513 // Walk the register/memloc assignments, inserting copies/loads. In the case
1514 // of tail call optimization arguments are handle later.
Chris Lattner423c5f42007-02-28 05:31:48 +00001515 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1516 CCValAssign &VA = ArgLocs[i];
Dan Gohman475871a2008-07-27 21:46:04 +00001517 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001518 bool isByVal = cast<ARG_FLAGSSDNode>(Op.getOperand(6+2*VA.getValNo()))->
1519 getArgFlags().isByVal();
1520
Chris Lattner423c5f42007-02-28 05:31:48 +00001521 // Promote the value if needed.
1522 switch (VA.getLocInfo()) {
1523 default: assert(0 && "Unknown loc info!");
1524 case CCValAssign::Full: break;
1525 case CCValAssign::SExt:
1526 Arg = DAG.getNode(ISD::SIGN_EXTEND, VA.getLocVT(), Arg);
1527 break;
1528 case CCValAssign::ZExt:
1529 Arg = DAG.getNode(ISD::ZERO_EXTEND, VA.getLocVT(), Arg);
1530 break;
1531 case CCValAssign::AExt:
1532 Arg = DAG.getNode(ISD::ANY_EXTEND, VA.getLocVT(), Arg);
1533 break;
Evan Cheng6b5783d2006-05-25 18:56:34 +00001534 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001535
1536 if (VA.isRegLoc()) {
Evan Cheng10e86422008-04-25 19:11:04 +00001537 if (Is64Bit) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00001538 MVT RegVT = VA.getLocVT();
1539 if (RegVT.isVector() && RegVT.getSizeInBits() == 64)
Evan Cheng10e86422008-04-25 19:11:04 +00001540 switch (VA.getLocReg()) {
1541 default:
1542 break;
1543 case X86::RDI: case X86::RSI: case X86::RDX: case X86::RCX:
1544 case X86::R8: {
1545 // Special case: passing MMX values in GPR registers.
1546 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1547 break;
1548 }
1549 case X86::XMM0: case X86::XMM1: case X86::XMM2: case X86::XMM3:
1550 case X86::XMM4: case X86::XMM5: case X86::XMM6: case X86::XMM7: {
1551 // Special case: passing MMX values in XMM registers.
1552 Arg = DAG.getNode(ISD::BIT_CONVERT, MVT::i64, Arg);
1553 Arg = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i64, Arg);
1554 Arg = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
1555 DAG.getNode(ISD::UNDEF, MVT::v2i64), Arg,
1556 getMOVLMask(2, DAG));
1557 break;
1558 }
1559 }
1560 }
Chris Lattner423c5f42007-02-28 05:31:48 +00001561 RegsToPass.push_back(std::make_pair(VA.getLocReg(), Arg));
1562 } else {
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001563 if (!IsTailCall || (IsTailCall && isByVal)) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001564 assert(VA.isMemLoc());
1565 if (StackPtr.Val == 0)
1566 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1567
1568 MemOpChains.push_back(LowerMemOpCallTo(Op, DAG, StackPtr, VA, Chain,
1569 Arg));
1570 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001571 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001572 }
Chris Lattnerc0bdf342007-02-28 05:39:26 +00001573
Evan Cheng32fe1032006-05-25 00:59:30 +00001574 if (!MemOpChains.empty())
Chris Lattnerbd564bf2006-08-08 02:23:42 +00001575 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
1576 &MemOpChains[0], MemOpChains.size());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001577
Evan Cheng347d5f72006-04-28 21:29:37 +00001578 // Build a sequence of copy-to-reg nodes chained together with token chain
1579 // and flag operands which copy the outgoing args into registers.
Dan Gohman475871a2008-07-27 21:46:04 +00001580 SDValue InFlag;
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001581 // Tail call byval lowering might overwrite argument registers so in case of
1582 // tail call optimization the copies to registers are lowered later.
1583 if (!IsTailCall)
1584 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1585 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1586 InFlag);
1587 InFlag = Chain.getValue(1);
1588 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001589
Evan Chengf4684712007-02-21 21:18:14 +00001590 // ELF / PIC requires GOT in the EBX register before function calls via PLT
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001591 // GOT pointer.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001592 if (CallRequiresGOTPtrInReg(Is64Bit, IsTailCall)) {
1593 Chain = DAG.getCopyToReg(Chain, X86::EBX,
1594 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
1595 InFlag);
1596 InFlag = Chain.getValue(1);
1597 }
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001598 // If we are tail calling and generating PIC/GOT style code load the address
1599 // of the callee into ecx. The value in ecx is used as target of the tail
1600 // jump. This is done to circumvent the ebx/callee-saved problem for tail
1601 // calls on PIC/GOT architectures. Normally we would just put the address of
1602 // GOT into ebx and then call target@PLT. But for tail callss ebx would be
1603 // restored (since ebx is callee saved) before jumping to the target@PLT.
Arnold Schwaighofer258bb1b2008-02-26 22:21:54 +00001604 if (CallRequiresFnAddressInReg(Is64Bit, IsTailCall)) {
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001605 // Note: The actual moving to ecx is done further down.
1606 GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee);
1607 if (G && !G->getGlobal()->hasHiddenVisibility() &&
1608 !G->getGlobal()->hasProtectedVisibility())
1609 Callee = LowerGlobalAddress(Callee, DAG);
1610 else if (isa<ExternalSymbolSDNode>(Callee))
1611 Callee = LowerExternalSymbol(Callee,DAG);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00001612 }
Gordon Henriksenae636f82008-01-03 16:47:34 +00001613
Gordon Henriksen86737662008-01-05 16:56:59 +00001614 if (Is64Bit && isVarArg) {
1615 // From AMD64 ABI document:
1616 // For calls that may call functions that use varargs or stdargs
1617 // (prototype-less calls or calls to functions containing ellipsis (...) in
1618 // the declaration) %al is used as hidden argument to specify the number
1619 // of SSE registers used. The contents of %al do not need to match exactly
1620 // the number of registers, but must be an ubound on the number of SSE
1621 // registers used and is in the range 0 - 8 inclusive.
Anton Korobeynikov998a5bc2008-04-27 23:15:03 +00001622
1623 // FIXME: Verify this on Win64
Gordon Henriksen86737662008-01-05 16:56:59 +00001624 // Count the number of XMM registers allocated.
1625 static const unsigned XMMArgRegs[] = {
1626 X86::XMM0, X86::XMM1, X86::XMM2, X86::XMM3,
1627 X86::XMM4, X86::XMM5, X86::XMM6, X86::XMM7
1628 };
1629 unsigned NumXMMRegs = CCInfo.getFirstUnallocated(XMMArgRegs, 8);
1630
1631 Chain = DAG.getCopyToReg(Chain, X86::AL,
1632 DAG.getConstant(NumXMMRegs, MVT::i8), InFlag);
1633 InFlag = Chain.getValue(1);
1634 }
1635
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001636
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001637 // For tail calls lower the arguments to the 'real' stack slot.
Gordon Henriksen86737662008-01-05 16:56:59 +00001638 if (IsTailCall) {
Dan Gohman475871a2008-07-27 21:46:04 +00001639 SmallVector<SDValue, 8> MemOpChains2;
1640 SDValue FIN;
Gordon Henriksen86737662008-01-05 16:56:59 +00001641 int FI = 0;
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001642 // Do not flag preceeding copytoreg stuff together with the following stuff.
Dan Gohman475871a2008-07-27 21:46:04 +00001643 InFlag = SDValue();
Gordon Henriksen86737662008-01-05 16:56:59 +00001644 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1645 CCValAssign &VA = ArgLocs[i];
1646 if (!VA.isRegLoc()) {
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001647 assert(VA.isMemLoc());
Dan Gohman475871a2008-07-27 21:46:04 +00001648 SDValue Arg = Op.getOperand(5+2*VA.getValNo());
1649 SDValue FlagsOp = Op.getOperand(6+2*VA.getValNo());
Duncan Sands276dcbd2008-03-21 09:14:45 +00001650 ISD::ArgFlagsTy Flags =
1651 cast<ARG_FLAGSSDNode>(FlagsOp)->getArgFlags();
Gordon Henriksen86737662008-01-05 16:56:59 +00001652 // Create frame index.
1653 int32_t Offset = VA.getLocMemOffset()+FPDiff;
Duncan Sands83ec4b62008-06-06 12:08:01 +00001654 uint32_t OpSize = (VA.getLocVT().getSizeInBits()+7)/8;
Gordon Henriksen86737662008-01-05 16:56:59 +00001655 FI = MF.getFrameInfo()->CreateFixedObject(OpSize, Offset);
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001656 FIN = DAG.getFrameIndex(FI, getPointerTy());
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001657
Duncan Sands276dcbd2008-03-21 09:14:45 +00001658 if (Flags.isByVal()) {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001659 // Copy relative to framepointer.
Dan Gohman475871a2008-07-27 21:46:04 +00001660 SDValue Source = DAG.getIntPtrConstant(VA.getLocMemOffset());
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001661 if (StackPtr.Val == 0)
1662 StackPtr = DAG.getCopyFromReg(Chain, X86StackPtr, getPointerTy());
1663 Source = DAG.getNode(ISD::ADD, getPointerTy(), StackPtr, Source);
1664
1665 MemOpChains2.push_back(CreateCopyOfByValArgument(Source, FIN, Chain,
Evan Cheng8e5712b2008-01-12 01:08:07 +00001666 Flags, DAG));
Gordon Henriksen86737662008-01-05 16:56:59 +00001667 } else {
Evan Cheng8e5712b2008-01-12 01:08:07 +00001668 // Store relative to framepointer.
Dan Gohman69de1932008-02-06 22:27:42 +00001669 MemOpChains2.push_back(
Arnold Schwaighofer865c6812008-02-26 09:19:59 +00001670 DAG.getStore(Chain, Arg, FIN,
Dan Gohmana54cf172008-07-11 22:44:52 +00001671 PseudoSourceValue::getFixedStack(FI), 0));
Arnold Schwaighoferc8ab8cd2008-01-11 16:49:42 +00001672 }
Gordon Henriksen86737662008-01-05 16:56:59 +00001673 }
1674 }
1675
1676 if (!MemOpChains2.empty())
1677 Chain = DAG.getNode(ISD::TokenFactor, MVT::Other,
Arnold Schwaighofer719eb022008-01-11 14:34:56 +00001678 &MemOpChains2[0], MemOpChains2.size());
Gordon Henriksen86737662008-01-05 16:56:59 +00001679
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001680 // Copy arguments to their registers.
1681 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i) {
1682 Chain = DAG.getCopyToReg(Chain, RegsToPass[i].first, RegsToPass[i].second,
1683 InFlag);
1684 InFlag = Chain.getValue(1);
1685 }
Dan Gohman475871a2008-07-27 21:46:04 +00001686 InFlag =SDValue();
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001687
Gordon Henriksen86737662008-01-05 16:56:59 +00001688 // Store the return address to the appropriate stack slot.
Arnold Schwaighofer4b5324a2008-04-12 18:11:06 +00001689 Chain = EmitTailCallStoreRetAddr(DAG, MF, Chain, RetAddrFrIdx, Is64Bit,
1690 FPDiff);
Gordon Henriksen86737662008-01-05 16:56:59 +00001691 }
1692
Evan Cheng32fe1032006-05-25 00:59:30 +00001693 // If the callee is a GlobalAddress node (quite common, every direct call is)
1694 // turn it into a TargetGlobalAddress node so that legalize doesn't hack it.
Anton Korobeynikova5986852006-11-20 10:46:14 +00001695 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee)) {
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00001696 // We should use extra load for direct calls to dllimported functions in
1697 // non-JIT mode.
Evan Cheng817a6a92008-07-16 01:34:02 +00001698 if (!Subtarget->GVRequiresExtraLoad(G->getGlobal(),
1699 getTargetMachine(), true))
Anton Korobeynikova5986852006-11-20 10:46:14 +00001700 Callee = DAG.getTargetGlobalAddress(G->getGlobal(), getPointerTy());
Gordon Henriksenae636f82008-01-03 16:47:34 +00001701 } else if (ExternalSymbolSDNode *S = dyn_cast<ExternalSymbolSDNode>(Callee)) {
Evan Cheng817a6a92008-07-16 01:34:02 +00001702 Callee = DAG.getTargetExternalSymbol(S->getSymbol(), getPointerTy());
Gordon Henriksen86737662008-01-05 16:56:59 +00001703 } else if (IsTailCall) {
Gordon Henriksen86737662008-01-05 16:56:59 +00001704 unsigned Opc = Is64Bit ? X86::R9 : X86::ECX;
1705
1706 Chain = DAG.getCopyToReg(Chain,
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001707 DAG.getRegister(Opc, getPointerTy()),
Gordon Henriksen86737662008-01-05 16:56:59 +00001708 Callee,InFlag);
1709 Callee = DAG.getRegister(Opc, getPointerTy());
1710 // Add register as live out.
1711 DAG.getMachineFunction().getRegInfo().addLiveOut(Opc);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001712 }
1713
Chris Lattnerd96d0722007-02-25 06:40:16 +00001714 // Returns a chain & a flag for retval copy to use.
1715 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00001716 SmallVector<SDValue, 8> Ops;
Gordon Henriksen86737662008-01-05 16:56:59 +00001717
1718 if (IsTailCall) {
1719 Ops.push_back(Chain);
Chris Lattner0bd48932008-01-17 07:00:52 +00001720 Ops.push_back(DAG.getIntPtrConstant(NumBytes));
1721 Ops.push_back(DAG.getIntPtrConstant(0));
Gordon Henriksen86737662008-01-05 16:56:59 +00001722 if (InFlag.Val)
1723 Ops.push_back(InFlag);
1724 Chain = DAG.getNode(ISD::CALLSEQ_END, NodeTys, &Ops[0], Ops.size());
1725 InFlag = Chain.getValue(1);
1726
1727 // Returns a chain & a flag for retval copy to use.
1728 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
1729 Ops.clear();
1730 }
1731
Nate Begeman4c5dcf52006-02-17 00:03:04 +00001732 Ops.push_back(Chain);
1733 Ops.push_back(Callee);
Evan Chengb69d1132006-06-14 18:17:40 +00001734
Gordon Henriksen86737662008-01-05 16:56:59 +00001735 if (IsTailCall)
1736 Ops.push_back(DAG.getConstant(FPDiff, MVT::i32));
Evan Chengf4684712007-02-21 21:18:14 +00001737
Gordon Henriksen86737662008-01-05 16:56:59 +00001738 // Add argument registers to the end of the list so that they are known live
1739 // into the call.
Evan Cheng9b449442008-01-07 23:08:23 +00001740 for (unsigned i = 0, e = RegsToPass.size(); i != e; ++i)
1741 Ops.push_back(DAG.getRegister(RegsToPass[i].first,
1742 RegsToPass[i].second.getValueType()));
Gordon Henriksen86737662008-01-05 16:56:59 +00001743
Evan Cheng586ccac2008-03-18 23:36:35 +00001744 // Add an implicit use GOT pointer in EBX.
1745 if (!IsTailCall && !Is64Bit &&
1746 getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
1747 Subtarget->isPICStyleGOT())
1748 Ops.push_back(DAG.getRegister(X86::EBX, getPointerTy()));
1749
1750 // Add an implicit use of AL for x86 vararg functions.
1751 if (Is64Bit && isVarArg)
1752 Ops.push_back(DAG.getRegister(X86::AL, MVT::i8));
1753
Evan Cheng347d5f72006-04-28 21:29:37 +00001754 if (InFlag.Val)
1755 Ops.push_back(InFlag);
Gordon Henriksenae636f82008-01-03 16:47:34 +00001756
Gordon Henriksen86737662008-01-05 16:56:59 +00001757 if (IsTailCall) {
1758 assert(InFlag.Val &&
1759 "Flag must be set. Depend on flag being set in LowerRET");
1760 Chain = DAG.getNode(X86ISD::TAILCALL,
1761 Op.Val->getVTList(), &Ops[0], Ops.size());
1762
Dan Gohman475871a2008-07-27 21:46:04 +00001763 return SDValue(Chain.Val, Op.ResNo);
Gordon Henriksen86737662008-01-05 16:56:59 +00001764 }
1765
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001766 Chain = DAG.getNode(X86ISD::CALL, NodeTys, &Ops[0], Ops.size());
Evan Cheng347d5f72006-04-28 21:29:37 +00001767 InFlag = Chain.getValue(1);
Evan Chengd90eb7f2006-01-05 00:27:02 +00001768
Chris Lattner2d297092006-05-23 18:50:38 +00001769 // Create the CALLSEQ_END node.
Gordon Henriksen86737662008-01-05 16:56:59 +00001770 unsigned NumBytesForCalleeToPush;
1771 if (IsCalleePop(Op))
1772 NumBytesForCalleeToPush = NumBytes; // Callee pops everything
Evan Cheng0d9e9762008-01-29 19:34:22 +00001773 else if (!Is64Bit && IsStructRet)
Anton Korobeynikovb10308e2007-01-28 13:31:35 +00001774 // If this is is a call to a struct-return function, the callee
1775 // pops the hidden struct pointer, so we have to push it back.
1776 // This is common for Darwin/X86, Linux & Mingw32 targets.
Gordon Henriksenae636f82008-01-03 16:47:34 +00001777 NumBytesForCalleeToPush = 4;
Gordon Henriksen86737662008-01-05 16:56:59 +00001778 else
Gordon Henriksenae636f82008-01-03 16:47:34 +00001779 NumBytesForCalleeToPush = 0; // Callee pops nothing.
Gordon Henriksen86737662008-01-05 16:56:59 +00001780
Gordon Henriksenae636f82008-01-03 16:47:34 +00001781 // Returns a flag for retval copy to use.
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001782 Chain = DAG.getCALLSEQ_END(Chain,
Chris Lattner0bd48932008-01-17 07:00:52 +00001783 DAG.getIntPtrConstant(NumBytes),
1784 DAG.getIntPtrConstant(NumBytesForCalleeToPush),
Bill Wendling0f8d9c02007-11-13 00:44:25 +00001785 InFlag);
Chris Lattner3085e152007-02-25 08:59:22 +00001786 InFlag = Chain.getValue(1);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001787
Chris Lattner3085e152007-02-25 08:59:22 +00001788 // Handle result values, copying them out of physregs into vregs that we
1789 // return.
Dan Gohman475871a2008-07-27 21:46:04 +00001790 return SDValue(LowerCallResult(Chain, InFlag, Op.Val, CC, DAG), Op.ResNo);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001791}
1792
Evan Cheng25ab6902006-09-08 06:48:29 +00001793
1794//===----------------------------------------------------------------------===//
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001795// Fast Calling Convention (tail call) implementation
1796//===----------------------------------------------------------------------===//
1797
1798// Like std call, callee cleans arguments, convention except that ECX is
1799// reserved for storing the tail called function address. Only 2 registers are
1800// free for argument passing (inreg). Tail call optimization is performed
1801// provided:
1802// * tailcallopt is enabled
1803// * caller/callee are fastcc
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001804// On X86_64 architecture with GOT-style position independent code only local
1805// (within module) calls are supported at the moment.
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001806// To keep the stack aligned according to platform abi the function
1807// GetAlignedArgumentStackSize ensures that argument delta is always multiples
1808// of stack alignment. (Dynamic linkers need this - darwin's dyld for example)
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001809// If a tail called function callee has more arguments than the caller the
1810// caller needs to make sure that there is room to move the RETADDR to. This is
Arnold Schwaighofer48abc5c2007-10-12 21:30:57 +00001811// achieved by reserving an area the size of the argument delta right after the
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001812// original REtADDR, but before the saved framepointer or the spilled registers
1813// e.g. caller(arg1, arg2) calls callee(arg1, arg2,arg3,arg4)
1814// stack layout:
1815// arg1
1816// arg2
1817// RETADDR
1818// [ new RETADDR
1819// move area ]
1820// (possible EBP)
1821// ESI
1822// EDI
1823// local1 ..
1824
1825/// GetAlignedArgumentStackSize - Make the stack size align e.g 16n + 12 aligned
1826/// for a 16 byte align requirement.
1827unsigned X86TargetLowering::GetAlignedArgumentStackSize(unsigned StackSize,
1828 SelectionDAG& DAG) {
1829 if (PerformTailCallOpt) {
1830 MachineFunction &MF = DAG.getMachineFunction();
1831 const TargetMachine &TM = MF.getTarget();
1832 const TargetFrameInfo &TFI = *TM.getFrameInfo();
1833 unsigned StackAlignment = TFI.getStackAlignment();
1834 uint64_t AlignMask = StackAlignment - 1;
1835 int64_t Offset = StackSize;
1836 unsigned SlotSize = Subtarget->is64Bit() ? 8 : 4;
1837 if ( (Offset & AlignMask) <= (StackAlignment - SlotSize) ) {
1838 // Number smaller than 12 so just add the difference.
1839 Offset += ((StackAlignment - SlotSize) - (Offset & AlignMask));
1840 } else {
1841 // Mask out lower bits, add stackalignment once plus the 12 bytes.
1842 Offset = ((~AlignMask) & Offset) + StackAlignment +
1843 (StackAlignment-SlotSize);
1844 }
1845 StackSize = Offset;
1846 }
1847 return StackSize;
1848}
1849
1850/// IsEligibleForTailCallElimination - Check to see whether the next instruction
Evan Cheng9df7dc52007-11-02 01:26:22 +00001851/// following the call is a return. A function is eligible if caller/callee
1852/// calling conventions match, currently only fastcc supports tail calls, and
1853/// the function CALL is immediatly followed by a RET.
Dan Gohman475871a2008-07-27 21:46:04 +00001854bool X86TargetLowering::IsEligibleForTailCallOptimization(SDValue Call,
1855 SDValue Ret,
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001856 SelectionDAG& DAG) const {
Evan Cheng9df7dc52007-11-02 01:26:22 +00001857 if (!PerformTailCallOpt)
1858 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001859
Arnold Schwaighofer30e62c02008-04-30 09:16:33 +00001860 if (CheckTailCallReturnConstraints(Call, Ret)) {
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001861 MachineFunction &MF = DAG.getMachineFunction();
1862 unsigned CallerCC = MF.getFunction()->getCallingConv();
1863 unsigned CalleeCC = cast<ConstantSDNode>(Call.getOperand(1))->getValue();
1864 if (CalleeCC == CallingConv::Fast && CallerCC == CalleeCC) {
Dan Gohman475871a2008-07-27 21:46:04 +00001865 SDValue Callee = Call.getOperand(4);
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001866 // On x86/32Bit PIC/GOT tail calls are supported.
Evan Cheng9df7dc52007-11-02 01:26:22 +00001867 if (getTargetMachine().getRelocationModel() != Reloc::PIC_ ||
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001868 !Subtarget->isPICStyleGOT()|| !Subtarget->is64Bit())
Evan Cheng9df7dc52007-11-02 01:26:22 +00001869 return true;
1870
Arnold Schwaighofera2a4b472008-02-26 10:21:54 +00001871 // Can only do local tail calls (in same module, hidden or protected) on
1872 // x86_64 PIC/GOT at the moment.
Gordon Henriksen86737662008-01-05 16:56:59 +00001873 if (GlobalAddressSDNode *G = dyn_cast<GlobalAddressSDNode>(Callee))
1874 return G->getGlobal()->hasHiddenVisibility()
1875 || G->getGlobal()->hasProtectedVisibility();
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001876 }
1877 }
Evan Cheng9df7dc52007-11-02 01:26:22 +00001878
1879 return false;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00001880}
1881
Dan Gohmanbb466332008-08-20 21:05:57 +00001882FastISel *X86TargetLowering::createFastISel(MachineFunction &mf) {
1883 return X86::createFastISel(mf);
Dan Gohmand9f3c482008-08-19 21:32:53 +00001884}
1885
1886
Chris Lattnerfcf1a3d2007-02-28 06:10:12 +00001887//===----------------------------------------------------------------------===//
1888// Other Lowering Hooks
1889//===----------------------------------------------------------------------===//
1890
1891
Dan Gohman475871a2008-07-27 21:46:04 +00001892SDValue X86TargetLowering::getReturnAddressFrameIndex(SelectionDAG &DAG) {
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001893 MachineFunction &MF = DAG.getMachineFunction();
1894 X86MachineFunctionInfo *FuncInfo = MF.getInfo<X86MachineFunctionInfo>();
1895 int ReturnAddrIndex = FuncInfo->getRAIndex();
1896
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001897 if (ReturnAddrIndex == 0) {
1898 // Set up a frame object for the return address.
Evan Cheng25ab6902006-09-08 06:48:29 +00001899 if (Subtarget->is64Bit())
1900 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(8, -8);
1901 else
1902 ReturnAddrIndex = MF.getFrameInfo()->CreateFixedObject(4, -4);
Anton Korobeynikova2780e12007-08-15 17:12:32 +00001903
1904 FuncInfo->setRAIndex(ReturnAddrIndex);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001905 }
1906
Evan Cheng25ab6902006-09-08 06:48:29 +00001907 return DAG.getFrameIndex(ReturnAddrIndex, getPointerTy());
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00001908}
1909
1910
1911
Evan Cheng6dfa9992006-01-30 23:41:35 +00001912/// translateX86CC - do a one to one translation of a ISD::CondCode to the X86
1913/// specific condition code. It returns a false if it cannot do a direct
Chris Lattnerf9570512006-09-13 03:22:10 +00001914/// translation. X86CC is the translated CondCode. LHS/RHS are modified as
1915/// needed.
Evan Cheng6be2c582006-04-05 23:38:46 +00001916static bool translateX86CC(ISD::CondCode SetCCOpcode, bool isFP,
Dan Gohman475871a2008-07-27 21:46:04 +00001917 unsigned &X86CC, SDValue &LHS, SDValue &RHS,
Chris Lattnerf9570512006-09-13 03:22:10 +00001918 SelectionDAG &DAG) {
Chris Lattner7fbe9722006-10-20 17:42:20 +00001919 X86CC = X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001920 if (!isFP) {
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001921 if (ConstantSDNode *RHSC = dyn_cast<ConstantSDNode>(RHS)) {
1922 if (SetCCOpcode == ISD::SETGT && RHSC->isAllOnesValue()) {
1923 // X > -1 -> X == 0, jump !sign.
1924 RHS = DAG.getConstant(0, RHS.getValueType());
Chris Lattner7fbe9722006-10-20 17:42:20 +00001925 X86CC = X86::COND_NS;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001926 return true;
1927 } else if (SetCCOpcode == ISD::SETLT && RHSC->isNullValue()) {
1928 // X < 0 -> X == 0, jump on sign.
Chris Lattner7fbe9722006-10-20 17:42:20 +00001929 X86CC = X86::COND_S;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001930 return true;
Dan Gohman5f6913c2007-09-17 14:49:27 +00001931 } else if (SetCCOpcode == ISD::SETLT && RHSC->getValue() == 1) {
1932 // X < 1 -> X <= 0
1933 RHS = DAG.getConstant(0, RHS.getValueType());
1934 X86CC = X86::COND_LE;
1935 return true;
Chris Lattnerbfd68a72006-09-13 17:04:54 +00001936 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001937 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00001938
Evan Chengd9558e02006-01-06 00:43:03 +00001939 switch (SetCCOpcode) {
1940 default: break;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001941 case ISD::SETEQ: X86CC = X86::COND_E; break;
1942 case ISD::SETGT: X86CC = X86::COND_G; break;
1943 case ISD::SETGE: X86CC = X86::COND_GE; break;
1944 case ISD::SETLT: X86CC = X86::COND_L; break;
1945 case ISD::SETLE: X86CC = X86::COND_LE; break;
1946 case ISD::SETNE: X86CC = X86::COND_NE; break;
1947 case ISD::SETULT: X86CC = X86::COND_B; break;
1948 case ISD::SETUGT: X86CC = X86::COND_A; break;
1949 case ISD::SETULE: X86CC = X86::COND_BE; break;
1950 case ISD::SETUGE: X86CC = X86::COND_AE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001951 }
1952 } else {
1953 // On a floating point condition, the flags are set as follows:
1954 // ZF PF CF op
1955 // 0 | 0 | 0 | X > Y
1956 // 0 | 0 | 1 | X < Y
1957 // 1 | 0 | 0 | X == Y
1958 // 1 | 1 | 1 | unordered
Chris Lattnerf9570512006-09-13 03:22:10 +00001959 bool Flip = false;
Evan Chengd9558e02006-01-06 00:43:03 +00001960 switch (SetCCOpcode) {
1961 default: break;
1962 case ISD::SETUEQ:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001963 case ISD::SETEQ: X86CC = X86::COND_E; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001964 case ISD::SETOLT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001965 case ISD::SETOGT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001966 case ISD::SETGT: X86CC = X86::COND_A; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001967 case ISD::SETOLE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001968 case ISD::SETOGE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001969 case ISD::SETGE: X86CC = X86::COND_AE; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001970 case ISD::SETUGT: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001971 case ISD::SETULT:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001972 case ISD::SETLT: X86CC = X86::COND_B; break;
Evan Cheng5001ea12006-04-17 07:24:10 +00001973 case ISD::SETUGE: Flip = true; // Fallthrough
Evan Chengd9558e02006-01-06 00:43:03 +00001974 case ISD::SETULE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001975 case ISD::SETLE: X86CC = X86::COND_BE; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001976 case ISD::SETONE:
Chris Lattner7fbe9722006-10-20 17:42:20 +00001977 case ISD::SETNE: X86CC = X86::COND_NE; break;
1978 case ISD::SETUO: X86CC = X86::COND_P; break;
1979 case ISD::SETO: X86CC = X86::COND_NP; break;
Evan Chengd9558e02006-01-06 00:43:03 +00001980 }
Chris Lattnerf9570512006-09-13 03:22:10 +00001981 if (Flip)
1982 std::swap(LHS, RHS);
Evan Chengd9558e02006-01-06 00:43:03 +00001983 }
Evan Cheng6dfa9992006-01-30 23:41:35 +00001984
Chris Lattner7fbe9722006-10-20 17:42:20 +00001985 return X86CC != X86::COND_INVALID;
Evan Chengd9558e02006-01-06 00:43:03 +00001986}
1987
Evan Cheng4a460802006-01-11 00:33:36 +00001988/// hasFPCMov - is there a floating point cmov for the specific X86 condition
1989/// code. Current x86 isa includes the following FP cmov instructions:
Evan Chengaaca22c2006-01-10 20:26:56 +00001990/// fcmovb, fcomvbe, fcomve, fcmovu, fcmovae, fcmova, fcmovne, fcmovnu.
Evan Cheng4a460802006-01-11 00:33:36 +00001991static bool hasFPCMov(unsigned X86CC) {
Evan Chengaaca22c2006-01-10 20:26:56 +00001992 switch (X86CC) {
1993 default:
1994 return false;
Chris Lattner7fbe9722006-10-20 17:42:20 +00001995 case X86::COND_B:
1996 case X86::COND_BE:
1997 case X86::COND_E:
1998 case X86::COND_P:
1999 case X86::COND_A:
2000 case X86::COND_AE:
2001 case X86::COND_NE:
2002 case X86::COND_NP:
Evan Chengaaca22c2006-01-10 20:26:56 +00002003 return true;
2004 }
2005}
2006
Evan Cheng5ced1d82006-04-06 23:23:56 +00002007/// isUndefOrInRange - Op is either an undef node or a ConstantSDNode. Return
Evan Chengc5cdff22006-04-07 21:53:05 +00002008/// true if Op is undef or if its value falls within the specified range (L, H].
Dan Gohman475871a2008-07-27 21:46:04 +00002009static bool isUndefOrInRange(SDValue Op, unsigned Low, unsigned Hi) {
Evan Cheng5ced1d82006-04-06 23:23:56 +00002010 if (Op.getOpcode() == ISD::UNDEF)
2011 return true;
2012
2013 unsigned Val = cast<ConstantSDNode>(Op)->getValue();
Evan Chengc5cdff22006-04-07 21:53:05 +00002014 return (Val >= Low && Val < Hi);
2015}
2016
2017/// isUndefOrEqual - Op is either an undef node or a ConstantSDNode. Return
2018/// true if Op is undef or if its value equal to the specified value.
Dan Gohman475871a2008-07-27 21:46:04 +00002019static bool isUndefOrEqual(SDValue Op, unsigned Val) {
Evan Chengc5cdff22006-04-07 21:53:05 +00002020 if (Op.getOpcode() == ISD::UNDEF)
2021 return true;
2022 return cast<ConstantSDNode>(Op)->getValue() == Val;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002023}
2024
Evan Cheng0188ecb2006-03-22 18:59:22 +00002025/// isPSHUFDMask - Return true if the specified VECTOR_SHUFFLE operand
2026/// specifies a shuffle of elements that is suitable for input to PSHUFD.
2027bool X86::isPSHUFDMask(SDNode *N) {
2028 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2029
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002030 if (N->getNumOperands() != 2 && N->getNumOperands() != 4)
Evan Cheng0188ecb2006-03-22 18:59:22 +00002031 return false;
2032
2033 // Check if the value doesn't reference the second vector.
Evan Cheng506d3df2006-03-29 23:07:14 +00002034 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002035 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002036 if (Arg.getOpcode() == ISD::UNDEF) continue;
2037 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Dan Gohman7f55fcb2007-08-02 21:17:01 +00002038 if (cast<ConstantSDNode>(Arg)->getValue() >= e)
Evan Cheng506d3df2006-03-29 23:07:14 +00002039 return false;
2040 }
2041
2042 return true;
2043}
2044
2045/// isPSHUFHWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002046/// specifies a shuffle of elements that is suitable for input to PSHUFHW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002047bool X86::isPSHUFHWMask(SDNode *N) {
2048 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2049
2050 if (N->getNumOperands() != 8)
2051 return false;
2052
2053 // Lower quadword copied in order.
2054 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002055 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002056 if (Arg.getOpcode() == ISD::UNDEF) continue;
2057 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2058 if (cast<ConstantSDNode>(Arg)->getValue() != i)
Evan Cheng506d3df2006-03-29 23:07:14 +00002059 return false;
2060 }
2061
2062 // Upper quadword shuffled.
2063 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002064 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002065 if (Arg.getOpcode() == ISD::UNDEF) continue;
2066 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2067 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002068 if (Val < 4 || Val > 7)
2069 return false;
2070 }
2071
2072 return true;
2073}
2074
2075/// isPSHUFLWMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Chengc21a0532006-04-05 01:47:37 +00002076/// specifies a shuffle of elements that is suitable for input to PSHUFLW.
Evan Cheng506d3df2006-03-29 23:07:14 +00002077bool X86::isPSHUFLWMask(SDNode *N) {
2078 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2079
2080 if (N->getNumOperands() != 8)
2081 return false;
2082
2083 // Upper quadword copied in order.
Evan Chengc5cdff22006-04-07 21:53:05 +00002084 for (unsigned i = 4; i != 8; ++i)
2085 if (!isUndefOrEqual(N->getOperand(i), i))
Evan Cheng506d3df2006-03-29 23:07:14 +00002086 return false;
Evan Cheng506d3df2006-03-29 23:07:14 +00002087
2088 // Lower quadword shuffled.
Evan Chengc5cdff22006-04-07 21:53:05 +00002089 for (unsigned i = 0; i != 4; ++i)
2090 if (!isUndefOrInRange(N->getOperand(i), 0, 4))
Evan Cheng506d3df2006-03-29 23:07:14 +00002091 return false;
Evan Cheng0188ecb2006-03-22 18:59:22 +00002092
2093 return true;
2094}
2095
Evan Cheng14aed5e2006-03-24 01:18:28 +00002096/// isSHUFPMask - Return true if the specified VECTOR_SHUFFLE operand
2097/// specifies a shuffle of elements that is suitable for input to SHUFP*.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002098static bool isSHUFPMask(SDOperandPtr Elems, unsigned NumElems) {
Evan Cheng39623da2006-04-20 08:58:49 +00002099 if (NumElems != 2 && NumElems != 4) return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002100
Evan Cheng39623da2006-04-20 08:58:49 +00002101 unsigned Half = NumElems / 2;
2102 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002103 if (!isUndefOrInRange(Elems[i], 0, NumElems))
Evan Cheng39623da2006-04-20 08:58:49 +00002104 return false;
2105 for (unsigned i = Half; i < NumElems; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002106 if (!isUndefOrInRange(Elems[i], NumElems, NumElems*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002107 return false;
Evan Cheng14aed5e2006-03-24 01:18:28 +00002108
2109 return true;
2110}
2111
Evan Cheng39623da2006-04-20 08:58:49 +00002112bool X86::isSHUFPMask(SDNode *N) {
2113 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002114 return ::isSHUFPMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002115}
2116
Evan Cheng213d2cf2007-05-17 18:45:50 +00002117/// isCommutedSHUFP - Returns true if the shuffle mask is exactly
Evan Cheng39623da2006-04-20 08:58:49 +00002118/// the reverse of what x86 shuffles want. x86 shuffles requires the lower
2119/// half elements to come from vector 1 (which would equal the dest.) and
2120/// the upper half to come from vector 2.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002121static bool isCommutedSHUFP(SDOperandPtr Ops, unsigned NumOps) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002122 if (NumOps != 2 && NumOps != 4) return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002123
Chris Lattner5a88b832007-02-25 07:10:00 +00002124 unsigned Half = NumOps / 2;
Evan Cheng39623da2006-04-20 08:58:49 +00002125 for (unsigned i = 0; i < Half; ++i)
Chris Lattner5a88b832007-02-25 07:10:00 +00002126 if (!isUndefOrInRange(Ops[i], NumOps, NumOps*2))
Evan Cheng39623da2006-04-20 08:58:49 +00002127 return false;
Chris Lattner5a88b832007-02-25 07:10:00 +00002128 for (unsigned i = Half; i < NumOps; ++i)
2129 if (!isUndefOrInRange(Ops[i], 0, NumOps))
Evan Cheng39623da2006-04-20 08:58:49 +00002130 return false;
2131 return true;
2132}
2133
2134static bool isCommutedSHUFP(SDNode *N) {
2135 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002136 return isCommutedSHUFP(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002137}
2138
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002139/// isMOVHLPSMask - Return true if the specified VECTOR_SHUFFLE operand
2140/// specifies a shuffle of elements that is suitable for input to MOVHLPS.
2141bool X86::isMOVHLPSMask(SDNode *N) {
2142 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2143
Evan Cheng2064a2b2006-03-28 06:50:32 +00002144 if (N->getNumOperands() != 4)
Evan Cheng2c0dbd02006-03-24 02:58:06 +00002145 return false;
2146
Evan Cheng2064a2b2006-03-28 06:50:32 +00002147 // Expect bit0 == 6, bit1 == 7, bit2 == 2, bit3 == 3
Evan Chengc5cdff22006-04-07 21:53:05 +00002148 return isUndefOrEqual(N->getOperand(0), 6) &&
2149 isUndefOrEqual(N->getOperand(1), 7) &&
2150 isUndefOrEqual(N->getOperand(2), 2) &&
2151 isUndefOrEqual(N->getOperand(3), 3);
Evan Cheng2064a2b2006-03-28 06:50:32 +00002152}
2153
Evan Cheng6e56e2c2006-11-07 22:14:24 +00002154/// isMOVHLPS_v_undef_Mask - Special case of isMOVHLPSMask for canonical form
2155/// of vector_shuffle v, v, <2, 3, 2, 3>, i.e. vector_shuffle v, undef,
2156/// <2, 3, 2, 3>
2157bool X86::isMOVHLPS_v_undef_Mask(SDNode *N) {
2158 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2159
2160 if (N->getNumOperands() != 4)
2161 return false;
2162
2163 // Expect bit0 == 2, bit1 == 3, bit2 == 2, bit3 == 3
2164 return isUndefOrEqual(N->getOperand(0), 2) &&
2165 isUndefOrEqual(N->getOperand(1), 3) &&
2166 isUndefOrEqual(N->getOperand(2), 2) &&
2167 isUndefOrEqual(N->getOperand(3), 3);
2168}
2169
Evan Cheng5ced1d82006-04-06 23:23:56 +00002170/// isMOVLPMask - Return true if the specified VECTOR_SHUFFLE operand
2171/// specifies a shuffle of elements that is suitable for input to MOVLP{S|D}.
2172bool X86::isMOVLPMask(SDNode *N) {
2173 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2174
2175 unsigned NumElems = N->getNumOperands();
2176 if (NumElems != 2 && NumElems != 4)
2177 return false;
2178
Evan Chengc5cdff22006-04-07 21:53:05 +00002179 for (unsigned i = 0; i < NumElems/2; ++i)
2180 if (!isUndefOrEqual(N->getOperand(i), i + NumElems))
2181 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002182
Evan Chengc5cdff22006-04-07 21:53:05 +00002183 for (unsigned i = NumElems/2; i < NumElems; ++i)
2184 if (!isUndefOrEqual(N->getOperand(i), i))
2185 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002186
2187 return true;
2188}
2189
2190/// isMOVHPMask - Return true if the specified VECTOR_SHUFFLE operand
Evan Cheng533a0aa2006-04-19 20:35:22 +00002191/// specifies a shuffle of elements that is suitable for input to MOVHP{S|D}
2192/// and MOVLHPS.
Evan Cheng5ced1d82006-04-06 23:23:56 +00002193bool X86::isMOVHPMask(SDNode *N) {
2194 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2195
2196 unsigned NumElems = N->getNumOperands();
2197 if (NumElems != 2 && NumElems != 4)
2198 return false;
2199
Evan Chengc5cdff22006-04-07 21:53:05 +00002200 for (unsigned i = 0; i < NumElems/2; ++i)
2201 if (!isUndefOrEqual(N->getOperand(i), i))
2202 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002203
2204 for (unsigned i = 0; i < NumElems/2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002205 SDValue Arg = N->getOperand(i + NumElems/2);
Evan Chengc5cdff22006-04-07 21:53:05 +00002206 if (!isUndefOrEqual(Arg, i + NumElems))
2207 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002208 }
2209
2210 return true;
2211}
2212
Evan Cheng0038e592006-03-28 00:39:58 +00002213/// isUNPCKLMask - Return true if the specified VECTOR_SHUFFLE operand
2214/// specifies a shuffle of elements that is suitable for input to UNPCKL.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002215bool static isUNPCKLMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002216 bool V2IsSplat = false) {
2217 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng0038e592006-03-28 00:39:58 +00002218 return false;
2219
Chris Lattner5a88b832007-02-25 07:10:00 +00002220 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002221 SDValue BitI = Elts[i];
2222 SDValue BitI1 = Elts[i+1];
Evan Chengc5cdff22006-04-07 21:53:05 +00002223 if (!isUndefOrEqual(BitI, j))
2224 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002225 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002226 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002227 return false;
2228 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002229 if (!isUndefOrEqual(BitI1, j + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002230 return false;
2231 }
Evan Cheng0038e592006-03-28 00:39:58 +00002232 }
2233
2234 return true;
2235}
2236
Evan Cheng39623da2006-04-20 08:58:49 +00002237bool X86::isUNPCKLMask(SDNode *N, bool V2IsSplat) {
2238 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002239 return ::isUNPCKLMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002240}
2241
Evan Cheng4fcb9222006-03-28 02:43:26 +00002242/// isUNPCKHMask - Return true if the specified VECTOR_SHUFFLE operand
2243/// specifies a shuffle of elements that is suitable for input to UNPCKH.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002244bool static isUNPCKHMask(SDOperandPtr Elts, unsigned NumElts,
Chris Lattner5a88b832007-02-25 07:10:00 +00002245 bool V2IsSplat = false) {
2246 if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
Evan Cheng4fcb9222006-03-28 02:43:26 +00002247 return false;
2248
Chris Lattner5a88b832007-02-25 07:10:00 +00002249 for (unsigned i = 0, j = 0; i != NumElts; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002250 SDValue BitI = Elts[i];
2251 SDValue BitI1 = Elts[i+1];
Chris Lattner5a88b832007-02-25 07:10:00 +00002252 if (!isUndefOrEqual(BitI, j + NumElts/2))
Evan Chengc5cdff22006-04-07 21:53:05 +00002253 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002254 if (V2IsSplat) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002255 if (isUndefOrEqual(BitI1, NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002256 return false;
2257 } else {
Chris Lattner5a88b832007-02-25 07:10:00 +00002258 if (!isUndefOrEqual(BitI1, j + NumElts/2 + NumElts))
Evan Cheng39623da2006-04-20 08:58:49 +00002259 return false;
2260 }
Evan Cheng4fcb9222006-03-28 02:43:26 +00002261 }
2262
2263 return true;
2264}
2265
Evan Cheng39623da2006-04-20 08:58:49 +00002266bool X86::isUNPCKHMask(SDNode *N, bool V2IsSplat) {
2267 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002268 return ::isUNPCKHMask(N->op_begin(), N->getNumOperands(), V2IsSplat);
Evan Cheng39623da2006-04-20 08:58:49 +00002269}
2270
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002271/// isUNPCKL_v_undef_Mask - Special case of isUNPCKLMask for canonical form
2272/// of vector_shuffle v, v, <0, 4, 1, 5>, i.e. vector_shuffle v, undef,
2273/// <0, 0, 1, 1>
2274bool X86::isUNPCKL_v_undef_Mask(SDNode *N) {
2275 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2276
2277 unsigned NumElems = N->getNumOperands();
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002278 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002279 return false;
2280
2281 for (unsigned i = 0, j = 0; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002282 SDValue BitI = N->getOperand(i);
2283 SDValue BitI1 = N->getOperand(i+1);
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002284
Evan Chengc5cdff22006-04-07 21:53:05 +00002285 if (!isUndefOrEqual(BitI, j))
2286 return false;
2287 if (!isUndefOrEqual(BitI1, j))
2288 return false;
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002289 }
2290
2291 return true;
2292}
2293
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002294/// isUNPCKH_v_undef_Mask - Special case of isUNPCKHMask for canonical form
2295/// of vector_shuffle v, v, <2, 6, 3, 7>, i.e. vector_shuffle v, undef,
2296/// <2, 2, 3, 3>
2297bool X86::isUNPCKH_v_undef_Mask(SDNode *N) {
2298 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2299
2300 unsigned NumElems = N->getNumOperands();
2301 if (NumElems != 2 && NumElems != 4 && NumElems != 8 && NumElems != 16)
2302 return false;
2303
2304 for (unsigned i = 0, j = NumElems / 2; i != NumElems; i += 2, ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00002305 SDValue BitI = N->getOperand(i);
2306 SDValue BitI1 = N->getOperand(i + 1);
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00002307
2308 if (!isUndefOrEqual(BitI, j))
2309 return false;
2310 if (!isUndefOrEqual(BitI1, j))
2311 return false;
2312 }
2313
2314 return true;
2315}
2316
Evan Cheng017dcc62006-04-21 01:05:10 +00002317/// isMOVLMask - Return true if the specified VECTOR_SHUFFLE operand
2318/// specifies a shuffle of elements that is suitable for input to MOVSS,
2319/// MOVSD, and MOVD, i.e. setting the lowest element.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002320static bool isMOVLMask(SDOperandPtr Elts, unsigned NumElts) {
Evan Cheng10762102007-12-06 22:14:22 +00002321 if (NumElts != 2 && NumElts != 4)
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002322 return false;
2323
Chris Lattner5a88b832007-02-25 07:10:00 +00002324 if (!isUndefOrEqual(Elts[0], NumElts))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002325 return false;
2326
Chris Lattner5a88b832007-02-25 07:10:00 +00002327 for (unsigned i = 1; i < NumElts; ++i) {
2328 if (!isUndefOrEqual(Elts[i], i))
Evan Chengd6d1cbd2006-04-11 00:19:04 +00002329 return false;
2330 }
2331
2332 return true;
2333}
Evan Cheng1d5a8cc2006-04-05 07:20:06 +00002334
Evan Cheng017dcc62006-04-21 01:05:10 +00002335bool X86::isMOVLMask(SDNode *N) {
Evan Cheng39623da2006-04-20 08:58:49 +00002336 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002337 return ::isMOVLMask(N->op_begin(), N->getNumOperands());
Evan Cheng39623da2006-04-20 08:58:49 +00002338}
2339
Evan Cheng017dcc62006-04-21 01:05:10 +00002340/// isCommutedMOVL - Returns true if the shuffle mask is except the reverse
2341/// of what x86 movss want. X86 movs requires the lowest element to be lowest
Evan Cheng39623da2006-04-20 08:58:49 +00002342/// element of vector 2 and the other elements to come from vector 1 in order.
Roman Levenstein9cac5252008-04-16 16:15:27 +00002343static bool isCommutedMOVL(SDOperandPtr Ops, unsigned NumOps,
Chris Lattner5a88b832007-02-25 07:10:00 +00002344 bool V2IsSplat = false,
Evan Cheng8cf723d2006-09-08 01:50:06 +00002345 bool V2IsUndef = false) {
Chris Lattner5a88b832007-02-25 07:10:00 +00002346 if (NumOps != 2 && NumOps != 4 && NumOps != 8 && NumOps != 16)
Evan Cheng39623da2006-04-20 08:58:49 +00002347 return false;
2348
2349 if (!isUndefOrEqual(Ops[0], 0))
2350 return false;
2351
Chris Lattner5a88b832007-02-25 07:10:00 +00002352 for (unsigned i = 1; i < NumOps; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002353 SDValue Arg = Ops[i];
Chris Lattner5a88b832007-02-25 07:10:00 +00002354 if (!(isUndefOrEqual(Arg, i+NumOps) ||
2355 (V2IsUndef && isUndefOrInRange(Arg, NumOps, NumOps*2)) ||
2356 (V2IsSplat && isUndefOrEqual(Arg, NumOps))))
Evan Cheng8cf723d2006-09-08 01:50:06 +00002357 return false;
Evan Cheng39623da2006-04-20 08:58:49 +00002358 }
2359
2360 return true;
2361}
2362
Evan Cheng8cf723d2006-09-08 01:50:06 +00002363static bool isCommutedMOVL(SDNode *N, bool V2IsSplat = false,
2364 bool V2IsUndef = false) {
Evan Cheng39623da2006-04-20 08:58:49 +00002365 assert(N->getOpcode() == ISD::BUILD_VECTOR);
Chris Lattner5a88b832007-02-25 07:10:00 +00002366 return isCommutedMOVL(N->op_begin(), N->getNumOperands(),
2367 V2IsSplat, V2IsUndef);
Evan Cheng39623da2006-04-20 08:58:49 +00002368}
2369
Evan Chengd9539472006-04-14 21:59:03 +00002370/// isMOVSHDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2371/// specifies a shuffle of elements that is suitable for input to MOVSHDUP.
2372bool X86::isMOVSHDUPMask(SDNode *N) {
2373 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2374
2375 if (N->getNumOperands() != 4)
2376 return false;
2377
2378 // Expect 1, 1, 3, 3
2379 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002380 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002381 if (Arg.getOpcode() == ISD::UNDEF) continue;
2382 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2383 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2384 if (Val != 1) return false;
2385 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002386
2387 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002388 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002389 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002390 if (Arg.getOpcode() == ISD::UNDEF) continue;
2391 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2392 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2393 if (Val != 3) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002394 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002395 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002396
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002397 // Don't use movshdup if it can be done with a shufps.
2398 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002399}
2400
2401/// isMOVSLDUPMask - Return true if the specified VECTOR_SHUFFLE operand
2402/// specifies a shuffle of elements that is suitable for input to MOVSLDUP.
2403bool X86::isMOVSLDUPMask(SDNode *N) {
2404 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2405
2406 if (N->getNumOperands() != 4)
2407 return false;
2408
2409 // Expect 0, 0, 2, 2
2410 for (unsigned i = 0; i < 2; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002411 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002412 if (Arg.getOpcode() == ISD::UNDEF) continue;
2413 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2414 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2415 if (Val != 0) return false;
2416 }
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002417
2418 bool HasHi = false;
Evan Chengd9539472006-04-14 21:59:03 +00002419 for (unsigned i = 2; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002420 SDValue Arg = N->getOperand(i);
Evan Chengd9539472006-04-14 21:59:03 +00002421 if (Arg.getOpcode() == ISD::UNDEF) continue;
2422 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2423 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2424 if (Val != 2) return false;
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002425 HasHi = true;
Evan Chengd9539472006-04-14 21:59:03 +00002426 }
Evan Cheng39fc1452006-04-15 03:13:24 +00002427
Evan Cheng57ebe9f2006-04-15 05:37:34 +00002428 // Don't use movshdup if it can be done with a shufps.
2429 return HasHi;
Evan Chengd9539472006-04-14 21:59:03 +00002430}
2431
Evan Cheng49892af2007-06-19 00:02:56 +00002432/// isIdentityMask - Return true if the specified VECTOR_SHUFFLE operand
2433/// specifies a identity operation on the LHS or RHS.
2434static bool isIdentityMask(SDNode *N, bool RHS = false) {
2435 unsigned NumElems = N->getNumOperands();
2436 for (unsigned i = 0; i < NumElems; ++i)
2437 if (!isUndefOrEqual(N->getOperand(i), i + (RHS ? NumElems : 0)))
2438 return false;
2439 return true;
2440}
2441
Evan Chengb9df0ca2006-03-22 02:53:00 +00002442/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2443/// a splat of a single element.
Evan Chengc575ca22006-04-17 20:43:08 +00002444static bool isSplatMask(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002445 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2446
Evan Chengb9df0ca2006-03-22 02:53:00 +00002447 // This is a splat operation if each element of the permute is the same, and
2448 // if the value doesn't reference the second vector.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002449 unsigned NumElems = N->getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002450 SDValue ElementBase;
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002451 unsigned i = 0;
2452 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002453 SDValue Elt = N->getOperand(i);
Reid Spencer3ed469c2006-11-02 20:25:50 +00002454 if (isa<ConstantSDNode>(Elt)) {
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002455 ElementBase = Elt;
2456 break;
2457 }
2458 }
2459
2460 if (!ElementBase.Val)
2461 return false;
2462
2463 for (; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002464 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002465 if (Arg.getOpcode() == ISD::UNDEF) continue;
2466 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002467 if (Arg != ElementBase) return false;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002468 }
2469
2470 // Make sure it is a splat of the first vector operand.
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002471 return cast<ConstantSDNode>(ElementBase)->getValue() < NumElems;
Evan Chengb9df0ca2006-03-22 02:53:00 +00002472}
2473
Evan Chengc575ca22006-04-17 20:43:08 +00002474/// isSplatMask - Return true if the specified VECTOR_SHUFFLE operand specifies
2475/// a splat of a single element and it's a 2 or 4 element mask.
2476bool X86::isSplatMask(SDNode *N) {
2477 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2478
Evan Cheng94fe5eb2006-04-19 23:28:59 +00002479 // We can only splat 64-bit, and 32-bit quantities with a single instruction.
Evan Chengc575ca22006-04-17 20:43:08 +00002480 if (N->getNumOperands() != 4 && N->getNumOperands() != 2)
2481 return false;
2482 return ::isSplatMask(N);
2483}
2484
Evan Chengf686d9b2006-10-27 21:08:32 +00002485/// isSplatLoMask - Return true if the specified VECTOR_SHUFFLE operand
2486/// specifies a splat of zero element.
2487bool X86::isSplatLoMask(SDNode *N) {
2488 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2489
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002490 for (unsigned i = 0, e = N->getNumOperands(); i < e; ++i)
Evan Chengf686d9b2006-10-27 21:08:32 +00002491 if (!isUndefOrEqual(N->getOperand(i), 0))
2492 return false;
2493 return true;
2494}
2495
Evan Cheng63d33002006-03-22 08:01:21 +00002496/// getShuffleSHUFImmediate - Return the appropriate immediate to shuffle
2497/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUF* and SHUFP*
2498/// instructions.
2499unsigned X86::getShuffleSHUFImmediate(SDNode *N) {
Evan Chengb9df0ca2006-03-22 02:53:00 +00002500 unsigned NumOperands = N->getNumOperands();
2501 unsigned Shift = (NumOperands == 4) ? 2 : 1;
2502 unsigned Mask = 0;
Evan Cheng36b27f32006-03-28 23:41:33 +00002503 for (unsigned i = 0; i < NumOperands; ++i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002504 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002505 SDValue Arg = N->getOperand(NumOperands-i-1);
Evan Chengef698ca2006-03-31 00:30:29 +00002506 if (Arg.getOpcode() != ISD::UNDEF)
2507 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14aed5e2006-03-24 01:18:28 +00002508 if (Val >= NumOperands) Val -= NumOperands;
Evan Cheng63d33002006-03-22 08:01:21 +00002509 Mask |= Val;
Evan Cheng36b27f32006-03-28 23:41:33 +00002510 if (i != NumOperands - 1)
2511 Mask <<= Shift;
2512 }
Evan Cheng63d33002006-03-22 08:01:21 +00002513
2514 return Mask;
2515}
2516
Evan Cheng506d3df2006-03-29 23:07:14 +00002517/// getShufflePSHUFHWImmediate - Return the appropriate immediate to shuffle
2518/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFHW
2519/// instructions.
2520unsigned X86::getShufflePSHUFHWImmediate(SDNode *N) {
2521 unsigned Mask = 0;
2522 // 8 nodes, but we only care about the last 4.
2523 for (unsigned i = 7; i >= 4; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002524 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002525 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002526 if (Arg.getOpcode() != ISD::UNDEF)
2527 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002528 Mask |= (Val - 4);
2529 if (i != 4)
2530 Mask <<= 2;
2531 }
2532
2533 return Mask;
2534}
2535
2536/// getShufflePSHUFLWImmediate - Return the appropriate immediate to shuffle
2537/// the specified isShuffleMask VECTOR_SHUFFLE mask with PSHUFLW
2538/// instructions.
2539unsigned X86::getShufflePSHUFLWImmediate(SDNode *N) {
2540 unsigned Mask = 0;
2541 // 8 nodes, but we only care about the first 4.
2542 for (int i = 3; i >= 0; --i) {
Evan Chengef698ca2006-03-31 00:30:29 +00002543 unsigned Val = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00002544 SDValue Arg = N->getOperand(i);
Evan Chengef698ca2006-03-31 00:30:29 +00002545 if (Arg.getOpcode() != ISD::UNDEF)
2546 Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng506d3df2006-03-29 23:07:14 +00002547 Mask |= Val;
2548 if (i != 0)
2549 Mask <<= 2;
2550 }
2551
2552 return Mask;
2553}
2554
Evan Chengc21a0532006-04-05 01:47:37 +00002555/// isPSHUFHW_PSHUFLWMask - true if the specified VECTOR_SHUFFLE operand
2556/// specifies a 8 element shuffle that can be broken into a pair of
2557/// PSHUFHW and PSHUFLW.
2558static bool isPSHUFHW_PSHUFLWMask(SDNode *N) {
2559 assert(N->getOpcode() == ISD::BUILD_VECTOR);
2560
2561 if (N->getNumOperands() != 8)
2562 return false;
2563
2564 // Lower quadword shuffled.
2565 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002566 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002567 if (Arg.getOpcode() == ISD::UNDEF) continue;
2568 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2569 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00002570 if (Val >= 4)
Evan Chengc21a0532006-04-05 01:47:37 +00002571 return false;
2572 }
2573
2574 // Upper quadword shuffled.
2575 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002576 SDValue Arg = N->getOperand(i);
Evan Chengc21a0532006-04-05 01:47:37 +00002577 if (Arg.getOpcode() == ISD::UNDEF) continue;
2578 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2579 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2580 if (Val < 4 || Val > 7)
2581 return false;
2582 }
2583
2584 return true;
2585}
2586
Chris Lattner8a594482007-11-25 00:24:49 +00002587/// CommuteVectorShuffle - Swap vector_shuffle operands as well as
Evan Cheng5ced1d82006-04-06 23:23:56 +00002588/// values in ther permute mask.
Dan Gohman475871a2008-07-27 21:46:04 +00002589static SDValue CommuteVectorShuffle(SDValue Op, SDValue &V1,
2590 SDValue &V2, SDValue &Mask,
Evan Cheng9eca5e82006-10-25 21:49:50 +00002591 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002592 MVT VT = Op.getValueType();
2593 MVT MaskVT = Mask.getValueType();
2594 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng5ced1d82006-04-06 23:23:56 +00002595 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002596 SmallVector<SDValue, 8> MaskVec;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002597
2598 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002599 SDValue Arg = Mask.getOperand(i);
Evan Cheng80d428c2006-04-19 22:48:17 +00002600 if (Arg.getOpcode() == ISD::UNDEF) {
2601 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2602 continue;
2603 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002604 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2605 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2606 if (Val < NumElems)
2607 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2608 else
2609 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2610 }
2611
Evan Cheng9eca5e82006-10-25 21:49:50 +00002612 std::swap(V1, V2);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002613 Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
Evan Cheng9eca5e82006-10-25 21:49:50 +00002614 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng5ced1d82006-04-06 23:23:56 +00002615}
2616
Evan Cheng779ccea2007-12-07 21:30:01 +00002617/// CommuteVectorShuffleMask - Change values in a shuffle permute mask assuming
2618/// the two vector operands have swapped position.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002619static
Dan Gohman475871a2008-07-27 21:46:04 +00002620SDValue CommuteVectorShuffleMask(SDValue Mask, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002621 MVT MaskVT = Mask.getValueType();
2622 MVT EltVT = MaskVT.getVectorElementType();
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002623 unsigned NumElems = Mask.getNumOperands();
Dan Gohman475871a2008-07-27 21:46:04 +00002624 SmallVector<SDValue, 8> MaskVec;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002625 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002626 SDValue Arg = Mask.getOperand(i);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00002627 if (Arg.getOpcode() == ISD::UNDEF) {
2628 MaskVec.push_back(DAG.getNode(ISD::UNDEF, EltVT));
2629 continue;
2630 }
2631 assert(isa<ConstantSDNode>(Arg) && "Invalid VECTOR_SHUFFLE mask!");
2632 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2633 if (Val < NumElems)
2634 MaskVec.push_back(DAG.getConstant(Val + NumElems, EltVT));
2635 else
2636 MaskVec.push_back(DAG.getConstant(Val - NumElems, EltVT));
2637 }
2638 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], NumElems);
2639}
2640
2641
Evan Cheng533a0aa2006-04-19 20:35:22 +00002642/// ShouldXformToMOVHLPS - Return true if the node should be transformed to
2643/// match movhlps. The lower half elements should come from upper half of
2644/// V1 (and in order), and the upper half elements should come from the upper
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00002645/// half of V2 (and in order).
Evan Cheng533a0aa2006-04-19 20:35:22 +00002646static bool ShouldXformToMOVHLPS(SDNode *Mask) {
2647 unsigned NumElems = Mask->getNumOperands();
2648 if (NumElems != 4)
2649 return false;
2650 for (unsigned i = 0, e = 2; i != e; ++i)
2651 if (!isUndefOrEqual(Mask->getOperand(i), i+2))
2652 return false;
2653 for (unsigned i = 2; i != 4; ++i)
2654 if (!isUndefOrEqual(Mask->getOperand(i), i+4))
2655 return false;
2656 return true;
2657}
2658
Evan Cheng5ced1d82006-04-06 23:23:56 +00002659/// isScalarLoadToVector - Returns true if the node is a scalar load that
Evan Cheng7e2ff772008-05-08 00:57:18 +00002660/// is promoted to a vector. It also returns the LoadSDNode by reference if
2661/// required.
2662static bool isScalarLoadToVector(SDNode *N, LoadSDNode **LD = NULL) {
Evan Cheng533a0aa2006-04-19 20:35:22 +00002663 if (N->getOpcode() == ISD::SCALAR_TO_VECTOR) {
2664 N = N->getOperand(0).Val;
Evan Cheng7e2ff772008-05-08 00:57:18 +00002665 if (ISD::isNON_EXTLoad(N)) {
2666 if (LD)
2667 *LD = cast<LoadSDNode>(N);
2668 return true;
2669 }
Evan Cheng5ced1d82006-04-06 23:23:56 +00002670 }
2671 return false;
2672}
2673
Evan Cheng533a0aa2006-04-19 20:35:22 +00002674/// ShouldXformToMOVLP{S|D} - Return true if the node should be transformed to
2675/// match movlp{s|d}. The lower half elements should come from lower half of
2676/// V1 (and in order), and the upper half elements should come from the upper
2677/// half of V2 (and in order). And since V1 will become the source of the
2678/// MOVLP, it must be either a vector load or a scalar load to vector.
Evan Cheng23425f52006-10-09 21:39:25 +00002679static bool ShouldXformToMOVLP(SDNode *V1, SDNode *V2, SDNode *Mask) {
Evan Cheng466685d2006-10-09 20:57:25 +00002680 if (!ISD::isNON_EXTLoad(V1) && !isScalarLoadToVector(V1))
Evan Cheng533a0aa2006-04-19 20:35:22 +00002681 return false;
Evan Cheng23425f52006-10-09 21:39:25 +00002682 // Is V2 is a vector load, don't do this transformation. We will try to use
2683 // load folding shufps op.
2684 if (ISD::isNON_EXTLoad(V2))
2685 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002686
Evan Cheng533a0aa2006-04-19 20:35:22 +00002687 unsigned NumElems = Mask->getNumOperands();
2688 if (NumElems != 2 && NumElems != 4)
2689 return false;
2690 for (unsigned i = 0, e = NumElems/2; i != e; ++i)
2691 if (!isUndefOrEqual(Mask->getOperand(i), i))
2692 return false;
2693 for (unsigned i = NumElems/2; i != NumElems; ++i)
2694 if (!isUndefOrEqual(Mask->getOperand(i), i+NumElems))
2695 return false;
2696 return true;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002697}
2698
Evan Cheng39623da2006-04-20 08:58:49 +00002699/// isSplatVector - Returns true if N is a BUILD_VECTOR node whose elements are
2700/// all the same.
2701static bool isSplatVector(SDNode *N) {
2702 if (N->getOpcode() != ISD::BUILD_VECTOR)
2703 return false;
Evan Cheng5ced1d82006-04-06 23:23:56 +00002704
Dan Gohman475871a2008-07-27 21:46:04 +00002705 SDValue SplatValue = N->getOperand(0);
Evan Cheng39623da2006-04-20 08:58:49 +00002706 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i)
2707 if (N->getOperand(i) != SplatValue)
Evan Cheng5ced1d82006-04-06 23:23:56 +00002708 return false;
2709 return true;
2710}
2711
Evan Cheng8cf723d2006-09-08 01:50:06 +00002712/// isUndefShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2713/// to an undef.
2714static bool isUndefShuffle(SDNode *N) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002715 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
Evan Cheng8cf723d2006-09-08 01:50:06 +00002716 return false;
2717
Dan Gohman475871a2008-07-27 21:46:04 +00002718 SDValue V1 = N->getOperand(0);
2719 SDValue V2 = N->getOperand(1);
2720 SDValue Mask = N->getOperand(2);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002721 unsigned NumElems = Mask.getNumOperands();
2722 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002723 SDValue Arg = Mask.getOperand(i);
Evan Cheng8cf723d2006-09-08 01:50:06 +00002724 if (Arg.getOpcode() != ISD::UNDEF) {
2725 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2726 if (Val < NumElems && V1.getOpcode() != ISD::UNDEF)
2727 return false;
2728 else if (Val >= NumElems && V2.getOpcode() != ISD::UNDEF)
2729 return false;
2730 }
2731 }
2732 return true;
2733}
2734
Evan Cheng213d2cf2007-05-17 18:45:50 +00002735/// isZeroNode - Returns true if Elt is a constant zero or a floating point
2736/// constant +0.0.
Dan Gohman475871a2008-07-27 21:46:04 +00002737static inline bool isZeroNode(SDValue Elt) {
Evan Cheng213d2cf2007-05-17 18:45:50 +00002738 return ((isa<ConstantSDNode>(Elt) &&
2739 cast<ConstantSDNode>(Elt)->getValue() == 0) ||
2740 (isa<ConstantFPSDNode>(Elt) &&
Dale Johanneseneaf08942007-08-31 04:03:46 +00002741 cast<ConstantFPSDNode>(Elt)->getValueAPF().isPosZero()));
Evan Cheng213d2cf2007-05-17 18:45:50 +00002742}
2743
2744/// isZeroShuffle - Returns true if N is a VECTOR_SHUFFLE that can be resolved
2745/// to an zero vector.
2746static bool isZeroShuffle(SDNode *N) {
2747 if (N->getOpcode() != ISD::VECTOR_SHUFFLE)
2748 return false;
2749
Dan Gohman475871a2008-07-27 21:46:04 +00002750 SDValue V1 = N->getOperand(0);
2751 SDValue V2 = N->getOperand(1);
2752 SDValue Mask = N->getOperand(2);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002753 unsigned NumElems = Mask.getNumOperands();
2754 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002755 SDValue Arg = Mask.getOperand(i);
Chris Lattner8a594482007-11-25 00:24:49 +00002756 if (Arg.getOpcode() == ISD::UNDEF)
2757 continue;
2758
2759 unsigned Idx = cast<ConstantSDNode>(Arg)->getValue();
2760 if (Idx < NumElems) {
2761 unsigned Opc = V1.Val->getOpcode();
2762 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V1.Val))
2763 continue;
2764 if (Opc != ISD::BUILD_VECTOR ||
2765 !isZeroNode(V1.Val->getOperand(Idx)))
2766 return false;
2767 } else if (Idx >= NumElems) {
2768 unsigned Opc = V2.Val->getOpcode();
2769 if (Opc == ISD::UNDEF || ISD::isBuildVectorAllZeros(V2.Val))
2770 continue;
2771 if (Opc != ISD::BUILD_VECTOR ||
2772 !isZeroNode(V2.Val->getOperand(Idx - NumElems)))
2773 return false;
Evan Cheng213d2cf2007-05-17 18:45:50 +00002774 }
2775 }
2776 return true;
2777}
2778
2779/// getZeroVector - Returns a vector of specified type with all zero elements.
2780///
Dan Gohman475871a2008-07-27 21:46:04 +00002781static SDValue getZeroVector(MVT VT, bool HasSSE2, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002782 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002783
2784 // Always build zero vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2785 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002786 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002787 if (VT.getSizeInBits() == 64) { // MMX
Dan Gohman475871a2008-07-27 21:46:04 +00002788 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002789 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002790 } else if (HasSSE2) { // SSE2
Dan Gohman475871a2008-07-27 21:46:04 +00002791 SDValue Cst = DAG.getTargetConstant(0, MVT::i32);
Chris Lattner8a594482007-11-25 00:24:49 +00002792 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
Evan Chengf0df0312008-05-15 08:39:06 +00002793 } else { // SSE1
Dan Gohman475871a2008-07-27 21:46:04 +00002794 SDValue Cst = DAG.getTargetConstantFP(+0.0, MVT::f32);
Evan Chengf0df0312008-05-15 08:39:06 +00002795 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4f32, Cst, Cst, Cst, Cst);
2796 }
Chris Lattner8a594482007-11-25 00:24:49 +00002797 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
Evan Cheng213d2cf2007-05-17 18:45:50 +00002798}
2799
Chris Lattner8a594482007-11-25 00:24:49 +00002800/// getOnesVector - Returns a vector of specified type with all bits set.
2801///
Dan Gohman475871a2008-07-27 21:46:04 +00002802static SDValue getOnesVector(MVT VT, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002803 assert(VT.isVector() && "Expected a vector type");
Chris Lattner8a594482007-11-25 00:24:49 +00002804
2805 // Always build ones vectors as <4 x i32> or <2 x i32> bitcasted to their dest
2806 // type. This ensures they get CSE'd.
Dan Gohman475871a2008-07-27 21:46:04 +00002807 SDValue Cst = DAG.getTargetConstant(~0U, MVT::i32);
2808 SDValue Vec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00002809 if (VT.getSizeInBits() == 64) // MMX
Chris Lattner8a594482007-11-25 00:24:49 +00002810 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, Cst, Cst);
2811 else // SSE
2812 Vec = DAG.getNode(ISD::BUILD_VECTOR, MVT::v4i32, Cst, Cst, Cst, Cst);
2813 return DAG.getNode(ISD::BIT_CONVERT, VT, Vec);
2814}
2815
2816
Evan Cheng39623da2006-04-20 08:58:49 +00002817/// NormalizeMask - V2 is a splat, modify the mask (if needed) so all elements
2818/// that point to V2 points to its first element.
Dan Gohman475871a2008-07-27 21:46:04 +00002819static SDValue NormalizeMask(SDValue Mask, SelectionDAG &DAG) {
Evan Cheng39623da2006-04-20 08:58:49 +00002820 assert(Mask.getOpcode() == ISD::BUILD_VECTOR);
2821
2822 bool Changed = false;
Dan Gohman475871a2008-07-27 21:46:04 +00002823 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002824 unsigned NumElems = Mask.getNumOperands();
2825 for (unsigned i = 0; i != NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00002826 SDValue Arg = Mask.getOperand(i);
Evan Cheng39623da2006-04-20 08:58:49 +00002827 if (Arg.getOpcode() != ISD::UNDEF) {
2828 unsigned Val = cast<ConstantSDNode>(Arg)->getValue();
2829 if (Val > NumElems) {
2830 Arg = DAG.getConstant(NumElems, Arg.getValueType());
2831 Changed = true;
2832 }
2833 }
2834 MaskVec.push_back(Arg);
2835 }
2836
2837 if (Changed)
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002838 Mask = DAG.getNode(ISD::BUILD_VECTOR, Mask.getValueType(),
2839 &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002840 return Mask;
2841}
2842
Evan Cheng017dcc62006-04-21 01:05:10 +00002843/// getMOVLMask - Returns a vector_shuffle mask for an movs{s|d}, movd
2844/// operation of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002845static SDValue getMOVLMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002846 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2847 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002848
Dan Gohman475871a2008-07-27 21:46:04 +00002849 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002850 MaskVec.push_back(DAG.getConstant(NumElems, BaseVT));
2851 for (unsigned i = 1; i != NumElems; ++i)
2852 MaskVec.push_back(DAG.getConstant(i, BaseVT));
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002853 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002854}
2855
Evan Chengc575ca22006-04-17 20:43:08 +00002856/// getUnpacklMask - Returns a vector_shuffle mask for an unpackl operation
2857/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002858static SDValue getUnpacklMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002859 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2860 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002861 SmallVector<SDValue, 8> MaskVec;
Evan Chengc575ca22006-04-17 20:43:08 +00002862 for (unsigned i = 0, e = NumElems/2; i != e; ++i) {
2863 MaskVec.push_back(DAG.getConstant(i, BaseVT));
2864 MaskVec.push_back(DAG.getConstant(i + NumElems, BaseVT));
2865 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002866 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Chengc575ca22006-04-17 20:43:08 +00002867}
2868
Evan Cheng39623da2006-04-20 08:58:49 +00002869/// getUnpackhMask - Returns a vector_shuffle mask for an unpackh operation
2870/// of specified width.
Dan Gohman475871a2008-07-27 21:46:04 +00002871static SDValue getUnpackhMask(unsigned NumElems, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002872 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2873 MVT BaseVT = MaskVT.getVectorElementType();
Evan Cheng39623da2006-04-20 08:58:49 +00002874 unsigned Half = NumElems/2;
Dan Gohman475871a2008-07-27 21:46:04 +00002875 SmallVector<SDValue, 8> MaskVec;
Evan Cheng39623da2006-04-20 08:58:49 +00002876 for (unsigned i = 0; i != Half; ++i) {
2877 MaskVec.push_back(DAG.getConstant(i + Half, BaseVT));
2878 MaskVec.push_back(DAG.getConstant(i + NumElems + Half, BaseVT));
2879 }
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002880 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
Evan Cheng39623da2006-04-20 08:58:49 +00002881}
2882
Chris Lattner62098042008-03-09 01:05:04 +00002883/// getSwapEltZeroMask - Returns a vector_shuffle mask for a shuffle that swaps
2884/// element #0 of a vector with the specified index, leaving the rest of the
2885/// elements in place.
Dan Gohman475871a2008-07-27 21:46:04 +00002886static SDValue getSwapEltZeroMask(unsigned NumElems, unsigned DestElt,
Chris Lattner62098042008-03-09 01:05:04 +00002887 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002888 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2889 MVT BaseVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002890 SmallVector<SDValue, 8> MaskVec;
Chris Lattner62098042008-03-09 01:05:04 +00002891 // Element #0 of the result gets the elt we are replacing.
2892 MaskVec.push_back(DAG.getConstant(DestElt, BaseVT));
2893 for (unsigned i = 1; i != NumElems; ++i)
2894 MaskVec.push_back(DAG.getConstant(i == DestElt ? 0 : i, BaseVT));
2895 return DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], MaskVec.size());
2896}
2897
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002898/// PromoteSplat - Promote a splat of v4f32, v8i16 or v16i8 to v4i32.
Dan Gohman475871a2008-07-27 21:46:04 +00002899static SDValue PromoteSplat(SDValue Op, SelectionDAG &DAG, bool HasSSE2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002900 MVT PVT = HasSSE2 ? MVT::v4i32 : MVT::v4f32;
2901 MVT VT = Op.getValueType();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002902 if (PVT == VT)
2903 return Op;
Dan Gohman475871a2008-07-27 21:46:04 +00002904 SDValue V1 = Op.getOperand(0);
2905 SDValue Mask = Op.getOperand(2);
Evan Cheng017dcc62006-04-21 01:05:10 +00002906 unsigned NumElems = Mask.getNumOperands();
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002907 // Special handling of v4f32 -> v4i32.
2908 if (VT != MVT::v4f32) {
2909 Mask = getUnpacklMask(NumElems, DAG);
2910 while (NumElems > 4) {
2911 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1, Mask);
2912 NumElems >>= 1;
2913 }
Evan Chengf0df0312008-05-15 08:39:06 +00002914 Mask = getZeroVector(MVT::v4i32, true, DAG);
Evan Chengc575ca22006-04-17 20:43:08 +00002915 }
Evan Chengc575ca22006-04-17 20:43:08 +00002916
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002917 V1 = DAG.getNode(ISD::BIT_CONVERT, PVT, V1);
Dan Gohman475871a2008-07-27 21:46:04 +00002918 SDValue Shuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, PVT, V1,
Evan Cheng0c0f83f2008-04-05 00:30:36 +00002919 DAG.getNode(ISD::UNDEF, PVT), Mask);
Evan Chengc575ca22006-04-17 20:43:08 +00002920 return DAG.getNode(ISD::BIT_CONVERT, VT, Shuffle);
2921}
2922
Evan Chengba05f722006-04-21 23:03:30 +00002923/// getShuffleVectorZeroOrUndef - Return a vector_shuffle of the specified
Chris Lattner8a594482007-11-25 00:24:49 +00002924/// vector of zero or undef vector. This produces a shuffle where the low
2925/// element of V2 is swizzled into the zero/undef vector, landing at element
2926/// 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 +00002927static SDValue getShuffleVectorZeroOrUndef(SDValue V2, unsigned Idx,
Evan Chengf0df0312008-05-15 08:39:06 +00002928 bool isZero, bool HasSSE2,
2929 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00002930 MVT VT = V2.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00002931 SDValue V1 = isZero
Evan Chengf0df0312008-05-15 08:39:06 +00002932 ? getZeroVector(VT, HasSSE2, DAG) : DAG.getNode(ISD::UNDEF, VT);
Duncan Sands83ec4b62008-06-06 12:08:01 +00002933 unsigned NumElems = V2.getValueType().getVectorNumElements();
2934 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
2935 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00002936 SmallVector<SDValue, 16> MaskVec;
Chris Lattner8a594482007-11-25 00:24:49 +00002937 for (unsigned i = 0; i != NumElems; ++i)
2938 if (i == Idx) // If this is the insertion idx, put the low elt of V2 here.
2939 MaskVec.push_back(DAG.getConstant(NumElems, EVT));
2940 else
2941 MaskVec.push_back(DAG.getConstant(i, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00002942 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00002943 &MaskVec[0], MaskVec.size());
Evan Chengba05f722006-04-21 23:03:30 +00002944 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, Mask);
Evan Cheng017dcc62006-04-21 01:05:10 +00002945}
2946
Evan Chengf26ffe92008-05-29 08:22:04 +00002947/// getNumOfConsecutiveZeros - Return the number of elements in a result of
2948/// a shuffle that is zero.
2949static
Dan Gohman475871a2008-07-27 21:46:04 +00002950unsigned getNumOfConsecutiveZeros(SDValue Op, SDValue Mask,
Evan Chengf26ffe92008-05-29 08:22:04 +00002951 unsigned NumElems, bool Low,
2952 SelectionDAG &DAG) {
2953 unsigned NumZeros = 0;
2954 for (unsigned i = 0; i < NumElems; ++i) {
Evan Chengab262272008-06-25 20:52:59 +00002955 unsigned Index = Low ? i : NumElems-i-1;
Dan Gohman475871a2008-07-27 21:46:04 +00002956 SDValue Idx = Mask.getOperand(Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00002957 if (Idx.getOpcode() == ISD::UNDEF) {
2958 ++NumZeros;
2959 continue;
2960 }
Dan Gohman475871a2008-07-27 21:46:04 +00002961 SDValue Elt = DAG.getShuffleScalarElt(Op.Val, Index);
Evan Chengf26ffe92008-05-29 08:22:04 +00002962 if (Elt.Val && isZeroNode(Elt))
2963 ++NumZeros;
2964 else
2965 break;
2966 }
2967 return NumZeros;
2968}
2969
2970/// isVectorShift - Returns true if the shuffle can be implemented as a
2971/// logical left or right shift of a vector.
Dan Gohman475871a2008-07-27 21:46:04 +00002972static bool isVectorShift(SDValue Op, SDValue Mask, SelectionDAG &DAG,
2973 bool &isLeft, SDValue &ShVal, unsigned &ShAmt) {
Evan Chengf26ffe92008-05-29 08:22:04 +00002974 unsigned NumElems = Mask.getNumOperands();
2975
2976 isLeft = true;
2977 unsigned NumZeros= getNumOfConsecutiveZeros(Op, Mask, NumElems, true, DAG);
2978 if (!NumZeros) {
2979 isLeft = false;
2980 NumZeros = getNumOfConsecutiveZeros(Op, Mask, NumElems, false, DAG);
2981 if (!NumZeros)
2982 return false;
2983 }
2984
2985 bool SeenV1 = false;
2986 bool SeenV2 = false;
2987 for (unsigned i = NumZeros; i < NumElems; ++i) {
2988 unsigned Val = isLeft ? (i - NumZeros) : i;
Dan Gohman475871a2008-07-27 21:46:04 +00002989 SDValue Idx = Mask.getOperand(isLeft ? i : (i - NumZeros));
Evan Chengf26ffe92008-05-29 08:22:04 +00002990 if (Idx.getOpcode() == ISD::UNDEF)
2991 continue;
2992 unsigned Index = cast<ConstantSDNode>(Idx)->getValue();
2993 if (Index < NumElems)
2994 SeenV1 = true;
2995 else {
2996 Index -= NumElems;
2997 SeenV2 = true;
2998 }
2999 if (Index != Val)
3000 return false;
3001 }
3002 if (SeenV1 && SeenV2)
3003 return false;
3004
3005 ShVal = SeenV1 ? Op.getOperand(0) : Op.getOperand(1);
3006 ShAmt = NumZeros;
3007 return true;
3008}
3009
3010
Evan Chengc78d3b42006-04-24 18:01:45 +00003011/// LowerBuildVectorv16i8 - Custom lower build_vector of v16i8.
3012///
Dan Gohman475871a2008-07-27 21:46:04 +00003013static SDValue LowerBuildVectorv16i8(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003014 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003015 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003016 if (NumNonZero > 8)
Dan Gohman475871a2008-07-27 21:46:04 +00003017 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003018
Dan Gohman475871a2008-07-27 21:46:04 +00003019 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003020 bool First = true;
3021 for (unsigned i = 0; i < 16; ++i) {
3022 bool ThisIsNonZero = (NonZeros & (1 << i)) != 0;
3023 if (ThisIsNonZero && First) {
3024 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003025 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003026 else
3027 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3028 First = false;
3029 }
3030
3031 if ((i & 1) != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003032 SDValue ThisElt(0, 0), LastElt(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003033 bool LastIsNonZero = (NonZeros & (1 << (i-1))) != 0;
3034 if (LastIsNonZero) {
3035 LastElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i-1));
3036 }
3037 if (ThisIsNonZero) {
3038 ThisElt = DAG.getNode(ISD::ZERO_EXTEND, MVT::i16, Op.getOperand(i));
3039 ThisElt = DAG.getNode(ISD::SHL, MVT::i16,
3040 ThisElt, DAG.getConstant(8, MVT::i8));
3041 if (LastIsNonZero)
3042 ThisElt = DAG.getNode(ISD::OR, MVT::i16, ThisElt, LastElt);
3043 } else
3044 ThisElt = LastElt;
3045
3046 if (ThisElt.Val)
3047 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, ThisElt,
Chris Lattner0bd48932008-01-17 07:00:52 +00003048 DAG.getIntPtrConstant(i/2));
Evan Chengc78d3b42006-04-24 18:01:45 +00003049 }
3050 }
3051
3052 return DAG.getNode(ISD::BIT_CONVERT, MVT::v16i8, V);
3053}
3054
Bill Wendlinga348c562007-03-22 18:42:45 +00003055/// LowerBuildVectorv8i16 - Custom lower build_vector of v8i16.
Evan Chengc78d3b42006-04-24 18:01:45 +00003056///
Dan Gohman475871a2008-07-27 21:46:04 +00003057static SDValue LowerBuildVectorv8i16(SDValue Op, unsigned NonZeros,
Evan Chengc78d3b42006-04-24 18:01:45 +00003058 unsigned NumNonZero, unsigned NumZero,
Evan Cheng25ab6902006-09-08 06:48:29 +00003059 SelectionDAG &DAG, TargetLowering &TLI) {
Evan Chengc78d3b42006-04-24 18:01:45 +00003060 if (NumNonZero > 4)
Dan Gohman475871a2008-07-27 21:46:04 +00003061 return SDValue();
Evan Chengc78d3b42006-04-24 18:01:45 +00003062
Dan Gohman475871a2008-07-27 21:46:04 +00003063 SDValue V(0, 0);
Evan Chengc78d3b42006-04-24 18:01:45 +00003064 bool First = true;
3065 for (unsigned i = 0; i < 8; ++i) {
3066 bool isNonZero = (NonZeros & (1 << i)) != 0;
3067 if (isNonZero) {
3068 if (First) {
3069 if (NumZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003070 V = getZeroVector(MVT::v8i16, true, DAG);
Evan Chengc78d3b42006-04-24 18:01:45 +00003071 else
3072 V = DAG.getNode(ISD::UNDEF, MVT::v8i16);
3073 First = false;
3074 }
3075 V = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, V, Op.getOperand(i),
Chris Lattner0bd48932008-01-17 07:00:52 +00003076 DAG.getIntPtrConstant(i));
Evan Chengc78d3b42006-04-24 18:01:45 +00003077 }
3078 }
3079
3080 return V;
3081}
3082
Evan Chengf26ffe92008-05-29 08:22:04 +00003083/// getVShift - Return a vector logical shift node.
3084///
Dan Gohman475871a2008-07-27 21:46:04 +00003085static SDValue getVShift(bool isLeft, MVT VT, SDValue SrcOp,
Evan Chengf26ffe92008-05-29 08:22:04 +00003086 unsigned NumBits, SelectionDAG &DAG,
3087 const TargetLowering &TLI) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003088 bool isMMX = VT.getSizeInBits() == 64;
3089 MVT ShVT = isMMX ? MVT::v1i64 : MVT::v2i64;
Evan Chengf26ffe92008-05-29 08:22:04 +00003090 unsigned Opc = isLeft ? X86ISD::VSHL : X86ISD::VSRL;
3091 SrcOp = DAG.getNode(ISD::BIT_CONVERT, ShVT, SrcOp);
3092 return DAG.getNode(ISD::BIT_CONVERT, VT,
3093 DAG.getNode(Opc, ShVT, SrcOp,
3094 DAG.getConstant(NumBits, TLI.getShiftAmountTy())));
3095}
3096
Dan Gohman475871a2008-07-27 21:46:04 +00003097SDValue
3098X86TargetLowering::LowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) {
Chris Lattner8a594482007-11-25 00:24:49 +00003099 // All zero's are handled with pxor, all one's are handled with pcmpeqd.
3100 if (ISD::isBuildVectorAllZeros(Op.Val) || ISD::isBuildVectorAllOnes(Op.Val)) {
3101 // Canonicalize this to either <4 x i32> or <2 x i32> (SSE vs MMX) to
3102 // 1) ensure the zero vectors are CSE'd, and 2) ensure that i64 scalars are
3103 // eliminated on x86-32 hosts.
3104 if (Op.getValueType() == MVT::v4i32 || Op.getValueType() == MVT::v2i32)
3105 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003106
Chris Lattner8a594482007-11-25 00:24:49 +00003107 if (ISD::isBuildVectorAllOnes(Op.Val))
3108 return getOnesVector(Op.getValueType(), DAG);
Evan Chengf0df0312008-05-15 08:39:06 +00003109 return getZeroVector(Op.getValueType(), Subtarget->hasSSE2(), DAG);
Chris Lattner8a594482007-11-25 00:24:49 +00003110 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003111
Duncan Sands83ec4b62008-06-06 12:08:01 +00003112 MVT VT = Op.getValueType();
3113 MVT EVT = VT.getVectorElementType();
3114 unsigned EVTBits = EVT.getSizeInBits();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003115
3116 unsigned NumElems = Op.getNumOperands();
3117 unsigned NumZero = 0;
3118 unsigned NumNonZero = 0;
3119 unsigned NonZeros = 0;
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003120 bool IsAllConstants = true;
Dan Gohman475871a2008-07-27 21:46:04 +00003121 SmallSet<SDValue, 8> Values;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003122 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003123 SDValue Elt = Op.getOperand(i);
Evan Chengdb2d5242007-12-12 06:45:40 +00003124 if (Elt.getOpcode() == ISD::UNDEF)
3125 continue;
3126 Values.insert(Elt);
3127 if (Elt.getOpcode() != ISD::Constant &&
3128 Elt.getOpcode() != ISD::ConstantFP)
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003129 IsAllConstants = false;
Evan Chengdb2d5242007-12-12 06:45:40 +00003130 if (isZeroNode(Elt))
3131 NumZero++;
3132 else {
3133 NonZeros |= (1 << i);
3134 NumNonZero++;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003135 }
3136 }
3137
Dan Gohman7f321562007-06-25 16:23:39 +00003138 if (NumNonZero == 0) {
Chris Lattner8a594482007-11-25 00:24:49 +00003139 // All undef vector. Return an UNDEF. All zero vectors were handled above.
3140 return DAG.getNode(ISD::UNDEF, VT);
Dan Gohman7f321562007-06-25 16:23:39 +00003141 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003142
Chris Lattner67f453a2008-03-09 05:42:06 +00003143 // Special case for single non-zero, non-undef, element.
Evan Chengdb2d5242007-12-12 06:45:40 +00003144 if (NumNonZero == 1 && NumElems <= 4) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00003145 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003146 SDValue Item = Op.getOperand(Idx);
Chris Lattner19f79692008-03-08 22:59:52 +00003147
Chris Lattner62098042008-03-09 01:05:04 +00003148 // If this is an insertion of an i64 value on x86-32, and if the top bits of
3149 // the value are obviously zero, truncate the value to i32 and do the
3150 // insertion that way. Only do this if the value is non-constant or if the
3151 // value is a constant being inserted into element 0. It is cheaper to do
3152 // a constant pool load than it is to do a movd + shuffle.
3153 if (EVT == MVT::i64 && !Subtarget->is64Bit() &&
3154 (!IsAllConstants || Idx == 0)) {
3155 if (DAG.MaskedValueIsZero(Item, APInt::getBitsSet(64, 32, 64))) {
3156 // Handle MMX and SSE both.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003157 MVT VecVT = VT == MVT::v2i64 ? MVT::v4i32 : MVT::v2i32;
3158 unsigned VecElts = VT == MVT::v2i64 ? 4 : 2;
Chris Lattner62098042008-03-09 01:05:04 +00003159
3160 // Truncate the value (which may itself be a constant) to i32, and
3161 // convert it to a vector with movd (S2V+shuffle to zero extend).
3162 Item = DAG.getNode(ISD::TRUNCATE, MVT::i32, Item);
3163 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VecVT, Item);
Evan Chengf0df0312008-05-15 08:39:06 +00003164 Item = getShuffleVectorZeroOrUndef(Item, 0, true,
3165 Subtarget->hasSSE2(), DAG);
Chris Lattner62098042008-03-09 01:05:04 +00003166
3167 // Now we have our 32-bit value zero extended in the low element of
3168 // a vector. If Idx != 0, swizzle it into place.
3169 if (Idx != 0) {
Dan Gohman475871a2008-07-27 21:46:04 +00003170 SDValue Ops[] = {
Chris Lattner62098042008-03-09 01:05:04 +00003171 Item, DAG.getNode(ISD::UNDEF, Item.getValueType()),
3172 getSwapEltZeroMask(VecElts, Idx, DAG)
3173 };
3174 Item = DAG.getNode(ISD::VECTOR_SHUFFLE, VecVT, Ops, 3);
3175 }
3176 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(), Item);
3177 }
3178 }
3179
Chris Lattner19f79692008-03-08 22:59:52 +00003180 // If we have a constant or non-constant insertion into the low element of
3181 // a vector, we can do this with SCALAR_TO_VECTOR + shuffle of zero into
3182 // the rest of the elements. This will be matched as movd/movq/movss/movsd
3183 // depending on what the source datatype is. Because we can only get here
3184 // when NumElems <= 4, this only needs to handle i32/f32/i64/f64.
3185 if (Idx == 0 &&
3186 // Don't do this for i64 values on x86-32.
3187 (EVT != MVT::i64 || Subtarget->is64Bit())) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003188 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003189 // Turn it into a MOVL (i.e. movss, movsd, or movd) to a zero vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003190 return getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3191 Subtarget->hasSSE2(), DAG);
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003192 }
Evan Chengf26ffe92008-05-29 08:22:04 +00003193
3194 // Is it a vector logical left shift?
3195 if (NumElems == 2 && Idx == 1 &&
3196 isZeroNode(Op.getOperand(0)) && !isZeroNode(Op.getOperand(1))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003197 unsigned NumBits = VT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003198 return getVShift(true, VT,
3199 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(1)),
3200 NumBits/2, DAG, *this);
3201 }
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003202
3203 if (IsAllConstants) // Otherwise, it's better to do a constpool load.
Dan Gohman475871a2008-07-27 21:46:04 +00003204 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003205
Chris Lattner19f79692008-03-08 22:59:52 +00003206 // Otherwise, if this is a vector with i32 or f32 elements, and the element
3207 // is a non-constant being inserted into an element other than the low one,
3208 // we can't use a constant pool load. Instead, use SCALAR_TO_VECTOR (aka
3209 // movd/movss) to move this into the low element, then shuffle it into
3210 // place.
Evan Cheng0db9fe62006-04-25 20:13:52 +00003211 if (EVTBits == 32) {
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003212 Item = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Item);
3213
Evan Cheng0db9fe62006-04-25 20:13:52 +00003214 // Turn it into a shuffle of zero and zero-extended scalar to vector.
Evan Chengf0df0312008-05-15 08:39:06 +00003215 Item = getShuffleVectorZeroOrUndef(Item, 0, NumZero > 0,
3216 Subtarget->hasSSE2(), DAG);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003217 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3218 MVT MaskEVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003219 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003220 for (unsigned i = 0; i < NumElems; i++)
3221 MaskVec.push_back(DAG.getConstant((i == Idx) ? 0 : 1, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003222 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnerbd564bf2006-08-08 02:23:42 +00003223 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003224 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, Item,
3225 DAG.getNode(ISD::UNDEF, VT), Mask);
3226 }
3227 }
3228
Chris Lattner67f453a2008-03-09 05:42:06 +00003229 // Splat is obviously ok. Let legalizer expand it to a shuffle.
3230 if (Values.size() == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00003231 return SDValue();
Chris Lattner67f453a2008-03-09 05:42:06 +00003232
Dan Gohmana3941172007-07-24 22:55:08 +00003233 // A vector full of immediates; various special cases are already
3234 // handled, so this is best done with a single constant-pool load.
Chris Lattnerc9517fb2008-03-08 22:48:29 +00003235 if (IsAllConstants)
Dan Gohman475871a2008-07-27 21:46:04 +00003236 return SDValue();
Dan Gohmana3941172007-07-24 22:55:08 +00003237
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003238 // Let legalizer expand 2-wide build_vectors.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003239 if (EVTBits == 64) {
3240 if (NumNonZero == 1) {
3241 // One half is zero or undef.
3242 unsigned Idx = CountTrailingZeros_32(NonZeros);
Dan Gohman475871a2008-07-27 21:46:04 +00003243 SDValue V2 = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003244 Op.getOperand(Idx));
Evan Chengf0df0312008-05-15 08:39:06 +00003245 return getShuffleVectorZeroOrUndef(V2, Idx, true,
3246 Subtarget->hasSSE2(), DAG);
Evan Cheng7e2ff772008-05-08 00:57:18 +00003247 }
Dan Gohman475871a2008-07-27 21:46:04 +00003248 return SDValue();
Evan Cheng7e2ff772008-05-08 00:57:18 +00003249 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003250
3251 // If element VT is < 32 bits, convert it to inserts into a zero vector.
Bill Wendling826f36f2007-03-28 00:57:11 +00003252 if (EVTBits == 8 && NumElems == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00003253 SDValue V = LowerBuildVectorv16i8(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003254 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003255 if (V.Val) return V;
3256 }
3257
Bill Wendling826f36f2007-03-28 00:57:11 +00003258 if (EVTBits == 16 && NumElems == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003259 SDValue V = LowerBuildVectorv8i16(Op, NonZeros,NumNonZero,NumZero, DAG,
Evan Cheng25ab6902006-09-08 06:48:29 +00003260 *this);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003261 if (V.Val) return V;
3262 }
3263
3264 // If element VT is == 32 bits, turn it into a number of shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003265 SmallVector<SDValue, 8> V;
Chris Lattner5a88b832007-02-25 07:10:00 +00003266 V.resize(NumElems);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003267 if (NumElems == 4 && NumZero > 0) {
3268 for (unsigned i = 0; i < 4; ++i) {
3269 bool isZero = !(NonZeros & (1 << i));
3270 if (isZero)
Evan Chengf0df0312008-05-15 08:39:06 +00003271 V[i] = getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003272 else
3273 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3274 }
3275
3276 for (unsigned i = 0; i < 2; ++i) {
3277 switch ((NonZeros & (0x3 << i*2)) >> (i*2)) {
3278 default: break;
3279 case 0:
3280 V[i] = V[i*2]; // Must be a zero vector.
3281 break;
3282 case 1:
3283 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2+1], V[i*2],
3284 getMOVLMask(NumElems, DAG));
3285 break;
3286 case 2:
3287 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3288 getMOVLMask(NumElems, DAG));
3289 break;
3290 case 3:
3291 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i*2], V[i*2+1],
3292 getUnpacklMask(NumElems, DAG));
3293 break;
3294 }
3295 }
3296
Duncan Sands83ec4b62008-06-06 12:08:01 +00003297 MVT MaskVT = MVT::getIntVectorWithNumElements(NumElems);
3298 MVT EVT = MaskVT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00003299 SmallVector<SDValue, 8> MaskVec;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003300 bool Reverse = (NonZeros & 0x3) == 2;
3301 for (unsigned i = 0; i < 2; ++i)
3302 if (Reverse)
3303 MaskVec.push_back(DAG.getConstant(1-i, EVT));
3304 else
3305 MaskVec.push_back(DAG.getConstant(i, EVT));
3306 Reverse = ((NonZeros & (0x3 << 2)) >> 2) == 2;
3307 for (unsigned i = 0; i < 2; ++i)
3308 if (Reverse)
3309 MaskVec.push_back(DAG.getConstant(1-i+NumElems, EVT));
3310 else
3311 MaskVec.push_back(DAG.getConstant(i+NumElems, EVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003312 SDValue ShufMask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00003313 &MaskVec[0], MaskVec.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003314 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[0], V[1], ShufMask);
3315 }
3316
3317 if (Values.size() > 2) {
3318 // Expand into a number of unpckl*.
3319 // e.g. for v4f32
3320 // Step 1: unpcklps 0, 2 ==> X: <?, ?, 2, 0>
3321 // : unpcklps 1, 3 ==> Y: <?, ?, 3, 1>
3322 // Step 2: unpcklps X, Y ==> <3, 2, 1, 0>
Dan Gohman475871a2008-07-27 21:46:04 +00003323 SDValue UnpckMask = getUnpacklMask(NumElems, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003324 for (unsigned i = 0; i < NumElems; ++i)
3325 V[i] = DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, Op.getOperand(i));
3326 NumElems >>= 1;
3327 while (NumElems != 0) {
3328 for (unsigned i = 0; i < NumElems; ++i)
3329 V[i] = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V[i], V[i + NumElems],
3330 UnpckMask);
3331 NumElems >>= 1;
3332 }
3333 return V[0];
3334 }
3335
Dan Gohman475871a2008-07-27 21:46:04 +00003336 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003337}
3338
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003339static
Dan Gohman475871a2008-07-27 21:46:04 +00003340SDValue LowerVECTOR_SHUFFLEv8i16(SDValue V1, SDValue V2,
3341 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003342 TargetLowering &TLI) {
Dan Gohman475871a2008-07-27 21:46:04 +00003343 SDValue NewV;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003344 MVT MaskVT = MVT::getIntVectorWithNumElements(8);
3345 MVT MaskEVT = MaskVT.getVectorElementType();
3346 MVT PtrVT = TLI.getPointerTy();
Dan Gohman475871a2008-07-27 21:46:04 +00003347 SmallVector<SDValue, 8> MaskElts(PermMask.Val->op_begin(),
Evan Cheng14b32e12007-12-11 01:46:18 +00003348 PermMask.Val->op_end());
3349
3350 // First record which half of which vector the low elements come from.
3351 SmallVector<unsigned, 4> LowQuad(4);
3352 for (unsigned i = 0; i < 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003353 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003354 if (Elt.getOpcode() == ISD::UNDEF)
3355 continue;
3356 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3357 int QuadIdx = EltIdx / 4;
3358 ++LowQuad[QuadIdx];
3359 }
3360 int BestLowQuad = -1;
3361 unsigned MaxQuad = 1;
3362 for (unsigned i = 0; i < 4; ++i) {
3363 if (LowQuad[i] > MaxQuad) {
3364 BestLowQuad = i;
3365 MaxQuad = LowQuad[i];
3366 }
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003367 }
3368
Evan Cheng14b32e12007-12-11 01:46:18 +00003369 // Record which half of which vector the high elements come from.
3370 SmallVector<unsigned, 4> HighQuad(4);
3371 for (unsigned i = 4; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003372 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003373 if (Elt.getOpcode() == ISD::UNDEF)
3374 continue;
3375 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3376 int QuadIdx = EltIdx / 4;
3377 ++HighQuad[QuadIdx];
3378 }
3379 int BestHighQuad = -1;
3380 MaxQuad = 1;
3381 for (unsigned i = 0; i < 4; ++i) {
3382 if (HighQuad[i] > MaxQuad) {
3383 BestHighQuad = i;
3384 MaxQuad = HighQuad[i];
3385 }
3386 }
3387
3388 // If it's possible to sort parts of either half with PSHUF{H|L}W, then do it.
3389 if (BestLowQuad != -1 || BestHighQuad != -1) {
3390 // First sort the 4 chunks in order using shufpd.
Dan Gohman475871a2008-07-27 21:46:04 +00003391 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003392 if (BestLowQuad != -1)
3393 MaskVec.push_back(DAG.getConstant(BestLowQuad, MVT::i32));
3394 else
3395 MaskVec.push_back(DAG.getConstant(0, MVT::i32));
3396 if (BestHighQuad != -1)
3397 MaskVec.push_back(DAG.getConstant(BestHighQuad, MVT::i32));
3398 else
3399 MaskVec.push_back(DAG.getConstant(1, MVT::i32));
Dan Gohman475871a2008-07-27 21:46:04 +00003400 SDValue Mask= DAG.getNode(ISD::BUILD_VECTOR, MVT::v2i32, &MaskVec[0],2);
Evan Cheng14b32e12007-12-11 01:46:18 +00003401 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v2i64,
3402 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V1),
3403 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, V2), Mask);
3404 NewV = DAG.getNode(ISD::BIT_CONVERT, MVT::v8i16, NewV);
3405
3406 // Now sort high and low parts separately.
3407 BitVector InOrder(8);
3408 if (BestLowQuad != -1) {
3409 // Sort lower half in order using PSHUFLW.
3410 MaskVec.clear();
3411 bool AnyOutOrder = false;
3412 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003413 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003414 if (Elt.getOpcode() == ISD::UNDEF) {
3415 MaskVec.push_back(Elt);
3416 InOrder.set(i);
3417 } else {
3418 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3419 if (EltIdx != i)
3420 AnyOutOrder = true;
3421 MaskVec.push_back(DAG.getConstant(EltIdx % 4, MaskEVT));
3422 // If this element is in the right place after this shuffle, then
3423 // remember it.
3424 if ((int)(EltIdx / 4) == BestLowQuad)
3425 InOrder.set(i);
3426 }
3427 }
3428 if (AnyOutOrder) {
3429 for (unsigned i = 4; i != 8; ++i)
3430 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
Dan Gohman475871a2008-07-27 21:46:04 +00003431 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003432 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3433 }
3434 }
3435
3436 if (BestHighQuad != -1) {
3437 // Sort high half in order using PSHUFHW if possible.
3438 MaskVec.clear();
3439 for (unsigned i = 0; i != 4; ++i)
3440 MaskVec.push_back(DAG.getConstant(i, MaskEVT));
3441 bool AnyOutOrder = false;
3442 for (unsigned i = 4; i != 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003443 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003444 if (Elt.getOpcode() == ISD::UNDEF) {
3445 MaskVec.push_back(Elt);
3446 InOrder.set(i);
3447 } else {
3448 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3449 if (EltIdx != i)
3450 AnyOutOrder = true;
3451 MaskVec.push_back(DAG.getConstant((EltIdx % 4) + 4, MaskEVT));
3452 // If this element is in the right place after this shuffle, then
3453 // remember it.
3454 if ((int)(EltIdx / 4) == BestHighQuad)
3455 InOrder.set(i);
3456 }
3457 }
3458 if (AnyOutOrder) {
Dan Gohman475871a2008-07-27 21:46:04 +00003459 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003460 NewV = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, NewV, NewV, Mask);
3461 }
3462 }
3463
3464 // The other elements are put in the right place using pextrw and pinsrw.
3465 for (unsigned i = 0; i != 8; ++i) {
3466 if (InOrder[i])
3467 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003468 SDValue Elt = MaskElts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003469 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003470 SDValue ExtOp = (EltIdx < 8)
Evan Cheng14b32e12007-12-11 01:46:18 +00003471 ? DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
3472 DAG.getConstant(EltIdx, PtrVT))
3473 : DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
3474 DAG.getConstant(EltIdx - 8, PtrVT));
3475 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3476 DAG.getConstant(i, PtrVT));
3477 }
3478 return NewV;
3479 }
3480
3481 // PSHUF{H|L}W are not used. Lower into extracts and inserts but try to use
3482 ///as few as possible.
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003483 // First, let's find out how many elements are already in the right order.
3484 unsigned V1InOrder = 0;
3485 unsigned V1FromV1 = 0;
3486 unsigned V2InOrder = 0;
3487 unsigned V2FromV2 = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003488 SmallVector<SDValue, 8> V1Elts;
3489 SmallVector<SDValue, 8> V2Elts;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003490 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003491 SDValue Elt = MaskElts[i];
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003492 if (Elt.getOpcode() == ISD::UNDEF) {
Evan Cheng14b32e12007-12-11 01:46:18 +00003493 V1Elts.push_back(Elt);
3494 V2Elts.push_back(Elt);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003495 ++V1InOrder;
3496 ++V2InOrder;
Evan Cheng14b32e12007-12-11 01:46:18 +00003497 continue;
3498 }
3499 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3500 if (EltIdx == i) {
3501 V1Elts.push_back(Elt);
3502 V2Elts.push_back(DAG.getConstant(i+8, MaskEVT));
3503 ++V1InOrder;
3504 } else if (EltIdx == i+8) {
3505 V1Elts.push_back(Elt);
3506 V2Elts.push_back(DAG.getConstant(i, MaskEVT));
3507 ++V2InOrder;
3508 } else if (EltIdx < 8) {
3509 V1Elts.push_back(Elt);
3510 ++V1FromV1;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003511 } else {
Evan Cheng14b32e12007-12-11 01:46:18 +00003512 V2Elts.push_back(DAG.getConstant(EltIdx-8, MaskEVT));
3513 ++V2FromV2;
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003514 }
3515 }
3516
3517 if (V2InOrder > V1InOrder) {
3518 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3519 std::swap(V1, V2);
3520 std::swap(V1Elts, V2Elts);
3521 std::swap(V1FromV1, V2FromV2);
3522 }
3523
Evan Cheng14b32e12007-12-11 01:46:18 +00003524 if ((V1FromV1 + V1InOrder) != 8) {
3525 // Some elements are from V2.
3526 if (V1FromV1) {
3527 // If there are elements that are from V1 but out of place,
3528 // then first sort them in place
Dan Gohman475871a2008-07-27 21:46:04 +00003529 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003530 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003531 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003532 if (Elt.getOpcode() == ISD::UNDEF) {
3533 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3534 continue;
3535 }
3536 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3537 if (EltIdx >= 8)
3538 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3539 else
3540 MaskVec.push_back(DAG.getConstant(EltIdx, MaskEVT));
3541 }
Dan Gohman475871a2008-07-27 21:46:04 +00003542 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &MaskVec[0], 8);
Evan Cheng14b32e12007-12-11 01:46:18 +00003543 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, MVT::v8i16, V1, V1, Mask);
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003544 }
Evan Cheng14b32e12007-12-11 01:46:18 +00003545
3546 NewV = V1;
3547 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003548 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003549 if (Elt.getOpcode() == ISD::UNDEF)
3550 continue;
3551 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3552 if (EltIdx < 8)
3553 continue;
Dan Gohman475871a2008-07-27 21:46:04 +00003554 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V2,
Evan Cheng14b32e12007-12-11 01:46:18 +00003555 DAG.getConstant(EltIdx - 8, PtrVT));
3556 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3557 DAG.getConstant(i, PtrVT));
3558 }
3559 return NewV;
3560 } else {
3561 // All elements are from V1.
3562 NewV = V1;
3563 for (unsigned i = 0; i < 8; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003564 SDValue Elt = V1Elts[i];
Evan Cheng14b32e12007-12-11 01:46:18 +00003565 if (Elt.getOpcode() == ISD::UNDEF)
3566 continue;
3567 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00003568 SDValue ExtOp = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i16, V1,
Evan Cheng14b32e12007-12-11 01:46:18 +00003569 DAG.getConstant(EltIdx, PtrVT));
3570 NewV = DAG.getNode(ISD::INSERT_VECTOR_ELT, MVT::v8i16, NewV, ExtOp,
3571 DAG.getConstant(i, PtrVT));
3572 }
3573 return NewV;
3574 }
3575}
3576
Evan Cheng7a831ce2007-12-15 03:00:47 +00003577/// RewriteAsNarrowerShuffle - Try rewriting v8i16 and v16i8 shuffles as 4 wide
3578/// ones, or rewriting v4i32 / v2f32 as 2 wide ones if possible. This can be
3579/// done when every pair / quad of shuffle mask elements point to elements in
3580/// the right sequence. e.g.
Evan Cheng14b32e12007-12-11 01:46:18 +00003581/// vector_shuffle <>, <>, < 3, 4, | 10, 11, | 0, 1, | 14, 15>
3582static
Dan Gohman475871a2008-07-27 21:46:04 +00003583SDValue RewriteAsNarrowerShuffle(SDValue V1, SDValue V2,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003584 MVT VT,
Dan Gohman475871a2008-07-27 21:46:04 +00003585 SDValue PermMask, SelectionDAG &DAG,
Evan Cheng14b32e12007-12-11 01:46:18 +00003586 TargetLowering &TLI) {
3587 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng7a831ce2007-12-15 03:00:47 +00003588 unsigned NewWidth = (NumElems == 4) ? 2 : 4;
Duncan Sands83ec4b62008-06-06 12:08:01 +00003589 MVT MaskVT = MVT::getIntVectorWithNumElements(NewWidth);
Duncan Sandsd038e042008-07-21 10:20:31 +00003590 MVT MaskEltVT = MaskVT.getVectorElementType();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003591 MVT NewVT = MaskVT;
3592 switch (VT.getSimpleVT()) {
3593 default: assert(false && "Unexpected!");
Evan Cheng7a831ce2007-12-15 03:00:47 +00003594 case MVT::v4f32: NewVT = MVT::v2f64; break;
3595 case MVT::v4i32: NewVT = MVT::v2i64; break;
3596 case MVT::v8i16: NewVT = MVT::v4i32; break;
3597 case MVT::v16i8: NewVT = MVT::v4i32; break;
Evan Cheng7a831ce2007-12-15 03:00:47 +00003598 }
3599
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003600 if (NewWidth == 2) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00003601 if (VT.isInteger())
Evan Cheng7a831ce2007-12-15 03:00:47 +00003602 NewVT = MVT::v2i64;
3603 else
3604 NewVT = MVT::v2f64;
Anton Korobeynikov7c1c2612008-02-20 11:22:39 +00003605 }
Evan Cheng7a831ce2007-12-15 03:00:47 +00003606 unsigned Scale = NumElems / NewWidth;
Dan Gohman475871a2008-07-27 21:46:04 +00003607 SmallVector<SDValue, 8> MaskVec;
Evan Cheng14b32e12007-12-11 01:46:18 +00003608 for (unsigned i = 0; i < NumElems; i += Scale) {
3609 unsigned StartIdx = ~0U;
3610 for (unsigned j = 0; j < Scale; ++j) {
Dan Gohman475871a2008-07-27 21:46:04 +00003611 SDValue Elt = PermMask.getOperand(i+j);
Evan Cheng14b32e12007-12-11 01:46:18 +00003612 if (Elt.getOpcode() == ISD::UNDEF)
3613 continue;
3614 unsigned EltIdx = cast<ConstantSDNode>(Elt)->getValue();
3615 if (StartIdx == ~0U)
3616 StartIdx = EltIdx - (EltIdx % Scale);
3617 if (EltIdx != StartIdx + j)
Dan Gohman475871a2008-07-27 21:46:04 +00003618 return SDValue();
Evan Cheng14b32e12007-12-11 01:46:18 +00003619 }
3620 if (StartIdx == ~0U)
Duncan Sandsd038e042008-07-21 10:20:31 +00003621 MaskVec.push_back(DAG.getNode(ISD::UNDEF, MaskEltVT));
Evan Cheng14b32e12007-12-11 01:46:18 +00003622 else
Duncan Sandsd038e042008-07-21 10:20:31 +00003623 MaskVec.push_back(DAG.getConstant(StartIdx / Scale, MaskEltVT));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003624 }
3625
Evan Cheng7a831ce2007-12-15 03:00:47 +00003626 V1 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V1);
3627 V2 = DAG.getNode(ISD::BIT_CONVERT, NewVT, V2);
3628 return DAG.getNode(ISD::VECTOR_SHUFFLE, NewVT, V1, V2,
3629 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3630 &MaskVec[0], MaskVec.size()));
Evan Cheng8a86c3f2007-12-07 08:07:39 +00003631}
3632
Evan Chengd880b972008-05-09 21:53:03 +00003633/// getVZextMovL - Return a zero-extending vector move low node.
Evan Cheng7e2ff772008-05-08 00:57:18 +00003634///
Dan Gohman475871a2008-07-27 21:46:04 +00003635static SDValue getVZextMovL(MVT VT, MVT OpVT,
3636 SDValue SrcOp, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00003637 const X86Subtarget *Subtarget) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00003638 if (VT == MVT::v2f64 || VT == MVT::v4f32) {
3639 LoadSDNode *LD = NULL;
3640 if (!isScalarLoadToVector(SrcOp.Val, &LD))
3641 LD = dyn_cast<LoadSDNode>(SrcOp);
3642 if (!LD) {
3643 // movssrr and movsdrr do not clear top bits. Try to use movd, movq
3644 // instead.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003645 MVT EVT = (OpVT == MVT::v2f64) ? MVT::i64 : MVT::i32;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003646 if ((EVT != MVT::i64 || Subtarget->is64Bit()) &&
3647 SrcOp.getOpcode() == ISD::SCALAR_TO_VECTOR &&
3648 SrcOp.getOperand(0).getOpcode() == ISD::BIT_CONVERT &&
3649 SrcOp.getOperand(0).getOperand(0).getValueType() == EVT) {
3650 // PR2108
3651 OpVT = (OpVT == MVT::v2f64) ? MVT::v2i64 : MVT::v4i32;
3652 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003653 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003654 DAG.getNode(ISD::SCALAR_TO_VECTOR, OpVT,
3655 SrcOp.getOperand(0).getOperand(0))));
3656 }
3657 }
3658 }
3659
3660 return DAG.getNode(ISD::BIT_CONVERT, VT,
Evan Chengd880b972008-05-09 21:53:03 +00003661 DAG.getNode(X86ISD::VZEXT_MOVL, OpVT,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003662 DAG.getNode(ISD::BIT_CONVERT, OpVT, SrcOp)));
3663}
3664
Evan Chengace3c172008-07-22 21:13:36 +00003665/// LowerVECTOR_SHUFFLE_4wide - Handle all 4 wide cases with a number of
3666/// shuffles.
Dan Gohman475871a2008-07-27 21:46:04 +00003667static SDValue
3668LowerVECTOR_SHUFFLE_4wide(SDValue V1, SDValue V2,
3669 SDValue PermMask, MVT VT, SelectionDAG &DAG) {
Evan Chengace3c172008-07-22 21:13:36 +00003670 MVT MaskVT = PermMask.getValueType();
3671 MVT MaskEVT = MaskVT.getVectorElementType();
3672 SmallVector<std::pair<int, int>, 8> Locs;
3673 Locs.reserve(4);
Dan Gohman475871a2008-07-27 21:46:04 +00003674 SmallVector<SDValue, 8> Mask1(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003675 unsigned NumHi = 0;
3676 unsigned NumLo = 0;
Evan Chengace3c172008-07-22 21:13:36 +00003677 for (unsigned i = 0; i != 4; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00003678 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003679 if (Elt.getOpcode() == ISD::UNDEF) {
3680 Locs[i] = std::make_pair(-1, -1);
3681 } else {
3682 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
Dan Gohmand0859942008-08-04 23:09:15 +00003683 assert(Val < 8 && "Invalid VECTOR_SHUFFLE index!");
Evan Chengace3c172008-07-22 21:13:36 +00003684 if (Val < 4) {
3685 Locs[i] = std::make_pair(0, NumLo);
3686 Mask1[NumLo] = Elt;
3687 NumLo++;
3688 } else {
3689 Locs[i] = std::make_pair(1, NumHi);
3690 if (2+NumHi < 4)
3691 Mask1[2+NumHi] = Elt;
3692 NumHi++;
3693 }
3694 }
3695 }
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003696
Evan Chengace3c172008-07-22 21:13:36 +00003697 if (NumLo <= 2 && NumHi <= 2) {
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003698 // If no more than two elements come from either vector. This can be
3699 // implemented with two shuffles. First shuffle gather the elements.
3700 // The second shuffle, which takes the first shuffle as both of its
3701 // vector operands, put the elements into the right order.
Evan Chengace3c172008-07-22 21:13:36 +00003702 V1 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3703 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3704 &Mask1[0], Mask1.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003705
Dan Gohman475871a2008-07-27 21:46:04 +00003706 SmallVector<SDValue, 8> Mask2(4, DAG.getNode(ISD::UNDEF, MaskEVT));
Evan Chengace3c172008-07-22 21:13:36 +00003707 for (unsigned i = 0; i != 4; ++i) {
3708 if (Locs[i].first == -1)
3709 continue;
3710 else {
3711 unsigned Idx = (i < 2) ? 0 : 4;
3712 Idx += Locs[i].first * 2 + Locs[i].second;
3713 Mask2[i] = DAG.getConstant(Idx, MaskEVT);
3714 }
3715 }
3716
3717 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V1,
3718 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3719 &Mask2[0], Mask2.size()));
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003720 } else if (NumLo == 3 || NumHi == 3) {
3721 // Otherwise, we must have three elements from one vector, call it X, and
3722 // one element from the other, call it Y. First, use a shufps to build an
3723 // intermediate vector with the one element from Y and the element from X
3724 // that will be in the same half in the final destination (the indexes don't
3725 // matter). Then, use a shufps to build the final vector, taking the half
3726 // containing the element from Y from the intermediate, and the other half
3727 // from X.
3728 if (NumHi == 3) {
3729 // Normalize it so the 3 elements come from V1.
3730 PermMask = CommuteVectorShuffleMask(PermMask, DAG);
3731 std::swap(V1, V2);
3732 }
3733
3734 // Find the element from V2.
3735 unsigned HiIndex;
3736 for (HiIndex = 0; HiIndex < 3; ++HiIndex) {
Dan Gohman475871a2008-07-27 21:46:04 +00003737 SDValue Elt = PermMask.getOperand(HiIndex);
Evan Cheng5e6ebaf2008-07-23 00:22:17 +00003738 if (Elt.getOpcode() == ISD::UNDEF)
3739 continue;
3740 unsigned Val = cast<ConstantSDNode>(Elt)->getValue();
3741 if (Val >= 4)
3742 break;
3743 }
3744
3745 Mask1[0] = PermMask.getOperand(HiIndex);
3746 Mask1[1] = DAG.getNode(ISD::UNDEF, MaskEVT);
3747 Mask1[2] = PermMask.getOperand(HiIndex^1);
3748 Mask1[3] = DAG.getNode(ISD::UNDEF, MaskEVT);
3749 V2 = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3750 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3751
3752 if (HiIndex >= 2) {
3753 Mask1[0] = PermMask.getOperand(0);
3754 Mask1[1] = PermMask.getOperand(1);
3755 Mask1[2] = DAG.getConstant(HiIndex & 1 ? 6 : 4, MaskEVT);
3756 Mask1[3] = DAG.getConstant(HiIndex & 1 ? 4 : 6, MaskEVT);
3757 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
3758 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3759 } else {
3760 Mask1[0] = DAG.getConstant(HiIndex & 1 ? 2 : 0, MaskEVT);
3761 Mask1[1] = DAG.getConstant(HiIndex & 1 ? 0 : 2, MaskEVT);
3762 Mask1[2] = PermMask.getOperand(2);
3763 Mask1[3] = PermMask.getOperand(3);
3764 if (Mask1[2].getOpcode() != ISD::UNDEF)
3765 Mask1[2] = DAG.getConstant(cast<ConstantSDNode>(Mask1[2])->getValue()+4,
3766 MaskEVT);
3767 if (Mask1[3].getOpcode() != ISD::UNDEF)
3768 Mask1[3] = DAG.getConstant(cast<ConstantSDNode>(Mask1[3])->getValue()+4,
3769 MaskEVT);
3770 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V2, V1,
3771 DAG.getNode(ISD::BUILD_VECTOR, MaskVT, &Mask1[0], 4));
3772 }
Evan Chengace3c172008-07-22 21:13:36 +00003773 }
3774
3775 // Break it into (shuffle shuffle_hi, shuffle_lo).
3776 Locs.clear();
Dan Gohman475871a2008-07-27 21:46:04 +00003777 SmallVector<SDValue,8> LoMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3778 SmallVector<SDValue,8> HiMask(4, DAG.getNode(ISD::UNDEF, MaskEVT));
3779 SmallVector<SDValue,8> *MaskPtr = &LoMask;
Evan Chengace3c172008-07-22 21:13:36 +00003780 unsigned MaskIdx = 0;
3781 unsigned LoIdx = 0;
3782 unsigned HiIdx = 2;
3783 for (unsigned i = 0; i != 4; ++i) {
3784 if (i == 2) {
3785 MaskPtr = &HiMask;
3786 MaskIdx = 1;
3787 LoIdx = 0;
3788 HiIdx = 2;
3789 }
Dan Gohman475871a2008-07-27 21:46:04 +00003790 SDValue Elt = PermMask.getOperand(i);
Evan Chengace3c172008-07-22 21:13:36 +00003791 if (Elt.getOpcode() == ISD::UNDEF) {
3792 Locs[i] = std::make_pair(-1, -1);
3793 } else if (cast<ConstantSDNode>(Elt)->getValue() < 4) {
3794 Locs[i] = std::make_pair(MaskIdx, LoIdx);
3795 (*MaskPtr)[LoIdx] = Elt;
3796 LoIdx++;
3797 } else {
3798 Locs[i] = std::make_pair(MaskIdx, HiIdx);
3799 (*MaskPtr)[HiIdx] = Elt;
3800 HiIdx++;
3801 }
3802 }
3803
Dan Gohman475871a2008-07-27 21:46:04 +00003804 SDValue LoShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003805 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3806 &LoMask[0], LoMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003807 SDValue HiShuffle = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2,
Evan Chengace3c172008-07-22 21:13:36 +00003808 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3809 &HiMask[0], HiMask.size()));
Dan Gohman475871a2008-07-27 21:46:04 +00003810 SmallVector<SDValue, 8> MaskOps;
Evan Chengace3c172008-07-22 21:13:36 +00003811 for (unsigned i = 0; i != 4; ++i) {
3812 if (Locs[i].first == -1) {
3813 MaskOps.push_back(DAG.getNode(ISD::UNDEF, MaskEVT));
3814 } else {
3815 unsigned Idx = Locs[i].first * 4 + Locs[i].second;
3816 MaskOps.push_back(DAG.getConstant(Idx, MaskEVT));
3817 }
3818 }
3819 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, LoShuffle, HiShuffle,
3820 DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
3821 &MaskOps[0], MaskOps.size()));
3822}
3823
Dan Gohman475871a2008-07-27 21:46:04 +00003824SDValue
3825X86TargetLowering::LowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) {
3826 SDValue V1 = Op.getOperand(0);
3827 SDValue V2 = Op.getOperand(1);
3828 SDValue PermMask = Op.getOperand(2);
Duncan Sands83ec4b62008-06-06 12:08:01 +00003829 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00003830 unsigned NumElems = PermMask.getNumOperands();
Duncan Sands83ec4b62008-06-06 12:08:01 +00003831 bool isMMX = VT.getSizeInBits() == 64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003832 bool V1IsUndef = V1.getOpcode() == ISD::UNDEF;
3833 bool V2IsUndef = V2.getOpcode() == ISD::UNDEF;
Evan Chengd9b8e402006-10-16 06:36:00 +00003834 bool V1IsSplat = false;
3835 bool V2IsSplat = false;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003836
Evan Cheng8cf723d2006-09-08 01:50:06 +00003837 if (isUndefShuffle(Op.Val))
3838 return DAG.getNode(ISD::UNDEF, VT);
3839
Evan Cheng213d2cf2007-05-17 18:45:50 +00003840 if (isZeroShuffle(Op.Val))
Evan Chengf0df0312008-05-15 08:39:06 +00003841 return getZeroVector(VT, Subtarget->hasSSE2(), DAG);
Evan Cheng213d2cf2007-05-17 18:45:50 +00003842
Evan Cheng49892af2007-06-19 00:02:56 +00003843 if (isIdentityMask(PermMask.Val))
3844 return V1;
3845 else if (isIdentityMask(PermMask.Val, true))
3846 return V2;
3847
Evan Cheng0db9fe62006-04-25 20:13:52 +00003848 if (isSplatMask(PermMask.Val)) {
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003849 if (isMMX || NumElems < 4) return Op;
3850 // Promote it to a v4{if}32 splat.
3851 return PromoteSplat(Op, DAG, Subtarget->hasSSE2());
Evan Cheng0db9fe62006-04-25 20:13:52 +00003852 }
3853
Evan Cheng7a831ce2007-12-15 03:00:47 +00003854 // If the shuffle can be profitably rewritten as a narrower shuffle, then
3855 // do it!
3856 if (VT == MVT::v8i16 || VT == MVT::v16i8) {
Dan Gohman475871a2008-07-27 21:46:04 +00003857 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask, DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003858 if (NewOp.Val)
3859 return DAG.getNode(ISD::BIT_CONVERT, VT, LowerVECTOR_SHUFFLE(NewOp, DAG));
3860 } else if ((VT == MVT::v4i32 || (VT == MVT::v4f32 && Subtarget->hasSSE2()))) {
3861 // FIXME: Figure out a cleaner way to do this.
3862 // Try to make use of movq to zero out the top part.
3863 if (ISD::isBuildVectorAllZeros(V2.Val)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003864 SDValue NewOp = RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003865 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003866 if (NewOp.Val) {
Dan Gohman475871a2008-07-27 21:46:04 +00003867 SDValue NewV1 = NewOp.getOperand(0);
3868 SDValue NewV2 = NewOp.getOperand(1);
3869 SDValue NewMask = NewOp.getOperand(2);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003870 if (isCommutedMOVL(NewMask.Val, true, false)) {
3871 NewOp = CommuteVectorShuffle(NewOp, NewV1, NewV2, NewMask, DAG);
Evan Chengd880b972008-05-09 21:53:03 +00003872 return getVZextMovL(VT, NewOp.getValueType(), NewV2, DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003873 }
3874 }
3875 } else if (ISD::isBuildVectorAllZeros(V1.Val)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003876 SDValue NewOp= RewriteAsNarrowerShuffle(V1, V2, VT, PermMask,
Evan Cheng7e2ff772008-05-08 00:57:18 +00003877 DAG, *this);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003878 if (NewOp.Val && X86::isMOVLMask(NewOp.getOperand(2).Val))
Evan Chengd880b972008-05-09 21:53:03 +00003879 return getVZextMovL(VT, NewOp.getValueType(), NewOp.getOperand(1),
Evan Cheng7e2ff772008-05-08 00:57:18 +00003880 DAG, Subtarget);
Evan Cheng7a831ce2007-12-15 03:00:47 +00003881 }
3882 }
3883
Evan Chengf26ffe92008-05-29 08:22:04 +00003884 // Check if this can be converted into a logical shift.
3885 bool isLeft = false;
3886 unsigned ShAmt = 0;
Dan Gohman475871a2008-07-27 21:46:04 +00003887 SDValue ShVal;
Evan Chengf26ffe92008-05-29 08:22:04 +00003888 bool isShift = isVectorShift(Op, PermMask, DAG, isLeft, ShVal, ShAmt);
3889 if (isShift && ShVal.hasOneUse()) {
3890 // If the shifted value has multiple uses, it may be cheaper to use
3891 // v_set0 + movlhps or movhlps, etc.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003892 MVT EVT = VT.getVectorElementType();
3893 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003894 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3895 }
3896
Evan Cheng7e2ff772008-05-08 00:57:18 +00003897 if (X86::isMOVLMask(PermMask.Val)) {
3898 if (V1IsUndef)
3899 return V2;
3900 if (ISD::isBuildVectorAllZeros(V1.Val))
Evan Chengd880b972008-05-09 21:53:03 +00003901 return getVZextMovL(VT, VT, V2, DAG, Subtarget);
Nate Begemanfb8ead02008-07-25 19:05:58 +00003902 if (!isMMX)
3903 return Op;
Evan Cheng7e2ff772008-05-08 00:57:18 +00003904 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003905
Nate Begemanfb8ead02008-07-25 19:05:58 +00003906 if (!isMMX && (X86::isMOVSHDUPMask(PermMask.Val) ||
3907 X86::isMOVSLDUPMask(PermMask.Val) ||
3908 X86::isMOVHLPSMask(PermMask.Val) ||
3909 X86::isMOVHPMask(PermMask.Val) ||
3910 X86::isMOVLPMask(PermMask.Val)))
Evan Cheng9bbbb982006-10-25 20:48:19 +00003911 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00003912
Evan Cheng9bbbb982006-10-25 20:48:19 +00003913 if (ShouldXformToMOVHLPS(PermMask.Val) ||
3914 ShouldXformToMOVLP(V1.Val, V2.Val, PermMask.Val))
Evan Cheng9eca5e82006-10-25 21:49:50 +00003915 return CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003916
Evan Chengf26ffe92008-05-29 08:22:04 +00003917 if (isShift) {
3918 // No better options. Use a vshl / vsrl.
Duncan Sands83ec4b62008-06-06 12:08:01 +00003919 MVT EVT = VT.getVectorElementType();
3920 ShAmt *= EVT.getSizeInBits();
Evan Chengf26ffe92008-05-29 08:22:04 +00003921 return getVShift(isLeft, VT, ShVal, ShAmt, DAG, *this);
3922 }
3923
Evan Cheng9eca5e82006-10-25 21:49:50 +00003924 bool Commuted = false;
Chris Lattner8a594482007-11-25 00:24:49 +00003925 // FIXME: This should also accept a bitcast of a splat? Be careful, not
3926 // 1,1,1,1 -> v8i16 though.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003927 V1IsSplat = isSplatVector(V1.Val);
3928 V2IsSplat = isSplatVector(V2.Val);
Chris Lattner8a594482007-11-25 00:24:49 +00003929
3930 // Canonicalize the splat or undef, if present, to be on the RHS.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003931 if ((V1IsSplat || V1IsUndef) && !(V2IsSplat || V2IsUndef)) {
Evan Cheng9eca5e82006-10-25 21:49:50 +00003932 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003933 std::swap(V1IsSplat, V2IsSplat);
3934 std::swap(V1IsUndef, V2IsUndef);
Evan Cheng9eca5e82006-10-25 21:49:50 +00003935 Commuted = true;
Evan Cheng9bbbb982006-10-25 20:48:19 +00003936 }
3937
Evan Cheng7a831ce2007-12-15 03:00:47 +00003938 // FIXME: Figure out a cleaner way to do this.
Evan Cheng9bbbb982006-10-25 20:48:19 +00003939 if (isCommutedMOVL(PermMask.Val, V2IsSplat, V2IsUndef)) {
3940 if (V2IsUndef) return V1;
Evan Cheng9eca5e82006-10-25 21:49:50 +00003941 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003942 if (V2IsSplat) {
3943 // V2 is a splat, so the mask may be malformed. That is, it may point
3944 // to any V2 element. The instruction selectior won't like this. Get
3945 // a corrected mask and commute to form a proper MOVS{S|D}.
Dan Gohman475871a2008-07-27 21:46:04 +00003946 SDValue NewMask = getMOVLMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003947 if (NewMask.Val != PermMask.Val)
3948 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003949 }
Evan Cheng9bbbb982006-10-25 20:48:19 +00003950 return Op;
Evan Chengd9b8e402006-10-16 06:36:00 +00003951 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003952
Evan Chengd9b8e402006-10-16 06:36:00 +00003953 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003954 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Chengd9b8e402006-10-16 06:36:00 +00003955 X86::isUNPCKLMask(PermMask.Val) ||
3956 X86::isUNPCKHMask(PermMask.Val))
3957 return Op;
Evan Chenge1113032006-10-04 18:33:38 +00003958
Evan Cheng9bbbb982006-10-25 20:48:19 +00003959 if (V2IsSplat) {
3960 // Normalize mask so all entries that point to V2 points to its first
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00003961 // element then try to match unpck{h|l} again. If match, return a
Evan Cheng9bbbb982006-10-25 20:48:19 +00003962 // new vector_shuffle with the corrected mask.
Dan Gohman475871a2008-07-27 21:46:04 +00003963 SDValue NewMask = NormalizeMask(PermMask, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003964 if (NewMask.Val != PermMask.Val) {
3965 if (X86::isUNPCKLMask(PermMask.Val, true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003966 SDValue NewMask = getUnpacklMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003967 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
3968 } else if (X86::isUNPCKHMask(PermMask.Val, true)) {
Dan Gohman475871a2008-07-27 21:46:04 +00003969 SDValue NewMask = getUnpackhMask(NumElems, DAG);
Evan Cheng9bbbb982006-10-25 20:48:19 +00003970 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1, V2, NewMask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00003971 }
3972 }
3973 }
3974
3975 // Normalize the node to match x86 shuffle ops if needed
Evan Cheng9eca5e82006-10-25 21:49:50 +00003976 if (V2.getOpcode() != ISD::UNDEF && isCommutedSHUFP(PermMask.Val))
3977 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3978
3979 if (Commuted) {
3980 // Commute is back and try unpck* again.
3981 Op = CommuteVectorShuffle(Op, V1, V2, PermMask, DAG);
3982 if (X86::isUNPCKL_v_undef_Mask(PermMask.Val) ||
Bill Wendling2f9bb1a2007-04-24 21:16:55 +00003983 X86::isUNPCKH_v_undef_Mask(PermMask.Val) ||
Evan Cheng9eca5e82006-10-25 21:49:50 +00003984 X86::isUNPCKLMask(PermMask.Val) ||
3985 X86::isUNPCKHMask(PermMask.Val))
3986 return Op;
3987 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00003988
Evan Cheng0c0f83f2008-04-05 00:30:36 +00003989 // Try PSHUF* first, then SHUFP*.
3990 // MMX doesn't have PSHUFD but it does have PSHUFW. While it's theoretically
3991 // possible to shuffle a v2i32 using PSHUFW, that's not yet implemented.
3992 if (isMMX && NumElems == 4 && X86::isPSHUFDMask(PermMask.Val)) {
3993 if (V2.getOpcode() != ISD::UNDEF)
3994 return DAG.getNode(ISD::VECTOR_SHUFFLE, VT, V1,
3995 DAG.getNode(ISD::UNDEF, VT), PermMask);
3996 return Op;
3997 }
3998
3999 if (!isMMX) {
4000 if (Subtarget->hasSSE2() &&
4001 (X86::isPSHUFDMask(PermMask.Val) ||
4002 X86::isPSHUFHWMask(PermMask.Val) ||
4003 X86::isPSHUFLWMask(PermMask.Val))) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004004 MVT RVT = VT;
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004005 if (VT == MVT::v4f32) {
4006 RVT = MVT::v4i32;
4007 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT,
4008 DAG.getNode(ISD::BIT_CONVERT, RVT, V1),
4009 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4010 } else if (V2.getOpcode() != ISD::UNDEF)
4011 Op = DAG.getNode(ISD::VECTOR_SHUFFLE, RVT, V1,
4012 DAG.getNode(ISD::UNDEF, RVT), PermMask);
4013 if (RVT != VT)
4014 Op = DAG.getNode(ISD::BIT_CONVERT, VT, Op);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004015 return Op;
4016 }
4017
Evan Cheng0c0f83f2008-04-05 00:30:36 +00004018 // Binary or unary shufps.
4019 if (X86::isSHUFPMask(PermMask.Val) ||
4020 (V2.getOpcode() == ISD::UNDEF && X86::isPSHUFDMask(PermMask.Val)))
Evan Cheng0db9fe62006-04-25 20:13:52 +00004021 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004022 }
4023
Evan Cheng14b32e12007-12-11 01:46:18 +00004024 // Handle v8i16 specifically since SSE can do byte extraction and insertion.
4025 if (VT == MVT::v8i16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004026 SDValue NewOp = LowerVECTOR_SHUFFLEv8i16(V1, V2, PermMask, DAG, *this);
Evan Cheng14b32e12007-12-11 01:46:18 +00004027 if (NewOp.Val)
4028 return NewOp;
4029 }
4030
Evan Chengace3c172008-07-22 21:13:36 +00004031 // Handle all 4 wide cases with a number of shuffles except for MMX.
4032 if (NumElems == 4 && !isMMX)
4033 return LowerVECTOR_SHUFFLE_4wide(V1, V2, PermMask, VT, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004034
Dan Gohman475871a2008-07-27 21:46:04 +00004035 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004036}
4037
Dan Gohman475871a2008-07-27 21:46:04 +00004038SDValue
4039X86TargetLowering::LowerEXTRACT_VECTOR_ELT_SSE4(SDValue Op,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004040 SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004041 MVT VT = Op.getValueType();
4042 if (VT.getSizeInBits() == 8) {
Dan Gohman475871a2008-07-27 21:46:04 +00004043 SDValue Extract = DAG.getNode(X86ISD::PEXTRB, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004044 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004045 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004046 DAG.getValueType(VT));
4047 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004048 } else if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004049 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, MVT::i32,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004050 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004051 SDValue Assert = DAG.getNode(ISD::AssertZext, MVT::i32, Extract,
Nate Begeman14d12ca2008-02-11 04:19:36 +00004052 DAG.getValueType(VT));
4053 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Evan Cheng62a3f152008-03-24 21:52:23 +00004054 } else if (VT == MVT::f32) {
4055 // EXTRACTPS outputs to a GPR32 register which will require a movd to copy
4056 // the result back to FR32 register. It's only worth matching if the
Dan Gohman171c11e2008-04-16 02:32:24 +00004057 // result has a single use which is a store or a bitcast to i32.
Evan Cheng62a3f152008-03-24 21:52:23 +00004058 if (!Op.hasOneUse())
Dan Gohman475871a2008-07-27 21:46:04 +00004059 return SDValue();
Dan Gohman89684502008-07-27 20:43:25 +00004060 SDNode *User = *Op.Val->use_begin();
Dan Gohman171c11e2008-04-16 02:32:24 +00004061 if (User->getOpcode() != ISD::STORE &&
4062 (User->getOpcode() != ISD::BIT_CONVERT ||
4063 User->getValueType(0) != MVT::i32))
Dan Gohman475871a2008-07-27 21:46:04 +00004064 return SDValue();
4065 SDValue Extract = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
Evan Cheng62a3f152008-03-24 21:52:23 +00004066 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Op.getOperand(0)),
4067 Op.getOperand(1));
4068 return DAG.getNode(ISD::BIT_CONVERT, MVT::f32, Extract);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004069 }
Dan Gohman475871a2008-07-27 21:46:04 +00004070 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004071}
4072
4073
Dan Gohman475871a2008-07-27 21:46:04 +00004074SDValue
4075X86TargetLowering::LowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004076 if (!isa<ConstantSDNode>(Op.getOperand(1)))
Dan Gohman475871a2008-07-27 21:46:04 +00004077 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004078
Evan Cheng62a3f152008-03-24 21:52:23 +00004079 if (Subtarget->hasSSE41()) {
Dan Gohman475871a2008-07-27 21:46:04 +00004080 SDValue Res = LowerEXTRACT_VECTOR_ELT_SSE4(Op, DAG);
Evan Cheng62a3f152008-03-24 21:52:23 +00004081 if (Res.Val)
4082 return Res;
4083 }
Nate Begeman14d12ca2008-02-11 04:19:36 +00004084
Duncan Sands83ec4b62008-06-06 12:08:01 +00004085 MVT VT = Op.getValueType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004086 // TODO: handle v16i8.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004087 if (VT.getSizeInBits() == 16) {
Dan Gohman475871a2008-07-27 21:46:04 +00004088 SDValue Vec = Op.getOperand(0);
Evan Cheng14b32e12007-12-11 01:46:18 +00004089 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4090 if (Idx == 0)
4091 return DAG.getNode(ISD::TRUNCATE, MVT::i16,
4092 DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::i32,
4093 DAG.getNode(ISD::BIT_CONVERT, MVT::v4i32, Vec),
4094 Op.getOperand(1)));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004095 // Transform it so it match pextrw which produces a 32-bit result.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004096 MVT EVT = (MVT::SimpleValueType)(VT.getSimpleVT()+1);
Dan Gohman475871a2008-07-27 21:46:04 +00004097 SDValue Extract = DAG.getNode(X86ISD::PEXTRW, EVT,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004098 Op.getOperand(0), Op.getOperand(1));
Dan Gohman475871a2008-07-27 21:46:04 +00004099 SDValue Assert = DAG.getNode(ISD::AssertZext, EVT, Extract,
Evan Cheng0db9fe62006-04-25 20:13:52 +00004100 DAG.getValueType(VT));
4101 return DAG.getNode(ISD::TRUNCATE, VT, Assert);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004102 } else if (VT.getSizeInBits() == 32) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004103 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4104 if (Idx == 0)
4105 return Op;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004106 // SHUFPS the element to the lowest double word, then movss.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004107 MVT MaskVT = MVT::getIntVectorWithNumElements(4);
Dan Gohman475871a2008-07-27 21:46:04 +00004108 SmallVector<SDValue, 8> IdxVec;
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004109 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004110 push_back(DAG.getConstant(Idx, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004111 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004112 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004113 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004114 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004115 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004116 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004117 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004118 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004119 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004120 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
Evan Cheng6e56e2c2006-11-07 22:14:24 +00004121 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004122 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004123 DAG.getIntPtrConstant(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004124 } else if (VT.getSizeInBits() == 64) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004125 // FIXME: .td only matches this for <2 x f64>, not <2 x i64> on 32b
4126 // FIXME: seems like this should be unnecessary if mov{h,l}pd were taught
4127 // to match extract_elt for f64.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004128 unsigned Idx = cast<ConstantSDNode>(Op.getOperand(1))->getValue();
4129 if (Idx == 0)
4130 return Op;
4131
4132 // UNPCKHPD the element to the lowest double word, then movsd.
4133 // Note if the lower 64 bits of the result of the UNPCKHPD is then stored
4134 // to a f64mem, the whole operation is folded into a single MOVHPDmr.
Duncan Sandsd038e042008-07-21 10:20:31 +00004135 MVT MaskVT = MVT::getIntVectorWithNumElements(2);
Dan Gohman475871a2008-07-27 21:46:04 +00004136 SmallVector<SDValue, 8> IdxVec;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004137 IdxVec.push_back(DAG.getConstant(1, MaskVT.getVectorElementType()));
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00004138 IdxVec.
Duncan Sands83ec4b62008-06-06 12:08:01 +00004139 push_back(DAG.getNode(ISD::UNDEF, MaskVT.getVectorElementType()));
Dan Gohman475871a2008-07-27 21:46:04 +00004140 SDValue Mask = DAG.getNode(ISD::BUILD_VECTOR, MaskVT,
Chris Lattnere2199452006-08-11 17:38:39 +00004141 &IdxVec[0], IdxVec.size());
Dan Gohman475871a2008-07-27 21:46:04 +00004142 SDValue Vec = Op.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004143 Vec = DAG.getNode(ISD::VECTOR_SHUFFLE, Vec.getValueType(),
4144 Vec, DAG.getNode(ISD::UNDEF, Vec.getValueType()), Mask);
4145 return DAG.getNode(ISD::EXTRACT_VECTOR_ELT, VT, Vec,
Chris Lattner0bd48932008-01-17 07:00:52 +00004146 DAG.getIntPtrConstant(0));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004147 }
4148
Dan Gohman475871a2008-07-27 21:46:04 +00004149 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004150}
4151
Dan Gohman475871a2008-07-27 21:46:04 +00004152SDValue
4153X86TargetLowering::LowerINSERT_VECTOR_ELT_SSE4(SDValue Op, SelectionDAG &DAG){
Duncan Sands83ec4b62008-06-06 12:08:01 +00004154 MVT VT = Op.getValueType();
4155 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004156
Dan Gohman475871a2008-07-27 21:46:04 +00004157 SDValue N0 = Op.getOperand(0);
4158 SDValue N1 = Op.getOperand(1);
4159 SDValue N2 = Op.getOperand(2);
Nate Begeman14d12ca2008-02-11 04:19:36 +00004160
Dan Gohmanef521f12008-08-14 22:53:18 +00004161 if ((EVT.getSizeInBits() == 8 || EVT.getSizeInBits() == 16) &&
4162 isa<ConstantSDNode>(N2)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004163 unsigned Opc = (EVT.getSizeInBits() == 8) ? X86ISD::PINSRB
Nate Begeman14d12ca2008-02-11 04:19:36 +00004164 : X86ISD::PINSRW;
4165 // Transform it so it match pinsr{b,w} which expects a GR32 as its second
4166 // argument.
4167 if (N1.getValueType() != MVT::i32)
4168 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4169 if (N2.getValueType() != MVT::i32)
4170 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
4171 return DAG.getNode(Opc, VT, N0, N1, N2);
Dan Gohmanc0573b12008-08-14 22:43:26 +00004172 } else if (EVT == MVT::f32 && isa<ConstantSDNode>(N2)) {
Nate Begeman14d12ca2008-02-11 04:19:36 +00004173 // Bits [7:6] of the constant are the source select. This will always be
4174 // zero here. The DAG Combiner may combine an extract_elt index into these
4175 // bits. For example (insert (extract, 3), 2) could be matched by putting
4176 // the '3' into bits [7:6] of X86ISD::INSERTPS.
4177 // Bits [5:4] of the constant are the destination select. This is the
4178 // value of the incoming immediate.
4179 // Bits [3:0] of the constant are the zero mask. The DAG Combiner may
4180 // combine either bitwise AND or insert of float 0.0 to set these bits.
4181 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue() << 4);
4182 return DAG.getNode(X86ISD::INSERTPS, VT, N0, N1, N2);
4183 }
Dan Gohman475871a2008-07-27 21:46:04 +00004184 return SDValue();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004185}
4186
Dan Gohman475871a2008-07-27 21:46:04 +00004187SDValue
4188X86TargetLowering::LowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004189 MVT VT = Op.getValueType();
4190 MVT EVT = VT.getVectorElementType();
Nate Begeman14d12ca2008-02-11 04:19:36 +00004191
4192 if (Subtarget->hasSSE41())
4193 return LowerINSERT_VECTOR_ELT_SSE4(Op, DAG);
4194
Evan Cheng794405e2007-12-12 07:55:34 +00004195 if (EVT == MVT::i8)
Dan Gohman475871a2008-07-27 21:46:04 +00004196 return SDValue();
Evan Cheng794405e2007-12-12 07:55:34 +00004197
Dan Gohman475871a2008-07-27 21:46:04 +00004198 SDValue N0 = Op.getOperand(0);
4199 SDValue N1 = Op.getOperand(1);
4200 SDValue N2 = Op.getOperand(2);
Evan Cheng794405e2007-12-12 07:55:34 +00004201
Duncan Sands83ec4b62008-06-06 12:08:01 +00004202 if (EVT.getSizeInBits() == 16) {
Evan Cheng794405e2007-12-12 07:55:34 +00004203 // Transform it so it match pinsrw which expects a 16-bit value in a GR32
4204 // as its second argument.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004205 if (N1.getValueType() != MVT::i32)
4206 N1 = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, N1);
4207 if (N2.getValueType() != MVT::i32)
Chris Lattner0bd48932008-01-17 07:00:52 +00004208 N2 = DAG.getIntPtrConstant(cast<ConstantSDNode>(N2)->getValue());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004209 return DAG.getNode(X86ISD::PINSRW, VT, N0, N1, N2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004210 }
Dan Gohman475871a2008-07-27 21:46:04 +00004211 return SDValue();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004212}
4213
Dan Gohman475871a2008-07-27 21:46:04 +00004214SDValue
4215X86TargetLowering::LowerSCALAR_TO_VECTOR(SDValue Op, SelectionDAG &DAG) {
Evan Cheng52672b82008-07-22 18:39:19 +00004216 if (Op.getValueType() == MVT::v2f32)
4217 return DAG.getNode(ISD::BIT_CONVERT, MVT::v2f32,
4218 DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2i32,
4219 DAG.getNode(ISD::BIT_CONVERT, MVT::i32,
4220 Op.getOperand(0))));
4221
Dan Gohman475871a2008-07-27 21:46:04 +00004222 SDValue AnyExt = DAG.getNode(ISD::ANY_EXTEND, MVT::i32, Op.getOperand(0));
Duncan Sands83ec4b62008-06-06 12:08:01 +00004223 MVT VT = MVT::v2i32;
4224 switch (Op.getValueType().getSimpleVT()) {
Evan Chengefec7512008-02-18 23:04:32 +00004225 default: break;
4226 case MVT::v16i8:
4227 case MVT::v8i16:
4228 VT = MVT::v4i32;
4229 break;
4230 }
4231 return DAG.getNode(ISD::BIT_CONVERT, Op.getValueType(),
4232 DAG.getNode(ISD::SCALAR_TO_VECTOR, VT, AnyExt));
Evan Cheng0db9fe62006-04-25 20:13:52 +00004233}
4234
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00004235// ConstantPool, JumpTable, GlobalAddress, and ExternalSymbol are lowered as
Evan Cheng0db9fe62006-04-25 20:13:52 +00004236// their target countpart wrapped in the X86ISD::Wrapper node. Suppose N is
4237// one of the above mentioned nodes. It has to be wrapped because otherwise
4238// Select(N) returns N. So the raw TargetGlobalAddress nodes, etc. can only
4239// be used to form addressing mode. These wrapped nodes will be selected
4240// into MOV32ri.
Dan Gohman475871a2008-07-27 21:46:04 +00004241SDValue
4242X86TargetLowering::LowerConstantPool(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004243 ConstantPoolSDNode *CP = cast<ConstantPoolSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004244 SDValue Result = DAG.getTargetConstantPool(CP->getConstVal(),
Evan Chengd0ff02c2006-11-29 23:19:46 +00004245 getPointerTy(),
4246 CP->getAlignment());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004247 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004248 // With PIC, the address is actually $g + Offset.
4249 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4250 !Subtarget->isPICStyleRIPRel()) {
4251 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4252 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4253 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004254 }
4255
4256 return Result;
4257}
4258
Dan Gohman475871a2008-07-27 21:46:04 +00004259SDValue
4260X86TargetLowering::LowerGlobalAddress(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004261 GlobalValue *GV = cast<GlobalAddressSDNode>(Op)->getGlobal();
Dan Gohman475871a2008-07-27 21:46:04 +00004262 SDValue Result = DAG.getTargetGlobalAddress(GV, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004263 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004264 // With PIC, the address is actually $g + Offset.
4265 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4266 !Subtarget->isPICStyleRIPRel()) {
4267 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4268 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4269 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004270 }
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004271
4272 // For Darwin & Mingw32, external and weak symbols are indirect, so we want to
4273 // load the value at address GV, not the value of GV itself. This means that
4274 // the GlobalAddress must be in the base or index register of the address, not
4275 // the GV offset field. Platform check is inside GVRequiresExtraLoad() call
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004276 // The same applies for external symbols during PIC codegen
Anton Korobeynikov2b2bc682006-12-22 22:29:05 +00004277 if (Subtarget->GVRequiresExtraLoad(GV, getTargetMachine(), false))
Dan Gohman69de1932008-02-06 22:27:42 +00004278 Result = DAG.getLoad(getPointerTy(), DAG.getEntryNode(), Result,
Dan Gohman3069b872008-02-07 18:41:25 +00004279 PseudoSourceValue::getGOT(), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004280
4281 return Result;
4282}
4283
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004284// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 32 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004285static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004286LowerToTLSGeneralDynamicModel32(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004287 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004288 SDValue InFlag;
4289 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), X86::EBX,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004290 DAG.getNode(X86ISD::GlobalBaseReg,
4291 PtrVT), InFlag);
4292 InFlag = Chain.getValue(1);
4293
4294 // emit leal symbol@TLSGD(,%ebx,1), %eax
4295 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004296 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004297 GA->getValueType(0),
4298 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004299 SDValue Ops[] = { Chain, TGA, InFlag };
4300 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 3);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004301 InFlag = Result.getValue(2);
4302 Chain = Result.getValue(1);
4303
4304 // call ___tls_get_addr. This function receives its argument in
4305 // the register EAX.
4306 Chain = DAG.getCopyToReg(Chain, X86::EAX, Result, InFlag);
4307 InFlag = Chain.getValue(1);
4308
4309 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004310 SDValue Ops1[] = { Chain,
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004311 DAG.getTargetExternalSymbol("___tls_get_addr",
4312 PtrVT),
4313 DAG.getRegister(X86::EAX, PtrVT),
4314 DAG.getRegister(X86::EBX, PtrVT),
4315 InFlag };
4316 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 5);
4317 InFlag = Chain.getValue(1);
4318
4319 return DAG.getCopyFromReg(Chain, X86::EAX, PtrVT, InFlag);
4320}
4321
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004322// Lower ISD::GlobalTLSAddress using the "general dynamic" model, 64 bit
Dan Gohman475871a2008-07-27 21:46:04 +00004323static SDValue
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004324LowerToTLSGeneralDynamicModel64(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004325 const MVT PtrVT) {
Dan Gohman475871a2008-07-27 21:46:04 +00004326 SDValue InFlag, Chain;
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004327
4328 // emit leaq symbol@TLSGD(%rip), %rdi
4329 SDVTList NodeTys = DAG.getVTList(PtrVT, MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004330 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004331 GA->getValueType(0),
4332 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004333 SDValue Ops[] = { DAG.getEntryNode(), TGA};
4334 SDValue Result = DAG.getNode(X86ISD::TLSADDR, NodeTys, Ops, 2);
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004335 Chain = Result.getValue(1);
4336 InFlag = Result.getValue(2);
4337
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004338 // call __tls_get_addr. This function receives its argument in
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004339 // the register RDI.
4340 Chain = DAG.getCopyToReg(Chain, X86::RDI, Result, InFlag);
4341 InFlag = Chain.getValue(1);
4342
4343 NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004344 SDValue Ops1[] = { Chain,
Anton Korobeynikovd97f2952008-08-16 12:58:29 +00004345 DAG.getTargetExternalSymbol("__tls_get_addr",
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004346 PtrVT),
4347 DAG.getRegister(X86::RDI, PtrVT),
4348 InFlag };
4349 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops1, 4);
4350 InFlag = Chain.getValue(1);
4351
4352 return DAG.getCopyFromReg(Chain, X86::RAX, PtrVT, InFlag);
4353}
4354
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004355// Lower ISD::GlobalTLSAddress using the "initial exec" (for no-pic) or
4356// "local exec" model.
Dan Gohman475871a2008-07-27 21:46:04 +00004357static SDValue LowerToTLSExecModel(GlobalAddressSDNode *GA, SelectionDAG &DAG,
Duncan Sands83ec4b62008-06-06 12:08:01 +00004358 const MVT PtrVT) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004359 // Get the Thread Pointer
Dan Gohman475871a2008-07-27 21:46:04 +00004360 SDValue ThreadPointer = DAG.getNode(X86ISD::THREAD_POINTER, PtrVT);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004361 // emit "addl x@ntpoff,%eax" (local exec) or "addl x@indntpoff,%eax" (initial
4362 // exec)
Dan Gohman475871a2008-07-27 21:46:04 +00004363 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(),
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004364 GA->getValueType(0),
4365 GA->getOffset());
Dan Gohman475871a2008-07-27 21:46:04 +00004366 SDValue Offset = DAG.getNode(X86ISD::Wrapper, PtrVT, TGA);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004367
4368 if (GA->getGlobal()->isDeclaration()) // initial exec TLS model
Dan Gohman69de1932008-02-06 22:27:42 +00004369 Offset = DAG.getLoad(PtrVT, DAG.getEntryNode(), Offset,
Dan Gohman3069b872008-02-07 18:41:25 +00004370 PseudoSourceValue::getGOT(), 0);
Lauro Ramos Venancio7d2cc2b2007-04-22 22:50:52 +00004371
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004372 // The address of the thread local variable is the add of the thread
4373 // pointer with the offset of the variable.
4374 return DAG.getNode(ISD::ADD, PtrVT, ThreadPointer, Offset);
4375}
4376
Dan Gohman475871a2008-07-27 21:46:04 +00004377SDValue
4378X86TargetLowering::LowerGlobalTLSAddress(SDValue Op, SelectionDAG &DAG) {
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004379 // TODO: implement the "local dynamic" model
Lauro Ramos Venancio2c5c1112007-04-21 20:56:26 +00004380 // TODO: implement the "initial exec"model for pic executables
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004381 assert(Subtarget->isTargetELF() &&
4382 "TLS not implemented for non-ELF targets");
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004383 GlobalAddressSDNode *GA = cast<GlobalAddressSDNode>(Op);
4384 // If the relocation model is PIC, use the "General Dynamic" TLS Model,
4385 // otherwise use the "Local Exec"TLS Model
Anton Korobeynikov6625eff2008-05-04 21:36:32 +00004386 if (Subtarget->is64Bit()) {
4387 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
4388 } else {
4389 if (getTargetMachine().getRelocationModel() == Reloc::PIC_)
4390 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
4391 else
4392 return LowerToTLSExecModel(GA, DAG, getPointerTy());
4393 }
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00004394}
4395
Dan Gohman475871a2008-07-27 21:46:04 +00004396SDValue
4397X86TargetLowering::LowerExternalSymbol(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004398 const char *Sym = cast<ExternalSymbolSDNode>(Op)->getSymbol();
Dan Gohman475871a2008-07-27 21:46:04 +00004399 SDValue Result = DAG.getTargetExternalSymbol(Sym, getPointerTy());
Evan Cheng19f2ffc2006-12-05 04:01:03 +00004400 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004401 // With PIC, the address is actually $g + Offset.
4402 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4403 !Subtarget->isPICStyleRIPRel()) {
4404 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4405 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4406 Result);
4407 }
4408
4409 return Result;
4410}
4411
Dan Gohman475871a2008-07-27 21:46:04 +00004412SDValue X86TargetLowering::LowerJumpTable(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004413 JumpTableSDNode *JT = cast<JumpTableSDNode>(Op);
Dan Gohman475871a2008-07-27 21:46:04 +00004414 SDValue Result = DAG.getTargetJumpTable(JT->getIndex(), getPointerTy());
Anton Korobeynikov7f705592007-01-12 19:20:47 +00004415 Result = DAG.getNode(X86ISD::Wrapper, getPointerTy(), Result);
4416 // With PIC, the address is actually $g + Offset.
4417 if (getTargetMachine().getRelocationModel() == Reloc::PIC_ &&
4418 !Subtarget->isPICStyleRIPRel()) {
4419 Result = DAG.getNode(ISD::ADD, getPointerTy(),
4420 DAG.getNode(X86ISD::GlobalBaseReg, getPointerTy()),
4421 Result);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004422 }
4423
4424 return Result;
4425}
4426
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004427/// LowerShift - Lower SRA_PARTS and friends, which return two i32 values and
4428/// take a 2 x i32 value to shift plus a shift amount.
Dan Gohman475871a2008-07-27 21:46:04 +00004429SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004430 assert(Op.getNumOperands() == 3 && "Not a double-shift!");
Duncan Sands83ec4b62008-06-06 12:08:01 +00004431 MVT VT = Op.getValueType();
4432 unsigned VTBits = VT.getSizeInBits();
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004433 bool isSRA = Op.getOpcode() == ISD::SRA_PARTS;
Dan Gohman475871a2008-07-27 21:46:04 +00004434 SDValue ShOpLo = Op.getOperand(0);
4435 SDValue ShOpHi = Op.getOperand(1);
4436 SDValue ShAmt = Op.getOperand(2);
4437 SDValue Tmp1 = isSRA ?
Dan Gohman4c1fa612008-03-03 22:22:09 +00004438 DAG.getNode(ISD::SRA, VT, ShOpHi, DAG.getConstant(VTBits - 1, MVT::i8)) :
4439 DAG.getConstant(0, VT);
Evan Chenge3413162006-01-09 18:33:28 +00004440
Dan Gohman475871a2008-07-27 21:46:04 +00004441 SDValue Tmp2, Tmp3;
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004442 if (Op.getOpcode() == ISD::SHL_PARTS) {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004443 Tmp2 = DAG.getNode(X86ISD::SHLD, VT, ShOpHi, ShOpLo, ShAmt);
4444 Tmp3 = DAG.getNode(ISD::SHL, VT, ShOpLo, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004445 } else {
Dan Gohman4c1fa612008-03-03 22:22:09 +00004446 Tmp2 = DAG.getNode(X86ISD::SHRD, VT, ShOpLo, ShOpHi, ShAmt);
4447 Tmp3 = DAG.getNode(isSRA ? ISD::SRA : ISD::SRL, VT, ShOpHi, ShAmt);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004448 }
Evan Chenge3413162006-01-09 18:33:28 +00004449
Dan Gohman475871a2008-07-27 21:46:04 +00004450 SDValue AndNode = DAG.getNode(ISD::AND, MVT::i8, ShAmt,
Dan Gohman4c1fa612008-03-03 22:22:09 +00004451 DAG.getConstant(VTBits, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00004452 SDValue Cond = DAG.getNode(X86ISD::CMP, VT,
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004453 AndNode, DAG.getConstant(0, MVT::i8));
Evan Chenge3413162006-01-09 18:33:28 +00004454
Dan Gohman475871a2008-07-27 21:46:04 +00004455 SDValue Hi, Lo;
4456 SDValue CC = DAG.getConstant(X86::COND_NE, MVT::i8);
4457 SDValue Ops0[4] = { Tmp2, Tmp3, CC, Cond };
4458 SDValue Ops1[4] = { Tmp3, Tmp1, CC, Cond };
Duncan Sandsf9516202008-06-30 10:19:09 +00004459
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004460 if (Op.getOpcode() == ISD::SHL_PARTS) {
Duncan Sandsf9516202008-06-30 10:19:09 +00004461 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4462 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004463 } else {
Duncan Sandsf9516202008-06-30 10:19:09 +00004464 Lo = DAG.getNode(X86ISD::CMOV, VT, Ops0, 4);
4465 Hi = DAG.getNode(X86ISD::CMOV, VT, Ops1, 4);
Chris Lattner2ff75ee2007-10-17 06:02:13 +00004466 }
4467
Dan Gohman475871a2008-07-27 21:46:04 +00004468 SDValue Ops[2] = { Lo, Hi };
Duncan Sands4bdcb612008-07-02 17:40:58 +00004469 return DAG.getMergeValues(Ops, 2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004470}
Evan Chenga3195e82006-01-12 22:54:21 +00004471
Dan Gohman475871a2008-07-27 21:46:04 +00004472SDValue X86TargetLowering::LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004473 MVT SrcVT = Op.getOperand(0).getValueType();
Duncan Sands8e4eb092008-06-08 20:54:56 +00004474 assert(SrcVT.getSimpleVT() <= MVT::i64 && SrcVT.getSimpleVT() >= MVT::i16 &&
Chris Lattnerb09916b2008-02-27 05:57:41 +00004475 "Unknown SINT_TO_FP to lower!");
4476
4477 // These are really Legal; caller falls through into that case.
4478 if (SrcVT == MVT::i32 && isScalarFPTypeInSSEReg(Op.getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004479 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004480 if (SrcVT == MVT::i64 && Op.getValueType() != MVT::f80 &&
4481 Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00004482 return SDValue();
Chris Lattnerb09916b2008-02-27 05:57:41 +00004483
Duncan Sands83ec4b62008-06-06 12:08:01 +00004484 unsigned Size = SrcVT.getSizeInBits()/8;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004485 MachineFunction &MF = DAG.getMachineFunction();
4486 int SSFI = MF.getFrameInfo()->CreateStackObject(Size, Size);
Dan Gohman475871a2008-07-27 21:46:04 +00004487 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4488 SDValue Chain = DAG.getStore(DAG.getEntryNode(), Op.getOperand(0),
Dan Gohman69de1932008-02-06 22:27:42 +00004489 StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004490 PseudoSourceValue::getFixedStack(SSFI), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004491
4492 // Build the FILD
Chris Lattner5a88b832007-02-25 07:10:00 +00004493 SDVTList Tys;
Chris Lattner78631162008-01-16 06:24:21 +00004494 bool useSSE = isScalarFPTypeInSSEReg(Op.getValueType());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004495 if (useSSE)
Chris Lattner5a88b832007-02-25 07:10:00 +00004496 Tys = DAG.getVTList(MVT::f64, MVT::Other, MVT::Flag);
4497 else
Dale Johannesen849f2142007-07-03 00:53:03 +00004498 Tys = DAG.getVTList(Op.getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004499 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004500 Ops.push_back(Chain);
4501 Ops.push_back(StackSlot);
4502 Ops.push_back(DAG.getValueType(SrcVT));
Dan Gohman475871a2008-07-27 21:46:04 +00004503 SDValue Result = DAG.getNode(useSSE ? X86ISD::FILD_FLAG : X86ISD::FILD,
Chris Lattnerb09916b2008-02-27 05:57:41 +00004504 Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004505
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004506 if (useSSE) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004507 Chain = Result.getValue(1);
Dan Gohman475871a2008-07-27 21:46:04 +00004508 SDValue InFlag = Result.getValue(2);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004509
4510 // FIXME: Currently the FST is flagged to the FILD_FLAG. This
4511 // shouldn't be necessary except that RFP cannot be live across
4512 // multiple blocks. When stackifier is fixed, they can be uncoupled.
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004513 MachineFunction &MF = DAG.getMachineFunction();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004514 int SSFI = MF.getFrameInfo()->CreateStackObject(8, 8);
Dan Gohman475871a2008-07-27 21:46:04 +00004515 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Chris Lattner5a88b832007-02-25 07:10:00 +00004516 Tys = DAG.getVTList(MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004517 SmallVector<SDValue, 8> Ops;
Evan Chenga3195e82006-01-12 22:54:21 +00004518 Ops.push_back(Chain);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004519 Ops.push_back(Result);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004520 Ops.push_back(StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004521 Ops.push_back(DAG.getValueType(Op.getValueType()));
4522 Ops.push_back(InFlag);
Chris Lattnerbd564bf2006-08-08 02:23:42 +00004523 Chain = DAG.getNode(X86ISD::FST, Tys, &Ops[0], Ops.size());
Dan Gohman69de1932008-02-06 22:27:42 +00004524 Result = DAG.getLoad(Op.getValueType(), Chain, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004525 PseudoSourceValue::getFixedStack(SSFI), 0);
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004526 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004527
Evan Cheng0db9fe62006-04-25 20:13:52 +00004528 return Result;
4529}
4530
Dan Gohman475871a2008-07-27 21:46:04 +00004531std::pair<SDValue,SDValue> X86TargetLowering::
4532FP_TO_SINTHelper(SDValue Op, SelectionDAG &DAG) {
Duncan Sands8e4eb092008-06-08 20:54:56 +00004533 assert(Op.getValueType().getSimpleVT() <= MVT::i64 &&
4534 Op.getValueType().getSimpleVT() >= MVT::i16 &&
Evan Cheng0db9fe62006-04-25 20:13:52 +00004535 "Unknown FP_TO_SINT to lower!");
Evan Cheng0db9fe62006-04-25 20:13:52 +00004536
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004537 // These are really Legal.
Dale Johannesenf1fc3a82007-09-23 14:52:20 +00004538 if (Op.getValueType() == MVT::i32 &&
Chris Lattner78631162008-01-16 06:24:21 +00004539 isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType()))
Dan Gohman475871a2008-07-27 21:46:04 +00004540 return std::make_pair(SDValue(), SDValue());
Dale Johannesen73328d12007-09-19 23:55:34 +00004541 if (Subtarget->is64Bit() &&
4542 Op.getValueType() == MVT::i64 &&
4543 Op.getOperand(0).getValueType() != MVT::f80)
Dan Gohman475871a2008-07-27 21:46:04 +00004544 return std::make_pair(SDValue(), SDValue());
Dale Johannesen9e3d3ab2007-09-14 22:26:36 +00004545
Evan Cheng87c89352007-10-15 20:11:21 +00004546 // We lower FP->sint64 into FISTP64, followed by a load, all to a temporary
4547 // stack slot.
4548 MachineFunction &MF = DAG.getMachineFunction();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004549 unsigned MemSize = Op.getValueType().getSizeInBits()/8;
Evan Cheng87c89352007-10-15 20:11:21 +00004550 int SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
Dan Gohman475871a2008-07-27 21:46:04 +00004551 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Evan Cheng0db9fe62006-04-25 20:13:52 +00004552 unsigned Opc;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004553 switch (Op.getValueType().getSimpleVT()) {
Chris Lattner27a6c732007-11-24 07:07:01 +00004554 default: assert(0 && "Invalid FP_TO_SINT to lower!");
4555 case MVT::i16: Opc = X86ISD::FP_TO_INT16_IN_MEM; break;
4556 case MVT::i32: Opc = X86ISD::FP_TO_INT32_IN_MEM; break;
4557 case MVT::i64: Opc = X86ISD::FP_TO_INT64_IN_MEM; break;
Evan Cheng0db9fe62006-04-25 20:13:52 +00004558 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004559
Dan Gohman475871a2008-07-27 21:46:04 +00004560 SDValue Chain = DAG.getEntryNode();
4561 SDValue Value = Op.getOperand(0);
Chris Lattner78631162008-01-16 06:24:21 +00004562 if (isScalarFPTypeInSSEReg(Op.getOperand(0).getValueType())) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00004563 assert(Op.getValueType() == MVT::i64 && "Invalid FP_TO_SINT to lower!");
Dan Gohman69de1932008-02-06 22:27:42 +00004564 Chain = DAG.getStore(Chain, Value, StackSlot,
Dan Gohmana54cf172008-07-11 22:44:52 +00004565 PseudoSourceValue::getFixedStack(SSFI), 0);
Dale Johannesen849f2142007-07-03 00:53:03 +00004566 SDVTList Tys = DAG.getVTList(Op.getOperand(0).getValueType(), MVT::Other);
Dan Gohman475871a2008-07-27 21:46:04 +00004567 SDValue Ops[] = {
Chris Lattner5a88b832007-02-25 07:10:00 +00004568 Chain, StackSlot, DAG.getValueType(Op.getOperand(0).getValueType())
4569 };
4570 Value = DAG.getNode(X86ISD::FLD, Tys, Ops, 3);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004571 Chain = Value.getValue(1);
4572 SSFI = MF.getFrameInfo()->CreateStackObject(MemSize, MemSize);
4573 StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
4574 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00004575
Evan Cheng0db9fe62006-04-25 20:13:52 +00004576 // Build the FP_TO_INT*_IN_MEM
Dan Gohman475871a2008-07-27 21:46:04 +00004577 SDValue Ops[] = { Chain, Value, StackSlot };
4578 SDValue FIST = DAG.getNode(Opc, MVT::Other, Ops, 3);
Evan Chengd9558e02006-01-06 00:43:03 +00004579
Chris Lattner27a6c732007-11-24 07:07:01 +00004580 return std::make_pair(FIST, StackSlot);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004581}
4582
Dan Gohman475871a2008-07-27 21:46:04 +00004583SDValue X86TargetLowering::LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) {
4584 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(Op, DAG);
4585 SDValue FIST = Vals.first, StackSlot = Vals.second;
4586 if (FIST.Val == 0) return SDValue();
Chris Lattner27a6c732007-11-24 07:07:01 +00004587
4588 // Load the result.
4589 return DAG.getLoad(Op.getValueType(), FIST, StackSlot, NULL, 0);
4590}
4591
4592SDNode *X86TargetLowering::ExpandFP_TO_SINT(SDNode *N, SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00004593 std::pair<SDValue,SDValue> Vals = FP_TO_SINTHelper(SDValue(N, 0), DAG);
4594 SDValue FIST = Vals.first, StackSlot = Vals.second;
Chris Lattner27a6c732007-11-24 07:07:01 +00004595 if (FIST.Val == 0) return 0;
Duncan Sandsf9516202008-06-30 10:19:09 +00004596
4597 MVT VT = N->getValueType(0);
4598
4599 // Return a load from the stack slot.
Dan Gohman475871a2008-07-27 21:46:04 +00004600 SDValue Res = DAG.getLoad(VT, FIST, StackSlot, NULL, 0);
Chris Lattner27a6c732007-11-24 07:07:01 +00004601
Duncan Sands4bdcb612008-07-02 17:40:58 +00004602 // Use MERGE_VALUES to drop the chain result value and get a node with one
4603 // result. This requires turning off getMergeValues simplification, since
4604 // otherwise it will give us Res back.
4605 return DAG.getMergeValues(&Res, 1, false).Val;
Duncan Sandsf9516202008-06-30 10:19:09 +00004606}
Chris Lattner27a6c732007-11-24 07:07:01 +00004607
Dan Gohman475871a2008-07-27 21:46:04 +00004608SDValue X86TargetLowering::LowerFABS(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004609 MVT VT = Op.getValueType();
4610 MVT EltVT = VT;
4611 if (VT.isVector())
4612 EltVT = VT.getVectorElementType();
Evan Cheng0db9fe62006-04-25 20:13:52 +00004613 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004614 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004615 Constant *C = ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63))));
Dan Gohman20382522007-07-10 00:05:58 +00004616 CV.push_back(C);
4617 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004618 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004619 Constant *C = ConstantFP::get(APFloat(APInt(32, ~(1U << 31))));
Dan Gohman20382522007-07-10 00:05:58 +00004620 CV.push_back(C);
4621 CV.push_back(C);
4622 CV.push_back(C);
4623 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004624 }
Dan Gohmand3006222007-07-27 17:16:43 +00004625 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004626 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4627 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004628 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004629 false, 16);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004630 return DAG.getNode(X86ISD::FAND, VT, Op.getOperand(0), Mask);
4631}
4632
Dan Gohman475871a2008-07-27 21:46:04 +00004633SDValue X86TargetLowering::LowerFNEG(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00004634 MVT VT = Op.getValueType();
4635 MVT EltVT = VT;
Evan Chengd4d01b72007-07-19 23:36:01 +00004636 unsigned EltNum = 1;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004637 if (VT.isVector()) {
4638 EltVT = VT.getVectorElementType();
4639 EltNum = VT.getVectorNumElements();
Evan Chengd4d01b72007-07-19 23:36:01 +00004640 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004641 std::vector<Constant*> CV;
Dan Gohman20382522007-07-10 00:05:58 +00004642 if (EltVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004643 Constant *C = ConstantFP::get(APFloat(APInt(64, 1ULL << 63)));
Dan Gohman20382522007-07-10 00:05:58 +00004644 CV.push_back(C);
4645 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004646 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004647 Constant *C = ConstantFP::get(APFloat(APInt(32, 1U << 31)));
Dan Gohman20382522007-07-10 00:05:58 +00004648 CV.push_back(C);
4649 CV.push_back(C);
4650 CV.push_back(C);
4651 CV.push_back(C);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004652 }
Dan Gohmand3006222007-07-27 17:16:43 +00004653 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004654 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4655 SDValue Mask = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004656 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004657 false, 16);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004658 if (VT.isVector()) {
Evan Chengd4d01b72007-07-19 23:36:01 +00004659 return DAG.getNode(ISD::BIT_CONVERT, VT,
4660 DAG.getNode(ISD::XOR, MVT::v2i64,
4661 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Op.getOperand(0)),
4662 DAG.getNode(ISD::BIT_CONVERT, MVT::v2i64, Mask)));
4663 } else {
Evan Chengd4d01b72007-07-19 23:36:01 +00004664 return DAG.getNode(X86ISD::FXOR, VT, Op.getOperand(0), Mask);
4665 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00004666}
4667
Dan Gohman475871a2008-07-27 21:46:04 +00004668SDValue X86TargetLowering::LowerFCOPYSIGN(SDValue Op, SelectionDAG &DAG) {
4669 SDValue Op0 = Op.getOperand(0);
4670 SDValue Op1 = Op.getOperand(1);
Duncan Sands83ec4b62008-06-06 12:08:01 +00004671 MVT VT = Op.getValueType();
4672 MVT SrcVT = Op1.getValueType();
Evan Cheng73d6cf12007-01-05 21:37:56 +00004673
4674 // If second operand is smaller, extend it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004675 if (SrcVT.bitsLT(VT)) {
Evan Cheng73d6cf12007-01-05 21:37:56 +00004676 Op1 = DAG.getNode(ISD::FP_EXTEND, VT, Op1);
4677 SrcVT = VT;
4678 }
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004679 // And if it is bigger, shrink it first.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004680 if (SrcVT.bitsGT(VT)) {
Chris Lattner0bd48932008-01-17 07:00:52 +00004681 Op1 = DAG.getNode(ISD::FP_ROUND, VT, Op1, DAG.getIntPtrConstant(1));
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004682 SrcVT = VT;
Dale Johannesen61c7ef32007-10-21 01:07:44 +00004683 }
4684
4685 // At this point the operands and the result should have the same
4686 // type, and that won't be f80 since that is not custom lowered.
Evan Cheng73d6cf12007-01-05 21:37:56 +00004687
Evan Cheng68c47cb2007-01-05 07:55:56 +00004688 // First get the sign bit of second operand.
4689 std::vector<Constant*> CV;
4690 if (SrcVT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004691 CV.push_back(ConstantFP::get(APFloat(APInt(64, 1ULL << 63))));
4692 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004693 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004694 CV.push_back(ConstantFP::get(APFloat(APInt(32, 1U << 31))));
4695 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4696 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4697 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004698 }
Dan Gohmand3006222007-07-27 17:16:43 +00004699 Constant *C = ConstantVector::get(CV);
Dan Gohman475871a2008-07-27 21:46:04 +00004700 SDValue CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
4701 SDValue Mask1 = DAG.getLoad(SrcVT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004702 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004703 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004704 SDValue SignBit = DAG.getNode(X86ISD::FAND, SrcVT, Op1, Mask1);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004705
4706 // Shift sign bit right or left if the two operands have different types.
Duncan Sands8e4eb092008-06-08 20:54:56 +00004707 if (SrcVT.bitsGT(VT)) {
Evan Cheng68c47cb2007-01-05 07:55:56 +00004708 // Op0 is MVT::f32, Op1 is MVT::f64.
4709 SignBit = DAG.getNode(ISD::SCALAR_TO_VECTOR, MVT::v2f64, SignBit);
4710 SignBit = DAG.getNode(X86ISD::FSRL, MVT::v2f64, SignBit,
4711 DAG.getConstant(32, MVT::i32));
4712 SignBit = DAG.getNode(ISD::BIT_CONVERT, MVT::v4f32, SignBit);
4713 SignBit = DAG.getNode(ISD::EXTRACT_VECTOR_ELT, MVT::f32, SignBit,
Chris Lattner0bd48932008-01-17 07:00:52 +00004714 DAG.getIntPtrConstant(0));
Evan Cheng68c47cb2007-01-05 07:55:56 +00004715 }
4716
Evan Cheng73d6cf12007-01-05 21:37:56 +00004717 // Clear first operand sign bit.
4718 CV.clear();
4719 if (VT == MVT::f64) {
Chris Lattner02a260a2008-04-20 00:41:09 +00004720 CV.push_back(ConstantFP::get(APFloat(APInt(64, ~(1ULL << 63)))));
4721 CV.push_back(ConstantFP::get(APFloat(APInt(64, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004722 } else {
Chris Lattner02a260a2008-04-20 00:41:09 +00004723 CV.push_back(ConstantFP::get(APFloat(APInt(32, ~(1U << 31)))));
4724 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4725 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
4726 CV.push_back(ConstantFP::get(APFloat(APInt(32, 0))));
Evan Cheng73d6cf12007-01-05 21:37:56 +00004727 }
Dan Gohmand3006222007-07-27 17:16:43 +00004728 C = ConstantVector::get(CV);
4729 CPIdx = DAG.getConstantPool(C, getPointerTy(), 4);
Dan Gohman475871a2008-07-27 21:46:04 +00004730 SDValue Mask2 = DAG.getLoad(VT, DAG.getEntryNode(), CPIdx,
Dan Gohman3069b872008-02-07 18:41:25 +00004731 PseudoSourceValue::getConstantPool(), 0,
Dan Gohmand3006222007-07-27 17:16:43 +00004732 false, 16);
Dan Gohman475871a2008-07-27 21:46:04 +00004733 SDValue Val = DAG.getNode(X86ISD::FAND, VT, Op0, Mask2);
Evan Cheng73d6cf12007-01-05 21:37:56 +00004734
4735 // Or the value with the sign bit.
4736 return DAG.getNode(X86ISD::FOR, VT, Val, SignBit);
Evan Cheng68c47cb2007-01-05 07:55:56 +00004737}
4738
Dan Gohman475871a2008-07-27 21:46:04 +00004739SDValue X86TargetLowering::LowerSETCC(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0488db92007-09-25 01:57:46 +00004740 assert(Op.getValueType() == MVT::i8 && "SetCC type must be 8-bit integer");
Dan Gohman475871a2008-07-27 21:46:04 +00004741 SDValue Cond;
4742 SDValue Op0 = Op.getOperand(0);
4743 SDValue Op1 = Op.getOperand(1);
4744 SDValue CC = Op.getOperand(2);
Evan Cheng0488db92007-09-25 01:57:46 +00004745 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004746 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
Evan Cheng0488db92007-09-25 01:57:46 +00004747 unsigned X86CC;
4748
Evan Cheng0488db92007-09-25 01:57:46 +00004749 if (translateX86CC(cast<CondCodeSDNode>(CC)->get(), isFP, X86CC,
Evan Cheng1a35edb2007-09-26 00:45:55 +00004750 Op0, Op1, DAG)) {
Evan Chenge5f62042007-09-29 00:00:36 +00004751 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
4752 return DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004753 DAG.getConstant(X86CC, MVT::i8), Cond);
Evan Cheng1a35edb2007-09-26 00:45:55 +00004754 }
Evan Cheng0488db92007-09-25 01:57:46 +00004755
4756 assert(isFP && "Illegal integer SetCC!");
4757
Evan Chenge5f62042007-09-29 00:00:36 +00004758 Cond = DAG.getNode(X86ISD::CMP, MVT::i32, Op0, Op1);
Evan Cheng0488db92007-09-25 01:57:46 +00004759 switch (SetCCOpcode) {
4760 default: assert(false && "Illegal floating point SetCC!");
4761 case ISD::SETOEQ: { // !PF & ZF
Dan Gohman475871a2008-07-27 21:46:04 +00004762 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004763 DAG.getConstant(X86::COND_NP, MVT::i8), Cond);
Dan Gohman475871a2008-07-27 21:46:04 +00004764 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004765 DAG.getConstant(X86::COND_E, MVT::i8), Cond);
4766 return DAG.getNode(ISD::AND, MVT::i8, Tmp1, Tmp2);
4767 }
4768 case ISD::SETUNE: { // PF | !ZF
Dan Gohman475871a2008-07-27 21:46:04 +00004769 SDValue Tmp1 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004770 DAG.getConstant(X86::COND_P, MVT::i8), Cond);
Dan Gohman475871a2008-07-27 21:46:04 +00004771 SDValue Tmp2 = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0488db92007-09-25 01:57:46 +00004772 DAG.getConstant(X86::COND_NE, MVT::i8), Cond);
4773 return DAG.getNode(ISD::OR, MVT::i8, Tmp1, Tmp2);
4774 }
4775 }
4776}
4777
Dan Gohman475871a2008-07-27 21:46:04 +00004778SDValue X86TargetLowering::LowerVSETCC(SDValue Op, SelectionDAG &DAG) {
4779 SDValue Cond;
4780 SDValue Op0 = Op.getOperand(0);
4781 SDValue Op1 = Op.getOperand(1);
4782 SDValue CC = Op.getOperand(2);
Nate Begeman30a0de92008-07-17 16:51:19 +00004783 MVT VT = Op.getValueType();
4784 ISD::CondCode SetCCOpcode = cast<CondCodeSDNode>(CC)->get();
4785 bool isFP = Op.getOperand(1).getValueType().isFloatingPoint();
4786
4787 if (isFP) {
4788 unsigned SSECC = 8;
Evan Chenge9d50352008-08-05 22:19:15 +00004789 MVT VT0 = Op0.getValueType();
4790 assert(VT0 == MVT::v4f32 || VT0 == MVT::v2f64);
4791 unsigned Opc = VT0 == MVT::v4f32 ? X86ISD::CMPPS : X86ISD::CMPPD;
Nate Begeman30a0de92008-07-17 16:51:19 +00004792 bool Swap = false;
4793
4794 switch (SetCCOpcode) {
4795 default: break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004796 case ISD::SETOEQ:
Nate Begeman30a0de92008-07-17 16:51:19 +00004797 case ISD::SETEQ: SSECC = 0; break;
4798 case ISD::SETOGT:
4799 case ISD::SETGT: Swap = true; // Fallthrough
4800 case ISD::SETLT:
4801 case ISD::SETOLT: SSECC = 1; break;
4802 case ISD::SETOGE:
4803 case ISD::SETGE: Swap = true; // Fallthrough
4804 case ISD::SETLE:
4805 case ISD::SETOLE: SSECC = 2; break;
4806 case ISD::SETUO: SSECC = 3; break;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004807 case ISD::SETUNE:
Nate Begeman30a0de92008-07-17 16:51:19 +00004808 case ISD::SETNE: SSECC = 4; break;
4809 case ISD::SETULE: Swap = true;
4810 case ISD::SETUGE: SSECC = 5; break;
4811 case ISD::SETULT: Swap = true;
4812 case ISD::SETUGT: SSECC = 6; break;
4813 case ISD::SETO: SSECC = 7; break;
4814 }
4815 if (Swap)
4816 std::swap(Op0, Op1);
4817
Nate Begemanfb8ead02008-07-25 19:05:58 +00004818 // In the two special cases we can't handle, emit two comparisons.
Nate Begeman30a0de92008-07-17 16:51:19 +00004819 if (SSECC == 8) {
Nate Begemanfb8ead02008-07-25 19:05:58 +00004820 if (SetCCOpcode == ISD::SETUEQ) {
Dan Gohman475871a2008-07-27 21:46:04 +00004821 SDValue UNORD, EQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004822 UNORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(3, MVT::i8));
4823 EQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(0, MVT::i8));
4824 return DAG.getNode(ISD::OR, VT, UNORD, EQ);
4825 }
4826 else if (SetCCOpcode == ISD::SETONE) {
Dan Gohman475871a2008-07-27 21:46:04 +00004827 SDValue ORD, NEQ;
Nate Begemanfb8ead02008-07-25 19:05:58 +00004828 ORD = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(7, MVT::i8));
4829 NEQ = DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(4, MVT::i8));
4830 return DAG.getNode(ISD::AND, VT, ORD, NEQ);
4831 }
4832 assert(0 && "Illegal FP comparison");
Nate Begeman30a0de92008-07-17 16:51:19 +00004833 }
4834 // Handle all other FP comparisons here.
4835 return DAG.getNode(Opc, VT, Op0, Op1, DAG.getConstant(SSECC, MVT::i8));
4836 }
4837
4838 // We are handling one of the integer comparisons here. Since SSE only has
4839 // GT and EQ comparisons for integer, swapping operands and multiple
4840 // operations may be required for some comparisons.
4841 unsigned Opc = 0, EQOpc = 0, GTOpc = 0;
4842 bool Swap = false, Invert = false, FlipSigns = false;
4843
4844 switch (VT.getSimpleVT()) {
4845 default: break;
4846 case MVT::v16i8: EQOpc = X86ISD::PCMPEQB; GTOpc = X86ISD::PCMPGTB; break;
4847 case MVT::v8i16: EQOpc = X86ISD::PCMPEQW; GTOpc = X86ISD::PCMPGTW; break;
4848 case MVT::v4i32: EQOpc = X86ISD::PCMPEQD; GTOpc = X86ISD::PCMPGTD; break;
4849 case MVT::v2i64: EQOpc = X86ISD::PCMPEQQ; GTOpc = X86ISD::PCMPGTQ; break;
4850 }
4851
4852 switch (SetCCOpcode) {
4853 default: break;
4854 case ISD::SETNE: Invert = true;
4855 case ISD::SETEQ: Opc = EQOpc; break;
4856 case ISD::SETLT: Swap = true;
4857 case ISD::SETGT: Opc = GTOpc; break;
4858 case ISD::SETGE: Swap = true;
4859 case ISD::SETLE: Opc = GTOpc; Invert = true; break;
4860 case ISD::SETULT: Swap = true;
4861 case ISD::SETUGT: Opc = GTOpc; FlipSigns = true; break;
4862 case ISD::SETUGE: Swap = true;
4863 case ISD::SETULE: Opc = GTOpc; FlipSigns = true; Invert = true; break;
4864 }
4865 if (Swap)
4866 std::swap(Op0, Op1);
4867
4868 // Since SSE has no unsigned integer comparisons, we need to flip the sign
4869 // bits of the inputs before performing those operations.
4870 if (FlipSigns) {
4871 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00004872 SDValue SignBit = DAG.getConstant(EltVT.getIntegerVTSignBit(), EltVT);
4873 std::vector<SDValue> SignBits(VT.getVectorNumElements(), SignBit);
4874 SDValue SignVec = DAG.getNode(ISD::BUILD_VECTOR, VT, &SignBits[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00004875 SignBits.size());
4876 Op0 = DAG.getNode(ISD::XOR, VT, Op0, SignVec);
4877 Op1 = DAG.getNode(ISD::XOR, VT, Op1, SignVec);
4878 }
4879
Dan Gohman475871a2008-07-27 21:46:04 +00004880 SDValue Result = DAG.getNode(Opc, VT, Op0, Op1);
Nate Begeman30a0de92008-07-17 16:51:19 +00004881
4882 // If the logical-not of the result is required, perform that now.
4883 if (Invert) {
4884 MVT EltVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00004885 SDValue NegOne = DAG.getConstant(EltVT.getIntegerVTBitMask(), EltVT);
4886 std::vector<SDValue> NegOnes(VT.getVectorNumElements(), NegOne);
4887 SDValue NegOneV = DAG.getNode(ISD::BUILD_VECTOR, VT, &NegOnes[0],
Nate Begeman30a0de92008-07-17 16:51:19 +00004888 NegOnes.size());
4889 Result = DAG.getNode(ISD::XOR, VT, Result, NegOneV);
4890 }
4891 return Result;
4892}
Evan Cheng0488db92007-09-25 01:57:46 +00004893
Dan Gohman475871a2008-07-27 21:46:04 +00004894SDValue X86TargetLowering::LowerSELECT(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004895 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004896 SDValue Cond = Op.getOperand(0);
4897 SDValue CC;
Evan Cheng9bba8942006-01-26 02:13:10 +00004898
Evan Cheng734503b2006-09-11 02:19:56 +00004899 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004900 Cond = LowerSETCC(Cond, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00004901
Evan Cheng3f41d662007-10-08 22:16:29 +00004902 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4903 // setting operand in place of the X86ISD::SETCC.
Evan Cheng734503b2006-09-11 02:19:56 +00004904 if (Cond.getOpcode() == X86ISD::SETCC) {
4905 CC = Cond.getOperand(0);
4906
Dan Gohman475871a2008-07-27 21:46:04 +00004907 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00004908 unsigned Opc = Cmp.getOpcode();
Duncan Sands83ec4b62008-06-06 12:08:01 +00004909 MVT VT = Op.getValueType();
Chris Lattner1956d152008-01-16 06:19:45 +00004910
Evan Cheng3f41d662007-10-08 22:16:29 +00004911 bool IllegalFPCMov = false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00004912 if (VT.isFloatingPoint() && !VT.isVector() &&
Chris Lattner78631162008-01-16 06:24:21 +00004913 !isScalarFPTypeInSSEReg(VT)) // FPStack?
Evan Cheng3f41d662007-10-08 22:16:29 +00004914 IllegalFPCMov = !hasFPCMov(cast<ConstantSDNode>(CC)->getSignExtended());
Chris Lattner1956d152008-01-16 06:19:45 +00004915
Evan Chenge5f62042007-09-29 00:00:36 +00004916 if ((Opc == X86ISD::CMP ||
4917 Opc == X86ISD::COMI ||
4918 Opc == X86ISD::UCOMI) && !IllegalFPCMov) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004919 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004920 addTest = false;
4921 }
4922 }
4923
4924 if (addTest) {
4925 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Cheng3f41d662007-10-08 22:16:29 +00004926 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004927 }
4928
Duncan Sands83ec4b62008-06-06 12:08:01 +00004929 const MVT *VTs = DAG.getNodeValueTypes(Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004930 MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00004931 SmallVector<SDValue, 4> Ops;
Evan Cheng0488db92007-09-25 01:57:46 +00004932 // X86ISD::CMOV means set the result (which is operand 1) to the RHS if
4933 // condition is true.
4934 Ops.push_back(Op.getOperand(2));
4935 Ops.push_back(Op.getOperand(1));
4936 Ops.push_back(CC);
4937 Ops.push_back(Cond);
Evan Chenge5f62042007-09-29 00:00:36 +00004938 return DAG.getNode(X86ISD::CMOV, VTs, 2, &Ops[0], Ops.size());
Evan Cheng0488db92007-09-25 01:57:46 +00004939}
4940
Dan Gohman475871a2008-07-27 21:46:04 +00004941SDValue X86TargetLowering::LowerBRCOND(SDValue Op, SelectionDAG &DAG) {
Evan Cheng734503b2006-09-11 02:19:56 +00004942 bool addTest = true;
Dan Gohman475871a2008-07-27 21:46:04 +00004943 SDValue Chain = Op.getOperand(0);
4944 SDValue Cond = Op.getOperand(1);
4945 SDValue Dest = Op.getOperand(2);
4946 SDValue CC;
Evan Cheng734503b2006-09-11 02:19:56 +00004947
Evan Cheng0db9fe62006-04-25 20:13:52 +00004948 if (Cond.getOpcode() == ISD::SETCC)
Evan Chenge5f62042007-09-29 00:00:36 +00004949 Cond = LowerSETCC(Cond, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004950
Evan Cheng3f41d662007-10-08 22:16:29 +00004951 // If condition flag is set by a X86ISD::CMP, then use it as the condition
4952 // setting operand in place of the X86ISD::SETCC.
Evan Cheng0db9fe62006-04-25 20:13:52 +00004953 if (Cond.getOpcode() == X86ISD::SETCC) {
Evan Cheng734503b2006-09-11 02:19:56 +00004954 CC = Cond.getOperand(0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00004955
Dan Gohman475871a2008-07-27 21:46:04 +00004956 SDValue Cmp = Cond.getOperand(1);
Evan Cheng734503b2006-09-11 02:19:56 +00004957 unsigned Opc = Cmp.getOpcode();
Evan Chenge5f62042007-09-29 00:00:36 +00004958 if (Opc == X86ISD::CMP ||
4959 Opc == X86ISD::COMI ||
4960 Opc == X86ISD::UCOMI) {
Evan Cheng3f41d662007-10-08 22:16:29 +00004961 Cond = Cmp;
Evan Cheng0488db92007-09-25 01:57:46 +00004962 addTest = false;
4963 }
4964 }
4965
4966 if (addTest) {
4967 CC = DAG.getConstant(X86::COND_NE, MVT::i8);
Evan Chenge5f62042007-09-29 00:00:36 +00004968 Cond= DAG.getNode(X86ISD::CMP, MVT::i32, Cond, DAG.getConstant(0, MVT::i8));
Evan Cheng0488db92007-09-25 01:57:46 +00004969 }
Evan Chenge5f62042007-09-29 00:00:36 +00004970 return DAG.getNode(X86ISD::BRCOND, Op.getValueType(),
Evan Cheng0488db92007-09-25 01:57:46 +00004971 Chain, Op.getOperand(2), CC, Cond);
4972}
4973
Anton Korobeynikove060b532007-04-17 19:34:00 +00004974
4975// Lower dynamic stack allocation to _alloca call for Cygwin/Mingw targets.
4976// Calls to _alloca is needed to probe the stack when allocating more than 4k
4977// bytes in one go. Touching the stack at 4K increments is necessary to ensure
4978// that the guard pages used by the OS virtual memory manager are allocated in
4979// correct sequence.
Dan Gohman475871a2008-07-27 21:46:04 +00004980SDValue
4981X86TargetLowering::LowerDYNAMIC_STACKALLOC(SDValue Op,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004982 SelectionDAG &DAG) {
Anton Korobeynikove060b532007-04-17 19:34:00 +00004983 assert(Subtarget->isTargetCygMing() &&
4984 "This should be used only on Cygwin/Mingw targets");
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004985
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004986 // Get the inputs.
Dan Gohman475871a2008-07-27 21:46:04 +00004987 SDValue Chain = Op.getOperand(0);
4988 SDValue Size = Op.getOperand(1);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004989 // FIXME: Ensure alignment here
4990
Dan Gohman475871a2008-07-27 21:46:04 +00004991 SDValue Flag;
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004992
Duncan Sands83ec4b62008-06-06 12:08:01 +00004993 MVT IntPtr = getPointerTy();
4994 MVT SPTy = Subtarget->is64Bit() ? MVT::i64 : MVT::i32;
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00004995
Anton Korobeynikov096b4612008-06-11 20:16:42 +00004996 Chain = DAG.getCALLSEQ_START(Chain, DAG.getIntPtrConstant(0));
4997
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00004998 Chain = DAG.getCopyToReg(Chain, X86::EAX, Size, Flag);
4999 Flag = Chain.getValue(1);
5000
5001 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005002 SDValue Ops[] = { Chain,
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005003 DAG.getTargetExternalSymbol("_alloca", IntPtr),
5004 DAG.getRegister(X86::EAX, IntPtr),
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005005 DAG.getRegister(X86StackPtr, SPTy),
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005006 Flag };
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005007 Chain = DAG.getNode(X86ISD::CALL, NodeTys, Ops, 5);
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005008 Flag = Chain.getValue(1);
5009
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005010 Chain = DAG.getCALLSEQ_END(Chain,
5011 DAG.getIntPtrConstant(0),
5012 DAG.getIntPtrConstant(0),
5013 Flag);
5014
Anton Korobeynikov4304bcc2007-07-05 20:36:08 +00005015 Chain = DAG.getCopyFromReg(Chain, X86StackPtr, SPTy).getValue(1);
Anton Korobeynikov096b4612008-06-11 20:16:42 +00005016
Dan Gohman475871a2008-07-27 21:46:04 +00005017 SDValue Ops1[2] = { Chain.getValue(0), Chain };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005018 return DAG.getMergeValues(Ops1, 2);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005019}
5020
Dan Gohman475871a2008-07-27 21:46:04 +00005021SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005022X86TargetLowering::EmitTargetCodeForMemset(SelectionDAG &DAG,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005023 SDValue Chain,
5024 SDValue Dst, SDValue Src,
5025 SDValue Size, unsigned Align,
Dan Gohman1f13c682008-04-28 17:15:20 +00005026 const Value *DstSV, uint64_t DstSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005027 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005028
Dan Gohman707e0182008-04-12 04:36:06 +00005029 /// If not DWORD aligned or size is more than the threshold, call the library.
5030 /// The libc version is likely to be faster for these cases. It can use the
5031 /// address value and run time information about the CPU.
Evan Cheng1887c1c2008-08-21 21:00:15 +00005032 if ((Align & 3) != 0 ||
Dan Gohman707e0182008-04-12 04:36:06 +00005033 !ConstantSize ||
5034 ConstantSize->getValue() > getSubtarget()->getMaxInlineSizeThreshold()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005035 SDValue InFlag(0, 0);
Dan Gohman68d599d2008-04-01 20:38:36 +00005036
5037 // Check to see if there is a specialized entry-point for memory zeroing.
Dan Gohman707e0182008-04-12 04:36:06 +00005038 ConstantSDNode *V = dyn_cast<ConstantSDNode>(Src);
5039 if (const char *bzeroEntry =
5040 V && V->isNullValue() ? Subtarget->getBZeroEntry() : 0) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005041 MVT IntPtr = getPointerTy();
Dan Gohman707e0182008-04-12 04:36:06 +00005042 const Type *IntPtrTy = getTargetData()->getIntPtrType();
5043 TargetLowering::ArgListTy Args;
5044 TargetLowering::ArgListEntry Entry;
5045 Entry.Node = Dst;
Dan Gohman68d599d2008-04-01 20:38:36 +00005046 Entry.Ty = IntPtrTy;
5047 Args.push_back(Entry);
Dan Gohman707e0182008-04-12 04:36:06 +00005048 Entry.Node = Size;
5049 Args.push_back(Entry);
Dan Gohman475871a2008-07-27 21:46:04 +00005050 std::pair<SDValue,SDValue> CallResult =
Dan Gohman707e0182008-04-12 04:36:06 +00005051 LowerCallTo(Chain, Type::VoidTy, false, false, false, CallingConv::C,
5052 false, DAG.getExternalSymbol(bzeroEntry, IntPtr),
5053 Args, DAG);
5054 return CallResult.second;
Dan Gohman68d599d2008-04-01 20:38:36 +00005055 }
5056
Dan Gohman707e0182008-04-12 04:36:06 +00005057 // Otherwise have the target-independent code call memset.
Dan Gohman475871a2008-07-27 21:46:04 +00005058 return SDValue();
Evan Cheng48090aa2006-03-21 23:01:21 +00005059 }
Evan Chengb9df0ca2006-03-22 02:53:00 +00005060
Dan Gohman707e0182008-04-12 04:36:06 +00005061 uint64_t SizeVal = ConstantSize->getValue();
Dan Gohman475871a2008-07-27 21:46:04 +00005062 SDValue InFlag(0, 0);
Duncan Sands83ec4b62008-06-06 12:08:01 +00005063 MVT AVT;
Dan Gohman475871a2008-07-27 21:46:04 +00005064 SDValue Count;
Dan Gohman707e0182008-04-12 04:36:06 +00005065 ConstantSDNode *ValC = dyn_cast<ConstantSDNode>(Src);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005066 unsigned BytesLeft = 0;
5067 bool TwoRepStos = false;
5068 if (ValC) {
5069 unsigned ValReg;
Evan Cheng25ab6902006-09-08 06:48:29 +00005070 uint64_t Val = ValC->getValue() & 255;
Evan Cheng5ced1d82006-04-06 23:23:56 +00005071
Evan Cheng0db9fe62006-04-25 20:13:52 +00005072 // If the value is a constant, then we can potentially use larger sets.
5073 switch (Align & 3) {
Evan Cheng1887c1c2008-08-21 21:00:15 +00005074 case 2: // WORD aligned
5075 AVT = MVT::i16;
5076 ValReg = X86::AX;
5077 Val = (Val << 8) | Val;
5078 break;
5079 case 0: // DWORD aligned
5080 AVT = MVT::i32;
5081 ValReg = X86::EAX;
5082 Val = (Val << 8) | Val;
5083 Val = (Val << 16) | Val;
5084 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) { // QWORD aligned
5085 AVT = MVT::i64;
5086 ValReg = X86::RAX;
5087 Val = (Val << 32) | Val;
5088 }
5089 break;
5090 default: // Byte aligned
5091 AVT = MVT::i8;
5092 ValReg = X86::AL;
5093 Count = DAG.getIntPtrConstant(SizeVal);
5094 break;
Evan Cheng80d428c2006-04-19 22:48:17 +00005095 }
5096
Duncan Sands8e4eb092008-06-08 20:54:56 +00005097 if (AVT.bitsGT(MVT::i8)) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005098 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005099 Count = DAG.getIntPtrConstant(SizeVal / UBytes);
5100 BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005101 }
5102
Evan Cheng0db9fe62006-04-25 20:13:52 +00005103 Chain = DAG.getCopyToReg(Chain, ValReg, DAG.getConstant(Val, AVT),
5104 InFlag);
5105 InFlag = Chain.getValue(1);
5106 } else {
5107 AVT = MVT::i8;
Dan Gohmanbcda2852008-04-16 01:32:32 +00005108 Count = DAG.getIntPtrConstant(SizeVal);
Dan Gohman707e0182008-04-12 04:36:06 +00005109 Chain = DAG.getCopyToReg(Chain, X86::AL, Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005110 InFlag = Chain.getValue(1);
Evan Chengb9df0ca2006-03-22 02:53:00 +00005111 }
Evan Chengc78d3b42006-04-24 18:01:45 +00005112
Evan Cheng25ab6902006-09-08 06:48:29 +00005113 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5114 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005115 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005116 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005117 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005118 InFlag = Chain.getValue(1);
Evan Chenga0b3afb2006-03-27 07:00:16 +00005119
Chris Lattnerd96d0722007-02-25 06:40:16 +00005120 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005121 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005122 Ops.push_back(Chain);
5123 Ops.push_back(DAG.getValueType(AVT));
5124 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005125 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Chengc78d3b42006-04-24 18:01:45 +00005126
Evan Cheng0db9fe62006-04-25 20:13:52 +00005127 if (TwoRepStos) {
5128 InFlag = Chain.getValue(1);
Dan Gohman707e0182008-04-12 04:36:06 +00005129 Count = Size;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005130 MVT CVT = Count.getValueType();
Dan Gohman475871a2008-07-27 21:46:04 +00005131 SDValue Left = DAG.getNode(ISD::AND, CVT, Count,
Evan Cheng25ab6902006-09-08 06:48:29 +00005132 DAG.getConstant((AVT == MVT::i64) ? 7 : 3, CVT));
5133 Chain = DAG.getCopyToReg(Chain, (CVT == MVT::i64) ? X86::RCX : X86::ECX,
5134 Left, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005135 InFlag = Chain.getValue(1);
Chris Lattnerd96d0722007-02-25 06:40:16 +00005136 Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005137 Ops.clear();
5138 Ops.push_back(Chain);
5139 Ops.push_back(DAG.getValueType(MVT::i8));
5140 Ops.push_back(InFlag);
Evan Cheng311ace02006-08-11 07:35:45 +00005141 Chain = DAG.getNode(X86ISD::REP_STOS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005142 } else if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005143 // Handle the last 1 - 7 bytes.
5144 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005145 MVT AddrVT = Dst.getValueType();
5146 MVT SizeVT = Size.getValueType();
Dan Gohman707e0182008-04-12 04:36:06 +00005147
5148 Chain = DAG.getMemset(Chain,
5149 DAG.getNode(ISD::ADD, AddrVT, Dst,
5150 DAG.getConstant(Offset, AddrVT)),
5151 Src,
5152 DAG.getConstant(BytesLeft, SizeVT),
Dan Gohman1f13c682008-04-28 17:15:20 +00005153 Align, DstSV, DstSVOff + Offset);
Evan Cheng386031a2006-03-24 07:29:27 +00005154 }
Evan Cheng11e15b32006-04-03 20:53:28 +00005155
Dan Gohman707e0182008-04-12 04:36:06 +00005156 // TODO: Use a Tokenfactor, as in memcpy, instead of a single chain.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005157 return Chain;
5158}
Evan Cheng11e15b32006-04-03 20:53:28 +00005159
Dan Gohman475871a2008-07-27 21:46:04 +00005160SDValue
Dan Gohman707e0182008-04-12 04:36:06 +00005161X86TargetLowering::EmitTargetCodeForMemcpy(SelectionDAG &DAG,
Evan Cheng1887c1c2008-08-21 21:00:15 +00005162 SDValue Chain, SDValue Dst, SDValue Src,
5163 SDValue Size, unsigned Align,
5164 bool AlwaysInline,
5165 const Value *DstSV, uint64_t DstSVOff,
5166 const Value *SrcSV, uint64_t SrcSVOff) {
Dan Gohman707e0182008-04-12 04:36:06 +00005167 // This requires the copy size to be a constant, preferrably
5168 // within a subtarget-specific limit.
5169 ConstantSDNode *ConstantSize = dyn_cast<ConstantSDNode>(Size);
5170 if (!ConstantSize)
Dan Gohman475871a2008-07-27 21:46:04 +00005171 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005172 uint64_t SizeVal = ConstantSize->getValue();
5173 if (!AlwaysInline && SizeVal > getSubtarget()->getMaxInlineSizeThreshold())
Dan Gohman475871a2008-07-27 21:46:04 +00005174 return SDValue();
Dan Gohman707e0182008-04-12 04:36:06 +00005175
Evan Cheng1887c1c2008-08-21 21:00:15 +00005176 /// If not DWORD aligned, call the library.
5177 if ((Align & 3) != 0)
5178 return SDValue();
5179
5180 // DWORD aligned
5181 MVT AVT = MVT::i32;
5182 if (Subtarget->is64Bit() && ((Align & 0x7) == 0)) // QWORD aligned
Dan Gohman707e0182008-04-12 04:36:06 +00005183 AVT = MVT::i64;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005184
Duncan Sands83ec4b62008-06-06 12:08:01 +00005185 unsigned UBytes = AVT.getSizeInBits() / 8;
Dan Gohman707e0182008-04-12 04:36:06 +00005186 unsigned CountVal = SizeVal / UBytes;
Dan Gohman475871a2008-07-27 21:46:04 +00005187 SDValue Count = DAG.getIntPtrConstant(CountVal);
Evan Cheng1887c1c2008-08-21 21:00:15 +00005188 unsigned BytesLeft = SizeVal % UBytes;
Evan Cheng25ab6902006-09-08 06:48:29 +00005189
Dan Gohman475871a2008-07-27 21:46:04 +00005190 SDValue InFlag(0, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005191 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RCX : X86::ECX,
5192 Count, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005193 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005194 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RDI : X86::EDI,
Dan Gohman707e0182008-04-12 04:36:06 +00005195 Dst, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005196 InFlag = Chain.getValue(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005197 Chain = DAG.getCopyToReg(Chain, Subtarget->is64Bit() ? X86::RSI : X86::ESI,
Dan Gohman707e0182008-04-12 04:36:06 +00005198 Src, InFlag);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005199 InFlag = Chain.getValue(1);
5200
Chris Lattnerd96d0722007-02-25 06:40:16 +00005201 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005202 SmallVector<SDValue, 8> Ops;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005203 Ops.push_back(Chain);
5204 Ops.push_back(DAG.getValueType(AVT));
5205 Ops.push_back(InFlag);
Dan Gohman475871a2008-07-27 21:46:04 +00005206 SDValue RepMovs = DAG.getNode(X86ISD::REP_MOVS, Tys, &Ops[0], Ops.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005207
Dan Gohman475871a2008-07-27 21:46:04 +00005208 SmallVector<SDValue, 4> Results;
Evan Cheng2749c722008-04-25 00:26:43 +00005209 Results.push_back(RepMovs);
Rafael Espindola068317b2007-09-28 12:53:01 +00005210 if (BytesLeft) {
Dan Gohman707e0182008-04-12 04:36:06 +00005211 // Handle the last 1 - 7 bytes.
5212 unsigned Offset = SizeVal - BytesLeft;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005213 MVT DstVT = Dst.getValueType();
5214 MVT SrcVT = Src.getValueType();
5215 MVT SizeVT = Size.getValueType();
Evan Cheng2749c722008-04-25 00:26:43 +00005216 Results.push_back(DAG.getMemcpy(Chain,
Dan Gohman707e0182008-04-12 04:36:06 +00005217 DAG.getNode(ISD::ADD, DstVT, Dst,
Evan Cheng2749c722008-04-25 00:26:43 +00005218 DAG.getConstant(Offset, DstVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005219 DAG.getNode(ISD::ADD, SrcVT, Src,
Evan Cheng2749c722008-04-25 00:26:43 +00005220 DAG.getConstant(Offset, SrcVT)),
Dan Gohman707e0182008-04-12 04:36:06 +00005221 DAG.getConstant(BytesLeft, SizeVT),
5222 Align, AlwaysInline,
Dan Gohman1f13c682008-04-28 17:15:20 +00005223 DstSV, DstSVOff + Offset,
5224 SrcSV, SrcSVOff + Offset));
Evan Chengb067a1e2006-03-31 19:22:53 +00005225 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005226
Dan Gohman707e0182008-04-12 04:36:06 +00005227 return DAG.getNode(ISD::TokenFactor, MVT::Other, &Results[0], Results.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005228}
5229
Chris Lattner27a6c732007-11-24 07:07:01 +00005230/// Expand the result of: i64,outchain = READCYCLECOUNTER inchain
5231SDNode *X86TargetLowering::ExpandREADCYCLECOUNTER(SDNode *N, SelectionDAG &DAG){
Chris Lattnerd96d0722007-02-25 06:40:16 +00005232 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005233 SDValue TheChain = N->getOperand(0);
5234 SDValue rd = DAG.getNode(X86ISD::RDTSC_DAG, Tys, &TheChain, 1);
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005235 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005236 SDValue rax = DAG.getCopyFromReg(rd, X86::RAX, MVT::i64, rd.getValue(1));
5237 SDValue rdx = DAG.getCopyFromReg(rax.getValue(1), X86::RDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005238 MVT::i64, rax.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00005239 SDValue Tmp = DAG.getNode(ISD::SHL, MVT::i64, rdx,
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005240 DAG.getConstant(32, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00005241 SDValue Ops[] = {
Chris Lattner27a6c732007-11-24 07:07:01 +00005242 DAG.getNode(ISD::OR, MVT::i64, rax, Tmp), rdx.getValue(1)
Chris Lattner5a88b832007-02-25 07:10:00 +00005243 };
Chris Lattnerd96d0722007-02-25 06:40:16 +00005244
Duncan Sands4bdcb612008-07-02 17:40:58 +00005245 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng3fa9dff2006-11-29 08:28:13 +00005246 }
Chris Lattner5a88b832007-02-25 07:10:00 +00005247
Dan Gohman475871a2008-07-27 21:46:04 +00005248 SDValue eax = DAG.getCopyFromReg(rd, X86::EAX, MVT::i32, rd.getValue(1));
5249 SDValue edx = DAG.getCopyFromReg(eax.getValue(1), X86::EDX,
Chris Lattner27a6c732007-11-24 07:07:01 +00005250 MVT::i32, eax.getValue(2));
5251 // Use a buildpair to merge the two 32-bit values into a 64-bit one.
Dan Gohman475871a2008-07-27 21:46:04 +00005252 SDValue Ops[] = { eax, edx };
Chris Lattner27a6c732007-11-24 07:07:01 +00005253 Ops[0] = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, Ops, 2);
5254
5255 // Use a MERGE_VALUES to return the value and chain.
5256 Ops[1] = edx.getValue(1);
Duncan Sands4bdcb612008-07-02 17:40:58 +00005257 return DAG.getMergeValues(Ops, 2).Val;
Evan Cheng0db9fe62006-04-25 20:13:52 +00005258}
5259
Dan Gohman475871a2008-07-27 21:46:04 +00005260SDValue X86TargetLowering::LowerVASTART(SDValue Op, SelectionDAG &DAG) {
Dan Gohman69de1932008-02-06 22:27:42 +00005261 const Value *SV = cast<SrcValueSDNode>(Op.getOperand(2))->getValue();
Evan Cheng8b2794a2006-10-13 21:14:26 +00005262
Evan Cheng25ab6902006-09-08 06:48:29 +00005263 if (!Subtarget->is64Bit()) {
5264 // vastart just stores the address of the VarArgsFrameIndex slot into the
5265 // memory location argument.
Dan Gohman475871a2008-07-27 21:46:04 +00005266 SDValue FR = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005267 return DAG.getStore(Op.getOperand(0), FR,Op.getOperand(1), SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005268 }
5269
5270 // __va_list_tag:
5271 // gp_offset (0 - 6 * 8)
5272 // fp_offset (48 - 48 + 8 * 16)
5273 // overflow_arg_area (point to parameters coming in memory).
5274 // reg_save_area
Dan Gohman475871a2008-07-27 21:46:04 +00005275 SmallVector<SDValue, 8> MemOps;
5276 SDValue FIN = Op.getOperand(1);
Evan Cheng25ab6902006-09-08 06:48:29 +00005277 // Store gp_offset
Dan Gohman475871a2008-07-27 21:46:04 +00005278 SDValue Store = DAG.getStore(Op.getOperand(0),
Evan Cheng786225a2006-10-05 23:01:46 +00005279 DAG.getConstant(VarArgsGPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005280 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005281 MemOps.push_back(Store);
5282
5283 // Store fp_offset
Chris Lattner0bd48932008-01-17 07:00:52 +00005284 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Evan Cheng786225a2006-10-05 23:01:46 +00005285 Store = DAG.getStore(Op.getOperand(0),
5286 DAG.getConstant(VarArgsFPOffset, MVT::i32),
Dan Gohman69de1932008-02-06 22:27:42 +00005287 FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005288 MemOps.push_back(Store);
5289
5290 // Store ptr to overflow_arg_area
Chris Lattner0bd48932008-01-17 07:00:52 +00005291 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(4));
Dan Gohman475871a2008-07-27 21:46:04 +00005292 SDValue OVFIN = DAG.getFrameIndex(VarArgsFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005293 Store = DAG.getStore(Op.getOperand(0), OVFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005294 MemOps.push_back(Store);
5295
5296 // Store ptr to reg_save_area.
Chris Lattner0bd48932008-01-17 07:00:52 +00005297 FIN = DAG.getNode(ISD::ADD, getPointerTy(), FIN, DAG.getIntPtrConstant(8));
Dan Gohman475871a2008-07-27 21:46:04 +00005298 SDValue RSFIN = DAG.getFrameIndex(RegSaveFrameIndex, getPointerTy());
Dan Gohman69de1932008-02-06 22:27:42 +00005299 Store = DAG.getStore(Op.getOperand(0), RSFIN, FIN, SV, 0);
Evan Cheng25ab6902006-09-08 06:48:29 +00005300 MemOps.push_back(Store);
5301 return DAG.getNode(ISD::TokenFactor, MVT::Other, &MemOps[0], MemOps.size());
Evan Cheng0db9fe62006-04-25 20:13:52 +00005302}
5303
Dan Gohman475871a2008-07-27 21:46:04 +00005304SDValue X86TargetLowering::LowerVAARG(SDValue Op, SelectionDAG &DAG) {
Dan Gohman9018e832008-05-10 01:26:14 +00005305 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
5306 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_arg!");
Dan Gohman475871a2008-07-27 21:46:04 +00005307 SDValue Chain = Op.getOperand(0);
5308 SDValue SrcPtr = Op.getOperand(1);
5309 SDValue SrcSV = Op.getOperand(2);
Dan Gohman9018e832008-05-10 01:26:14 +00005310
5311 assert(0 && "VAArgInst is not yet implemented for x86-64!");
5312 abort();
Dan Gohman475871a2008-07-27 21:46:04 +00005313 return SDValue();
Dan Gohman9018e832008-05-10 01:26:14 +00005314}
5315
Dan Gohman475871a2008-07-27 21:46:04 +00005316SDValue X86TargetLowering::LowerVACOPY(SDValue Op, SelectionDAG &DAG) {
Evan Chengae642192007-03-02 23:16:35 +00005317 // X86-64 va_list is a struct { i32, i32, i8*, i8* }.
Dan Gohman28269132008-04-18 20:55:41 +00005318 assert(Subtarget->is64Bit() && "This code only handles 64-bit va_copy!");
Dan Gohman475871a2008-07-27 21:46:04 +00005319 SDValue Chain = Op.getOperand(0);
5320 SDValue DstPtr = Op.getOperand(1);
5321 SDValue SrcPtr = Op.getOperand(2);
Dan Gohman69de1932008-02-06 22:27:42 +00005322 const Value *DstSV = cast<SrcValueSDNode>(Op.getOperand(3))->getValue();
5323 const Value *SrcSV = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Evan Chengae642192007-03-02 23:16:35 +00005324
Dan Gohman28269132008-04-18 20:55:41 +00005325 return DAG.getMemcpy(Chain, DstPtr, SrcPtr,
5326 DAG.getIntPtrConstant(24), 8, false,
5327 DstSV, 0, SrcSV, 0);
Evan Chengae642192007-03-02 23:16:35 +00005328}
5329
Dan Gohman475871a2008-07-27 21:46:04 +00005330SDValue
5331X86TargetLowering::LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005332 unsigned IntNo = cast<ConstantSDNode>(Op.getOperand(0))->getValue();
5333 switch (IntNo) {
Dan Gohman475871a2008-07-27 21:46:04 +00005334 default: return SDValue(); // Don't custom lower most intrinsics.
Evan Cheng5759f972008-05-04 09:15:50 +00005335 // Comparison intrinsics.
Evan Cheng0db9fe62006-04-25 20:13:52 +00005336 case Intrinsic::x86_sse_comieq_ss:
5337 case Intrinsic::x86_sse_comilt_ss:
5338 case Intrinsic::x86_sse_comile_ss:
5339 case Intrinsic::x86_sse_comigt_ss:
5340 case Intrinsic::x86_sse_comige_ss:
5341 case Intrinsic::x86_sse_comineq_ss:
5342 case Intrinsic::x86_sse_ucomieq_ss:
5343 case Intrinsic::x86_sse_ucomilt_ss:
5344 case Intrinsic::x86_sse_ucomile_ss:
5345 case Intrinsic::x86_sse_ucomigt_ss:
5346 case Intrinsic::x86_sse_ucomige_ss:
5347 case Intrinsic::x86_sse_ucomineq_ss:
5348 case Intrinsic::x86_sse2_comieq_sd:
5349 case Intrinsic::x86_sse2_comilt_sd:
5350 case Intrinsic::x86_sse2_comile_sd:
5351 case Intrinsic::x86_sse2_comigt_sd:
5352 case Intrinsic::x86_sse2_comige_sd:
5353 case Intrinsic::x86_sse2_comineq_sd:
5354 case Intrinsic::x86_sse2_ucomieq_sd:
5355 case Intrinsic::x86_sse2_ucomilt_sd:
5356 case Intrinsic::x86_sse2_ucomile_sd:
5357 case Intrinsic::x86_sse2_ucomigt_sd:
5358 case Intrinsic::x86_sse2_ucomige_sd:
5359 case Intrinsic::x86_sse2_ucomineq_sd: {
5360 unsigned Opc = 0;
5361 ISD::CondCode CC = ISD::SETCC_INVALID;
5362 switch (IntNo) {
5363 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00005364 case Intrinsic::x86_sse_comieq_ss:
5365 case Intrinsic::x86_sse2_comieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005366 Opc = X86ISD::COMI;
5367 CC = ISD::SETEQ;
5368 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005369 case Intrinsic::x86_sse_comilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005370 case Intrinsic::x86_sse2_comilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005371 Opc = X86ISD::COMI;
5372 CC = ISD::SETLT;
5373 break;
5374 case Intrinsic::x86_sse_comile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005375 case Intrinsic::x86_sse2_comile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005376 Opc = X86ISD::COMI;
5377 CC = ISD::SETLE;
5378 break;
5379 case Intrinsic::x86_sse_comigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005380 case Intrinsic::x86_sse2_comigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005381 Opc = X86ISD::COMI;
5382 CC = ISD::SETGT;
5383 break;
5384 case Intrinsic::x86_sse_comige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005385 case Intrinsic::x86_sse2_comige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005386 Opc = X86ISD::COMI;
5387 CC = ISD::SETGE;
5388 break;
5389 case Intrinsic::x86_sse_comineq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005390 case Intrinsic::x86_sse2_comineq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005391 Opc = X86ISD::COMI;
5392 CC = ISD::SETNE;
5393 break;
5394 case Intrinsic::x86_sse_ucomieq_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005395 case Intrinsic::x86_sse2_ucomieq_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005396 Opc = X86ISD::UCOMI;
5397 CC = ISD::SETEQ;
5398 break;
5399 case Intrinsic::x86_sse_ucomilt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005400 case Intrinsic::x86_sse2_ucomilt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005401 Opc = X86ISD::UCOMI;
5402 CC = ISD::SETLT;
5403 break;
5404 case Intrinsic::x86_sse_ucomile_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005405 case Intrinsic::x86_sse2_ucomile_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005406 Opc = X86ISD::UCOMI;
5407 CC = ISD::SETLE;
5408 break;
5409 case Intrinsic::x86_sse_ucomigt_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005410 case Intrinsic::x86_sse2_ucomigt_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005411 Opc = X86ISD::UCOMI;
5412 CC = ISD::SETGT;
5413 break;
5414 case Intrinsic::x86_sse_ucomige_ss:
Evan Cheng6be2c582006-04-05 23:38:46 +00005415 case Intrinsic::x86_sse2_ucomige_sd:
Evan Cheng0db9fe62006-04-25 20:13:52 +00005416 Opc = X86ISD::UCOMI;
5417 CC = ISD::SETGE;
5418 break;
5419 case Intrinsic::x86_sse_ucomineq_ss:
5420 case Intrinsic::x86_sse2_ucomineq_sd:
5421 Opc = X86ISD::UCOMI;
5422 CC = ISD::SETNE;
5423 break;
Evan Cheng6be2c582006-04-05 23:38:46 +00005424 }
Evan Cheng734503b2006-09-11 02:19:56 +00005425
Evan Cheng0db9fe62006-04-25 20:13:52 +00005426 unsigned X86CC;
Dan Gohman475871a2008-07-27 21:46:04 +00005427 SDValue LHS = Op.getOperand(1);
5428 SDValue RHS = Op.getOperand(2);
Chris Lattnerf9570512006-09-13 03:22:10 +00005429 translateX86CC(CC, true, X86CC, LHS, RHS, DAG);
Evan Cheng734503b2006-09-11 02:19:56 +00005430
Dan Gohman475871a2008-07-27 21:46:04 +00005431 SDValue Cond = DAG.getNode(Opc, MVT::i32, LHS, RHS);
5432 SDValue SetCC = DAG.getNode(X86ISD::SETCC, MVT::i8,
Evan Cheng0ac3fc22008-08-17 19:22:34 +00005433 DAG.getConstant(X86CC, MVT::i8), Cond);
5434 return DAG.getNode(ISD::ZERO_EXTEND, MVT::i32, SetCC);
Evan Cheng6be2c582006-04-05 23:38:46 +00005435 }
Evan Cheng5759f972008-05-04 09:15:50 +00005436
5437 // Fix vector shift instructions where the last operand is a non-immediate
5438 // i32 value.
5439 case Intrinsic::x86_sse2_pslli_w:
5440 case Intrinsic::x86_sse2_pslli_d:
5441 case Intrinsic::x86_sse2_pslli_q:
5442 case Intrinsic::x86_sse2_psrli_w:
5443 case Intrinsic::x86_sse2_psrli_d:
5444 case Intrinsic::x86_sse2_psrli_q:
5445 case Intrinsic::x86_sse2_psrai_w:
5446 case Intrinsic::x86_sse2_psrai_d:
5447 case Intrinsic::x86_mmx_pslli_w:
5448 case Intrinsic::x86_mmx_pslli_d:
5449 case Intrinsic::x86_mmx_pslli_q:
5450 case Intrinsic::x86_mmx_psrli_w:
5451 case Intrinsic::x86_mmx_psrli_d:
5452 case Intrinsic::x86_mmx_psrli_q:
5453 case Intrinsic::x86_mmx_psrai_w:
5454 case Intrinsic::x86_mmx_psrai_d: {
Dan Gohman475871a2008-07-27 21:46:04 +00005455 SDValue ShAmt = Op.getOperand(2);
Evan Cheng5759f972008-05-04 09:15:50 +00005456 if (isa<ConstantSDNode>(ShAmt))
Dan Gohman475871a2008-07-27 21:46:04 +00005457 return SDValue();
Evan Cheng5759f972008-05-04 09:15:50 +00005458
5459 unsigned NewIntNo = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005460 MVT ShAmtVT = MVT::v4i32;
Evan Cheng5759f972008-05-04 09:15:50 +00005461 switch (IntNo) {
5462 case Intrinsic::x86_sse2_pslli_w:
5463 NewIntNo = Intrinsic::x86_sse2_psll_w;
5464 break;
5465 case Intrinsic::x86_sse2_pslli_d:
5466 NewIntNo = Intrinsic::x86_sse2_psll_d;
5467 break;
5468 case Intrinsic::x86_sse2_pslli_q:
5469 NewIntNo = Intrinsic::x86_sse2_psll_q;
5470 break;
5471 case Intrinsic::x86_sse2_psrli_w:
5472 NewIntNo = Intrinsic::x86_sse2_psrl_w;
5473 break;
5474 case Intrinsic::x86_sse2_psrli_d:
5475 NewIntNo = Intrinsic::x86_sse2_psrl_d;
5476 break;
5477 case Intrinsic::x86_sse2_psrli_q:
5478 NewIntNo = Intrinsic::x86_sse2_psrl_q;
5479 break;
5480 case Intrinsic::x86_sse2_psrai_w:
5481 NewIntNo = Intrinsic::x86_sse2_psra_w;
5482 break;
5483 case Intrinsic::x86_sse2_psrai_d:
5484 NewIntNo = Intrinsic::x86_sse2_psra_d;
5485 break;
5486 default: {
5487 ShAmtVT = MVT::v2i32;
5488 switch (IntNo) {
5489 case Intrinsic::x86_mmx_pslli_w:
5490 NewIntNo = Intrinsic::x86_mmx_psll_w;
5491 break;
5492 case Intrinsic::x86_mmx_pslli_d:
5493 NewIntNo = Intrinsic::x86_mmx_psll_d;
5494 break;
5495 case Intrinsic::x86_mmx_pslli_q:
5496 NewIntNo = Intrinsic::x86_mmx_psll_q;
5497 break;
5498 case Intrinsic::x86_mmx_psrli_w:
5499 NewIntNo = Intrinsic::x86_mmx_psrl_w;
5500 break;
5501 case Intrinsic::x86_mmx_psrli_d:
5502 NewIntNo = Intrinsic::x86_mmx_psrl_d;
5503 break;
5504 case Intrinsic::x86_mmx_psrli_q:
5505 NewIntNo = Intrinsic::x86_mmx_psrl_q;
5506 break;
5507 case Intrinsic::x86_mmx_psrai_w:
5508 NewIntNo = Intrinsic::x86_mmx_psra_w;
5509 break;
5510 case Intrinsic::x86_mmx_psrai_d:
5511 NewIntNo = Intrinsic::x86_mmx_psra_d;
5512 break;
5513 default: abort(); // Can't reach here.
5514 }
5515 break;
5516 }
5517 }
Duncan Sands83ec4b62008-06-06 12:08:01 +00005518 MVT VT = Op.getValueType();
Evan Cheng5759f972008-05-04 09:15:50 +00005519 ShAmt = DAG.getNode(ISD::BIT_CONVERT, VT,
5520 DAG.getNode(ISD::SCALAR_TO_VECTOR, ShAmtVT, ShAmt));
5521 return DAG.getNode(ISD::INTRINSIC_WO_CHAIN, VT,
5522 DAG.getConstant(NewIntNo, MVT::i32),
5523 Op.getOperand(1), ShAmt);
5524 }
Evan Cheng38bcbaf2005-12-23 07:31:11 +00005525 }
Chris Lattnerdbdbf0c2005-11-15 00:40:23 +00005526}
Evan Cheng72261582005-12-20 06:22:03 +00005527
Dan Gohman475871a2008-07-27 21:46:04 +00005528SDValue X86TargetLowering::LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) {
Nate Begemanbcc5f362007-01-29 22:58:52 +00005529 // Depths > 0 not supported yet!
5530 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005531 return SDValue();
Nate Begemanbcc5f362007-01-29 22:58:52 +00005532
5533 // Just load the return address
Dan Gohman475871a2008-07-27 21:46:04 +00005534 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005535 return DAG.getLoad(getPointerTy(), DAG.getEntryNode(), RetAddrFI, NULL, 0);
5536}
5537
Dan Gohman475871a2008-07-27 21:46:04 +00005538SDValue X86TargetLowering::LowerFRAMEADDR(SDValue Op, SelectionDAG &DAG) {
Nate Begemanbcc5f362007-01-29 22:58:52 +00005539 // Depths > 0 not supported yet!
5540 if (cast<ConstantSDNode>(Op.getOperand(0))->getValue() > 0)
Dan Gohman475871a2008-07-27 21:46:04 +00005541 return SDValue();
Nate Begemanbcc5f362007-01-29 22:58:52 +00005542
Dan Gohman475871a2008-07-27 21:46:04 +00005543 SDValue RetAddrFI = getReturnAddressFrameIndex(DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005544 return DAG.getNode(ISD::SUB, getPointerTy(), RetAddrFI,
Bill Wendling71ca3532008-07-11 07:18:52 +00005545 DAG.getIntPtrConstant(!Subtarget->is64Bit() ? 4 : 8));
Nate Begemanbcc5f362007-01-29 22:58:52 +00005546}
5547
Dan Gohman475871a2008-07-27 21:46:04 +00005548SDValue X86TargetLowering::LowerFRAME_TO_ARGS_OFFSET(SDValue Op,
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005549 SelectionDAG &DAG) {
5550 // Is not yet supported on x86-64
5551 if (Subtarget->is64Bit())
Dan Gohman475871a2008-07-27 21:46:04 +00005552 return SDValue();
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005553
Chris Lattner0bd48932008-01-17 07:00:52 +00005554 return DAG.getIntPtrConstant(8);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005555}
5556
Dan Gohman475871a2008-07-27 21:46:04 +00005557SDValue X86TargetLowering::LowerEH_RETURN(SDValue Op, SelectionDAG &DAG)
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005558{
5559 assert(!Subtarget->is64Bit() &&
5560 "Lowering of eh_return builtin is not supported yet on x86-64");
5561
5562 MachineFunction &MF = DAG.getMachineFunction();
Dan Gohman475871a2008-07-27 21:46:04 +00005563 SDValue Chain = Op.getOperand(0);
5564 SDValue Offset = Op.getOperand(1);
5565 SDValue Handler = Op.getOperand(2);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005566
Dan Gohman475871a2008-07-27 21:46:04 +00005567 SDValue Frame = DAG.getRegister(RegInfo->getFrameRegister(MF),
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005568 getPointerTy());
5569
Dan Gohman475871a2008-07-27 21:46:04 +00005570 SDValue StoreAddr = DAG.getNode(ISD::SUB, getPointerTy(), Frame,
Chris Lattner0bd48932008-01-17 07:00:52 +00005571 DAG.getIntPtrConstant(-4UL));
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005572 StoreAddr = DAG.getNode(ISD::ADD, getPointerTy(), StoreAddr, Offset);
5573 Chain = DAG.getStore(Chain, Handler, StoreAddr, NULL, 0);
5574 Chain = DAG.getCopyToReg(Chain, X86::ECX, StoreAddr);
Chris Lattner84bc5422007-12-31 04:13:23 +00005575 MF.getRegInfo().addLiveOut(X86::ECX);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005576
5577 return DAG.getNode(X86ISD::EH_RETURN, MVT::Other,
5578 Chain, DAG.getRegister(X86::ECX, getPointerTy()));
5579}
5580
Dan Gohman475871a2008-07-27 21:46:04 +00005581SDValue X86TargetLowering::LowerTRAMPOLINE(SDValue Op,
Duncan Sandsb116fac2007-07-27 20:02:49 +00005582 SelectionDAG &DAG) {
Dan Gohman475871a2008-07-27 21:46:04 +00005583 SDValue Root = Op.getOperand(0);
5584 SDValue Trmp = Op.getOperand(1); // trampoline
5585 SDValue FPtr = Op.getOperand(2); // nested function
5586 SDValue Nest = Op.getOperand(3); // 'nest' parameter value
Duncan Sandsb116fac2007-07-27 20:02:49 +00005587
Dan Gohman69de1932008-02-06 22:27:42 +00005588 const Value *TrmpAddr = cast<SrcValueSDNode>(Op.getOperand(4))->getValue();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005589
Duncan Sands339e14f2008-01-16 22:55:25 +00005590 const X86InstrInfo *TII =
5591 ((X86TargetMachine&)getTargetMachine()).getInstrInfo();
5592
Duncan Sandsb116fac2007-07-27 20:02:49 +00005593 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00005594 SDValue OutChains[6];
Duncan Sands339e14f2008-01-16 22:55:25 +00005595
5596 // Large code-model.
5597
5598 const unsigned char JMP64r = TII->getBaseOpcodeFor(X86::JMP64r);
5599 const unsigned char MOV64ri = TII->getBaseOpcodeFor(X86::MOV64ri);
5600
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005601 const unsigned char N86R10 = RegInfo->getX86RegNum(X86::R10);
5602 const unsigned char N86R11 = RegInfo->getX86RegNum(X86::R11);
Duncan Sands339e14f2008-01-16 22:55:25 +00005603
5604 const unsigned char REX_WB = 0x40 | 0x08 | 0x01; // REX prefix
5605
5606 // Load the pointer to the nested function into R11.
5607 unsigned OpCode = ((MOV64ri | N86R11) << 8) | REX_WB; // movabsq r11
Dan Gohman475871a2008-07-27 21:46:04 +00005608 SDValue Addr = Trmp;
Duncan Sands339e14f2008-01-16 22:55:25 +00005609 OutChains[0] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005610 TrmpAddr, 0);
Duncan Sands339e14f2008-01-16 22:55:25 +00005611
5612 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(2, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005613 OutChains[1] = DAG.getStore(Root, FPtr, Addr, TrmpAddr, 2, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005614
5615 // Load the 'nest' parameter value into R10.
5616 // R10 is specified in X86CallingConv.td
5617 OpCode = ((MOV64ri | N86R10) << 8) | REX_WB; // movabsq r10
5618 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(10, MVT::i64));
5619 OutChains[2] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005620 TrmpAddr, 10);
Duncan Sands339e14f2008-01-16 22:55:25 +00005621
5622 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(12, MVT::i64));
Dan Gohman69de1932008-02-06 22:27:42 +00005623 OutChains[3] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 12, false, 2);
Duncan Sands339e14f2008-01-16 22:55:25 +00005624
5625 // Jump to the nested function.
5626 OpCode = (JMP64r << 8) | REX_WB; // jmpq *...
5627 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(20, MVT::i64));
5628 OutChains[4] = DAG.getStore(Root, DAG.getConstant(OpCode, MVT::i16), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005629 TrmpAddr, 20);
Duncan Sands339e14f2008-01-16 22:55:25 +00005630
5631 unsigned char ModRM = N86R11 | (4 << 3) | (3 << 6); // ...r11
5632 Addr = DAG.getNode(ISD::ADD, MVT::i64, Trmp, DAG.getConstant(22, MVT::i64));
5633 OutChains[5] = DAG.getStore(Root, DAG.getConstant(ModRM, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005634 TrmpAddr, 22);
Duncan Sands339e14f2008-01-16 22:55:25 +00005635
Dan Gohman475871a2008-07-27 21:46:04 +00005636 SDValue Ops[] =
Duncan Sands339e14f2008-01-16 22:55:25 +00005637 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 6) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005638 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005639 } else {
Dan Gohmanbbfb9c52008-01-31 01:01:48 +00005640 const Function *Func =
Duncan Sandsb116fac2007-07-27 20:02:49 +00005641 cast<Function>(cast<SrcValueSDNode>(Op.getOperand(5))->getValue());
5642 unsigned CC = Func->getCallingConv();
Duncan Sandsee465742007-08-29 19:01:20 +00005643 unsigned NestReg;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005644
5645 switch (CC) {
5646 default:
5647 assert(0 && "Unsupported calling convention");
5648 case CallingConv::C:
Duncan Sandsb116fac2007-07-27 20:02:49 +00005649 case CallingConv::X86_StdCall: {
5650 // Pass 'nest' parameter in ECX.
5651 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005652 NestReg = X86::ECX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005653
5654 // Check that ECX wasn't needed by an 'inreg' parameter.
5655 const FunctionType *FTy = Func->getFunctionType();
Chris Lattner58d74912008-03-12 17:45:29 +00005656 const PAListPtr &Attrs = Func->getParamAttrs();
Duncan Sandsb116fac2007-07-27 20:02:49 +00005657
Chris Lattner58d74912008-03-12 17:45:29 +00005658 if (!Attrs.isEmpty() && !Func->isVarArg()) {
Duncan Sandsb116fac2007-07-27 20:02:49 +00005659 unsigned InRegCount = 0;
5660 unsigned Idx = 1;
5661
5662 for (FunctionType::param_iterator I = FTy->param_begin(),
5663 E = FTy->param_end(); I != E; ++I, ++Idx)
Chris Lattner58d74912008-03-12 17:45:29 +00005664 if (Attrs.paramHasAttr(Idx, ParamAttr::InReg))
Duncan Sandsb116fac2007-07-27 20:02:49 +00005665 // FIXME: should only count parameters that are lowered to integers.
5666 InRegCount += (getTargetData()->getTypeSizeInBits(*I) + 31) / 32;
5667
5668 if (InRegCount > 2) {
5669 cerr << "Nest register in use - reduce number of inreg parameters!\n";
5670 abort();
5671 }
5672 }
5673 break;
5674 }
5675 case CallingConv::X86_FastCall:
5676 // Pass 'nest' parameter in EAX.
5677 // Must be kept in sync with X86CallingConv.td
Duncan Sandsee465742007-08-29 19:01:20 +00005678 NestReg = X86::EAX;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005679 break;
5680 }
5681
Dan Gohman475871a2008-07-27 21:46:04 +00005682 SDValue OutChains[4];
5683 SDValue Addr, Disp;
Duncan Sandsb116fac2007-07-27 20:02:49 +00005684
5685 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(10, MVT::i32));
5686 Disp = DAG.getNode(ISD::SUB, MVT::i32, FPtr, Addr);
5687
Duncan Sands339e14f2008-01-16 22:55:25 +00005688 const unsigned char MOV32ri = TII->getBaseOpcodeFor(X86::MOV32ri);
Dan Gohmanc9f5f3f2008-05-14 01:58:56 +00005689 const unsigned char N86Reg = RegInfo->getX86RegNum(NestReg);
Duncan Sandsee465742007-08-29 19:01:20 +00005690 OutChains[0] = DAG.getStore(Root, DAG.getConstant(MOV32ri|N86Reg, MVT::i8),
Dan Gohman69de1932008-02-06 22:27:42 +00005691 Trmp, TrmpAddr, 0);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005692
5693 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(1, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005694 OutChains[1] = DAG.getStore(Root, Nest, Addr, TrmpAddr, 1, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005695
Duncan Sands339e14f2008-01-16 22:55:25 +00005696 const unsigned char JMP = TII->getBaseOpcodeFor(X86::JMP);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005697 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(5, MVT::i32));
5698 OutChains[2] = DAG.getStore(Root, DAG.getConstant(JMP, MVT::i8), Addr,
Dan Gohman69de1932008-02-06 22:27:42 +00005699 TrmpAddr, 5, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005700
5701 Addr = DAG.getNode(ISD::ADD, MVT::i32, Trmp, DAG.getConstant(6, MVT::i32));
Dan Gohman69de1932008-02-06 22:27:42 +00005702 OutChains[3] = DAG.getStore(Root, Disp, Addr, TrmpAddr, 6, false, 1);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005703
Dan Gohman475871a2008-07-27 21:46:04 +00005704 SDValue Ops[] =
Duncan Sandsf7331b32007-09-11 14:10:23 +00005705 { Trmp, DAG.getNode(ISD::TokenFactor, MVT::Other, OutChains, 4) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005706 return DAG.getMergeValues(Ops, 2);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005707 }
5708}
5709
Dan Gohman475871a2008-07-27 21:46:04 +00005710SDValue X86TargetLowering::LowerFLT_ROUNDS_(SDValue Op, SelectionDAG &DAG) {
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005711 /*
5712 The rounding mode is in bits 11:10 of FPSR, and has the following
5713 settings:
5714 00 Round to nearest
5715 01 Round to -inf
5716 10 Round to +inf
5717 11 Round to 0
5718
5719 FLT_ROUNDS, on the other hand, expects the following:
5720 -1 Undefined
5721 0 Round to 0
5722 1 Round to nearest
5723 2 Round to +inf
5724 3 Round to -inf
5725
5726 To perform the conversion, we do:
5727 (((((FPSR & 0x800) >> 11) | ((FPSR & 0x400) >> 9)) + 1) & 3)
5728 */
5729
5730 MachineFunction &MF = DAG.getMachineFunction();
5731 const TargetMachine &TM = MF.getTarget();
5732 const TargetFrameInfo &TFI = *TM.getFrameInfo();
5733 unsigned StackAlignment = TFI.getStackAlignment();
Duncan Sands83ec4b62008-06-06 12:08:01 +00005734 MVT VT = Op.getValueType();
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005735
5736 // Save FP Control Word to stack slot
5737 int SSFI = MF.getFrameInfo()->CreateStackObject(2, StackAlignment);
Dan Gohman475871a2008-07-27 21:46:04 +00005738 SDValue StackSlot = DAG.getFrameIndex(SSFI, getPointerTy());
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005739
Dan Gohman475871a2008-07-27 21:46:04 +00005740 SDValue Chain = DAG.getNode(X86ISD::FNSTCW16m, MVT::Other,
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005741 DAG.getEntryNode(), StackSlot);
5742
5743 // Load FP Control Word from stack slot
Dan Gohman475871a2008-07-27 21:46:04 +00005744 SDValue CWD = DAG.getLoad(MVT::i16, Chain, StackSlot, NULL, 0);
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005745
5746 // Transform as necessary
Dan Gohman475871a2008-07-27 21:46:04 +00005747 SDValue CWD1 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005748 DAG.getNode(ISD::SRL, MVT::i16,
5749 DAG.getNode(ISD::AND, MVT::i16,
5750 CWD, DAG.getConstant(0x800, MVT::i16)),
5751 DAG.getConstant(11, MVT::i8));
Dan Gohman475871a2008-07-27 21:46:04 +00005752 SDValue CWD2 =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005753 DAG.getNode(ISD::SRL, MVT::i16,
5754 DAG.getNode(ISD::AND, MVT::i16,
5755 CWD, DAG.getConstant(0x400, MVT::i16)),
5756 DAG.getConstant(9, MVT::i8));
5757
Dan Gohman475871a2008-07-27 21:46:04 +00005758 SDValue RetVal =
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005759 DAG.getNode(ISD::AND, MVT::i16,
5760 DAG.getNode(ISD::ADD, MVT::i16,
5761 DAG.getNode(ISD::OR, MVT::i16, CWD1, CWD2),
5762 DAG.getConstant(1, MVT::i16)),
5763 DAG.getConstant(3, MVT::i16));
5764
5765
Duncan Sands83ec4b62008-06-06 12:08:01 +00005766 return DAG.getNode((VT.getSizeInBits() < 16 ?
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00005767 ISD::TRUNCATE : ISD::ZERO_EXTEND), VT, RetVal);
5768}
5769
Dan Gohman475871a2008-07-27 21:46:04 +00005770SDValue X86TargetLowering::LowerCTLZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005771 MVT VT = Op.getValueType();
5772 MVT OpVT = VT;
5773 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005774
5775 Op = Op.getOperand(0);
5776 if (VT == MVT::i8) {
Evan Cheng152804e2007-12-14 08:30:15 +00005777 // Zero extend to i32 since there is not an i8 bsr.
Evan Cheng18efe262007-12-14 02:13:44 +00005778 OpVT = MVT::i32;
5779 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5780 }
Evan Cheng18efe262007-12-14 02:13:44 +00005781
Evan Cheng152804e2007-12-14 08:30:15 +00005782 // Issue a bsr (scan bits in reverse) which also sets EFLAGS.
5783 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5784 Op = DAG.getNode(X86ISD::BSR, VTs, Op);
5785
5786 // If src is zero (i.e. bsr sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00005787 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00005788 Ops.push_back(Op);
5789 Ops.push_back(DAG.getConstant(NumBits+NumBits-1, OpVT));
5790 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5791 Ops.push_back(Op.getValue(1));
5792 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5793
5794 // Finally xor with NumBits-1.
5795 Op = DAG.getNode(ISD::XOR, OpVT, Op, DAG.getConstant(NumBits-1, OpVT));
5796
Evan Cheng18efe262007-12-14 02:13:44 +00005797 if (VT == MVT::i8)
5798 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5799 return Op;
5800}
5801
Dan Gohman475871a2008-07-27 21:46:04 +00005802SDValue X86TargetLowering::LowerCTTZ(SDValue Op, SelectionDAG &DAG) {
Duncan Sands83ec4b62008-06-06 12:08:01 +00005803 MVT VT = Op.getValueType();
5804 MVT OpVT = VT;
5805 unsigned NumBits = VT.getSizeInBits();
Evan Cheng18efe262007-12-14 02:13:44 +00005806
5807 Op = Op.getOperand(0);
5808 if (VT == MVT::i8) {
5809 OpVT = MVT::i32;
5810 Op = DAG.getNode(ISD::ZERO_EXTEND, OpVT, Op);
5811 }
Evan Cheng152804e2007-12-14 08:30:15 +00005812
5813 // Issue a bsf (scan bits forward) which also sets EFLAGS.
5814 SDVTList VTs = DAG.getVTList(OpVT, MVT::i32);
5815 Op = DAG.getNode(X86ISD::BSF, VTs, Op);
5816
5817 // If src is zero (i.e. bsf sets ZF), returns NumBits.
Dan Gohman475871a2008-07-27 21:46:04 +00005818 SmallVector<SDValue, 4> Ops;
Evan Cheng152804e2007-12-14 08:30:15 +00005819 Ops.push_back(Op);
5820 Ops.push_back(DAG.getConstant(NumBits, OpVT));
5821 Ops.push_back(DAG.getConstant(X86::COND_E, MVT::i8));
5822 Ops.push_back(Op.getValue(1));
5823 Op = DAG.getNode(X86ISD::CMOV, OpVT, &Ops[0], 4);
5824
Evan Cheng18efe262007-12-14 02:13:44 +00005825 if (VT == MVT::i8)
5826 Op = DAG.getNode(ISD::TRUNCATE, MVT::i8, Op);
5827 return Op;
5828}
5829
Dan Gohman475871a2008-07-27 21:46:04 +00005830SDValue X86TargetLowering::LowerCMP_SWAP(SDValue Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005831 MVT T = Op.getValueType();
Andrew Lenhartha76e2f02008-03-04 21:13:33 +00005832 unsigned Reg = 0;
5833 unsigned size = 0;
Duncan Sands83ec4b62008-06-06 12:08:01 +00005834 switch(T.getSimpleVT()) {
5835 default:
5836 assert(false && "Invalid value type!");
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005837 case MVT::i8: Reg = X86::AL; size = 1; break;
5838 case MVT::i16: Reg = X86::AX; size = 2; break;
5839 case MVT::i32: Reg = X86::EAX; size = 4; break;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005840 case MVT::i64:
5841 if (Subtarget->is64Bit()) {
5842 Reg = X86::RAX; size = 8;
5843 } else //Should go away when LowerType stuff lands
Dan Gohman475871a2008-07-27 21:46:04 +00005844 return SDValue(ExpandATOMIC_CMP_SWAP(Op.Val, DAG), 0);
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005845 break;
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005846 };
Dan Gohman475871a2008-07-27 21:46:04 +00005847 SDValue cpIn = DAG.getCopyToReg(Op.getOperand(0), Reg,
5848 Op.getOperand(3), SDValue());
5849 SDValue Ops[] = { cpIn.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005850 Op.getOperand(1),
5851 Op.getOperand(2),
5852 DAG.getTargetConstant(size, MVT::i8),
5853 cpIn.getValue(1) };
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005854 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005855 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG_DAG, Tys, Ops, 5);
5856 SDValue cpOut =
Andrew Lenharth26ed8692008-03-01 21:52:34 +00005857 DAG.getCopyFromReg(Result.getValue(0), Reg, T, Result.getValue(1));
5858 return cpOut;
5859}
5860
Mon P Wang28873102008-06-25 08:15:39 +00005861SDNode* X86TargetLowering::ExpandATOMIC_CMP_SWAP(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005862 MVT T = Op->getValueType(0);
Mon P Wang28873102008-06-25 08:15:39 +00005863 assert (T == MVT::i64 && "Only know how to expand i64 Cmp and Swap");
Dan Gohman475871a2008-07-27 21:46:04 +00005864 SDValue cpInL, cpInH;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005865 cpInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5866 DAG.getConstant(0, MVT::i32));
5867 cpInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(3),
5868 DAG.getConstant(1, MVT::i32));
5869 cpInL = DAG.getCopyToReg(Op->getOperand(0), X86::EAX,
Dan Gohman475871a2008-07-27 21:46:04 +00005870 cpInL, SDValue());
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005871 cpInH = DAG.getCopyToReg(cpInL.getValue(0), X86::EDX,
5872 cpInH, cpInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005873 SDValue swapInL, swapInH;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005874 swapInL = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5875 DAG.getConstant(0, MVT::i32));
5876 swapInH = DAG.getNode(ISD::EXTRACT_ELEMENT, MVT::i32, Op->getOperand(2),
5877 DAG.getConstant(1, MVT::i32));
5878 swapInL = DAG.getCopyToReg(cpInH.getValue(0), X86::EBX,
5879 swapInL, cpInH.getValue(1));
5880 swapInH = DAG.getCopyToReg(swapInL.getValue(0), X86::ECX,
5881 swapInH, swapInL.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005882 SDValue Ops[] = { swapInH.getValue(0),
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005883 Op->getOperand(1),
5884 swapInH.getValue(1)};
5885 SDVTList Tys = DAG.getVTList(MVT::Other, MVT::Flag);
Dan Gohman475871a2008-07-27 21:46:04 +00005886 SDValue Result = DAG.getNode(X86ISD::LCMPXCHG8_DAG, Tys, Ops, 3);
5887 SDValue cpOutL = DAG.getCopyFromReg(Result.getValue(0), X86::EAX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005888 Result.getValue(1));
Dan Gohman475871a2008-07-27 21:46:04 +00005889 SDValue cpOutH = DAG.getCopyFromReg(cpOutL.getValue(1), X86::EDX, MVT::i32,
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005890 cpOutL.getValue(2));
Dan Gohman475871a2008-07-27 21:46:04 +00005891 SDValue OpsF[] = { cpOutL.getValue(0), cpOutH.getValue(0)};
5892 SDValue ResultVal = DAG.getNode(ISD::BUILD_PAIR, MVT::i64, OpsF, 2);
5893 SDValue Vals[2] = { ResultVal, cpOutH.getValue(1) };
Duncan Sands4bdcb612008-07-02 17:40:58 +00005894 return DAG.getMergeValues(Vals, 2).Val;
Andrew Lenharthd19189e2008-03-05 01:15:49 +00005895}
5896
Mon P Wang28873102008-06-25 08:15:39 +00005897SDNode* X86TargetLowering::ExpandATOMIC_LOAD_SUB(SDNode* Op, SelectionDAG &DAG) {
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005898 MVT T = Op->getValueType(0);
Dan Gohman475871a2008-07-27 21:46:04 +00005899 SDValue negOp = DAG.getNode(ISD::SUB, T,
Mon P Wang63307c32008-05-05 19:05:59 +00005900 DAG.getConstant(0, T), Op->getOperand(2));
Mon P Wang28873102008-06-25 08:15:39 +00005901 return DAG.getAtomic(ISD::ATOMIC_LOAD_ADD, Op->getOperand(0),
Dan Gohmanfd4418f2008-06-25 16:07:49 +00005902 Op->getOperand(1), negOp,
Mon P Wang28873102008-06-25 08:15:39 +00005903 cast<AtomicSDNode>(Op)->getSrcValue(),
5904 cast<AtomicSDNode>(Op)->getAlignment()).Val;
Mon P Wang63307c32008-05-05 19:05:59 +00005905}
5906
Evan Cheng0db9fe62006-04-25 20:13:52 +00005907/// LowerOperation - Provide custom lowering hooks for some operations.
5908///
Dan Gohman475871a2008-07-27 21:46:04 +00005909SDValue X86TargetLowering::LowerOperation(SDValue Op, SelectionDAG &DAG) {
Evan Cheng0db9fe62006-04-25 20:13:52 +00005910 switch (Op.getOpcode()) {
5911 default: assert(0 && "Should not custom lower this!");
Mon P Wang28873102008-06-25 08:15:39 +00005912 case ISD::ATOMIC_CMP_SWAP: return LowerCMP_SWAP(Op,DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005913 case ISD::BUILD_VECTOR: return LowerBUILD_VECTOR(Op, DAG);
5914 case ISD::VECTOR_SHUFFLE: return LowerVECTOR_SHUFFLE(Op, DAG);
5915 case ISD::EXTRACT_VECTOR_ELT: return LowerEXTRACT_VECTOR_ELT(Op, DAG);
5916 case ISD::INSERT_VECTOR_ELT: return LowerINSERT_VECTOR_ELT(Op, DAG);
5917 case ISD::SCALAR_TO_VECTOR: return LowerSCALAR_TO_VECTOR(Op, DAG);
5918 case ISD::ConstantPool: return LowerConstantPool(Op, DAG);
5919 case ISD::GlobalAddress: return LowerGlobalAddress(Op, DAG);
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00005920 case ISD::GlobalTLSAddress: return LowerGlobalTLSAddress(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005921 case ISD::ExternalSymbol: return LowerExternalSymbol(Op, DAG);
5922 case ISD::SHL_PARTS:
5923 case ISD::SRA_PARTS:
5924 case ISD::SRL_PARTS: return LowerShift(Op, DAG);
5925 case ISD::SINT_TO_FP: return LowerSINT_TO_FP(Op, DAG);
5926 case ISD::FP_TO_SINT: return LowerFP_TO_SINT(Op, DAG);
5927 case ISD::FABS: return LowerFABS(Op, DAG);
5928 case ISD::FNEG: return LowerFNEG(Op, DAG);
Evan Cheng68c47cb2007-01-05 07:55:56 +00005929 case ISD::FCOPYSIGN: return LowerFCOPYSIGN(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005930 case ISD::SETCC: return LowerSETCC(Op, DAG);
Nate Begeman30a0de92008-07-17 16:51:19 +00005931 case ISD::VSETCC: return LowerVSETCC(Op, DAG);
Evan Chenge5f62042007-09-29 00:00:36 +00005932 case ISD::SELECT: return LowerSELECT(Op, DAG);
5933 case ISD::BRCOND: return LowerBRCOND(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005934 case ISD::JumpTable: return LowerJumpTable(Op, DAG);
Evan Cheng32fe1032006-05-25 00:59:30 +00005935 case ISD::CALL: return LowerCALL(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005936 case ISD::RET: return LowerRET(Op, DAG);
Evan Cheng1bc78042006-04-26 01:20:17 +00005937 case ISD::FORMAL_ARGUMENTS: return LowerFORMAL_ARGUMENTS(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005938 case ISD::VASTART: return LowerVASTART(Op, DAG);
Dan Gohman9018e832008-05-10 01:26:14 +00005939 case ISD::VAARG: return LowerVAARG(Op, DAG);
Evan Chengae642192007-03-02 23:16:35 +00005940 case ISD::VACOPY: return LowerVACOPY(Op, DAG);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005941 case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG);
Nate Begemanbcc5f362007-01-29 22:58:52 +00005942 case ISD::RETURNADDR: return LowerRETURNADDR(Op, DAG);
5943 case ISD::FRAMEADDR: return LowerFRAMEADDR(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005944 case ISD::FRAME_TO_ARGS_OFFSET:
5945 return LowerFRAME_TO_ARGS_OFFSET(Op, DAG);
Anton Korobeynikov57fc00d2007-04-17 09:20:00 +00005946 case ISD::DYNAMIC_STACKALLOC: return LowerDYNAMIC_STACKALLOC(Op, DAG);
Anton Korobeynikov2365f512007-07-14 14:06:15 +00005947 case ISD::EH_RETURN: return LowerEH_RETURN(Op, DAG);
Duncan Sandsb116fac2007-07-27 20:02:49 +00005948 case ISD::TRAMPOLINE: return LowerTRAMPOLINE(Op, DAG);
Dan Gohman1a024862008-01-31 00:41:03 +00005949 case ISD::FLT_ROUNDS_: return LowerFLT_ROUNDS_(Op, DAG);
Evan Cheng18efe262007-12-14 02:13:44 +00005950 case ISD::CTLZ: return LowerCTLZ(Op, DAG);
5951 case ISD::CTTZ: return LowerCTTZ(Op, DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005952
5953 // FIXME: REMOVE THIS WHEN LegalizeDAGTypes lands.
5954 case ISD::READCYCLECOUNTER:
Dan Gohman475871a2008-07-27 21:46:04 +00005955 return SDValue(ExpandREADCYCLECOUNTER(Op.Val, DAG), 0);
Evan Cheng0db9fe62006-04-25 20:13:52 +00005956 }
Chris Lattner27a6c732007-11-24 07:07:01 +00005957}
5958
Duncan Sands126d9072008-07-04 11:47:58 +00005959/// ReplaceNodeResults - Replace a node with an illegal result type
5960/// with a new node built out of custom code.
5961SDNode *X86TargetLowering::ReplaceNodeResults(SDNode *N, SelectionDAG &DAG) {
Chris Lattner27a6c732007-11-24 07:07:01 +00005962 switch (N->getOpcode()) {
5963 default: assert(0 && "Should not custom lower this!");
5964 case ISD::FP_TO_SINT: return ExpandFP_TO_SINT(N, DAG);
5965 case ISD::READCYCLECOUNTER: return ExpandREADCYCLECOUNTER(N, DAG);
Mon P Wang28873102008-06-25 08:15:39 +00005966 case ISD::ATOMIC_CMP_SWAP: return ExpandATOMIC_CMP_SWAP(N, DAG);
5967 case ISD::ATOMIC_LOAD_SUB: return ExpandATOMIC_LOAD_SUB(N,DAG);
Chris Lattner27a6c732007-11-24 07:07:01 +00005968 }
Evan Cheng0db9fe62006-04-25 20:13:52 +00005969}
5970
Evan Cheng72261582005-12-20 06:22:03 +00005971const char *X86TargetLowering::getTargetNodeName(unsigned Opcode) const {
5972 switch (Opcode) {
5973 default: return NULL;
Evan Cheng18efe262007-12-14 02:13:44 +00005974 case X86ISD::BSF: return "X86ISD::BSF";
5975 case X86ISD::BSR: return "X86ISD::BSR";
Evan Chenge3413162006-01-09 18:33:28 +00005976 case X86ISD::SHLD: return "X86ISD::SHLD";
5977 case X86ISD::SHRD: return "X86ISD::SHRD";
Evan Chengef6ffb12006-01-31 03:14:29 +00005978 case X86ISD::FAND: return "X86ISD::FAND";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005979 case X86ISD::FOR: return "X86ISD::FOR";
Evan Cheng223547a2006-01-31 22:28:30 +00005980 case X86ISD::FXOR: return "X86ISD::FXOR";
Evan Cheng68c47cb2007-01-05 07:55:56 +00005981 case X86ISD::FSRL: return "X86ISD::FSRL";
Evan Chenga3195e82006-01-12 22:54:21 +00005982 case X86ISD::FILD: return "X86ISD::FILD";
Evan Chenge3de85b2006-02-04 02:20:30 +00005983 case X86ISD::FILD_FLAG: return "X86ISD::FILD_FLAG";
Evan Cheng72261582005-12-20 06:22:03 +00005984 case X86ISD::FP_TO_INT16_IN_MEM: return "X86ISD::FP_TO_INT16_IN_MEM";
5985 case X86ISD::FP_TO_INT32_IN_MEM: return "X86ISD::FP_TO_INT32_IN_MEM";
5986 case X86ISD::FP_TO_INT64_IN_MEM: return "X86ISD::FP_TO_INT64_IN_MEM";
Evan Chengb077b842005-12-21 02:39:21 +00005987 case X86ISD::FLD: return "X86ISD::FLD";
Evan Chengd90eb7f2006-01-05 00:27:02 +00005988 case X86ISD::FST: return "X86ISD::FST";
Evan Cheng72261582005-12-20 06:22:03 +00005989 case X86ISD::CALL: return "X86ISD::CALL";
5990 case X86ISD::TAILCALL: return "X86ISD::TAILCALL";
5991 case X86ISD::RDTSC_DAG: return "X86ISD::RDTSC_DAG";
5992 case X86ISD::CMP: return "X86ISD::CMP";
Evan Cheng6be2c582006-04-05 23:38:46 +00005993 case X86ISD::COMI: return "X86ISD::COMI";
5994 case X86ISD::UCOMI: return "X86ISD::UCOMI";
Evan Chengd5781fc2005-12-21 20:21:51 +00005995 case X86ISD::SETCC: return "X86ISD::SETCC";
Evan Cheng72261582005-12-20 06:22:03 +00005996 case X86ISD::CMOV: return "X86ISD::CMOV";
5997 case X86ISD::BRCOND: return "X86ISD::BRCOND";
Evan Chengb077b842005-12-21 02:39:21 +00005998 case X86ISD::RET_FLAG: return "X86ISD::RET_FLAG";
Evan Cheng8df346b2006-03-04 01:12:00 +00005999 case X86ISD::REP_STOS: return "X86ISD::REP_STOS";
6000 case X86ISD::REP_MOVS: return "X86ISD::REP_MOVS";
Evan Cheng7ccced62006-02-18 00:15:05 +00006001 case X86ISD::GlobalBaseReg: return "X86ISD::GlobalBaseReg";
Evan Cheng020d2e82006-02-23 20:41:18 +00006002 case X86ISD::Wrapper: return "X86ISD::Wrapper";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006003 case X86ISD::PEXTRB: return "X86ISD::PEXTRB";
Evan Chengb067a1e2006-03-31 19:22:53 +00006004 case X86ISD::PEXTRW: return "X86ISD::PEXTRW";
Nate Begeman14d12ca2008-02-11 04:19:36 +00006005 case X86ISD::INSERTPS: return "X86ISD::INSERTPS";
6006 case X86ISD::PINSRB: return "X86ISD::PINSRB";
Evan Cheng653159f2006-03-31 21:55:24 +00006007 case X86ISD::PINSRW: return "X86ISD::PINSRW";
Evan Cheng8ca29322006-11-10 21:43:37 +00006008 case X86ISD::FMAX: return "X86ISD::FMAX";
6009 case X86ISD::FMIN: return "X86ISD::FMIN";
Dan Gohman20382522007-07-10 00:05:58 +00006010 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
6011 case X86ISD::FRCP: return "X86ISD::FRCP";
Lauro Ramos Venanciob3a04172007-04-20 21:38:10 +00006012 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
6013 case X86ISD::THREAD_POINTER: return "X86ISD::THREAD_POINTER";
Anton Korobeynikov2365f512007-07-14 14:06:15 +00006014 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
Arnold Schwaighoferc85e1712007-10-11 19:40:01 +00006015 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
Anton Korobeynikov45b22fa2007-11-16 01:31:51 +00006016 case X86ISD::FNSTCW16m: return "X86ISD::FNSTCW16m";
Evan Cheng7e2ff772008-05-08 00:57:18 +00006017 case X86ISD::LCMPXCHG_DAG: return "X86ISD::LCMPXCHG_DAG";
6018 case X86ISD::LCMPXCHG8_DAG: return "X86ISD::LCMPXCHG8_DAG";
Evan Chengd880b972008-05-09 21:53:03 +00006019 case X86ISD::VZEXT_MOVL: return "X86ISD::VZEXT_MOVL";
6020 case X86ISD::VZEXT_LOAD: return "X86ISD::VZEXT_LOAD";
Evan Chengf26ffe92008-05-29 08:22:04 +00006021 case X86ISD::VSHL: return "X86ISD::VSHL";
6022 case X86ISD::VSRL: return "X86ISD::VSRL";
Nate Begeman30a0de92008-07-17 16:51:19 +00006023 case X86ISD::CMPPD: return "X86ISD::CMPPD";
6024 case X86ISD::CMPPS: return "X86ISD::CMPPS";
6025 case X86ISD::PCMPEQB: return "X86ISD::PCMPEQB";
6026 case X86ISD::PCMPEQW: return "X86ISD::PCMPEQW";
6027 case X86ISD::PCMPEQD: return "X86ISD::PCMPEQD";
6028 case X86ISD::PCMPEQQ: return "X86ISD::PCMPEQQ";
6029 case X86ISD::PCMPGTB: return "X86ISD::PCMPGTB";
6030 case X86ISD::PCMPGTW: return "X86ISD::PCMPGTW";
6031 case X86ISD::PCMPGTD: return "X86ISD::PCMPGTD";
6032 case X86ISD::PCMPGTQ: return "X86ISD::PCMPGTQ";
Evan Cheng72261582005-12-20 06:22:03 +00006033 }
6034}
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006035
Chris Lattnerc9addb72007-03-30 23:15:24 +00006036// isLegalAddressingMode - Return true if the addressing mode represented
6037// by AM is legal for this target, for a load/store of the specified type.
6038bool X86TargetLowering::isLegalAddressingMode(const AddrMode &AM,
6039 const Type *Ty) const {
6040 // X86 supports extremely general addressing modes.
6041
6042 // X86 allows a sign-extended 32-bit immediate field as a displacement.
6043 if (AM.BaseOffs <= -(1LL << 32) || AM.BaseOffs >= (1LL << 32)-1)
6044 return false;
6045
6046 if (AM.BaseGV) {
Evan Cheng52787842007-08-01 23:46:47 +00006047 // We can only fold this if we don't need an extra load.
Chris Lattnerc9addb72007-03-30 23:15:24 +00006048 if (Subtarget->GVRequiresExtraLoad(AM.BaseGV, getTargetMachine(), false))
6049 return false;
Evan Cheng52787842007-08-01 23:46:47 +00006050
6051 // X86-64 only supports addr of globals in small code model.
6052 if (Subtarget->is64Bit()) {
6053 if (getTargetMachine().getCodeModel() != CodeModel::Small)
6054 return false;
6055 // If lower 4G is not available, then we must use rip-relative addressing.
6056 if (AM.BaseOffs || AM.Scale > 1)
6057 return false;
6058 }
Chris Lattnerc9addb72007-03-30 23:15:24 +00006059 }
6060
6061 switch (AM.Scale) {
6062 case 0:
6063 case 1:
6064 case 2:
6065 case 4:
6066 case 8:
6067 // These scales always work.
6068 break;
6069 case 3:
6070 case 5:
6071 case 9:
6072 // These scales are formed with basereg+scalereg. Only accept if there is
6073 // no basereg yet.
6074 if (AM.HasBaseReg)
6075 return false;
6076 break;
6077 default: // Other stuff never works.
6078 return false;
6079 }
6080
6081 return true;
6082}
6083
6084
Evan Cheng2bd122c2007-10-26 01:56:11 +00006085bool X86TargetLowering::isTruncateFree(const Type *Ty1, const Type *Ty2) const {
6086 if (!Ty1->isInteger() || !Ty2->isInteger())
6087 return false;
Evan Chenge127a732007-10-29 07:57:50 +00006088 unsigned NumBits1 = Ty1->getPrimitiveSizeInBits();
6089 unsigned NumBits2 = Ty2->getPrimitiveSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006090 if (NumBits1 <= NumBits2)
Evan Chenge127a732007-10-29 07:57:50 +00006091 return false;
6092 return Subtarget->is64Bit() || NumBits1 < 64;
Evan Cheng2bd122c2007-10-26 01:56:11 +00006093}
6094
Duncan Sands83ec4b62008-06-06 12:08:01 +00006095bool X86TargetLowering::isTruncateFree(MVT VT1, MVT VT2) const {
6096 if (!VT1.isInteger() || !VT2.isInteger())
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006097 return false;
Duncan Sands83ec4b62008-06-06 12:08:01 +00006098 unsigned NumBits1 = VT1.getSizeInBits();
6099 unsigned NumBits2 = VT2.getSizeInBits();
Evan Cheng260e07e2008-03-20 02:18:41 +00006100 if (NumBits1 <= NumBits2)
Evan Cheng3c3ddb32007-10-29 19:58:20 +00006101 return false;
6102 return Subtarget->is64Bit() || NumBits1 < 64;
6103}
Evan Cheng2bd122c2007-10-26 01:56:11 +00006104
Evan Cheng60c07e12006-07-05 22:17:51 +00006105/// isShuffleMaskLegal - Targets can use this to indicate that they only
6106/// support *some* VECTOR_SHUFFLE operations, those with specific masks.
6107/// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
6108/// are assumed to be legal.
6109bool
Dan Gohman475871a2008-07-27 21:46:04 +00006110X86TargetLowering::isShuffleMaskLegal(SDValue Mask, MVT VT) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006111 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006112 if (VT.getSizeInBits() == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006113 return (Mask.Val->getNumOperands() <= 4 ||
Evan Cheng49892af2007-06-19 00:02:56 +00006114 isIdentityMask(Mask.Val) ||
6115 isIdentityMask(Mask.Val, true) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00006116 isSplatMask(Mask.Val) ||
6117 isPSHUFHW_PSHUFLWMask(Mask.Val) ||
6118 X86::isUNPCKLMask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00006119 X86::isUNPCKHMask(Mask.Val) ||
Evan Cheng60c07e12006-07-05 22:17:51 +00006120 X86::isUNPCKL_v_undef_Mask(Mask.Val) ||
Evan Cheng49892af2007-06-19 00:02:56 +00006121 X86::isUNPCKH_v_undef_Mask(Mask.Val));
Evan Cheng60c07e12006-07-05 22:17:51 +00006122}
6123
Dan Gohman7d8143f2008-04-09 20:09:42 +00006124bool
Dan Gohman475871a2008-07-27 21:46:04 +00006125X86TargetLowering::isVectorClearMaskLegal(const std::vector<SDValue> &BVOps,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006126 MVT EVT, SelectionDAG &DAG) const {
Evan Cheng60c07e12006-07-05 22:17:51 +00006127 unsigned NumElts = BVOps.size();
6128 // Only do shuffles on 128-bit vector types for now.
Duncan Sands83ec4b62008-06-06 12:08:01 +00006129 if (EVT.getSizeInBits() * NumElts == 64) return false;
Evan Cheng60c07e12006-07-05 22:17:51 +00006130 if (NumElts == 2) return true;
6131 if (NumElts == 4) {
Chris Lattner5a88b832007-02-25 07:10:00 +00006132 return (isMOVLMask(&BVOps[0], 4) ||
6133 isCommutedMOVL(&BVOps[0], 4, true) ||
6134 isSHUFPMask(&BVOps[0], 4) ||
6135 isCommutedSHUFP(&BVOps[0], 4));
Evan Cheng60c07e12006-07-05 22:17:51 +00006136 }
6137 return false;
6138}
6139
6140//===----------------------------------------------------------------------===//
6141// X86 Scheduler Hooks
6142//===----------------------------------------------------------------------===//
6143
Mon P Wang63307c32008-05-05 19:05:59 +00006144// private utility function
6145MachineBasicBlock *
6146X86TargetLowering::EmitAtomicBitwiseWithCustomInserter(MachineInstr *bInstr,
6147 MachineBasicBlock *MBB,
6148 unsigned regOpc,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006149 unsigned immOpc,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006150 unsigned LoadOpc,
6151 unsigned CXchgOpc,
6152 unsigned copyOpc,
6153 unsigned notOpc,
6154 unsigned EAXreg,
6155 TargetRegisterClass *RC,
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006156 bool invSrc) {
Mon P Wang63307c32008-05-05 19:05:59 +00006157 // For the atomic bitwise operator, we generate
6158 // thisMBB:
6159 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006160 // ld t1 = [bitinstr.addr]
6161 // op t2 = t1, [bitinstr.val]
6162 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006163 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6164 // bz newMBB
6165 // fallthrough -->nextMBB
6166 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6167 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006168 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006169 ++MBBIter;
6170
6171 /// First build the CFG
6172 MachineFunction *F = MBB->getParent();
6173 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006174 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6175 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6176 F->insert(MBBIter, newMBB);
6177 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006178
6179 // Move all successors to thisMBB to nextMBB
6180 nextMBB->transferSuccessors(thisMBB);
6181
6182 // Update thisMBB to fall through to newMBB
6183 thisMBB->addSuccessor(newMBB);
6184
6185 // newMBB jumps to itself and fall through to nextMBB
6186 newMBB->addSuccessor(nextMBB);
6187 newMBB->addSuccessor(newMBB);
6188
6189 // Insert instructions into newMBB based on incoming instruction
6190 assert(bInstr->getNumOperands() < 8 && "unexpected number of operands");
6191 MachineOperand& destOper = bInstr->getOperand(0);
6192 MachineOperand* argOpers[6];
6193 int numArgs = bInstr->getNumOperands() - 1;
6194 for (int i=0; i < numArgs; ++i)
6195 argOpers[i] = &bInstr->getOperand(i+1);
6196
6197 // x86 address has 4 operands: base, index, scale, and displacement
6198 int lastAddrIndx = 3; // [0,3]
6199 int valArgIndx = 4;
6200
Dale Johannesen140be2d2008-08-19 18:47:28 +00006201 unsigned t1 = F->getRegInfo().createVirtualRegister(RC);
6202 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(LoadOpc), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006203 for (int i=0; i <= lastAddrIndx; ++i)
6204 (*MIB).addOperand(*argOpers[i]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006205
Dale Johannesen140be2d2008-08-19 18:47:28 +00006206 unsigned tt = F->getRegInfo().createVirtualRegister(RC);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006207 if (invSrc) {
Dale Johannesen140be2d2008-08-19 18:47:28 +00006208 MIB = BuildMI(newMBB, TII->get(notOpc), tt).addReg(t1);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006209 }
6210 else
6211 tt = t1;
6212
Dale Johannesen140be2d2008-08-19 18:47:28 +00006213 unsigned t2 = F->getRegInfo().createVirtualRegister(RC);
Mon P Wang63307c32008-05-05 19:05:59 +00006214 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6215 && "invalid operand");
6216 if (argOpers[valArgIndx]->isReg())
6217 MIB = BuildMI(newMBB, TII->get(regOpc), t2);
6218 else
6219 MIB = BuildMI(newMBB, TII->get(immOpc), t2);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006220 MIB.addReg(tt);
Mon P Wang63307c32008-05-05 19:05:59 +00006221 (*MIB).addOperand(*argOpers[valArgIndx]);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006222
Dale Johannesen140be2d2008-08-19 18:47:28 +00006223 MIB = BuildMI(newMBB, TII->get(copyOpc), EAXreg);
Mon P Wangab3e7472008-05-05 22:56:23 +00006224 MIB.addReg(t1);
6225
Dale Johannesen140be2d2008-08-19 18:47:28 +00006226 MIB = BuildMI(newMBB, TII->get(CXchgOpc));
Mon P Wang63307c32008-05-05 19:05:59 +00006227 for (int i=0; i <= lastAddrIndx; ++i)
6228 (*MIB).addOperand(*argOpers[i]);
6229 MIB.addReg(t2);
Mon P Wangf5952662008-07-17 04:54:06 +00006230 assert(bInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6231 (*MIB).addMemOperand(*F, *bInstr->memoperands_begin());
6232
Dale Johannesen140be2d2008-08-19 18:47:28 +00006233 MIB = BuildMI(newMBB, TII->get(copyOpc), destOper.getReg());
6234 MIB.addReg(EAXreg);
Mon P Wang63307c32008-05-05 19:05:59 +00006235
6236 // insert branch
6237 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6238
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006239 F->DeleteMachineInstr(bInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006240 return nextMBB;
6241}
6242
6243// private utility function
6244MachineBasicBlock *
6245X86TargetLowering::EmitAtomicMinMaxWithCustomInserter(MachineInstr *mInstr,
6246 MachineBasicBlock *MBB,
6247 unsigned cmovOpc) {
6248 // For the atomic min/max operator, we generate
6249 // thisMBB:
6250 // newMBB:
Mon P Wangab3e7472008-05-05 22:56:23 +00006251 // ld t1 = [min/max.addr]
Mon P Wang63307c32008-05-05 19:05:59 +00006252 // mov t2 = [min/max.val]
6253 // cmp t1, t2
6254 // cmov[cond] t2 = t1
Mon P Wangab3e7472008-05-05 22:56:23 +00006255 // mov EAX = t1
Mon P Wang63307c32008-05-05 19:05:59 +00006256 // lcs dest = [bitinstr.addr], t2 [EAX is implicit]
6257 // bz newMBB
6258 // fallthrough -->nextMBB
6259 //
6260 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
6261 const BasicBlock *LLVM_BB = MBB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006262 MachineFunction::iterator MBBIter = MBB;
Mon P Wang63307c32008-05-05 19:05:59 +00006263 ++MBBIter;
6264
6265 /// First build the CFG
6266 MachineFunction *F = MBB->getParent();
6267 MachineBasicBlock *thisMBB = MBB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006268 MachineBasicBlock *newMBB = F->CreateMachineBasicBlock(LLVM_BB);
6269 MachineBasicBlock *nextMBB = F->CreateMachineBasicBlock(LLVM_BB);
6270 F->insert(MBBIter, newMBB);
6271 F->insert(MBBIter, nextMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006272
6273 // Move all successors to thisMBB to nextMBB
6274 nextMBB->transferSuccessors(thisMBB);
6275
6276 // Update thisMBB to fall through to newMBB
6277 thisMBB->addSuccessor(newMBB);
6278
6279 // newMBB jumps to newMBB and fall through to nextMBB
6280 newMBB->addSuccessor(nextMBB);
6281 newMBB->addSuccessor(newMBB);
6282
6283 // Insert instructions into newMBB based on incoming instruction
6284 assert(mInstr->getNumOperands() < 8 && "unexpected number of operands");
6285 MachineOperand& destOper = mInstr->getOperand(0);
6286 MachineOperand* argOpers[6];
6287 int numArgs = mInstr->getNumOperands() - 1;
6288 for (int i=0; i < numArgs; ++i)
6289 argOpers[i] = &mInstr->getOperand(i+1);
6290
6291 // x86 address has 4 operands: base, index, scale, and displacement
6292 int lastAddrIndx = 3; // [0,3]
6293 int valArgIndx = 4;
6294
Mon P Wangab3e7472008-05-05 22:56:23 +00006295 unsigned t1 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6296 MachineInstrBuilder MIB = BuildMI(newMBB, TII->get(X86::MOV32rm), t1);
Mon P Wang63307c32008-05-05 19:05:59 +00006297 for (int i=0; i <= lastAddrIndx; ++i)
6298 (*MIB).addOperand(*argOpers[i]);
Mon P Wangab3e7472008-05-05 22:56:23 +00006299
Mon P Wang63307c32008-05-05 19:05:59 +00006300 // We only support register and immediate values
6301 assert( (argOpers[valArgIndx]->isReg() || argOpers[valArgIndx]->isImm())
6302 && "invalid operand");
6303
6304 unsigned t2 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6305 if (argOpers[valArgIndx]->isReg())
6306 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6307 else
6308 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), t2);
6309 (*MIB).addOperand(*argOpers[valArgIndx]);
6310
Mon P Wangab3e7472008-05-05 22:56:23 +00006311 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), X86::EAX);
6312 MIB.addReg(t1);
6313
Mon P Wang63307c32008-05-05 19:05:59 +00006314 MIB = BuildMI(newMBB, TII->get(X86::CMP32rr));
6315 MIB.addReg(t1);
6316 MIB.addReg(t2);
6317
6318 // Generate movc
6319 unsigned t3 = F->getRegInfo().createVirtualRegister(X86::GR32RegisterClass);
6320 MIB = BuildMI(newMBB, TII->get(cmovOpc),t3);
6321 MIB.addReg(t2);
6322 MIB.addReg(t1);
6323
6324 // Cmp and exchange if none has modified the memory location
6325 MIB = BuildMI(newMBB, TII->get(X86::LCMPXCHG32));
6326 for (int i=0; i <= lastAddrIndx; ++i)
6327 (*MIB).addOperand(*argOpers[i]);
6328 MIB.addReg(t3);
Mon P Wangf5952662008-07-17 04:54:06 +00006329 assert(mInstr->hasOneMemOperand() && "Unexpected number of memoperand");
6330 (*MIB).addMemOperand(*F, *mInstr->memoperands_begin());
Mon P Wang63307c32008-05-05 19:05:59 +00006331
6332 MIB = BuildMI(newMBB, TII->get(X86::MOV32rr), destOper.getReg());
6333 MIB.addReg(X86::EAX);
6334
6335 // insert branch
6336 BuildMI(newMBB, TII->get(X86::JNE)).addMBB(newMBB);
6337
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006338 F->DeleteMachineInstr(mInstr); // The pseudo instruction is gone now.
Mon P Wang63307c32008-05-05 19:05:59 +00006339 return nextMBB;
6340}
6341
6342
Evan Cheng60c07e12006-07-05 22:17:51 +00006343MachineBasicBlock *
Evan Chengff9b3732008-01-30 18:18:23 +00006344X86TargetLowering::EmitInstrWithCustomInserter(MachineInstr *MI,
6345 MachineBasicBlock *BB) {
Evan Chengc0f64ff2006-11-27 23:37:22 +00006346 const TargetInstrInfo *TII = getTargetMachine().getInstrInfo();
Evan Cheng60c07e12006-07-05 22:17:51 +00006347 switch (MI->getOpcode()) {
6348 default: assert(false && "Unexpected instr type to insert");
6349 case X86::CMOV_FR32:
6350 case X86::CMOV_FR64:
6351 case X86::CMOV_V4F32:
6352 case X86::CMOV_V2F64:
Evan Chenge5f62042007-09-29 00:00:36 +00006353 case X86::CMOV_V2I64: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006354 // To "insert" a SELECT_CC instruction, we actually have to insert the
6355 // diamond control-flow pattern. The incoming instruction knows the
6356 // destination vreg to set, the condition code register to branch on, the
6357 // true/false values to select between, and a branch opcode to use.
6358 const BasicBlock *LLVM_BB = BB->getBasicBlock();
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006359 MachineFunction::iterator It = BB;
Evan Cheng60c07e12006-07-05 22:17:51 +00006360 ++It;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006361
Evan Cheng60c07e12006-07-05 22:17:51 +00006362 // thisMBB:
6363 // ...
6364 // TrueVal = ...
6365 // cmpTY ccX, r1, r2
6366 // bCC copy1MBB
6367 // fallthrough --> copy0MBB
6368 MachineBasicBlock *thisMBB = BB;
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006369 MachineFunction *F = BB->getParent();
6370 MachineBasicBlock *copy0MBB = F->CreateMachineBasicBlock(LLVM_BB);
6371 MachineBasicBlock *sinkMBB = F->CreateMachineBasicBlock(LLVM_BB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006372 unsigned Opc =
Chris Lattner7fbe9722006-10-20 17:42:20 +00006373 X86::GetCondBranchFromCond((X86::CondCode)MI->getOperand(3).getImm());
Evan Chengc0f64ff2006-11-27 23:37:22 +00006374 BuildMI(BB, TII->get(Opc)).addMBB(sinkMBB);
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006375 F->insert(It, copy0MBB);
6376 F->insert(It, sinkMBB);
Mon P Wang63307c32008-05-05 19:05:59 +00006377 // Update machine-CFG edges by transferring all successors of the current
Evan Cheng60c07e12006-07-05 22:17:51 +00006378 // block to the new block which will contain the Phi node for the select.
Mon P Wang63307c32008-05-05 19:05:59 +00006379 sinkMBB->transferSuccessors(BB);
6380
6381 // Add the true and fallthrough blocks as its successors.
Evan Cheng60c07e12006-07-05 22:17:51 +00006382 BB->addSuccessor(copy0MBB);
6383 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006384
Evan Cheng60c07e12006-07-05 22:17:51 +00006385 // copy0MBB:
6386 // %FalseValue = ...
6387 // # fallthrough to sinkMBB
6388 BB = copy0MBB;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006389
Evan Cheng60c07e12006-07-05 22:17:51 +00006390 // Update machine-CFG edges
6391 BB->addSuccessor(sinkMBB);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006392
Evan Cheng60c07e12006-07-05 22:17:51 +00006393 // sinkMBB:
6394 // %Result = phi [ %FalseValue, copy0MBB ], [ %TrueValue, thisMBB ]
6395 // ...
6396 BB = sinkMBB;
Evan Chengc0f64ff2006-11-27 23:37:22 +00006397 BuildMI(BB, TII->get(X86::PHI), MI->getOperand(0).getReg())
Evan Cheng60c07e12006-07-05 22:17:51 +00006398 .addReg(MI->getOperand(1).getReg()).addMBB(copy0MBB)
6399 .addReg(MI->getOperand(2).getReg()).addMBB(thisMBB);
6400
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006401 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006402 return BB;
6403 }
6404
Dale Johannesen849f2142007-07-03 00:53:03 +00006405 case X86::FP32_TO_INT16_IN_MEM:
6406 case X86::FP32_TO_INT32_IN_MEM:
6407 case X86::FP32_TO_INT64_IN_MEM:
6408 case X86::FP64_TO_INT16_IN_MEM:
6409 case X86::FP64_TO_INT32_IN_MEM:
Dale Johannesena996d522007-08-07 01:17:37 +00006410 case X86::FP64_TO_INT64_IN_MEM:
6411 case X86::FP80_TO_INT16_IN_MEM:
6412 case X86::FP80_TO_INT32_IN_MEM:
6413 case X86::FP80_TO_INT64_IN_MEM: {
Evan Cheng60c07e12006-07-05 22:17:51 +00006414 // Change the floating point control register to use "round towards zero"
6415 // mode when truncating to an integer value.
6416 MachineFunction *F = BB->getParent();
6417 int CWFrameIdx = F->getFrameInfo()->CreateStackObject(2, 2);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006418 addFrameReference(BuildMI(BB, TII->get(X86::FNSTCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006419
6420 // Load the old value of the high byte of the control word...
6421 unsigned OldCW =
Chris Lattner84bc5422007-12-31 04:13:23 +00006422 F->getRegInfo().createVirtualRegister(X86::GR16RegisterClass);
Evan Chengc0f64ff2006-11-27 23:37:22 +00006423 addFrameReference(BuildMI(BB, TII->get(X86::MOV16rm), OldCW), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006424
6425 // Set the high part to be round to zero...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006426 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mi)), CWFrameIdx)
6427 .addImm(0xC7F);
Evan Cheng60c07e12006-07-05 22:17:51 +00006428
6429 // Reload the modified control word now...
Evan Chengc0f64ff2006-11-27 23:37:22 +00006430 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006431
6432 // Restore the memory image of control word to original value
Evan Chengc0f64ff2006-11-27 23:37:22 +00006433 addFrameReference(BuildMI(BB, TII->get(X86::MOV16mr)), CWFrameIdx)
6434 .addReg(OldCW);
Evan Cheng60c07e12006-07-05 22:17:51 +00006435
6436 // Get the X86 opcode to use.
6437 unsigned Opc;
6438 switch (MI->getOpcode()) {
6439 default: assert(0 && "illegal opcode!");
Dale Johannesene377d4d2007-07-04 21:07:47 +00006440 case X86::FP32_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m32; break;
6441 case X86::FP32_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m32; break;
6442 case X86::FP32_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m32; break;
6443 case X86::FP64_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m64; break;
6444 case X86::FP64_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m64; break;
6445 case X86::FP64_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m64; break;
Dale Johannesena996d522007-08-07 01:17:37 +00006446 case X86::FP80_TO_INT16_IN_MEM: Opc = X86::IST_Fp16m80; break;
6447 case X86::FP80_TO_INT32_IN_MEM: Opc = X86::IST_Fp32m80; break;
6448 case X86::FP80_TO_INT64_IN_MEM: Opc = X86::IST_Fp64m80; break;
Evan Cheng60c07e12006-07-05 22:17:51 +00006449 }
6450
6451 X86AddressMode AM;
6452 MachineOperand &Op = MI->getOperand(0);
6453 if (Op.isRegister()) {
6454 AM.BaseType = X86AddressMode::RegBase;
6455 AM.Base.Reg = Op.getReg();
6456 } else {
6457 AM.BaseType = X86AddressMode::FrameIndexBase;
Chris Lattner8aa797a2007-12-30 23:10:15 +00006458 AM.Base.FrameIndex = Op.getIndex();
Evan Cheng60c07e12006-07-05 22:17:51 +00006459 }
6460 Op = MI->getOperand(1);
6461 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006462 AM.Scale = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006463 Op = MI->getOperand(2);
6464 if (Op.isImmediate())
Chris Lattner7fbe9722006-10-20 17:42:20 +00006465 AM.IndexReg = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006466 Op = MI->getOperand(3);
6467 if (Op.isGlobalAddress()) {
6468 AM.GV = Op.getGlobal();
6469 } else {
Chris Lattner7fbe9722006-10-20 17:42:20 +00006470 AM.Disp = Op.getImm();
Evan Cheng60c07e12006-07-05 22:17:51 +00006471 }
Evan Chengc0f64ff2006-11-27 23:37:22 +00006472 addFullAddress(BuildMI(BB, TII->get(Opc)), AM)
6473 .addReg(MI->getOperand(4).getReg());
Evan Cheng60c07e12006-07-05 22:17:51 +00006474
6475 // Reload the original control word now.
Evan Chengc0f64ff2006-11-27 23:37:22 +00006476 addFrameReference(BuildMI(BB, TII->get(X86::FLDCW16m)), CWFrameIdx);
Evan Cheng60c07e12006-07-05 22:17:51 +00006477
Dan Gohman8e5f2c62008-07-07 23:14:23 +00006478 F->DeleteMachineInstr(MI); // The pseudo instruction is gone now.
Evan Cheng60c07e12006-07-05 22:17:51 +00006479 return BB;
6480 }
Mon P Wang63307c32008-05-05 19:05:59 +00006481 case X86::ATOMAND32:
6482 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006483 X86::AND32ri, X86::MOV32rm,
6484 X86::LCMPXCHG32, X86::MOV32rr,
6485 X86::NOT32r, X86::EAX,
6486 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00006487 case X86::ATOMOR32:
6488 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006489 X86::OR32ri, X86::MOV32rm,
6490 X86::LCMPXCHG32, X86::MOV32rr,
6491 X86::NOT32r, X86::EAX,
6492 X86::GR32RegisterClass);
Mon P Wang63307c32008-05-05 19:05:59 +00006493 case X86::ATOMXOR32:
6494 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006495 X86::XOR32ri, X86::MOV32rm,
6496 X86::LCMPXCHG32, X86::MOV32rr,
6497 X86::NOT32r, X86::EAX,
6498 X86::GR32RegisterClass);
Andrew Lenharth507a58a2008-06-14 05:48:15 +00006499 case X86::ATOMNAND32:
6500 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND32rr,
Dale Johannesen140be2d2008-08-19 18:47:28 +00006501 X86::AND32ri, X86::MOV32rm,
6502 X86::LCMPXCHG32, X86::MOV32rr,
6503 X86::NOT32r, X86::EAX,
6504 X86::GR32RegisterClass, true);
Mon P Wang63307c32008-05-05 19:05:59 +00006505 case X86::ATOMMIN32:
6506 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL32rr);
6507 case X86::ATOMMAX32:
6508 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG32rr);
6509 case X86::ATOMUMIN32:
6510 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB32rr);
6511 case X86::ATOMUMAX32:
6512 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA32rr);
Dale Johannesen140be2d2008-08-19 18:47:28 +00006513
6514 case X86::ATOMAND16:
6515 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6516 X86::AND16ri, X86::MOV16rm,
6517 X86::LCMPXCHG16, X86::MOV16rr,
6518 X86::NOT16r, X86::AX,
6519 X86::GR16RegisterClass);
6520 case X86::ATOMOR16:
6521 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR16rr,
6522 X86::OR16ri, X86::MOV16rm,
6523 X86::LCMPXCHG16, X86::MOV16rr,
6524 X86::NOT16r, X86::AX,
6525 X86::GR16RegisterClass);
6526 case X86::ATOMXOR16:
6527 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR16rr,
6528 X86::XOR16ri, X86::MOV16rm,
6529 X86::LCMPXCHG16, X86::MOV16rr,
6530 X86::NOT16r, X86::AX,
6531 X86::GR16RegisterClass);
6532 case X86::ATOMNAND16:
6533 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND16rr,
6534 X86::AND16ri, X86::MOV16rm,
6535 X86::LCMPXCHG16, X86::MOV16rr,
6536 X86::NOT16r, X86::AX,
6537 X86::GR16RegisterClass, true);
6538 case X86::ATOMMIN16:
6539 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL16rr);
6540 case X86::ATOMMAX16:
6541 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG16rr);
6542 case X86::ATOMUMIN16:
6543 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB16rr);
6544 case X86::ATOMUMAX16:
6545 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA16rr);
6546
6547 case X86::ATOMAND8:
6548 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6549 X86::AND8ri, X86::MOV8rm,
6550 X86::LCMPXCHG8, X86::MOV8rr,
6551 X86::NOT8r, X86::AL,
6552 X86::GR8RegisterClass);
6553 case X86::ATOMOR8:
6554 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR8rr,
6555 X86::OR8ri, X86::MOV8rm,
6556 X86::LCMPXCHG8, X86::MOV8rr,
6557 X86::NOT8r, X86::AL,
6558 X86::GR8RegisterClass);
6559 case X86::ATOMXOR8:
6560 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR8rr,
6561 X86::XOR8ri, X86::MOV8rm,
6562 X86::LCMPXCHG8, X86::MOV8rr,
6563 X86::NOT8r, X86::AL,
6564 X86::GR8RegisterClass);
6565 case X86::ATOMNAND8:
6566 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND8rr,
6567 X86::AND8ri, X86::MOV8rm,
6568 X86::LCMPXCHG8, X86::MOV8rr,
6569 X86::NOT8r, X86::AL,
6570 X86::GR8RegisterClass, true);
6571 // FIXME: There are no CMOV8 instructions; MIN/MAX need some other way.
Dale Johannesena99e3842008-08-20 00:48:50 +00006572 case X86::ATOMAND64:
6573 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6574 X86::AND64ri32, X86::MOV64rm,
6575 X86::LCMPXCHG64, X86::MOV64rr,
6576 X86::NOT64r, X86::RAX,
6577 X86::GR64RegisterClass);
6578 case X86::ATOMOR64:
6579 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::OR64rr,
6580 X86::OR64ri32, X86::MOV64rm,
6581 X86::LCMPXCHG64, X86::MOV64rr,
6582 X86::NOT64r, X86::RAX,
6583 X86::GR64RegisterClass);
6584 case X86::ATOMXOR64:
6585 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::XOR64rr,
6586 X86::XOR64ri32, X86::MOV64rm,
6587 X86::LCMPXCHG64, X86::MOV64rr,
6588 X86::NOT64r, X86::RAX,
6589 X86::GR64RegisterClass);
6590 case X86::ATOMNAND64:
6591 return EmitAtomicBitwiseWithCustomInserter(MI, BB, X86::AND64rr,
6592 X86::AND64ri32, X86::MOV64rm,
6593 X86::LCMPXCHG64, X86::MOV64rr,
6594 X86::NOT64r, X86::RAX,
6595 X86::GR64RegisterClass, true);
6596 case X86::ATOMMIN64:
6597 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVL64rr);
6598 case X86::ATOMMAX64:
6599 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVG64rr);
6600 case X86::ATOMUMIN64:
6601 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVB64rr);
6602 case X86::ATOMUMAX64:
6603 return EmitAtomicMinMaxWithCustomInserter(MI, BB, X86::CMOVA64rr);
Evan Cheng60c07e12006-07-05 22:17:51 +00006604 }
6605}
6606
6607//===----------------------------------------------------------------------===//
6608// X86 Optimization Hooks
6609//===----------------------------------------------------------------------===//
6610
Dan Gohman475871a2008-07-27 21:46:04 +00006611void X86TargetLowering::computeMaskedBitsForTargetNode(const SDValue Op,
Dan Gohman977a76f2008-02-13 22:28:48 +00006612 const APInt &Mask,
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006613 APInt &KnownZero,
6614 APInt &KnownOne,
Dan Gohmanea859be2007-06-22 14:59:07 +00006615 const SelectionDAG &DAG,
Nate Begeman368e18d2006-02-16 21:11:51 +00006616 unsigned Depth) const {
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006617 unsigned Opc = Op.getOpcode();
Evan Cheng865f0602006-04-05 06:11:20 +00006618 assert((Opc >= ISD::BUILTIN_OP_END ||
6619 Opc == ISD::INTRINSIC_WO_CHAIN ||
6620 Opc == ISD::INTRINSIC_W_CHAIN ||
6621 Opc == ISD::INTRINSIC_VOID) &&
6622 "Should use MaskedValueIsZero if you don't know whether Op"
6623 " is a target node!");
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006624
Dan Gohmanf4f92f52008-02-13 23:07:24 +00006625 KnownZero = KnownOne = APInt(Mask.getBitWidth(), 0); // Don't know anything.
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006626 switch (Opc) {
Evan Cheng865f0602006-04-05 06:11:20 +00006627 default: break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006628 case X86ISD::SETCC:
Dan Gohmanfd29e0e2008-02-13 00:35:47 +00006629 KnownZero |= APInt::getHighBitsSet(Mask.getBitWidth(),
6630 Mask.getBitWidth() - 1);
Nate Begeman368e18d2006-02-16 21:11:51 +00006631 break;
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006632 }
Evan Cheng3a03ebb2005-12-21 23:05:39 +00006633}
Chris Lattner259e97c2006-01-31 19:43:35 +00006634
Evan Cheng206ee9d2006-07-07 08:33:52 +00006635/// isGAPlusOffset - Returns true (and the GlobalValue and the offset) if the
Evan Chengad4196b2008-05-12 19:56:52 +00006636/// node is a GlobalAddress + offset.
6637bool X86TargetLowering::isGAPlusOffset(SDNode *N,
6638 GlobalValue* &GA, int64_t &Offset) const{
6639 if (N->getOpcode() == X86ISD::Wrapper) {
6640 if (isa<GlobalAddressSDNode>(N->getOperand(0))) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006641 GA = cast<GlobalAddressSDNode>(N->getOperand(0))->getGlobal();
6642 return true;
6643 }
Evan Cheng206ee9d2006-07-07 08:33:52 +00006644 }
Evan Chengad4196b2008-05-12 19:56:52 +00006645 return TargetLowering::isGAPlusOffset(N, GA, Offset);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006646}
6647
Evan Chengad4196b2008-05-12 19:56:52 +00006648static bool isBaseAlignmentOfN(unsigned N, SDNode *Base,
6649 const TargetLowering &TLI) {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006650 GlobalValue *GV;
Nick Lewycky916a9f02008-02-02 08:29:58 +00006651 int64_t Offset = 0;
Evan Chengad4196b2008-05-12 19:56:52 +00006652 if (TLI.isGAPlusOffset(Base, GV, Offset))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006653 return (GV->getAlignment() >= N && (Offset % N) == 0);
Chris Lattnerba96fbc2008-01-26 20:07:42 +00006654 // DAG combine handles the stack object case.
Evan Cheng206ee9d2006-07-07 08:33:52 +00006655 return false;
6656}
6657
Dan Gohman475871a2008-07-27 21:46:04 +00006658static bool EltsFromConsecutiveLoads(SDNode *N, SDValue PermMask,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006659 unsigned NumElems, MVT EVT,
Evan Chengad4196b2008-05-12 19:56:52 +00006660 SDNode *&Base,
6661 SelectionDAG &DAG, MachineFrameInfo *MFI,
6662 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006663 Base = NULL;
6664 for (unsigned i = 0; i < NumElems; ++i) {
Dan Gohman475871a2008-07-27 21:46:04 +00006665 SDValue Idx = PermMask.getOperand(i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006666 if (Idx.getOpcode() == ISD::UNDEF) {
6667 if (!Base)
6668 return false;
6669 continue;
6670 }
6671
Dan Gohman475871a2008-07-27 21:46:04 +00006672 SDValue Elt = DAG.getShuffleScalarElt(N, i);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006673 if (!Elt.Val ||
6674 (Elt.getOpcode() != ISD::UNDEF && !ISD::isNON_EXTLoad(Elt.Val)))
6675 return false;
6676 if (!Base) {
6677 Base = Elt.Val;
Evan Cheng50d9e722008-05-10 06:46:49 +00006678 if (Base->getOpcode() == ISD::UNDEF)
6679 return false;
Evan Cheng7e2ff772008-05-08 00:57:18 +00006680 continue;
6681 }
6682 if (Elt.getOpcode() == ISD::UNDEF)
6683 continue;
6684
Evan Chengad4196b2008-05-12 19:56:52 +00006685 if (!TLI.isConsecutiveLoad(Elt.Val, Base,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006686 EVT.getSizeInBits()/8, i, MFI))
Evan Cheng7e2ff772008-05-08 00:57:18 +00006687 return false;
6688 }
6689 return true;
6690}
Evan Cheng206ee9d2006-07-07 08:33:52 +00006691
6692/// PerformShuffleCombine - Combine a vector_shuffle that is equal to
6693/// build_vector load1, load2, load3, load4, <0, 1, 2, 3> into a 128-bit load
6694/// if the load addresses are consecutive, non-overlapping, and in the right
6695/// order.
Dan Gohman475871a2008-07-27 21:46:04 +00006696static SDValue PerformShuffleCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006697 const TargetLowering &TLI) {
Evan Cheng7e2ff772008-05-08 00:57:18 +00006698 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
Duncan Sands83ec4b62008-06-06 12:08:01 +00006699 MVT VT = N->getValueType(0);
6700 MVT EVT = VT.getVectorElementType();
Dan Gohman475871a2008-07-27 21:46:04 +00006701 SDValue PermMask = N->getOperand(2);
Evan Cheng71f489d2008-05-05 22:12:23 +00006702 unsigned NumElems = PermMask.getNumOperands();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006703 SDNode *Base = NULL;
Evan Chengad4196b2008-05-12 19:56:52 +00006704 if (!EltsFromConsecutiveLoads(N, PermMask, NumElems, EVT, Base,
6705 DAG, MFI, TLI))
Dan Gohman475871a2008-07-27 21:46:04 +00006706 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006707
Dan Gohmand3006222007-07-27 17:16:43 +00006708 LoadSDNode *LD = cast<LoadSDNode>(Base);
Evan Chengad4196b2008-05-12 19:56:52 +00006709 if (isBaseAlignmentOfN(16, Base->getOperand(1).Val, TLI))
Evan Cheng466685d2006-10-09 20:57:25 +00006710 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
Dan Gohmand3006222007-07-27 17:16:43 +00006711 LD->getSrcValueOffset(), LD->isVolatile());
Evan Cheng71f489d2008-05-05 22:12:23 +00006712 return DAG.getLoad(VT, LD->getChain(), LD->getBasePtr(), LD->getSrcValue(),
6713 LD->getSrcValueOffset(), LD->isVolatile(),
6714 LD->getAlignment());
Evan Cheng206ee9d2006-07-07 08:33:52 +00006715}
6716
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006717/// PerformBuildVectorCombine - build_vector 0,(load i64 / f64) -> movq / movsd.
Dan Gohman475871a2008-07-27 21:46:04 +00006718static SDValue PerformBuildVectorCombine(SDNode *N, SelectionDAG &DAG,
Evan Chengad4196b2008-05-12 19:56:52 +00006719 const X86Subtarget *Subtarget,
6720 const TargetLowering &TLI) {
Evan Chengf26ffe92008-05-29 08:22:04 +00006721 unsigned NumOps = N->getNumOperands();
6722
Evan Chengd880b972008-05-09 21:53:03 +00006723 // Ignore single operand BUILD_VECTOR.
Evan Chengf26ffe92008-05-29 08:22:04 +00006724 if (NumOps == 1)
Dan Gohman475871a2008-07-27 21:46:04 +00006725 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006726
Duncan Sands83ec4b62008-06-06 12:08:01 +00006727 MVT VT = N->getValueType(0);
6728 MVT EVT = VT.getVectorElementType();
Evan Chengd880b972008-05-09 21:53:03 +00006729 if ((EVT != MVT::i64 && EVT != MVT::f64) || Subtarget->is64Bit())
6730 // We are looking for load i64 and zero extend. We want to transform
6731 // it before legalizer has a chance to expand it. Also look for i64
6732 // BUILD_PAIR bit casted to f64.
Dan Gohman475871a2008-07-27 21:46:04 +00006733 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006734 // This must be an insertion into a zero vector.
Dan Gohman475871a2008-07-27 21:46:04 +00006735 SDValue HighElt = N->getOperand(1);
Evan Cheng25210da2008-05-10 00:58:41 +00006736 if (!isZeroNode(HighElt))
Dan Gohman475871a2008-07-27 21:46:04 +00006737 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006738
6739 // Value must be a load.
Evan Chengd880b972008-05-09 21:53:03 +00006740 SDNode *Base = N->getOperand(0).Val;
6741 if (!isa<LoadSDNode>(Base)) {
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006742 if (Base->getOpcode() != ISD::BIT_CONVERT)
Dan Gohman475871a2008-07-27 21:46:04 +00006743 return SDValue();
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006744 Base = Base->getOperand(0).Val;
6745 if (!isa<LoadSDNode>(Base))
Dan Gohman475871a2008-07-27 21:46:04 +00006746 return SDValue();
Evan Chengd880b972008-05-09 21:53:03 +00006747 }
Evan Chengd880b972008-05-09 21:53:03 +00006748
6749 // Transform it into VZEXT_LOAD addr.
Evan Cheng9bfa03c2008-05-12 23:04:07 +00006750 LoadSDNode *LD = cast<LoadSDNode>(Base);
Nate Begemanf7333bf2008-05-28 00:24:25 +00006751
6752 // Load must not be an extload.
6753 if (LD->getExtensionType() != ISD::NON_EXTLOAD)
Dan Gohman475871a2008-07-27 21:46:04 +00006754 return SDValue();
Nate Begemanf7333bf2008-05-28 00:24:25 +00006755
Evan Chengd880b972008-05-09 21:53:03 +00006756 return DAG.getNode(X86ISD::VZEXT_LOAD, VT, LD->getChain(), LD->getBasePtr());
6757}
6758
Chris Lattner83e6c992006-10-04 06:57:07 +00006759/// PerformSELECTCombine - Do target-specific dag combines on SELECT nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006760static SDValue PerformSELECTCombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner83e6c992006-10-04 06:57:07 +00006761 const X86Subtarget *Subtarget) {
Dan Gohman475871a2008-07-27 21:46:04 +00006762 SDValue Cond = N->getOperand(0);
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006763
Chris Lattner83e6c992006-10-04 06:57:07 +00006764 // If we have SSE[12] support, try to form min/max nodes.
6765 if (Subtarget->hasSSE2() &&
6766 (N->getValueType(0) == MVT::f32 || N->getValueType(0) == MVT::f64)) {
6767 if (Cond.getOpcode() == ISD::SETCC) {
6768 // Get the LHS/RHS of the select.
Dan Gohman475871a2008-07-27 21:46:04 +00006769 SDValue LHS = N->getOperand(1);
6770 SDValue RHS = N->getOperand(2);
Chris Lattner83e6c992006-10-04 06:57:07 +00006771 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006772
Evan Cheng8ca29322006-11-10 21:43:37 +00006773 unsigned Opcode = 0;
Chris Lattner83e6c992006-10-04 06:57:07 +00006774 if (LHS == Cond.getOperand(0) && RHS == Cond.getOperand(1)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006775 switch (CC) {
6776 default: break;
6777 case ISD::SETOLE: // (X <= Y) ? X : Y -> min
6778 case ISD::SETULE:
6779 case ISD::SETLE:
6780 if (!UnsafeFPMath) break;
6781 // FALL THROUGH.
6782 case ISD::SETOLT: // (X olt/lt Y) ? X : Y -> min
6783 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006784 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006785 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006786
Chris Lattner1907a7b2006-10-05 04:11:26 +00006787 case ISD::SETOGT: // (X > Y) ? X : Y -> max
6788 case ISD::SETUGT:
6789 case ISD::SETGT:
6790 if (!UnsafeFPMath) break;
6791 // FALL THROUGH.
6792 case ISD::SETUGE: // (X uge/ge Y) ? X : Y -> max
6793 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006794 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006795 break;
6796 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006797 } else if (LHS == Cond.getOperand(1) && RHS == Cond.getOperand(0)) {
Chris Lattner1907a7b2006-10-05 04:11:26 +00006798 switch (CC) {
6799 default: break;
6800 case ISD::SETOGT: // (X > Y) ? Y : X -> min
6801 case ISD::SETUGT:
6802 case ISD::SETGT:
6803 if (!UnsafeFPMath) break;
6804 // FALL THROUGH.
6805 case ISD::SETUGE: // (X uge/ge Y) ? Y : X -> min
6806 case ISD::SETGE:
Evan Cheng8ca29322006-11-10 21:43:37 +00006807 Opcode = X86ISD::FMIN;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006808 break;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006809
Chris Lattner1907a7b2006-10-05 04:11:26 +00006810 case ISD::SETOLE: // (X <= Y) ? Y : X -> max
6811 case ISD::SETULE:
6812 case ISD::SETLE:
6813 if (!UnsafeFPMath) break;
6814 // FALL THROUGH.
6815 case ISD::SETOLT: // (X olt/lt Y) ? Y : X -> max
6816 case ISD::SETLT:
Evan Cheng8ca29322006-11-10 21:43:37 +00006817 Opcode = X86ISD::FMAX;
Chris Lattner1907a7b2006-10-05 04:11:26 +00006818 break;
6819 }
Chris Lattner83e6c992006-10-04 06:57:07 +00006820 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006821
Evan Cheng8ca29322006-11-10 21:43:37 +00006822 if (Opcode)
6823 return DAG.getNode(Opcode, N->getValueType(0), LHS, RHS);
Chris Lattner83e6c992006-10-04 06:57:07 +00006824 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00006825
Chris Lattner83e6c992006-10-04 06:57:07 +00006826 }
6827
Dan Gohman475871a2008-07-27 21:46:04 +00006828 return SDValue();
Chris Lattner83e6c992006-10-04 06:57:07 +00006829}
6830
Chris Lattner149a4e52008-02-22 02:09:43 +00006831/// PerformSTORECombine - Do target-specific dag combines on STORE nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006832static SDValue PerformSTORECombine(SDNode *N, SelectionDAG &DAG,
Chris Lattner149a4e52008-02-22 02:09:43 +00006833 const X86Subtarget *Subtarget) {
6834 // Turn load->store of MMX types into GPR load/stores. This avoids clobbering
6835 // the FP state in cases where an emms may be missing.
Dale Johannesen079f2a62008-02-25 19:20:14 +00006836 // A preferable solution to the general problem is to figure out the right
6837 // places to insert EMMS. This qualifies as a quick hack.
Evan Cheng7e2ff772008-05-08 00:57:18 +00006838 StoreSDNode *St = cast<StoreSDNode>(N);
Duncan Sands83ec4b62008-06-06 12:08:01 +00006839 if (St->getValue().getValueType().isVector() &&
6840 St->getValue().getValueType().getSizeInBits() == 64 &&
Dale Johannesen079f2a62008-02-25 19:20:14 +00006841 isa<LoadSDNode>(St->getValue()) &&
6842 !cast<LoadSDNode>(St->getValue())->isVolatile() &&
6843 St->getChain().hasOneUse() && !St->isVolatile()) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006844 SDNode* LdVal = St->getValue().Val;
Dale Johannesen079f2a62008-02-25 19:20:14 +00006845 LoadSDNode *Ld = 0;
6846 int TokenFactorIndex = -1;
Dan Gohman475871a2008-07-27 21:46:04 +00006847 SmallVector<SDValue, 8> Ops;
Dale Johannesen079f2a62008-02-25 19:20:14 +00006848 SDNode* ChainVal = St->getChain().Val;
6849 // Must be a store of a load. We currently handle two cases: the load
6850 // is a direct child, and it's under an intervening TokenFactor. It is
6851 // possible to dig deeper under nested TokenFactors.
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006852 if (ChainVal == LdVal)
Dale Johannesen079f2a62008-02-25 19:20:14 +00006853 Ld = cast<LoadSDNode>(St->getChain());
6854 else if (St->getValue().hasOneUse() &&
6855 ChainVal->getOpcode() == ISD::TokenFactor) {
6856 for (unsigned i=0, e = ChainVal->getNumOperands(); i != e; ++i) {
Dale Johannesen14e2ea92008-02-25 22:29:22 +00006857 if (ChainVal->getOperand(i).Val == LdVal) {
Dale Johannesen079f2a62008-02-25 19:20:14 +00006858 TokenFactorIndex = i;
6859 Ld = cast<LoadSDNode>(St->getValue());
6860 } else
6861 Ops.push_back(ChainVal->getOperand(i));
6862 }
6863 }
6864 if (Ld) {
6865 // If we are a 64-bit capable x86, lower to a single movq load/store pair.
6866 if (Subtarget->is64Bit()) {
Dan Gohman475871a2008-07-27 21:46:04 +00006867 SDValue NewLd = DAG.getLoad(MVT::i64, Ld->getChain(),
Dale Johannesen079f2a62008-02-25 19:20:14 +00006868 Ld->getBasePtr(), Ld->getSrcValue(),
6869 Ld->getSrcValueOffset(), Ld->isVolatile(),
6870 Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006871 SDValue NewChain = NewLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006872 if (TokenFactorIndex != -1) {
Dan Gohmand4a2ad32008-03-28 23:45:16 +00006873 Ops.push_back(NewChain);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006874 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6875 Ops.size());
6876 }
6877 return DAG.getStore(NewChain, NewLd, St->getBasePtr(),
6878 St->getSrcValue(), St->getSrcValueOffset(),
6879 St->isVolatile(), St->getAlignment());
6880 }
6881
6882 // Otherwise, lower to two 32-bit copies.
Dan Gohman475871a2008-07-27 21:46:04 +00006883 SDValue LoAddr = Ld->getBasePtr();
6884 SDValue HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006885 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006886
Dan Gohman475871a2008-07-27 21:46:04 +00006887 SDValue LoLd = DAG.getLoad(MVT::i32, Ld->getChain(), LoAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006888 Ld->getSrcValue(), Ld->getSrcValueOffset(),
6889 Ld->isVolatile(), Ld->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006890 SDValue HiLd = DAG.getLoad(MVT::i32, Ld->getChain(), HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006891 Ld->getSrcValue(), Ld->getSrcValueOffset()+4,
6892 Ld->isVolatile(),
6893 MinAlign(Ld->getAlignment(), 4));
6894
Dan Gohman475871a2008-07-27 21:46:04 +00006895 SDValue NewChain = LoLd.getValue(1);
Dale Johannesen079f2a62008-02-25 19:20:14 +00006896 if (TokenFactorIndex != -1) {
6897 Ops.push_back(LoLd);
6898 Ops.push_back(HiLd);
6899 NewChain = DAG.getNode(ISD::TokenFactor, MVT::Other, &Ops[0],
6900 Ops.size());
6901 }
6902
6903 LoAddr = St->getBasePtr();
6904 HiAddr = DAG.getNode(ISD::ADD, MVT::i32, LoAddr,
Duncan Sands83ec4b62008-06-06 12:08:01 +00006905 DAG.getConstant(4, MVT::i32));
Dale Johannesen079f2a62008-02-25 19:20:14 +00006906
Dan Gohman475871a2008-07-27 21:46:04 +00006907 SDValue LoSt = DAG.getStore(NewChain, LoLd, LoAddr,
Chris Lattner149a4e52008-02-22 02:09:43 +00006908 St->getSrcValue(), St->getSrcValueOffset(),
6909 St->isVolatile(), St->getAlignment());
Dan Gohman475871a2008-07-27 21:46:04 +00006910 SDValue HiSt = DAG.getStore(NewChain, HiLd, HiAddr,
Dale Johannesen079f2a62008-02-25 19:20:14 +00006911 St->getSrcValue(), St->getSrcValueOffset()+4,
6912 St->isVolatile(),
6913 MinAlign(St->getAlignment(), 4));
6914 return DAG.getNode(ISD::TokenFactor, MVT::Other, LoSt, HiSt);
Chris Lattner149a4e52008-02-22 02:09:43 +00006915 }
Chris Lattner149a4e52008-02-22 02:09:43 +00006916 }
Dan Gohman475871a2008-07-27 21:46:04 +00006917 return SDValue();
Chris Lattner149a4e52008-02-22 02:09:43 +00006918}
6919
Chris Lattner6cf73262008-01-25 06:14:17 +00006920/// PerformFORCombine - Do target-specific dag combines on X86ISD::FOR and
6921/// X86ISD::FXOR nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006922static SDValue PerformFORCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattner6cf73262008-01-25 06:14:17 +00006923 assert(N->getOpcode() == X86ISD::FOR || N->getOpcode() == X86ISD::FXOR);
6924 // F[X]OR(0.0, x) -> x
6925 // F[X]OR(x, 0.0) -> x
Chris Lattneraf723b92008-01-25 05:46:26 +00006926 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6927 if (C->getValueAPF().isPosZero())
6928 return N->getOperand(1);
6929 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6930 if (C->getValueAPF().isPosZero())
6931 return N->getOperand(0);
Dan Gohman475871a2008-07-27 21:46:04 +00006932 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00006933}
6934
6935/// PerformFANDCombine - Do target-specific dag combines on X86ISD::FAND nodes.
Dan Gohman475871a2008-07-27 21:46:04 +00006936static SDValue PerformFANDCombine(SDNode *N, SelectionDAG &DAG) {
Chris Lattneraf723b92008-01-25 05:46:26 +00006937 // FAND(0.0, x) -> 0.0
6938 // FAND(x, 0.0) -> 0.0
6939 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(0)))
6940 if (C->getValueAPF().isPosZero())
6941 return N->getOperand(0);
6942 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N->getOperand(1)))
6943 if (C->getValueAPF().isPosZero())
6944 return N->getOperand(1);
Dan Gohman475871a2008-07-27 21:46:04 +00006945 return SDValue();
Chris Lattneraf723b92008-01-25 05:46:26 +00006946}
6947
Chris Lattner83e6c992006-10-04 06:57:07 +00006948
Dan Gohman475871a2008-07-27 21:46:04 +00006949SDValue X86TargetLowering::PerformDAGCombine(SDNode *N,
Evan Cheng206ee9d2006-07-07 08:33:52 +00006950 DAGCombinerInfo &DCI) const {
Evan Cheng206ee9d2006-07-07 08:33:52 +00006951 SelectionDAG &DAG = DCI.DAG;
6952 switch (N->getOpcode()) {
6953 default: break;
Evan Chengad4196b2008-05-12 19:56:52 +00006954 case ISD::VECTOR_SHUFFLE: return PerformShuffleCombine(N, DAG, *this);
6955 case ISD::BUILD_VECTOR:
6956 return PerformBuildVectorCombine(N, DAG, Subtarget, *this);
Chris Lattneraf723b92008-01-25 05:46:26 +00006957 case ISD::SELECT: return PerformSELECTCombine(N, DAG, Subtarget);
Evan Cheng7e2ff772008-05-08 00:57:18 +00006958 case ISD::STORE: return PerformSTORECombine(N, DAG, Subtarget);
Chris Lattner6cf73262008-01-25 06:14:17 +00006959 case X86ISD::FXOR:
Chris Lattneraf723b92008-01-25 05:46:26 +00006960 case X86ISD::FOR: return PerformFORCombine(N, DAG);
6961 case X86ISD::FAND: return PerformFANDCombine(N, DAG);
Evan Cheng206ee9d2006-07-07 08:33:52 +00006962 }
6963
Dan Gohman475871a2008-07-27 21:46:04 +00006964 return SDValue();
Evan Cheng206ee9d2006-07-07 08:33:52 +00006965}
6966
Evan Cheng60c07e12006-07-05 22:17:51 +00006967//===----------------------------------------------------------------------===//
6968// X86 Inline Assembly Support
6969//===----------------------------------------------------------------------===//
6970
Chris Lattnerf4dff842006-07-11 02:54:03 +00006971/// getConstraintType - Given a constraint letter, return the type of
6972/// constraint it is for this target.
6973X86TargetLowering::ConstraintType
Chris Lattner4234f572007-03-25 02:14:49 +00006974X86TargetLowering::getConstraintType(const std::string &Constraint) const {
6975 if (Constraint.size() == 1) {
6976 switch (Constraint[0]) {
6977 case 'A':
Chris Lattnerfce84ac2008-03-11 19:06:29 +00006978 case 'f':
Chris Lattner4234f572007-03-25 02:14:49 +00006979 case 'r':
6980 case 'R':
6981 case 'l':
6982 case 'q':
6983 case 'Q':
6984 case 'x':
Dale Johannesen2ffbcac2008-04-01 00:57:48 +00006985 case 'y':
Chris Lattner4234f572007-03-25 02:14:49 +00006986 case 'Y':
6987 return C_RegisterClass;
6988 default:
6989 break;
6990 }
Chris Lattnerf4dff842006-07-11 02:54:03 +00006991 }
Chris Lattner4234f572007-03-25 02:14:49 +00006992 return TargetLowering::getConstraintType(Constraint);
Chris Lattnerf4dff842006-07-11 02:54:03 +00006993}
6994
Dale Johannesenba2a0b92008-01-29 02:21:21 +00006995/// LowerXConstraint - try to replace an X constraint, which matches anything,
6996/// with another that has more specific requirements based on the type of the
6997/// corresponding operand.
Chris Lattner5e764232008-04-26 23:02:14 +00006998const char *X86TargetLowering::
Duncan Sands83ec4b62008-06-06 12:08:01 +00006999LowerXConstraint(MVT ConstraintVT) const {
Chris Lattner5e764232008-04-26 23:02:14 +00007000 // FP X constraints get lowered to SSE1/2 registers if available, otherwise
7001 // 'f' like normal targets.
Duncan Sands83ec4b62008-06-06 12:08:01 +00007002 if (ConstraintVT.isFloatingPoint()) {
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007003 if (Subtarget->hasSSE2())
Chris Lattner5e764232008-04-26 23:02:14 +00007004 return "Y";
7005 if (Subtarget->hasSSE1())
7006 return "x";
7007 }
7008
7009 return TargetLowering::LowerXConstraint(ConstraintVT);
Dale Johannesenba2a0b92008-01-29 02:21:21 +00007010}
7011
Chris Lattner48884cd2007-08-25 00:47:38 +00007012/// LowerAsmOperandForConstraint - Lower the specified operand into the Ops
7013/// vector. If it is invalid, don't add anything to Ops.
Dan Gohman475871a2008-07-27 21:46:04 +00007014void X86TargetLowering::LowerAsmOperandForConstraint(SDValue Op,
Chris Lattner48884cd2007-08-25 00:47:38 +00007015 char Constraint,
Dan Gohman475871a2008-07-27 21:46:04 +00007016 std::vector<SDValue>&Ops,
Chris Lattner5e764232008-04-26 23:02:14 +00007017 SelectionDAG &DAG) const {
Dan Gohman475871a2008-07-27 21:46:04 +00007018 SDValue Result(0, 0);
Chris Lattner48884cd2007-08-25 00:47:38 +00007019
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007020 switch (Constraint) {
7021 default: break;
Devang Patel84f7fd22007-03-17 00:13:28 +00007022 case 'I':
Chris Lattner188b9fe2007-03-25 01:57:35 +00007023 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00007024 if (C->getValue() <= 31) {
7025 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7026 break;
7027 }
Devang Patel84f7fd22007-03-17 00:13:28 +00007028 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007029 return;
Chris Lattner188b9fe2007-03-25 01:57:35 +00007030 case 'N':
7031 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op)) {
Chris Lattner48884cd2007-08-25 00:47:38 +00007032 if (C->getValue() <= 255) {
7033 Result = DAG.getTargetConstant(C->getValue(), Op.getValueType());
7034 break;
7035 }
Chris Lattner188b9fe2007-03-25 01:57:35 +00007036 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007037 return;
Chris Lattnerdc43a882007-05-03 16:52:29 +00007038 case 'i': {
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007039 // Literal immediates are always ok.
Chris Lattner48884cd2007-08-25 00:47:38 +00007040 if (ConstantSDNode *CST = dyn_cast<ConstantSDNode>(Op)) {
7041 Result = DAG.getTargetConstant(CST->getValue(), Op.getValueType());
7042 break;
7043 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007044
Chris Lattnerdc43a882007-05-03 16:52:29 +00007045 // If we are in non-pic codegen mode, we allow the address of a global (with
7046 // an optional displacement) to be used with 'i'.
7047 GlobalAddressSDNode *GA = dyn_cast<GlobalAddressSDNode>(Op);
7048 int64_t Offset = 0;
7049
7050 // Match either (GA) or (GA+C)
7051 if (GA) {
7052 Offset = GA->getOffset();
7053 } else if (Op.getOpcode() == ISD::ADD) {
7054 ConstantSDNode *C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7055 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7056 if (C && GA) {
7057 Offset = GA->getOffset()+C->getValue();
7058 } else {
7059 C = dyn_cast<ConstantSDNode>(Op.getOperand(1));
7060 GA = dyn_cast<GlobalAddressSDNode>(Op.getOperand(0));
7061 if (C && GA)
7062 Offset = GA->getOffset()+C->getValue();
7063 else
7064 C = 0, GA = 0;
7065 }
7066 }
7067
7068 if (GA) {
7069 // If addressing this global requires a load (e.g. in PIC mode), we can't
7070 // match.
7071 if (Subtarget->GVRequiresExtraLoad(GA->getGlobal(), getTargetMachine(),
7072 false))
Chris Lattner48884cd2007-08-25 00:47:38 +00007073 return;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007074
Chris Lattnerdc43a882007-05-03 16:52:29 +00007075 Op = DAG.getTargetGlobalAddress(GA->getGlobal(), GA->getValueType(0),
7076 Offset);
Chris Lattner48884cd2007-08-25 00:47:38 +00007077 Result = Op;
7078 break;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007079 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007080
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007081 // Otherwise, not valid for this mode.
Chris Lattner48884cd2007-08-25 00:47:38 +00007082 return;
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007083 }
Chris Lattnerdc43a882007-05-03 16:52:29 +00007084 }
Chris Lattner48884cd2007-08-25 00:47:38 +00007085
7086 if (Result.Val) {
7087 Ops.push_back(Result);
7088 return;
7089 }
7090 return TargetLowering::LowerAsmOperandForConstraint(Op, Constraint, Ops, DAG);
Chris Lattner22aaf1d2006-10-31 20:13:11 +00007091}
7092
Chris Lattner259e97c2006-01-31 19:43:35 +00007093std::vector<unsigned> X86TargetLowering::
Chris Lattner1efa40f2006-02-22 00:56:39 +00007094getRegClassForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007095 MVT VT) const {
Chris Lattner259e97c2006-01-31 19:43:35 +00007096 if (Constraint.size() == 1) {
7097 // FIXME: not handling fp-stack yet!
Chris Lattner259e97c2006-01-31 19:43:35 +00007098 switch (Constraint[0]) { // GCC X86 Constraint Letters
Chris Lattnerf4dff842006-07-11 02:54:03 +00007099 default: break; // Unknown constraint letter
7100 case 'A': // EAX/EDX
7101 if (VT == MVT::i32 || VT == MVT::i64)
7102 return make_vector<unsigned>(X86::EAX, X86::EDX, 0);
7103 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00007104 case 'q': // Q_REGS (GENERAL_REGS in 64-bit mode)
7105 case 'Q': // Q_REGS
Chris Lattner80a7ecc2006-05-06 00:29:37 +00007106 if (VT == MVT::i32)
7107 return make_vector<unsigned>(X86::EAX, X86::EDX, X86::ECX, X86::EBX, 0);
7108 else if (VT == MVT::i16)
7109 return make_vector<unsigned>(X86::AX, X86::DX, X86::CX, X86::BX, 0);
7110 else if (VT == MVT::i8)
Evan Cheng12914382007-08-13 23:27:11 +00007111 return make_vector<unsigned>(X86::AL, X86::DL, X86::CL, X86::BL, 0);
Chris Lattner03e6c702007-11-04 06:51:12 +00007112 else if (VT == MVT::i64)
7113 return make_vector<unsigned>(X86::RAX, X86::RDX, X86::RCX, X86::RBX, 0);
7114 break;
Chris Lattner259e97c2006-01-31 19:43:35 +00007115 }
7116 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007117
Chris Lattner1efa40f2006-02-22 00:56:39 +00007118 return std::vector<unsigned>();
Chris Lattner259e97c2006-01-31 19:43:35 +00007119}
Chris Lattnerf76d1802006-07-31 23:26:50 +00007120
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007121std::pair<unsigned, const TargetRegisterClass*>
Chris Lattnerf76d1802006-07-31 23:26:50 +00007122X86TargetLowering::getRegForInlineAsmConstraint(const std::string &Constraint,
Duncan Sands83ec4b62008-06-06 12:08:01 +00007123 MVT VT) const {
Chris Lattnerad043e82007-04-09 05:11:28 +00007124 // First, see if this is a constraint that directly corresponds to an LLVM
7125 // register class.
7126 if (Constraint.size() == 1) {
7127 // GCC Constraint Letters
7128 switch (Constraint[0]) {
7129 default: break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00007130 case 'r': // GENERAL_REGS
7131 case 'R': // LEGACY_REGS
7132 case 'l': // INDEX_REGS
7133 if (VT == MVT::i64 && Subtarget->is64Bit())
7134 return std::make_pair(0U, X86::GR64RegisterClass);
7135 if (VT == MVT::i32)
7136 return std::make_pair(0U, X86::GR32RegisterClass);
7137 else if (VT == MVT::i16)
7138 return std::make_pair(0U, X86::GR16RegisterClass);
7139 else if (VT == MVT::i8)
7140 return std::make_pair(0U, X86::GR8RegisterClass);
7141 break;
Chris Lattnerfce84ac2008-03-11 19:06:29 +00007142 case 'f': // FP Stack registers.
7143 // If SSE is enabled for this VT, use f80 to ensure the isel moves the
7144 // value to the correct fpstack register class.
7145 if (VT == MVT::f32 && !isScalarFPTypeInSSEReg(VT))
7146 return std::make_pair(0U, X86::RFP32RegisterClass);
7147 if (VT == MVT::f64 && !isScalarFPTypeInSSEReg(VT))
7148 return std::make_pair(0U, X86::RFP64RegisterClass);
7149 return std::make_pair(0U, X86::RFP80RegisterClass);
Chris Lattner6c284d72007-04-12 04:14:49 +00007150 case 'y': // MMX_REGS if MMX allowed.
7151 if (!Subtarget->hasMMX()) break;
7152 return std::make_pair(0U, X86::VR64RegisterClass);
7153 break;
Chris Lattner0f65cad2007-04-09 05:49:22 +00007154 case 'Y': // SSE_REGS if SSE2 allowed
7155 if (!Subtarget->hasSSE2()) break;
7156 // FALL THROUGH.
7157 case 'x': // SSE_REGS if SSE1 allowed
7158 if (!Subtarget->hasSSE1()) break;
Duncan Sands83ec4b62008-06-06 12:08:01 +00007159
7160 switch (VT.getSimpleVT()) {
Chris Lattner0f65cad2007-04-09 05:49:22 +00007161 default: break;
7162 // Scalar SSE types.
7163 case MVT::f32:
7164 case MVT::i32:
Chris Lattnerad043e82007-04-09 05:11:28 +00007165 return std::make_pair(0U, X86::FR32RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007166 case MVT::f64:
7167 case MVT::i64:
Chris Lattnerad043e82007-04-09 05:11:28 +00007168 return std::make_pair(0U, X86::FR64RegisterClass);
Chris Lattner0f65cad2007-04-09 05:49:22 +00007169 // Vector types.
Chris Lattner0f65cad2007-04-09 05:49:22 +00007170 case MVT::v16i8:
7171 case MVT::v8i16:
7172 case MVT::v4i32:
7173 case MVT::v2i64:
7174 case MVT::v4f32:
7175 case MVT::v2f64:
7176 return std::make_pair(0U, X86::VR128RegisterClass);
7177 }
Chris Lattnerad043e82007-04-09 05:11:28 +00007178 break;
7179 }
7180 }
7181
Chris Lattnerf76d1802006-07-31 23:26:50 +00007182 // Use the default implementation in TargetLowering to convert the register
7183 // constraint into a member of a register class.
7184 std::pair<unsigned, const TargetRegisterClass*> Res;
7185 Res = TargetLowering::getRegForInlineAsmConstraint(Constraint, VT);
Chris Lattner1a60aa72006-10-31 19:42:44 +00007186
7187 // Not found as a standard register?
7188 if (Res.second == 0) {
7189 // GCC calls "st(0)" just plain "st".
7190 if (StringsEqualNoCase("{st}", Constraint)) {
7191 Res.first = X86::ST0;
Chris Lattner9b4baf12007-09-24 05:27:37 +00007192 Res.second = X86::RFP80RegisterClass;
Chris Lattner1a60aa72006-10-31 19:42:44 +00007193 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007194
Chris Lattner1a60aa72006-10-31 19:42:44 +00007195 return Res;
7196 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007197
Chris Lattnerf76d1802006-07-31 23:26:50 +00007198 // Otherwise, check to see if this is a register class of the wrong value
7199 // type. For example, we want to map "{ax},i32" -> {eax}, we don't want it to
7200 // turn into {ax},{dx}.
7201 if (Res.second->hasType(VT))
7202 return Res; // Correct type already, nothing to do.
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007203
Chris Lattnerf76d1802006-07-31 23:26:50 +00007204 // All of the single-register GCC register classes map their values onto
7205 // 16-bit register pieces "ax","dx","cx","bx","si","di","bp","sp". If we
7206 // really want an 8-bit or 32-bit register, map to the appropriate register
7207 // class and return the appropriate register.
7208 if (Res.second != X86::GR16RegisterClass)
7209 return Res;
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007210
Chris Lattnerf76d1802006-07-31 23:26:50 +00007211 if (VT == MVT::i8) {
7212 unsigned DestReg = 0;
7213 switch (Res.first) {
7214 default: break;
7215 case X86::AX: DestReg = X86::AL; break;
7216 case X86::DX: DestReg = X86::DL; break;
7217 case X86::CX: DestReg = X86::CL; break;
7218 case X86::BX: DestReg = X86::BL; break;
7219 }
7220 if (DestReg) {
7221 Res.first = DestReg;
7222 Res.second = Res.second = X86::GR8RegisterClass;
7223 }
7224 } else if (VT == MVT::i32) {
7225 unsigned DestReg = 0;
7226 switch (Res.first) {
7227 default: break;
7228 case X86::AX: DestReg = X86::EAX; break;
7229 case X86::DX: DestReg = X86::EDX; break;
7230 case X86::CX: DestReg = X86::ECX; break;
7231 case X86::BX: DestReg = X86::EBX; break;
7232 case X86::SI: DestReg = X86::ESI; break;
7233 case X86::DI: DestReg = X86::EDI; break;
7234 case X86::BP: DestReg = X86::EBP; break;
7235 case X86::SP: DestReg = X86::ESP; break;
7236 }
7237 if (DestReg) {
7238 Res.first = DestReg;
7239 Res.second = Res.second = X86::GR32RegisterClass;
7240 }
Evan Cheng25ab6902006-09-08 06:48:29 +00007241 } else if (VT == MVT::i64) {
7242 unsigned DestReg = 0;
7243 switch (Res.first) {
7244 default: break;
7245 case X86::AX: DestReg = X86::RAX; break;
7246 case X86::DX: DestReg = X86::RDX; break;
7247 case X86::CX: DestReg = X86::RCX; break;
7248 case X86::BX: DestReg = X86::RBX; break;
7249 case X86::SI: DestReg = X86::RSI; break;
7250 case X86::DI: DestReg = X86::RDI; break;
7251 case X86::BP: DestReg = X86::RBP; break;
7252 case X86::SP: DestReg = X86::RSP; break;
7253 }
7254 if (DestReg) {
7255 Res.first = DestReg;
7256 Res.second = Res.second = X86::GR64RegisterClass;
7257 }
Chris Lattnerf76d1802006-07-31 23:26:50 +00007258 }
Anton Korobeynikov12c49af2006-11-21 00:01:06 +00007259
Chris Lattnerf76d1802006-07-31 23:26:50 +00007260 return Res;
7261}